blob: 1c144d1be59f55c2c54417fbfed0765334d6f1d9 [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 Deymob7ca0962014-10-01 17:58:07 -070026#include "update_engine/fake_system_state.h"
27
28using std::string;
29using testing::Return;
30using testing::SetArgumentPointee;
31using testing::_;
Alex Deymob7ca0962014-10-01 17:58:07 -070032
33namespace chromeos_update_engine {
34
35class UpdateEngineServiceTest : public ::testing::Test {
36 protected:
37 UpdateEngineServiceTest()
38 : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
Casey Dahlina93cd532016-01-14 16:55:11 -080039 common_service_(&fake_system_state_) {}
Alex Deymob7ca0962014-10-01 17:58:07 -070040
41 void SetUp() override {
42 fake_system_state_.set_device_policy(nullptr);
43 }
44
45 // Fake/mock infrastructure.
46 FakeSystemState fake_system_state_;
47 policy::MockDevicePolicy mock_device_policy_;
48
49 // Shortcut for fake_system_state_.mock_update_attempter().
50 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) {
Alex Deymob7ca0962014-10-01 17:58:07 -070057 EXPECT_CALL(*mock_update_attempter_, CheckForUpdate(
58 "app_ver", "url", false /* interactive */));
59 // The update is non-interactive when we pass the non-interactive flag.
Casey Dahlina93cd532016-01-14 16:55:11 -080060 EXPECT_TRUE(common_service_.AttemptUpdate(
Alex Deymod6deb1d2015-08-28 15:54:37 -070061 &error_, "app_ver", "url",
Casey Dahlina93cd532016-01-14 16:55:11 -080062 UpdateEngineService::kAttemptUpdateFlagNonInteractive));
Alex Deymob7ca0962014-10-01 17:58:07 -070063 EXPECT_EQ(nullptr, error_);
64}
65
66// SetChannel is allowed when there's no device policy (the device is not
67// enterprise enrolled).
68TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
69 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
70 // If SetTargetChannel is called it means the policy check passed.
71 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -080072 SetTargetChannel("stable-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -070073 .WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -080074 EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -070075 ASSERT_EQ(nullptr, error_);
76}
77
78// When the policy is present, the delegated value should be checked.
79TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
80 policy::MockDevicePolicy mock_device_policy;
81 fake_system_state_.set_device_policy(&mock_device_policy);
82 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
83 .WillOnce(DoAll(SetArgumentPointee<0>(true), Return(true)));
84 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -080085 SetTargetChannel("beta-channel", true, _))
Alex Deymob7ca0962014-10-01 17:58:07 -070086 .WillOnce(Return(true));
87
Casey Dahlina93cd532016-01-14 16:55:11 -080088 EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -070089 ASSERT_EQ(nullptr, error_);
90}
91
92// When passing an invalid value (SetTargetChannel fails) an error should be
93// raised.
94TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
95 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
96 EXPECT_CALL(*fake_system_state_.mock_request_params(),
Alex Deymod942f9d2015-11-06 16:11:50 -080097 SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false));
Alex Deymob7ca0962014-10-01 17:58:07 -070098
Casey Dahlina93cd532016-01-14 16:55:11 -080099 EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
Alex Deymob7ca0962014-10-01 17:58:07 -0700100 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800101 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
102 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700103}
104
105TEST_F(UpdateEngineServiceTest, GetChannel) {
106 fake_system_state_.mock_request_params()->set_current_channel("current");
107 fake_system_state_.mock_request_params()->set_target_channel("target");
108 string channel;
Casey Dahlina93cd532016-01-14 16:55:11 -0800109 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700110 &error_, true /* get_current_channel */, &channel));
111 EXPECT_EQ(nullptr, error_);
112 EXPECT_EQ("current", channel);
113
Casey Dahlina93cd532016-01-14 16:55:11 -0800114 EXPECT_TRUE(common_service_.GetChannel(
Alex Deymob7ca0962014-10-01 17:58:07 -0700115 &error_, false /* get_current_channel */, &channel));
116 EXPECT_EQ(nullptr, error_);
117 EXPECT_EQ("target", channel);
118}
119
120TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
121 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
Casey Dahlina93cd532016-01-14 16:55:11 -0800122 EXPECT_TRUE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700123 EXPECT_EQ(nullptr, error_);
124}
125
126TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
127 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
Casey Dahlina93cd532016-01-14 16:55:11 -0800128 EXPECT_FALSE(common_service_.ResetStatus(&error_));
Alex Deymob7ca0962014-10-01 17:58:07 -0700129 ASSERT_NE(nullptr, error_);
Casey Dahlina93cd532016-01-14 16:55:11 -0800130 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
131 UpdateEngineService::kErrorFailed));
Alex Deymob7ca0962014-10-01 17:58:07 -0700132}
133
134} // namespace chromeos_update_engine