Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/system_logs/single_log_source.h" | 5 #include "chrome/browser/chromeos/system_logs/single_log_source.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 return base::WriteFile(log_dir_.GetPath().Append(relative_path), | 65 return base::WriteFile(log_dir_.GetPath().Append(relative_path), |
| 66 input.data(), | 66 input.data(), |
| 67 input.size()) == static_cast<int>(input.size()); | 67 input.size()) == static_cast<int>(input.size()); |
| 68 } | 68 } |
| 69 bool AppendToFile(const base::FilePath& relative_path, | 69 bool AppendToFile(const base::FilePath& relative_path, |
| 70 const std::string& input) { | 70 const std::string& input) { |
| 71 return base::AppendToFile(log_dir_.GetPath().Append(relative_path), | 71 return base::AppendToFile(log_dir_.GetPath().Append(relative_path), |
| 72 input.data(), input.size()); | 72 input.data(), input.size()); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Moves source file to destination path, then creates an empty file at the | |
| 76 // path of the original source file. | |
| 77 // | |
| 78 // |src_relative_path|: Source file path relative to |log_dir_|. | |
| 79 // |dest_relative_path|: Destination path relative to |log_dir_|. | |
| 80 bool RotateFile(const base::FilePath& src_relative_path, | |
| 81 const base::FilePath& dest_relative_path) { | |
| 82 return base::Move(log_dir_.GetPath().Append(src_relative_path), | |
| 83 log_dir_.GetPath().Append(dest_relative_path)) && | |
| 84 WriteFile(src_relative_path, ""); | |
| 85 } | |
| 86 | |
| 75 // Calls source_.Fetch() to start a logs fetch operation. Passes in | 87 // Calls source_.Fetch() to start a logs fetch operation. Passes in |
| 76 // OnFileRead() as a callback. Runs until Fetch() has completed. | 88 // OnFileRead() as a callback. Runs until Fetch() has completed. |
| 77 void FetchFromSource() { | 89 void FetchFromSource() { |
| 78 source_->Fetch( | 90 source_->Fetch( |
| 79 base::Bind(&SingleLogSourceTest::OnFileRead, base::Unretained(this))); | 91 base::Bind(&SingleLogSourceTest::OnFileRead, base::Unretained(this))); |
| 80 scoped_task_environment_.RunUntilIdle(); | 92 scoped_task_environment_.RunUntilIdle(); |
| 81 } | 93 } |
| 82 | 94 |
| 83 // Callback for fetching logs from |source_|. Overwrites the previous stored | 95 // Callback for fetching logs from |source_|. Overwrites the previous stored |
| 84 // value of |latest_response_|. | 96 // value of |latest_response_|. |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 EXPECT_EQ(2, num_callback_calls()); | 284 EXPECT_EQ(2, num_callback_calls()); |
| 273 EXPECT_EQ("", latest_response()); | 285 EXPECT_EQ("", latest_response()); |
| 274 | 286 |
| 275 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"), "D:99:EF:77\n")); | 287 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"), "D:99:EF:77\n")); |
| 276 FetchFromSource(); | 288 FetchFromSource(); |
| 277 | 289 |
| 278 EXPECT_EQ(3, num_callback_calls()); | 290 EXPECT_EQ(3, num_callback_calls()); |
| 279 EXPECT_EQ("Your MAC address is: ab:88:cd:00:00:02\n", latest_response()); | 291 EXPECT_EQ("Your MAC address is: ab:88:cd:00:00:02\n", latest_response()); |
| 280 } | 292 } |
| 281 | 293 |
| 294 TEST_F(SingleLogSourceTest, HandleLogFileRotation) { | |
| 295 InitializeSource(SingleLogSource::SupportedSource::kMessages); | |
| 296 | |
| 297 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "1st log file\n")); | |
| 298 FetchFromSource(); | |
| 299 EXPECT_EQ(1, num_callback_calls()); | |
| 300 EXPECT_EQ("1st log file\n", latest_response()); | |
| 301 | |
| 302 // Rotate file. Make sure the rest of the old file and the contents of the new | |
| 303 // file are both read. | |
| 304 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "More 1st log file\n")); | |
| 305 EXPECT_TRUE( | |
| 306 RotateFile(base::FilePath("messages"), base::FilePath("messages.1"))); | |
| 307 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "2nd log file\n")); | |
| 308 | |
| 309 FetchFromSource(); | |
| 310 EXPECT_EQ(2, num_callback_calls()); | |
| 311 EXPECT_EQ("More 1st log file\n2nd log file\n", latest_response()); | |
| 312 | |
| 313 // Rotate again, but this time omit the newline before rotating. | |
| 314 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "No newline here...")); | |
| 315 EXPECT_TRUE( | |
| 316 RotateFile(base::FilePath("messages"), base::FilePath("messages.1"))); | |
| 317 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "3rd log file\n")); | |
| 318 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "Also no newline here")); | |
| 319 | |
| 320 FetchFromSource(); | |
| 321 EXPECT_EQ(3, num_callback_calls()); | |
| 322 // Make sure the rotation didn't break anything: the last part of the new file | |
| 323 // does not end with a newline; thus the new file should not be read. | |
| 324 EXPECT_EQ("No newline here...3rd log file\n", latest_response()); | |
| 325 | |
| 326 // Finish the previous read attempt by adding the missing newline. | |
| 327 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "...yet\n")); | |
| 328 FetchFromSource(); | |
| 329 EXPECT_EQ(4, num_callback_calls()); | |
| 330 EXPECT_EQ("Also no newline here...yet\n", latest_response()); | |
|
afakhry
2017/06/13 17:39:21
Nice test! Thanks!
| |
| 331 } | |
| 332 | |
| 282 } // namespace system_logs | 333 } // namespace system_logs |
| OLD | NEW |