Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Side by Side Diff: kernel_collector_test.cc

Issue 3179006: Collect and send kernel crash diagnostics (Closed) Base URL: ssh://git@chromiumos-git//crash-reporter.git
Patch Set: Respond to reviews Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « kernel_collector.cc ('k') | unclean_shutdown_collector.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <unistd.h>
6
7 #include "base/file_util.h"
8 #include "base/string_util.h"
9 #include "crash-reporter/kernel_collector.h"
10 #include "crash-reporter/system_logging_mock.h"
11 #include "gflags/gflags.h"
12 #include "gtest/gtest.h"
13
14 static int s_crashes = 0;
15 static bool s_metrics = false;
16
17 static const char kTestKCrash[] = "test/kcrash";
18 static const char kTestCrashDirectory[] = "test/crash_directory";
19
20 void CountCrash() {
21 ++s_crashes;
22 }
23
24 bool IsMetrics() {
25 return s_metrics;
26 }
27
28 class KernelCollectorTest : public ::testing::Test {
29 void SetUp() {
30 s_crashes = 0;
31 s_metrics = true;
32 collector_.Initialize(CountCrash,
33 IsMetrics,
34 &logging_);
35 mkdir("test", 0777);
36 test_kcrash_ = FilePath(kTestKCrash);
37 collector_.OverridePreservedDumpPath(test_kcrash_);
38 unlink(kTestKCrash);
39 mkdir(kTestCrashDirectory, 0777);
40 }
41 protected:
42 void WriteStringToFile(const FilePath &file_path,
43 const char *data) {
44 ASSERT_EQ(strlen(data),
45 file_util::WriteFile(file_path, data, strlen(data)));
46 }
47
48 void SetUpSuccessfulCollect();
49 void CheckPreservedDumpClear();
50
51 SystemLoggingMock logging_;
52 KernelCollector collector_;
53 FilePath test_kcrash_;
54 };
55
56 TEST_F(KernelCollectorTest, LoadPreservedDump) {
57 ASSERT_FALSE(file_util::PathExists(test_kcrash_));
58 std::string dump;
59 ASSERT_FALSE(collector_.LoadPreservedDump(&dump));
60 WriteStringToFile(test_kcrash_, "");
61 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
62 ASSERT_EQ("", dump);
63 WriteStringToFile(test_kcrash_, "something");
64 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
65 ASSERT_EQ("something", dump);
66 }
67
68 TEST_F(KernelCollectorTest, EnableMissingKernel) {
69 ASSERT_FALSE(collector_.Enable());
70 ASSERT_FALSE(collector_.IsEnabled());
71 ASSERT_EQ(std::string::npos,
72 logging_.log().find("Enabling kernel crash handling"));
73 ASSERT_NE(std::string::npos,
74 logging_.log().find("Kernel does not support crash dumping"));
75 ASSERT_EQ(s_crashes, 0);
76 }
77
78 TEST_F(KernelCollectorTest, EnableOK) {
79 WriteStringToFile(test_kcrash_, "");
80 ASSERT_TRUE(collector_.Enable());
81 ASSERT_TRUE(collector_.IsEnabled());
82 ASSERT_NE(std::string::npos,
83 logging_.log().find("Enabling kernel crash handling"));
84 ASSERT_EQ(s_crashes, 0);
85 }
86
87 TEST_F(KernelCollectorTest, ClearPreservedDump) {
88 std::string dump;
89 ASSERT_FALSE(file_util::PathExists(test_kcrash_));
90 WriteStringToFile(test_kcrash_, "something");
91 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
92 ASSERT_EQ("something", dump);
93 ASSERT_TRUE(collector_.ClearPreservedDump());
94 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
95 ASSERT_EQ(KernelCollector::kClearingSequence, dump);
96 }
97
98 TEST_F(KernelCollectorTest, GetKernelCrashPath) {
99 FilePath root("/var/spool/crash");
100 struct tm tm = {0};
101 tm.tm_sec = 15;
102 tm.tm_min = 50;
103 tm.tm_hour = 13;
104 tm.tm_mday = 23;
105 tm.tm_mon = 4;
106 tm.tm_year = 110;
107 tm.tm_isdst = -1;
108 FilePath path = collector_.GetKernelCrashPath(root, mktime(&tm));
109 ASSERT_EQ("/var/spool/crash/kernel.20100523.135015.0.kcrash",
110 path.value());
111 }
112
113 TEST_F(KernelCollectorTest, CollectPreservedFileMissing) {
114 ASSERT_FALSE(collector_.Collect());
115 ASSERT_NE(logging_.log().find("Unable to read test/kcrash"),
116 std::string::npos);
117 ASSERT_EQ(0, s_crashes);
118 }
119
120 TEST_F(KernelCollectorTest, CollectNoCrash) {
121 WriteStringToFile(test_kcrash_, "");
122 ASSERT_FALSE(collector_.Collect());
123 ASSERT_EQ(logging_.log().find("Collected kernel crash"),
124 std::string::npos);
125 ASSERT_EQ(0, s_crashes);
126 }
127
128 TEST_F(KernelCollectorTest, CollectBadDirectory) {
129 WriteStringToFile(test_kcrash_, "something");
130 ASSERT_TRUE(collector_.Collect());
131 ASSERT_NE(logging_.log().find(
132 "Unable to create appropriate crash directory"), std::string::npos);
133 ASSERT_EQ(1, s_crashes);
134 }
135
136 void KernelCollectorTest::SetUpSuccessfulCollect() {
137 collector_.ForceCrashDirectory(kTestCrashDirectory);
138 WriteStringToFile(test_kcrash_, "something");
139 ASSERT_EQ(0, s_crashes);
140 }
141
142 void KernelCollectorTest::CheckPreservedDumpClear() {
143 // Make sure the preserved dump is now clear.
144 std::string dump;
145 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
146 ASSERT_EQ(KernelCollector::kClearingSequence, dump);
147 }
148
149 TEST_F(KernelCollectorTest, CollectOptedOut) {
150 SetUpSuccessfulCollect();
151 s_metrics = false;
152 ASSERT_TRUE(collector_.Collect());
153 ASSERT_NE(std::string::npos,
154 logging_.log().find("Crash not saved since metrics disabled"));
155 ASSERT_EQ(0, s_crashes);
156
157 CheckPreservedDumpClear();
158 }
159
160
161 TEST_F(KernelCollectorTest, CollectOK) {
162 SetUpSuccessfulCollect();
163 ASSERT_TRUE(collector_.Collect());
164 ASSERT_EQ(1, s_crashes);
165 static const char kNamePrefix[] = "Collected kernel crash diagnostics into ";
166 size_t pos = logging_.log().find(kNamePrefix);
167 ASSERT_NE(std::string::npos, pos);
168 pos += strlen(kNamePrefix);
169 std::string filename = logging_.log().substr(pos, std::string::npos);
170 // Take the name up until \n
171 size_t end_pos = filename.find_first_of("\n");
172 ASSERT_NE(std::string::npos, end_pos);
173 filename = filename.substr(0, end_pos);
174 ASSERT_EQ(0, filename.find(kTestCrashDirectory));
175 ASSERT_TRUE(file_util::PathExists(FilePath(filename)));
176 std::string contents;
177 ASSERT_TRUE(file_util::ReadFileToString(FilePath(filename), &contents));
178 ASSERT_EQ("something", contents);
179
180 CheckPreservedDumpClear();
181 }
182
183 int main(int argc, char **argv) {
184 ::testing::InitGoogleTest(&argc, argv);
185 return RUN_ALL_TESTS();
186 }
OLDNEW
« no previous file with comments | « kernel_collector.cc ('k') | unclean_shutdown_collector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698