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 "crash-reporter/crash_collector.h" | 9 #include "crash-reporter/crash_collector.h" |
9 #include "crash-reporter/system_logging_mock.h" | 10 #include "crash-reporter/system_logging_mock.h" |
10 #include "gflags/gflags.h" | 11 #include "gflags/gflags.h" |
11 #include "gtest/gtest.h" | 12 #include "gtest/gtest.h" |
12 | 13 |
13 void CountCrash() { | 14 void CountCrash() { |
14 ADD_FAILURE(); | 15 ADD_FAILURE(); |
15 } | 16 } |
16 | 17 |
17 bool IsMetrics() { | 18 bool IsMetrics() { |
18 ADD_FAILURE(); | 19 ADD_FAILURE(); |
19 return false; | 20 return false; |
20 } | 21 } |
21 | 22 |
22 class CrashCollectorTest : public ::testing::Test { | 23 class CrashCollectorTest : public ::testing::Test { |
| 24 public: |
23 void SetUp() { | 25 void SetUp() { |
24 collector_.Initialize(CountCrash, | 26 collector_.Initialize(CountCrash, |
25 IsMetrics, | 27 IsMetrics, |
26 &logging_); | 28 &logging_); |
| 29 test_dir_ = FilePath("test"); |
| 30 file_util::CreateDirectory(test_dir_); |
27 } | 31 } |
| 32 |
| 33 void TearDown() { |
| 34 file_util::Delete(test_dir_, true); |
| 35 } |
| 36 |
| 37 bool CheckHasCapacity(); |
| 38 |
28 protected: | 39 protected: |
29 SystemLoggingMock logging_; | 40 SystemLoggingMock logging_; |
30 CrashCollector collector_; | 41 CrashCollector collector_; |
31 pid_t pid_; | 42 FilePath test_dir_; |
32 }; | 43 }; |
33 | 44 |
34 TEST_F(CrashCollectorTest, Initialize) { | 45 TEST_F(CrashCollectorTest, Initialize) { |
35 ASSERT_TRUE(CountCrash == collector_.count_crash_function_); | 46 ASSERT_TRUE(CountCrash == collector_.count_crash_function_); |
36 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_); | 47 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_); |
37 ASSERT_TRUE(&logging_ == collector_.logger_); | 48 ASSERT_TRUE(&logging_ == collector_.logger_); |
38 } | 49 } |
39 | 50 |
40 TEST_F(CrashCollectorTest, GetCrashDirectoryInfo) { | 51 TEST_F(CrashCollectorTest, GetCrashDirectoryInfo) { |
41 FilePath path; | 52 FilePath path; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 tm.tm_hour = 13; | 103 tm.tm_hour = 13; |
93 tm.tm_mday = 23; | 104 tm.tm_mday = 23; |
94 tm.tm_mon = 4; | 105 tm.tm_mon = 4; |
95 tm.tm_year = 110; | 106 tm.tm_year = 110; |
96 tm.tm_isdst = -1; | 107 tm.tm_isdst = -1; |
97 std::string basename = | 108 std::string basename = |
98 collector_.FormatDumpBasename("foo", mktime(&tm), 100); | 109 collector_.FormatDumpBasename("foo", mktime(&tm), 100); |
99 ASSERT_EQ("foo.20100523.135015.100", basename); | 110 ASSERT_EQ("foo.20100523.135015.100", basename); |
100 } | 111 } |
101 | 112 |
| 113 bool CrashCollectorTest::CheckHasCapacity() { |
| 114 static const char kFullMessage[] = "Crash directory test already full"; |
| 115 bool has_capacity = collector_.CheckHasCapacity(test_dir_); |
| 116 bool has_message = (logging_.log().find(kFullMessage) != std::string::npos); |
| 117 EXPECT_EQ(has_message, !has_capacity); |
| 118 return has_capacity; |
| 119 } |
| 120 |
| 121 TEST_F(CrashCollectorTest, CheckHasCapacityOverNonCore) { |
| 122 // Test up to kMaxCrashDirectorySize-1 non-core files can be added. |
| 123 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) { |
| 124 EXPECT_TRUE(CheckHasCapacity()); |
| 125 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d", i)), "", 0); |
| 126 } |
| 127 |
| 128 // Test an additional kMaxCrashDirectorySize - 1 core files fit. |
| 129 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) { |
| 130 EXPECT_TRUE(CheckHasCapacity()); |
| 131 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)), |
| 132 "", 0); |
| 133 } |
| 134 |
| 135 // Test an additional kMaxCrashDirectorySize non-core files don't fit. |
| 136 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize; ++i) { |
| 137 file_util::WriteFile(test_dir_.Append(StringPrintf("overage%d", i)), "", 0); |
| 138 EXPECT_FALSE(CheckHasCapacity()); |
| 139 } |
| 140 } |
| 141 |
| 142 TEST_F(CrashCollectorTest, CheckHasCapacityOverCore) { |
| 143 // Set up kMaxCrashDirectorySize - 1 core files. |
| 144 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) { |
| 145 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)), |
| 146 "", 0); |
| 147 } |
| 148 |
| 149 EXPECT_TRUE(CheckHasCapacity()); |
| 150 |
| 151 // Test an additional core file does not fit. |
| 152 file_util::WriteFile(test_dir_.Append("overage.core"), "", 0); |
| 153 EXPECT_FALSE(CheckHasCapacity()); |
| 154 } |
| 155 |
102 int main(int argc, char **argv) { | 156 int main(int argc, char **argv) { |
103 ::testing::InitGoogleTest(&argc, argv); | 157 ::testing::InitGoogleTest(&argc, argv); |
104 return RUN_ALL_TESTS(); | 158 return RUN_ALL_TESTS(); |
105 } | 159 } |
OLD | NEW |