blob: 56d27045ed2ffb11dd29935ac196566da4e226d8 [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ken Mixter03403162010-08-18 15:23:16 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "unclean_shutdown_collector.h"
Ben Chan7e776902014-06-18 13:19:51 -070018
Ken Mixter03403162010-08-18 15:23:16 -070019#include <unistd.h>
20
Ben Chanab6cc902014-09-05 08:21:06 -070021#include <base/files/file_util.h>
Steve Fung78fcf662016-01-20 01:45:11 -080022#include <base/files/scoped_temp_dir.h>
Ben Chan7e776902014-06-18 13:19:51 -070023#include <base/strings/string_util.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070024#include <brillo/syslog_logging.h>
Steve Fung6e139522015-02-05 14:54:16 -080025#include <gmock/gmock.h>
Ben Chan7e776902014-06-18 13:19:51 -070026#include <gtest/gtest.h>
Ken Mixter03403162010-08-18 15:23:16 -070027
Simon Que9f90aca2013-02-19 17:19:52 -080028using base::FilePath;
Alex Vakulenko74dc6242015-10-13 09:23:34 -070029using ::brillo::FindLog;
Ken Mixtera3249322011-03-03 08:47:38 -080030
Daniel Eratd257ea12015-01-28 10:23:28 -070031namespace {
32
33int s_crashes = 0;
34bool s_metrics = true;
35
Ken Mixter03403162010-08-18 15:23:16 -070036void CountCrash() {
37 ++s_crashes;
38}
39
40bool IsMetrics() {
41 return s_metrics;
42}
43
Daniel Eratd257ea12015-01-28 10:23:28 -070044} // namespace
45
Steve Fung6e139522015-02-05 14:54:16 -080046class UncleanShutdownCollectorMock : public UncleanShutdownCollector {
47 public:
48 MOCK_METHOD0(SetUpDBus, void());
49};
50
Ken Mixter03403162010-08-18 15:23:16 -070051class UncleanShutdownCollectorTest : public ::testing::Test {
52 void SetUp() {
53 s_crashes = 0;
Steve Fung6e139522015-02-05 14:54:16 -080054
55 EXPECT_CALL(collector_, SetUpDBus()).WillRepeatedly(testing::Return());
56
Ken Mixter03403162010-08-18 15:23:16 -070057 collector_.Initialize(CountCrash,
Ken Mixtera3249322011-03-03 08:47:38 -080058 IsMetrics);
Steve Fung78fcf662016-01-20 01:45:11 -080059
60 EXPECT_TRUE(test_dir_.CreateUniqueTempDir());
61
62 test_directory_ = test_dir_.path().Append("test");
63 test_unclean_ = test_dir_.path().Append("test/unclean");
64
65 collector_.unclean_shutdown_file_ = test_unclean_.value().c_str();
Mike Frysingera557c112014-02-05 22:55:39 -050066 base::DeleteFile(test_unclean_, true);
Daniel Erat9c9e1c42013-07-18 15:57:11 -070067 // Set up an alternate power manager state file as well
Steve Fung78fcf662016-01-20 01:45:11 -080068 collector_.powerd_suspended_file_ =
69 test_dir_.path().Append("test/suspended");
Alex Vakulenko74dc6242015-10-13 09:23:34 -070070 brillo::ClearLog();
Ken Mixter03403162010-08-18 15:23:16 -070071 }
Steve Fung6e139522015-02-05 14:54:16 -080072
Ken Mixter03403162010-08-18 15:23:16 -070073 protected:
74 void WriteStringToFile(const FilePath &file_path,
75 const char *data) {
Ben Chanf30c6412014-05-22 23:09:01 -070076 ASSERT_EQ(strlen(data), base::WriteFile(file_path, data, strlen(data)));
Ken Mixter03403162010-08-18 15:23:16 -070077 }
78
Steve Fung6e139522015-02-05 14:54:16 -080079 UncleanShutdownCollectorMock collector_;
Steve Fung78fcf662016-01-20 01:45:11 -080080
81 // Temporary directory used for tests.
82 base::ScopedTempDir test_dir_;
83 FilePath test_directory_;
Ken Mixter03403162010-08-18 15:23:16 -070084 FilePath test_unclean_;
85};
86
87TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) {
88 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -050089 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -070090}
91
92TEST_F(UncleanShutdownCollectorTest, EnableWithParent) {
Steve Fung78fcf662016-01-20 01:45:11 -080093 mkdir(test_directory_.value().c_str(), 0777);
Ken Mixter03403162010-08-18 15:23:16 -070094 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -050095 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -070096}
97
98TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) {
99 collector_.unclean_shutdown_file_ = "/bad/path";
100 ASSERT_FALSE(collector_.Enable());
Ken Mixtera3249322011-03-03 08:47:38 -0800101 ASSERT_TRUE(FindLog("Unable to create shutdown check file"));
Ken Mixter03403162010-08-18 15:23:16 -0700102}
103
104TEST_F(UncleanShutdownCollectorTest, CollectTrue) {
105 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500106 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700107 ASSERT_TRUE(collector_.Collect());
Mike Frysingera557c112014-02-05 22:55:39 -0500108 ASSERT_FALSE(base::PathExists(test_unclean_));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800109 ASSERT_EQ(1, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800110 ASSERT_TRUE(FindLog("Last shutdown was not clean"));
Ken Mixter03403162010-08-18 15:23:16 -0700111}
112
113TEST_F(UncleanShutdownCollectorTest, CollectFalse) {
114 ASSERT_FALSE(collector_.Collect());
Simon Que3f7ed5d2010-11-30 17:10:54 -0800115 ASSERT_EQ(0, s_crashes);
116}
117
Simon Que3f7ed5d2010-11-30 17:10:54 -0800118TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) {
119 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500120 ASSERT_TRUE(base::PathExists(test_unclean_));
Ben Chanf30c6412014-05-22 23:09:01 -0700121 base::WriteFile(collector_.powerd_suspended_file_, "", 0);
Simon Que3f7ed5d2010-11-30 17:10:54 -0800122 ASSERT_FALSE(collector_.Collect());
Mike Frysingera557c112014-02-05 22:55:39 -0500123 ASSERT_FALSE(base::PathExists(test_unclean_));
124 ASSERT_FALSE(base::PathExists(collector_.powerd_suspended_file_));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800125 ASSERT_EQ(0, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800126 ASSERT_TRUE(FindLog("Unclean shutdown occurred while suspended."));
Ken Mixter03403162010-08-18 15:23:16 -0700127}
128
129TEST_F(UncleanShutdownCollectorTest, Disable) {
130 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500131 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700132 ASSERT_TRUE(collector_.Disable());
Mike Frysingera557c112014-02-05 22:55:39 -0500133 ASSERT_FALSE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700134 ASSERT_FALSE(collector_.Collect());
135}
136
137TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) {
138 ASSERT_TRUE(collector_.Disable());
139}
140
141TEST_F(UncleanShutdownCollectorTest, CantDisable) {
Steve Fung78fcf662016-01-20 01:45:11 -0800142 mkdir(test_directory_.value().c_str(), 0700);
143 if (mkdir(test_unclean_.value().c_str(), 0700)) {
Michael Krebs87a49502012-04-12 15:24:22 -0700144 ASSERT_EQ(EEXIST, errno)
Steve Fung78fcf662016-01-20 01:45:11 -0800145 << "Error while creating directory '" << test_unclean_.value()
Michael Krebs87a49502012-04-12 15:24:22 -0700146 << "': " << strerror(errno);
147 }
Ben Chanf30c6412014-05-22 23:09:01 -0700148 ASSERT_EQ(0, base::WriteFile(test_unclean_.Append("foo"), "", 0))
Michael Krebs87a49502012-04-12 15:24:22 -0700149 << "Error while creating empty file '"
150 << test_unclean_.Append("foo").value() << "': " << strerror(errno);
Ken Mixter03403162010-08-18 15:23:16 -0700151 ASSERT_FALSE(collector_.Disable());
Steve Fung78fcf662016-01-20 01:45:11 -0800152 rmdir(test_unclean_.value().c_str());
Ken Mixter03403162010-08-18 15:23:16 -0700153}