rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include "update_engine/file_writer.h" |
| 11 | #include "update_engine/test_utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | using std::vector; |
| 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | class FileWriterTest : public ::testing::Test { }; |
| 19 | |
| 20 | TEST(FileWriterTest, SimpleTest) { |
| 21 | DirectFileWriter file_writer; |
| 22 | const string path("/tmp/FileWriterTest"); |
| 23 | ASSERT_EQ(0, file_writer.Open(path.c_str(), |
| 24 | O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, |
| 25 | 0644)); |
| 26 | ASSERT_EQ(4, file_writer.Write("test", 4)); |
| 27 | vector<char> actual_data = ReadFile(path); |
| 28 | |
| 29 | EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size())); |
| 30 | EXPECT_EQ(0, file_writer.Close()); |
| 31 | unlink(path.c_str()); |
| 32 | } |
| 33 | |
| 34 | TEST(FileWriterTest, ErrorTest) { |
| 35 | DirectFileWriter file_writer; |
| 36 | const string path("/tmp/ENOENT/FileWriterTest"); |
| 37 | EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(), |
| 38 | O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); |
| 39 | } |
| 40 | |
| 41 | TEST(FileWriterTest, WriteErrorTest) { |
| 42 | DirectFileWriter file_writer; |
| 43 | const string path("/tmp/FileWriterTest"); |
| 44 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
| 45 | O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, |
| 46 | 0644)); |
| 47 | EXPECT_EQ(-EBADF, file_writer.Write("x", 1)); |
| 48 | EXPECT_EQ(0, file_writer.Close()); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | } // namespace chromeos_update_engine |