Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium 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 "chrome/browser/chromeos/system_logs/single_log_source.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/scoped_temp_dir.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/test/scoped_task_scheduler.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace system_logs { | |
| 19 | |
| 20 class SingleLogSourceTest : public ::testing::Test { | |
| 21 public: | |
| 22 SingleLogSourceTest() | |
| 23 : source_(SingleLogSource::SupportedSource::kMessages), | |
| 24 num_callback_calls_(0) { | |
| 25 CHECK(dir_.CreateUniqueTempDir()); | |
| 26 log_file_path_ = dir_.GetPath().Append("log_file"); | |
| 27 | |
| 28 // Create the dummy log file for writing. | |
| 29 base::File new_file; | |
| 30 new_file.Initialize(log_file_path_, | |
| 31 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ); | |
| 32 new_file.Close(); | |
| 33 CHECK(base::PathExists(log_file_path_)); | |
| 34 | |
| 35 // Open the dummy log file for reading from within the log source. | |
| 36 source_.file_.Initialize(log_file_path_, | |
| 37 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 38 CHECK(source_.file_.IsValid()); | |
| 39 } | |
| 40 | |
| 41 ~SingleLogSourceTest() override {} | |
| 42 | |
| 43 // Writes a string to |log_file_path_|. | |
| 44 bool WriteFile(const std::string& input) { | |
| 45 return base::WriteFile(log_file_path_, input.data(), input.size()); | |
| 46 } | |
| 47 // Appends a string to |log_file_path_|. | |
| 48 bool AppendToFile(const std::string& input) { | |
| 49 return base::AppendToFile(log_file_path_, input.data(), input.size()); | |
| 50 } | |
| 51 | |
| 52 // Calls source_.Fetch() to start a logs fetch operation. Passes in | |
| 53 // OnFileRead() as a callback. | |
| 54 void FetchFromSource() { | |
| 55 source_.Fetch( | |
| 56 base::Bind(&SingleLogSourceTest::OnFileRead, base::Unretained(this))); | |
| 57 } | |
| 58 | |
| 59 // Callback for fetching logs from |source_|. Overwrites the previous stored | |
| 60 // value of |latest_response_|. | |
| 61 void OnFileRead(SystemLogsResponse* response) { | |
| 62 ++num_callback_calls_; | |
| 63 if (response->empty()) | |
| 64 return; | |
| 65 | |
| 66 // Since |source_| represents a single log source, it should only return a | |
| 67 // single string result. | |
| 68 EXPECT_EQ(1U, response->size()); | |
| 69 latest_response_ = std::move(response->begin()->second); | |
| 70 } | |
| 71 | |
| 72 int num_callback_calls() const { return num_callback_calls_; } | |
| 73 | |
| 74 const std::string& latest_response() const { return latest_response_; } | |
| 75 | |
| 76 const base::FilePath& log_file_path() const { return log_file_path_; } | |
| 77 | |
| 78 private: | |
| 79 // Unit under test. | |
| 80 SingleLogSource source_; | |
| 81 | |
| 82 // Counts the number of times that |source_| has invoked the callback. | |
| 83 int num_callback_calls_; | |
| 84 | |
| 85 // Stores the string response returned from |source_| the last time it invoked | |
| 86 // OnFileRead. | |
| 87 std::string latest_response_; | |
| 88 | |
| 89 // Temporary dir for creating a dummy log file. | |
| 90 base::ScopedTempDir dir_; | |
| 91 | |
| 92 // Path to the dummy log file in |dir_|. | |
| 93 base::FilePath log_file_path_; | |
| 94 | |
| 95 // Used for running scheduled tasks. | |
| 96 base::test::ScopedTaskScheduler scheduler_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(SingleLogSourceTest); | |
| 99 }; | |
| 100 | |
| 101 TEST_F(SingleLogSourceTest, EmptyFile) { | |
| 102 FetchFromSource(); | |
| 103 base::RunLoop().RunUntilIdle(); | |
|
afakhry
2017/05/09 00:39:23
Nit: You can even move the RunUntilIdle() into Fet
Simon Que
2017/05/09 02:06:22
Done.
| |
| 104 | |
| 105 EXPECT_EQ(1, num_callback_calls()); | |
| 106 EXPECT_EQ("", latest_response()); | |
| 107 } | |
| 108 | |
| 109 TEST_F(SingleLogSourceTest, SingleRead) { | |
| 110 EXPECT_TRUE(AppendToFile("Hello world!\n")); | |
| 111 FetchFromSource(); | |
| 112 base::RunLoop().RunUntilIdle(); | |
| 113 | |
| 114 EXPECT_EQ(1, num_callback_calls()); | |
| 115 EXPECT_EQ("Hello world!\n", latest_response()); | |
| 116 } | |
| 117 | |
| 118 TEST_F(SingleLogSourceTest, IncrementalReads) { | |
| 119 EXPECT_TRUE(AppendToFile("Hello world!\n")); | |
| 120 FetchFromSource(); | |
| 121 base::RunLoop().RunUntilIdle(); | |
| 122 | |
| 123 EXPECT_EQ(1, num_callback_calls()); | |
| 124 EXPECT_EQ("Hello world!\n", latest_response()); | |
| 125 | |
| 126 EXPECT_TRUE(AppendToFile("The quick brown fox jumps over the lazy dog\n")); | |
| 127 FetchFromSource(); | |
| 128 base::RunLoop().RunUntilIdle(); | |
| 129 | |
| 130 EXPECT_EQ(2, num_callback_calls()); | |
| 131 EXPECT_EQ("The quick brown fox jumps over the lazy dog\n", latest_response()); | |
| 132 | |
| 133 EXPECT_TRUE(AppendToFile("Some like it hot.\nSome like it cold\n")); | |
| 134 FetchFromSource(); | |
| 135 base::RunLoop().RunUntilIdle(); | |
| 136 | |
| 137 EXPECT_EQ(3, num_callback_calls()); | |
| 138 EXPECT_EQ("Some like it hot.\nSome like it cold\n", latest_response()); | |
| 139 | |
| 140 // As a sanity check, read entire contents of file separately to make sure it | |
| 141 // was written incrementally, and hence read incrementally. | |
| 142 std::string file_contents; | |
| 143 EXPECT_TRUE(base::ReadFileToString(log_file_path(), &file_contents)); | |
| 144 EXPECT_EQ( | |
| 145 "Hello world!\nThe quick brown fox jumps over the lazy dog\n" | |
| 146 "Some like it hot.\nSome like it cold\n", | |
| 147 file_contents); | |
| 148 } | |
| 149 | |
| 150 // The log files read by SingleLogSource are not expected to be overwritten. | |
| 151 // This test is just to ensure that the SingleLogSource class is robust enough | |
| 152 // not to break in the event of an overwrite. | |
| 153 TEST_F(SingleLogSourceTest, FileOverwrite) { | |
| 154 EXPECT_TRUE(AppendToFile("0123456789\n")); | |
| 155 FetchFromSource(); | |
| 156 base::RunLoop().RunUntilIdle(); | |
| 157 | |
| 158 EXPECT_EQ(1, num_callback_calls()); | |
| 159 EXPECT_EQ("0123456789\n", latest_response()); | |
| 160 | |
| 161 // Overwrite the file. | |
| 162 EXPECT_TRUE(WriteFile("abcdefg\n")); | |
| 163 FetchFromSource(); | |
| 164 base::RunLoop().RunUntilIdle(); | |
| 165 | |
| 166 // Should re-read from the beginning. | |
| 167 EXPECT_EQ(2, num_callback_calls()); | |
| 168 EXPECT_EQ("abcdefg\n", latest_response()); | |
| 169 | |
| 170 // Append to the file to make sure incremental read still works. | |
| 171 EXPECT_TRUE(AppendToFile("hijk\n")); | |
| 172 FetchFromSource(); | |
| 173 base::RunLoop().RunUntilIdle(); | |
| 174 | |
| 175 EXPECT_EQ(3, num_callback_calls()); | |
| 176 EXPECT_EQ("hijk\n", latest_response()); | |
| 177 | |
| 178 // Overwrite again, this time with a longer length than the existing file. | |
| 179 // Previous contents: | |
| 180 // abcdefg~hijk~ <-- "~" is a single-char representation of newline. | |
| 181 // New contents: | |
| 182 // lmnopqrstuvwxyz~ <-- excess text beyond end of prev contents: "yz~" | |
| 183 EXPECT_TRUE(WriteFile("lmnopqrstuvwxyz\n")); | |
| 184 FetchFromSource(); | |
| 185 base::RunLoop().RunUntilIdle(); | |
| 186 | |
| 187 EXPECT_EQ(4, num_callback_calls()); | |
| 188 EXPECT_EQ("yz\n", latest_response()); | |
| 189 } | |
| 190 | |
| 191 TEST_F(SingleLogSourceTest, IncompleteLines) { | |
| 192 EXPECT_TRUE(AppendToFile("0123456789")); | |
| 193 FetchFromSource(); | |
| 194 base::RunLoop().RunUntilIdle(); | |
| 195 | |
| 196 EXPECT_EQ(1, num_callback_calls()); | |
| 197 EXPECT_EQ("", latest_response()); | |
| 198 | |
| 199 EXPECT_TRUE(AppendToFile("abcdefg")); | |
| 200 FetchFromSource(); | |
| 201 base::RunLoop().RunUntilIdle(); | |
| 202 | |
| 203 EXPECT_EQ(2, num_callback_calls()); | |
| 204 EXPECT_EQ("", latest_response()); | |
| 205 | |
| 206 EXPECT_TRUE(AppendToFile("hijk\n")); | |
| 207 FetchFromSource(); | |
| 208 base::RunLoop().RunUntilIdle(); | |
| 209 | |
| 210 EXPECT_EQ(3, num_callback_calls()); | |
| 211 // All the previously written text should be read this time. | |
| 212 EXPECT_EQ("0123456789abcdefghijk\n", latest_response()); | |
| 213 | |
| 214 EXPECT_TRUE(AppendToFile("Hello world\n")); | |
| 215 EXPECT_TRUE(AppendToFile("Goodbye world")); | |
| 216 FetchFromSource(); | |
| 217 base::RunLoop().RunUntilIdle(); | |
| 218 | |
| 219 // Partial whole-line reads are not supported. The last byte of the read must | |
| 220 // be a new line. | |
| 221 EXPECT_EQ(4, num_callback_calls()); | |
| 222 EXPECT_EQ("", latest_response()); | |
| 223 | |
| 224 EXPECT_TRUE(AppendToFile("\n")); | |
| 225 FetchFromSource(); | |
| 226 base::RunLoop().RunUntilIdle(); | |
| 227 | |
| 228 EXPECT_EQ(5, num_callback_calls()); | |
| 229 EXPECT_EQ("Hello world\nGoodbye world\n", latest_response()); | |
| 230 } | |
| 231 | |
| 232 TEST_F(SingleLogSourceTest, Anonymize) { | |
| 233 EXPECT_TRUE(AppendToFile("My MAC address is: 11:22:33:44:55:66\n")); | |
| 234 FetchFromSource(); | |
| 235 base::RunLoop().RunUntilIdle(); | |
| 236 | |
| 237 EXPECT_EQ(1, num_callback_calls()); | |
| 238 EXPECT_EQ("My MAC address is: 11:22:33:00:00:01\n", latest_response()); | |
| 239 | |
| 240 // Suppose the write operation is not atomic, and the MAC address is written | |
| 241 // across two separate writes. | |
| 242 EXPECT_TRUE(AppendToFile("Your MAC address is: AB:88:C")); | |
| 243 FetchFromSource(); | |
| 244 base::RunLoop().RunUntilIdle(); | |
| 245 | |
| 246 EXPECT_EQ(2, num_callback_calls()); | |
| 247 EXPECT_EQ("", latest_response()); | |
| 248 | |
| 249 EXPECT_TRUE(AppendToFile("D:99:EF:77\n")); | |
| 250 FetchFromSource(); | |
| 251 base::RunLoop().RunUntilIdle(); | |
| 252 | |
| 253 EXPECT_EQ(3, num_callback_calls()); | |
| 254 EXPECT_EQ("Your MAC address is: ab:88:cd:00:00:02\n", latest_response()); | |
| 255 } | |
| 256 | |
| 257 } // namespace system_logs | |
| OLD | NEW |