Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 1 | // 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 | |
| 16 | using base::TimeDelta; |
| 17 | using std::string; |
| 18 | |
| 19 | using testing::_; |
| 20 | using testing::Return; |
| 21 | using testing::StrictMock; |
| 22 | |
| 23 | namespace chromeos_policy_manager { |
| 24 | |
| 25 | class 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. |
| 37 | class FailingPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame^] | 38 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 39 | string* error, |
| 40 | bool* result) const { |
| 41 | *error = "FailingPolicy failed."; |
| 42 | return EvalStatusFailed; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | // The LazyPolicy always returns |
| 47 | class LazyPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame^] | 48 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 49 | string* error, |
| 50 | bool* result) const { |
| 51 | return EvalStatusAskMeAgainLater; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | TEST_F(PmPolicyManagerTest, PolicyRequestCall) { |
| 56 | bool result; |
| 57 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
| 58 | EXPECT_EQ(status, EvalStatusSucceeded); |
| 59 | } |
| 60 | |
| 61 | TEST_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 Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame^] | 67 | EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _)) |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 68 | .WillOnce(Return(EvalStatusSucceeded)); |
| 69 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
| 70 | EXPECT_EQ(status, EvalStatusSucceeded); |
| 71 | } |
| 72 | |
| 73 | TEST_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 | |
| 84 | TEST_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 |