blob: 5d24eab10311b80a2d2ee8e10f613b4673799dcd [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 Deymob5ba9e42014-05-16 13:17:21 -070015
rspangler@google.com49fdf182009-10-10 00:57:34 +000016#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000017#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000018
19using std::string;
20using std::vector;
21
22namespace chromeos_update_engine {
23
24class FileWriterTest : public ::testing::Test { };
25
26TEST(FileWriterTest, SimpleTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070027 // Create a uniquely named file for testing.
28 string path;
Gilad Arnolda6742b32014-01-11 00:18:34 -080029 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, NULL));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070030 ScopedPathUnlinker path_unlinker(path);
31
rspangler@google.com49fdf182009-10-10 00:57:34 +000032 DirectFileWriter file_writer;
Don Garrette410e0f2011-11-10 15:39:01 -080033 EXPECT_EQ(0, file_writer.Open(path.c_str(),
rspangler@google.com49fdf182009-10-10 00:57:34 +000034 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
35 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080036 EXPECT_TRUE(file_writer.Write("test", 4));
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037 vector<char> actual_data;
38 EXPECT_TRUE(utils::ReadFile(path, &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
40 EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size()));
41 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000042}
43
44TEST(FileWriterTest, ErrorTest) {
45 DirectFileWriter file_writer;
46 const string path("/tmp/ENOENT/FileWriterTest");
47 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(),
48 O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
49}
50
51TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070052 // Create a uniquely named file for testing.
53 string path;
Gilad Arnolda6742b32014-01-11 00:18:34 -080054 ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, NULL));
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070055 ScopedPathUnlinker path_unlinker(path);
56
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 DirectFileWriter file_writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +000058 EXPECT_EQ(0, file_writer.Open(path.c_str(),
59 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
60 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080061 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000062 EXPECT_EQ(0, file_writer.Close());
63}
64
65
66} // namespace chromeos_update_engine