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 | |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 5 | #include "update_engine/file_writer.h" |
| 6 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 7 | #include <errno.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <string.h> |
| 9 | #include <unistd.h> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 10 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 13 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 15 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | #include "update_engine/test_utils.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | class FileWriterTest : public ::testing::Test { }; |
| 25 | |
| 26 | TEST(FileWriterTest, SimpleTest) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 27 | // Create a uniquely named file for testing. |
| 28 | string path; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 29 | ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr)); |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 30 | ScopedPathUnlinker path_unlinker(path); |
| 31 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | DirectFileWriter file_writer; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 33 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, |
| 35 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 36 | EXPECT_TRUE(file_writer.Write("test", 4)); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 37 | vector<char> actual_data; |
| 38 | EXPECT_TRUE(utils::ReadFile(path, &actual_data)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | |
| 40 | EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size())); |
| 41 | EXPECT_EQ(0, file_writer.Close()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(FileWriterTest, ErrorTest) { |
| 45 | DirectFileWriter file_writer; |
| 46 | const string path("/tmp/ENOENT/FileWriterTest"); |
| 47 | EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(), |
| 48 | O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); |
| 49 | } |
| 50 | |
| 51 | TEST(FileWriterTest, WriteErrorTest) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 52 | // Create a uniquely named file for testing. |
| 53 | string path; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 54 | ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr)); |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 55 | ScopedPathUnlinker path_unlinker(path); |
| 56 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | DirectFileWriter file_writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
| 59 | O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, |
| 60 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 61 | EXPECT_FALSE(file_writer.Write("x", 1)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 62 | EXPECT_EQ(0, file_writer.Close()); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | } // namespace chromeos_update_engine |