blob: 9e28ebc9e969f055e70de21352df2895c28dff42 [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>
11#include <base/time.h>
12#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"
16#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 Deymoc705cc82014-02-19 11:15:00 -080023using base::TimeDelta;
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 -080027
28using testing::_;
29using testing::Return;
30using testing::StrictMock;
31
32namespace chromeos_policy_manager {
33
34class PmPolicyManagerTest : public ::testing::Test {
35 protected:
36 virtual void SetUp() {
37 EXPECT_TRUE(pmut_.Init());
38 }
39
40 PolicyManager pmut_; // PolicyManager undert test.
41};
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