blob: 1d79c5c20fdf5ef1db7c1d97a5c2860101cf67d3 [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
Alex Deymoaab50e32014-11-10 19:55:35 -08005#include "update_engine/update_manager/update_manager.h"
6
Gilad Arnoldf9f85d62014-06-19 18:07:01 -07007#include <unistd.h>
8
Alex Deymo7b948f02014-03-10 17:01:10 -07009#include <algorithm>
Ben Chan02f7c1d2014-10-18 15:18:02 -070010#include <memory>
Alex Deymo7b948f02014-03-10 17:01:10 -070011#include <string>
Gilad Arnolddc4bb262014-07-23 10:45:19 -070012#include <tuple>
Alex Deymo94c06162014-03-21 20:34:46 -070013#include <utility>
Alex Deymo7b948f02014-03-10 17:01:10 -070014#include <vector>
15
16#include <base/bind.h>
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070017#include <base/time/time.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080018#include <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070019#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080020
Alex Deymo41a75a72014-04-15 15:36:22 -070021#include "update_engine/fake_clock.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070022#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070023#include "update_engine/update_manager/default_policy.h"
24#include "update_engine/update_manager/fake_state.h"
25#include "update_engine/update_manager/mock_policy.h"
26#include "update_engine/update_manager/umtest_utils.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080027
Alex Deymo7b948f02014-03-10 17:01:10 -070028using base::Bind;
29using base::Callback;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070030using base::Time;
31using base::TimeDelta;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070032using chromeos_update_engine::ErrorCode;
Alex Deymo41a75a72014-04-15 15:36:22 -070033using chromeos_update_engine::FakeClock;
Alex Deymo10875d92014-11-10 21:52:57 -080034using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
Alex Deymo7b948f02014-03-10 17:01:10 -070035using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080036using std::string;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070037using std::tuple;
Ben Chan02f7c1d2014-10-18 15:18:02 -070038using std::unique_ptr;
Alex Deymo7b948f02014-03-10 17:01:10 -070039using std::vector;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080040
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070041namespace {
42
43// Generates a fixed timestamp for use in faking the current time.
44Time FixedTime() {
45 Time::Exploded now_exp;
46 now_exp.year = 2014;
47 now_exp.month = 3;
48 now_exp.day_of_week = 2;
49 now_exp.day_of_month = 18;
50 now_exp.hour = 8;
51 now_exp.minute = 5;
52 now_exp.second = 33;
53 now_exp.millisecond = 675;
54 return Time::FromLocalExploded(now_exp);
55}
56
57} // namespace
58
Alex Deymo63784a52014-05-28 10:46:14 -070059namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080060
Alex Deymo63784a52014-05-28 10:46:14 -070061class UmUpdateManagerTest : public ::testing::Test {
Alex Deymoc705cc82014-02-19 11:15:00 -080062 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -070063 void SetUp() override {
Alex Deymo42c30c32014-04-24 18:41:18 -070064 fake_state_ = new FakeState();
Gilad Arnoldb2271992014-06-19 12:35:24 -070065 umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070066 TimeDelta::FromSeconds(1), fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080067 }
68
Alex Deymo63784a52014-05-28 10:46:14 -070069 FakeState* fake_state_; // Owned by the umut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070070 FakeClock fake_clock_;
Ben Chan02f7c1d2014-10-18 15:18:02 -070071 unique_ptr<UpdateManager> umut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080072};
73
74// The FailingPolicy implements a single method and make it always fail. This
75// class extends the DefaultPolicy class to allow extensions of the Policy
76// class without extending nor changing this test.
77class FailingPolicy : public DefaultPolicy {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070078 public:
79 explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {}
80 FailingPolicy() : FailingPolicy(nullptr) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -070081 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
82 string* error,
83 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070084 if (num_called_p_)
85 (*num_called_p_)++;
Alex Deymoc705cc82014-02-19 11:15:00 -080086 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070087 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080088 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070089
90 protected:
Alex Deymof329b932014-10-30 01:37:48 -070091 string PolicyName() const override { return "FailingPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070092
93 private:
94 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -080095};
96
Alex Deymo7b948f02014-03-10 17:01:10 -070097// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080098class LazyPolicy : public DefaultPolicy {
Alex Vakulenko157fe302014-08-11 15:59:58 -070099 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
100 string* error,
101 UpdateCheckParams* result) const override {
Alex Deymoe636c3c2014-03-11 19:02:08 -0700102 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -0800103 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700104
105 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700106 string PolicyName() const override { return "LazyPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700107};
108
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700109// A policy that sleeps for a predetermined amount of time, then checks for a
110// wallclock-based time threshold (if given) and returns
111// EvalStatus::kAskMeAgainLater if not passed; otherwise, returns
112// EvalStatus::kSucceeded. Increments a counter every time it is being queried,
113// if a pointer to it is provided.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700114class DelayPolicy : public DefaultPolicy {
115 public:
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700116 DelayPolicy(int sleep_secs, Time time_threshold, int* num_called_p)
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700117 : sleep_secs_(sleep_secs), time_threshold_(time_threshold),
118 num_called_p_(num_called_p) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -0700119 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
120 string* error,
121 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700122 if (num_called_p_)
123 (*num_called_p_)++;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700124
125 // Sleep for a predetermined amount of time.
126 if (sleep_secs_ > 0)
127 sleep(sleep_secs_);
128
129 // Check for a time threshold. This can be used to ensure that the policy
130 // has some non-constant dependency.
131 if (time_threshold_ < Time::Max() &&
132 ec->IsWallclockTimeGreaterThan(time_threshold_))
133 return EvalStatus::kSucceeded;
134
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700135 return EvalStatus::kAskMeAgainLater;
136 }
137
138 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700139 string PolicyName() const override { return "DelayPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700140
141 private:
142 int sleep_secs_;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700143 Time time_threshold_;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700144 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800145};
146
Alex Deymo7b948f02014-03-10 17:01:10 -0700147// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
148// of EvalStatus and T instances. This allows to create a callback that keeps
149// track of when it is called and the arguments passed to it, to be used with
Alex Deymo63784a52014-05-28 10:46:14 -0700150// the UpdateManager::AsyncPolicyRequest().
Alex Deymo7b948f02014-03-10 17:01:10 -0700151template<typename T>
152static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
153 EvalStatus status, const T& result) {
154 acc->push_back(std::make_pair(status, result));
155}
156
Alex Deymo0d11c602014-04-23 20:12:20 -0700157// Tests that policy requests are completed successfully. It is important that
158// this tests cover all policy requests as defined in Policy.
Alex Deymo63784a52014-05-28 10:46:14 -0700159TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700160 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700161 EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest(
Alex Deymo0d11c602014-04-23 20:12:20 -0700162 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -0800163}
164
Alex Deymo63784a52014-05-28 10:46:14 -0700165TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700166 UpdateState update_state = UpdateState();
167 update_state.is_interactive = true;
168 update_state.is_delta_payload = false;
169 update_state.first_seen = FixedTime();
170 update_state.num_checks = 1;
171 update_state.num_failures = 0;
172 update_state.failures_last_updated = Time();
173 update_state.download_urls = vector<string>{"http://fake/url/"};
174 update_state.download_errors_max = 10;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700175 update_state.p2p_downloading_disabled = false;
176 update_state.p2p_sharing_disabled = false;
Gilad Arnold349ac832014-10-06 14:20:28 -0700177 update_state.p2p_num_attempts = 0;
178 update_state.p2p_first_attempted = Time();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700179 update_state.last_download_url_idx = -1;
180 update_state.last_download_url_num_errors = 0;
181 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
182 update_state.backoff_expiry = Time();
183 update_state.is_backoff_disabled = false;
184 update_state.scatter_wait_period = TimeDelta::FromSeconds(15);
185 update_state.scatter_check_threshold = 4;
186 update_state.scatter_wait_period_max = TimeDelta::FromSeconds(60);
187 update_state.scatter_check_threshold_min = 2;
188 update_state.scatter_check_threshold_max = 8;
189
Gilad Arnold42f253b2014-06-25 12:39:17 -0700190 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700191 EXPECT_EQ(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700192 umut_->PolicyRequest(&Policy::UpdateCanStart, &result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700193 update_state));
194}
195
Alex Deymo63784a52014-05-28 10:46:14 -0700196TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
197 umut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800198
199 // Tests that the DefaultPolicy instance is called when the method fails,
200 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -0700201 UpdateCheckParams result;
202 result.updates_enabled = false;
Alex Deymo63784a52014-05-28 10:46:14 -0700203 EvalStatus status = umut_->PolicyRequest(
Alex Deymo680d0222014-04-24 21:00:08 -0700204 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700205 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700206 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800207}
208
Gilad Arnold897b5e52014-05-21 09:37:18 -0700209// This test only applies to debug builds where DCHECK is enabled.
210#if DCHECK_IS_ON
211TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
212 // The update manager should die (DCHECK) if a policy called synchronously
213 // returns a kAskMeAgainLater value.
Alex Deymo0d11c602014-04-23 20:12:20 -0700214 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700215 umut_->set_policy(new LazyPolicy());
Gilad Arnold897b5e52014-05-21 09:37:18 -0700216 EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
Alex Deymoc705cc82014-02-19 11:15:00 -0800217}
Gilad Arnold897b5e52014-05-21 09:37:18 -0700218#endif // DCHECK_IS_ON
Alex Deymoc705cc82014-02-19 11:15:00 -0800219
Alex Deymo63784a52014-05-28 10:46:14 -0700220TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700221 // To avoid differences in code execution order between an AsyncPolicyRequest
222 // call on a policy that returns AskMeAgainLater the first time and one that
223 // succeeds the first time, we ensure that the passed callback is called from
224 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo63784a52014-05-28 10:46:14 -0700225 umut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700226
Alex Deymo0d11c602014-04-23 20:12:20 -0700227 vector<pair<EvalStatus, UpdateCheckParams>> calls;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700228 Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind(
229 AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700230
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700231 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700232 // The callback should wait until we run the main loop for it to be executed.
233 EXPECT_EQ(0, calls.size());
Alex Deymo10875d92014-11-10 21:52:57 -0800234 RunGMainLoopMaxIterations(100);
Alex Deymo7b948f02014-03-10 17:01:10 -0700235 EXPECT_EQ(1, calls.size());
236}
237
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700238TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700239 // Set up an async policy call to return immediately, then wait a little and
240 // ensure that the timeout event does not fire.
241 int num_called = 0;
242 umut_->set_policy(new FailingPolicy(&num_called));
243
244 vector<pair<EvalStatus, UpdateCheckParams>> calls;
245 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
246 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
247
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700248 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700249 // Run the main loop, ensure that policy was attempted once before deferring
250 // to the default.
Alex Deymo10875d92014-11-10 21:52:57 -0800251 RunGMainLoopMaxIterations(100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700252 EXPECT_EQ(1, num_called);
253 ASSERT_EQ(1, calls.size());
254 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
255 // Wait for the timeout to expire, run the main loop again, ensure that
256 // nothing happened.
257 sleep(2);
Alex Deymo10875d92014-11-10 21:52:57 -0800258 RunGMainLoopMaxIterations(10);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700259 EXPECT_EQ(1, num_called);
260 EXPECT_EQ(1, calls.size());
261}
262
263TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) {
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700264 // Set up an async policy call to exceed its expiration timeout, make sure
265 // that the default policy was not used (no callback) and that evaluation is
266 // reattempted.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700267 int num_called = 0;
268 umut_->set_policy(new DelayPolicy(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700269 0, fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(3),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700270 &num_called));
271
272 vector<pair<EvalStatus, UpdateCheckParams>> calls;
273 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
274 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
275
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700276 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700277 // Run the main loop, ensure that policy was attempted once but the callback
278 // was not invoked.
Alex Deymo10875d92014-11-10 21:52:57 -0800279 RunGMainLoopMaxIterations(100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700280 EXPECT_EQ(1, num_called);
281 EXPECT_EQ(0, calls.size());
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700282 // Wait for the expiration timeout to expire, run the main loop again,
283 // ensure that reevaluation occurred but callback was not invoked (i.e.
284 // default policy was not consulted).
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700285 sleep(2);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700286 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
287 TimeDelta::FromSeconds(2));
Alex Deymo10875d92014-11-10 21:52:57 -0800288 RunGMainLoopMaxIterations(10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700289 EXPECT_EQ(2, num_called);
290 EXPECT_EQ(0, calls.size());
291 // Wait for reevaluation due to delay to happen, ensure that it occurs and
292 // that the callback is invoked.
293 sleep(2);
294 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
295 TimeDelta::FromSeconds(2));
Alex Deymo10875d92014-11-10 21:52:57 -0800296 RunGMainLoopMaxIterations(10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700297 EXPECT_EQ(3, num_called);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700298 ASSERT_EQ(1, calls.size());
299 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700300}
301
Alex Deymo63784a52014-05-28 10:46:14 -0700302} // namespace chromeos_update_manager