blob: a6b00140738c4513b444e5205c1caaf81f44985a [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/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"
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/cros/fake_system_state.h"
29#include "update_engine/cros/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:
Amin Hassani538bd592020-11-04 20:46:08 -080042 UpdateEngineServiceTest() = default;
Alex Deymob7ca0962014-10-01 17:58:07 -070043
Amin Hassani538bd592020-11-04 20:46:08 -080044 void SetUp() override {
45 FakeSystemState::CreateInstance();
46 FakeSystemState::Get()->set_device_policy(nullptr);
47 mock_update_attempter_ = FakeSystemState::Get()->mock_update_attempter();
48 }
Alex Deymob7ca0962014-10-01 17:58:07 -070049
Alex Deymob7ca0962014-10-01 17:58:07 -070050 MockUpdateAttempter* mock_update_attempter_;
51
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070052 brillo::ErrorPtr error_;
Casey Dahlina93cd532016-01-14 16:55:11 -080053 UpdateEngineService common_service_;
Alex Deymob7ca0962014-10-01 17:58:07 -070054};
55
56TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
Aaron Wood081c0232017-10-19 17:14:58 -070057 EXPECT_CALL(
58 *mock_update_attempter_,
59 CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kFlagNonInteractive))
60 .WillOnce(Return(true));
61
62 // The non-interactive flag needs to be passed through to CheckForUpdate.
63 bool result = false;
64 EXPECT_TRUE(
65 common_service_.AttemptUpdate(&error_,
66 "app_ver",
67 "url",
68 UpdateAttemptFlags::kFlagNonInteractive,
69 &result));
Alex Deymob7ca0962014-10-01 17:58:07 -070070 EXPECT_EQ(nullptr, error_);
Aaron Wood081c0232017-10-19 17:14:58 -070071 EXPECT_TRUE(result);
72}
73
74TEST_F(UpdateEngineServiceTest, AttemptUpdateReturnsFalse) {
75 EXPECT_CALL(*mock_update_attempter_,
76 CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kNone))
77 .WillOnce(Return(false));
78 bool result = true;
79 EXPECT_TRUE(common_service_.AttemptUpdate(
80 &error_, "app_ver", "url", UpdateAttemptFlags::kNone, &result));
81 EXPECT_EQ(nullptr, error_);
82 EXPECT_FALSE(result);
Alex Deymob7ca0962014-10-01 17:58:07 -070083}
84
Xiaochu Liu88d90382018-08-29 16:09:11 -070085TEST_F(UpdateEngineServiceTest, AttemptInstall) {
86 EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _))
87 .WillOnce(Return(true));
88
89 EXPECT_TRUE(common_service_.AttemptInstall(&error_, "", {}));
90 EXPECT_EQ(nullptr, error_);
91}
92
93TEST_F(UpdateEngineServiceTest, AttemptInstallReturnsFalse) {
94 EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _))
95 .WillOnce(Return(false));
96
97 EXPECT_FALSE(common_service_.AttemptInstall(&error_, "", {}));
98}
99
Andrewa8d7df32020-03-15 20:10:01 -0700100TEST_F(UpdateEngineServiceTest, SetDlcActiveValue) {
101 EXPECT_CALL(*mock_update_attempter_, SetDlcActiveValue(_, _))
102 .WillOnce(Return(true));
103
104 EXPECT_TRUE(common_service_.SetDlcActiveValue(&error_, true, "dlc0"));
105}
106
107TEST_F(UpdateEngineServiceTest, SetDlcActiveValueReturnsFalse) {
108 EXPECT_CALL(*mock_update_attempter_, SetDlcActiveValue(_, _))
109 .WillOnce(Return(false));
110
111 EXPECT_FALSE(common_service_.SetDlcActiveValue(&error_, true, "dlc0"));
112}
113
Alex Deymob7ca0962014-10-01 17:58:07 -0700114// SetChannel is allowed when there's no device policy (the device is not
115// enterprise enrolled).
116TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
117 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
118 // If SetTargetChannel is called it means the policy check passed.
Amin Hassani538bd592020-11-04 20:46:08 -0800119 EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -0800120 SetTargetChannel("stable-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -0700121 .WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -0800122 EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700123 ASSERT_EQ(nullptr, error_);
124}
125
126// When the policy is present, the delegated value should be checked.
127TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
128 policy::MockDevicePolicy mock_device_policy;
Amin Hassani538bd592020-11-04 20:46:08 -0800129 FakeSystemState::Get()->set_device_policy(&mock_device_policy);
Alex Deymob7ca0962014-10-01 17:58:07 -0700130 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
Ben Chan672c1f52017-10-23 15:41:39 -0700131 .WillOnce(DoAll(SetArgPointee<0>(true), Return(true)));
Amin Hassani538bd592020-11-04 20:46:08 -0800132 EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -0800133 SetTargetChannel("beta-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -0700134 .WillOnce(Return(true));
135
Casey Dahlina93cd532016-01-14 16:55:11 -0800136 EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700137 ASSERT_EQ(nullptr, error_);
138}
139
140// When passing an invalid value (SetTargetChannel fails) an error should be
141// raised.
142TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
143 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
Amin Hassani538bd592020-11-04 20:46:08 -0800144 EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800145 SetTargetChannel("foo-channel", true, _))
146 .WillOnce(Return(false));
Alex Deymob7ca0962014-10-01 17:58:07 -0700147
Casey Dahlina93cd532016-01-14 16:55:11 -0800148 EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700149 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800150 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
151 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700152}
153
154TEST_F(UpdateEngineServiceTest, GetChannel) {
Amin Hassani538bd592020-11-04 20:46:08 -0800155 FakeSystemState::Get()->mock_request_params()->set_current_channel("current");
156 FakeSystemState::Get()->mock_request_params()->set_target_channel("target");
Alex Deymob7ca0962014-10-01 17:58:07 -0700157 string channel;
Casey Dahlina93cd532016-01-14 16:55:11 -0800158 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700159 &error_, true /* get_current_channel */, &channel));
160 EXPECT_EQ(nullptr, error_);
161 EXPECT_EQ("current", channel);
162
Casey Dahlina93cd532016-01-14 16:55:11 -0800163 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700164 &error_, false /* get_current_channel */, &channel));
165 EXPECT_EQ(nullptr, error_);
166 EXPECT_EQ("target", channel);
167}
168
169TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
170 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -0800171 EXPECT_TRUE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700172 EXPECT_EQ(nullptr, error_);
173}
174
175TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
176 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
Casey Dahlina93cd532016-01-14 16:55:11 -0800177 EXPECT_FALSE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700178 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800179 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
180 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700181}
182
Alex Deymob7ca0962014-10-01 17:58:07 -0700183} // namespace chromeos_update_engine