Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 16 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 17 | #include "update_engine/common_service.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <string> |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 21 | #include <vector> |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 22 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 23 | #include <brillo/errors/error.h> |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 24 | #include <policy/libpolicy.h> |
| 25 | #include <policy/mock_device_policy.h> |
| 26 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 27 | #include "update_engine/common/fake_prefs.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 28 | #include "update_engine/fake_system_state.h" |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 29 | #include "update_engine/omaha_utils.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 30 | |
| 31 | using std::string; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 32 | using std::vector; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 33 | using testing::_; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 34 | using testing::Return; |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 35 | using testing::SetArgPointee; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 36 | using update_engine::UpdateAttemptFlags; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | class UpdateEngineServiceTest : public ::testing::Test { |
| 41 | protected: |
| 42 | UpdateEngineServiceTest() |
| 43 | : mock_update_attempter_(fake_system_state_.mock_update_attempter()), |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 44 | common_service_(&fake_system_state_) {} |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 45 | |
| 46 | void SetUp() override { |
| 47 | fake_system_state_.set_device_policy(nullptr); |
| 48 | } |
| 49 | |
| 50 | // Fake/mock infrastructure. |
| 51 | FakeSystemState fake_system_state_; |
| 52 | policy::MockDevicePolicy mock_device_policy_; |
| 53 | |
| 54 | // Shortcut for fake_system_state_.mock_update_attempter(). |
| 55 | MockUpdateAttempter* mock_update_attempter_; |
| 56 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 57 | brillo::ErrorPtr error_; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 58 | UpdateEngineService common_service_; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_F(UpdateEngineServiceTest, AttemptUpdate) { |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 62 | EXPECT_CALL( |
| 63 | *mock_update_attempter_, |
| 64 | CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kFlagNonInteractive)) |
| 65 | .WillOnce(Return(true)); |
| 66 | |
| 67 | // The non-interactive flag needs to be passed through to CheckForUpdate. |
| 68 | bool result = false; |
| 69 | EXPECT_TRUE( |
| 70 | common_service_.AttemptUpdate(&error_, |
| 71 | "app_ver", |
| 72 | "url", |
| 73 | UpdateAttemptFlags::kFlagNonInteractive, |
| 74 | &result)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 75 | EXPECT_EQ(nullptr, error_); |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(result); |
| 77 | } |
| 78 | |
| 79 | TEST_F(UpdateEngineServiceTest, AttemptUpdateReturnsFalse) { |
| 80 | EXPECT_CALL(*mock_update_attempter_, |
| 81 | CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kNone)) |
| 82 | .WillOnce(Return(false)); |
| 83 | bool result = true; |
| 84 | EXPECT_TRUE(common_service_.AttemptUpdate( |
| 85 | &error_, "app_ver", "url", UpdateAttemptFlags::kNone, &result)); |
| 86 | EXPECT_EQ(nullptr, error_); |
| 87 | EXPECT_FALSE(result); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 90 | TEST_F(UpdateEngineServiceTest, AttemptInstall) { |
| 91 | EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _)) |
| 92 | .WillOnce(Return(true)); |
| 93 | |
| 94 | EXPECT_TRUE(common_service_.AttemptInstall(&error_, "", {})); |
| 95 | EXPECT_EQ(nullptr, error_); |
| 96 | } |
| 97 | |
| 98 | TEST_F(UpdateEngineServiceTest, AttemptInstallReturnsFalse) { |
| 99 | EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _)) |
| 100 | .WillOnce(Return(false)); |
| 101 | |
| 102 | EXPECT_FALSE(common_service_.AttemptInstall(&error_, "", {})); |
| 103 | } |
| 104 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 105 | // SetChannel is allowed when there's no device policy (the device is not |
| 106 | // enterprise enrolled). |
| 107 | TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) { |
| 108 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); |
| 109 | // If SetTargetChannel is called it means the policy check passed. |
| 110 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 111 | SetTargetChannel("stable-channel", true, _)) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 112 | .WillOnce(Return(true)); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 113 | EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 114 | ASSERT_EQ(nullptr, error_); |
| 115 | } |
| 116 | |
| 117 | // When the policy is present, the delegated value should be checked. |
| 118 | TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) { |
| 119 | policy::MockDevicePolicy mock_device_policy; |
| 120 | fake_system_state_.set_device_policy(&mock_device_policy); |
| 121 | EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_)) |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 122 | .WillOnce(DoAll(SetArgPointee<0>(true), Return(true))); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 123 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 124 | SetTargetChannel("beta-channel", true, _)) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 125 | .WillOnce(Return(true)); |
| 126 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 127 | EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 128 | ASSERT_EQ(nullptr, error_); |
| 129 | } |
| 130 | |
| 131 | // When passing an invalid value (SetTargetChannel fails) an error should be |
| 132 | // raised. |
| 133 | TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) { |
| 134 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); |
| 135 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 136 | SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 137 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 138 | EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 139 | ASSERT_NE(nullptr, error_); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 140 | EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain, |
| 141 | UpdateEngineService::kErrorFailed)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | TEST_F(UpdateEngineServiceTest, GetChannel) { |
| 145 | fake_system_state_.mock_request_params()->set_current_channel("current"); |
| 146 | fake_system_state_.mock_request_params()->set_target_channel("target"); |
| 147 | string channel; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 148 | EXPECT_TRUE(common_service_.GetChannel( |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 149 | &error_, true /* get_current_channel */, &channel)); |
| 150 | EXPECT_EQ(nullptr, error_); |
| 151 | EXPECT_EQ("current", channel); |
| 152 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 153 | EXPECT_TRUE(common_service_.GetChannel( |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 154 | &error_, false /* get_current_channel */, &channel)); |
| 155 | EXPECT_EQ(nullptr, error_); |
| 156 | EXPECT_EQ("target", channel); |
| 157 | } |
| 158 | |
| 159 | TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) { |
| 160 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true)); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 161 | EXPECT_TRUE(common_service_.ResetStatus(&error_)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 162 | EXPECT_EQ(nullptr, error_); |
| 163 | } |
| 164 | |
| 165 | TEST_F(UpdateEngineServiceTest, ResetStatusFails) { |
| 166 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false)); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 167 | EXPECT_FALSE(common_service_.ResetStatus(&error_)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 168 | ASSERT_NE(nullptr, error_); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 169 | EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain, |
| 170 | UpdateEngineService::kErrorFailed)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 173 | TEST_F(UpdateEngineServiceTest, GetEolStatusTest) { |
| 174 | FakePrefs fake_prefs; |
| 175 | fake_system_state_.set_prefs(&fake_prefs); |
| 176 | // The default value should be "supported". |
| 177 | int32_t eol_status = static_cast<int32_t>(EolStatus::kEol); |
| 178 | EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status)); |
| 179 | EXPECT_EQ(nullptr, error_); |
| 180 | EXPECT_EQ(EolStatus::kSupported, static_cast<EolStatus>(eol_status)); |
| 181 | |
| 182 | fake_prefs.SetString(kPrefsOmahaEolStatus, "security-only"); |
| 183 | EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status)); |
| 184 | EXPECT_EQ(nullptr, error_); |
| 185 | EXPECT_EQ(EolStatus::kSecurityOnly, static_cast<EolStatus>(eol_status)); |
| 186 | } |
| 187 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 188 | } // namespace chromeos_update_engine |