1 // 2 // Copyright (C) 2009 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "update_engine/payload_consumer/file_writer.h" 18 19 #include <errno.h> 20 #include <string.h> 21 #include <unistd.h> 22 23 #include <string> 24 25 #include <brillo/secure_blob.h> 26 #include <gtest/gtest.h> 27 28 #include "update_engine/common/test_utils.h" 29 #include "update_engine/common/utils.h" 30 31 using std::string; 32 33 namespace chromeos_update_engine { 34 35 class FileWriterTest : public ::testing::Test {}; 36 37 TEST(FileWriterTest, SimpleTest) { 38 // Create a uniquely named file for testing. 39 test_utils::ScopedTempFile file("FileWriterTest-XXXXXX"); 40 DirectFileWriter file_writer; 41 EXPECT_EQ(0, 42 file_writer.Open(file.path().c_str(), 43 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, 44 0644)); 45 EXPECT_TRUE(file_writer.Write("test", 4)); 46 brillo::Blob actual_data; 47 EXPECT_TRUE(utils::ReadFile(file.path(), &actual_data)); 48 49 EXPECT_EQ("test", string(actual_data.begin(), actual_data.end())); 50 EXPECT_EQ(0, file_writer.Close()); 51 } 52 53 TEST(FileWriterTest, ErrorTest) { 54 DirectFileWriter file_writer; 55 const string path("/tmp/ENOENT/FileWriterTest"); 56 EXPECT_EQ( 57 -ENOENT, 58 file_writer.Open(path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); 59 } 60 61 TEST(FileWriterTest, WriteErrorTest) { 62 // Create a uniquely named file for testing. 63 test_utils::ScopedTempFile file("FileWriterTest-XXXXXX"); 64 DirectFileWriter file_writer; 65 EXPECT_EQ(0, 66 file_writer.Open(file.path().c_str(), 67 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, 68 0644)); 69 EXPECT_FALSE(file_writer.Write("x", 1)); 70 EXPECT_EQ(0, file_writer.Close()); 71 } 72 73 } // namespace chromeos_update_engine 74