blob: 36372aee3348e2cf2e6800f71649c5f8a7fc08b5 [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) {
James Hawkinsa28317d2016-02-26 11:24:38 -080076 unsigned int numBytesWritten =
77 base::WriteFile(file_path, data, strlen(data));
78 ASSERT_EQ(strlen(data), numBytesWritten);
Ken Mixter03403162010-08-18 15:23:16 -070079 }
80
Steve Fung6e139522015-02-05 14:54:16 -080081 UncleanShutdownCollectorMock collector_;
Steve Fung78fcf662016-01-20 01:45:11 -080082
83 // Temporary directory used for tests.
84 base::ScopedTempDir test_dir_;
85 FilePath test_directory_;
Ken Mixter03403162010-08-18 15:23:16 -070086 FilePath test_unclean_;
87};
88
89TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) {
90 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -050091 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -070092}
93
94TEST_F(UncleanShutdownCollectorTest, EnableWithParent) {
Steve Fung78fcf662016-01-20 01:45:11 -080095 mkdir(test_directory_.value().c_str(), 0777);
Ken Mixter03403162010-08-18 15:23:16 -070096 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -050097 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -070098}
99
100TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) {
101 collector_.unclean_shutdown_file_ = "/bad/path";
102 ASSERT_FALSE(collector_.Enable());
Ken Mixtera3249322011-03-03 08:47:38 -0800103 ASSERT_TRUE(FindLog("Unable to create shutdown check file"));
Ken Mixter03403162010-08-18 15:23:16 -0700104}
105
106TEST_F(UncleanShutdownCollectorTest, CollectTrue) {
107 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500108 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700109 ASSERT_TRUE(collector_.Collect());
Mike Frysingera557c112014-02-05 22:55:39 -0500110 ASSERT_FALSE(base::PathExists(test_unclean_));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800111 ASSERT_EQ(1, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800112 ASSERT_TRUE(FindLog("Last shutdown was not clean"));
Ken Mixter03403162010-08-18 15:23:16 -0700113}
114
115TEST_F(UncleanShutdownCollectorTest, CollectFalse) {
116 ASSERT_FALSE(collector_.Collect());
Simon Que3f7ed5d2010-11-30 17:10:54 -0800117 ASSERT_EQ(0, s_crashes);
118}
119
Simon Que3f7ed5d2010-11-30 17:10:54 -0800120TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) {
121 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500122 ASSERT_TRUE(base::PathExists(test_unclean_));
Ben Chanf30c6412014-05-22 23:09:01 -0700123 base::WriteFile(collector_.powerd_suspended_file_, "", 0);
Simon Que3f7ed5d2010-11-30 17:10:54 -0800124 ASSERT_FALSE(collector_.Collect());
Mike Frysingera557c112014-02-05 22:55:39 -0500125 ASSERT_FALSE(base::PathExists(test_unclean_));
126 ASSERT_FALSE(base::PathExists(collector_.powerd_suspended_file_));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800127 ASSERT_EQ(0, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800128 ASSERT_TRUE(FindLog("Unclean shutdown occurred while suspended."));
Ken Mixter03403162010-08-18 15:23:16 -0700129}
130
131TEST_F(UncleanShutdownCollectorTest, Disable) {
132 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500133 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700134 ASSERT_TRUE(collector_.Disable());
Mike Frysingera557c112014-02-05 22:55:39 -0500135 ASSERT_FALSE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700136 ASSERT_FALSE(collector_.Collect());
137}
138
139TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) {
140 ASSERT_TRUE(collector_.Disable());
141}
142
143TEST_F(UncleanShutdownCollectorTest, CantDisable) {
Steve Fung78fcf662016-01-20 01:45:11 -0800144 mkdir(test_directory_.value().c_str(), 0700);
145 if (mkdir(test_unclean_.value().c_str(), 0700)) {
Michael Krebs87a49502012-04-12 15:24:22 -0700146 ASSERT_EQ(EEXIST, errno)
Steve Fung78fcf662016-01-20 01:45:11 -0800147 << "Error while creating directory '" << test_unclean_.value()
Michael Krebs87a49502012-04-12 15:24:22 -0700148 << "': " << strerror(errno);
149 }
Ben Chanf30c6412014-05-22 23:09:01 -0700150 ASSERT_EQ(0, base::WriteFile(test_unclean_.Append("foo"), "", 0))
Michael Krebs87a49502012-04-12 15:24:22 -0700151 << "Error while creating empty file '"
152 << test_unclean_.Append("foo").value() << "': " << strerror(errno);
Ken Mixter03403162010-08-18 15:23:16 -0700153 ASSERT_FALSE(collector_.Disable());
Steve Fung78fcf662016-01-20 01:45:11 -0800154 rmdir(test_unclean_.value().c_str());
Ken Mixter03403162010-08-18 15:23:16 -0700155}