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