blob: 3b959f309bd8f95623b2a64074c900720c177a8e [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) {
Amin Hassanied03b442020-10-26 17:21:29 -070038 ScopedTempFile file("FileWriterTest-XXXXXX");
rspangler@google.com49fdf182009-10-10 00:57:34 +000039 DirectFileWriter file_writer;
Sen Jiang0779a152018-07-02 17:34:56 -070040 EXPECT_EQ(0,
41 file_writer.Open(file.path().c_str(),
42 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
43 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080044 EXPECT_TRUE(file_writer.Write("test", 4));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070045 brillo::Blob actual_data;
Sen Jiang0779a152018-07-02 17:34:56 -070046 EXPECT_TRUE(utils::ReadFile(file.path(), &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
Sen Jiang0779a152018-07-02 17:34:56 -070048 EXPECT_EQ("test", string(actual_data.begin(), actual_data.end()));
rspangler@google.com49fdf182009-10-10 00:57:34 +000049 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000050}
51
52TEST(FileWriterTest, ErrorTest) {
53 DirectFileWriter file_writer;
54 const string path("/tmp/ENOENT/FileWriterTest");
Amin Hassani008c4582019-01-13 16:22:47 -080055 EXPECT_EQ(
56 -ENOENT,
57 file_writer.Open(path.c_str(), O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
rspangler@google.com49fdf182009-10-10 00:57:34 +000058}
59
60TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070061 // Create a uniquely named file for testing.
Amin Hassanied03b442020-10-26 17:21:29 -070062 ScopedTempFile file("FileWriterTest-XXXXXX");
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 DirectFileWriter file_writer;
Sen Jiang0779a152018-07-02 17:34:56 -070064 EXPECT_EQ(0,
65 file_writer.Open(file.path().c_str(),
66 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
67 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080068 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 EXPECT_EQ(0, file_writer.Close());
70}
71
rspangler@google.com49fdf182009-10-10 00:57:34 +000072} // namespace chromeos_update_engine