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