| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS 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 <unistd.h> | 5 #include <unistd.h> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chromeos/syslog_logging.h" |
| 10 #include "chromeos/test_helpers.h" |
| 9 #include "crash-reporter/unclean_shutdown_collector.h" | 11 #include "crash-reporter/unclean_shutdown_collector.h" |
| 10 #include "crash-reporter/system_logging_mock.h" | |
| 11 #include "gflags/gflags.h" | 12 #include "gflags/gflags.h" |
| 12 #include "gtest/gtest.h" | 13 #include "gtest/gtest.h" |
| 13 | 14 |
| 14 static int s_crashes = 0; | 15 static int s_crashes = 0; |
| 15 static bool s_metrics = true; | 16 static bool s_metrics = true; |
| 16 | 17 |
| 17 static const char kTestLowBattery[] = "test/low_battery"; | 18 static const char kTestLowBattery[] = "test/low_battery"; |
| 18 static const char kTestSuspended[] = "test/suspended"; | 19 static const char kTestSuspended[] = "test/suspended"; |
| 19 static const char kTestUnclean[] = "test/unclean"; | 20 static const char kTestUnclean[] = "test/unclean"; |
| 20 | 21 |
| 22 using ::chromeos::FindLog; |
| 23 |
| 21 void CountCrash() { | 24 void CountCrash() { |
| 22 ++s_crashes; | 25 ++s_crashes; |
| 23 } | 26 } |
| 24 | 27 |
| 25 bool IsMetrics() { | 28 bool IsMetrics() { |
| 26 return s_metrics; | 29 return s_metrics; |
| 27 } | 30 } |
| 28 | 31 |
| 29 class UncleanShutdownCollectorTest : public ::testing::Test { | 32 class UncleanShutdownCollectorTest : public ::testing::Test { |
| 30 void SetUp() { | 33 void SetUp() { |
| 31 s_crashes = 0; | 34 s_crashes = 0; |
| 32 collector_.Initialize(CountCrash, | 35 collector_.Initialize(CountCrash, |
| 33 IsMetrics, | 36 IsMetrics); |
| 34 &logging_); | |
| 35 rmdir("test"); | 37 rmdir("test"); |
| 36 test_unclean_ = FilePath(kTestUnclean); | 38 test_unclean_ = FilePath(kTestUnclean); |
| 37 collector_.unclean_shutdown_file_ = kTestUnclean; | 39 collector_.unclean_shutdown_file_ = kTestUnclean; |
| 38 file_util::Delete(test_unclean_, true); | 40 file_util::Delete(test_unclean_, true); |
| 39 // Set up alternate power manager tracing files as well | 41 // Set up alternate power manager tracing files as well |
| 40 collector_.powerd_suspended_file_ = FilePath(kTestSuspended); | 42 collector_.powerd_suspended_file_ = FilePath(kTestSuspended); |
| 41 collector_.powerd_low_battery_file_ = FilePath(kTestLowBattery); | 43 collector_.powerd_low_battery_file_ = FilePath(kTestLowBattery); |
| 44 chromeos::ClearLog(); |
| 42 } | 45 } |
| 43 protected: | 46 protected: |
| 44 void WriteStringToFile(const FilePath &file_path, | 47 void WriteStringToFile(const FilePath &file_path, |
| 45 const char *data) { | 48 const char *data) { |
| 46 ASSERT_EQ(strlen(data), | 49 ASSERT_EQ(strlen(data), |
| 47 file_util::WriteFile(file_path, data, strlen(data))); | 50 file_util::WriteFile(file_path, data, strlen(data))); |
| 48 } | 51 } |
| 49 | 52 |
| 50 SystemLoggingMock logging_; | |
| 51 UncleanShutdownCollector collector_; | 53 UncleanShutdownCollector collector_; |
| 52 FilePath test_unclean_; | 54 FilePath test_unclean_; |
| 53 }; | 55 }; |
| 54 | 56 |
| 55 TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { | 57 TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { |
| 56 ASSERT_TRUE(collector_.Enable()); | 58 ASSERT_TRUE(collector_.Enable()); |
| 57 ASSERT_TRUE(file_util::PathExists(test_unclean_)); | 59 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 58 } | 60 } |
| 59 | 61 |
| 60 TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { | 62 TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { |
| 61 mkdir("test", 0777); | 63 mkdir("test", 0777); |
| 62 ASSERT_TRUE(collector_.Enable()); | 64 ASSERT_TRUE(collector_.Enable()); |
| 63 ASSERT_TRUE(file_util::PathExists(test_unclean_)); | 65 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 64 } | 66 } |
| 65 | 67 |
| 66 TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { | 68 TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { |
| 67 collector_.unclean_shutdown_file_ = "/bad/path"; | 69 collector_.unclean_shutdown_file_ = "/bad/path"; |
| 68 ASSERT_FALSE(collector_.Enable()); | 70 ASSERT_FALSE(collector_.Enable()); |
| 69 ASSERT_NE(std::string::npos, | 71 ASSERT_TRUE(FindLog("Unable to create shutdown check file")); |
| 70 logging_.log().find("Unable to create shutdown check file")); | |
| 71 } | 72 } |
| 72 | 73 |
| 73 TEST_F(UncleanShutdownCollectorTest, CollectTrue) { | 74 TEST_F(UncleanShutdownCollectorTest, CollectTrue) { |
| 74 ASSERT_TRUE(collector_.Enable()); | 75 ASSERT_TRUE(collector_.Enable()); |
| 75 ASSERT_TRUE(file_util::PathExists(test_unclean_)); | 76 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 76 ASSERT_TRUE(collector_.Collect()); | 77 ASSERT_TRUE(collector_.Collect()); |
| 77 ASSERT_FALSE(file_util::PathExists(test_unclean_)); | 78 ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 78 ASSERT_EQ(1, s_crashes); | 79 ASSERT_EQ(1, s_crashes); |
| 79 ASSERT_NE(std::string::npos, | 80 ASSERT_TRUE(FindLog("Last shutdown was not clean")); |
| 80 logging_.log().find("Last shutdown was not clean")); | |
| 81 } | 81 } |
| 82 | 82 |
| 83 TEST_F(UncleanShutdownCollectorTest, CollectFalse) { | 83 TEST_F(UncleanShutdownCollectorTest, CollectFalse) { |
| 84 ASSERT_FALSE(collector_.Collect()); | 84 ASSERT_FALSE(collector_.Collect()); |
| 85 ASSERT_EQ(0, s_crashes); | 85 ASSERT_EQ(0, s_crashes); |
| 86 } | 86 } |
| 87 | 87 |
| 88 TEST_F(UncleanShutdownCollectorTest, CollectDeadBatteryRunningLow) { | 88 TEST_F(UncleanShutdownCollectorTest, CollectDeadBatteryRunningLow) { |
| 89 ASSERT_TRUE(collector_.Enable()); | 89 ASSERT_TRUE(collector_.Enable()); |
| 90 ASSERT_TRUE(file_util::PathExists(test_unclean_)); | 90 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 91 file_util::WriteFile(collector_.powerd_low_battery_file_, "", 0); | 91 file_util::WriteFile(collector_.powerd_low_battery_file_, "", 0); |
| 92 ASSERT_FALSE(collector_.Collect()); | 92 ASSERT_FALSE(collector_.Collect()); |
| 93 ASSERT_FALSE(file_util::PathExists(test_unclean_)); | 93 ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 94 ASSERT_FALSE(file_util::PathExists(collector_.powerd_low_battery_file_)); | 94 ASSERT_FALSE(file_util::PathExists(collector_.powerd_low_battery_file_)); |
| 95 ASSERT_EQ(0, s_crashes); | 95 ASSERT_EQ(0, s_crashes); |
| 96 ASSERT_NE(std::string::npos, | 96 ASSERT_TRUE(FindLog( |
| 97 logging_.log().find("Unclean shutdown occurred while running with " | 97 "Unclean shutdown occurred while running with battery critically low.")); |
| 98 "battery critically low.")); | |
| 99 } | 98 } |
| 100 | 99 |
| 101 TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) { | 100 TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) { |
| 102 ASSERT_TRUE(collector_.Enable()); | 101 ASSERT_TRUE(collector_.Enable()); |
| 103 ASSERT_TRUE(file_util::PathExists(test_unclean_)); | 102 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 104 file_util::WriteFile(collector_.powerd_suspended_file_, "", 0); | 103 file_util::WriteFile(collector_.powerd_suspended_file_, "", 0); |
| 105 ASSERT_FALSE(collector_.Collect()); | 104 ASSERT_FALSE(collector_.Collect()); |
| 106 ASSERT_FALSE(file_util::PathExists(test_unclean_)); | 105 ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 107 ASSERT_FALSE(file_util::PathExists(collector_.powerd_suspended_file_)); | 106 ASSERT_FALSE(file_util::PathExists(collector_.powerd_suspended_file_)); |
| 108 ASSERT_EQ(0, s_crashes); | 107 ASSERT_EQ(0, s_crashes); |
| 109 ASSERT_NE(std::string::npos, | 108 ASSERT_TRUE(FindLog("Unclean shutdown occurred while suspended.")); |
| 110 logging_.log().find("Unclean shutdown occurred while suspended.")); | |
| 111 } | 109 } |
| 112 | 110 |
| 113 TEST_F(UncleanShutdownCollectorTest, Disable) { | 111 TEST_F(UncleanShutdownCollectorTest, Disable) { |
| 114 ASSERT_TRUE(collector_.Enable()); | 112 ASSERT_TRUE(collector_.Enable()); |
| 115 ASSERT_TRUE(file_util::PathExists(test_unclean_)); | 113 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 116 ASSERT_TRUE(collector_.Disable()); | 114 ASSERT_TRUE(collector_.Disable()); |
| 117 ASSERT_FALSE(file_util::PathExists(test_unclean_)); | 115 ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 118 ASSERT_FALSE(collector_.Collect()); | 116 ASSERT_FALSE(collector_.Collect()); |
| 119 } | 117 } |
| 120 | 118 |
| 121 TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { | 119 TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { |
| 122 ASSERT_TRUE(collector_.Disable()); | 120 ASSERT_TRUE(collector_.Disable()); |
| 123 } | 121 } |
| 124 | 122 |
| 125 TEST_F(UncleanShutdownCollectorTest, CantDisable) { | 123 TEST_F(UncleanShutdownCollectorTest, CantDisable) { |
| 126 mkdir(kTestUnclean, 0700); | 124 mkdir(kTestUnclean, 0700); |
| 127 file_util::WriteFile(test_unclean_.Append("foo"), "", 0); | 125 file_util::WriteFile(test_unclean_.Append("foo"), "", 0); |
| 128 ASSERT_FALSE(collector_.Disable()); | 126 ASSERT_FALSE(collector_.Disable()); |
| 129 rmdir(kTestUnclean); | 127 rmdir(kTestUnclean); |
| 130 } | 128 } |
| 131 | 129 |
| 132 int main(int argc, char **argv) { | 130 int main(int argc, char **argv) { |
| 133 ::testing::InitGoogleTest(&argc, argv); | 131 SetUpTests(&argc, argv, false); |
| 134 return RUN_ALL_TESTS(); | 132 return RUN_ALL_TESTS(); |
| 135 } | 133 } |
| OLD | NEW |