blob: c0ce092521f4811b584d57f065f280376e104810 [file] [log] [blame]
Alex Deymoc705cc82014-02-19 11:15:00 -08001// 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
Gilad Arnoldf9f85d62014-06-19 18:07:01 -07005#include <unistd.h>
6
Alex Deymo7b948f02014-03-10 17:01:10 -07007#include <algorithm>
Ben Chan02f7c1d2014-10-18 15:18:02 -07008#include <memory>
Alex Deymo7b948f02014-03-10 17:01:10 -07009#include <string>
Gilad Arnolddc4bb262014-07-23 10:45:19 -070010#include <tuple>
Alex Deymo94c06162014-03-21 20:34:46 -070011#include <utility>
Alex Deymo7b948f02014-03-10 17:01:10 -070012#include <vector>
13
14#include <base/bind.h>
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070015#include <base/time/time.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080016#include <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070017#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080018
Alex Deymo41a75a72014-04-15 15:36:22 -070019#include "update_engine/fake_clock.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070020#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070021#include "update_engine/update_manager/default_policy.h"
22#include "update_engine/update_manager/fake_state.h"
23#include "update_engine/update_manager/mock_policy.h"
24#include "update_engine/update_manager/umtest_utils.h"
25#include "update_engine/update_manager/update_manager.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080026
Alex Deymo7b948f02014-03-10 17:01:10 -070027using base::Bind;
28using base::Callback;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070029using base::Time;
30using base::TimeDelta;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070031using chromeos_update_engine::ErrorCode;
Alex Deymo41a75a72014-04-15 15:36:22 -070032using chromeos_update_engine::FakeClock;
Alex Deymo10875d92014-11-10 21:52:57 -080033using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
Alex Deymo7b948f02014-03-10 17:01:10 -070034using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080035using std::string;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070036using std::tuple;
Ben Chan02f7c1d2014-10-18 15:18:02 -070037using std::unique_ptr;
Alex Deymo7b948f02014-03-10 17:01:10 -070038using std::vector;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080039
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070040namespace {
41
42// Generates a fixed timestamp for use in faking the current time.
43Time FixedTime() {
44 Time::Exploded now_exp;
45 now_exp.year = 2014;
46 now_exp.month = 3;
47 now_exp.day_of_week = 2;
48 now_exp.day_of_month = 18;
49 now_exp.hour = 8;
50 now_exp.minute = 5;
51 now_exp.second = 33;
52 now_exp.millisecond = 675;
53 return Time::FromLocalExploded(now_exp);
54}
55
56} // namespace
57
Alex Deymo63784a52014-05-28 10:46:14 -070058namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080059
Alex Deymo63784a52014-05-28 10:46:14 -070060class UmUpdateManagerTest : public ::testing::Test {
Alex Deymoc705cc82014-02-19 11:15:00 -080061 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -070062 void SetUp() override {
Alex Deymo42c30c32014-04-24 18:41:18 -070063 fake_state_ = new FakeState();
Gilad Arnoldb2271992014-06-19 12:35:24 -070064 umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070065 TimeDelta::FromSeconds(1), fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080066 }
67
Alex Deymo63784a52014-05-28 10:46:14 -070068 FakeState* fake_state_; // Owned by the umut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070069 FakeClock fake_clock_;
Ben Chan02f7c1d2014-10-18 15:18:02 -070070 unique_ptr<UpdateManager> umut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080071};
72
73// The FailingPolicy implements a single method and make it always fail. This
74// class extends the DefaultPolicy class to allow extensions of the Policy
75// class without extending nor changing this test.
76class FailingPolicy : public DefaultPolicy {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070077 public:
78 explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {}
79 FailingPolicy() : FailingPolicy(nullptr) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -070080 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
81 string* error,
82 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070083 if (num_called_p_)
84 (*num_called_p_)++;
Alex Deymoc705cc82014-02-19 11:15:00 -080085 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070086 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080087 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070088
89 protected:
Alex Deymof329b932014-10-30 01:37:48 -070090 string PolicyName() const override { return "FailingPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070091
92 private:
93 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -080094};
95
Alex Deymo7b948f02014-03-10 17:01:10 -070096// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080097class LazyPolicy : public DefaultPolicy {
Alex Vakulenko157fe302014-08-11 15:59:58 -070098 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
99 string* error,
100 UpdateCheckParams* result) const override {
Alex Deymoe636c3c2014-03-11 19:02:08 -0700101 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -0800102 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700103
104 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700105 string PolicyName() const override { return "LazyPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700106};
107
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700108// A policy that sleeps for a predetermined amount of time, then checks for a
109// wallclock-based time threshold (if given) and returns
110// EvalStatus::kAskMeAgainLater if not passed; otherwise, returns
111// EvalStatus::kSucceeded. Increments a counter every time it is being queried,
112// if a pointer to it is provided.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700113class DelayPolicy : public DefaultPolicy {
114 public:
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700115 DelayPolicy(int sleep_secs, Time time_threshold, int* num_called_p)
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700116 : sleep_secs_(sleep_secs), time_threshold_(time_threshold),
117 num_called_p_(num_called_p) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -0700118 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
119 string* error,
120 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700121 if (num_called_p_)
122 (*num_called_p_)++;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700123
124 // Sleep for a predetermined amount of time.
125 if (sleep_secs_ > 0)
126 sleep(sleep_secs_);
127
128 // Check for a time threshold. This can be used to ensure that the policy
129 // has some non-constant dependency.
130 if (time_threshold_ < Time::Max() &&
131 ec->IsWallclockTimeGreaterThan(time_threshold_))
132 return EvalStatus::kSucceeded;
133
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700134 return EvalStatus::kAskMeAgainLater;
135 }
136
137 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700138 string PolicyName() const override { return "DelayPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700139
140 private:
141 int sleep_secs_;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700142 Time time_threshold_;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700143 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800144};
145
Alex Deymo7b948f02014-03-10 17:01:10 -0700146// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
147// of EvalStatus and T instances. This allows to create a callback that keeps
148// track of when it is called and the arguments passed to it, to be used with
Alex Deymo63784a52014-05-28 10:46:14 -0700149// the UpdateManager::AsyncPolicyRequest().
Alex Deymo7b948f02014-03-10 17:01:10 -0700150template<typename T>
151static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
152 EvalStatus status, const T& result) {
153 acc->push_back(std::make_pair(status, result));
154}
155
Alex Deymo0d11c602014-04-23 20:12:20 -0700156// Tests that policy requests are completed successfully. It is important that
157// this tests cover all policy requests as defined in Policy.
Alex Deymo63784a52014-05-28 10:46:14 -0700158TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700159 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700160 EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest(
Alex Deymo0d11c602014-04-23 20:12:20 -0700161 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -0800162}
163
Alex Deymo63784a52014-05-28 10:46:14 -0700164TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700165 UpdateState update_state = UpdateState();
166 update_state.is_interactive = true;
167 update_state.is_delta_payload = false;
168 update_state.first_seen = FixedTime();
169 update_state.num_checks = 1;
170 update_state.num_failures = 0;
171 update_state.failures_last_updated = Time();
172 update_state.download_urls = vector<string>{"http://fake/url/"};
173 update_state.download_errors_max = 10;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700174 update_state.p2p_downloading_disabled = false;
175 update_state.p2p_sharing_disabled = false;
Gilad Arnold349ac832014-10-06 14:20:28 -0700176 update_state.p2p_num_attempts = 0;
177 update_state.p2p_first_attempted = Time();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700178 update_state.last_download_url_idx = -1;
179 update_state.last_download_url_num_errors = 0;
180 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
181 update_state.backoff_expiry = Time();
182 update_state.is_backoff_disabled = false;
183 update_state.scatter_wait_period = TimeDelta::FromSeconds(15);
184 update_state.scatter_check_threshold = 4;
185 update_state.scatter_wait_period_max = TimeDelta::FromSeconds(60);
186 update_state.scatter_check_threshold_min = 2;
187 update_state.scatter_check_threshold_max = 8;
188
Gilad Arnold42f253b2014-06-25 12:39:17 -0700189 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700190 EXPECT_EQ(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700191 umut_->PolicyRequest(&Policy::UpdateCanStart, &result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700192 update_state));
193}
194
Alex Deymo63784a52014-05-28 10:46:14 -0700195TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
196 umut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800197
198 // Tests that the DefaultPolicy instance is called when the method fails,
199 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -0700200 UpdateCheckParams result;
201 result.updates_enabled = false;
Alex Deymo63784a52014-05-28 10:46:14 -0700202 EvalStatus status = umut_->PolicyRequest(
Alex Deymo680d0222014-04-24 21:00:08 -0700203 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700204 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700205 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800206}
207
Gilad Arnold897b5e52014-05-21 09:37:18 -0700208// This test only applies to debug builds where DCHECK is enabled.
209#if DCHECK_IS_ON
210TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
211 // The update manager should die (DCHECK) if a policy called synchronously
212 // returns a kAskMeAgainLater value.
Alex Deymo0d11c602014-04-23 20:12:20 -0700213 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700214 umut_->set_policy(new LazyPolicy());
Gilad Arnold897b5e52014-05-21 09:37:18 -0700215 EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
Alex Deymoc705cc82014-02-19 11:15:00 -0800216}
Gilad Arnold897b5e52014-05-21 09:37:18 -0700217#endif // DCHECK_IS_ON
Alex Deymoc705cc82014-02-19 11:15:00 -0800218
Alex Deymo63784a52014-05-28 10:46:14 -0700219TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700220 // To avoid differences in code execution order between an AsyncPolicyRequest
221 // call on a policy that returns AskMeAgainLater the first time and one that
222 // succeeds the first time, we ensure that the passed callback is called from
223 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo63784a52014-05-28 10:46:14 -0700224 umut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700225
Alex Deymo0d11c602014-04-23 20:12:20 -0700226 vector<pair<EvalStatus, UpdateCheckParams>> calls;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700227 Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind(
228 AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700229
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700230 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700231 // The callback should wait until we run the main loop for it to be executed.
232 EXPECT_EQ(0, calls.size());
Alex Deymo10875d92014-11-10 21:52:57 -0800233 RunGMainLoopMaxIterations(100);
Alex Deymo7b948f02014-03-10 17:01:10 -0700234 EXPECT_EQ(1, calls.size());
235}
236
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700237TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700238 // Set up an async policy call to return immediately, then wait a little and
239 // ensure that the timeout event does not fire.
240 int num_called = 0;
241 umut_->set_policy(new FailingPolicy(&num_called));
242
243 vector<pair<EvalStatus, UpdateCheckParams>> calls;
244 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
245 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
246
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700247 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700248 // Run the main loop, ensure that policy was attempted once before deferring
249 // to the default.
Alex Deymo10875d92014-11-10 21:52:57 -0800250 RunGMainLoopMaxIterations(100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700251 EXPECT_EQ(1, num_called);
252 ASSERT_EQ(1, calls.size());
253 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
254 // Wait for the timeout to expire, run the main loop again, ensure that
255 // nothing happened.
256 sleep(2);
Alex Deymo10875d92014-11-10 21:52:57 -0800257 RunGMainLoopMaxIterations(10);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700258 EXPECT_EQ(1, num_called);
259 EXPECT_EQ(1, calls.size());
260}
261
262TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) {
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700263 // Set up an async policy call to exceed its expiration timeout, make sure
264 // that the default policy was not used (no callback) and that evaluation is
265 // reattempted.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700266 int num_called = 0;
267 umut_->set_policy(new DelayPolicy(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700268 0, fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(3),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700269 &num_called));
270
271 vector<pair<EvalStatus, UpdateCheckParams>> calls;
272 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
273 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
274
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700275 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700276 // Run the main loop, ensure that policy was attempted once but the callback
277 // was not invoked.
Alex Deymo10875d92014-11-10 21:52:57 -0800278 RunGMainLoopMaxIterations(100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700279 EXPECT_EQ(1, num_called);
280 EXPECT_EQ(0, calls.size());
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700281 // Wait for the expiration timeout to expire, run the main loop again,
282 // ensure that reevaluation occurred but callback was not invoked (i.e.
283 // default policy was not consulted).
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700284 sleep(2);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700285 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
286 TimeDelta::FromSeconds(2));
Alex Deymo10875d92014-11-10 21:52:57 -0800287 RunGMainLoopMaxIterations(10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700288 EXPECT_EQ(2, num_called);
289 EXPECT_EQ(0, calls.size());
290 // Wait for reevaluation due to delay to happen, ensure that it occurs and
291 // that the callback is invoked.
292 sleep(2);
293 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
294 TimeDelta::FromSeconds(2));
Alex Deymo10875d92014-11-10 21:52:57 -0800295 RunGMainLoopMaxIterations(10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700296 EXPECT_EQ(3, num_called);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700297 ASSERT_EQ(1, calls.size());
298 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700299}
300
Alex Deymo63784a52014-05-28 10:46:14 -0700301} // namespace chromeos_update_manager