Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include "base/file_util.h" |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 9 | #include "chromeos/syslog_logging.h" |
| 10 | #include "chromeos/test_helpers.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 11 | #include "crash-reporter/unclean_shutdown_collector.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | static int s_crashes = 0; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 15 | static bool s_metrics = true; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 17 | static const char kTestDirectory[] = "test"; |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 18 | static const char kTestSuspended[] = "test/suspended"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 19 | static const char kTestUnclean[] = "test/unclean"; |
| 20 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 21 | using base::FilePath; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 22 | using ::chromeos::FindLog; |
| 23 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 24 | void CountCrash() { |
| 25 | ++s_crashes; |
| 26 | } |
| 27 | |
| 28 | bool IsMetrics() { |
| 29 | return s_metrics; |
| 30 | } |
| 31 | |
| 32 | class UncleanShutdownCollectorTest : public ::testing::Test { |
| 33 | void SetUp() { |
| 34 | s_crashes = 0; |
| 35 | collector_.Initialize(CountCrash, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 36 | IsMetrics); |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 37 | rmdir(kTestDirectory); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 38 | test_unclean_ = FilePath(kTestUnclean); |
| 39 | collector_.unclean_shutdown_file_ = kTestUnclean; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 40 | base::DeleteFile(test_unclean_, true); |
Daniel Erat | 9c9e1c4 | 2013-07-18 15:57:11 -0700 | [diff] [blame] | 41 | // Set up an alternate power manager state file as well |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 42 | collector_.powerd_suspended_file_ = FilePath(kTestSuspended); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 43 | chromeos::ClearLog(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 44 | } |
| 45 | protected: |
| 46 | void WriteStringToFile(const FilePath &file_path, |
| 47 | const char *data) { |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame^] | 48 | ASSERT_EQ(strlen(data), base::WriteFile(file_path, data, strlen(data))); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 51 | UncleanShutdownCollector collector_; |
| 52 | FilePath test_unclean_; |
| 53 | }; |
| 54 | |
| 55 | TEST_F(UncleanShutdownCollectorTest, EnableWithoutParent) { |
| 56 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 57 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST_F(UncleanShutdownCollectorTest, EnableWithParent) { |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 61 | mkdir(kTestDirectory, 0777); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 62 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 63 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST_F(UncleanShutdownCollectorTest, EnableCannotWrite) { |
| 67 | collector_.unclean_shutdown_file_ = "/bad/path"; |
| 68 | ASSERT_FALSE(collector_.Enable()); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 69 | ASSERT_TRUE(FindLog("Unable to create shutdown check file")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(UncleanShutdownCollectorTest, CollectTrue) { |
| 73 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 74 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 75 | ASSERT_TRUE(collector_.Collect()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 76 | ASSERT_FALSE(base::PathExists(test_unclean_)); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 77 | ASSERT_EQ(1, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 78 | ASSERT_TRUE(FindLog("Last shutdown was not clean")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST_F(UncleanShutdownCollectorTest, CollectFalse) { |
| 82 | ASSERT_FALSE(collector_.Collect()); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 83 | ASSERT_EQ(0, s_crashes); |
| 84 | } |
| 85 | |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 86 | TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) { |
| 87 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 88 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame^] | 89 | base::WriteFile(collector_.powerd_suspended_file_, "", 0); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 90 | ASSERT_FALSE(collector_.Collect()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 91 | ASSERT_FALSE(base::PathExists(test_unclean_)); |
| 92 | ASSERT_FALSE(base::PathExists(collector_.powerd_suspended_file_)); |
Simon Que | 3f7ed5d | 2010-11-30 17:10:54 -0800 | [diff] [blame] | 93 | ASSERT_EQ(0, s_crashes); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 94 | ASSERT_TRUE(FindLog("Unclean shutdown occurred while suspended.")); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST_F(UncleanShutdownCollectorTest, Disable) { |
| 98 | ASSERT_TRUE(collector_.Enable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 99 | ASSERT_TRUE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 100 | ASSERT_TRUE(collector_.Disable()); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 101 | ASSERT_FALSE(base::PathExists(test_unclean_)); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 102 | ASSERT_FALSE(collector_.Collect()); |
| 103 | } |
| 104 | |
| 105 | TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { |
| 106 | ASSERT_TRUE(collector_.Disable()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(UncleanShutdownCollectorTest, CantDisable) { |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 110 | mkdir(kTestDirectory, 0700); |
| 111 | if (mkdir(kTestUnclean, 0700)) { |
| 112 | ASSERT_EQ(EEXIST, errno) |
| 113 | << "Error while creating directory '" << kTestUnclean |
| 114 | << "': " << strerror(errno); |
| 115 | } |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame^] | 116 | ASSERT_EQ(0, base::WriteFile(test_unclean_.Append("foo"), "", 0)) |
Michael Krebs | 87a4950 | 2012-04-12 15:24:22 -0700 | [diff] [blame] | 117 | << "Error while creating empty file '" |
| 118 | << test_unclean_.Append("foo").value() << "': " << strerror(errno); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 119 | ASSERT_FALSE(collector_.Disable()); |
| 120 | rmdir(kTestUnclean); |
| 121 | } |
| 122 | |
| 123 | int main(int argc, char **argv) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 124 | SetUpTests(&argc, argv, false); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 125 | return RUN_ALL_TESTS(); |
| 126 | } |