blob: 05df307c20dc382c70b6590876ecb6e4e453880b [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
35class FileWriterTest : public ::testing::Test { };
36
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");
56 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(),
57 O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
58}
59
60TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070061 // Create a uniquely named file for testing.
Sen Jiang0779a152018-07-02 17:34:56 -070062 test_utils::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
72
73} // namespace chromeos_update_engine