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