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> |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 11 | // TODO(garnold) Remove once shill DBus constants not needed. |
| 12 | #include <chromeos/dbus/service_constants.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
| 14 | #include <gmock/gmock.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 15 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 16 | #include "update_engine/fake_clock.h" |
| 17 | #include "update_engine/mock_dbus_wrapper.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 18 | #include "update_engine/policy_manager/default_policy.h" |
| 19 | #include "update_engine/policy_manager/mock_policy.h" |
| 20 | #include "update_engine/policy_manager/pmtest_utils.h" |
| 21 | #include "update_engine/policy_manager/policy_manager.h" |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 22 | // TODO(garnold) Remove once we stop mocking DBus. |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 23 | #include "update_engine/test_utils.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 24 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 25 | using base::Bind; |
| 26 | using base::Callback; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 27 | using chromeos_update_engine::FakeClock; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 28 | using chromeos_update_engine::GValueNewString; |
| 29 | using chromeos_update_engine::GValueFree; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 30 | using chromeos_update_engine::MockDBusWrapper; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 31 | using std::pair; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 32 | using std::string; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 33 | using std::vector; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 34 | using testing::NiceMock; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 35 | using testing::Return; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 36 | using testing::SetArgPointee; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 37 | using testing::StrictMock; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 38 | using testing::_; |
| 39 | |
| 40 | namespace { |
| 41 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 42 | // TODO(garnold) This whole section gets removed once we mock the shill provider |
| 43 | // itself in tests. |
| 44 | |
| 45 | // Fake dbus-glib objects. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 46 | DBusGConnection* const kFakeConnection = reinterpret_cast<DBusGConnection*>(1); |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 47 | DBusGProxy* const kFakeManagerProxy = reinterpret_cast<DBusGProxy*>(2); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 48 | |
| 49 | } // namespace |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 50 | |
| 51 | namespace chromeos_policy_manager { |
| 52 | |
| 53 | class PmPolicyManagerTest : public ::testing::Test { |
| 54 | protected: |
| 55 | virtual void SetUp() { |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 56 | // TODO(garnold) Replace this low-level DBus injection with a high-level |
| 57 | // mock shill provider. |
| 58 | EXPECT_CALL(mock_dbus_, BusGet(_, _)) |
| 59 | .WillOnce(Return(kFakeConnection)); |
| 60 | EXPECT_CALL(mock_dbus_, ProxyNewForName(_, _, _, _)) |
| 61 | .WillOnce(Return(kFakeManagerProxy)); |
| 62 | EXPECT_CALL(mock_dbus_, ProxyAddSignal_2(_, _, _, _)) |
| 63 | .WillOnce(Return()); |
| 64 | EXPECT_CALL(mock_dbus_, ProxyConnectSignal(_, _, _, _, _)) |
| 65 | .WillOnce(Return()); |
| 66 | auto properties = g_hash_table_new_full(g_str_hash, g_str_equal, free, |
| 67 | GValueFree); |
| 68 | g_hash_table_insert(properties, strdup(shill::kDefaultServiceProperty), |
| 69 | GValueNewString("/")); |
| 70 | EXPECT_CALL(mock_dbus_, ProxyCall_0_1(_, _, _, _)) |
| 71 | .WillOnce(DoAll(SetArgPointee<3>(g_hash_table_ref(properties)), |
| 72 | Return(true))); |
| 73 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 74 | EXPECT_TRUE(pmut_.Init(&mock_dbus_, &fake_clock_)); |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame^] | 75 | |
| 76 | // TODO(garnold) Remove this, too. |
| 77 | g_hash_table_unref(properties); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 80 | NiceMock<MockDBusWrapper> mock_dbus_; |
| 81 | FakeClock fake_clock_; |
| 82 | PolicyManager pmut_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | // The FailingPolicy implements a single method and make it always fail. This |
| 86 | // class extends the DefaultPolicy class to allow extensions of the Policy |
| 87 | // class without extending nor changing this test. |
| 88 | class FailingPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 89 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 90 | string* error, |
| 91 | bool* result) const { |
| 92 | *error = "FailingPolicy failed."; |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 93 | return EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 94 | } |
| 95 | }; |
| 96 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 97 | // The LazyPolicy always returns EvalStatus::kAskMeAgainLater. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 98 | class LazyPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 99 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 100 | string* error, |
| 101 | bool* result) const { |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 102 | return EvalStatus::kAskMeAgainLater; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 106 | // AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs |
| 107 | // of EvalStatus and T instances. This allows to create a callback that keeps |
| 108 | // track of when it is called and the arguments passed to it, to be used with |
| 109 | // the PolicyManager::AsyncPolicyRequest(). |
| 110 | template<typename T> |
| 111 | static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc, |
| 112 | EvalStatus status, const T& result) { |
| 113 | acc->push_back(std::make_pair(status, result)); |
| 114 | } |
| 115 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 116 | TEST_F(PmPolicyManagerTest, PolicyRequestCall) { |
| 117 | bool result; |
| 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::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) { |
| 123 | StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>(); |
| 124 | pmut_.policy_.reset(policy); |
| 125 | bool result; |
| 126 | |
| 127 | // Tests that the method is called on the policy_ instance. |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 128 | EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _)) |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 129 | .WillOnce(Return(EvalStatus::kSucceeded)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 130 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 131 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) { |
| 135 | pmut_.policy_.reset(new FailingPolicy()); |
| 136 | |
| 137 | // Tests that the DefaultPolicy instance is called when the method fails, |
| 138 | // which will set this as true. |
| 139 | bool result = false; |
| 140 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 141 | EXPECT_EQ(status, EvalStatus::kSucceeded); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 142 | EXPECT_TRUE(result); |
| 143 | } |
| 144 | |
| 145 | TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) { |
| 146 | pmut_.policy_.reset(new LazyPolicy()); |
| 147 | bool result; |
| 148 | |
| 149 | EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result); |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 150 | EXPECT_EQ(status, EvalStatus::kAskMeAgainLater); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 153 | TEST_F(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation) { |
| 154 | // To avoid differences in code execution order between an AsyncPolicyRequest |
| 155 | // call on a policy that returns AskMeAgainLater the first time and one that |
| 156 | // succeeds the first time, we ensure that the passed callback is called from |
| 157 | // the main loop in both cases even when we could evaluate it right now. |
| 158 | pmut_.policy_.reset(new FailingPolicy()); |
| 159 | |
| 160 | vector<pair<EvalStatus, bool>> calls; |
| 161 | Callback<void(EvalStatus, const bool& result)> callback = |
| 162 | Bind(AccumulateCallsCallback<bool>, &calls); |
| 163 | |
| 164 | pmut_.AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); |
| 165 | // The callback should wait until we run the main loop for it to be executed. |
| 166 | EXPECT_EQ(0, calls.size()); |
| 167 | chromeos_update_engine::RunGMainLoopMaxIterations(100); |
| 168 | EXPECT_EQ(1, calls.size()); |
| 169 | } |
| 170 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 171 | } // namespace chromeos_policy_manager |