| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> |
| 5 #include <string.h> | 6 #include <string.h> |
| 6 #include <unistd.h> | 7 #include <unistd.h> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
| 10 #include "update_engine/file_writer.h" | 11 #include "update_engine/file_writer.h" |
| 11 #include "update_engine/test_utils.h" | 12 #include "update_engine/test_utils.h" |
| 13 #include "update_engine/utils.h" |
| 12 | 14 |
| 13 using std::string; | 15 using std::string; |
| 14 using std::vector; | 16 using std::vector; |
| 15 | 17 |
| 16 namespace chromeos_update_engine { | 18 namespace chromeos_update_engine { |
| 17 | 19 |
| 18 class FileWriterTest : public ::testing::Test { }; | 20 class FileWriterTest : public ::testing::Test { }; |
| 19 | 21 |
| 20 TEST(FileWriterTest, SimpleTest) { | 22 TEST(FileWriterTest, SimpleTest) { |
| 21 DirectFileWriter file_writer; | 23 DirectFileWriter file_writer; |
| 22 const string path("/tmp/FileWriterTest"); | 24 const string path("/tmp/FileWriterTest"); |
| 23 ASSERT_EQ(0, file_writer.Open(path.c_str(), | 25 ASSERT_EQ(0, file_writer.Open(path.c_str(), |
| 24 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, | 26 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, |
| 25 0644)); | 27 0644)); |
| 26 ASSERT_EQ(4, file_writer.Write("test", 4)); | 28 ASSERT_EQ(4, file_writer.Write("test", 4)); |
| 27 vector<char> actual_data = ReadFile(path); | 29 vector<char> actual_data; |
| 30 EXPECT_TRUE(utils::ReadFile(path, &actual_data)); |
| 28 | 31 |
| 29 EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size())); | 32 EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size())); |
| 30 EXPECT_EQ(0, file_writer.Close()); | 33 EXPECT_EQ(0, file_writer.Close()); |
| 31 unlink(path.c_str()); | 34 unlink(path.c_str()); |
| 32 } | 35 } |
| 33 | 36 |
| 34 TEST(FileWriterTest, ErrorTest) { | 37 TEST(FileWriterTest, ErrorTest) { |
| 35 DirectFileWriter file_writer; | 38 DirectFileWriter file_writer; |
| 36 const string path("/tmp/ENOENT/FileWriterTest"); | 39 const string path("/tmp/ENOENT/FileWriterTest"); |
| 37 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(), | 40 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(), |
| 38 O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); | 41 O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); |
| 39 } | 42 } |
| 40 | 43 |
| 41 TEST(FileWriterTest, WriteErrorTest) { | 44 TEST(FileWriterTest, WriteErrorTest) { |
| 42 DirectFileWriter file_writer; | 45 DirectFileWriter file_writer; |
| 43 const string path("/tmp/FileWriterTest"); | 46 const string path("/tmp/FileWriterTest"); |
| 44 EXPECT_EQ(0, file_writer.Open(path.c_str(), | 47 EXPECT_EQ(0, file_writer.Open(path.c_str(), |
| 45 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, | 48 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, |
| 46 0644)); | 49 0644)); |
| 47 EXPECT_EQ(-EBADF, file_writer.Write("x", 1)); | 50 EXPECT_EQ(-EBADF, file_writer.Write("x", 1)); |
| 48 EXPECT_EQ(0, file_writer.Close()); | 51 EXPECT_EQ(0, file_writer.Close()); |
| 49 } | 52 } |
| 50 | 53 |
| 51 | 54 |
| 52 } // namespace chromeos_update_engine | 55 } // namespace chromeos_update_engine |
| OLD | NEW |