blob: ee4515468ad0c2bcd438fc3e27637fac23a4c0cb [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
Ben Chancd8fda42014-09-05 08:21:06 -070010#include <base/files/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 Chanbeefd0d2011-07-25 09:31:34 -070015namespace cros_disks {
16
17class FileReaderTest : public ::testing::Test {
18 public:
Ben Chan213c6d92019-04-10 16:21:52 -070019 void VerifyReadLines(const base::FilePath& path,
20 const std::vector<std::string>& lines) {
21 std::string line;
Ben Chanbeefd0d2011-07-25 09:31:34 -070022 EXPECT_FALSE(reader_.ReadLine(&line));
23 EXPECT_TRUE(reader_.Open(path));
Ben Chan6057fe62016-12-02 10:08:59 -080024 for (const auto& expected_line : lines) {
Ben Chanbeefd0d2011-07-25 09:31:34 -070025 EXPECT_TRUE(reader_.ReadLine(&line));
Ben Chan6057fe62016-12-02 10:08:59 -080026 EXPECT_EQ(expected_line, line);
Ben Chanbeefd0d2011-07-25 09:31:34 -070027 }
28 EXPECT_FALSE(reader_.ReadLine(&line));
29 reader_.Close();
30 EXPECT_FALSE(reader_.ReadLine(&line));
31 }
32
33 protected:
34 FileReader reader_;
35};
36
37TEST_F(FileReaderTest, OpenNonExistentFile) {
Ben Chan213c6d92019-04-10 16:21:52 -070038 EXPECT_FALSE(reader_.Open(base::FilePath("a_nonexistent_file")));
Ben Chanbeefd0d2011-07-25 09:31:34 -070039}
40
41TEST_F(FileReaderTest, OpenEmptyFile) {
Ben Chan97f99ff2013-02-14 14:59:33 -080042 base::ScopedTempDir temp_dir;
Ben Chanbeefd0d2011-07-25 09:31:34 -070043 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Ben Chan213c6d92019-04-10 16:21:52 -070044 base::FilePath path;
Eric Caruso23d6fe02018-01-22 15:44:59 -080045 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &path));
Ben Chanbeefd0d2011-07-25 09:31:34 -070046
47 EXPECT_TRUE(reader_.Open(path));
Ben Chan213c6d92019-04-10 16:21:52 -070048 std::string line;
Ben Chanbeefd0d2011-07-25 09:31:34 -070049 EXPECT_FALSE(reader_.ReadLine(&line));
50 reader_.Close();
51}
52
53TEST_F(FileReaderTest, ReadLine) {
Ben Chan213c6d92019-04-10 16:21:52 -070054 std::vector<std::string> lines;
Ben Chanbeefd0d2011-07-25 09:31:34 -070055 lines.push_back("this is");
56 lines.push_back("a");
57 lines.push_back("");
58 lines.push_back("test");
Ben Chan213c6d92019-04-10 16:21:52 -070059 std::string content = base::JoinString(lines, "\n");
Ben Chanbeefd0d2011-07-25 09:31:34 -070060
Ben Chan97f99ff2013-02-14 14:59:33 -080061 base::ScopedTempDir temp_dir;
Ben Chanbeefd0d2011-07-25 09:31:34 -070062 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Ben Chan213c6d92019-04-10 16:21:52 -070063 base::FilePath path;
Eric Caruso23d6fe02018-01-22 15:44:59 -080064 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &path));
Ben Chanbeefd0d2011-07-25 09:31:34 -070065
66 // Test a file not ending with a new-line character
67 ASSERT_EQ(content.size(),
Ben Chanaf25ddb2014-05-21 18:32:47 -070068 base::WriteFile(path, content.c_str(), content.size()));
Ben Chanbeefd0d2011-07-25 09:31:34 -070069 VerifyReadLines(path, lines);
70
71 // Test a file ending with a new-line character
72 content.push_back('\n');
73 ASSERT_EQ(content.size(),
Ben Chanaf25ddb2014-05-21 18:32:47 -070074 base::WriteFile(path, content.c_str(), content.size()));
Ben Chanbeefd0d2011-07-25 09:31:34 -070075 VerifyReadLines(path, lines);
76}
77
78} // namespace cros_disks