| 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" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 const char kBuffer[] = "buffer"; | 60 const char kBuffer[] = "buffer"; |
| 61 EXPECT_EQ(strlen(kBuffer), | 61 EXPECT_EQ(strlen(kBuffer), |
| 62 collector_.WriteNewFile(test_file, | 62 collector_.WriteNewFile(test_file, |
| 63 kBuffer, | 63 kBuffer, |
| 64 strlen(kBuffer))); | 64 strlen(kBuffer))); |
| 65 EXPECT_LT(collector_.WriteNewFile(test_file, | 65 EXPECT_LT(collector_.WriteNewFile(test_file, |
| 66 kBuffer, | 66 kBuffer, |
| 67 strlen(kBuffer)), 0); | 67 strlen(kBuffer)), 0); |
| 68 } | 68 } |
| 69 | 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 | |
| 133 TEST_F(CrashCollectorTest, Sanitize) { | 70 TEST_F(CrashCollectorTest, Sanitize) { |
| 134 EXPECT_EQ("chrome", collector_.Sanitize("chrome")); | 71 EXPECT_EQ("chrome", collector_.Sanitize("chrome")); |
| 135 EXPECT_EQ("CHROME", collector_.Sanitize("CHROME")); | 72 EXPECT_EQ("CHROME", collector_.Sanitize("CHROME")); |
| 136 EXPECT_EQ("1chrome2", collector_.Sanitize("1chrome2")); | 73 EXPECT_EQ("1chrome2", collector_.Sanitize("1chrome2")); |
| 137 EXPECT_EQ("chrome__deleted_", collector_.Sanitize("chrome (deleted)")); | 74 EXPECT_EQ("chrome__deleted_", collector_.Sanitize("chrome (deleted)")); |
| 138 EXPECT_EQ("foo_bar", collector_.Sanitize("foo.bar")); | 75 EXPECT_EQ("foo_bar", collector_.Sanitize("foo.bar")); |
| 139 EXPECT_EQ("", collector_.Sanitize("")); | 76 EXPECT_EQ("", collector_.Sanitize("")); |
| 140 EXPECT_EQ("_", collector_.Sanitize(" ")); | 77 EXPECT_EQ("_", collector_.Sanitize(" ")); |
| 141 } | 78 } |
| 142 | 79 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 } | 319 } |
| 383 | 320 |
| 384 TEST_F(CrashCollectorTest, GetLogContents) { | 321 TEST_F(CrashCollectorTest, GetLogContents) { |
| 385 FilePath config_file = test_dir_.Append("crash_config"); | 322 FilePath config_file = test_dir_.Append("crash_config"); |
| 386 FilePath output_file = test_dir_.Append("crash_log"); | 323 FilePath output_file = test_dir_.Append("crash_log"); |
| 387 const char kConfigContents[] = | 324 const char kConfigContents[] = |
| 388 "foobar:echo hello there | sed -e \"s/there/world/\""; | 325 "foobar:echo hello there | sed -e \"s/there/world/\""; |
| 389 ASSERT_TRUE( | 326 ASSERT_TRUE( |
| 390 file_util::WriteFile(config_file, | 327 file_util::WriteFile(config_file, |
| 391 kConfigContents, strlen(kConfigContents))); | 328 kConfigContents, strlen(kConfigContents))); |
| 329 file_util::Delete(FilePath(output_file), false); |
| 392 EXPECT_FALSE(collector_.GetLogContents(config_file, | 330 EXPECT_FALSE(collector_.GetLogContents(config_file, |
| 393 "barfoo", | 331 "barfoo", |
| 394 output_file)); | 332 output_file)); |
| 395 EXPECT_FALSE(file_util::PathExists(output_file)); | 333 EXPECT_FALSE(file_util::PathExists(output_file)); |
| 334 file_util::Delete(FilePath(output_file), false); |
| 396 EXPECT_TRUE(collector_.GetLogContents(config_file, | 335 EXPECT_TRUE(collector_.GetLogContents(config_file, |
| 397 "foobar", | 336 "foobar", |
| 398 output_file)); | 337 output_file)); |
| 399 ASSERT_TRUE(file_util::PathExists(output_file)); | 338 ASSERT_TRUE(file_util::PathExists(output_file)); |
| 400 std::string contents; | 339 std::string contents; |
| 401 EXPECT_TRUE(file_util::ReadFileToString(output_file, &contents)); | 340 EXPECT_TRUE(file_util::ReadFileToString(output_file, &contents)); |
| 402 EXPECT_EQ("hello world\n", contents); | 341 EXPECT_EQ("hello world\n", contents); |
| 403 } | 342 } |
| 404 | 343 |
| 344 class ForkExecAndPipeTest : public CrashCollectorTest { |
| 345 public: |
| 346 void SetUp() { |
| 347 CrashCollectorTest::SetUp(); |
| 348 output_file_ = "test/fork_out"; |
| 349 file_util::Delete(FilePath(output_file_), false); |
| 350 } |
| 351 |
| 352 void TearDown() { |
| 353 CrashCollectorTest::TearDown(); |
| 354 } |
| 355 |
| 356 protected: |
| 357 std::vector<const char *> args_; |
| 358 const char *output_file_; |
| 359 }; |
| 360 |
| 361 TEST_F(ForkExecAndPipeTest, Basic) { |
| 362 args_.push_back(kBinEcho); |
| 363 args_.push_back("hello world"); |
| 364 EXPECT_EQ(0, collector_.ForkExecAndPipe(args_, output_file_)); |
| 365 ExpectFileEquals("hello world\n", output_file_); |
| 366 EXPECT_EQ("", logging_.log()); |
| 367 } |
| 368 |
| 369 TEST_F(ForkExecAndPipeTest, NonZeroReturnValue) { |
| 370 args_.push_back(kBinFalse); |
| 371 EXPECT_EQ(1, collector_.ForkExecAndPipe(args_, output_file_)); |
| 372 ExpectFileEquals("", output_file_); |
| 373 EXPECT_EQ("", logging_.log()); |
| 374 } |
| 375 |
| 376 TEST_F(ForkExecAndPipeTest, BadOutputFile) { |
| 377 EXPECT_EQ(127, collector_.ForkExecAndPipe(args_, "/bad/path")); |
| 378 } |
| 379 |
| 380 TEST_F(ForkExecAndPipeTest, ExistingOutputFile) { |
| 381 args_.push_back(kBinEcho); |
| 382 args_.push_back("hello world"); |
| 383 EXPECT_FALSE(file_util::PathExists(FilePath(output_file_))); |
| 384 EXPECT_EQ(0, collector_.ForkExecAndPipe(args_, output_file_)); |
| 385 EXPECT_TRUE(file_util::PathExists(FilePath(output_file_))); |
| 386 EXPECT_EQ(127, collector_.ForkExecAndPipe(args_, output_file_)); |
| 387 } |
| 388 |
| 389 TEST_F(ForkExecAndPipeTest, BadExecutable) { |
| 390 args_.push_back("false"); |
| 391 EXPECT_EQ(127, collector_.ForkExecAndPipe(args_, output_file_)); |
| 392 } |
| 393 |
| 394 TEST_F(ForkExecAndPipeTest, StderrCaptured) { |
| 395 std::string contents; |
| 396 args_.push_back(kBinCp); |
| 397 EXPECT_EQ(1, collector_.ForkExecAndPipe(args_, output_file_)); |
| 398 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), |
| 399 &contents)); |
| 400 EXPECT_NE(std::string::npos, contents.find("missing file operand")); |
| 401 EXPECT_EQ("", logging_.log()); |
| 402 } |
| 403 |
| 404 TEST_F(ForkExecAndPipeTest, NULLParam) { |
| 405 args_.push_back(NULL); |
| 406 EXPECT_EQ(-1, collector_.ForkExecAndPipe(args_, output_file_)); |
| 407 EXPECT_NE(std::string::npos, |
| 408 logging_.log().find("Bad parameter")); |
| 409 } |
| 410 |
| 411 TEST_F(ForkExecAndPipeTest, NoParams) { |
| 412 EXPECT_EQ(127, collector_.ForkExecAndPipe(args_, output_file_)); |
| 413 } |
| 414 |
| 415 TEST_F(ForkExecAndPipeTest, SegFaultHandling) { |
| 416 args_.push_back(kBinBash); |
| 417 args_.push_back("-c"); |
| 418 args_.push_back("kill -SEGV $$"); |
| 419 EXPECT_EQ(-1, collector_.ForkExecAndPipe(args_, output_file_)); |
| 420 EXPECT_NE(std::string::npos, |
| 421 logging_.log().find("Process did not exit normally")); |
| 422 } |
| 423 |
| 405 int main(int argc, char **argv) { | 424 int main(int argc, char **argv) { |
| 406 ::testing::InitGoogleTest(&argc, argv); | 425 ::testing::InitGoogleTest(&argc, argv); |
| 407 return RUN_ALL_TESTS(); | 426 return RUN_ALL_TESTS(); |
| 408 } | 427 } |
| OLD | NEW |