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 "crash-reporter/system_logging_mock.h" | 8 #include "crash-reporter/system_logging_mock.h" |
9 #include "crash-reporter/user_collector.h" | 9 #include "crash-reporter/user_collector.h" |
10 #include "crash-reporter/test_helpers.h" | 10 #include "crash-reporter/test_helpers.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 } | 25 } |
26 | 26 |
27 class UserCollectorTest : public ::testing::Test { | 27 class UserCollectorTest : public ::testing::Test { |
28 void SetUp() { | 28 void SetUp() { |
29 s_crashes = 0; | 29 s_crashes = 0; |
30 collector_.Initialize(CountCrash, | 30 collector_.Initialize(CountCrash, |
31 kFilePath, | 31 kFilePath, |
32 IsMetrics, | 32 IsMetrics, |
33 &logging_, | 33 &logging_, |
34 false); | 34 false); |
| 35 file_util::Delete(FilePath("test"), true); |
35 mkdir("test", 0777); | 36 mkdir("test", 0777); |
36 collector_.set_core_pattern_file("test/core_pattern"); | 37 collector_.set_core_pattern_file("test/core_pattern"); |
| 38 collector_.set_core_pipe_limit_file("test/core_pipe_limit"); |
37 pid_ = getpid(); | 39 pid_ = getpid(); |
38 } | 40 } |
39 protected: | 41 protected: |
40 void ExpectFileEquals(const char *golden, | 42 void ExpectFileEquals(const char *golden, |
41 const char *file_path) { | 43 const char *file_path) { |
42 std::string contents; | 44 std::string contents; |
43 EXPECT_TRUE(file_util::ReadFileToString(FilePath(file_path), | 45 EXPECT_TRUE(file_util::ReadFileToString(FilePath(file_path), |
44 &contents)); | 46 &contents)); |
45 EXPECT_EQ(golden, contents); | 47 EXPECT_EQ(golden, contents); |
46 } | 48 } |
47 | 49 |
48 SystemLoggingMock logging_; | 50 SystemLoggingMock logging_; |
49 UserCollector collector_; | 51 UserCollector collector_; |
50 pid_t pid_; | 52 pid_t pid_; |
51 }; | 53 }; |
52 | 54 |
53 TEST_F(UserCollectorTest, EnableOK) { | 55 TEST_F(UserCollectorTest, EnableOK) { |
54 ASSERT_TRUE(collector_.Enable()); | 56 ASSERT_TRUE(collector_.Enable()); |
55 ExpectFileEquals("|/my/path --signal=%s --pid=%p", "test/core_pattern"); | 57 ExpectFileEquals("|/my/path --signal=%s --pid=%p", "test/core_pattern"); |
| 58 ExpectFileEquals("4", "test/core_pipe_limit"); |
56 ASSERT_EQ(s_crashes, 0); | 59 ASSERT_EQ(s_crashes, 0); |
57 ASSERT_NE(logging_.log().find("Enabling user crash handling"), | 60 ASSERT_NE(logging_.log().find("Enabling user crash handling"), |
58 std::string::npos); | 61 std::string::npos); |
59 } | 62 } |
60 | 63 |
61 TEST_F(UserCollectorTest, EnableNoFileAccess) { | 64 TEST_F(UserCollectorTest, EnableNoPatternFileAccess) { |
62 collector_.set_core_pattern_file("/does_not_exist"); | 65 collector_.set_core_pattern_file("/does_not_exist"); |
63 ASSERT_FALSE(collector_.Enable()); | 66 ASSERT_FALSE(collector_.Enable()); |
64 ASSERT_EQ(s_crashes, 0); | 67 ASSERT_EQ(s_crashes, 0); |
65 ASSERT_NE(logging_.log().find("Enabling user crash handling"), | 68 ASSERT_NE(logging_.log().find("Enabling user crash handling"), |
66 std::string::npos); | 69 std::string::npos); |
67 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), | 70 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), |
68 std::string::npos); | 71 std::string::npos); |
69 } | 72 } |
70 | 73 |
| 74 TEST_F(UserCollectorTest, EnableNoPipeLimitFileAccess) { |
| 75 collector_.set_core_pipe_limit_file("/does_not_exist"); |
| 76 ASSERT_FALSE(collector_.Enable()); |
| 77 ASSERT_EQ(s_crashes, 0); |
| 78 // Core pattern should not be written if we cannot access the pipe limit |
| 79 // or otherwise we may set a pattern that results in infinite recursion. |
| 80 ASSERT_FALSE(file_util::PathExists(FilePath("test/core_pattern"))); |
| 81 ASSERT_NE(logging_.log().find("Enabling user crash handling"), |
| 82 std::string::npos); |
| 83 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), |
| 84 std::string::npos); |
| 85 } |
| 86 |
71 TEST_F(UserCollectorTest, DisableOK) { | 87 TEST_F(UserCollectorTest, DisableOK) { |
72 ASSERT_TRUE(collector_.Disable()); | 88 ASSERT_TRUE(collector_.Disable()); |
73 ExpectFileEquals("core", "test/core_pattern"); | 89 ExpectFileEquals("core", "test/core_pattern"); |
74 ASSERT_EQ(s_crashes, 0); | 90 ASSERT_EQ(s_crashes, 0); |
75 ASSERT_NE(logging_.log().find("Disabling user crash handling"), | 91 ASSERT_NE(logging_.log().find("Disabling user crash handling"), |
76 std::string::npos); | 92 std::string::npos); |
77 } | 93 } |
78 | 94 |
79 TEST_F(UserCollectorTest, DisableNoFileAccess) { | 95 TEST_F(UserCollectorTest, DisableNoFileAccess) { |
80 collector_.set_core_pattern_file("/does_not_exist"); | 96 collector_.set_core_pattern_file("/does_not_exist"); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 EXPECT_EQ(expectations[i].exists, | 275 EXPECT_EQ(expectations[i].exists, |
260 file_util::PathExists( | 276 file_util::PathExists( |
261 container_path.Append(expectations[i].name))); | 277 container_path.Append(expectations[i].name))); |
262 } | 278 } |
263 } | 279 } |
264 | 280 |
265 int main(int argc, char **argv) { | 281 int main(int argc, char **argv) { |
266 ::testing::InitGoogleTest(&argc, argv); | 282 ::testing::InitGoogleTest(&argc, argv); |
267 return RUN_ALL_TESTS(); | 283 return RUN_ALL_TESTS(); |
268 } | 284 } |
OLD | NEW |