blob: 00a83b4a7e1885d89662c92ce5b2b357da35a8f3 [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>
7#include <vector>
8
9#include <base/bind.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080010#include <base/memory/scoped_ptr.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080011#include <gtest/gtest.h>
12#include <gmock/gmock.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080013
14#include "update_engine/policy_manager/default_policy.h"
Gilad Arnold308c1012014-03-12 15:37:06 -070015#include "update_engine/policy_manager/fake_state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080016#include "update_engine/policy_manager/mock_policy.h"
17#include "update_engine/policy_manager/pmtest_utils.h"
18#include "update_engine/policy_manager/policy_manager.h"
Alex Deymo7b948f02014-03-10 17:01:10 -070019#include "update_engine/test_utils.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080020
Alex Deymo7b948f02014-03-10 17:01:10 -070021using base::Bind;
22using base::Callback;
Alex Deymo7b948f02014-03-10 17:01:10 -070023using std::pair;
Alex Deymoc705cc82014-02-19 11:15:00 -080024using std::string;
Alex Deymo7b948f02014-03-10 17:01:10 -070025using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080026using testing::Return;
27using testing::StrictMock;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080028using testing::_;
29
Alex Deymoc705cc82014-02-19 11:15:00 -080030namespace chromeos_policy_manager {
31
32class PmPolicyManagerTest : public ::testing::Test {
33 protected:
34 virtual void SetUp() {
Gilad Arnold308c1012014-03-12 15:37:06 -070035 FakeState* fake_state = new FakeState();
36 ASSERT_TRUE(fake_state->Init());
37 EXPECT_TRUE(pmut_.Init(fake_state));
Alex Deymoc705cc82014-02-19 11:15:00 -080038 }
39
Gilad Arnold5ef9c482014-03-03 13:51:02 -080040 PolicyManager pmut_;
Alex Deymoc705cc82014-02-19 11:15:00 -080041};
42
43// The FailingPolicy implements a single method and make it always fail. This
44// class extends the DefaultPolicy class to allow extensions of the Policy
45// class without extending nor changing this test.
46class FailingPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080047 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080048 string* error,
49 bool* result) const {
50 *error = "FailingPolicy failed.";
Alex Deymoe636c3c2014-03-11 19:02:08 -070051 return EvalStatus::kFailed;
Alex Deymoc705cc82014-02-19 11:15:00 -080052 }
53};
54
Alex Deymo7b948f02014-03-10 17:01:10 -070055// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
Alex Deymoc705cc82014-02-19 11:15:00 -080056class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080057 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080058 string* error,
59 bool* result) const {
Alex Deymoe636c3c2014-03-11 19:02:08 -070060 return EvalStatus::kAskMeAgainLater;
Alex Deymoc705cc82014-02-19 11:15:00 -080061 }
62};
63
Alex Deymo7b948f02014-03-10 17:01:10 -070064// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
65// of EvalStatus and T instances. This allows to create a callback that keeps
66// track of when it is called and the arguments passed to it, to be used with
67// the PolicyManager::AsyncPolicyRequest().
68template<typename T>
69static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
70 EvalStatus status, const T& result) {
71 acc->push_back(std::make_pair(status, result));
72}
73
Alex Deymoc705cc82014-02-19 11:15:00 -080074TEST_F(PmPolicyManagerTest, PolicyRequestCall) {
75 bool result;
76 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070077 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -080078}
79
80TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) {
81 StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>();
82 pmut_.policy_.reset(policy);
83 bool result;
84
85 // Tests that the method is called on the policy_ instance.
Alex Deymo2de23f52014-02-26 14:30:13 -080086 EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _))
Alex Deymoe636c3c2014-03-11 19:02:08 -070087 .WillOnce(Return(EvalStatus::kSucceeded));
Alex Deymoc705cc82014-02-19 11:15:00 -080088 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070089 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -080090}
91
92TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
93 pmut_.policy_.reset(new FailingPolicy());
94
95 // Tests that the DefaultPolicy instance is called when the method fails,
96 // which will set this as true.
97 bool result = false;
98 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -070099 EXPECT_EQ(status, EvalStatus::kSucceeded);
Alex Deymoc705cc82014-02-19 11:15:00 -0800100 EXPECT_TRUE(result);
101}
102
103TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
104 pmut_.policy_.reset(new LazyPolicy());
105 bool result;
106
107 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
Alex Deymoe636c3c2014-03-11 19:02:08 -0700108 EXPECT_EQ(status, EvalStatus::kAskMeAgainLater);
Alex Deymoc705cc82014-02-19 11:15:00 -0800109}
110
Alex Deymo7b948f02014-03-10 17:01:10 -0700111TEST_F(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation) {
112 // To avoid differences in code execution order between an AsyncPolicyRequest
113 // call on a policy that returns AskMeAgainLater the first time and one that
114 // succeeds the first time, we ensure that the passed callback is called from
115 // the main loop in both cases even when we could evaluate it right now.
116 pmut_.policy_.reset(new FailingPolicy());
117
118 vector<pair<EvalStatus, bool>> calls;
119 Callback<void(EvalStatus, const bool& result)> callback =
120 Bind(AccumulateCallsCallback<bool>, &calls);
121
122 pmut_.AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
123 // The callback should wait until we run the main loop for it to be executed.
124 EXPECT_EQ(0, calls.size());
125 chromeos_update_engine::RunGMainLoopMaxIterations(100);
126 EXPECT_EQ(1, calls.size());
127}
128
Alex Deymoc705cc82014-02-19 11:15:00 -0800129} // namespace chromeos_policy_manager