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