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