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