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) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame^] | 23 | // Create a uniquely named file for testing. |
| 24 | string path; |
| 25 | ASSERT_TRUE(utils::MakeTempFile("/tmp/FileWriterTest-XXXXXX", &path, NULL)); |
| 26 | ScopedPathUnlinker path_unlinker(path); |
| 27 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | DirectFileWriter file_writer; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 29 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, |
| 31 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 32 | EXPECT_TRUE(file_writer.Write("test", 4)); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 33 | vector<char> actual_data; |
| 34 | EXPECT_TRUE(utils::ReadFile(path, &actual_data)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | |
| 36 | EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size())); |
| 37 | EXPECT_EQ(0, file_writer.Close()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | TEST(FileWriterTest, ErrorTest) { |
| 41 | DirectFileWriter file_writer; |
| 42 | const string path("/tmp/ENOENT/FileWriterTest"); |
| 43 | EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(), |
| 44 | O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); |
| 45 | } |
| 46 | |
| 47 | TEST(FileWriterTest, WriteErrorTest) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame^] | 48 | // Create a uniquely named file for testing. |
| 49 | string path; |
| 50 | ASSERT_TRUE(utils::MakeTempFile("/tmp/FileWriterTest-XXXXXX", &path, NULL)); |
| 51 | ScopedPathUnlinker path_unlinker(path); |
| 52 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | DirectFileWriter file_writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
| 55 | O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, |
| 56 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 57 | EXPECT_FALSE(file_writer.Write("x", 1)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | EXPECT_EQ(0, file_writer.Close()); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | } // namespace chromeos_update_engine |