blob: 1562727eba39d4ef558a12f9d159f705855dff09 [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 Deymoc705cc82014-02-19 11:15:00 -080011#include <base/memory/scoped_ptr.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080012#include <gtest/gtest.h>
13#include <gmock/gmock.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080014
15#include "update_engine/policy_manager/default_policy.h"
Gilad Arnold308c1012014-03-12 15:37:06 -070016#include "update_engine/policy_manager/fake_state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080017#include "update_engine/policy_manager/mock_policy.h"
18#include "update_engine/policy_manager/pmtest_utils.h"
19#include "update_engine/policy_manager/policy_manager.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070020#include "update_engine/test_utils.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080021
Alex Deymo7b948f02014-03-10 17:01:10 -070022using base::Bind;
23using base::Callback;
Alex Deymo7b948f02014-03-10 17:01:10 -070024using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080025using std::string;
Alex Deymo7b948f02014-03-10 17:01:10 -070026using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080027using testing::Return;
28using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080029using testing::_;
30
Alex Deymoc705cc82014-02-19 11:15:00 -080031namespace chromeos_policy_manager {
32
33class PmPolicyManagerTest : public ::testing::Test {
34 protected:
35 virtual void SetUp() {
Alex Deymo94c06162014-03-21 20:34:46 -070036 fake_state_ = FakeState::Construct();
37 PMTEST_ASSERT_NOT_NULL(fake_state_);
38 EXPECT_TRUE(pmut_.Init(fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080039 }
40
Alex Deymo94c06162014-03-21 20:34:46 -070041 FakeState* fake_state_;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080042 PolicyManager pmut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080043};
44
45// The FailingPolicy implements a single method and make it always fail. This
46// class extends the DefaultPolicy class to allow extensions of the Policy
47// class without extending nor changing this test.
48class FailingPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080049 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080050 string* error,
51 bool* result) const {
52 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070053 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080054 }
55};
56
Alex Deymo7b948f02014-03-10 17:01:10 -070057// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080058class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080059 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080060 string* error,
61 bool* result) const {
Alex Deymoe636c3c2014-03-11 19:02:08 -070062 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -080063 }
64};
65
Alex Deymo7b948f02014-03-10 17:01:10 -070066// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
67// of EvalStatus and T instances. This allows to create a callback that keeps
68// track of when it is called and the arguments passed to it, to be used with
69// the PolicyManager::AsyncPolicyRequest().
70template<typename T>
71static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
72 EvalStatus status, const T& result) {
73 acc->push_back(std::make_pair(status, result));
74}
75
Alex Deymoc705cc82014-02-19 11:15:00 -080076TEST_F(PmPolicyManagerTest, PolicyRequestCall) {
77 bool result;
78 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070079 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -080080}
81
82TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) {
83 StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>();
84 pmut_.policy_.reset(policy);
85 bool result;
86
87 // Tests that the method is called on the policy_ instance.
Alex Deymo2de23f52014-02-26 14:30:13 -080088 EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _))
Alex Deymoe636c3c2014-03-11 19:02:08 -070089 .WillOnce(Return(EvalStatus::kSucceeded));
Alex Deymoc705cc82014-02-19 11:15:00 -080090 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070091 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -080092}
93
94TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
95 pmut_.policy_.reset(new FailingPolicy());
96
97 // Tests that the DefaultPolicy instance is called when the method fails,
98 // which will set this as true.
99 bool result = false;
100 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -0700101 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -0800102 EXPECT_TRUE(result);
103}
104
105TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
106 pmut_.policy_.reset(new LazyPolicy());
107 bool result;
108
109 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -0700110 EXPECT_EQ(status, EvalStatus::kAskMeAgainLater);
Alex Deymoc705cc82014-02-19 11:15:00 -0800111}
112
Alex Deymo7b948f02014-03-10 17:01:10 -0700113TEST_F(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation) {
114 // To avoid differences in code execution order between an AsyncPolicyRequest
115 // call on a policy that returns AskMeAgainLater the first time and one that
116 // succeeds the first time, we ensure that the passed callback is called from
117 // the main loop in both cases even when we could evaluate it right now.
118 pmut_.policy_.reset(new FailingPolicy());
119
120 vector<pair<EvalStatus, bool>> calls;
121 Callback<void(EvalStatus, const bool& result)> callback =
122 Bind(AccumulateCallsCallback<bool>, &calls);
123
124 pmut_.AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
125 // The callback should wait until we run the main loop for it to be executed.
126 EXPECT_EQ(0, calls.size());
127 chromeos_update_engine::RunGMainLoopMaxIterations(100);
128 EXPECT_EQ(1, calls.size());
129}
130
Alex Deymoc705cc82014-02-19 11:15:00 -0800131} // namespace chromeos_policy_manager