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 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <base/bind.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 10 | #include <base/memory/scoped_ptr.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | #include <gmock/gmock.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 13 | |
| 14 | #include "update_engine/policy_manager/default_policy.h" |
Gilad Arnold | 308c101 | 2014-03-12 15:37:06 -0700 | [diff] [blame] | 15 | #include "update_engine/policy_manager/fake_state.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 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 Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 19 | #include "update_engine/test_utils.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 20 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 21 | using base::Bind; |
| 22 | using base::Callback; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 23 | using std::pair; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 24 | using std::string; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 25 | using std::vector; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 26 | using testing::Return; |
| 27 | using testing::StrictMock; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 28 | using testing::_; |
| 29 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 30 | namespace chromeos_policy_manager { |
| 31 | |
| 32 | class PmPolicyManagerTest : public ::testing::Test { |
| 33 | protected: |
| 34 | virtual void SetUp() { |
Gilad Arnold | 308c101 | 2014-03-12 15:37:06 -0700 | [diff] [blame] | 35 | FakeState* fake_state = new FakeState(); |
| 36 | ASSERT_TRUE(fake_state->Init()); |
| 37 | EXPECT_TRUE(pmut_.Init(fake_state)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 40 | PolicyManager pmut_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 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. |
| 46 | class FailingPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 47 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 48 | string* error, |
| 49 | bool* result) const { |
| 50 | *error = "FailingPolicy failed."; |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 51 | return EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 52 | } |
| 53 | }; |
| 54 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 55 | // The LazyPolicy always returns EvalStatus::kAskMeAgainLater. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 56 | class LazyPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 57 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 58 | string* error, |
| 59 | bool* result) const { |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 60 | return EvalStatus::kAskMeAgainLater; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 64 | // 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(). |
| 68 | template<typename T> |
| 69 | static 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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 74 | TEST_F(PmPolicyManagerTest, PolicyRequestCall) { |
| 75 | bool result; |
| 76 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 77 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST_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 Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 86 | EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _)) |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 87 | .WillOnce(Return(EvalStatus::kSucceeded)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 88 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 89 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_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 Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 99 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 100 | EXPECT_TRUE(result); |
| 101 | } |
| 102 | |
| 103 | TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) { |
| 104 | pmut_.policy_.reset(new LazyPolicy()); |
| 105 | bool result; |
| 106 | |
| 107 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 108 | EXPECT_EQ(status, EvalStatus::kAskMeAgainLater); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 111 | TEST_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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 129 | } // namespace chromeos_policy_manager |