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/kernel_collector.h" | 11 #include "crash-reporter/kernel_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 = false; | 16 static bool s_metrics = false; |
16 | 17 |
17 static const char kTestKCrash[] = "test/kcrash"; | 18 static const char kTestKCrash[] = "test/kcrash"; |
18 static const char kTestCrashDirectory[] = "test/crash_directory"; | 19 static const char kTestCrashDirectory[] = "test/crash_directory"; |
19 | 20 |
| 21 using chromeos::FindLog; |
| 22 |
20 void CountCrash() { | 23 void CountCrash() { |
21 ++s_crashes; | 24 ++s_crashes; |
22 } | 25 } |
23 | 26 |
24 bool IsMetrics() { | 27 bool IsMetrics() { |
25 return s_metrics; | 28 return s_metrics; |
26 } | 29 } |
27 | 30 |
28 class KernelCollectorTest : public ::testing::Test { | 31 class KernelCollectorTest : public ::testing::Test { |
29 void SetUp() { | 32 void SetUp() { |
30 s_crashes = 0; | 33 s_crashes = 0; |
31 s_metrics = true; | 34 s_metrics = true; |
32 collector_.Initialize(CountCrash, | 35 collector_.Initialize(CountCrash, |
33 IsMetrics, | 36 IsMetrics); |
34 &logging_); | |
35 mkdir("test", 0777); | 37 mkdir("test", 0777); |
36 test_kcrash_ = FilePath(kTestKCrash); | 38 test_kcrash_ = FilePath(kTestKCrash); |
37 collector_.OverridePreservedDumpPath(test_kcrash_); | 39 collector_.OverridePreservedDumpPath(test_kcrash_); |
38 unlink(kTestKCrash); | 40 unlink(kTestKCrash); |
39 mkdir(kTestCrashDirectory, 0777); | 41 mkdir(kTestCrashDirectory, 0777); |
| 42 chromeos::ClearLog(); |
40 } | 43 } |
41 protected: | 44 protected: |
42 void WriteStringToFile(const FilePath &file_path, | 45 void WriteStringToFile(const FilePath &file_path, |
43 const char *data) { | 46 const char *data) { |
44 ASSERT_EQ(strlen(data), | 47 ASSERT_EQ(strlen(data), |
45 file_util::WriteFile(file_path, data, strlen(data))); | 48 file_util::WriteFile(file_path, data, strlen(data))); |
46 } | 49 } |
47 | 50 |
48 void SetUpSuccessfulCollect(); | 51 void SetUpSuccessfulCollect(); |
49 void CheckPreservedDumpClear(); | 52 void CheckPreservedDumpClear(); |
50 | 53 |
51 SystemLoggingMock logging_; | |
52 KernelCollector collector_; | 54 KernelCollector collector_; |
53 FilePath test_kcrash_; | 55 FilePath test_kcrash_; |
54 }; | 56 }; |
55 | 57 |
56 TEST_F(KernelCollectorTest, LoadPreservedDump) { | 58 TEST_F(KernelCollectorTest, LoadPreservedDump) { |
57 ASSERT_FALSE(file_util::PathExists(test_kcrash_)); | 59 ASSERT_FALSE(file_util::PathExists(test_kcrash_)); |
58 std::string dump; | 60 std::string dump; |
59 ASSERT_FALSE(collector_.LoadPreservedDump(&dump)); | 61 ASSERT_FALSE(collector_.LoadPreservedDump(&dump)); |
60 WriteStringToFile(test_kcrash_, ""); | 62 WriteStringToFile(test_kcrash_, ""); |
61 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); | 63 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); |
62 ASSERT_EQ("", dump); | 64 ASSERT_EQ("", dump); |
63 WriteStringToFile(test_kcrash_, "something"); | 65 WriteStringToFile(test_kcrash_, "something"); |
64 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); | 66 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); |
65 ASSERT_EQ("something", dump); | 67 ASSERT_EQ("something", dump); |
66 } | 68 } |
67 | 69 |
68 TEST_F(KernelCollectorTest, EnableMissingKernel) { | 70 TEST_F(KernelCollectorTest, EnableMissingKernel) { |
69 ASSERT_FALSE(collector_.Enable()); | 71 ASSERT_FALSE(collector_.Enable()); |
70 ASSERT_FALSE(collector_.IsEnabled()); | 72 ASSERT_FALSE(collector_.IsEnabled()); |
71 ASSERT_EQ(std::string::npos, | 73 ASSERT_TRUE(FindLog( |
72 logging_.log().find("Enabling kernel crash handling")); | 74 "Kernel does not support crash dumping")); |
73 ASSERT_NE(std::string::npos, | |
74 logging_.log().find("Kernel does not support crash dumping")); | |
75 ASSERT_EQ(s_crashes, 0); | 75 ASSERT_EQ(s_crashes, 0); |
76 } | 76 } |
77 | 77 |
78 TEST_F(KernelCollectorTest, EnableOK) { | 78 TEST_F(KernelCollectorTest, EnableOK) { |
79 WriteStringToFile(test_kcrash_, ""); | 79 WriteStringToFile(test_kcrash_, ""); |
80 ASSERT_TRUE(collector_.Enable()); | 80 ASSERT_TRUE(collector_.Enable()); |
81 ASSERT_TRUE(collector_.IsEnabled()); | 81 ASSERT_TRUE(collector_.IsEnabled()); |
82 ASSERT_NE(std::string::npos, | 82 ASSERT_TRUE(FindLog("Enabling kernel crash handling")); |
83 logging_.log().find("Enabling kernel crash handling")); | |
84 ASSERT_EQ(s_crashes, 0); | 83 ASSERT_EQ(s_crashes, 0); |
85 } | 84 } |
86 | 85 |
87 TEST_F(KernelCollectorTest, ClearPreservedDump) { | 86 TEST_F(KernelCollectorTest, ClearPreservedDump) { |
88 std::string dump; | 87 std::string dump; |
89 ASSERT_FALSE(file_util::PathExists(test_kcrash_)); | 88 ASSERT_FALSE(file_util::PathExists(test_kcrash_)); |
90 WriteStringToFile(test_kcrash_, "something"); | 89 WriteStringToFile(test_kcrash_, "something"); |
91 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); | 90 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); |
92 ASSERT_EQ("something", dump); | 91 ASSERT_EQ("something", dump); |
93 ASSERT_TRUE(collector_.ClearPreservedDump()); | 92 ASSERT_TRUE(collector_.ClearPreservedDump()); |
94 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); | 93 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); |
95 ASSERT_EQ(KernelCollector::kClearingSequence, dump); | 94 ASSERT_EQ(KernelCollector::kClearingSequence, dump); |
96 } | 95 } |
97 | 96 |
98 TEST_F(KernelCollectorTest, CollectPreservedFileMissing) { | 97 TEST_F(KernelCollectorTest, CollectPreservedFileMissing) { |
99 ASSERT_FALSE(collector_.Collect()); | 98 ASSERT_FALSE(collector_.Collect()); |
100 ASSERT_NE(logging_.log().find("Unable to read test/kcrash"), | 99 ASSERT_TRUE(FindLog("Unable to read test/kcrash")); |
101 std::string::npos); | |
102 ASSERT_EQ(0, s_crashes); | 100 ASSERT_EQ(0, s_crashes); |
103 } | 101 } |
104 | 102 |
105 TEST_F(KernelCollectorTest, CollectNoCrash) { | 103 TEST_F(KernelCollectorTest, CollectNoCrash) { |
106 WriteStringToFile(test_kcrash_, ""); | 104 WriteStringToFile(test_kcrash_, ""); |
107 ASSERT_FALSE(collector_.Collect()); | 105 ASSERT_FALSE(collector_.Collect()); |
108 ASSERT_EQ(logging_.log().find("Collected kernel crash"), | 106 ASSERT_FALSE(FindLog("Collected kernel crash")); |
109 std::string::npos); | |
110 ASSERT_EQ(0, s_crashes); | 107 ASSERT_EQ(0, s_crashes); |
111 } | 108 } |
112 | 109 |
113 TEST_F(KernelCollectorTest, CollectBadDirectory) { | 110 TEST_F(KernelCollectorTest, CollectBadDirectory) { |
114 WriteStringToFile(test_kcrash_, "something"); | 111 WriteStringToFile(test_kcrash_, "something"); |
115 ASSERT_TRUE(collector_.Collect()); | 112 ASSERT_TRUE(collector_.Collect()); |
116 ASSERT_NE(logging_.log().find( | 113 ASSERT_TRUE(FindLog( |
117 "Unable to create appropriate crash directory"), std::string::npos); | 114 "Unable to create appropriate crash directory")); |
118 ASSERT_EQ(1, s_crashes); | 115 ASSERT_EQ(1, s_crashes); |
119 } | 116 } |
120 | 117 |
121 void KernelCollectorTest::SetUpSuccessfulCollect() { | 118 void KernelCollectorTest::SetUpSuccessfulCollect() { |
122 collector_.ForceCrashDirectory(kTestCrashDirectory); | 119 collector_.ForceCrashDirectory(kTestCrashDirectory); |
123 WriteStringToFile(test_kcrash_, "something"); | 120 WriteStringToFile(test_kcrash_, "something"); |
124 ASSERT_EQ(0, s_crashes); | 121 ASSERT_EQ(0, s_crashes); |
125 } | 122 } |
126 | 123 |
127 void KernelCollectorTest::CheckPreservedDumpClear() { | 124 void KernelCollectorTest::CheckPreservedDumpClear() { |
128 // Make sure the preserved dump is now clear. | 125 // Make sure the preserved dump is now clear. |
129 std::string dump; | 126 std::string dump; |
130 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); | 127 ASSERT_TRUE(collector_.LoadPreservedDump(&dump)); |
131 ASSERT_EQ(KernelCollector::kClearingSequence, dump); | 128 ASSERT_EQ(KernelCollector::kClearingSequence, dump); |
132 } | 129 } |
133 | 130 |
134 TEST_F(KernelCollectorTest, CollectOptedOut) { | 131 TEST_F(KernelCollectorTest, CollectOptedOut) { |
135 SetUpSuccessfulCollect(); | 132 SetUpSuccessfulCollect(); |
136 s_metrics = false; | 133 s_metrics = false; |
137 ASSERT_TRUE(collector_.Collect()); | 134 ASSERT_TRUE(collector_.Collect()); |
138 ASSERT_NE(std::string::npos, logging_.log().find("(ignoring - no consent)")); | 135 ASSERT_TRUE(FindLog("(ignoring - no consent)")); |
139 ASSERT_EQ(0, s_crashes); | 136 ASSERT_EQ(0, s_crashes); |
140 | 137 |
141 CheckPreservedDumpClear(); | 138 CheckPreservedDumpClear(); |
142 } | 139 } |
143 | 140 |
144 TEST_F(KernelCollectorTest, CollectOK) { | 141 TEST_F(KernelCollectorTest, CollectOK) { |
145 SetUpSuccessfulCollect(); | 142 SetUpSuccessfulCollect(); |
146 ASSERT_TRUE(collector_.Collect()); | 143 ASSERT_TRUE(collector_.Collect()); |
147 ASSERT_EQ(1, s_crashes); | 144 ASSERT_EQ(1, s_crashes); |
148 ASSERT_NE(std::string::npos, logging_.log().find("(handling)")); | 145 ASSERT_TRUE(FindLog("(handling)")); |
149 static const char kNamePrefix[] = "Stored kcrash to "; | 146 static const char kNamePrefix[] = "Stored kcrash to "; |
150 size_t pos = logging_.log().find(kNamePrefix); | 147 std::string log = chromeos::GetLog(); |
| 148 size_t pos = log.find(kNamePrefix); |
151 ASSERT_NE(std::string::npos, pos); | 149 ASSERT_NE(std::string::npos, pos); |
152 pos += strlen(kNamePrefix); | 150 pos += strlen(kNamePrefix); |
153 std::string filename = logging_.log().substr(pos, std::string::npos); | 151 std::string filename = log.substr(pos, std::string::npos); |
154 // Take the name up until \n | 152 // Take the name up until \n |
155 size_t end_pos = filename.find_first_of("\n"); | 153 size_t end_pos = filename.find_first_of("\n"); |
156 ASSERT_NE(std::string::npos, end_pos); | 154 ASSERT_NE(std::string::npos, end_pos); |
157 filename = filename.substr(0, end_pos); | 155 filename = filename.substr(0, end_pos); |
158 ASSERT_EQ(0, filename.find(kTestCrashDirectory)); | 156 ASSERT_EQ(0, filename.find(kTestCrashDirectory)); |
159 ASSERT_TRUE(file_util::PathExists(FilePath(filename))); | 157 ASSERT_TRUE(file_util::PathExists(FilePath(filename))); |
160 std::string contents; | 158 std::string contents; |
161 ASSERT_TRUE(file_util::ReadFileToString(FilePath(filename), &contents)); | 159 ASSERT_TRUE(file_util::ReadFileToString(FilePath(filename), &contents)); |
162 ASSERT_EQ("something", contents); | 160 ASSERT_EQ("something", contents); |
163 | 161 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 EXPECT_TRUE( | 263 EXPECT_TRUE( |
266 collector_.ComputeKernelStackSignature(kTruncatedMessage, | 264 collector_.ComputeKernelStackSignature(kTruncatedMessage, |
267 &signature, | 265 &signature, |
268 false)); | 266 false)); |
269 EXPECT_EQ("kernel-0123456789012345678901234567890123456789-00000000", | 267 EXPECT_EQ("kernel-0123456789012345678901234567890123456789-00000000", |
270 signature); | 268 signature); |
271 | 269 |
272 } | 270 } |
273 | 271 |
274 int main(int argc, char **argv) { | 272 int main(int argc, char **argv) { |
275 ::testing::InitGoogleTest(&argc, argv); | 273 SetUpTests(&argc, argv, false); |
276 return RUN_ALL_TESTS(); | 274 return RUN_ALL_TESTS(); |
277 } | 275 } |
OLD | NEW |