blob: ba001c971c6d09ea2f709808db4613edc5cc7e94 [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
5#include <base/memory/scoped_ptr.h>
6#include <base/time.h>
7#include <gtest/gtest.h>
8#include <gmock/gmock.h>
9#include <string>
10
11#include "update_engine/policy_manager/default_policy.h"
12#include "update_engine/policy_manager/mock_policy.h"
13#include "update_engine/policy_manager/pmtest_utils.h"
14#include "update_engine/policy_manager/policy_manager.h"
15
16using base::TimeDelta;
17using std::string;
18
19using testing::_;
20using testing::Return;
21using testing::StrictMock;
22
23namespace chromeos_policy_manager {
24
25class PmPolicyManagerTest : public ::testing::Test {
26 protected:
27 virtual void SetUp() {
28 EXPECT_TRUE(pmut_.Init());
29 }
30
31 PolicyManager pmut_; // PolicyManager undert test.
32};
33
34// The FailingPolicy implements a single method and make it always fail. This
35// class extends the DefaultPolicy class to allow extensions of the Policy
36// class without extending nor changing this test.
37class FailingPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080038 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080039 string* error,
40 bool* result) const {
41 *error = "FailingPolicy failed.";
42 return EvalStatusFailed;
43 }
44};
45
46// The LazyPolicy always returns
47class LazyPolicy : public DefaultPolicy {
Alex Deymo2de23f52014-02-26 14:30:13 -080048 virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
Alex Deymoc705cc82014-02-19 11:15:00 -080049 string* error,
50 bool* result) const {
51 return EvalStatusAskMeAgainLater;
52 }
53};
54
55TEST_F(PmPolicyManagerTest, PolicyRequestCall) {
56 bool result;
57 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
58 EXPECT_EQ(status, EvalStatusSucceeded);
59}
60
61TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) {
62 StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>();
63 pmut_.policy_.reset(policy);
64 bool result;
65
66 // Tests that the method is called on the policy_ instance.
Alex Deymo2de23f52014-02-26 14:30:13 -080067 EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _))
Alex Deymoc705cc82014-02-19 11:15:00 -080068 .WillOnce(Return(EvalStatusSucceeded));
69 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
70 EXPECT_EQ(status, EvalStatusSucceeded);
71}
72
73TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
74 pmut_.policy_.reset(new FailingPolicy());
75
76 // Tests that the DefaultPolicy instance is called when the method fails,
77 // which will set this as true.
78 bool result = false;
79 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
80 EXPECT_EQ(status, EvalStatusSucceeded);
81 EXPECT_TRUE(result);
82}
83
84TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
85 pmut_.policy_.reset(new LazyPolicy());
86 bool result;
87
88 EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
89 EXPECT_EQ(status, EvalStatusAskMeAgainLater);
90}
91
92} // namespace chromeos_policy_manager