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> |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 12 | #include <base/time/time.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 13 | #include <gmock/gmock.h> |
Alex Deymo | 1f01291 | 2014-04-24 19:08:04 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 15 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 16 | #include "update_engine/fake_clock.h" |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 17 | #include "update_engine/test_utils.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 18 | #include "update_engine/update_manager/default_policy.h" |
| 19 | #include "update_engine/update_manager/fake_state.h" |
| 20 | #include "update_engine/update_manager/mock_policy.h" |
| 21 | #include "update_engine/update_manager/umtest_utils.h" |
| 22 | #include "update_engine/update_manager/update_manager.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 23 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 24 | using base::Bind; |
| 25 | using base::Callback; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 26 | using base::Time; |
| 27 | using base::TimeDelta; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 28 | using chromeos_update_engine::FakeClock; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 29 | using std::pair; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 30 | using std::string; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 31 | using std::vector; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 32 | using testing::Return; |
| 33 | using testing::StrictMock; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 34 | using testing::_; |
| 35 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | // Generates a fixed timestamp for use in faking the current time. |
| 39 | Time FixedTime() { |
| 40 | Time::Exploded now_exp; |
| 41 | now_exp.year = 2014; |
| 42 | now_exp.month = 3; |
| 43 | now_exp.day_of_week = 2; |
| 44 | now_exp.day_of_month = 18; |
| 45 | now_exp.hour = 8; |
| 46 | now_exp.minute = 5; |
| 47 | now_exp.second = 33; |
| 48 | now_exp.millisecond = 675; |
| 49 | return Time::FromLocalExploded(now_exp); |
| 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 54 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 55 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 56 | class UmUpdateManagerTest : public ::testing::Test { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 57 | protected: |
| 58 | virtual void SetUp() { |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 59 | fake_state_ = new FakeState(); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 60 | umut_.reset(new UpdateManager(&fake_clock_, fake_state_)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 63 | FakeState* fake_state_; // Owned by the umut_. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 64 | FakeClock fake_clock_; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 65 | scoped_ptr<UpdateManager> umut_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // The FailingPolicy implements a single method and make it always fail. This |
| 69 | // class extends the DefaultPolicy class to allow extensions of the Policy |
| 70 | // class without extending nor changing this test. |
| 71 | class FailingPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 72 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 73 | string* error, |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 74 | UpdateCheckParams* result) const { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 75 | *error = "FailingPolicy failed."; |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 76 | return EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 77 | } |
| 78 | }; |
| 79 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 80 | // The LazyPolicy always returns EvalStatus::kAskMeAgainLater. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 81 | class LazyPolicy : public DefaultPolicy { |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 82 | virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 83 | string* error, |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 84 | UpdateCheckParams* result) const { |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 85 | return EvalStatus::kAskMeAgainLater; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 89 | // AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs |
| 90 | // of EvalStatus and T instances. This allows to create a callback that keeps |
| 91 | // track of when it is called and the arguments passed to it, to be used with |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 92 | // the UpdateManager::AsyncPolicyRequest(). |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 93 | template<typename T> |
| 94 | static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc, |
| 95 | EvalStatus status, const T& result) { |
| 96 | acc->push_back(std::make_pair(status, result)); |
| 97 | } |
| 98 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 99 | // Tests that policy requests are completed successfully. It is important that |
| 100 | // this tests cover all policy requests as defined in Policy. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 101 | TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 102 | UpdateCheckParams result; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 103 | EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest( |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 104 | &Policy::UpdateCheckAllowed, &result)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 107 | TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 108 | const UpdateState update_state = { |
| 109 | FixedTime(), 1, TimeDelta::FromSeconds(15), TimeDelta::FromSeconds(60), |
| 110 | 4, 2, 8 |
| 111 | }; |
| 112 | UpdateCanStartResult result; |
| 113 | EXPECT_EQ(EvalStatus::kSucceeded, |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 114 | umut_->PolicyRequest(&Policy::UpdateCanStart, &result, true, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 115 | update_state)); |
| 116 | } |
| 117 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 118 | TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) { |
| 119 | umut_->set_policy(new FailingPolicy()); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 120 | |
| 121 | // Tests that the DefaultPolicy instance is called when the method fails, |
| 122 | // which will set this as true. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 123 | UpdateCheckParams result; |
| 124 | result.updates_enabled = false; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 125 | EvalStatus status = umut_->PolicyRequest( |
Alex Deymo | 680d022 | 2014-04-24 21:00:08 -0700 | [diff] [blame] | 126 | &Policy::UpdateCheckAllowed, &result); |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 127 | EXPECT_EQ(EvalStatus::kSucceeded, status); |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 128 | EXPECT_TRUE(result.updates_enabled); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame^] | 131 | // This test only applies to debug builds where DCHECK is enabled. |
| 132 | #if DCHECK_IS_ON |
| 133 | TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) { |
| 134 | // The update manager should die (DCHECK) if a policy called synchronously |
| 135 | // returns a kAskMeAgainLater value. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 136 | UpdateCheckParams result; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 137 | umut_->set_policy(new LazyPolicy()); |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame^] | 138 | EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), ""); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 139 | } |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame^] | 140 | #endif // DCHECK_IS_ON |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 141 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 142 | TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) { |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 143 | // To avoid differences in code execution order between an AsyncPolicyRequest |
| 144 | // call on a policy that returns AskMeAgainLater the first time and one that |
| 145 | // succeeds the first time, we ensure that the passed callback is called from |
| 146 | // the main loop in both cases even when we could evaluate it right now. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 147 | umut_->set_policy(new FailingPolicy()); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 148 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 149 | vector<pair<EvalStatus, UpdateCheckParams>> calls; |
| 150 | Callback<void(EvalStatus, const UpdateCheckParams& result)> callback = |
| 151 | Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 152 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 153 | umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 154 | // The callback should wait until we run the main loop for it to be executed. |
| 155 | EXPECT_EQ(0, calls.size()); |
| 156 | chromeos_update_engine::RunGMainLoopMaxIterations(100); |
| 157 | EXPECT_EQ(1, calls.size()); |
| 158 | } |
| 159 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 160 | } // namespace chromeos_update_manager |