async_log_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2021 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. #include "ash/system/diagnostics/async_log.h"
  5. #include <memory>
  6. #include "ash/system/diagnostics/log_test_helpers.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/run_loop.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/test/test_simple_task_runner.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace ash {
  15. namespace diagnostics {
  16. namespace {
  17. const char kLogFileName[] = "test_async_log";
  18. } // namespace
  19. class AsyncLogTest : public testing::Test {
  20. public:
  21. AsyncLogTest() : task_runner_(new base::TestSimpleTaskRunner) {
  22. EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
  23. log_path_ = temp_dir_.GetPath().AppendASCII(kLogFileName);
  24. }
  25. ~AsyncLogTest() override { base::RunLoop().RunUntilIdle(); }
  26. protected:
  27. base::test::TaskEnvironment task_environment_{
  28. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  29. scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
  30. base::ScopedTempDir temp_dir_;
  31. base::FilePath log_path_;
  32. };
  33. TEST_F(AsyncLogTest, NoWriteEmpty) {
  34. AsyncLog log(log_path_);
  35. log.SetTaskRunnerForTesting(task_runner_);
  36. // The file won't until it is written to.
  37. EXPECT_FALSE(base::PathExists(log_path_));
  38. // The log is empty.
  39. EXPECT_TRUE(log.GetContents().empty());
  40. }
  41. TEST_F(AsyncLogTest, WriteEmpty) {
  42. AsyncLog log(log_path_);
  43. log.SetTaskRunnerForTesting(task_runner_);
  44. // Append empty string to the log.
  45. log.Append("");
  46. EXPECT_TRUE(task_runner_->HasPendingTask());
  47. // Ensure pending tasks complete.
  48. task_runner_->RunUntilIdle();
  49. EXPECT_FALSE(task_runner_->HasPendingTask());
  50. // The file exists.
  51. EXPECT_TRUE(base::PathExists(log_path_));
  52. // But log is still empty.
  53. EXPECT_TRUE(log.GetContents().empty());
  54. }
  55. TEST_F(AsyncLogTest, WriteOneLine) {
  56. AsyncLog log(log_path_);
  57. log.SetTaskRunnerForTesting(task_runner_);
  58. const std::string line = "Hello";
  59. // Append `line` to the log.
  60. log.Append(line);
  61. // Ensure pending tasks complete.
  62. task_runner_->RunUntilIdle();
  63. // Log contains `line`.
  64. EXPECT_EQ(line, log.GetContents());
  65. }
  66. TEST_F(AsyncLogTest, WriteMultipleLines) {
  67. AsyncLog log(log_path_);
  68. log.SetTaskRunnerForTesting(task_runner_);
  69. const std::vector<std::string> lines = {
  70. "Line 1",
  71. "Line 2",
  72. "Line 3",
  73. };
  74. // Append all the `lines` with a new line to the log.
  75. for (auto line : lines) {
  76. log.Append(line + "\n");
  77. }
  78. // Ensure pending tasks complete.
  79. task_runner_->RunUntilIdle();
  80. // Read back the log and split the lines.
  81. EXPECT_EQ(lines, GetLogLines(log.GetContents()));
  82. }
  83. TEST_F(AsyncLogTest, NoUseAfterFreeCrash) {
  84. auto log = std::make_unique<AsyncLog>(log_path_);
  85. log->SetTaskRunnerForTesting(task_runner_);
  86. const std::string line_not_written = "Should not be written";
  87. log->Append(line_not_written);
  88. EXPECT_EQ(1u, task_runner_->NumPendingTasks());
  89. // Simulate log destroyed before append can complete.
  90. log.reset();
  91. // Attempt to run pending AppendImpl call after AsyncLog destroyed.
  92. task_runner_->RunUntilIdle();
  93. EXPECT_FALSE(base::PathExists(log_path_));
  94. EXPECT_EQ(0u, task_runner_->NumPendingTasks());
  95. }
  96. } // namespace diagnostics
  97. } // namespace ash