blob: 731b6f0ce437b1606c272dc728396c8f64f05ccb [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 Deymob5ba9e42014-05-16 13:17:21 -070017#include "update_engine/file_writer.h"
18
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>
24#include <vector>
Alex Deymob5ba9e42014-05-16 13:17:21 -070025
rspangler@google.com49fdf182009-10-10 00:57:34 +000026#include <gtest/gtest.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#include <brillo/secure_blob.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070028
rspangler@google.com49fdf182009-10-10 00:57:34 +000029#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000030#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000031
32using std::string;
33using std::vector;
34
35namespace chromeos_update_engine {
36
37class FileWriterTest : public ::testing::Test { };
38
39TEST(FileWriterTest, SimpleTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070040 // Create a uniquely named file for testing.
41 string path;
Alex Vakulenko88b591f2014-08-28 16:48:57 -070042 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070043 ScopedPathUnlinker path_unlinker(path);
44
rspangler@google.com49fdf182009-10-10 00:57:34 +000045 DirectFileWriter file_writer;
Don Garrette410e0f2011-11-10 15:39:01 -080046 EXPECT_EQ(0, file_writer.Open(path.c_str(),
rspangler@google.com49fdf182009-10-10 00:57:34 +000047 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
48 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080049 EXPECT_TRUE(file_writer.Write("test", 4));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070050 brillo::Blob actual_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000051 EXPECT_TRUE(utils::ReadFile(path, &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000052
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080053 EXPECT_FALSE(memcmp("test", actual_data.data(), actual_data.size()));
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000055}
56
57TEST(FileWriterTest, ErrorTest) {
58 DirectFileWriter file_writer;
59 const string path("/tmp/ENOENT/FileWriterTest");
60 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(),
61 O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
62}
63
64TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070065 // Create a uniquely named file for testing.
66 string path;
Alex Vakulenko88b591f2014-08-28 16:48:57 -070067 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070068 ScopedPathUnlinker path_unlinker(path);
69
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 DirectFileWriter file_writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 EXPECT_EQ(0, file_writer.Open(path.c_str(),
72 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
73 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080074 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 EXPECT_EQ(0, file_writer.Close());
76}
77
78
79} // namespace chromeos_update_engine