Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 17 | #include "update_engine/file_writer.h" |
| 18 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 19 | #include <errno.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <unistd.h> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 22 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 25 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 27 | #include <brillo/secure_blob.h> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 28 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 | #include "update_engine/test_utils.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 30 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | |
| 32 | using std::string; |
| 33 | using std::vector; |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
| 37 | class FileWriterTest : public ::testing::Test { }; |
| 38 | |
| 39 | TEST(FileWriterTest, SimpleTest) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 40 | // Create a uniquely named file for testing. |
| 41 | string path; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 42 | ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr)); |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 43 | ScopedPathUnlinker path_unlinker(path); |
| 44 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 45 | DirectFileWriter file_writer; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 46 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, |
| 48 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 49 | EXPECT_TRUE(file_writer.Write("test", 4)); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 50 | brillo::Blob actual_data; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 51 | EXPECT_TRUE(utils::ReadFile(path, &actual_data)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 53 | EXPECT_FALSE(memcmp("test", actual_data.data(), actual_data.size())); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | EXPECT_EQ(0, file_writer.Close()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST(FileWriterTest, ErrorTest) { |
| 58 | DirectFileWriter file_writer; |
| 59 | const string path("/tmp/ENOENT/FileWriterTest"); |
| 60 | EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(), |
| 61 | O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); |
| 62 | } |
| 63 | |
| 64 | TEST(FileWriterTest, WriteErrorTest) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 65 | // Create a uniquely named file for testing. |
| 66 | string path; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 67 | ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr)); |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 68 | ScopedPathUnlinker path_unlinker(path); |
| 69 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | DirectFileWriter file_writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 71 | EXPECT_EQ(0, file_writer.Open(path.c_str(), |
| 72 | O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, |
| 73 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 74 | EXPECT_FALSE(file_writer.Write("x", 1)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | EXPECT_EQ(0, file_writer.Close()); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | } // namespace chromeos_update_engine |