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> |
| 21 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 22 | #include <brillo/errors/error.h> |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 23 | #include <policy/libpolicy.h> |
| 24 | #include <policy/mock_device_policy.h> |
| 25 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 26 | #include "update_engine/common/fake_prefs.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 27 | #include "update_engine/fake_system_state.h" |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 28 | #include "update_engine/omaha_utils.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 29 | |
| 30 | using std::string; |
Aaron Wood | 224dfc2 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 31 | using testing::_; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 32 | using testing::Return; |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 33 | using testing::SetArgPointee; |
Aaron Wood | 224dfc2 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 34 | using update_engine::UpdateAttemptFlags; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
| 38 | class UpdateEngineServiceTest : public ::testing::Test { |
| 39 | protected: |
| 40 | UpdateEngineServiceTest() |
| 41 | : mock_update_attempter_(fake_system_state_.mock_update_attempter()), |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 42 | common_service_(&fake_system_state_) {} |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 43 | |
| 44 | void SetUp() override { |
| 45 | fake_system_state_.set_device_policy(nullptr); |
| 46 | } |
| 47 | |
| 48 | // Fake/mock infrastructure. |
| 49 | FakeSystemState fake_system_state_; |
| 50 | policy::MockDevicePolicy mock_device_policy_; |
| 51 | |
| 52 | // Shortcut for fake_system_state_.mock_update_attempter(). |
| 53 | MockUpdateAttempter* mock_update_attempter_; |
| 54 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 55 | brillo::ErrorPtr error_; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 56 | UpdateEngineService common_service_; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | TEST_F(UpdateEngineServiceTest, AttemptUpdate) { |
Aaron Wood | 3a9c962 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 60 | EXPECT_CALL( |
| 61 | *mock_update_attempter_, |
| 62 | CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kFlagNonInteractive)) |
| 63 | .WillOnce(Return(true)); |
| 64 | |
| 65 | // The non-interactive flag needs to be passed through to CheckForUpdate. |
| 66 | bool result = false; |
| 67 | EXPECT_TRUE( |
| 68 | common_service_.AttemptUpdate(&error_, |
| 69 | "app_ver", |
| 70 | "url", |
| 71 | UpdateAttemptFlags::kFlagNonInteractive, |
| 72 | &result)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 73 | EXPECT_EQ(nullptr, error_); |
Aaron Wood | 3a9c962 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 74 | EXPECT_TRUE(result); |
| 75 | } |
| 76 | |
| 77 | TEST_F(UpdateEngineServiceTest, AttemptUpdateReturnsFalse) { |
| 78 | EXPECT_CALL(*mock_update_attempter_, |
| 79 | CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kNone)) |
| 80 | .WillOnce(Return(false)); |
| 81 | bool result = true; |
| 82 | EXPECT_TRUE(common_service_.AttemptUpdate( |
| 83 | &error_, "app_ver", "url", UpdateAttemptFlags::kNone, &result)); |
| 84 | EXPECT_EQ(nullptr, error_); |
| 85 | EXPECT_FALSE(result); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // SetChannel is allowed when there's no device policy (the device is not |
| 89 | // enterprise enrolled). |
| 90 | TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) { |
| 91 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); |
| 92 | // If SetTargetChannel is called it means the policy check passed. |
| 93 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 94 | SetTargetChannel("stable-channel", true, _)) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 95 | .WillOnce(Return(true)); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 96 | EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 97 | ASSERT_EQ(nullptr, error_); |
| 98 | } |
| 99 | |
| 100 | // When the policy is present, the delegated value should be checked. |
| 101 | TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) { |
| 102 | policy::MockDevicePolicy mock_device_policy; |
| 103 | fake_system_state_.set_device_policy(&mock_device_policy); |
| 104 | EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_)) |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 105 | .WillOnce(DoAll(SetArgPointee<0>(true), Return(true))); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 106 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 107 | SetTargetChannel("beta-channel", true, _)) |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 108 | .WillOnce(Return(true)); |
| 109 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 110 | EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 111 | ASSERT_EQ(nullptr, error_); |
| 112 | } |
| 113 | |
| 114 | // When passing an invalid value (SetTargetChannel fails) an error should be |
| 115 | // raised. |
| 116 | TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) { |
| 117 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); |
| 118 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 119 | SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 120 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 121 | EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 122 | ASSERT_NE(nullptr, error_); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 123 | EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain, |
| 124 | UpdateEngineService::kErrorFailed)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(UpdateEngineServiceTest, GetChannel) { |
| 128 | fake_system_state_.mock_request_params()->set_current_channel("current"); |
| 129 | fake_system_state_.mock_request_params()->set_target_channel("target"); |
| 130 | string channel; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 131 | EXPECT_TRUE(common_service_.GetChannel( |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 132 | &error_, true /* get_current_channel */, &channel)); |
| 133 | EXPECT_EQ(nullptr, error_); |
| 134 | EXPECT_EQ("current", channel); |
| 135 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 136 | EXPECT_TRUE(common_service_.GetChannel( |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 137 | &error_, false /* get_current_channel */, &channel)); |
| 138 | EXPECT_EQ(nullptr, error_); |
| 139 | EXPECT_EQ("target", channel); |
| 140 | } |
| 141 | |
| 142 | TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) { |
| 143 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true)); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 144 | EXPECT_TRUE(common_service_.ResetStatus(&error_)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 145 | EXPECT_EQ(nullptr, error_); |
| 146 | } |
| 147 | |
| 148 | TEST_F(UpdateEngineServiceTest, ResetStatusFails) { |
| 149 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false)); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 150 | EXPECT_FALSE(common_service_.ResetStatus(&error_)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 151 | ASSERT_NE(nullptr, error_); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 152 | EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain, |
| 153 | UpdateEngineService::kErrorFailed)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 156 | TEST_F(UpdateEngineServiceTest, GetEolStatusTest) { |
| 157 | FakePrefs fake_prefs; |
| 158 | fake_system_state_.set_prefs(&fake_prefs); |
| 159 | // The default value should be "supported". |
| 160 | int32_t eol_status = static_cast<int32_t>(EolStatus::kEol); |
| 161 | EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status)); |
| 162 | EXPECT_EQ(nullptr, error_); |
| 163 | EXPECT_EQ(EolStatus::kSupported, static_cast<EolStatus>(eol_status)); |
| 164 | |
| 165 | fake_prefs.SetString(kPrefsOmahaEolStatus, "security-only"); |
| 166 | EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status)); |
| 167 | EXPECT_EQ(nullptr, error_); |
| 168 | EXPECT_EQ(EolStatus::kSecurityOnly, static_cast<EolStatus>(eol_status)); |
| 169 | } |
| 170 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 171 | } // namespace chromeos_update_engine |