blob: c4968adfc8e9a7c128c7f9c145b41f4713e3abb6 [file] [log] [blame]
Alex Deymo0d11c602014-04-23 20:12:20 -07001// Copyright (c) 2014 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 "update_engine/policy_manager/chromeos_policy.h"
6
7#include <string>
8
9#include <base/time/time.h>
10#include <gtest/gtest.h>
11
12#include "update_engine/fake_clock.h"
13#include "update_engine/policy_manager/evaluation_context.h"
14#include "update_engine/policy_manager/fake_state.h"
15#include "update_engine/policy_manager/pmtest_utils.h"
16
17using base::Time;
18using base::TimeDelta;
19using chromeos_update_engine::FakeClock;
20using std::string;
21
22namespace chromeos_policy_manager {
23
24class PmChromeOSPolicyTest : public ::testing::Test {
25 protected:
26 virtual void SetUp() {
27 SetUpDefaultClock();
28 eval_ctx_ = new EvaluationContext(&fake_clock_);
29 }
30
31 // Sets the clock to fixed values.
32 void SetUpDefaultClock() {
33 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
34 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
35 }
36
37 void SetUpDefaultState() {
38 fake_state_.updater_provider()->var_updater_started_time()->reset(
39 new Time(fake_clock_.GetWallclockTime()));
40 fake_state_.updater_provider()->var_last_checked_time()->reset(
41 new Time(fake_clock_.GetWallclockTime()));
42 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
43 reset(new unsigned int(0));
44
45 fake_state_.random_provider()->var_seed()->reset(
46 new uint64_t(4)); // chosen by fair dice roll.
47 // guaranteed to be random.
48 }
49
50 // Runs the passed |policy_method| policy and expects it to return the
51 // |expected| return value.
52 template<typename T, typename R, typename... Args>
53 void ExpectPolicyStatus(
54 EvalStatus expected,
55 T policy_method,
56 R* result, Args... args) {
57 string error = "<None>";
58 eval_ctx_->ResetEvaluation();
59 EXPECT_EQ(expected,
60 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result))
61 << "Returned error: " << error;
62 // TODO(deymo): Dump the context of the EvaluationContext on failure.
63 }
64
65 FakeClock fake_clock_;
66 FakeState fake_state_;
67 scoped_refptr<EvaluationContext> eval_ctx_;
68 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
69};
70
71TEST_F(PmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
72 Time next_update_check;
73
74 SetUpDefaultState();
75 ExpectPolicyStatus(EvalStatus::kSucceeded,
76 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
77
78 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
79 EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
80 ChromeOSPolicy::kTimeoutInitialInterval +
81 ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check);
82}
83
84TEST_F(PmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
85 Time next_update_check;
86
87 SetUpDefaultState();
88 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
89 reset(new unsigned int(100));
90 ExpectPolicyStatus(EvalStatus::kSucceeded,
91 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
92
93 EXPECT_LE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
94 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
95 ChromeOSPolicy::kTimeoutRegularFuzz - 1), next_update_check);
96 EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
97 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
98 ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check);
99}
100
101TEST_F(PmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
102 // We get the next update_check timestamp from the policy's private method
103 // and then we check the public method respects that value on the normal
104 // case.
105 Time next_update_check;
106 Time last_checked_time =
107 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
108
109 SetUpDefaultClock();
110 SetUpDefaultState();
111 fake_state_.updater_provider()->var_last_checked_time()->reset(
112 new Time(last_checked_time));
113 ExpectPolicyStatus(EvalStatus::kSucceeded,
114 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
115
116 UpdateCheckParams result;
117
118 // Check that the policy blocks until the next_update_check is reached.
119 SetUpDefaultClock();
120 SetUpDefaultState();
121 fake_state_.updater_provider()->var_last_checked_time()->reset(
122 new Time(last_checked_time));
123 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
124 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
125 &Policy::UpdateCheckAllowed, &result);
126
127 SetUpDefaultClock();
128 SetUpDefaultState();
129 fake_state_.updater_provider()->var_last_checked_time()->reset(
130 new Time(last_checked_time));
131 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
132 ExpectPolicyStatus(EvalStatus::kSucceeded,
133 &Policy::UpdateCheckAllowed, &result);
134}
135
136} // namespace chromeos_policy_manager