blob: 3dc8a2273e442ec1db4c2a8e05477a43a64d31dc [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymob7ca0962014-10-01 17:58:07 -070016
Casey Dahlina93cd532016-01-14 16:55:11 -080017#include "update_engine/common_service.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070018
19#include <gtest/gtest.h>
20#include <string>
Xiaochu Liu88d90382018-08-29 16:09:11 -070021#include <vector>
Alex Deymob7ca0962014-10-01 17:58:07 -070022
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070023#include <brillo/errors/error.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070024#include <policy/libpolicy.h>
25#include <policy/mock_device_policy.h>
26
Alex Deymob3fa53b2016-04-18 19:57:58 -070027#include "update_engine/common/fake_prefs.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070028#include "update_engine/fake_system_state.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070029#include "update_engine/omaha_utils.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070030
31using std::string;
Xiaochu Liu88d90382018-08-29 16:09:11 -070032using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070033using testing::_;
Alex Deymob7ca0962014-10-01 17:58:07 -070034using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070035using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070036using update_engine::UpdateAttemptFlags;
Alex Deymob7ca0962014-10-01 17:58:07 -070037
38namespace chromeos_update_engine {
39
40class UpdateEngineServiceTest : public ::testing::Test {
41 protected:
42 UpdateEngineServiceTest()
43 : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
Casey Dahlina93cd532016-01-14 16:55:11 -080044 common_service_(&fake_system_state_) {}
Alex Deymob7ca0962014-10-01 17:58:07 -070045
Amin Hassani7cc8bb02019-01-14 16:29:47 -080046 void SetUp() override { fake_system_state_.set_device_policy(nullptr); }
Alex Deymob7ca0962014-10-01 17:58:07 -070047
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 Vakulenko3f39d5c2015-10-13 09:27:13 -070055 brillo::ErrorPtr error_;
Casey Dahlina93cd532016-01-14 16:55:11 -080056 UpdateEngineService common_service_;
Alex Deymob7ca0962014-10-01 17:58:07 -070057};
58
59TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
Aaron Wood081c0232017-10-19 17:14:58 -070060 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 Deymob7ca0962014-10-01 17:58:07 -070073 EXPECT_EQ(nullptr, error_);
Aaron Wood081c0232017-10-19 17:14:58 -070074 EXPECT_TRUE(result);
75}
76
77TEST_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 Deymob7ca0962014-10-01 17:58:07 -070086}
87
Xiaochu Liu88d90382018-08-29 16:09:11 -070088TEST_F(UpdateEngineServiceTest, AttemptInstall) {
89 EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _))
90 .WillOnce(Return(true));
91
92 EXPECT_TRUE(common_service_.AttemptInstall(&error_, "", {}));
93 EXPECT_EQ(nullptr, error_);
94}
95
96TEST_F(UpdateEngineServiceTest, AttemptInstallReturnsFalse) {
97 EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _))
98 .WillOnce(Return(false));
99
100 EXPECT_FALSE(common_service_.AttemptInstall(&error_, "", {}));
101}
102
Andrewa8d7df32020-03-15 20:10:01 -0700103TEST_F(UpdateEngineServiceTest, SetDlcActiveValue) {
104 EXPECT_CALL(*mock_update_attempter_, SetDlcActiveValue(_, _))
105 .WillOnce(Return(true));
106
107 EXPECT_TRUE(common_service_.SetDlcActiveValue(&error_, true, "dlc0"));
108}
109
110TEST_F(UpdateEngineServiceTest, SetDlcActiveValueReturnsFalse) {
111 EXPECT_CALL(*mock_update_attempter_, SetDlcActiveValue(_, _))
112 .WillOnce(Return(false));
113
114 EXPECT_FALSE(common_service_.SetDlcActiveValue(&error_, true, "dlc0"));
115}
116
Alex Deymob7ca0962014-10-01 17:58:07 -0700117// SetChannel is allowed when there's no device policy (the device is not
118// enterprise enrolled).
119TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
120 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
121 // If SetTargetChannel is called it means the policy check passed.
122 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -0800123 SetTargetChannel("stable-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -0700124 .WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -0800125 EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700126 ASSERT_EQ(nullptr, error_);
127}
128
129// When the policy is present, the delegated value should be checked.
130TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
131 policy::MockDevicePolicy mock_device_policy;
132 fake_system_state_.set_device_policy(&mock_device_policy);
133 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
Ben Chan672c1f52017-10-23 15:41:39 -0700134 .WillOnce(DoAll(SetArgPointee<0>(true), Return(true)));
Alex Deymob7ca0962014-10-01 17:58:07 -0700135 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -0800136 SetTargetChannel("beta-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -0700137 .WillOnce(Return(true));
138
Casey Dahlina93cd532016-01-14 16:55:11 -0800139 EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700140 ASSERT_EQ(nullptr, error_);
141}
142
143// When passing an invalid value (SetTargetChannel fails) an error should be
144// raised.
145TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
146 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
147 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800148 SetTargetChannel("foo-channel", true, _))
149 .WillOnce(Return(false));
Alex Deymob7ca0962014-10-01 17:58:07 -0700150
Casey Dahlina93cd532016-01-14 16:55:11 -0800151 EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700152 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800153 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
154 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700155}
156
157TEST_F(UpdateEngineServiceTest, GetChannel) {
158 fake_system_state_.mock_request_params()->set_current_channel("current");
159 fake_system_state_.mock_request_params()->set_target_channel("target");
160 string channel;
Casey Dahlina93cd532016-01-14 16:55:11 -0800161 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700162 &error_, true /* get_current_channel */, &channel));
163 EXPECT_EQ(nullptr, error_);
164 EXPECT_EQ("current", channel);
165
Casey Dahlina93cd532016-01-14 16:55:11 -0800166 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700167 &error_, false /* get_current_channel */, &channel));
168 EXPECT_EQ(nullptr, error_);
169 EXPECT_EQ("target", channel);
170}
171
172TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
173 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -0800174 EXPECT_TRUE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700175 EXPECT_EQ(nullptr, error_);
176}
177
178TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
179 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
Casey Dahlina93cd532016-01-14 16:55:11 -0800180 EXPECT_FALSE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700181 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800182 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
183 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700184}
185
Alex Deymob7ca0962014-10-01 17:58:07 -0700186} // namespace chromeos_update_engine