• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/file_reader.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <base/files/file_util.h>
23 #include <base/files/scoped_temp_dir.h>
24 #include <base/strings/string_util.h>
25 #include <gtest/gtest.h>
26 
27 using base::FilePath;
28 using std::string;
29 using std::vector;
30 
31 namespace shill {
32 
33 class FileReaderTest : public ::testing::Test {
34  public:
VerifyReadLines(const FilePath & path,const vector<string> & lines)35   void VerifyReadLines(const FilePath& path, const vector<string>& lines) {
36     string line;
37     EXPECT_FALSE(reader_.ReadLine(&line));
38     EXPECT_TRUE(reader_.Open(path));
39     for (size_t i = 0; i < lines.size(); ++i) {
40       EXPECT_TRUE(reader_.ReadLine(&line));
41       EXPECT_EQ(lines[i], line);
42     }
43     EXPECT_FALSE(reader_.ReadLine(&line));
44     reader_.Close();
45     EXPECT_FALSE(reader_.ReadLine(&line));
46   }
47 
48  protected:
49   FileReader reader_;
50 };
51 
TEST_F(FileReaderTest,OpenNonExistentFile)52 TEST_F(FileReaderTest, OpenNonExistentFile) {
53   EXPECT_FALSE(reader_.Open(FilePath("a_nonexistent_file")));
54 }
55 
TEST_F(FileReaderTest,OpenEmptyFile)56 TEST_F(FileReaderTest, OpenEmptyFile) {
57   base::ScopedTempDir temp_dir;
58   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
59   FilePath path;
60   ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &path));
61 
62   EXPECT_TRUE(reader_.Open(path));
63   string line;
64   EXPECT_FALSE(reader_.ReadLine(&line));
65   reader_.Close();
66 }
67 
TEST_F(FileReaderTest,ReadLine)68 TEST_F(FileReaderTest, ReadLine) {
69   vector<string> lines;
70   lines.push_back("this is");
71   lines.push_back("a");
72   lines.push_back("");
73   lines.push_back("test");
74   string content = base::JoinString(lines, "\n");
75 
76   base::ScopedTempDir temp_dir;
77   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
78   FilePath path;
79   ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &path));
80 
81   // Test a file not ending with a new-line character
82   ASSERT_EQ(content.size(),
83             base::WriteFile(path, content.c_str(), content.size()));
84   VerifyReadLines(path, lines);
85 
86   // Test a file ending with a new-line character
87   content.push_back('\n');
88   ASSERT_EQ(content.size(),
89             base::WriteFile(path, content.c_str(), content.size()));
90   VerifyReadLines(path, lines);
91 }
92 
93 }  // namespace shill
94