blob: 1bb0c9431d9d3fa1894409ee3901df3be8a12b0c [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"
Ken Mixtera3249322011-03-03 08:47:38 -08009#include "chromeos/syslog_logging.h"
10#include "chromeos/test_helpers.h"
Ken Mixter03403162010-08-18 15:23:16 -070011#include "crash-reporter/unclean_shutdown_collector.h"
Ken Mixter03403162010-08-18 15:23:16 -070012#include "gflags/gflags.h"
13#include "gtest/gtest.h"
14
15static int s_crashes = 0;
Simon Que3f7ed5d2010-11-30 17:10:54 -080016static bool s_metrics = true;
Ken Mixter03403162010-08-18 15:23:16 -070017
Michael Krebs87a49502012-04-12 15:24:22 -070018static const char kTestDirectory[] = "test";
Simon Que3f7ed5d2010-11-30 17:10:54 -080019static const char kTestLowBattery[] = "test/low_battery";
20static const char kTestSuspended[] = "test/suspended";
Ken Mixter03403162010-08-18 15:23:16 -070021static const char kTestUnclean[] = "test/unclean";
22
Simon Que9f90aca2013-02-19 17:19:52 -080023using base::FilePath;
Ken Mixtera3249322011-03-03 08:47:38 -080024using ::chromeos::FindLog;
25
Ken Mixter03403162010-08-18 15:23:16 -070026void CountCrash() {
27 ++s_crashes;
28}
29
30bool IsMetrics() {
31 return s_metrics;
32}
33
34class UncleanShutdownCollectorTest : public ::testing::Test {
35 void SetUp() {
36 s_crashes = 0;
37 collector_.Initialize(CountCrash,
Ken Mixtera3249322011-03-03 08:47:38 -080038 IsMetrics);
Michael Krebs87a49502012-04-12 15:24:22 -070039 rmdir(kTestDirectory);
Ken Mixter03403162010-08-18 15:23:16 -070040 test_unclean_ = FilePath(kTestUnclean);
41 collector_.unclean_shutdown_file_ = kTestUnclean;
42 file_util::Delete(test_unclean_, true);
Simon Que3f7ed5d2010-11-30 17:10:54 -080043 // 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 Mixtera3249322011-03-03 08:47:38 -080046 chromeos::ClearLog();
Ken Mixter03403162010-08-18 15:23:16 -070047 }
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 Mixter03403162010-08-18 15:23:16 -070055 UncleanShutdownCollector collector_;
56 FilePath test_unclean_;
57};
58
59TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) {
60 ASSERT_TRUE(collector_.Enable());
61 ASSERT_TRUE(file_util::PathExists(test_unclean_));
62}
63
64TEST_F(UncleanShutdownCollectorTest, EnableWithParent) {
Michael Krebs87a49502012-04-12 15:24:22 -070065 mkdir(kTestDirectory, 0777);
Ken Mixter03403162010-08-18 15:23:16 -070066 ASSERT_TRUE(collector_.Enable());
67 ASSERT_TRUE(file_util::PathExists(test_unclean_));
68}
69
70TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) {
71 collector_.unclean_shutdown_file_ = "/bad/path";
72 ASSERT_FALSE(collector_.Enable());
Ken Mixtera3249322011-03-03 08:47:38 -080073 ASSERT_TRUE(FindLog("Unable to create shutdown check file"));
Ken Mixter03403162010-08-18 15:23:16 -070074}
75
76TEST_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 Que3f7ed5d2010-11-30 17:10:54 -080081 ASSERT_EQ(1, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -080082 ASSERT_TRUE(FindLog("Last shutdown was not clean"));
Ken Mixter03403162010-08-18 15:23:16 -070083}
84
85TEST_F(UncleanShutdownCollectorTest, CollectFalse) {
86 ASSERT_FALSE(collector_.Collect());
Simon Que3f7ed5d2010-11-30 17:10:54 -080087 ASSERT_EQ(0, s_crashes);
88}
89
90TEST_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 Mixtera3249322011-03-03 08:47:38 -080098 ASSERT_TRUE(FindLog(
99 "Unclean shutdown occurred while running with battery critically low."));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800100}
101
102TEST_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 Mixtera3249322011-03-03 08:47:38 -0800110 ASSERT_TRUE(FindLog("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) {
Michael Krebs87a49502012-04-12 15:24:22 -0700126 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 Mixter03403162010-08-18 15:23:16 -0700135 ASSERT_FALSE(collector_.Disable());
136 rmdir(kTestUnclean);
137}
138
139int main(int argc, char **argv) {
Ken Mixtera3249322011-03-03 08:47:38 -0800140 SetUpTests(&argc, argv, false);
Ken Mixter03403162010-08-18 15:23:16 -0700141 return RUN_ALL_TESTS();
142}