blob: 32fd5acdf15832dbf02303cbdbd652baf49018bf [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>
8#include <string>
Alex Deymo94c06162014-03-21 20:34:46 -07009#include <utility>
Alex Deymo7b948f02014-03-10 17:01:10 -070010#include <vector>
11
12#include <base/bind.h>
Alex Deymo680d0222014-04-24 21:00:08 -070013#include <base/memory/scoped_ptr.h>
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070014#include <base/time/time.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080015#include <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070016#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080017
Alex Deymo41a75a72014-04-15 15:36:22 -070018#include "update_engine/fake_clock.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070019#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070020#include "update_engine/update_manager/default_policy.h"
21#include "update_engine/update_manager/fake_state.h"
22#include "update_engine/update_manager/mock_policy.h"
23#include "update_engine/update_manager/umtest_utils.h"
24#include "update_engine/update_manager/update_manager.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080025
Alex Deymo7b948f02014-03-10 17:01:10 -070026using base::Bind;
27using base::Callback;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070028using base::Time;
29using base::TimeDelta;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070030using chromeos_update_engine::ErrorCode;
Alex Deymo41a75a72014-04-15 15:36:22 -070031using chromeos_update_engine::FakeClock;
Alex Deymo7b948f02014-03-10 17:01:10 -070032using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080033using std::string;
Alex Deymo7b948f02014-03-10 17:01:10 -070034using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080035using testing::Return;
36using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080037using testing::_;
38
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070039namespace {
40
41// Generates a fixed timestamp for use in faking the current time.
42Time FixedTime() {
43 Time::Exploded now_exp;
44 now_exp.year = 2014;
45 now_exp.month = 3;
46 now_exp.day_of_week = 2;
47 now_exp.day_of_month = 18;
48 now_exp.hour = 8;
49 now_exp.minute = 5;
50 now_exp.second = 33;
51 now_exp.millisecond = 675;
52 return Time::FromLocalExploded(now_exp);
53}
54
55} // namespace
56
Alex Deymo63784a52014-05-28 10:46:14 -070057namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080058
Alex Deymo63784a52014-05-28 10:46:14 -070059class UmUpdateManagerTest : public ::testing::Test {
Alex Deymoc705cc82014-02-19 11:15:00 -080060 protected:
61 virtual void SetUp() {
Alex Deymo42c30c32014-04-24 18:41:18 -070062 fake_state_ = new FakeState();
Gilad Arnoldb2271992014-06-19 12:35:24 -070063 umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5),
64 fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080065 }
66
Alex Deymo63784a52014-05-28 10:46:14 -070067 FakeState* fake_state_; // Owned by the umut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070068 FakeClock fake_clock_;
Alex Deymo63784a52014-05-28 10:46:14 -070069 scoped_ptr<UpdateManager> umut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080070};
71
72// The FailingPolicy implements a single method and make it always fail. This
73// class extends the DefaultPolicy class to allow extensions of the Policy
74// class without extending nor changing this test.
75class FailingPolicy : public DefaultPolicy {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070076 public:
77 explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {}
78 FailingPolicy() : FailingPolicy(nullptr) {}
Alex Deymo2de23f52014-02-26 14:30:13 -080079 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080080 string* error,
Alex Deymo0d11c602014-04-23 20:12:20 -070081 UpdateCheckParams* result) const {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070082 if (num_called_p_)
83 (*num_called_p_)++;
Alex Deymoc705cc82014-02-19 11:15:00 -080084 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070085 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080086 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070087
88 protected:
89 virtual std::string PolicyName() const override { return "FailingPolicy"; }
90
91 private:
92 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -080093};
94
Alex Deymo7b948f02014-03-10 17:01:10 -070095// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080096class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080097 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080098 string* error,
Alex Deymo0d11c602014-04-23 20:12:20 -070099 UpdateCheckParams* result) const {
Alex Deymoe636c3c2014-03-11 19:02:08 -0700100 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -0800101 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700102
103 protected:
104 virtual std::string PolicyName() const override { return "LazyPolicy"; }
105};
106
107// A policy that sleeps and returns EvalStatus::kAskMeAgainlater. Will check
108// that time is greater than a given threshold (if non-zero). Increments a
Alex Vakulenko072359c2014-07-18 11:41:07 -0700109// counter every time it is being queried, if a pointer to it is provided.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700110class DelayPolicy : public DefaultPolicy {
111 public:
112 DelayPolicy(int sleep_secs, base::Time time_threshold, int* num_called_p)
113 : sleep_secs_(sleep_secs), time_threshold_(time_threshold),
114 num_called_p_(num_called_p) {}
115 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
116 string* error,
117 UpdateCheckParams* result) const {
118 if (num_called_p_)
119 (*num_called_p_)++;
120 sleep(sleep_secs_);
121 // We check for a time threshold to ensure that the policy has some
122 // non-constant dependency. The threshold is far enough in the future to
123 // ensure that it does not fire immediately.
124 if (time_threshold_ < base::Time::Max())
Gilad Arnolda65fced2014-07-23 09:01:31 -0700125 ec->IsWallclockTimeGreaterThan(time_threshold_);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700126 return EvalStatus::kAskMeAgainLater;
127 }
128
129 protected:
130 virtual std::string PolicyName() const override { return "DelayPolicy"; }
131
132 private:
133 int sleep_secs_;
134 base::Time time_threshold_;
135 int* num_called_p_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800136};
137
Alex Deymo7b948f02014-03-10 17:01:10 -0700138// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
139// of EvalStatus and T instances. This allows to create a callback that keeps
140// track of when it is called and the arguments passed to it, to be used with
Alex Deymo63784a52014-05-28 10:46:14 -0700141// the UpdateManager::AsyncPolicyRequest().
Alex Deymo7b948f02014-03-10 17:01:10 -0700142template<typename T>
143static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
144 EvalStatus status, const T& result) {
145 acc->push_back(std::make_pair(status, result));
146}
147
Alex Deymo0d11c602014-04-23 20:12:20 -0700148// Tests that policy requests are completed successfully. It is important that
149// this tests cover all policy requests as defined in Policy.
Alex Deymo63784a52014-05-28 10:46:14 -0700150TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700151 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700152 EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest(
Alex Deymo0d11c602014-04-23 20:12:20 -0700153 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -0800154}
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700157 const UpdateState update_state = {
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700158 FixedTime(), 1,
159 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
160 TimeDelta::FromSeconds(15), TimeDelta::FromSeconds(60),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700161 4, 2, 8
162 };
Gilad Arnold42f253b2014-06-25 12:39:17 -0700163 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700164 EXPECT_EQ(EvalStatus::kSucceeded,
Alex Deymo63784a52014-05-28 10:46:14 -0700165 umut_->PolicyRequest(&Policy::UpdateCanStart, &result, true,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700166 update_state));
167}
168
Alex Deymo63784a52014-05-28 10:46:14 -0700169TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
170 umut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800171
172 // Tests that the DefaultPolicy instance is called when the method fails,
173 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -0700174 UpdateCheckParams result;
175 result.updates_enabled = false;
Alex Deymo63784a52014-05-28 10:46:14 -0700176 EvalStatus status = umut_->PolicyRequest(
Alex Deymo680d0222014-04-24 21:00:08 -0700177 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700178 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700179 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800180}
181
Gilad Arnold897b5e52014-05-21 09:37:18 -0700182// This test only applies to debug builds where DCHECK is enabled.
183#if DCHECK_IS_ON
184TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
185 // The update manager should die (DCHECK) if a policy called synchronously
186 // returns a kAskMeAgainLater value.
Alex Deymo0d11c602014-04-23 20:12:20 -0700187 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700188 umut_->set_policy(new LazyPolicy());
Gilad Arnold897b5e52014-05-21 09:37:18 -0700189 EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
Alex Deymoc705cc82014-02-19 11:15:00 -0800190}
Gilad Arnold897b5e52014-05-21 09:37:18 -0700191#endif // DCHECK_IS_ON
Alex Deymoc705cc82014-02-19 11:15:00 -0800192
Alex Deymo63784a52014-05-28 10:46:14 -0700193TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700194 // To avoid differences in code execution order between an AsyncPolicyRequest
195 // call on a policy that returns AskMeAgainLater the first time and one that
196 // succeeds the first time, we ensure that the passed callback is called from
197 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo63784a52014-05-28 10:46:14 -0700198 umut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700199
Alex Deymo0d11c602014-04-23 20:12:20 -0700200 vector<pair<EvalStatus, UpdateCheckParams>> calls;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700201 Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind(
202 AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700203
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700204 umut_->AsyncPolicyRequest(callback, base::TimeDelta::FromSeconds(5),
205 &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700206 // The callback should wait until we run the main loop for it to be executed.
207 EXPECT_EQ(0, calls.size());
208 chromeos_update_engine::RunGMainLoopMaxIterations(100);
209 EXPECT_EQ(1, calls.size());
210}
211
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700212TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDoesNotTimeOut) {
213 // Set up an async policy call to return immediately, then wait a little and
214 // ensure that the timeout event does not fire.
215 int num_called = 0;
216 umut_->set_policy(new FailingPolicy(&num_called));
217
218 vector<pair<EvalStatus, UpdateCheckParams>> calls;
219 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
220 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
221
222 umut_->AsyncPolicyRequest(callback, base::TimeDelta::FromSeconds(1),
223 &Policy::UpdateCheckAllowed);
224 // Run the main loop, ensure that policy was attempted once before deferring
225 // to the default.
226 chromeos_update_engine::RunGMainLoopMaxIterations(100);
227 EXPECT_EQ(1, num_called);
228 ASSERT_EQ(1, calls.size());
229 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
230 // Wait for the timeout to expire, run the main loop again, ensure that
231 // nothing happened.
232 sleep(2);
233 chromeos_update_engine::RunGMainLoopMaxIterations(10);
234 EXPECT_EQ(1, num_called);
235 EXPECT_EQ(1, calls.size());
236}
237
238TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) {
239 // Set up an async policy call to exceed its overall execution time, and make
240 // sure that it is aborted. Also ensure that the async call is not reattempted
241 // after the timeout fires by waiting pas the time threshold for reevaluation.
242 int num_called = 0;
243 umut_->set_policy(new DelayPolicy(
244 2, fake_clock_.GetWallclockTime() + base::TimeDelta::FromSeconds(3),
245 &num_called));
246
247 vector<pair<EvalStatus, UpdateCheckParams>> calls;
248 Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
249 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
250
251 umut_->AsyncPolicyRequest(callback, base::TimeDelta::FromSeconds(1),
252 &Policy::UpdateCheckAllowed);
253 // Run the main loop, ensure that policy was attempted once but the callback
254 // was not invoked.
255 chromeos_update_engine::RunGMainLoopMaxIterations(100);
256 EXPECT_EQ(1, num_called);
257 EXPECT_EQ(0, calls.size());
258 // Wait for the time threshold to be satisfied, run the main loop again,
259 // ensure that reevaluation was not attempted but the callback invoked.
260 sleep(2);
261 chromeos_update_engine::RunGMainLoopMaxIterations(10);
262 EXPECT_EQ(1, num_called);
263 ASSERT_EQ(1, calls.size());
264 EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
265 EXPECT_EQ(true, calls[0].second.updates_enabled);
266}
267
Alex Deymo63784a52014-05-28 10:46:14 -0700268} // namespace chromeos_update_manager