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 //
16
17 #include "update_engine/cros/common_service.h"
18
19 #include <gtest/gtest.h>
20 #include <string>
21 #include <vector>
22
23 #include <brillo/errors/error.h>
24 #include <policy/libpolicy.h>
25 #include <policy/mock_device_policy.h>
26
27 #include "update_engine/cros/fake_system_state.h"
28 #include "update_engine/cros/omaha_utils.h"
29
30 using std::string;
31 using std::vector;
32 using testing::_;
33 using testing::Return;
34 using testing::SetArgPointee;
35 using update_engine::UpdateAttemptFlags;
36
37 namespace chromeos_update_engine {
38
39 class UpdateEngineServiceTest : public ::testing::Test {
40 protected:
41 UpdateEngineServiceTest() = default;
42
SetUp()43 void SetUp() override {
44 FakeSystemState::CreateInstance();
45 FakeSystemState::Get()->set_device_policy(nullptr);
46 mock_update_attempter_ = FakeSystemState::Get()->mock_update_attempter();
47 }
48
49 MockUpdateAttempter* mock_update_attempter_;
50
51 brillo::ErrorPtr error_;
52 UpdateEngineService common_service_;
53 };
54
TEST_F(UpdateEngineServiceTest,AttemptUpdate)55 TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
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));
69 EXPECT_EQ(nullptr, error_);
70 EXPECT_TRUE(result);
71 }
72
TEST_F(UpdateEngineServiceTest,AttemptUpdateReturnsFalse)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);
82 }
83
TEST_F(UpdateEngineServiceTest,AttemptInstall)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
TEST_F(UpdateEngineServiceTest,AttemptInstallReturnsFalse)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
TEST_F(UpdateEngineServiceTest,SetDlcActiveValue)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
TEST_F(UpdateEngineServiceTest,SetDlcActiveValueReturnsFalse)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
113 // SetChannel is allowed when there's no device policy (the device is not
114 // enterprise enrolled).
TEST_F(UpdateEngineServiceTest,SetChannelWithNoPolicy)115 TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
116 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
117 // If SetTargetChannel is called it means the policy check passed.
118 EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
119 SetTargetChannel("stable-channel", true, _))
120 .WillOnce(Return(true));
121 EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
122 ASSERT_EQ(nullptr, error_);
123 }
124
125 // When the policy is present, the delegated value should be checked.
TEST_F(UpdateEngineServiceTest,SetChannelWithDelegatedPolicy)126 TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
127 policy::MockDevicePolicy mock_device_policy;
128 FakeSystemState::Get()->set_device_policy(&mock_device_policy);
129 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
130 .WillOnce(DoAll(SetArgPointee<0>(true), Return(true)));
131 EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
132 SetTargetChannel("beta-channel", true, _))
133 .WillOnce(Return(true));
134
135 EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
136 ASSERT_EQ(nullptr, error_);
137 }
138
139 // When passing an invalid value (SetTargetChannel fails) an error should be
140 // raised.
TEST_F(UpdateEngineServiceTest,SetChannelWithInvalidChannel)141 TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
142 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
143 EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
144 SetTargetChannel("foo-channel", true, _))
145 .WillOnce(Return(false));
146
147 EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
148 ASSERT_NE(nullptr, error_);
149 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
150 UpdateEngineService::kErrorFailed));
151 }
152
TEST_F(UpdateEngineServiceTest,GetChannel)153 TEST_F(UpdateEngineServiceTest, GetChannel) {
154 FakeSystemState::Get()->mock_request_params()->set_current_channel("current");
155 FakeSystemState::Get()->mock_request_params()->set_target_channel("target");
156 string channel;
157 EXPECT_TRUE(common_service_.GetChannel(
158 &error_, true /* get_current_channel */, &channel));
159 EXPECT_EQ(nullptr, error_);
160 EXPECT_EQ("current", channel);
161
162 EXPECT_TRUE(common_service_.GetChannel(
163 &error_, false /* get_current_channel */, &channel));
164 EXPECT_EQ(nullptr, error_);
165 EXPECT_EQ("target", channel);
166 }
167
TEST_F(UpdateEngineServiceTest,ResetStatusSucceeds)168 TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
169 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
170 EXPECT_TRUE(common_service_.ResetStatus(&error_));
171 EXPECT_EQ(nullptr, error_);
172 }
173
TEST_F(UpdateEngineServiceTest,ResetStatusFails)174 TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
175 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
176 EXPECT_FALSE(common_service_.ResetStatus(&error_));
177 ASSERT_NE(nullptr, error_);
178 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
179 UpdateEngineService::kErrorFailed));
180 }
181
182 } // namespace chromeos_update_engine
183