blob: cf097fa4da857ce19205d6ca0c4b6ca87f712fce [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>
Alex Deymo509dd532015-06-10 14:11:05 -070017#include <base/test/simple_test_clock.h>
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070018#include <base/time/time.h>
Alex Deymo509dd532015-06-10 14:11:05 -070019#include <chromeos/message_loops/fake_message_loop.h>
20#include <chromeos/message_loops/message_loop.h>
21#include <chromeos/message_loops/message_loop_utils.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080022#include <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070023#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080024
Alex Deymo41a75a72014-04-15 15:36:22 -070025#include "update_engine/fake_clock.h"
Alex Deymo63784a52014-05-28 10:46:14 -070026#include "update_engine/update_manager/default_policy.h"
27#include "update_engine/update_manager/fake_state.h"
28#include "update_engine/update_manager/mock_policy.h"
29#include "update_engine/update_manager/umtest_utils.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080030
Alex Deymo7b948f02014-03-10 17:01:10 -070031using base::Bind;
32using base::Callback;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070033using base::Time;
34using base::TimeDelta;
Alex Deymo509dd532015-06-10 14:11:05 -070035using chromeos::MessageLoop;
36using chromeos::MessageLoopRunMaxIterations;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070037using chromeos_update_engine::ErrorCode;
Alex Deymo41a75a72014-04-15 15:36:22 -070038using chromeos_update_engine::FakeClock;
Alex Deymo7b948f02014-03-10 17:01:10 -070039using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080040using std::string;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070041using std::tuple;
Ben Chan02f7c1d2014-10-18 15:18:02 -070042using std::unique_ptr;
Alex Deymo7b948f02014-03-10 17:01:10 -070043using std::vector;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080044
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070045namespace {
46
47// Generates a fixed timestamp for use in faking the current time.
48Time FixedTime() {
49 Time::Exploded now_exp;
50 now_exp.year = 2014;
51 now_exp.month = 3;
52 now_exp.day_of_week = 2;
53 now_exp.day_of_month = 18;
54 now_exp.hour = 8;
55 now_exp.minute = 5;
56 now_exp.second = 33;
57 now_exp.millisecond = 675;
58 return Time::FromLocalExploded(now_exp);
59}
60
61} // namespace
62
Alex Deymo63784a52014-05-28 10:46:14 -070063namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080064
Alex Deymo63784a52014-05-28 10:46:14 -070065class UmUpdateManagerTest : public ::testing::Test {
Alex Deymoc705cc82014-02-19 11:15:00 -080066 protected:
Alex Vakulenko157fe302014-08-11 15:59:58 -070067 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070068 loop_.SetAsCurrent();
Alex Deymo42c30c32014-04-24 18:41:18 -070069 fake_state_ = new FakeState();
Gilad Arnoldb2271992014-06-19 12:35:24 -070070 umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070071 TimeDelta::FromSeconds(1), fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080072 }
73
Alex Deymo509dd532015-06-10 14:11:05 -070074 void TearDown() override {
75 EXPECT_FALSE(loop_.PendingTasks());
76 }
77
78 base::SimpleTestClock test_clock_;
79 chromeos::FakeMessageLoop loop_{&test_clock_};
Alex Deymo63784a52014-05-28 10:46:14 -070080 FakeState* fake_state_; // Owned by the umut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070081 FakeClock fake_clock_;
Ben Chan02f7c1d2014-10-18 15:18:02 -070082 unique_ptr<UpdateManager> umut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080083};
84
85// The FailingPolicy implements a single method and make it always fail. This
86// class extends the DefaultPolicy class to allow extensions of the Policy
87// class without extending nor changing this test.
88class FailingPolicy : public DefaultPolicy {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070089 public:
90 explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {}
91 FailingPolicy() : FailingPolicy(nullptr) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -070092 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
93 string* error,
94 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070095 if (num_called_p_)
96 (*num_called_p_)++;
Alex Deymoc705cc82014-02-19 11:15:00 -080097 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070098 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080099 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700100
101 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700102 string PolicyName() const override { return "FailingPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700103
104 private:
105 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800106};
107
Alex Deymo7b948f02014-03-10 17:01:10 -0700108// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -0800109class LazyPolicy : public DefaultPolicy {
Alex Vakulenko157fe302014-08-11 15:59:58 -0700110 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
111 string* error,
112 UpdateCheckParams* result) const override {
Alex Deymoe636c3c2014-03-11 19:02:08 -0700113 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -0800114 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700115
116 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700117 string PolicyName() const override { return "LazyPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700118};
119
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700120// A policy that sleeps for a predetermined amount of time, then checks for a
121// wallclock-based time threshold (if given) and returns
122// EvalStatus::kAskMeAgainLater if not passed; otherwise, returns
123// EvalStatus::kSucceeded. Increments a counter every time it is being queried,
124// if a pointer to it is provided.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700125class DelayPolicy : public DefaultPolicy {
126 public:
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700127 DelayPolicy(int sleep_secs, Time time_threshold, int* num_called_p)
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700128 : sleep_secs_(sleep_secs), time_threshold_(time_threshold),
129 num_called_p_(num_called_p) {}
Alex Vakulenko157fe302014-08-11 15:59:58 -0700130 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
131 string* error,
132 UpdateCheckParams* result) const override {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700133 if (num_called_p_)
134 (*num_called_p_)++;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700135
136 // Sleep for a predetermined amount of time.
137 if (sleep_secs_ > 0)
138 sleep(sleep_secs_);
139
140 // Check for a time threshold. This can be used to ensure that the policy
141 // has some non-constant dependency.
142 if (time_threshold_ < Time::Max() &&
143 ec->IsWallclockTimeGreaterThan(time_threshold_))
144 return EvalStatus::kSucceeded;
145
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700146 return EvalStatus::kAskMeAgainLater;
147 }
148
149 protected:
Alex Deymof329b932014-10-30 01:37:48 -0700150 string PolicyName() const override { return "DelayPolicy"; }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700151
152 private:
153 int sleep_secs_;
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700154 Time time_threshold_;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700155 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800156};
157
Alex Deymo7b948f02014-03-10 17:01:10 -0700158// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
159// of EvalStatus and T instances. This allows to create a callback that keeps
160// track of when it is called and the arguments passed to it, to be used with
Alex Deymo63784a52014-05-28 10:46:14 -0700161// the UpdateManager::AsyncPolicyRequest().
Alex Deymo7b948f02014-03-10 17:01:10 -0700162template<typename T>
163static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
164 EvalStatus status, const T& result) {
165 acc->push_back(std::make_pair(status, result));
166}
167
Alex Deymo0d11c602014-04-23 20:12:20 -0700168// Tests that policy requests are completed successfully. It is important that
169// this tests cover all policy requests as defined in Policy.
Alex Deymo63784a52014-05-28 10:46:14 -0700170TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700171 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700172 EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest(
Alex Deymo0d11c602014-04-23 20:12:20 -0700173 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -0800174}
175
Alex Deymo63784a52014-05-28 10:46:14 -0700176TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700177 UpdateState update_state = UpdateState();
178 update_state.is_interactive = true;
179 update_state.is_delta_payload = false;
180 update_state.first_seen = FixedTime();
181 update_state.num_checks = 1;
182 update_state.num_failures = 0;
183 update_state.failures_last_updated = Time();
184 update_state.download_urls = vector<string>{"http://fake/url/"};
185 update_state.download_errors_max = 10;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700186 update_state.p2p_downloading_disabled = false;
187 update_state.p2p_sharing_disabled = false;
Gilad Arnold349ac832014-10-06 14:20:28 -0700188 update_state.p2p_num_attempts = 0;
189 update_state.p2p_first_attempted = Time();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700190 update_state.last_download_url_idx = -1;
191 update_state.last_download_url_num_errors = 0;
192 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
193 update_state.backoff_expiry = Time();
194 update_state.is_backoff_disabled = false;
195 update_state.scatter_wait_period = TimeDelta::FromSeconds(15);
196 update_state.scatter_check_threshold = 4;
197 update_state.scatter_wait_period_max = TimeDelta::FromSeconds(60);
198 update_state.scatter_check_threshold_min = 2;
199 update_state.scatter_check_threshold_max = 8;
200
Gilad Arnold42f253b2014-06-25 12:39:17 -0700201 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700202 EXPECT_EQ(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700203 umut_->PolicyRequest(&Policy::UpdateCanStart, &result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700204 update_state));
205}
206
Alex Deymo63784a52014-05-28 10:46:14 -0700207TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
208 umut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800209
210 // Tests that the DefaultPolicy instance is called when the method fails,
211 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -0700212 UpdateCheckParams result;
213 result.updates_enabled = false;
Alex Deymo63784a52014-05-28 10:46:14 -0700214 EvalStatus status = umut_->PolicyRequest(
Alex Deymo680d0222014-04-24 21:00:08 -0700215 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700216 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700217 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800218}
219
Gilad Arnold897b5e52014-05-21 09:37:18 -0700220// This test only applies to debug builds where DCHECK is enabled.
221#if DCHECK_IS_ON
222TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
223 // The update manager should die (DCHECK) if a policy called synchronously
224 // returns a kAskMeAgainLater value.
Alex Deymo0d11c602014-04-23 20:12:20 -0700225 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700226 umut_->set_policy(new LazyPolicy());
Gilad Arnold897b5e52014-05-21 09:37:18 -0700227 EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
Alex Deymoc705cc82014-02-19 11:15:00 -0800228}
Gilad Arnold897b5e52014-05-21 09:37:18 -0700229#endif // DCHECK_IS_ON
Alex Deymoc705cc82014-02-19 11:15:00 -0800230
Alex Deymo63784a52014-05-28 10:46:14 -0700231TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700232 // To avoid differences in code execution order between an AsyncPolicyRequest
233 // call on a policy that returns AskMeAgainLater the first time and one that
234 // succeeds the first time, we ensure that the passed callback is called from
235 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo63784a52014-05-28 10:46:14 -0700236 umut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700237
Alex Deymo0d11c602014-04-23 20:12:20 -0700238 vector<pair<EvalStatus, UpdateCheckParams>> calls;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700239 Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind(
240 AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700241
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700242 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700243 // The callback should wait until we run the main loop for it to be executed.
244 EXPECT_EQ(0, calls.size());
Alex Deymo509dd532015-06-10 14:11:05 -0700245 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo7b948f02014-03-10 17:01:10 -0700246 EXPECT_EQ(1, calls.size());
247}
248
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700249TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700250 // Set up an async policy call to return immediately, then wait a little and
251 // ensure that the timeout event does not fire.
252 int num_called = 0;
253 umut_->set_policy(new FailingPolicy(&num_called));
254
255 vector<pair<EvalStatus, UpdateCheckParams>> calls;
256 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
257 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
258
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700259 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700260 // Run the main loop, ensure that policy was attempted once before deferring
261 // to the default.
Alex Deymo509dd532015-06-10 14:11:05 -0700262 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700263 EXPECT_EQ(1, num_called);
264 ASSERT_EQ(1, calls.size());
265 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
266 // Wait for the timeout to expire, run the main loop again, ensure that
267 // nothing happened.
Alex Deymo509dd532015-06-10 14:11:05 -0700268 test_clock_.Advance(TimeDelta::FromSeconds(2));
269 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700270 EXPECT_EQ(1, num_called);
271 EXPECT_EQ(1, calls.size());
272}
273
274TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) {
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700275 // Set up an async policy call to exceed its expiration timeout, make sure
276 // that the default policy was not used (no callback) and that evaluation is
277 // reattempted.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700278 int num_called = 0;
279 umut_->set_policy(new DelayPolicy(
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700280 0, fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(3),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700281 &num_called));
282
283 vector<pair<EvalStatus, UpdateCheckParams>> calls;
284 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
285 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
286
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700287 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700288 // Run the main loop, ensure that policy was attempted once but the callback
289 // was not invoked.
Alex Deymo509dd532015-06-10 14:11:05 -0700290 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700291 EXPECT_EQ(1, num_called);
292 EXPECT_EQ(0, calls.size());
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700293 // Wait for the expiration timeout to expire, run the main loop again,
294 // ensure that reevaluation occurred but callback was not invoked (i.e.
295 // default policy was not consulted).
Alex Deymo509dd532015-06-10 14:11:05 -0700296 test_clock_.Advance(TimeDelta::FromSeconds(2));
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700297 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
298 TimeDelta::FromSeconds(2));
Alex Deymo509dd532015-06-10 14:11:05 -0700299 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700300 EXPECT_EQ(2, num_called);
301 EXPECT_EQ(0, calls.size());
302 // Wait for reevaluation due to delay to happen, ensure that it occurs and
303 // that the callback is invoked.
Alex Deymo509dd532015-06-10 14:11:05 -0700304 test_clock_.Advance(TimeDelta::FromSeconds(2));
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700305 fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
306 TimeDelta::FromSeconds(2));
Alex Deymo509dd532015-06-10 14:11:05 -0700307 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700308 EXPECT_EQ(3, num_called);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700309 ASSERT_EQ(1, calls.size());
310 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700311}
312
Alex Deymo63784a52014-05-28 10:46:14 -0700313} // namespace chromeos_update_manager