blob: b263fc8f0cb523cef1baf125063e42ac87e34748 [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 Deymo7b948f02014-03-10 17:01:10 -070033using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080034using std::string;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070035using std::tuple;
Ben Chan02f7c1d2014-10-18 15:18:02 -070036using std::unique_ptr;
Alex Deymo7b948f02014-03-10 17:01:10 -070037using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080038using testing::Return;
39using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080040using testing::_;
41
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070042namespace {
43
44// Generates a fixed timestamp for use in faking the current time.
45Time FixedTime() {
46 Time::Exploded now_exp;
47 now_exp.year = 2014;
48 now_exp.month = 3;
49 now_exp.day_of_week = 2;
50 now_exp.day_of_month = 18;
51 now_exp.hour = 8;
52 now_exp.minute = 5;
53 now_exp.second = 33;
54 now_exp.millisecond = 675;
55 return Time::FromLocalExploded(now_exp);
56}
57
58} // namespace
59
Alex Deymo63784a52014-05-28 10:46:14 -070060namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080061
Alex Deymo63784a52014-05-28 10:46:14 -070062class UmUpdateManagerTest : public ::testing::Test {
Alex Deymoc705cc82014-02-19 11:15:00 -080063 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -070064 void SetUp() override {
Alex Deymo42c30c32014-04-24 18:41:18 -070065 fake_state_ = new FakeState();
Gilad Arnoldb2271992014-06-19 12:35:24 -070066 umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070067 TimeDelta::FromSeconds(1), fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080068 }
69
Alex Deymo63784a52014-05-28 10:46:14 -070070 FakeState* fake_state_; // Owned by the umut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070071 FakeClock fake_clock_;
Ben Chan02f7c1d2014-10-18 15:18:02 -070072 unique_ptr<UpdateManager> umut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080073};
74
75// The FailingPolicy implements a single method and make it always fail. This
76// class extends the DefaultPolicy class to allow extensions of the Policy
77// class without extending nor changing this test.
78class FailingPolicy : public DefaultPolicy {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070079 public:
80 explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {}
81 FailingPolicy() : FailingPolicy(nullptr) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -070082 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
83 string* error,
84 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070085 if (num_called_p_)
86 (*num_called_p_)++;
Alex Deymoc705cc82014-02-19 11:15:00 -080087 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070088 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080089 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070090
91 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -070092 std::string PolicyName() const override { return "FailingPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070093
94 private:
95 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -080096};
97
Alex Deymo7b948f02014-03-10 17:01:10 -070098// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080099class LazyPolicy : public DefaultPolicy {
Alex Vakulenko157fe302014-08-11 15:59:58 -0700100 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
101 string* error,
102 UpdateCheckParams* result) const override {
Alex Deymoe636c3c2014-03-11 19:02:08 -0700103 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -0800104 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700105
106 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700107 std::string PolicyName() const override { return "LazyPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700108};
109
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700110// A policy that sleeps for a predetermined amount of time, then checks for a
111// wallclock-based time threshold (if given) and returns
112// EvalStatus::kAskMeAgainLater if not passed; otherwise, returns
113// EvalStatus::kSucceeded. Increments a counter every time it is being queried,
114// if a pointer to it is provided.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700115class DelayPolicy : public DefaultPolicy {
116 public:
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700117 DelayPolicy(int sleep_secs, Time time_threshold, int* num_called_p)
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700118 : sleep_secs_(sleep_secs), time_threshold_(time_threshold),
119 num_called_p_(num_called_p) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -0700120 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
121 string* error,
122 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700123 if (num_called_p_)
124 (*num_called_p_)++;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700125
126 // Sleep for a predetermined amount of time.
127 if (sleep_secs_ > 0)
128 sleep(sleep_secs_);
129
130 // Check for a time threshold. This can be used to ensure that the policy
131 // has some non-constant dependency.
132 if (time_threshold_ < Time::Max() &&
133 ec->IsWallclockTimeGreaterThan(time_threshold_))
134 return EvalStatus::kSucceeded;
135
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700136 return EvalStatus::kAskMeAgainLater;
137 }
138
139 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700140 std::string PolicyName() const override { return "DelayPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700141
142 private:
143 int sleep_secs_;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700144 Time time_threshold_;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700145 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800146};
147
Alex Deymo7b948f02014-03-10 17:01:10 -0700148// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
149// of EvalStatus and T instances. This allows to create a callback that keeps
150// track of when it is called and the arguments passed to it, to be used with
Alex Deymo63784a52014-05-28 10:46:14 -0700151// the UpdateManager::AsyncPolicyRequest().
Alex Deymo7b948f02014-03-10 17:01:10 -0700152template<typename T>
153static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
154 EvalStatus status, const T& result) {
155 acc->push_back(std::make_pair(status, result));
156}
157
Alex Deymo0d11c602014-04-23 20:12:20 -0700158// Tests that policy requests are completed successfully. It is important that
159// this tests cover all policy requests as defined in Policy.
Alex Deymo63784a52014-05-28 10:46:14 -0700160TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700161 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700162 EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest(
Alex Deymo0d11c602014-04-23 20:12:20 -0700163 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -0800164}
165
Alex Deymo63784a52014-05-28 10:46:14 -0700166TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700167 UpdateState update_state = UpdateState();
168 update_state.is_interactive = true;
169 update_state.is_delta_payload = false;
170 update_state.first_seen = FixedTime();
171 update_state.num_checks = 1;
172 update_state.num_failures = 0;
173 update_state.failures_last_updated = Time();
174 update_state.download_urls = vector<string>{"http://fake/url/"};
175 update_state.download_errors_max = 10;
176 update_state.last_download_url_idx = -1;
177 update_state.last_download_url_num_errors = 0;
178 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
179 update_state.backoff_expiry = Time();
180 update_state.is_backoff_disabled = false;
181 update_state.scatter_wait_period = TimeDelta::FromSeconds(15);
182 update_state.scatter_check_threshold = 4;
183 update_state.scatter_wait_period_max = TimeDelta::FromSeconds(60);
184 update_state.scatter_check_threshold_min = 2;
185 update_state.scatter_check_threshold_max = 8;
186
Gilad Arnold42f253b2014-06-25 12:39:17 -0700187 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700188 EXPECT_EQ(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700189 umut_->PolicyRequest(&Policy::UpdateCanStart, &result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700190 update_state));
191}
192
Alex Deymo63784a52014-05-28 10:46:14 -0700193TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
194 umut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800195
196 // Tests that the DefaultPolicy instance is called when the method fails,
197 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -0700198 UpdateCheckParams result;
199 result.updates_enabled = false;
Alex Deymo63784a52014-05-28 10:46:14 -0700200 EvalStatus status = umut_->PolicyRequest(
Alex Deymo680d0222014-04-24 21:00:08 -0700201 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700202 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700203 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800204}
205
Gilad Arnold897b5e52014-05-21 09:37:18 -0700206// This test only applies to debug builds where DCHECK is enabled.
207#if DCHECK_IS_ON
208TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
209 // The update manager should die (DCHECK) if a policy called synchronously
210 // returns a kAskMeAgainLater value.
Alex Deymo0d11c602014-04-23 20:12:20 -0700211 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700212 umut_->set_policy(new LazyPolicy());
Gilad Arnold897b5e52014-05-21 09:37:18 -0700213 EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
Alex Deymoc705cc82014-02-19 11:15:00 -0800214}
Gilad Arnold897b5e52014-05-21 09:37:18 -0700215#endif // DCHECK_IS_ON
Alex Deymoc705cc82014-02-19 11:15:00 -0800216
Alex Deymo63784a52014-05-28 10:46:14 -0700217TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700218 // To avoid differences in code execution order between an AsyncPolicyRequest
219 // call on a policy that returns AskMeAgainLater the first time and one that
220 // succeeds the first time, we ensure that the passed callback is called from
221 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo63784a52014-05-28 10:46:14 -0700222 umut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700223
Alex Deymo0d11c602014-04-23 20:12:20 -0700224 vector<pair<EvalStatus, UpdateCheckParams>> calls;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700225 Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind(
226 AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700227
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700228 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700229 // The callback should wait until we run the main loop for it to be executed.
230 EXPECT_EQ(0, calls.size());
231 chromeos_update_engine::RunGMainLoopMaxIterations(100);
232 EXPECT_EQ(1, calls.size());
233}
234
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700235TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700236 // Set up an async policy call to return immediately, then wait a little and
237 // ensure that the timeout event does not fire.
238 int num_called = 0;
239 umut_->set_policy(new FailingPolicy(&num_called));
240
241 vector<pair<EvalStatus, UpdateCheckParams>> calls;
242 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
243 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
244
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700245 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700246 // Run the main loop, ensure that policy was attempted once before deferring
247 // to the default.
248 chromeos_update_engine::RunGMainLoopMaxIterations(100);
249 EXPECT_EQ(1, num_called);
250 ASSERT_EQ(1, calls.size());
251 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
252 // Wait for the timeout to expire, run the main loop again, ensure that
253 // nothing happened.
254 sleep(2);
255 chromeos_update_engine::RunGMainLoopMaxIterations(10);
256 EXPECT_EQ(1, num_called);
257 EXPECT_EQ(1, calls.size());
258}
259
260TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) {
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700261 // Set up an async policy call to exceed its expiration timeout, make sure
262 // that the default policy was not used (no callback) and that evaluation is
263 // reattempted.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700264 int num_called = 0;
265 umut_->set_policy(new DelayPolicy(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700266 0, fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(3),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700267 &num_called));
268
269 vector<pair<EvalStatus, UpdateCheckParams>> calls;
270 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
271 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
272
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700273 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700274 // Run the main loop, ensure that policy was attempted once but the callback
275 // was not invoked.
276 chromeos_update_engine::RunGMainLoopMaxIterations(100);
277 EXPECT_EQ(1, num_called);
278 EXPECT_EQ(0, calls.size());
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700279 // Wait for the expiration timeout to expire, run the main loop again,
280 // ensure that reevaluation occurred but callback was not invoked (i.e.
281 // default policy was not consulted).
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700282 sleep(2);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700283 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
284 TimeDelta::FromSeconds(2));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700285 chromeos_update_engine::RunGMainLoopMaxIterations(10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700286 EXPECT_EQ(2, num_called);
287 EXPECT_EQ(0, calls.size());
288 // Wait for reevaluation due to delay to happen, ensure that it occurs and
289 // that the callback is invoked.
290 sleep(2);
291 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
292 TimeDelta::FromSeconds(2));
293 chromeos_update_engine::RunGMainLoopMaxIterations(10);
294 EXPECT_EQ(3, num_called);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700295 ASSERT_EQ(1, calls.size());
296 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700297}
298
Alex Deymo63784a52014-05-28 10:46:14 -0700299} // namespace chromeos_update_manager