blob: fa6f9dd29b77d7e28232cb1367d54cc00d3aca80 [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 Deymo7b948f02014-03-10 17:01:10 -07005#include <algorithm>
6#include <string>
Alex Deymo94c06162014-03-21 20:34:46 -07007#include <utility>
Alex Deymo7b948f02014-03-10 17:01:10 -07008#include <vector>
9
10#include <base/bind.h>
Alex Deymo680d0222014-04-24 21:00:08 -070011#include <base/memory/scoped_ptr.h>
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070012#include <base/time/time.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080013#include <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070014#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080015
Alex Deymo41a75a72014-04-15 15:36:22 -070016#include "update_engine/fake_clock.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070017#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070018#include "update_engine/update_manager/default_policy.h"
19#include "update_engine/update_manager/fake_state.h"
20#include "update_engine/update_manager/mock_policy.h"
21#include "update_engine/update_manager/umtest_utils.h"
22#include "update_engine/update_manager/update_manager.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080023
Alex Deymo7b948f02014-03-10 17:01:10 -070024using base::Bind;
25using base::Callback;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070026using base::Time;
27using base::TimeDelta;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070028using chromeos_update_engine::ErrorCode;
Alex Deymo41a75a72014-04-15 15:36:22 -070029using chromeos_update_engine::FakeClock;
Alex Deymo7b948f02014-03-10 17:01:10 -070030using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080031using std::string;
Alex Deymo7b948f02014-03-10 17:01:10 -070032using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080033using testing::Return;
34using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080035using testing::_;
36
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070037namespace {
38
39// Generates a fixed timestamp for use in faking the current time.
40Time FixedTime() {
41 Time::Exploded now_exp;
42 now_exp.year = 2014;
43 now_exp.month = 3;
44 now_exp.day_of_week = 2;
45 now_exp.day_of_month = 18;
46 now_exp.hour = 8;
47 now_exp.minute = 5;
48 now_exp.second = 33;
49 now_exp.millisecond = 675;
50 return Time::FromLocalExploded(now_exp);
51}
52
53} // namespace
54
Alex Deymo63784a52014-05-28 10:46:14 -070055namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080056
Alex Deymo63784a52014-05-28 10:46:14 -070057class UmUpdateManagerTest : public ::testing::Test {
Alex Deymoc705cc82014-02-19 11:15:00 -080058 protected:
59 virtual void SetUp() {
Alex Deymo42c30c32014-04-24 18:41:18 -070060 fake_state_ = new FakeState();
Alex Deymo63784a52014-05-28 10:46:14 -070061 umut_.reset(new UpdateManager(&fake_clock_, fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080062 }
63
Alex Deymo63784a52014-05-28 10:46:14 -070064 FakeState* fake_state_; // Owned by the umut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070065 FakeClock fake_clock_;
Alex Deymo63784a52014-05-28 10:46:14 -070066 scoped_ptr<UpdateManager> umut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080067};
68
69// The FailingPolicy implements a single method and make it always fail. This
70// class extends the DefaultPolicy class to allow extensions of the Policy
71// class without extending nor changing this test.
72class FailingPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080073 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080074 string* error,
Alex Deymo0d11c602014-04-23 20:12:20 -070075 UpdateCheckParams* result) const {
Alex Deymoc705cc82014-02-19 11:15:00 -080076 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070077 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080078 }
79};
80
Alex Deymo7b948f02014-03-10 17:01:10 -070081// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080082class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080083 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080084 string* error,
Alex Deymo0d11c602014-04-23 20:12:20 -070085 UpdateCheckParams* result) const {
Alex Deymoe636c3c2014-03-11 19:02:08 -070086 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -080087 }
88};
89
Alex Deymo7b948f02014-03-10 17:01:10 -070090// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
91// of EvalStatus and T instances. This allows to create a callback that keeps
92// track of when it is called and the arguments passed to it, to be used with
Alex Deymo63784a52014-05-28 10:46:14 -070093// the UpdateManager::AsyncPolicyRequest().
Alex Deymo7b948f02014-03-10 17:01:10 -070094template<typename T>
95static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
96 EvalStatus status, const T& result) {
97 acc->push_back(std::make_pair(status, result));
98}
99
Alex Deymo0d11c602014-04-23 20:12:20 -0700100// Tests that policy requests are completed successfully. It is important that
101// this tests cover all policy requests as defined in Policy.
Alex Deymo63784a52014-05-28 10:46:14 -0700102TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700103 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700104 EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest(
Alex Deymo0d11c602014-04-23 20:12:20 -0700105 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -0800106}
107
Alex Deymo63784a52014-05-28 10:46:14 -0700108TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700109 const UpdateState update_state = {
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700110 FixedTime(), 1,
111 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
112 TimeDelta::FromSeconds(15), TimeDelta::FromSeconds(60),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700113 4, 2, 8
114 };
115 UpdateCanStartResult result;
116 EXPECT_EQ(EvalStatus::kSucceeded,
Alex Deymo63784a52014-05-28 10:46:14 -0700117 umut_->PolicyRequest(&Policy::UpdateCanStart, &result, true,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700118 update_state));
119}
120
Alex Deymo63784a52014-05-28 10:46:14 -0700121TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
122 umut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800123
124 // Tests that the DefaultPolicy instance is called when the method fails,
125 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -0700126 UpdateCheckParams result;
127 result.updates_enabled = false;
Alex Deymo63784a52014-05-28 10:46:14 -0700128 EvalStatus status = umut_->PolicyRequest(
Alex Deymo680d0222014-04-24 21:00:08 -0700129 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700130 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700131 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800132}
133
Gilad Arnold897b5e52014-05-21 09:37:18 -0700134// This test only applies to debug builds where DCHECK is enabled.
135#if DCHECK_IS_ON
136TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
137 // The update manager should die (DCHECK) if a policy called synchronously
138 // returns a kAskMeAgainLater value.
Alex Deymo0d11c602014-04-23 20:12:20 -0700139 UpdateCheckParams result;
Alex Deymo63784a52014-05-28 10:46:14 -0700140 umut_->set_policy(new LazyPolicy());
Gilad Arnold897b5e52014-05-21 09:37:18 -0700141 EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
Alex Deymoc705cc82014-02-19 11:15:00 -0800142}
Gilad Arnold897b5e52014-05-21 09:37:18 -0700143#endif // DCHECK_IS_ON
Alex Deymoc705cc82014-02-19 11:15:00 -0800144
Alex Deymo63784a52014-05-28 10:46:14 -0700145TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700146 // To avoid differences in code execution order between an AsyncPolicyRequest
147 // call on a policy that returns AskMeAgainLater the first time and one that
148 // succeeds the first time, we ensure that the passed callback is called from
149 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo63784a52014-05-28 10:46:14 -0700150 umut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700151
Alex Deymo0d11c602014-04-23 20:12:20 -0700152 vector<pair<EvalStatus, UpdateCheckParams>> calls;
153 Callback<void(EvalStatus, const UpdateCheckParams& result)> callback =
154 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700155
Alex Deymo63784a52014-05-28 10:46:14 -0700156 umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700157 // The callback should wait until we run the main loop for it to be executed.
158 EXPECT_EQ(0, calls.size());
159 chromeos_update_engine::RunGMainLoopMaxIterations(100);
160 EXPECT_EQ(1, calls.size());
161}
162
Alex Deymo63784a52014-05-28 10:46:14 -0700163} // namespace chromeos_update_manager