blob: 9b14a00a39254d72e59aded1d2f25a5ad4cf847f [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 <gmock/gmock.h>
Alex Deymo1f012912014-04-24 19:08:04 -070012#include <gtest/gtest.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080013
Alex Deymo41a75a72014-04-15 15:36:22 -070014#include "update_engine/fake_clock.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080015#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 Deymo41a75a72014-04-15 15:36:22 -070024using chromeos_update_engine::FakeClock;
Alex Deymo7b948f02014-03-10 17:01:10 -070025using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080026using std::string;
Alex Deymo7b948f02014-03-10 17:01:10 -070027using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080028using testing::Return;
29using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080030using testing::_;
31
Alex Deymoc705cc82014-02-19 11:15:00 -080032namespace chromeos_policy_manager {
33
34class PmPolicyManagerTest : public ::testing::Test {
Alex Deymo41a75a72014-04-15 15:36:22 -070035 public:
36 PmPolicyManagerTest() : pmut_(&fake_clock_) {}
37
Alex Deymoc705cc82014-02-19 11:15:00 -080038 protected:
39 virtual void SetUp() {
Alex Deymo42c30c32014-04-24 18:41:18 -070040 fake_state_ = new FakeState();
Alex Deymo94c06162014-03-21 20:34:46 -070041 EXPECT_TRUE(pmut_.Init(fake_state_));
Alex Deymoc705cc82014-02-19 11:15:00 -080042 }
43
Alex Deymo94c06162014-03-21 20:34:46 -070044 FakeState* fake_state_;
Alex Deymo41a75a72014-04-15 15:36:22 -070045 FakeClock fake_clock_;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080046 PolicyManager pmut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080047};
48
49// The FailingPolicy implements a single method and make it always fail. This
50// class extends the DefaultPolicy class to allow extensions of the Policy
51// class without extending nor changing this test.
52class FailingPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080053 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080054 string* error,
55 bool* result) const {
56 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070057 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080058 }
59};
60
Alex Deymo7b948f02014-03-10 17:01:10 -070061// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080062class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080063 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080064 string* error,
65 bool* result) const {
Alex Deymoe636c3c2014-03-11 19:02:08 -070066 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -080067 }
68};
69
Alex Deymo7b948f02014-03-10 17:01:10 -070070// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
71// of EvalStatus and T instances. This allows to create a callback that keeps
72// track of when it is called and the arguments passed to it, to be used with
73// the PolicyManager::AsyncPolicyRequest().
74template<typename T>
75static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
76 EvalStatus status, const T& result) {
77 acc->push_back(std::make_pair(status, result));
78}
79
Alex Deymoc705cc82014-02-19 11:15:00 -080080TEST_F(PmPolicyManagerTest, PolicyRequestCall) {
81 bool result;
82 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070083 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -080084}
85
86TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) {
87 StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>();
88 pmut_.policy_.reset(policy);
89 bool result;
90
91 // Tests that the method is called on the policy_ instance.
Alex Deymo2de23f52014-02-26 14:30:13 -080092 EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _))
Alex Deymoe636c3c2014-03-11 19:02:08 -070093 .WillOnce(Return(EvalStatus::kSucceeded));
Alex Deymoc705cc82014-02-19 11:15:00 -080094 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070095 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -080096}
97
98TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
99 pmut_.policy_.reset(new FailingPolicy());
100
101 // Tests that the DefaultPolicy instance is called when the method fails,
102 // which will set this as true.
103 bool result = false;
104 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -0700105 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -0800106 EXPECT_TRUE(result);
107}
108
109TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
110 pmut_.policy_.reset(new LazyPolicy());
111 bool result;
112
113 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -0700114 EXPECT_EQ(status, EvalStatus::kAskMeAgainLater);
Alex Deymoc705cc82014-02-19 11:15:00 -0800115}
116
Alex Deymo7b948f02014-03-10 17:01:10 -0700117TEST_F(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation) {
118 // To avoid differences in code execution order between an AsyncPolicyRequest
119 // call on a policy that returns AskMeAgainLater the first time and one that
120 // succeeds the first time, we ensure that the passed callback is called from
121 // the main loop in both cases even when we could evaluate it right now.
122 pmut_.policy_.reset(new FailingPolicy());
123
124 vector<pair<EvalStatus, bool>> calls;
125 Callback<void(EvalStatus, const bool& result)> callback =
126 Bind(AccumulateCallsCallback<bool>, &calls);
127
128 pmut_.AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
129 // The callback should wait until we run the main loop for it to be executed.
130 EXPECT_EQ(0, calls.size());
131 chromeos_update_engine::RunGMainLoopMaxIterations(100);
132 EXPECT_EQ(1, calls.size());
133}
134
Alex Deymoc705cc82014-02-19 11:15:00 -0800135} // namespace chromeos_policy_manager