Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
| 5 | #include "shill/file_reader.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/file_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 11 | #include <base/files/scoped_temp_dir.h> |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 12 | #include <base/string_util.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 15 | using base::FilePath; |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 16 | using std::string; |
| 17 | using std::vector; |
| 18 | |
| 19 | namespace shill { |
| 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) { |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 45 | base::ScopedTempDir temp_dir; |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 46 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 47 | FilePath path; |
| 48 | ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), &path)); |
| 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 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 64 | base::ScopedTempDir temp_dir; |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 65 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 66 | FilePath path; |
| 67 | ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), &path)); |
| 68 | |
| 69 | // Test a file not ending with a new-line character |
| 70 | ASSERT_EQ(content.size(), |
| 71 | file_util::WriteFile(path, content.c_str(), content.size())); |
| 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(), |
| 77 | file_util::WriteFile(path, content.c_str(), content.size())); |
| 78 | VerifyReadLines(path, lines); |
| 79 | } |
| 80 | |
| 81 | } // namespace shill |