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