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 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame^] | 14 | #include "update_engine/fake_clock.h" |
| 15 | #include "update_engine/mock_dbus_wrapper.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | #include "update_engine/policy_manager/default_policy.h" |
| 17 | #include "update_engine/policy_manager/mock_policy.h" |
| 18 | #include "update_engine/policy_manager/pmtest_utils.h" |
| 19 | #include "update_engine/policy_manager/policy_manager.h" |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 20 | #include "update_engine/test_utils.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 21 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 22 | using base::Bind; |
| 23 | using base::Callback; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame^] | 24 | using chromeos_update_engine::FakeClock; |
| 25 | using chromeos_update_engine::MockDBusWrapper; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 26 | using std::pair; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 27 | using std::string; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 28 | using std::vector; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame^] | 29 | using testing::NiceMock; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 30 | using testing::Return; |
| 31 | using testing::StrictMock; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame^] | 32 | using testing::_; |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | DBusGConnection* const kFakeConnection = reinterpret_cast<DBusGConnection*>(1); |
| 37 | |
| 38 | } // namespace |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 39 | |
| 40 | namespace chromeos_policy_manager { |
| 41 | |
| 42 | class PmPolicyManagerTest : public ::testing::Test { |
| 43 | protected: |
| 44 | virtual void SetUp() { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame^] | 45 | EXPECT_CALL(mock_dbus_, BusGet(_, _)).WillOnce(Return(kFakeConnection)); |
| 46 | EXPECT_TRUE(pmut_.Init(&mock_dbus_, &fake_clock_)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame^] | 49 | NiceMock<MockDBusWrapper> mock_dbus_; |
| 50 | FakeClock fake_clock_; |
| 51 | PolicyManager pmut_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // The FailingPolicy implements a single method and make it always fail. This |
| 55 | // class extends the DefaultPolicy class to allow extensions of the Policy |
| 56 | // class without extending nor changing this test. |
| 57 | class FailingPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 58 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 59 | string* error, |
| 60 | bool* result) const { |
| 61 | *error = "FailingPolicy failed."; |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 62 | return EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 66 | // The LazyPolicy always returns EvalStatus::kAskMeAgainLater. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 67 | class LazyPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 68 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 69 | string* error, |
| 70 | bool* result) const { |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 71 | return EvalStatus::kAskMeAgainLater; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 72 | } |
| 73 | }; |
| 74 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 75 | // AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs |
| 76 | // of EvalStatus and T instances. This allows to create a callback that keeps |
| 77 | // track of when it is called and the arguments passed to it, to be used with |
| 78 | // the PolicyManager::AsyncPolicyRequest(). |
| 79 | template<typename T> |
| 80 | static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc, |
| 81 | EvalStatus status, const T& result) { |
| 82 | acc->push_back(std::make_pair(status, result)); |
| 83 | } |
| 84 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 85 | TEST_F(PmPolicyManagerTest, PolicyRequestCall) { |
| 86 | bool result; |
| 87 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 88 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) { |
| 92 | StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>(); |
| 93 | pmut_.policy_.reset(policy); |
| 94 | bool result; |
| 95 | |
| 96 | // Tests that the method is called on the policy_ instance. |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 97 | EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _)) |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 98 | .WillOnce(Return(EvalStatus::kSucceeded)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 99 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 100 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) { |
| 104 | pmut_.policy_.reset(new FailingPolicy()); |
| 105 | |
| 106 | // Tests that the DefaultPolicy instance is called when the method fails, |
| 107 | // which will set this as true. |
| 108 | bool result = false; |
| 109 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 110 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 111 | EXPECT_TRUE(result); |
| 112 | } |
| 113 | |
| 114 | TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) { |
| 115 | pmut_.policy_.reset(new LazyPolicy()); |
| 116 | bool result; |
| 117 | |
| 118 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 119 | EXPECT_EQ(status, EvalStatus::kAskMeAgainLater); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 122 | TEST_F(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation) { |
| 123 | // To avoid differences in code execution order between an AsyncPolicyRequest |
| 124 | // call on a policy that returns AskMeAgainLater the first time and one that |
| 125 | // succeeds the first time, we ensure that the passed callback is called from |
| 126 | // the main loop in both cases even when we could evaluate it right now. |
| 127 | pmut_.policy_.reset(new FailingPolicy()); |
| 128 | |
| 129 | vector<pair<EvalStatus, bool>> calls; |
| 130 | Callback<void(EvalStatus, const bool& result)> callback = |
| 131 | Bind(AccumulateCallsCallback<bool>, &calls); |
| 132 | |
| 133 | pmut_.AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); |
| 134 | // The callback should wait until we run the main loop for it to be executed. |
| 135 | EXPECT_EQ(0, calls.size()); |
| 136 | chromeos_update_engine::RunGMainLoopMaxIterations(100); |
| 137 | EXPECT_EQ(1, calls.size()); |
| 138 | } |
| 139 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 140 | } // namespace chromeos_policy_manager |