blob: d1da611c26e6d9f90cb46e823efdc97642897cbf [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
Ben Chan11c213f2014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Paul Stewart5ad16062013-02-21 18:10:48 -08008#include <base/files/scoped_temp_dir.h>
Utkarsh Sanghi83bd64b2014-07-29 16:01:43 -07009#include <chromeos/minijail/mock_minijail.h>
Darin Petkovf0136cd2012-11-07 16:18:02 +010010#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
Darin Petkov38066762012-12-17 15:35:45 +010013#include "shill/mock_process_killer.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070014#include "shill/net/mock_time.h"
Darin Petkovf0136cd2012-11-07 16:18:02 +010015
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080016using base::FilePath;
Utkarsh Sanghi83bd64b2014-07-29 16:01:43 -070017using chromeos::MockMinijail;
Darin Petkovbd0dcc82012-11-29 10:51:12 +010018using testing::_;
Darin Petkov38066762012-12-17 15:35:45 +010019using testing::ElementsAre;
Darin Petkovfac01f02012-12-06 12:44:07 +010020using testing::InSequence;
Darin Petkov38066762012-12-17 15:35:45 +010021using testing::IsNull;
Darin Petkovf0136cd2012-11-07 16:18:02 +010022using testing::Return;
Darin Petkov328e19d2012-12-04 15:54:07 +010023using testing::SetArgumentPointee;
Darin Petkov38066762012-12-17 15:35:45 +010024using testing::StrEq;
Darin Petkovf0136cd2012-11-07 16:18:02 +010025
26namespace shill {
27
Darin Petkovf0136cd2012-11-07 16:18:02 +010028namespace {
29
30class ReporterUnderTest : public DiagnosticsReporter {
31 public:
32 ReporterUnderTest() {}
33
34 MOCK_METHOD0(IsReportingEnabled, bool());
35
36 private:
37 DISALLOW_COPY_AND_ASSIGN(ReporterUnderTest);
38};
39
40} // namespace
41
Darin Petkov328e19d2012-12-04 15:54:07 +010042class DiagnosticsReporterTest : public testing::Test {
43 public:
44 DiagnosticsReporterTest() {
Darin Petkov38066762012-12-17 15:35:45 +010045 reporter_.minijail_ = &minijail_;
46 reporter_.process_killer_ = &process_killer_;
Darin Petkov328e19d2012-12-04 15:54:07 +010047 reporter_.time_ = &time_;
Darin Petkov328e19d2012-12-04 15:54:07 +010048 }
49
50 protected:
51 bool IsReportingEnabled() {
52 return DiagnosticsReporter::GetInstance()->IsReportingEnabled();
53 }
54
Ben Chan7fab8972014-08-10 17:14:46 -070055 void SetLastLogStash(uint64_t time) { reporter_.last_log_stash_ = time; }
56 uint64_t GetLastLogStash() { return reporter_.last_log_stash_; }
57 uint64_t GetLogStashThrottleSeconds() {
Darin Petkov328e19d2012-12-04 15:54:07 +010058 return DiagnosticsReporter::kLogStashThrottleSeconds;
59 }
60
Paul Stewart3b30ca52015-06-16 13:13:10 -070061 void SetStashedNetLog(const FilePath& stashed_net_log) {
Darin Petkov38066762012-12-17 15:35:45 +010062 reporter_.stashed_net_log_ = stashed_net_log;
63 }
64
65 MockMinijail minijail_;
66 MockProcessKiller process_killer_;
Darin Petkov328e19d2012-12-04 15:54:07 +010067 MockTime time_;
68 ReporterUnderTest reporter_;
69};
70
Darin Petkovf0136cd2012-11-07 16:18:02 +010071TEST_F(DiagnosticsReporterTest, IsReportingEnabled) {
72 EXPECT_FALSE(IsReportingEnabled());
73}
74
Darin Petkov328e19d2012-12-04 15:54:07 +010075TEST_F(DiagnosticsReporterTest, OnConnectivityEventThrottle) {
Alex Vakulenko8a532292014-06-16 17:18:44 -070076 using timeval_seconds_t = decltype(timeval::tv_sec);
77 const timeval_seconds_t kLastStash = 50;
78 auto kNow = kLastStash +
79 static_cast<timeval_seconds_t>(GetLogStashThrottleSeconds() - 1);
Darin Petkov328e19d2012-12-04 15:54:07 +010080 SetLastLogStash(kLastStash);
Alex Vakulenko8a532292014-06-16 17:18:44 -070081 const struct timeval now = {kNow, 0};
Darin Petkov328e19d2012-12-04 15:54:07 +010082 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) {
Alex Vakulenko8a532292014-06-16 17:18:44 -070089 using timeval_seconds_t = decltype(timeval::tv_sec);
Ben Chan7fab8972014-08-10 17:14:46 -070090 const uint64_t kInitStash = 0;
Darin Petkov19321e42012-12-17 15:40:59 +010091 SetLastLogStash(kInitStash);
92 // Test that the initial call is not throttled.
Alex Vakulenko8a532292014-06-16 17:18:44 -070093 auto kNow0 = static_cast<timeval_seconds_t>(kInitStash + 1);
94 const struct timeval now0 = {kNow0, 0};
95 auto kNow1 = kNow0 +
96 static_cast<timeval_seconds_t>(GetLogStashThrottleSeconds() + 1);
97 const struct timeval now1 = {kNow1, 0};
Paul Stewart5ad16062013-02-21 18:10:48 -080098 base::ScopedTempDir temp_dir_;
Darin Petkov38066762012-12-17 15:35:45 +010099 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
100 FilePath stashed_net_log = temp_dir_.path().Append("stashed-net-log");
Ben Chan6fbf64f2014-05-21 18:07:01 -0700101 EXPECT_EQ(0, base::WriteFile(stashed_net_log, "", 0));
Ben Chana0ddf462014-02-06 11:32:42 -0800102 EXPECT_TRUE(base::PathExists(stashed_net_log));
Darin Petkov38066762012-12-17 15:35:45 +0100103 SetStashedNetLog(stashed_net_log);
Darin Petkov328e19d2012-12-04 15:54:07 +0100104 EXPECT_CALL(time_, GetTimeMonotonic(_))
Darin Petkovfac01f02012-12-06 12:44:07 +0100105 .WillOnce(DoAll(SetArgumentPointee<0>(now0), Return(0)))
106 .WillOnce(DoAll(SetArgumentPointee<0>(now1), Return(0)));
107 EXPECT_CALL(reporter_, IsReportingEnabled())
108 .WillOnce(Return(false))
109 .WillOnce(Return(true));
Darin Petkov38066762012-12-17 15:35:45 +0100110 EXPECT_CALL(minijail_, New()).Times(2);
Utkarsh Sanghie4c6aff2014-07-30 14:49:03 -0700111 EXPECT_CALL(minijail_, DropRoot(_, StrEq("syslog"),
112 StrEq("syslog"))).Times(2);
Darin Petkov38066762012-12-17 15:35:45 +0100113 const pid_t kPID = 123;
114 {
115 InSequence s;
116 EXPECT_CALL(minijail_,
117 RunAndDestroy(_, ElementsAre(_, IsNull()), _))
118 .WillOnce(DoAll(SetArgumentPointee<2>(kPID), Return(true)));
119 EXPECT_CALL(
120 minijail_,
121 RunAndDestroy(_, ElementsAre(_, StrEq("--upload"), IsNull()), _))
122 .WillOnce(Return(false));
123 }
124 EXPECT_CALL(process_killer_, Wait(kPID, _)).Times(1);
Darin Petkov328e19d2012-12-04 15:54:07 +0100125 reporter_.OnConnectivityEvent();
Darin Petkovfac01f02012-12-06 12:44:07 +0100126 EXPECT_EQ(kNow0, GetLastLogStash());
Ben Chana0ddf462014-02-06 11:32:42 -0800127 EXPECT_FALSE(base::PathExists(stashed_net_log));
Darin Petkovfac01f02012-12-06 12:44:07 +0100128 reporter_.OnConnectivityEvent();
129 EXPECT_EQ(kNow1, GetLastLogStash());
Darin Petkov328e19d2012-12-04 15:54:07 +0100130}
131
Darin Petkovf0136cd2012-11-07 16:18:02 +0100132} // namespace shill