blob: 279051fae3839c5eb2df49dc41185c20fbbe711e [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>
8#include <base/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
Darin Petkovbd0dcc82012-11-29 10:51:12 +010016using testing::_;
Darin Petkov38066762012-12-17 15:35:45 +010017using testing::ElementsAre;
Darin Petkovfac01f02012-12-06 12:44:07 +010018using testing::InSequence;
Darin Petkov38066762012-12-17 15:35:45 +010019using testing::IsNull;
Darin Petkovf0136cd2012-11-07 16:18:02 +010020using testing::Return;
Darin Petkov328e19d2012-12-04 15:54:07 +010021using testing::SetArgumentPointee;
Darin Petkov38066762012-12-17 15:35:45 +010022using testing::StrEq;
Darin Petkovf0136cd2012-11-07 16:18:02 +010023
24namespace shill {
25
Darin Petkovf0136cd2012-11-07 16:18:02 +010026namespace {
27
28class 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 Petkov328e19d2012-12-04 15:54:07 +010040class DiagnosticsReporterTest : public testing::Test {
41 public:
42 DiagnosticsReporterTest() {
Darin Petkov38066762012-12-17 15:35:45 +010043 reporter_.minijail_ = &minijail_;
44 reporter_.process_killer_ = &process_killer_;
Darin Petkov328e19d2012-12-04 15:54:07 +010045 reporter_.time_ = &time_;
Darin Petkov328e19d2012-12-04 15:54:07 +010046 }
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 Petkov38066762012-12-17 15:35:45 +010059 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 Petkov328e19d2012-12-04 15:54:07 +010065 MockTime time_;
66 ReporterUnderTest reporter_;
67};
68
Darin Petkovf0136cd2012-11-07 16:18:02 +010069TEST_F(DiagnosticsReporterTest, IsReportingEnabled) {
70 EXPECT_FALSE(IsReportingEnabled());
71}
72
Darin Petkov328e19d2012-12-04 15:54:07 +010073TEST_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
87TEST_F(DiagnosticsReporterTest, OnConnectivityEvent) {
Darin Petkov19321e42012-12-17 15:40:59 +010088 const uint64 kInitStash = 0;
89 SetLastLogStash(kInitStash);
90 // Test that the initial call is not throttled.
91 const uint64 kNow0 = kInitStash + 1;
Darin Petkovfac01f02012-12-06 12:44:07 +010092 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 Petkov328e19d2012-12-04 15:54:07 +010099 .tv_usec = 0
100 };
Darin Petkov38066762012-12-17 15:35:45 +0100101 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 Petkov328e19d2012-12-04 15:54:07 +0100107 EXPECT_CALL(time_, GetTimeMonotonic(_))
Darin Petkovfac01f02012-12-06 12:44:07 +0100108 .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 Petkov38066762012-12-17 15:35:45 +0100113 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 Petkov328e19d2012-12-04 15:54:07 +0100127 reporter_.OnConnectivityEvent();
Darin Petkovfac01f02012-12-06 12:44:07 +0100128 EXPECT_EQ(kNow0, GetLastLogStash());
Darin Petkov38066762012-12-17 15:35:45 +0100129 EXPECT_FALSE(file_util::PathExists(stashed_net_log));
Darin Petkovfac01f02012-12-06 12:44:07 +0100130 reporter_.OnConnectivityEvent();
131 EXPECT_EQ(kNow1, GetLastLogStash());
Darin Petkov328e19d2012-12-04 15:54:07 +0100132}
133
Darin Petkovf0136cd2012-11-07 16:18:02 +0100134} // namespace shill