blob: 92837c88e5427e298bb32dec736062348ea2591d [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.
39 string path;
Alex Vakulenko88b591f2014-08-28 16:48:57 -070040 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070041 ScopedPathUnlinker path_unlinker(path);
42
rspangler@google.com49fdf182009-10-10 00:57:34 +000043 DirectFileWriter file_writer;
Don Garrette410e0f2011-11-10 15:39:01 -080044 EXPECT_EQ(0, file_writer.Open(path.c_str(),
rspangler@google.com49fdf182009-10-10 00:57:34 +000045 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
46 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080047 EXPECT_TRUE(file_writer.Write("test", 4));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070048 brillo::Blob actual_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000049 EXPECT_TRUE(utils::ReadFile(path, &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000050
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080051 EXPECT_FALSE(memcmp("test", actual_data.data(), actual_data.size()));
rspangler@google.com49fdf182009-10-10 00:57:34 +000052 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000053}
54
55TEST(FileWriterTest, ErrorTest) {
56 DirectFileWriter file_writer;
57 const string path("/tmp/ENOENT/FileWriterTest");
58 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(),
59 O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
60}
61
62TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070063 // Create a uniquely named file for testing.
64 string path;
Alex Vakulenko88b591f2014-08-28 16:48:57 -070065 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070066 ScopedPathUnlinker path_unlinker(path);
67
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 DirectFileWriter file_writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 EXPECT_EQ(0, file_writer.Open(path.c_str(),
70 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
71 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080072 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 EXPECT_EQ(0, file_writer.Close());
74}
75
76
77} // namespace chromeos_update_engine