blob: 5bdea3c3239bcc19cb8025b18fcc7883c7545111 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymob5ba9e42014-05-16 13:17:21 -07005#include "update_engine/file_writer.h"
6
adlr@google.comc98a7ed2009-12-04 18:54:03 +00007#include <errno.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <string.h>
9#include <unistd.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070010
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#include <string>
12#include <vector>
Alex Deymob5ba9e42014-05-16 13:17:21 -070013
rspangler@google.com49fdf182009-10-10 00:57:34 +000014#include <gtest/gtest.h>
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080015#include <chromeos/secure_blob.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070016
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000018#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
20using std::string;
21using std::vector;
22
23namespace chromeos_update_engine {
24
25class FileWriterTest : public ::testing::Test { };
26
27TEST(FileWriterTest, SimpleTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070028 // Create a uniquely named file for testing.
29 string path;
Alex Vakulenko88b591f2014-08-28 16:48:57 -070030 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070031 ScopedPathUnlinker path_unlinker(path);
32
rspangler@google.com49fdf182009-10-10 00:57:34 +000033 DirectFileWriter file_writer;
Don Garrette410e0f2011-11-10 15:39:01 -080034 EXPECT_EQ(0, file_writer.Open(path.c_str(),
rspangler@google.com49fdf182009-10-10 00:57:34 +000035 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
36 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080037 EXPECT_TRUE(file_writer.Write("test", 4));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080038 chromeos::Blob actual_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000039 EXPECT_TRUE(utils::ReadFile(path, &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080041 EXPECT_FALSE(memcmp("test", actual_data.data(), actual_data.size()));
rspangler@google.com49fdf182009-10-10 00:57:34 +000042 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000043}
44
45TEST(FileWriterTest, ErrorTest) {
46 DirectFileWriter file_writer;
47 const string path("/tmp/ENOENT/FileWriterTest");
48 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(),
49 O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
50}
51
52TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070053 // Create a uniquely named file for testing.
54 string path;
Alex Vakulenko88b591f2014-08-28 16:48:57 -070055 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070056 ScopedPathUnlinker path_unlinker(path);
57
rspangler@google.com49fdf182009-10-10 00:57:34 +000058 DirectFileWriter file_writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 EXPECT_EQ(0, file_writer.Open(path.c_str(),
60 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
61 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080062 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 EXPECT_EQ(0, file_writer.Close());
64}
65
66
67} // namespace chromeos_update_engine