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" |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 9 | #include "chromeos/syslog_logging.h" |
| 10 | #include "chromeos/test_helpers.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 11 | #include "crash-reporter/unclean_shutdown_collector.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 12 | #include "gflags/gflags.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | static int s_crashes = 0; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 16 | static bool s_metrics = true; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 17 | |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 18 | static const char kTestDirectory[] = "test"; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 19 | static const char kTestLowBattery[] = "test/low_battery"; |
| 20 | static const char kTestSuspended[] = "test/suspended"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 21 | static const char kTestUnclean[] = "test/unclean"; |
| 22 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame^] | 23 | using base::FilePath; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 24 | using ::chromeos::FindLog; |
| 25 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 26 | void CountCrash() { |
| 27 | ++s_crashes; |
| 28 | } |
| 29 | |
| 30 | bool IsMetrics() { |
| 31 | return s_metrics; |
| 32 | } |
| 33 | |
| 34 | class UncleanShutdownCollectorTest : public ::testing::Test { |
| 35 | void SetUp() { |
| 36 | s_crashes = 0; |
| 37 | collector_.Initialize(CountCrash, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 38 | IsMetrics); |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 39 | rmdir(kTestDirectory); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 40 | test_unclean_ = FilePath(kTestUnclean); |
| 41 | collector_.unclean_shutdown_file_ = kTestUnclean; |
| 42 | file_util::Delete(test_unclean_, true); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 43 | // Set up alternate power manager tracing files as well |
| 44 | collector_.powerd_suspended_file_ = FilePath(kTestSuspended); |
| 45 | collector_.powerd_low_battery_file_ = FilePath(kTestLowBattery); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 46 | chromeos::ClearLog(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 47 | } |
| 48 | protected: |
| 49 | void WriteStringToFile(const FilePath &file_path, |
| 50 | const char *data) { |
| 51 | ASSERT_EQ(strlen(data), |
| 52 | file_util::WriteFile(file_path, data, strlen(data))); |
| 53 | } |
| 54 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 55 | UncleanShutdownCollector collector_; |
| 56 | FilePath test_unclean_; |
| 57 | }; |
| 58 | |
| 59 | TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { |
| 60 | ASSERT_TRUE(collector_.Enable()); |
| 61 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 62 | } |
| 63 | |
| 64 | TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 65 | mkdir(kTestDirectory, 0777); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(collector_.Enable()); |
| 67 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 68 | } |
| 69 | |
| 70 | TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { |
| 71 | collector_.unclean_shutdown_file_ = "/bad/path"; |
| 72 | ASSERT_FALSE(collector_.Enable()); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 73 | ASSERT_TRUE(FindLog("Unable to create shutdown check file")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST_F(UncleanShutdownCollectorTest, CollectTrue) { |
| 77 | ASSERT_TRUE(collector_.Enable()); |
| 78 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 79 | ASSERT_TRUE(collector_.Collect()); |
| 80 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 81 | ASSERT_EQ(1, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 82 | ASSERT_TRUE(FindLog("Last shutdown was not clean")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST_F(UncleanShutdownCollectorTest, CollectFalse) { |
| 86 | ASSERT_FALSE(collector_.Collect()); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 87 | ASSERT_EQ(0, s_crashes); |
| 88 | } |
| 89 | |
| 90 | TEST_F(UncleanShutdownCollectorTest, CollectDeadBatteryRunningLow) { |
| 91 | ASSERT_TRUE(collector_.Enable()); |
| 92 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 93 | file_util::WriteFile(collector_.powerd_low_battery_file_, "", 0); |
| 94 | ASSERT_FALSE(collector_.Collect()); |
| 95 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 96 | ASSERT_FALSE(file_util::PathExists(collector_.powerd_low_battery_file_)); |
| 97 | ASSERT_EQ(0, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 98 | ASSERT_TRUE(FindLog( |
| 99 | "Unclean shutdown occurred while running with battery critically low.")); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) { |
| 103 | ASSERT_TRUE(collector_.Enable()); |
| 104 | ASSERT_TRUE(file_util::PathExists(test_unclean_)); |
| 105 | file_util::WriteFile(collector_.powerd_suspended_file_, "", 0); |
| 106 | ASSERT_FALSE(collector_.Collect()); |
| 107 | ASSERT_FALSE(file_util::PathExists(test_unclean_)); |
| 108 | ASSERT_FALSE(file_util::PathExists(collector_.powerd_suspended_file_)); |
| 109 | ASSERT_EQ(0, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 110 | ASSERT_TRUE(FindLog("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) { |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 126 | mkdir(kTestDirectory, 0700); |
| 127 | if (mkdir(kTestUnclean, 0700)) { |
| 128 | ASSERT_EQ(EEXIST, errno) |
| 129 | << "Error while creating directory '" << kTestUnclean |
| 130 | << "': " << strerror(errno); |
| 131 | } |
| 132 | ASSERT_EQ(0, file_util::WriteFile(test_unclean_.Append("foo"), "", 0)) |
| 133 | << "Error while creating empty file '" |
| 134 | << test_unclean_.Append("foo").value() << "': " << strerror(errno); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 135 | ASSERT_FALSE(collector_.Disable()); |
| 136 | rmdir(kTestUnclean); |
| 137 | } |
| 138 | |
| 139 | int main(int argc, char **argv) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 140 | SetUpTests(&argc, argv, false); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 141 | return RUN_ALL_TESTS(); |
| 142 | } |