| 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 "gflags/gflags.h" | 10 #include "gflags/gflags.h" |
| 11 #include "gtest/gtest.h" | 11 #include "gtest/gtest.h" |
| 12 | 12 |
| 13 int s_crashes = 0; | 13 static int s_crashes = 0; |
| 14 bool s_metrics = false; | 14 static bool s_metrics = false; |
| 15 | 15 |
| 16 static const char kFilePath[] = "/my/path"; | 16 static const char kFilePath[] = "/my/path"; |
| 17 | 17 |
| 18 void CountCrash() { | 18 void CountCrash() { |
| 19 ++s_crashes; | 19 ++s_crashes; |
| 20 } | 20 } |
| 21 | 21 |
| 22 bool IsMetrics() { | 22 bool IsMetrics() { |
| 23 return s_metrics; | 23 return s_metrics; |
| 24 } | 24 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 pid_t pid_; | 43 pid_t pid_; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 TEST_F(UserCollectorTest, EnableOK) { | 46 TEST_F(UserCollectorTest, EnableOK) { |
| 47 std::string contents; | 47 std::string contents; |
| 48 ASSERT_TRUE(collector_.Enable()); | 48 ASSERT_TRUE(collector_.Enable()); |
| 49 ASSERT_TRUE(file_util::ReadFileToString(FilePath("test/core_pattern"), | 49 ASSERT_TRUE(file_util::ReadFileToString(FilePath("test/core_pattern"), |
| 50 &contents)); | 50 &contents)); |
| 51 ASSERT_EQ("|/my/path --signal=%s --pid=%p", contents); | 51 ASSERT_EQ("|/my/path --signal=%s --pid=%p", contents); |
| 52 ASSERT_EQ(s_crashes, 0); | 52 ASSERT_EQ(s_crashes, 0); |
| 53 ASSERT_NE(logging_.log().find("Enabling crash handling"), std::string::npos); | 53 ASSERT_NE(logging_.log().find("Enabling user crash handling"), |
| 54 std::string::npos); |
| 54 } | 55 } |
| 55 | 56 |
| 56 TEST_F(UserCollectorTest, EnableNoFileAccess) { | 57 TEST_F(UserCollectorTest, EnableNoFileAccess) { |
| 57 collector_.set_core_pattern_file("/does_not_exist"); | 58 collector_.set_core_pattern_file("/does_not_exist"); |
| 58 ASSERT_FALSE(collector_.Enable()); | 59 ASSERT_FALSE(collector_.Enable()); |
| 59 ASSERT_EQ(s_crashes, 0); | 60 ASSERT_EQ(s_crashes, 0); |
| 60 ASSERT_NE(logging_.log().find("Enabling crash handling"), std::string::npos); | 61 ASSERT_NE(logging_.log().find("Enabling user crash handling"), |
| 62 std::string::npos); |
| 61 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), | 63 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), |
| 62 std::string::npos); | 64 std::string::npos); |
| 63 } | 65 } |
| 64 | 66 |
| 65 TEST_F(UserCollectorTest, DisableOK) { | 67 TEST_F(UserCollectorTest, DisableOK) { |
| 66 std::string contents; | 68 std::string contents; |
| 67 ASSERT_TRUE(collector_.Disable()); | 69 ASSERT_TRUE(collector_.Disable()); |
| 68 ASSERT_TRUE(file_util::ReadFileToString(FilePath("test/core_pattern"), | 70 ASSERT_TRUE(file_util::ReadFileToString(FilePath("test/core_pattern"), |
| 69 &contents)); | 71 &contents)); |
| 70 ASSERT_EQ("core", contents); | 72 ASSERT_EQ("core", contents); |
| 71 ASSERT_EQ(s_crashes, 0); | 73 ASSERT_EQ(s_crashes, 0); |
| 72 ASSERT_NE(logging_.log().find("Disabling crash handling"), | 74 ASSERT_NE(logging_.log().find("Disabling user crash handling"), |
| 73 std::string::npos); | 75 std::string::npos); |
| 74 } | 76 } |
| 75 | 77 |
| 76 TEST_F(UserCollectorTest, DisableNoFileAccess) { | 78 TEST_F(UserCollectorTest, DisableNoFileAccess) { |
| 77 collector_.set_core_pattern_file("/does_not_exist"); | 79 collector_.set_core_pattern_file("/does_not_exist"); |
| 78 ASSERT_FALSE(collector_.Disable()); | 80 ASSERT_FALSE(collector_.Disable()); |
| 79 ASSERT_EQ(s_crashes, 0); | 81 ASSERT_EQ(s_crashes, 0); |
| 80 ASSERT_NE(logging_.log().find("Disabling crash handling"), std::string::npos); | 82 ASSERT_NE(logging_.log().find("Disabling user crash handling"), |
| 83 std::string::npos); |
| 81 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), | 84 ASSERT_NE(logging_.log().find("Unable to write /does_not_exist"), |
| 82 std::string::npos); | 85 std::string::npos); |
| 83 } | 86 } |
| 84 | 87 |
| 85 TEST_F(UserCollectorTest, HandleCrashWithoutMetrics) { | 88 TEST_F(UserCollectorTest, HandleCrashWithoutMetrics) { |
| 86 s_metrics = false; | 89 s_metrics = false; |
| 87 collector_.HandleCrash(10, 20, "foobar"); | 90 collector_.HandleCrash(10, 20, "foobar"); |
| 88 ASSERT_NE(logging_.log().find( | 91 ASSERT_NE(logging_.log().find( |
| 89 "Received crash notification for foobar[20] sig 10"), | 92 "Received crash notification for foobar[20] sig 10"), |
| 90 std::string::npos); | 93 std::string::npos); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 } | 202 } |
| 200 | 203 |
| 201 TEST_F(UserCollectorTest, GetUserInfoFromName) { | 204 TEST_F(UserCollectorTest, GetUserInfoFromName) { |
| 202 gid_t gid = 100; | 205 gid_t gid = 100; |
| 203 uid_t uid = 100; | 206 uid_t uid = 100; |
| 204 EXPECT_TRUE(collector_.GetUserInfoFromName("root", &uid, &gid)); | 207 EXPECT_TRUE(collector_.GetUserInfoFromName("root", &uid, &gid)); |
| 205 EXPECT_EQ(0, uid); | 208 EXPECT_EQ(0, uid); |
| 206 EXPECT_EQ(0, gid); | 209 EXPECT_EQ(0, gid); |
| 207 } | 210 } |
| 208 | 211 |
| 209 TEST_F(UserCollectorTest, GetCrashDirectoryInfo) { | |
| 210 FilePath path; | |
| 211 const int kRootUid = 0; | |
| 212 const int kRootGid = 0; | |
| 213 const int kNtpUid = 5; | |
| 214 const int kChronosUid = 1000; | |
| 215 const int kChronosGid = 1001; | |
| 216 const mode_t kExpectedSystemMode = 01755; | |
| 217 const mode_t kExpectedUserMode = 0755; | |
| 218 | |
| 219 mode_t directory_mode; | |
| 220 uid_t directory_owner; | |
| 221 gid_t directory_group; | |
| 222 | |
| 223 path = collector_.GetCrashDirectoryInfo(kRootUid, | |
| 224 kChronosUid, | |
| 225 kChronosGid, | |
| 226 &directory_mode, | |
| 227 &directory_owner, | |
| 228 &directory_group); | |
| 229 EXPECT_EQ("/var/spool/crash", path.value()); | |
| 230 EXPECT_EQ(kExpectedSystemMode, directory_mode); | |
| 231 EXPECT_EQ(kRootUid, directory_owner); | |
| 232 EXPECT_EQ(kRootGid, directory_group); | |
| 233 | |
| 234 path = collector_.GetCrashDirectoryInfo(kNtpUid, | |
| 235 kChronosUid, | |
| 236 kChronosGid, | |
| 237 &directory_mode, | |
| 238 &directory_owner, | |
| 239 &directory_group); | |
| 240 EXPECT_EQ("/var/spool/crash", path.value()); | |
| 241 EXPECT_EQ(kExpectedSystemMode, directory_mode); | |
| 242 EXPECT_EQ(kRootUid, directory_owner); | |
| 243 EXPECT_EQ(kRootGid, directory_group); | |
| 244 | |
| 245 path = collector_.GetCrashDirectoryInfo(kChronosUid, | |
| 246 kChronosUid, | |
| 247 kChronosGid, | |
| 248 &directory_mode, | |
| 249 &directory_owner, | |
| 250 &directory_group); | |
| 251 EXPECT_EQ("/home/chronos/user/crash", path.value()); | |
| 252 EXPECT_EQ(kExpectedUserMode, directory_mode); | |
| 253 EXPECT_EQ(kChronosUid, directory_owner); | |
| 254 EXPECT_EQ(kChronosGid, directory_group); | |
| 255 } | |
| 256 | |
| 257 TEST_F(UserCollectorTest, CopyOffProcFilesBadPath) { | 212 TEST_F(UserCollectorTest, CopyOffProcFilesBadPath) { |
| 258 // Try a path that is not writable. | 213 // Try a path that is not writable. |
| 259 ASSERT_FALSE(collector_.CopyOffProcFiles(pid_, FilePath("/bad/path"))); | 214 ASSERT_FALSE(collector_.CopyOffProcFiles(pid_, FilePath("/bad/path"))); |
| 260 ASSERT_NE(logging_.log().find( | 215 ASSERT_NE(logging_.log().find( |
| 261 "Could not create /bad/path"), | 216 "Could not create /bad/path"), |
| 262 std::string::npos); | 217 std::string::npos); |
| 263 } | 218 } |
| 264 | 219 |
| 265 TEST_F(UserCollectorTest, CopyOffProcFilesBadPid) { | 220 TEST_F(UserCollectorTest, CopyOffProcFilesBadPid) { |
| 266 FilePath container_path("test/container"); | 221 FilePath container_path("test/container"); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 288 { "sched", false }, | 243 { "sched", false }, |
| 289 { "status", true } | 244 { "status", true } |
| 290 }; | 245 }; |
| 291 for (unsigned i = 0; i < sizeof(expectations)/sizeof(expectations[0]); ++i) { | 246 for (unsigned i = 0; i < sizeof(expectations)/sizeof(expectations[0]); ++i) { |
| 292 EXPECT_EQ(expectations[i].exists, | 247 EXPECT_EQ(expectations[i].exists, |
| 293 file_util::PathExists( | 248 file_util::PathExists( |
| 294 container_path.Append(expectations[i].name))); | 249 container_path.Append(expectations[i].name))); |
| 295 } | 250 } |
| 296 } | 251 } |
| 297 | 252 |
| 298 TEST_F(UserCollectorTest, FormatDumpBasename) { | |
| 299 struct tm tm = {0}; | |
| 300 tm.tm_sec = 15; | |
| 301 tm.tm_min = 50; | |
| 302 tm.tm_hour = 13; | |
| 303 tm.tm_mday = 23; | |
| 304 tm.tm_mon = 4; | |
| 305 tm.tm_year = 110; | |
| 306 tm.tm_isdst = -1; | |
| 307 std::string basename = | |
| 308 collector_.FormatDumpBasename("foo", mktime(&tm), 100); | |
| 309 ASSERT_EQ("foo.20100523.135015.100", basename); | |
| 310 } | |
| 311 | |
| 312 int main(int argc, char **argv) { | 253 int main(int argc, char **argv) { |
| 313 ::testing::InitGoogleTest(&argc, argv); | 254 ::testing::InitGoogleTest(&argc, argv); |
| 314 return RUN_ALL_TESTS(); | 255 return RUN_ALL_TESTS(); |
| 315 } | 256 } |
| OLD | NEW |