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 | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/file_writer.h" |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 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> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 24 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/secure_blob.h> |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Alex Deymo | b5ba9e4 | 2014-05-16 13:17:21 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/test_utils.h" |
| 29 | #include "update_engine/common/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | |
| 31 | using std::string; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | |
| 33 | namespace chromeos_update_engine { |
| 34 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 35 | class FileWriterTest : public ::testing::Test {}; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 36 | |
| 37 | TEST(FileWriterTest, SimpleTest) { |
Amin Hassani | ed03b44 | 2020-10-26 17:21:29 -0700 | [diff] [blame^] | 38 | ScopedTempFile file("FileWriterTest-XXXXXX"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | DirectFileWriter file_writer; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 40 | EXPECT_EQ(0, |
| 41 | file_writer.Open(file.path().c_str(), |
| 42 | O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY, |
| 43 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 44 | EXPECT_TRUE(file_writer.Write("test", 4)); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 45 | brillo::Blob actual_data; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 46 | EXPECT_TRUE(utils::ReadFile(file.path(), &actual_data)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 48 | EXPECT_EQ("test", string(actual_data.begin(), actual_data.end())); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 | EXPECT_EQ(0, file_writer.Close()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(FileWriterTest, ErrorTest) { |
| 53 | DirectFileWriter file_writer; |
| 54 | const string path("/tmp/ENOENT/FileWriterTest"); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 55 | EXPECT_EQ( |
| 56 | -ENOENT, |
| 57 | file_writer.Open(path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC, 0644)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(FileWriterTest, WriteErrorTest) { |
Gilad Arnold | cfc836c | 2013-07-22 17:57:21 -0700 | [diff] [blame] | 61 | // Create a uniquely named file for testing. |
Amin Hassani | ed03b44 | 2020-10-26 17:21:29 -0700 | [diff] [blame^] | 62 | ScopedTempFile file("FileWriterTest-XXXXXX"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 63 | DirectFileWriter file_writer; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 64 | EXPECT_EQ(0, |
| 65 | file_writer.Open(file.path().c_str(), |
| 66 | O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY, |
| 67 | 0644)); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 68 | EXPECT_FALSE(file_writer.Write("x", 1)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | EXPECT_EQ(0, file_writer.Close()); |
| 70 | } |
| 71 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 | } // namespace chromeos_update_engine |