blob: 5272bcd2a8a867c10858b470227c1f414a5526ec [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>
Alex Deymoc705cc82014-02-19 11:15:00 -080012#include <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070013#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080014
Alex Deymo41a75a72014-04-15 15:36:22 -070015#include "update_engine/fake_clock.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080016#include "update_engine/policy_manager/default_policy.h"
Gilad Arnold308c1012014-03-12 15:37:06 -070017#include "update_engine/policy_manager/fake_state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080018#include "update_engine/policy_manager/mock_policy.h"
19#include "update_engine/policy_manager/pmtest_utils.h"
20#include "update_engine/policy_manager/policy_manager.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070021#include "update_engine/test_utils.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080022
Alex Deymo7b948f02014-03-10 17:01:10 -070023using base::Bind;
24using base::Callback;
Alex Deymo41a75a72014-04-15 15:36:22 -070025using chromeos_update_engine::FakeClock;
Alex Deymo7b948f02014-03-10 17:01:10 -070026using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080027using std::string;
Alex Deymo7b948f02014-03-10 17:01:10 -070028using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080029using testing::Return;
30using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080031using testing::_;
32
Alex Deymoc705cc82014-02-19 11:15:00 -080033namespace chromeos_policy_manager {
34
35class PmPolicyManagerTest : public ::testing::Test {
36 protected:
37 virtual void SetUp() {
Alex Deymo42c30c32014-04-24 18:41:18 -070038 fake_state_ = new FakeState();
Alex Deymo680d0222014-04-24 21:00:08 -070039 pmut_.reset(new PolicyManager(&fake_clock_, fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080040 }
41
Alex Deymo680d0222014-04-24 21:00:08 -070042 FakeState* fake_state_; // Owned by the pmut_.
Alex Deymo41a75a72014-04-15 15:36:22 -070043 FakeClock fake_clock_;
Alex Deymo680d0222014-04-24 21:00:08 -070044 scoped_ptr<PolicyManager> pmut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080045};
46
47// The FailingPolicy implements a single method and make it always fail. This
48// class extends the DefaultPolicy class to allow extensions of the Policy
49// class without extending nor changing this test.
50class FailingPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080051 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080052 string* error,
Alex Deymo0d11c602014-04-23 20:12:20 -070053 UpdateCheckParams* result) const {
Alex Deymoc705cc82014-02-19 11:15:00 -080054 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070055 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080056 }
57};
58
Alex Deymo7b948f02014-03-10 17:01:10 -070059// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080060class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080061 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080062 string* error,
Alex Deymo0d11c602014-04-23 20:12:20 -070063 UpdateCheckParams* result) const {
Alex Deymoe636c3c2014-03-11 19:02:08 -070064 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -080065 }
66};
67
Alex Deymo7b948f02014-03-10 17:01:10 -070068// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
69// of EvalStatus and T instances. This allows to create a callback that keeps
70// track of when it is called and the arguments passed to it, to be used with
71// the PolicyManager::AsyncPolicyRequest().
72template<typename T>
73static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
74 EvalStatus status, const T& result) {
75 acc->push_back(std::make_pair(status, result));
76}
77
Alex Deymo0d11c602014-04-23 20:12:20 -070078// Tests that policy requests are completed successfully. It is important that
79// this tests cover all policy requests as defined in Policy.
80TEST_F(PmPolicyManagerTest, PolicyRequestCallUpdateDownloadAndApplyAllowed) {
Alex Deymoc705cc82014-02-19 11:15:00 -080081 bool result;
Alex Deymo0d11c602014-04-23 20:12:20 -070082 EXPECT_EQ(EvalStatus::kSucceeded,
83 pmut_->PolicyRequest(&Policy::UpdateDownloadAndApplyAllowed,
84 &result));
Alex Deymoc705cc82014-02-19 11:15:00 -080085}
86
Alex Deymo0d11c602014-04-23 20:12:20 -070087TEST_F(PmPolicyManagerTest, PolicyRequestCallUpdateCheckAllowed) {
88 UpdateCheckParams result;
89 EXPECT_EQ(EvalStatus::kSucceeded, pmut_->PolicyRequest(
90 &Policy::UpdateCheckAllowed, &result));
Alex Deymoc705cc82014-02-19 11:15:00 -080091}
92
93TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
Alex Deymo680d0222014-04-24 21:00:08 -070094 pmut_->set_policy(new FailingPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -080095
96 // Tests that the DefaultPolicy instance is called when the method fails,
97 // which will set this as true.
Alex Deymo0d11c602014-04-23 20:12:20 -070098 UpdateCheckParams result;
99 result.updates_enabled = false;
Alex Deymo680d0222014-04-24 21:00:08 -0700100 EvalStatus status = pmut_->PolicyRequest(
101 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700102 EXPECT_EQ(EvalStatus::kSucceeded, status);
Alex Deymo0d11c602014-04-23 20:12:20 -0700103 EXPECT_TRUE(result.updates_enabled);
Alex Deymoc705cc82014-02-19 11:15:00 -0800104}
105
106TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700107 UpdateCheckParams result;
Alex Deymo680d0222014-04-24 21:00:08 -0700108 pmut_->set_policy(new LazyPolicy());
Alex Deymoc705cc82014-02-19 11:15:00 -0800109
Alex Deymo680d0222014-04-24 21:00:08 -0700110 EvalStatus status = pmut_->PolicyRequest(
111 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700112 EXPECT_EQ(EvalStatus::kAskMeAgainLater, status);
Alex Deymoc705cc82014-02-19 11:15:00 -0800113}
114
Alex Deymo7b948f02014-03-10 17:01:10 -0700115TEST_F(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation) {
116 // To avoid differences in code execution order between an AsyncPolicyRequest
117 // call on a policy that returns AskMeAgainLater the first time and one that
118 // succeeds the first time, we ensure that the passed callback is called from
119 // the main loop in both cases even when we could evaluate it right now.
Alex Deymo680d0222014-04-24 21:00:08 -0700120 pmut_->set_policy(new FailingPolicy());
Alex Deymo7b948f02014-03-10 17:01:10 -0700121
Alex Deymo0d11c602014-04-23 20:12:20 -0700122 vector<pair<EvalStatus, UpdateCheckParams>> calls;
123 Callback<void(EvalStatus, const UpdateCheckParams& result)> callback =
124 Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
Alex Deymo7b948f02014-03-10 17:01:10 -0700125
Alex Deymo680d0222014-04-24 21:00:08 -0700126 pmut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
Alex Deymo7b948f02014-03-10 17:01:10 -0700127 // The callback should wait until we run the main loop for it to be executed.
128 EXPECT_EQ(0, calls.size());
129 chromeos_update_engine::RunGMainLoopMaxIterations(100);
130 EXPECT_EQ(1, calls.size());
131}
132
Alex Deymoc705cc82014-02-19 11:15:00 -0800133} // namespace chromeos_policy_manager