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 "gflags/gflags.h" | 11 #include "gflags/gflags.h" |
11 #include "gtest/gtest.h" | 12 #include "gtest/gtest.h" |
12 | 13 |
13 static int s_crashes = 0; | 14 static int s_crashes = 0; |
14 static bool s_metrics = false; | 15 static bool s_metrics = false; |
15 | 16 |
16 static const char kFilePath[] = "/my/path"; | 17 static const char kFilePath[] = "/my/path"; |
17 | 18 |
18 // This test assumes the following standard binaries are installed. | |
19 static const char kBinBash[] = "/bin/bash"; | |
20 static const char kBinCp[] = "/bin/cp"; | |
21 static const char kBinEcho[] = "/bin/echo"; | |
22 static const char kBinFalse[] = "/bin/false"; | |
23 | |
24 void CountCrash() { | 19 void CountCrash() { |
25 ++s_crashes; | 20 ++s_crashes; |
26 } | 21 } |
27 | 22 |
28 bool IsMetrics() { | 23 bool IsMetrics() { |
29 return s_metrics; | 24 return s_metrics; |
30 } | 25 } |
31 | 26 |
32 class UserCollectorTest : public ::testing::Test { | 27 class UserCollectorTest : public ::testing::Test { |
33 void SetUp() { | 28 void SetUp() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 TEST_F(UserCollectorTest, DisableNoFileAccess) { | 79 TEST_F(UserCollectorTest, DisableNoFileAccess) { |
85 collector_.set_core_pattern_file("/does_not_exist"); | 80 collector_.set_core_pattern_file("/does_not_exist"); |
86 ASSERT_FALSE(collector_.Disable()); | 81 ASSERT_FALSE(collector_.Disable()); |
87 ASSERT_EQ(s_crashes, 0); | 82 ASSERT_EQ(s_crashes, 0); |
88 ASSERT_NE(logging_.log().find("Disabling user crash handling"), | 83 ASSERT_NE(logging_.log().find("Disabling user crash handling"), |
89 std::string::npos); | 84 std::string::npos); |
90 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), | 85 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), |
91 std::string::npos); | 86 std::string::npos); |
92 } | 87 } |
93 | 88 |
94 TEST_F(UserCollectorTest, ForkExecAndPipe) { | |
95 std::vector<const char *> args; | |
96 char output_file[] = "test/fork_out"; | |
97 | |
98 // Test basic call with stdout. | |
99 args.clear(); | |
100 args.push_back(kBinEcho); | |
101 args.push_back("hello world"); | |
102 EXPECT_EQ(0, collector_.ForkExecAndPipe(args, output_file)); | |
103 ExpectFileEquals("hello world\n", output_file); | |
104 EXPECT_EQ("", logging_.log()); | |
105 | |
106 // Test non-zero return value | |
107 logging_.clear(); | |
108 args.clear(); | |
109 args.push_back(kBinFalse); | |
110 EXPECT_EQ(1, collector_.ForkExecAndPipe(args, output_file)); | |
111 ExpectFileEquals("", output_file); | |
112 EXPECT_EQ("", logging_.log()); | |
113 | |
114 // Test bad output_file. | |
115 EXPECT_EQ(127, collector_.ForkExecAndPipe(args, "/bad/path")); | |
116 | |
117 // Test bad executable. | |
118 logging_.clear(); | |
119 args.clear(); | |
120 args.push_back("false"); | |
121 EXPECT_EQ(127, collector_.ForkExecAndPipe(args, output_file)); | |
122 | |
123 // Test stderr captured. | |
124 std::string contents; | |
125 logging_.clear(); | |
126 args.clear(); | |
127 args.push_back(kBinCp); | |
128 EXPECT_EQ(1, collector_.ForkExecAndPipe(args, output_file)); | |
129 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file), | |
130 &contents)); | |
131 EXPECT_NE(std::string::npos, contents.find("missing file operand")); | |
132 EXPECT_EQ("", logging_.log()); | |
133 | |
134 // NULL parameter. | |
135 logging_.clear(); | |
136 args.clear(); | |
137 args.push_back(NULL); | |
138 EXPECT_EQ(-1, collector_.ForkExecAndPipe(args, output_file)); | |
139 EXPECT_NE(std::string::npos, | |
140 logging_.log().find("Bad parameter")); | |
141 | |
142 // No parameters. | |
143 args.clear(); | |
144 EXPECT_EQ(127, collector_.ForkExecAndPipe(args, output_file)); | |
145 | |
146 // Segmentation faulting process. | |
147 logging_.clear(); | |
148 args.clear(); | |
149 args.push_back(kBinBash); | |
150 args.push_back("-c"); | |
151 args.push_back("kill -SEGV $$"); | |
152 EXPECT_EQ(-1, collector_.ForkExecAndPipe(args, output_file)); | |
153 EXPECT_NE(std::string::npos, | |
154 logging_.log().find("Process did not exit normally")); | |
155 } | |
156 | |
157 TEST_F(UserCollectorTest, HandleCrashWithoutMetrics) { | 89 TEST_F(UserCollectorTest, HandleCrashWithoutMetrics) { |
158 s_metrics = false; | 90 s_metrics = false; |
159 collector_.HandleCrash(10, 20, "foobar"); | 91 collector_.HandleCrash(10, 20, "foobar"); |
160 ASSERT_NE(logging_.log().find( | 92 ASSERT_NE(logging_.log().find( |
161 "Received crash notification for foobar[20] sig 10"), | 93 "Received crash notification for foobar[20] sig 10"), |
162 std::string::npos); | 94 std::string::npos); |
163 ASSERT_EQ(s_crashes, 0); | 95 ASSERT_EQ(s_crashes, 0); |
164 } | 96 } |
165 | 97 |
166 TEST_F(UserCollectorTest, HandleCrashWithMetrics) { | 98 TEST_F(UserCollectorTest, HandleCrashWithMetrics) { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 EXPECT_EQ(expectations[i].exists, | 248 EXPECT_EQ(expectations[i].exists, |
317 file_util::PathExists( | 249 file_util::PathExists( |
318 container_path.Append(expectations[i].name))); | 250 container_path.Append(expectations[i].name))); |
319 } | 251 } |
320 } | 252 } |
321 | 253 |
322 int main(int argc, char **argv) { | 254 int main(int argc, char **argv) { |
323 ::testing::InitGoogleTest(&argc, argv); | 255 ::testing::InitGoogleTest(&argc, argv); |
324 return RUN_ALL_TESTS(); | 256 return RUN_ALL_TESTS(); |
325 } | 257 } |
OLD | NEW |