blob: 35bfb86ab6e979228dc6f45c7bfc1bf8c9f7e71f [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
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#include <errno.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00006#include <string.h>
7#include <unistd.h>
8#include <string>
9#include <vector>
10#include <gtest/gtest.h>
11#include "update_engine/file_writer.h"
12#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000013#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000014
15using std::string;
16using std::vector;
17
18namespace chromeos_update_engine {
19
20class FileWriterTest : public ::testing::Test { };
21
22TEST(FileWriterTest, SimpleTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070023 // Create a uniquely named file for testing.
24 string path;
25 ASSERT_TRUE(utils::MakeTempFile("/tmp/FileWriterTest-XXXXXX", &path, NULL));
26 ScopedPathUnlinker path_unlinker(path);
27
rspangler@google.com49fdf182009-10-10 00:57:34 +000028 DirectFileWriter file_writer;
Don Garrette410e0f2011-11-10 15:39:01 -080029 EXPECT_EQ(0, file_writer.Open(path.c_str(),
rspangler@google.com49fdf182009-10-10 00:57:34 +000030 O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
31 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080032 EXPECT_TRUE(file_writer.Write("test", 4));
adlr@google.comc98a7ed2009-12-04 18:54:03 +000033 vector<char> actual_data;
34 EXPECT_TRUE(utils::ReadFile(path, &actual_data));
rspangler@google.com49fdf182009-10-10 00:57:34 +000035
36 EXPECT_FALSE(memcmp("test", &actual_data[0], actual_data.size()));
37 EXPECT_EQ(0, file_writer.Close());
rspangler@google.com49fdf182009-10-10 00:57:34 +000038}
39
40TEST(FileWriterTest, ErrorTest) {
41 DirectFileWriter file_writer;
42 const string path("/tmp/ENOENT/FileWriterTest");
43 EXPECT_EQ(-ENOENT, file_writer.Open(path.c_str(),
44 O_CREAT | O_LARGEFILE | O_TRUNC, 0644));
45}
46
47TEST(FileWriterTest, WriteErrorTest) {
Gilad Arnoldcfc836c2013-07-22 17:57:21 -070048 // Create a uniquely named file for testing.
49 string path;
50 ASSERT_TRUE(utils::MakeTempFile("/tmp/FileWriterTest-XXXXXX", &path, NULL));
51 ScopedPathUnlinker path_unlinker(path);
52
rspangler@google.com49fdf182009-10-10 00:57:34 +000053 DirectFileWriter file_writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 EXPECT_EQ(0, file_writer.Open(path.c_str(),
55 O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
56 0644));
Don Garrette410e0f2011-11-10 15:39:01 -080057 EXPECT_FALSE(file_writer.Write("x", 1));
rspangler@google.com49fdf182009-10-10 00:57:34 +000058 EXPECT_EQ(0, file_writer.Close());
59}
60
61
62} // namespace chromeos_update_engine