Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "shill/diagnostics_reporter.h" |
| 6 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 7 | #include <base/file_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 8 | #include <base/files/scoped_temp_dir.h> |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 12 | #include "shill/mock_minijail.h" |
| 13 | #include "shill/mock_process_killer.h" |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 14 | #include "shill/mock_time.h" |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 15 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 16 | using base::FilePath; |
Darin Petkov | bd0dcc8 | 2012-11-29 10:51:12 +0100 | [diff] [blame] | 17 | using testing::_; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 18 | using testing::ElementsAre; |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 19 | using testing::InSequence; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 20 | using testing::IsNull; |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 21 | using testing::Return; |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 22 | using testing::SetArgumentPointee; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 23 | using testing::StrEq; |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 24 | |
| 25 | namespace shill { |
| 26 | |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | class ReporterUnderTest : public DiagnosticsReporter { |
| 30 | public: |
| 31 | ReporterUnderTest() {} |
| 32 | |
| 33 | MOCK_METHOD0(IsReportingEnabled, bool()); |
| 34 | |
| 35 | private: |
| 36 | DISALLOW_COPY_AND_ASSIGN(ReporterUnderTest); |
| 37 | }; |
| 38 | |
| 39 | } // namespace |
| 40 | |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 41 | class DiagnosticsReporterTest : public testing::Test { |
| 42 | public: |
| 43 | DiagnosticsReporterTest() { |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 44 | reporter_.minijail_ = &minijail_; |
| 45 | reporter_.process_killer_ = &process_killer_; |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 46 | reporter_.time_ = &time_; |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | protected: |
| 50 | bool IsReportingEnabled() { |
| 51 | return DiagnosticsReporter::GetInstance()->IsReportingEnabled(); |
| 52 | } |
| 53 | |
| 54 | void SetLastLogStash(uint64 time) { reporter_.last_log_stash_ = time; } |
| 55 | uint64 GetLastLogStash() { return reporter_.last_log_stash_; } |
| 56 | uint64 GetLogStashThrottleSeconds() { |
| 57 | return DiagnosticsReporter::kLogStashThrottleSeconds; |
| 58 | } |
| 59 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 60 | void SetStashedNetLog(const FilePath &stashed_net_log) { |
| 61 | reporter_.stashed_net_log_ = stashed_net_log; |
| 62 | } |
| 63 | |
| 64 | MockMinijail minijail_; |
| 65 | MockProcessKiller process_killer_; |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 66 | MockTime time_; |
| 67 | ReporterUnderTest reporter_; |
| 68 | }; |
| 69 | |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 70 | TEST_F(DiagnosticsReporterTest, IsReportingEnabled) { |
| 71 | EXPECT_FALSE(IsReportingEnabled()); |
| 72 | } |
| 73 | |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 74 | TEST_F(DiagnosticsReporterTest, OnConnectivityEventThrottle) { |
| 75 | const uint64 kLastStash = 50; |
| 76 | const uint64 kNow = kLastStash + GetLogStashThrottleSeconds() - 1; |
| 77 | SetLastLogStash(kLastStash); |
| 78 | const struct timeval now = { |
| 79 | .tv_sec = static_cast<long int>(kNow), |
| 80 | .tv_usec = 0 |
| 81 | }; |
| 82 | EXPECT_CALL(time_, GetTimeMonotonic(_)) |
| 83 | .WillOnce(DoAll(SetArgumentPointee<0>(now), Return(0))); |
| 84 | reporter_.OnConnectivityEvent(); |
| 85 | EXPECT_EQ(kLastStash, GetLastLogStash()); |
| 86 | } |
| 87 | |
| 88 | TEST_F(DiagnosticsReporterTest, OnConnectivityEvent) { |
Darin Petkov | 19321e4 | 2012-12-17 15:40:59 +0100 | [diff] [blame] | 89 | const uint64 kInitStash = 0; |
| 90 | SetLastLogStash(kInitStash); |
| 91 | // Test that the initial call is not throttled. |
| 92 | const uint64 kNow0 = kInitStash + 1; |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 93 | const struct timeval now0 = { |
| 94 | .tv_sec = static_cast<long int>(kNow0), |
| 95 | .tv_usec = 0 |
| 96 | }; |
| 97 | const uint64 kNow1 = kNow0 + GetLogStashThrottleSeconds() + 1; |
| 98 | const struct timeval now1 = { |
| 99 | .tv_sec = static_cast<long int>(kNow1), |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 100 | .tv_usec = 0 |
| 101 | }; |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 102 | base::ScopedTempDir temp_dir_; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 103 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 104 | FilePath stashed_net_log = temp_dir_.path().Append("stashed-net-log"); |
| 105 | EXPECT_EQ(0, file_util::WriteFile(stashed_net_log, "", 0)); |
| 106 | EXPECT_TRUE(file_util::PathExists(stashed_net_log)); |
| 107 | SetStashedNetLog(stashed_net_log); |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 108 | EXPECT_CALL(time_, GetTimeMonotonic(_)) |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 109 | .WillOnce(DoAll(SetArgumentPointee<0>(now0), Return(0))) |
| 110 | .WillOnce(DoAll(SetArgumentPointee<0>(now1), Return(0))); |
| 111 | EXPECT_CALL(reporter_, IsReportingEnabled()) |
| 112 | .WillOnce(Return(false)) |
| 113 | .WillOnce(Return(true)); |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 114 | EXPECT_CALL(minijail_, New()).Times(2); |
| 115 | EXPECT_CALL(minijail_, DropRoot(_, StrEq("syslog"))).Times(2); |
| 116 | const pid_t kPID = 123; |
| 117 | { |
| 118 | InSequence s; |
| 119 | EXPECT_CALL(minijail_, |
| 120 | RunAndDestroy(_, ElementsAre(_, IsNull()), _)) |
| 121 | .WillOnce(DoAll(SetArgumentPointee<2>(kPID), Return(true))); |
| 122 | EXPECT_CALL( |
| 123 | minijail_, |
| 124 | RunAndDestroy(_, ElementsAre(_, StrEq("--upload"), IsNull()), _)) |
| 125 | .WillOnce(Return(false)); |
| 126 | } |
| 127 | EXPECT_CALL(process_killer_, Wait(kPID, _)).Times(1); |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 128 | reporter_.OnConnectivityEvent(); |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 129 | EXPECT_EQ(kNow0, GetLastLogStash()); |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 130 | EXPECT_FALSE(file_util::PathExists(stashed_net_log)); |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 131 | reporter_.OnConnectivityEvent(); |
| 132 | EXPECT_EQ(kNow1, GetLastLogStash()); |
Darin Petkov | 328e19d | 2012-12-04 15:54:07 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Darin Petkov | f0136cd | 2012-11-07 16:18:02 +0100 | [diff] [blame] | 135 | } // namespace shill |