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 "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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 } | 264 } |
265 // Test dot files are treated as individual files. | 265 // Test dot files are treated as individual files. |
266 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 2; ++i) { | 266 for (int i = 0; i < CrashCollector::kMaxCrashDirectorySize - 2; ++i) { |
267 file_util::WriteFile(test_dir_.Append(StringPrintf(".file%d", i)), "", 0); | 267 file_util::WriteFile(test_dir_.Append(StringPrintf(".file%d", i)), "", 0); |
268 EXPECT_TRUE(CheckHasCapacity()); | 268 EXPECT_TRUE(CheckHasCapacity()); |
269 } | 269 } |
270 file_util::WriteFile(test_dir_.Append("normal.meta"), "", 0); | 270 file_util::WriteFile(test_dir_.Append("normal.meta"), "", 0); |
271 EXPECT_FALSE(CheckHasCapacity()); | 271 EXPECT_FALSE(CheckHasCapacity()); |
272 } | 272 } |
273 | 273 |
| 274 TEST_F(CrashCollectorTest, IsCommentLine) { |
| 275 EXPECT_FALSE(CrashCollector::IsCommentLine("")); |
| 276 EXPECT_TRUE(CrashCollector::IsCommentLine("#")); |
| 277 EXPECT_TRUE(CrashCollector::IsCommentLine("#real comment")); |
| 278 EXPECT_TRUE(CrashCollector::IsCommentLine(" # real comment")); |
| 279 EXPECT_FALSE(CrashCollector::IsCommentLine("not comment")); |
| 280 EXPECT_FALSE(CrashCollector::IsCommentLine(" not comment")); |
| 281 } |
| 282 |
274 TEST_F(CrashCollectorTest, ReadKeyValueFile) { | 283 TEST_F(CrashCollectorTest, ReadKeyValueFile) { |
275 const char *contents = ("a=b\n" | 284 const char *contents = ("a=b\n" |
276 "\n" | 285 "\n" |
277 " c=d \n"); | 286 " c=d \n"); |
278 FilePath path(test_dir_.Append("keyval")); | 287 FilePath path(test_dir_.Append("keyval")); |
279 std::map<std::string, std::string> dictionary; | 288 std::map<std::string, std::string> dictionary; |
280 std::map<std::string, std::string>::iterator i; | 289 std::map<std::string, std::string>::iterator i; |
281 | 290 |
282 file_util::WriteFile(path, contents, strlen(contents)); | 291 file_util::WriteFile(path, contents, strlen(contents)); |
283 | 292 |
284 EXPECT_TRUE(collector_.ReadKeyValueFile(path, '=', &dictionary)); | 293 EXPECT_TRUE(collector_.ReadKeyValueFile(path, '=', &dictionary)); |
285 i = dictionary.find("a"); | 294 i = dictionary.find("a"); |
286 EXPECT_TRUE(i != dictionary.end() && i->second == "b"); | 295 EXPECT_TRUE(i != dictionary.end() && i->second == "b"); |
287 i = dictionary.find("c"); | 296 i = dictionary.find("c"); |
288 EXPECT_TRUE(i != dictionary.end() && i->second == "d"); | 297 EXPECT_TRUE(i != dictionary.end() && i->second == "d"); |
289 | 298 |
290 dictionary.clear(); | 299 dictionary.clear(); |
291 | 300 |
292 contents = ("a=b c d\n" | 301 contents = ("a=b c d\n" |
293 "e\n" | 302 "e\n" |
294 " f g = h\n" | 303 " f g = h\n" |
295 "i=j\n" | 304 "i=j\n" |
296 "=k\n" | 305 "=k\n" |
| 306 "#comment=0\n" |
297 "l=\n"); | 307 "l=\n"); |
298 file_util::WriteFile(path, contents, strlen(contents)); | 308 file_util::WriteFile(path, contents, strlen(contents)); |
299 | 309 |
300 EXPECT_FALSE(collector_.ReadKeyValueFile(path, '=', &dictionary)); | 310 EXPECT_FALSE(collector_.ReadKeyValueFile(path, '=', &dictionary)); |
| 311 EXPECT_EQ(5, dictionary.size()); |
| 312 |
301 i = dictionary.find("a"); | 313 i = dictionary.find("a"); |
302 EXPECT_TRUE(i != dictionary.end() && i->second == "b c d"); | 314 EXPECT_TRUE(i != dictionary.end() && i->second == "b c d"); |
303 i = dictionary.find("e"); | 315 i = dictionary.find("e"); |
304 EXPECT_TRUE(i == dictionary.end()); | 316 EXPECT_TRUE(i == dictionary.end()); |
305 i = dictionary.find("f g"); | 317 i = dictionary.find("f g"); |
306 EXPECT_TRUE(i != dictionary.end() && i->second == "h"); | 318 EXPECT_TRUE(i != dictionary.end() && i->second == "h"); |
307 i = dictionary.find("i"); | 319 i = dictionary.find("i"); |
308 EXPECT_TRUE(i != dictionary.end() && i->second == "j"); | 320 EXPECT_TRUE(i != dictionary.end() && i->second == "j"); |
309 i = dictionary.find(""); | 321 i = dictionary.find(""); |
310 EXPECT_TRUE(i != dictionary.end() && i->second == "k"); | 322 EXPECT_TRUE(i != dictionary.end() && i->second == "k"); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 // Test target of dangling symlink is not created. | 374 // Test target of dangling symlink is not created. |
363 file_util::Delete(meta_file, false); | 375 file_util::Delete(meta_file, false); |
364 ASSERT_FALSE(file_util::PathExists(meta_file)); | 376 ASSERT_FALSE(file_util::PathExists(meta_file)); |
365 logging_.clear(); | 377 logging_.clear(); |
366 collector_.WriteCrashMetaData(meta_symlink_path, "kernel", | 378 collector_.WriteCrashMetaData(meta_symlink_path, "kernel", |
367 payload_file.value()); | 379 payload_file.value()); |
368 EXPECT_FALSE(file_util::PathExists(meta_file)); | 380 EXPECT_FALSE(file_util::PathExists(meta_file)); |
369 EXPECT_NE(std::string::npos, logging_.log().find("Unable to write")); | 381 EXPECT_NE(std::string::npos, logging_.log().find("Unable to write")); |
370 } | 382 } |
371 | 383 |
| 384 TEST_F(CrashCollectorTest, GetLogContents) { |
| 385 FilePath config_file = test_dir_.Append("crash_config"); |
| 386 FilePath output_file = test_dir_.Append("crash_log"); |
| 387 const char kConfigContents[] = |
| 388 "foobar:echo hello there | sed -e \"s/there/world/\""; |
| 389 ASSERT_TRUE( |
| 390 file_util::WriteFile(config_file, |
| 391 kConfigContents, strlen(kConfigContents))); |
| 392 EXPECT_FALSE(collector_.GetLogContents(config_file, |
| 393 "barfoo", |
| 394 output_file)); |
| 395 EXPECT_FALSE(file_util::PathExists(output_file)); |
| 396 EXPECT_TRUE(collector_.GetLogContents(config_file, |
| 397 "foobar", |
| 398 output_file)); |
| 399 ASSERT_TRUE(file_util::PathExists(output_file)); |
| 400 std::string contents; |
| 401 EXPECT_TRUE(file_util::ReadFileToString(output_file, &contents)); |
| 402 EXPECT_EQ("hello world\n", contents); |
| 403 } |
| 404 |
372 int main(int argc, char **argv) { | 405 int main(int argc, char **argv) { |
373 ::testing::InitGoogleTest(&argc, argv); | 406 ::testing::InitGoogleTest(&argc, argv); |
374 return RUN_ALL_TESTS(); | 407 return RUN_ALL_TESTS(); |
375 } | 408 } |
OLD | NEW |