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