Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 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; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame^] | 15 | static bool s_metrics = true; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame^] | 17 | static const char kTestLowBattery[] = "test/low_battery"; |
| 18 | static const char kTestSuspended[] = "test/suspended"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 19 | static const char kTestUnclean[] = "test/unclean"; |
| 20 | |
| 21 | void CountCrash() { |
| 22 | ++s_crashes; |
| 23 | } |
| 24 | |
| 25 | bool IsMetrics() { |
| 26 | return s_metrics; |
| 27 | } |
| 28 | |
| 29 | class UncleanShutdownCollectorTest : public ::testing::Test { |
| 30 | void SetUp() { |
| 31 | s_crashes = 0; |
| 32 | collector_.Initialize(CountCrash, |
| 33 | IsMetrics, |
| 34 | &logging_); |
| 35 | rmdir("test"); |
| 36 | test_unclean_ = FilePath(kTestUnclean); |
| 37 | collector_.unclean_shutdown_file_ = kTestUnclean; |
| 38 | file_util::Delete(test_unclean_, true); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame^] | 39 | // Set up alternate power manager tracing files as well |
| 40 | collector_.powerd_suspended_file_ = FilePath(kTestSuspended); |
| 41 | collector_.powerd_low_battery_file_ = FilePath(kTestLowBattery); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 42 | } |
| 43 | protected: |
| 44 | void WriteStringToFile(const FilePath &file_path, |
| 45 | const char *data) { |
| 46 | ASSERT_EQ(strlen(data), |
| 47 | file_util::WriteFile(file_path, data, strlen(data))); |
| 48 | } |
| 49 | |
| 50 | SystemLoggingMock logging_; |
| 51 | UncleanShutdownCollector collector_; |
| 52 | FilePath test_unclean_; |
| 53 | }; |
| 54 | |
| 55 | TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { |
| 56 | ASSERT_TRUE(collector_.Enable()); |
| 57 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 58 | } |
| 59 | |
| 60 | TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { |
| 61 | mkdir("test", 0777); |
| 62 | ASSERT_TRUE(collector_.Enable()); |
| 63 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 64 | } |
| 65 | |
| 66 | TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { |
| 67 | collector_.unclean_shutdown_file_ = "/bad/path"; |
| 68 | ASSERT_FALSE(collector_.Enable()); |
| 69 | ASSERT_NE(std::string::npos, |
| 70 | logging_.log().find("Unable to create shutdown check file")); |
| 71 | } |
| 72 | |
| 73 | TEST_F(UncleanShutdownCollectorTest, CollectTrue) { |
| 74 | ASSERT_TRUE(collector_.Enable()); |
| 75 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 76 | ASSERT_TRUE(collector_.Collect()); |
| 77 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame^] | 78 | ASSERT_EQ(1, s_crashes); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 79 | ASSERT_NE(std::string::npos, |
| 80 | logging_.log().find("Last shutdown was not clean")); |
| 81 | } |
| 82 | |
| 83 | TEST_F(UncleanShutdownCollectorTest, CollectFalse) { |
| 84 | ASSERT_FALSE(collector_.Collect()); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame^] | 85 | ASSERT_EQ(0, s_crashes); |
| 86 | } |
| 87 | |
| 88 | TEST_F(UncleanShutdownCollectorTest, CollectDeadBatteryRunningLow) { |
| 89 | ASSERT_TRUE(collector_.Enable()); |
| 90 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 91 | file_util::WriteFile(collector_.powerd_low_battery_file_, "", 0); |
| 92 | ASSERT_FALSE(collector_.Collect()); |
| 93 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 94 | ASSERT_FALSE(file_util::PathExists(collector_.powerd_low_battery_file_)); |
| 95 | ASSERT_EQ(0, s_crashes); |
| 96 | ASSERT_NE(std::string::npos, |
| 97 | logging_.log().find("Unclean shutdown occurred while running with " |
| 98 | "battery critically low.")); |
| 99 | } |
| 100 | |
| 101 | TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) { |
| 102 | ASSERT_TRUE(collector_.Enable()); |
| 103 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 104 | file_util::WriteFile(collector_.powerd_suspended_file_, "", 0); |
| 105 | ASSERT_FALSE(collector_.Collect()); |
| 106 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 107 | ASSERT_FALSE(file_util::PathExists(collector_.powerd_suspended_file_)); |
| 108 | ASSERT_EQ(0, s_crashes); |
| 109 | ASSERT_NE(std::string::npos, |
| 110 | logging_.log().find("Unclean shutdown occurred while suspended.")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(UncleanShutdownCollectorTest, Disable) { |
| 114 | ASSERT_TRUE(collector_.Enable()); |
| 115 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 116 | ASSERT_TRUE(collector_.Disable()); |
| 117 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 118 | ASSERT_FALSE(collector_.Collect()); |
| 119 | } |
| 120 | |
| 121 | TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { |
| 122 | ASSERT_TRUE(collector_.Disable()); |
| 123 | } |
| 124 | |
| 125 | TEST_F(UncleanShutdownCollectorTest, CantDisable) { |
| 126 | mkdir(kTestUnclean, 0700); |
| 127 | file_util::WriteFile(test_unclean_.Append("foo"), "", 0); |
| 128 | ASSERT_FALSE(collector_.Disable()); |
| 129 | rmdir(kTestUnclean); |
| 130 | } |
| 131 | |
| 132 | int main(int argc, char **argv) { |
| 133 | ::testing::InitGoogleTest(&argc, argv); |
| 134 | return RUN_ALL_TESTS(); |
| 135 | } |