blob: 59cfe2b9ffcd417d69dece09a560f4318ec48cf6 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/file_writer.h"
Alex Deymob5ba9e42014-05-16 13:17:21 -070018
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019#include <errno.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <string.h>
21#include <unistd.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070022
rspangler@google.com49fdf182009-10-10 00:57:34 +000023#include <string>
Alex Deymob5ba9e42014-05-16 13:17:21 -070024
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/secure_blob.h>
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include <gtest/gtest.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/test_utils.h"
29#include "update_engine/common/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000030
31using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000032
33namespace chromeos_update_engine {
34
Amin Hassani008c4582019-01-13 16:22:47 -080035class FileWriterTest : public ::testing::Test {};
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
37TEST(FileWriterTest, SimpleTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070038 // Create a uniquely named file for testing.
Sen Jiang0779a152018-07-02 17:34:56 -070039 test_utils::ScopedTempFile file("FileWriterTest-XXXXXX");
rspangler@google.com49fdf182009-10-10 00:57:34 +000040 DirectFileWriter file_writer;
Sen Jiang0779a152018-07-02 17:34:56 -070041 EXPECT_EQ(0,
42 file_writer.Open(file.path().c_str(),
43 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
44 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080045 EXPECT_TRUE(file_writer.Write("test", 4));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070046 brillo::Blob actual_data;
Sen Jiang0779a152018-07-02 17:34:56 -070047 EXPECT_TRUE(utils::ReadFile(file.path(), &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000048
Sen Jiang0779a152018-07-02 17:34:56 -070049 EXPECT_EQ("test", string(actual_data.begin(), actual_data.end()));
rspangler@google.com49fdf182009-10-10 00:57:34 +000050 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000051}
52
53TEST(FileWriterTest, ErrorTest) {
54 DirectFileWriter file_writer;
55 const string path("/tmp/ENOENT/FileWriterTest");
Amin Hassani008c4582019-01-13 16:22:47 -080056 EXPECT_EQ(
57 -ENOENT,
58 file_writer.Open(path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
rspangler@google.com49fdf182009-10-10 00:57:34 +000059}
60
61TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070062 // Create a uniquely named file for testing.
Sen Jiang0779a152018-07-02 17:34:56 -070063 test_utils::ScopedTempFile file("FileWriterTest-XXXXXX");
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 DirectFileWriter file_writer;
Sen Jiang0779a152018-07-02 17:34:56 -070065 EXPECT_EQ(0,
66 file_writer.Open(file.path().c_str(),
67 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
68 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080069 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 EXPECT_EQ(0, file_writer.Close());
71}
72
rspangler@google.com49fdf182009-10-10 00:57:34 +000073} // namespace chromeos_update_engine