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