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