OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <unistd.h> |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/string_util.h" |
| 9 #include "crash-reporter/unclean_shutdown_collector.h" |
| 10 #include "crash-reporter/system_logging_mock.h" |
| 11 #include "gflags/gflags.h" |
| 12 #include "gtest/gtest.h" |
| 13 |
| 14 static int s_crashes = 0; |
| 15 static bool s_metrics = false; |
| 16 |
| 17 static const char kTestUnclean[] = "test/unclean"; |
| 18 |
| 19 void CountCrash() { |
| 20 ++s_crashes; |
| 21 } |
| 22 |
| 23 bool IsMetrics() { |
| 24 return s_metrics; |
| 25 } |
| 26 |
| 27 class UncleanShutdownCollectorTest : public ::testing::Test { |
| 28 void SetUp() { |
| 29 s_crashes = 0; |
| 30 collector_.Initialize(CountCrash, |
| 31 IsMetrics, |
| 32 &logging_); |
| 33 rmdir("test"); |
| 34 test_unclean_ = FilePath(kTestUnclean); |
| 35 collector_.unclean_shutdown_file_ = kTestUnclean; |
| 36 file_util::Delete(test_unclean_, true); |
| 37 } |
| 38 protected: |
| 39 void WriteStringToFile(const FilePath &file_path, |
| 40 const char *data) { |
| 41 ASSERT_EQ(strlen(data), |
| 42 file_util::WriteFile(file_path, data, strlen(data))); |
| 43 } |
| 44 |
| 45 SystemLoggingMock logging_; |
| 46 UncleanShutdownCollector collector_; |
| 47 FilePath test_unclean_; |
| 48 }; |
| 49 |
| 50 TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { |
| 51 ASSERT_TRUE(collector_.Enable()); |
| 52 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 53 } |
| 54 |
| 55 TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { |
| 56 mkdir("test", 0777); |
| 57 ASSERT_TRUE(collector_.Enable()); |
| 58 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 59 } |
| 60 |
| 61 TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { |
| 62 collector_.unclean_shutdown_file_ = "/bad/path"; |
| 63 ASSERT_FALSE(collector_.Enable()); |
| 64 ASSERT_NE(std::string::npos, |
| 65 logging_.log().find("Unable to create shutdown check file")); |
| 66 } |
| 67 |
| 68 TEST_F(UncleanShutdownCollectorTest, CollectTrue) { |
| 69 ASSERT_TRUE(collector_.Enable()); |
| 70 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 71 ASSERT_TRUE(collector_.Collect()); |
| 72 ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 73 ASSERT_NE(std::string::npos, |
| 74 logging_.log().find("Last shutdown was not clean")); |
| 75 } |
| 76 |
| 77 TEST_F(UncleanShutdownCollectorTest, CollectFalse) { |
| 78 ASSERT_FALSE(collector_.Collect()); |
| 79 } |
| 80 |
| 81 TEST_F(UncleanShutdownCollectorTest, Disable) { |
| 82 ASSERT_TRUE(collector_.Enable()); |
| 83 ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 84 ASSERT_TRUE(collector_.Disable()); |
| 85 ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 86 ASSERT_FALSE(collector_.Collect()); |
| 87 } |
| 88 |
| 89 TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { |
| 90 ASSERT_TRUE(collector_.Disable()); |
| 91 } |
| 92 |
| 93 TEST_F(UncleanShutdownCollectorTest, CantDisable) { |
| 94 mkdir(kTestUnclean, 0700); |
| 95 file_util::WriteFile(test_unclean_.Append("foo"), "", 0); |
| 96 ASSERT_FALSE(collector_.Disable()); |
| 97 rmdir(kTestUnclean); |
| 98 } |
| 99 |
| 100 int main(int argc, char **argv) { |
| 101 ::testing::InitGoogleTest(&argc, argv); |
| 102 return RUN_ALL_TESTS(); |
| 103 } |
OLD | NEW |