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/crash_collector.h" | 9 #include "crash-reporter/crash_collector.h" |
10 #include "crash-reporter/system_logging_mock.h" | 10 #include "crash-reporter/system_logging_mock.h" |
| 11 #include "crash-reporter/test_helpers.h" |
11 #include "gflags/gflags.h" | 12 #include "gflags/gflags.h" |
12 #include "gtest/gtest.h" | 13 #include "gtest/gtest.h" |
13 | 14 |
| 15 // This test assumes the following standard binaries are installed. |
| 16 static const char kBinBash[] = "/bin/bash"; |
| 17 static const char kBinCp[] = "/bin/cp"; |
| 18 static const char kBinEcho[] = "/bin/echo"; |
| 19 static const char kBinFalse[] = "/bin/false"; |
| 20 |
14 void CountCrash() { | 21 void CountCrash() { |
15 ADD_FAILURE(); | 22 ADD_FAILURE(); |
16 } | 23 } |
17 | 24 |
18 bool IsMetrics() { | 25 bool IsMetrics() { |
19 ADD_FAILURE(); | 26 ADD_FAILURE(); |
20 return false; | 27 return false; |
21 } | 28 } |
22 | 29 |
23 class CrashCollectorTest : public ::testing::Test { | 30 class CrashCollectorTest : public ::testing::Test { |
(...skipping 17 matching lines...) Expand all Loading... |
41 CrashCollector collector_; | 48 CrashCollector collector_; |
42 FilePath test_dir_; | 49 FilePath test_dir_; |
43 }; | 50 }; |
44 | 51 |
45 TEST_F(CrashCollectorTest, Initialize) { | 52 TEST_F(CrashCollectorTest, Initialize) { |
46 ASSERT_TRUE(CountCrash == collector_.count_crash_function_); | 53 ASSERT_TRUE(CountCrash == collector_.count_crash_function_); |
47 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_); | 54 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_); |
48 ASSERT_TRUE(&logging_ == collector_.logger_); | 55 ASSERT_TRUE(&logging_ == collector_.logger_); |
49 } | 56 } |
50 | 57 |
| 58 TEST_F(CrashCollectorTest, WriteNewFile) { |
| 59 FilePath test_file = test_dir_.Append("test_new"); |
| 60 const char kBuffer[] = "buffer"; |
| 61 EXPECT_EQ(strlen(kBuffer), |
| 62 collector_.WriteNewFile(test_file, |
| 63 kBuffer, |
| 64 strlen(kBuffer))); |
| 65 EXPECT_LT(collector_.WriteNewFile(test_file, |
| 66 kBuffer, |
| 67 strlen(kBuffer)), 0); |
| 68 } |
| 69 |
| 70 TEST_F(CrashCollectorTest, ForkExecAndPipe) { |
| 71 std::vector<const char *> args; |
| 72 char output_file[] = "test/fork_out"; |
| 73 |
| 74 // Test basic call with stdout. |
| 75 args.clear(); |
| 76 args.push_back(kBinEcho); |
| 77 args.push_back("hello world"); |
| 78 EXPECT_EQ(0, collector_.ForkExecAndPipe(args, output_file)); |
| 79 ExpectFileEquals("hello world\n", output_file); |
| 80 EXPECT_EQ("", logging_.log()); |
| 81 |
| 82 // Test non-zero return value |
| 83 logging_.clear(); |
| 84 args.clear(); |
| 85 args.push_back(kBinFalse); |
| 86 EXPECT_EQ(1, collector_.ForkExecAndPipe(args, output_file)); |
| 87 ExpectFileEquals("", output_file); |
| 88 EXPECT_EQ("", logging_.log()); |
| 89 |
| 90 // Test bad output_file. |
| 91 EXPECT_EQ(127, collector_.ForkExecAndPipe(args, "/bad/path")); |
| 92 |
| 93 // Test bad executable. |
| 94 logging_.clear(); |
| 95 args.clear(); |
| 96 args.push_back("false"); |
| 97 EXPECT_EQ(127, collector_.ForkExecAndPipe(args, output_file)); |
| 98 |
| 99 // Test stderr captured. |
| 100 std::string contents; |
| 101 logging_.clear(); |
| 102 args.clear(); |
| 103 args.push_back(kBinCp); |
| 104 EXPECT_EQ(1, collector_.ForkExecAndPipe(args, output_file)); |
| 105 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file), |
| 106 &contents)); |
| 107 EXPECT_NE(std::string::npos, contents.find("missing file operand")); |
| 108 EXPECT_EQ("", logging_.log()); |
| 109 |
| 110 // NULL parameter. |
| 111 logging_.clear(); |
| 112 args.clear(); |
| 113 args.push_back(NULL); |
| 114 EXPECT_EQ(-1, collector_.ForkExecAndPipe(args, output_file)); |
| 115 EXPECT_NE(std::string::npos, |
| 116 logging_.log().find("Bad parameter")); |
| 117 |
| 118 // No parameters. |
| 119 args.clear(); |
| 120 EXPECT_EQ(127, collector_.ForkExecAndPipe(args, output_file)); |
| 121 |
| 122 // Segmentation faulting process. |
| 123 logging_.clear(); |
| 124 args.clear(); |
| 125 args.push_back(kBinBash); |
| 126 args.push_back("-c"); |
| 127 args.push_back("kill -SEGV $$"); |
| 128 EXPECT_EQ(-1, collector_.ForkExecAndPipe(args, output_file)); |
| 129 EXPECT_NE(std::string::npos, |
| 130 logging_.log().find("Process did not exit normally")); |
| 131 } |
| 132 |
51 TEST_F(CrashCollectorTest, Sanitize) { | 133 TEST_F(CrashCollectorTest, Sanitize) { |
52 EXPECT_EQ("chrome", collector_.Sanitize("chrome")); | 134 EXPECT_EQ("chrome", collector_.Sanitize("chrome")); |
53 EXPECT_EQ("CHROME", collector_.Sanitize("CHROME")); | 135 EXPECT_EQ("CHROME", collector_.Sanitize("CHROME")); |
54 EXPECT_EQ("1chrome2", collector_.Sanitize("1chrome2")); | 136 EXPECT_EQ("1chrome2", collector_.Sanitize("1chrome2")); |
55 EXPECT_EQ("chrome__deleted_", collector_.Sanitize("chrome (deleted)")); | 137 EXPECT_EQ("chrome__deleted_", collector_.Sanitize("chrome (deleted)")); |
56 EXPECT_EQ("foo_bar", collector_.Sanitize("foo.bar")); | 138 EXPECT_EQ("foo_bar", collector_.Sanitize("foo.bar")); |
57 EXPECT_EQ("", collector_.Sanitize("")); | 139 EXPECT_EQ("", collector_.Sanitize("")); |
58 EXPECT_EQ("_", collector_.Sanitize(" ")); | 140 EXPECT_EQ("_", collector_.Sanitize(" ")); |
59 } | 141 } |
60 | 142 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 EXPECT_TRUE(i != dictionary.end() && i->second == "h"); | 306 EXPECT_TRUE(i != dictionary.end() && i->second == "h"); |
225 i = dictionary.find("i"); | 307 i = dictionary.find("i"); |
226 EXPECT_TRUE(i != dictionary.end() && i->second == "j"); | 308 EXPECT_TRUE(i != dictionary.end() && i->second == "j"); |
227 i = dictionary.find(""); | 309 i = dictionary.find(""); |
228 EXPECT_TRUE(i != dictionary.end() && i->second == "k"); | 310 EXPECT_TRUE(i != dictionary.end() && i->second == "k"); |
229 i = dictionary.find("l"); | 311 i = dictionary.find("l"); |
230 EXPECT_TRUE(i != dictionary.end() && i->second == ""); | 312 EXPECT_TRUE(i != dictionary.end() && i->second == ""); |
231 } | 313 } |
232 | 314 |
233 TEST_F(CrashCollectorTest, MetaData) { | 315 TEST_F(CrashCollectorTest, MetaData) { |
234 FilePath meta_file = test_dir_.Append("generated.meta"); | 316 const char kMetaFileBasename[] = "generated.meta"; |
| 317 FilePath meta_file = test_dir_.Append(kMetaFileBasename); |
235 FilePath lsb_release = test_dir_.Append("lsb-release"); | 318 FilePath lsb_release = test_dir_.Append("lsb-release"); |
236 FilePath payload_file = test_dir_.Append("payload-file"); | 319 FilePath payload_file = test_dir_.Append("payload-file"); |
237 std::string contents; | 320 std::string contents; |
238 collector_.lsb_release_ = lsb_release.value().c_str(); | 321 collector_.lsb_release_ = lsb_release.value().c_str(); |
239 const char kLsbContents[] = "CHROMEOS_RELEASE_VERSION=version\n"; | 322 const char kLsbContents[] = "CHROMEOS_RELEASE_VERSION=version\n"; |
240 ASSERT_TRUE( | 323 ASSERT_TRUE( |
241 file_util::WriteFile(lsb_release, | 324 file_util::WriteFile(lsb_release, |
242 kLsbContents, strlen(kLsbContents))); | 325 kLsbContents, strlen(kLsbContents))); |
243 const char kPayload[] = "foo"; | 326 const char kPayload[] = "foo"; |
244 ASSERT_TRUE( | 327 ASSERT_TRUE( |
245 file_util::WriteFile(payload_file, | 328 file_util::WriteFile(payload_file, |
246 kPayload, strlen(kPayload))); | 329 kPayload, strlen(kPayload))); |
247 collector_.AddCrashMetaData("foo", "bar"); | 330 collector_.AddCrashMetaData("foo", "bar"); |
248 collector_.WriteCrashMetaData(meta_file, "kernel", payload_file.value()); | 331 collector_.WriteCrashMetaData(meta_file, "kernel", payload_file.value()); |
249 EXPECT_TRUE(file_util::ReadFileToString(meta_file, &contents)); | 332 EXPECT_TRUE(file_util::ReadFileToString(meta_file, &contents)); |
250 EXPECT_EQ("foo=bar\n" | 333 const char kExpectedMeta[] = |
251 "exec_name=kernel\n" | 334 "foo=bar\n" |
252 "ver=version\n" | 335 "exec_name=kernel\n" |
253 "payload=test/payload-file\n" | 336 "ver=version\n" |
254 "payload_size=3\n" | 337 "payload=test/payload-file\n" |
255 "done=1\n", contents); | 338 "payload_size=3\n" |
| 339 "done=1\n"; |
| 340 EXPECT_EQ(kExpectedMeta, contents); |
| 341 |
| 342 // Test target of symlink is not overwritten. |
| 343 payload_file = test_dir_.Append("payload2-file"); |
| 344 ASSERT_TRUE( |
| 345 file_util::WriteFile(payload_file, |
| 346 kPayload, strlen(kPayload))); |
| 347 FilePath meta_symlink_path = test_dir_.Append("symlink.meta"); |
| 348 ASSERT_EQ(0, |
| 349 symlink(kMetaFileBasename, |
| 350 meta_symlink_path.value().c_str())); |
| 351 ASSERT_TRUE(file_util::PathExists(meta_symlink_path)); |
| 352 logging_.clear(); |
| 353 collector_.WriteCrashMetaData(meta_symlink_path, |
| 354 "kernel", |
| 355 payload_file.value()); |
| 356 // Target metadata contents sould have stayed the same. |
| 357 contents.clear(); |
| 358 EXPECT_TRUE(file_util::ReadFileToString(meta_file, &contents)); |
| 359 EXPECT_EQ(kExpectedMeta, contents); |
| 360 EXPECT_NE(std::string::npos, logging_.log().find("Unable to write")); |
| 361 |
| 362 // Test target of dangling symlink is not created. |
| 363 file_util::Delete(meta_file, false); |
| 364 ASSERT_FALSE(file_util::PathExists(meta_file)); |
| 365 logging_.clear(); |
| 366 collector_.WriteCrashMetaData(meta_symlink_path, "kernel", |
| 367 payload_file.value()); |
| 368 EXPECT_FALSE(file_util::PathExists(meta_file)); |
| 369 EXPECT_NE(std::string::npos, logging_.log().find("Unable to write")); |
256 } | 370 } |
257 | 371 |
258 int main(int argc, char **argv) { | 372 int main(int argc, char **argv) { |
259 ::testing::InitGoogleTest(&argc, argv); | 373 ::testing::InitGoogleTest(&argc, argv); |
260 return RUN_ALL_TESTS(); | 374 return RUN_ALL_TESTS(); |
261 } | 375 } |
OLD | NEW |