blob: 69477b64d1b5dc010489ac1a30d3d7156ee4ddb9 [file] [log] [blame]
Mike Frysinger38ae98d2012-04-11 12:03:44 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ben Chanbeefd0d2011-07-25 09:31:34 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan5ccd9fe2013-11-13 18:28:27 -08005#include "cros-disks/file_reader.h"
Ben Chanbeefd0d2011-07-25 09:31:34 -07006
7#include <string>
8#include <vector>
9
10#include <base/file_util.h>
Ben Chan97f99ff2013-02-14 14:59:33 -080011#include <base/files/scoped_temp_dir.h>
Ben Chan97e20d42014-02-05 18:38:07 -080012#include <base/strings/string_util.h>
Ben Chanbeefd0d2011-07-25 09:31:34 -070013#include <gtest/gtest.h>
14
Ben Chan2e8aa6f2013-02-15 11:40:04 -080015using base::FilePath;
Ben Chanbeefd0d2011-07-25 09:31:34 -070016using std::string;
17using std::vector;
18
19namespace cros_disks {
20
21class FileReaderTest : public ::testing::Test {
22 public:
23 void VerifyReadLines(const FilePath& path, const vector<string>& lines) {
24 string line;
25 EXPECT_FALSE(reader_.ReadLine(&line));
26 EXPECT_TRUE(reader_.Open(path));
27 for (size_t i = 0; i < lines.size(); ++i) {
28 EXPECT_TRUE(reader_.ReadLine(&line));
29 EXPECT_EQ(lines[i], line);
30 }
31 EXPECT_FALSE(reader_.ReadLine(&line));
32 reader_.Close();
33 EXPECT_FALSE(reader_.ReadLine(&line));
34 }
35
36 protected:
37 FileReader reader_;
38};
39
40TEST_F(FileReaderTest, OpenNonExistentFile) {
41 EXPECT_FALSE(reader_.Open(FilePath("a_nonexistent_file")));
42}
43
44TEST_F(FileReaderTest, OpenEmptyFile) {
Ben Chan97f99ff2013-02-14 14:59:33 -080045 base::ScopedTempDir temp_dir;
Ben Chanbeefd0d2011-07-25 09:31:34 -070046 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
47 FilePath path;
Ben Chan97e20d42014-02-05 18:38:07 -080048 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &path));
Ben Chanbeefd0d2011-07-25 09:31:34 -070049
50 EXPECT_TRUE(reader_.Open(path));
51 string line;
52 EXPECT_FALSE(reader_.ReadLine(&line));
53 reader_.Close();
54}
55
56TEST_F(FileReaderTest, ReadLine) {
57 vector<string> lines;
58 lines.push_back("this is");
59 lines.push_back("a");
60 lines.push_back("");
61 lines.push_back("test");
62 string content = JoinString(lines, '\n');
63
Ben Chan97f99ff2013-02-14 14:59:33 -080064 base::ScopedTempDir temp_dir;
Ben Chanbeefd0d2011-07-25 09:31:34 -070065 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
66 FilePath path;
Ben Chan97e20d42014-02-05 18:38:07 -080067 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &path));
Ben Chanbeefd0d2011-07-25 09:31:34 -070068
69 // Test a file not ending with a new-line character
70 ASSERT_EQ(content.size(),
Ben Chanaf25ddb2014-05-21 18:32:47 -070071 base::WriteFile(path, content.c_str(), content.size()));
Ben Chanbeefd0d2011-07-25 09:31:34 -070072 VerifyReadLines(path, lines);
73
74 // Test a file ending with a new-line character
75 content.push_back('\n');
76 ASSERT_EQ(content.size(),
Ben Chanaf25ddb2014-05-21 18:32:47 -070077 base::WriteFile(path, content.c_str(), content.size()));
Ben Chanbeefd0d2011-07-25 09:31:34 -070078 VerifyReadLines(path, lines);
79}
80
81} // namespace cros_disks