blob: ad5fb0e090c6b291dec1b0b17e3c7f221ca4f36b [file] [log] [blame]
Darin Petkovf0136cd2012-11-07 16:18:02 +01001// 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 Petkov38066762012-12-17 15:35:45 +01007#include <base/file_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -08008#include <base/files/scoped_temp_dir.h>
Darin Petkovf0136cd2012-11-07 16:18:02 +01009#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
Darin Petkov38066762012-12-17 15:35:45 +010012#include "shill/mock_minijail.h"
13#include "shill/mock_process_killer.h"
Darin Petkov328e19d2012-12-04 15:54:07 +010014#include "shill/mock_time.h"
Darin Petkovf0136cd2012-11-07 16:18:02 +010015
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080016using base::FilePath;
Darin Petkovbd0dcc82012-11-29 10:51:12 +010017using testing::_;
Darin Petkov38066762012-12-17 15:35:45 +010018using testing::ElementsAre;
Darin Petkovfac01f02012-12-06 12:44:07 +010019using testing::InSequence;
Darin Petkov38066762012-12-17 15:35:45 +010020using testing::IsNull;
Darin Petkovf0136cd2012-11-07 16:18:02 +010021using testing::Return;
Darin Petkov328e19d2012-12-04 15:54:07 +010022using testing::SetArgumentPointee;
Darin Petkov38066762012-12-17 15:35:45 +010023using testing::StrEq;
Darin Petkovf0136cd2012-11-07 16:18:02 +010024
25namespace shill {
26
Darin Petkovf0136cd2012-11-07 16:18:02 +010027namespace {
28
29class 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 Petkov328e19d2012-12-04 15:54:07 +010041class DiagnosticsReporterTest : public testing::Test {
42 public:
43 DiagnosticsReporterTest() {
Darin Petkov38066762012-12-17 15:35:45 +010044 reporter_.minijail_ = &minijail_;
45 reporter_.process_killer_ = &process_killer_;
Darin Petkov328e19d2012-12-04 15:54:07 +010046 reporter_.time_ = &time_;
Darin Petkov328e19d2012-12-04 15:54:07 +010047 }
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 Petkov38066762012-12-17 15:35:45 +010060 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 Petkov328e19d2012-12-04 15:54:07 +010066 MockTime time_;
67 ReporterUnderTest reporter_;
68};
69
Darin Petkovf0136cd2012-11-07 16:18:02 +010070TEST_F(DiagnosticsReporterTest, IsReportingEnabled) {
71 EXPECT_FALSE(IsReportingEnabled());
72}
73
Darin Petkov328e19d2012-12-04 15:54:07 +010074TEST_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
88TEST_F(DiagnosticsReporterTest, OnConnectivityEvent) {
Darin Petkov19321e42012-12-17 15:40:59 +010089 const uint64 kInitStash = 0;
90 SetLastLogStash(kInitStash);
91 // Test that the initial call is not throttled.
92 const uint64 kNow0 = kInitStash + 1;
Darin Petkovfac01f02012-12-06 12:44:07 +010093 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 Petkov328e19d2012-12-04 15:54:07 +0100100 .tv_usec = 0
101 };
Paul Stewart5ad16062013-02-21 18:10:48 -0800102 base::ScopedTempDir temp_dir_;
Darin Petkov38066762012-12-17 15:35:45 +0100103 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 Petkov328e19d2012-12-04 15:54:07 +0100108 EXPECT_CALL(time_, GetTimeMonotonic(_))
Darin Petkovfac01f02012-12-06 12:44:07 +0100109 .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 Petkov38066762012-12-17 15:35:45 +0100114 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 Petkov328e19d2012-12-04 15:54:07 +0100128 reporter_.OnConnectivityEvent();
Darin Petkovfac01f02012-12-06 12:44:07 +0100129 EXPECT_EQ(kNow0, GetLastLogStash());
Darin Petkov38066762012-12-17 15:35:45 +0100130 EXPECT_FALSE(file_util::PathExists(stashed_net_log));
Darin Petkovfac01f02012-12-06 12:44:07 +0100131 reporter_.OnConnectivityEvent();
132 EXPECT_EQ(kNow1, GetLastLogStash());
Darin Petkov328e19d2012-12-04 15:54:07 +0100133}
134
Darin Petkovf0136cd2012-11-07 16:18:02 +0100135} // namespace shill