Steve Fung | 6c34c25 | 2015-08-20 00:27:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Steve Fung | 129bea5 | 2015-07-23 13:11:15 -0700 | [diff] [blame] | 17 | #include "unclean_shutdown_collector.h" |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 18 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 22 | #include <base/strings/string_util.h> |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame^] | 23 | #include <brillo/syslog_logging.h> |
Steve Fung | 6e13952 | 2015-02-05 14:54:16 -0800 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 26 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 27 | using base::FilePath; |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame^] | 28 | using ::brillo::FindLog; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 29 | |
Daniel Erat | d257ea1 | 2015-01-28 10:23:28 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | int s_crashes = 0; |
| 33 | bool s_metrics = true; |
| 34 | |
| 35 | const char kTestDirectory[] = "test"; |
| 36 | const char kTestSuspended[] = "test/suspended"; |
| 37 | const char kTestUnclean[] = "test/unclean"; |
| 38 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 39 | void CountCrash() { |
| 40 | ++s_crashes; |
| 41 | } |
| 42 | |
| 43 | bool IsMetrics() { |
| 44 | return s_metrics; |
| 45 | } |
| 46 | |
Daniel Erat | d257ea1 | 2015-01-28 10:23:28 -0700 | [diff] [blame] | 47 | } // namespace |
| 48 | |
Steve Fung | 6e13952 | 2015-02-05 14:54:16 -0800 | [diff] [blame] | 49 | class UncleanShutdownCollectorMock : public UncleanShutdownCollector { |
| 50 | public: |
| 51 | MOCK_METHOD0(SetUpDBus, void()); |
| 52 | }; |
| 53 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 54 | class UncleanShutdownCollectorTest : public ::testing::Test { |
| 55 | void SetUp() { |
| 56 | s_crashes = 0; |
Steve Fung | 6e13952 | 2015-02-05 14:54:16 -0800 | [diff] [blame] | 57 | |
| 58 | EXPECT_CALL(collector_, SetUpDBus()).WillRepeatedly(testing::Return()); |
| 59 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 60 | collector_.Initialize(CountCrash, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 61 | IsMetrics); |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 62 | rmdir(kTestDirectory); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 63 | test_unclean_ = FilePath(kTestUnclean); |
| 64 | collector_.unclean_shutdown_file_ = kTestUnclean; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 65 | base::DeleteFile(test_unclean_, true); |
Daniel Erat | 9c9e1c4 | 2013-07-18 15:57:11 -0700 | [diff] [blame] | 66 | // Set up an alternate power manager state file as well |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 67 | collector_.powerd_suspended_file_ = FilePath(kTestSuspended); |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame^] | 68 | brillo::ClearLog(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 69 | } |
Steve Fung | 6e13952 | 2015-02-05 14:54:16 -0800 | [diff] [blame] | 70 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 71 | protected: |
| 72 | void WriteStringToFile(const FilePath &file_path, |
| 73 | const char *data) { |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 74 | ASSERT_EQ(strlen(data), base::WriteFile(file_path, data, strlen(data))); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Steve Fung | 6e13952 | 2015-02-05 14:54:16 -0800 | [diff] [blame] | 77 | UncleanShutdownCollectorMock collector_; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 78 | FilePath test_unclean_; |
| 79 | }; |
| 80 | |
| 81 | TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { |
| 82 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 83 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 87 | mkdir(kTestDirectory, 0777); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 88 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 89 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { |
| 93 | collector_.unclean_shutdown_file_ = "/bad/path"; |
| 94 | ASSERT_FALSE(collector_.Enable()); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 95 | ASSERT_TRUE(FindLog("Unable to create shutdown check file")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(UncleanShutdownCollectorTest, CollectTrue) { |
| 99 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 100 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(collector_.Collect()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 102 | ASSERT_FALSE(base::PathExists(test_unclean_)); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 103 | ASSERT_EQ(1, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 104 | ASSERT_TRUE(FindLog("Last shutdown was not clean")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST_F(UncleanShutdownCollectorTest, CollectFalse) { |
| 108 | ASSERT_FALSE(collector_.Collect()); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 109 | ASSERT_EQ(0, s_crashes); |
| 110 | } |
| 111 | |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 112 | TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) { |
| 113 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 114 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 115 | base::WriteFile(collector_.powerd_suspended_file_, "", 0); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 116 | ASSERT_FALSE(collector_.Collect()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 117 | ASSERT_FALSE(base::PathExists(test_unclean_)); |
| 118 | ASSERT_FALSE(base::PathExists(collector_.powerd_suspended_file_)); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 119 | ASSERT_EQ(0, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 120 | ASSERT_TRUE(FindLog("Unclean shutdown occurred while suspended.")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(UncleanShutdownCollectorTest, Disable) { |
| 124 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 125 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 126 | ASSERT_TRUE(collector_.Disable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 127 | ASSERT_FALSE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 128 | ASSERT_FALSE(collector_.Collect()); |
| 129 | } |
| 130 | |
| 131 | TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { |
| 132 | ASSERT_TRUE(collector_.Disable()); |
| 133 | } |
| 134 | |
| 135 | TEST_F(UncleanShutdownCollectorTest, CantDisable) { |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 136 | mkdir(kTestDirectory, 0700); |
| 137 | if (mkdir(kTestUnclean, 0700)) { |
| 138 | ASSERT_EQ(EEXIST, errno) |
| 139 | << "Error while creating directory '" << kTestUnclean |
| 140 | << "': " << strerror(errno); |
| 141 | } |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 142 | ASSERT_EQ(0, base::WriteFile(test_unclean_.Append("foo"), "", 0)) |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 143 | << "Error while creating empty file '" |
| 144 | << test_unclean_.Append("foo").value() << "': " << strerror(errno); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 145 | ASSERT_FALSE(collector_.Disable()); |
| 146 | rmdir(kTestUnclean); |
| 147 | } |