Mike Frysinger | 38ae98d | 2012-04-11 12:03:44 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 5 | #include "cros-disks/file_reader.h" |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/file_util.h> |
Ben Chan | 97f99ff | 2013-02-14 14:59:33 -0800 | [diff] [blame] | 11 | #include <base/files/scoped_temp_dir.h> |
Ben Chan | 97e20d4 | 2014-02-05 18:38:07 -0800 | [diff] [blame] | 12 | #include <base/strings/string_util.h> |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
| 14 | |
Ben Chan | 2e8aa6f | 2013-02-15 11:40:04 -0800 | [diff] [blame] | 15 | using base::FilePath; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 16 | using std::string; |
| 17 | using std::vector; |
| 18 | |
| 19 | namespace cros_disks { |
| 20 | |
| 21 | class 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 | |
| 40 | TEST_F(FileReaderTest, OpenNonExistentFile) { |
| 41 | EXPECT_FALSE(reader_.Open(FilePath("a_nonexistent_file"))); |
| 42 | } |
| 43 | |
| 44 | TEST_F(FileReaderTest, OpenEmptyFile) { |
Ben Chan | 97f99ff | 2013-02-14 14:59:33 -0800 | [diff] [blame] | 45 | base::ScopedTempDir temp_dir; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 46 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 47 | FilePath path; |
Ben Chan | 97e20d4 | 2014-02-05 18:38:07 -0800 | [diff] [blame] | 48 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &path)); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 49 | |
| 50 | EXPECT_TRUE(reader_.Open(path)); |
| 51 | string line; |
| 52 | EXPECT_FALSE(reader_.ReadLine(&line)); |
| 53 | reader_.Close(); |
| 54 | } |
| 55 | |
| 56 | TEST_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 Chan | 97f99ff | 2013-02-14 14:59:33 -0800 | [diff] [blame] | 64 | base::ScopedTempDir temp_dir; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 66 | FilePath path; |
Ben Chan | 97e20d4 | 2014-02-05 18:38:07 -0800 | [diff] [blame] | 67 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &path)); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 68 | |
| 69 | // Test a file not ending with a new-line character |
| 70 | ASSERT_EQ(content.size(), |
Ben Chan | af25ddb | 2014-05-21 18:32:47 -0700 | [diff] [blame^] | 71 | base::WriteFile(path, content.c_str(), content.size())); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 72 | 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 Chan | af25ddb | 2014-05-21 18:32:47 -0700 | [diff] [blame^] | 77 | base::WriteFile(path, content.c_str(), content.size())); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 78 | VerifyReadLines(path, lines); |
| 79 | } |
| 80 | |
| 81 | } // namespace cros_disks |