blob: 17265b9bbe73dae58472e44dccdd16a112500f93 [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>
21
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070022#include <brillo/errors/error.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070023#include <policy/libpolicy.h>
24#include <policy/mock_device_policy.h>
25
Alex Deymob3fa53b2016-04-18 19:57:58 -070026#include "update_engine/common/fake_prefs.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070027#include "update_engine/fake_system_state.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070028#include "update_engine/omaha_utils.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070029
30using std::string;
31using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070032using testing::SetArgPointee;
Alex Deymob7ca0962014-10-01 17:58:07 -070033using testing::_;
Alex Deymob7ca0962014-10-01 17:58:07 -070034
35namespace chromeos_update_engine {
36
37class UpdateEngineServiceTest : public ::testing::Test {
38 protected:
39 UpdateEngineServiceTest()
40 : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
Casey Dahlina93cd532016-01-14 16:55:11 -080041 common_service_(&fake_system_state_) {}
Alex Deymob7ca0962014-10-01 17:58:07 -070042
43 void SetUp() override {
44 fake_system_state_.set_device_policy(nullptr);
45 }
46
47 // Fake/mock infrastructure.
48 FakeSystemState fake_system_state_;
49 policy::MockDevicePolicy mock_device_policy_;
50
51 // Shortcut for fake_system_state_.mock_update_attempter().
52 MockUpdateAttempter* mock_update_attempter_;
53
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070054 brillo::ErrorPtr error_;
Casey Dahlina93cd532016-01-14 16:55:11 -080055 UpdateEngineService common_service_;
Alex Deymob7ca0962014-10-01 17:58:07 -070056};
57
58TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
Alex Deymob7ca0962014-10-01 17:58:07 -070059 EXPECT_CALL(*mock_update_attempter_, CheckForUpdate(
60 "app_ver", "url", false /* interactive */));
61 // The update is non-interactive when we pass the non-interactive flag.
Casey Dahlina93cd532016-01-14 16:55:11 -080062 EXPECT_TRUE(common_service_.AttemptUpdate(
Alex Deymod6deb1d2015-08-28 15:54:37 -070063 &error_, "app_ver", "url",
Casey Dahlina93cd532016-01-14 16:55:11 -080064 UpdateEngineService::kAttemptUpdateFlagNonInteractive));
Alex Deymob7ca0962014-10-01 17:58:07 -070065 EXPECT_EQ(nullptr, error_);
66}
67
68// SetChannel is allowed when there's no device policy (the device is not
69// enterprise enrolled).
70TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
71 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
72 // If SetTargetChannel is called it means the policy check passed.
73 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -080074 SetTargetChannel("stable-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -070075 .WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -080076 EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -070077 ASSERT_EQ(nullptr, error_);
78}
79
80// When the policy is present, the delegated value should be checked.
81TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
82 policy::MockDevicePolicy mock_device_policy;
83 fake_system_state_.set_device_policy(&mock_device_policy);
84 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
Ben Chan672c1f52017-10-23 15:41:39 -070085 .WillOnce(DoAll(SetArgPointee<0>(true), Return(true)));
Alex Deymob7ca0962014-10-01 17:58:07 -070086 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -080087 SetTargetChannel("beta-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -070088 .WillOnce(Return(true));
89
Casey Dahlina93cd532016-01-14 16:55:11 -080090 EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -070091 ASSERT_EQ(nullptr, error_);
92}
93
94// When passing an invalid value (SetTargetChannel fails) an error should be
95// raised.
96TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
97 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
98 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -080099 SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false));
Alex Deymob7ca0962014-10-01 17:58:07 -0700100
Casey Dahlina93cd532016-01-14 16:55:11 -0800101 EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700102 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800103 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
104 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700105}
106
107TEST_F(UpdateEngineServiceTest, GetChannel) {
108 fake_system_state_.mock_request_params()->set_current_channel("current");
109 fake_system_state_.mock_request_params()->set_target_channel("target");
110 string channel;
Casey Dahlina93cd532016-01-14 16:55:11 -0800111 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700112 &error_, true /* get_current_channel */, &channel));
113 EXPECT_EQ(nullptr, error_);
114 EXPECT_EQ("current", channel);
115
Casey Dahlina93cd532016-01-14 16:55:11 -0800116 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700117 &error_, false /* get_current_channel */, &channel));
118 EXPECT_EQ(nullptr, error_);
119 EXPECT_EQ("target", channel);
120}
121
122TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
123 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -0800124 EXPECT_TRUE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700125 EXPECT_EQ(nullptr, error_);
126}
127
128TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
129 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
Casey Dahlina93cd532016-01-14 16:55:11 -0800130 EXPECT_FALSE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700131 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800132 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
133 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700134}
135
Alex Deymob3fa53b2016-04-18 19:57:58 -0700136TEST_F(UpdateEngineServiceTest, GetEolStatusTest) {
137 FakePrefs fake_prefs;
138 fake_system_state_.set_prefs(&fake_prefs);
139 // The default value should be "supported".
140 int32_t eol_status = static_cast<int32_t>(EolStatus::kEol);
141 EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status));
142 EXPECT_EQ(nullptr, error_);
143 EXPECT_EQ(EolStatus::kSupported, static_cast<EolStatus>(eol_status));
144
145 fake_prefs.SetString(kPrefsOmahaEolStatus, "security-only");
146 EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status));
147 EXPECT_EQ(nullptr, error_);
148 EXPECT_EQ(EolStatus::kSecurityOnly, static_cast<EolStatus>(eol_status));
149}
150
Alex Deymob7ca0962014-10-01 17:58:07 -0700151} // namespace chromeos_update_engine