blob: 3bdeca10ca47c4d684344c1e39a140c0a12efb80 [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>
Ben Chan7e776902014-06-18 13:19:51 -070022#include <base/strings/string_util.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070023#include <brillo/syslog_logging.h>
Steve Fung6e139522015-02-05 14:54:16 -080024#include <gmock/gmock.h>
Ben Chan7e776902014-06-18 13:19:51 -070025#include <gtest/gtest.h>
Ken Mixter03403162010-08-18 15:23:16 -070026
Simon Que9f90aca2013-02-19 17:19:52 -080027using base::FilePath;
Alex Vakulenko74dc6242015-10-13 09:23:34 -070028using ::brillo::FindLog;
Ken Mixtera3249322011-03-03 08:47:38 -080029
Daniel Eratd257ea12015-01-28 10:23:28 -070030namespace {
31
32int s_crashes = 0;
33bool s_metrics = true;
34
35const char kTestDirectory[] = "test";
36const char kTestSuspended[] = "test/suspended";
37const char kTestUnclean[] = "test/unclean";
38
Ken Mixter03403162010-08-18 15:23:16 -070039void CountCrash() {
40 ++s_crashes;
41}
42
43bool IsMetrics() {
44 return s_metrics;
45}
46
Daniel Eratd257ea12015-01-28 10:23:28 -070047} // namespace
48
Steve Fung6e139522015-02-05 14:54:16 -080049class UncleanShutdownCollectorMock : public UncleanShutdownCollector {
50 public:
51 MOCK_METHOD0(SetUpDBus, void());
52};
53
Ken Mixter03403162010-08-18 15:23:16 -070054class UncleanShutdownCollectorTest : public ::testing::Test {
55 void SetUp() {
56 s_crashes = 0;
Steve Fung6e139522015-02-05 14:54:16 -080057
58 EXPECT_CALL(collector_, SetUpDBus()).WillRepeatedly(testing::Return());
59
Ken Mixter03403162010-08-18 15:23:16 -070060 collector_.Initialize(CountCrash,
Ken Mixtera3249322011-03-03 08:47:38 -080061 IsMetrics);
Michael Krebs87a49502012-04-12 15:24:22 -070062 rmdir(kTestDirectory);
Ken Mixter03403162010-08-18 15:23:16 -070063 test_unclean_ = FilePath(kTestUnclean);
64 collector_.unclean_shutdown_file_ = kTestUnclean;
Mike Frysingera557c112014-02-05 22:55:39 -050065 base::DeleteFile(test_unclean_, true);
Daniel Erat9c9e1c42013-07-18 15:57:11 -070066 // Set up an alternate power manager state file as well
Simon Que3f7ed5d2010-11-30 17:10:54 -080067 collector_.powerd_suspended_file_ = FilePath(kTestSuspended);
Alex Vakulenko74dc6242015-10-13 09:23:34 -070068 brillo::ClearLog();
Ken Mixter03403162010-08-18 15:23:16 -070069 }
Steve Fung6e139522015-02-05 14:54:16 -080070
Ken Mixter03403162010-08-18 15:23:16 -070071 protected:
72 void WriteStringToFile(const FilePath &file_path,
73 const char *data) {
Ben Chanf30c6412014-05-22 23:09:01 -070074 ASSERT_EQ(strlen(data), base::WriteFile(file_path, data, strlen(data)));
Ken Mixter03403162010-08-18 15:23:16 -070075 }
76
Steve Fung6e139522015-02-05 14:54:16 -080077 UncleanShutdownCollectorMock collector_;
Ken Mixter03403162010-08-18 15:23:16 -070078 FilePath test_unclean_;
79};
80
81TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) {
82 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -050083 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -070084}
85
86TEST_F(UncleanShutdownCollectorTest, EnableWithParent) {
Michael Krebs87a49502012-04-12 15:24:22 -070087 mkdir(kTestDirectory, 0777);
Ken Mixter03403162010-08-18 15:23:16 -070088 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, EnableCannotWrite) {
93 collector_.unclean_shutdown_file_ = "/bad/path";
94 ASSERT_FALSE(collector_.Enable());
Ken Mixtera3249322011-03-03 08:47:38 -080095 ASSERT_TRUE(FindLog("Unable to create shutdown check file"));
Ken Mixter03403162010-08-18 15:23:16 -070096}
97
98TEST_F(UncleanShutdownCollectorTest, CollectTrue) {
99 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500100 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700101 ASSERT_TRUE(collector_.Collect());
Mike Frysingera557c112014-02-05 22:55:39 -0500102 ASSERT_FALSE(base::PathExists(test_unclean_));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800103 ASSERT_EQ(1, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800104 ASSERT_TRUE(FindLog("Last shutdown was not clean"));
Ken Mixter03403162010-08-18 15:23:16 -0700105}
106
107TEST_F(UncleanShutdownCollectorTest, CollectFalse) {
108 ASSERT_FALSE(collector_.Collect());
Simon Que3f7ed5d2010-11-30 17:10:54 -0800109 ASSERT_EQ(0, s_crashes);
110}
111
Simon Que3f7ed5d2010-11-30 17:10:54 -0800112TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) {
113 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500114 ASSERT_TRUE(base::PathExists(test_unclean_));
Ben Chanf30c6412014-05-22 23:09:01 -0700115 base::WriteFile(collector_.powerd_suspended_file_, "", 0);
Simon Que3f7ed5d2010-11-30 17:10:54 -0800116 ASSERT_FALSE(collector_.Collect());
Mike Frysingera557c112014-02-05 22:55:39 -0500117 ASSERT_FALSE(base::PathExists(test_unclean_));
118 ASSERT_FALSE(base::PathExists(collector_.powerd_suspended_file_));
Simon Que3f7ed5d2010-11-30 17:10:54 -0800119 ASSERT_EQ(0, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800120 ASSERT_TRUE(FindLog("Unclean shutdown occurred while suspended."));
Ken Mixter03403162010-08-18 15:23:16 -0700121}
122
123TEST_F(UncleanShutdownCollectorTest, Disable) {
124 ASSERT_TRUE(collector_.Enable());
Mike Frysingera557c112014-02-05 22:55:39 -0500125 ASSERT_TRUE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700126 ASSERT_TRUE(collector_.Disable());
Mike Frysingera557c112014-02-05 22:55:39 -0500127 ASSERT_FALSE(base::PathExists(test_unclean_));
Ken Mixter03403162010-08-18 15:23:16 -0700128 ASSERT_FALSE(collector_.Collect());
129}
130
131TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) {
132 ASSERT_TRUE(collector_.Disable());
133}
134
135TEST_F(UncleanShutdownCollectorTest, CantDisable) {
Michael Krebs87a49502012-04-12 15:24:22 -0700136 mkdir(kTestDirectory, 0700);
137 if (mkdir(kTestUnclean, 0700)) {
138 ASSERT_EQ(EEXIST, errno)
139 << "Error while creating directory '" << kTestUnclean
140 << "': " << strerror(errno);
141 }
Ben Chanf30c6412014-05-22 23:09:01 -0700142 ASSERT_EQ(0, base::WriteFile(test_unclean_.Append("foo"), "", 0))
Michael Krebs87a49502012-04-12 15:24:22 -0700143 << "Error while creating empty file '"
144 << test_unclean_.Append("foo").value() << "': " << strerror(errno);
Ken Mixter03403162010-08-18 15:23:16 -0700145 ASSERT_FALSE(collector_.Disable());
146 rmdir(kTestUnclean);
147}