blob: 2cf9fb5d6f61599f06ded723cdd0987c70bcbe5a [file] [log] [blame]
Ken Mixter03403162010-08-18 15:23:16 -07001// 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
14static int s_crashes = 0;
Simon Que3f7ed5d2010-11-30 17:10:54 -080015static bool s_metrics = true;
Ken Mixter03403162010-08-18 15:23:16 -070016
Simon Que3f7ed5d2010-11-30 17:10:54 -080017static const char kTestLowBattery[] = "test/low_battery";
18static const char kTestSuspended[] = "test/suspended";
Ken Mixter03403162010-08-18 15:23:16 -070019static const char kTestUnclean[] = "test/unclean";
20
21void CountCrash() {
22 ++s_crashes;
23}
24
25bool IsMetrics() {
26 return s_metrics;
27}
28
29class 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 Que3f7ed5d2010-11-30 17:10:54 -080039 // 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 Mixter03403162010-08-18 15:23:16 -070042 }
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
55TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) {
56 ASSERT_TRUE(collector_.Enable());
57 ASSERT_TRUE(file_util::PathExists(test_unclean_));
58}
59
60TEST_F(UncleanShutdownCollectorTest, EnableWithParent) {
61 mkdir("test", 0777);
62 ASSERT_TRUE(collector_.Enable());
63 ASSERT_TRUE(file_util::PathExists(test_unclean_));
64}
65
66TEST_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
73TEST_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 Que3f7ed5d2010-11-30 17:10:54 -080078 ASSERT_EQ(1, s_crashes);
Ken Mixter03403162010-08-18 15:23:16 -070079 ASSERT_NE(std::string::npos,
80 logging_.log().find("Last shutdown was not clean"));
81}
82
83TEST_F(UncleanShutdownCollectorTest, CollectFalse) {
84 ASSERT_FALSE(collector_.Collect());
Simon Que3f7ed5d2010-11-30 17:10:54 -080085 ASSERT_EQ(0, s_crashes);
86}
87
88TEST_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
101TEST_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 Mixter03403162010-08-18 15:23:16 -0700111}
112
113TEST_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
121TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) {
122 ASSERT_TRUE(collector_.Disable());
123}
124
125TEST_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
132int main(int argc, char **argv) {
133 ::testing::InitGoogleTest(&argc, argv);
134 return RUN_ALL_TESTS();
135}