Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: crash_collector_test.cc

Issue 3436029: crash-reporter: Send OS version at time of crash and related improvements (Closed) Base URL: http://git.chromium.org/git/crash-reporter.git
Patch Set: respond to petkov review Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « crash_collector.cc ('k') | crash_reporter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 30 matching lines...) Expand all
41 CrashCollector collector_; 41 CrashCollector collector_;
42 FilePath test_dir_; 42 FilePath test_dir_;
43 }; 43 };
44 44
45 TEST_F(CrashCollectorTest, Initialize) { 45 TEST_F(CrashCollectorTest, Initialize) {
46 ASSERT_TRUE(CountCrash == collector_.count_crash_function_); 46 ASSERT_TRUE(CountCrash == collector_.count_crash_function_);
47 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_); 47 ASSERT_TRUE(IsMetrics == collector_.is_feedback_allowed_function_);
48 ASSERT_TRUE(&logging_ == collector_.logger_); 48 ASSERT_TRUE(&logging_ == collector_.logger_);
49 } 49 }
50 50
51 TEST_F(CrashCollectorTest, Sanitize) {
52 EXPECT_EQ("chrome", collector_.Sanitize("chrome"));
53 EXPECT_EQ("CHROME", collector_.Sanitize("CHROME"));
54 EXPECT_EQ("1chrome2", collector_.Sanitize("1chrome2"));
55 EXPECT_EQ("chrome__deleted_", collector_.Sanitize("chrome (deleted)"));
56 EXPECT_EQ("foo_bar", collector_.Sanitize("foo.bar"));
57 EXPECT_EQ("", collector_.Sanitize(""));
58 EXPECT_EQ("_", collector_.Sanitize(" "));
59 }
60
51 TEST_F(CrashCollectorTest, GetCrashDirectoryInfo) { 61 TEST_F(CrashCollectorTest, GetCrashDirectoryInfo) {
52 FilePath path; 62 FilePath path;
53 const int kRootUid = 0; 63 const int kRootUid = 0;
54 const int kRootGid = 0; 64 const int kRootGid = 0;
55 const int kNtpUid = 5; 65 const int kNtpUid = 5;
56 const int kChronosUid = 1000; 66 const int kChronosUid = 1000;
57 const int kChronosGid = 1001; 67 const int kChronosGid = 1001;
58 const mode_t kExpectedSystemMode = 01755; 68 const mode_t kExpectedSystemMode = 01755;
59 const mode_t kExpectedUserMode = 0755; 69 const mode_t kExpectedUserMode = 0755;
60 70
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 121 }
112 122
113 bool CrashCollectorTest::CheckHasCapacity() { 123 bool CrashCollectorTest::CheckHasCapacity() {
114 static const char kFullMessage[] = "Crash directory test already full"; 124 static const char kFullMessage[] = "Crash directory test already full";
115 bool has_capacity = collector_.CheckHasCapacity(test_dir_); 125 bool has_capacity = collector_.CheckHasCapacity(test_dir_);
116 bool has_message = (logging_.log().find(kFullMessage) != std::string::npos); 126 bool has_message = (logging_.log().find(kFullMessage) != std::string::npos);
117 EXPECT_EQ(has_message, !has_capacity); 127 EXPECT_EQ(has_message, !has_capacity);
118 return has_capacity; 128 return has_capacity;
119 } 129 }
120 130
121 TEST_F(CrashCollectorTest, CheckHasCapacityOverNonCore) { 131 TEST_F(CrashCollectorTest, CheckHasCapacityUsual) {
122 // Test up to kMaxCrashDirectorySize-1 non-core files can be added. 132 // Test kMaxCrashDirectorySize - 1 non-meta files can be added.
123 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) { 133 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) {
134 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)),
135 "", 0);
124 EXPECT_TRUE(CheckHasCapacity()); 136 EXPECT_TRUE(CheckHasCapacity());
125 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d", i)), "", 0);
126 } 137 }
127 138
128 // Test an additional kMaxCrashDirectorySize - 1 core files fit. 139 // Test an additional kMaxCrashDirectorySize - 1 meta files fit.
129 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) { 140 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) {
141 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.meta", i)),
142 "", 0);
130 EXPECT_TRUE(CheckHasCapacity()); 143 EXPECT_TRUE(CheckHasCapacity());
131 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)),
132 "", 0);
133 } 144 }
134 145
135 // Test an additional kMaxCrashDirectorySize non-core files don't fit. 146 // Test an additional kMaxCrashDirectorySize meta files don't fit.
136 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize; ++i) { 147 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize; ++i) {
137 file_util::WriteFile(test_dir_.Append(StringPrintf("overage%d", i)), "", 0); 148 file_util::WriteFile(test_dir_.Append(StringPrintf("overage%d.meta", i)),
149 "", 0);
138 EXPECT_FALSE(CheckHasCapacity()); 150 EXPECT_FALSE(CheckHasCapacity());
139 } 151 }
140 } 152 }
141 153
142 TEST_F(CrashCollectorTest, CheckHasCapacityOverCore) { 154 TEST_F(CrashCollectorTest, CheckHasCapacityCorrectBasename) {
143 // Set up kMaxCrashDirectorySize - 1 core files. 155 // Test kMaxCrashDirectorySize - 1 files can be added.
144 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) { 156 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 1; ++i) {
145 file_util::WriteFile(test_dir_.Append(StringPrintf("file%d.core", i)), 157 file_util::WriteFile(test_dir_.Append(StringPrintf("file.%d.core", i)),
146 "", 0); 158 "", 0);
159 EXPECT_TRUE(CheckHasCapacity());
147 } 160 }
161 file_util::WriteFile(test_dir_.Append("file.last.core"), "", 0);
162 EXPECT_FALSE(CheckHasCapacity());
163 }
148 164
149 EXPECT_TRUE(CheckHasCapacity()); 165 TEST_F(CrashCollectorTest, CheckHasCapacityStrangeNames) {
166 // Test many files with different extensions and same base fit.
167 for (int i = 0; i < 5 * CrashCollector::kMaxCrashDirectorySize; ++i) {
168 file_util::WriteFile(test_dir_.Append(StringPrintf("a.%d", i)), "", 0);
169 EXPECT_TRUE(CheckHasCapacity());
170 }
171 // Test dot files are treated as individual files.
172 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 2; ++i) {
173 file_util::WriteFile(test_dir_.Append(StringPrintf(".file%d", i)), "", 0);
174 EXPECT_TRUE(CheckHasCapacity());
175 }
176 file_util::WriteFile(test_dir_.Append("normal.meta"), "", 0);
177 EXPECT_FALSE(CheckHasCapacity());
178 }
150 179
151 // Test an additional core file does not fit. 180 TEST_F(CrashCollectorTest, ReadKeyValueFile) {
152 file_util::WriteFile(test_dir_.Append("overage.core"), "", 0); 181 const char *contents = ("a=b\n"
153 EXPECT_FALSE(CheckHasCapacity()); 182 "\n"
183 " c=d \n");
184 FilePath path(test_dir_.Append("keyval"));
185 std::map<std::string, std::string> dictionary;
186 std::map<std::string, std::string>::iterator i;
187
188 file_util::WriteFile(path, contents, strlen(contents));
189
190 EXPECT_TRUE(collector_.ReadKeyValueFile(path, '=', &dictionary));
191 i = dictionary.find("a");
192 EXPECT_TRUE(i != dictionary.end() && i->second == "b");
193 i = dictionary.find("c");
194 EXPECT_TRUE(i != dictionary.end() && i->second == "d");
195
196 dictionary.clear();
197
198 contents = ("a=b c d\n"
199 "e\n"
200 " f g = h\n"
201 "i=j\n"
202 "=k\n"
203 "l=\n");
204 file_util::WriteFile(path, contents, strlen(contents));
205
206 EXPECT_FALSE(collector_.ReadKeyValueFile(path, '=', &dictionary));
207 i = dictionary.find("a");
208 EXPECT_TRUE(i != dictionary.end() && i->second == "b c d");
209 i = dictionary.find("e");
210 EXPECT_TRUE(i == dictionary.end());
211 i = dictionary.find("f g");
212 EXPECT_TRUE(i != dictionary.end() && i->second == "h");
213 i = dictionary.find("i");
214 EXPECT_TRUE(i != dictionary.end() && i->second == "j");
215 i = dictionary.find("");
216 EXPECT_TRUE(i != dictionary.end() && i->second == "k");
217 i = dictionary.find("l");
218 EXPECT_TRUE(i != dictionary.end() && i->second == "");
154 } 219 }
155 220
156 int main(int argc, char **argv) { 221 int main(int argc, char **argv) {
157 ::testing::InitGoogleTest(&argc, argv); 222 ::testing::InitGoogleTest(&argc, argv);
158 return RUN_ALL_TESTS(); 223 return RUN_ALL_TESTS();
159 } 224 }
OLDNEW
« no previous file with comments | « crash_collector.cc ('k') | crash_reporter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698