blob: 824ea71d4e40dfce6e0475fcf4dbb08caaae3e7b [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
17#include "update_engine/dbus_service.h"
18
19#include <gtest/gtest.h>
20#include <string>
21
22#include <chromeos/errors/error.h>
23#include <policy/libpolicy.h>
24#include <policy/mock_device_policy.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070025#include <update_engine/dbus-constants.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070026
Alex Deymob7ca0962014-10-01 17:58:07 -070027#include "update_engine/fake_system_state.h"
28
Alex Deymod6deb1d2015-08-28 15:54:37 -070029using chromeos::errors::dbus::kDomain;
Alex Deymob7ca0962014-10-01 17:58:07 -070030using std::string;
31using testing::Return;
32using testing::SetArgumentPointee;
33using testing::_;
Alex Deymod6deb1d2015-08-28 15:54:37 -070034using update_engine::kUpdateEngineServiceErrorFailed;
Alex Deymob7ca0962014-10-01 17:58:07 -070035
36namespace chromeos_update_engine {
37
38class UpdateEngineServiceTest : public ::testing::Test {
39 protected:
40 UpdateEngineServiceTest()
41 : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
42 dbus_service_(&fake_system_state_) {}
43
44 void SetUp() override {
45 fake_system_state_.set_device_policy(nullptr);
46 }
47
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
55 chromeos::ErrorPtr error_;
56 UpdateEngineService dbus_service_;
57};
58
59TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
60 // Simple test to ensure that the default is an interactive check.
61 EXPECT_CALL(*mock_update_attempter_,
62 CheckForUpdate("app_ver", "url", true /* interactive */));
63 EXPECT_TRUE(dbus_service_.AttemptUpdate(&error_, "app_ver", "url"));
64 EXPECT_EQ(nullptr, error_);
65}
66
67TEST_F(UpdateEngineServiceTest, AttemptUpdateWithFlags) {
68 EXPECT_CALL(*mock_update_attempter_, CheckForUpdate(
69 "app_ver", "url", false /* interactive */));
70 // The update is non-interactive when we pass the non-interactive flag.
71 EXPECT_TRUE(dbus_service_.AttemptUpdateWithFlags(
Alex Deymod6deb1d2015-08-28 15:54:37 -070072 &error_, "app_ver", "url",
73 update_engine::kAttemptUpdateFlagNonInteractive));
Alex Deymob7ca0962014-10-01 17:58:07 -070074 EXPECT_EQ(nullptr, error_);
75}
76
77// SetChannel is allowed when there's no device policy (the device is not
78// enterprise enrolled).
79TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
80 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
81 // If SetTargetChannel is called it means the policy check passed.
82 EXPECT_CALL(*fake_system_state_.mock_request_params(),
83 SetTargetChannel("stable-channel", true))
84 .WillOnce(Return(true));
85 EXPECT_TRUE(dbus_service_.SetChannel(&error_, "stable-channel", true));
86 ASSERT_EQ(nullptr, error_);
87}
88
89// When the policy is present, the delegated value should be checked.
90TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
91 policy::MockDevicePolicy mock_device_policy;
92 fake_system_state_.set_device_policy(&mock_device_policy);
93 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
94 .WillOnce(DoAll(SetArgumentPointee<0>(true), Return(true)));
95 EXPECT_CALL(*fake_system_state_.mock_request_params(),
96 SetTargetChannel("beta-channel", true))
97 .WillOnce(Return(true));
98
99 EXPECT_TRUE(dbus_service_.SetChannel(&error_, "beta-channel", true));
100 ASSERT_EQ(nullptr, error_);
101}
102
103// When passing an invalid value (SetTargetChannel fails) an error should be
104// raised.
105TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
106 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
107 EXPECT_CALL(*fake_system_state_.mock_request_params(),
108 SetTargetChannel("foo-channel", true)).WillOnce(Return(false));
109
110 EXPECT_FALSE(dbus_service_.SetChannel(&error_, "foo-channel", true));
111 ASSERT_NE(nullptr, error_);
112 EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed));
113}
114
115TEST_F(UpdateEngineServiceTest, GetChannel) {
116 fake_system_state_.mock_request_params()->set_current_channel("current");
117 fake_system_state_.mock_request_params()->set_target_channel("target");
118 string channel;
119 EXPECT_TRUE(dbus_service_.GetChannel(
120 &error_, true /* get_current_channel */, &channel));
121 EXPECT_EQ(nullptr, error_);
122 EXPECT_EQ("current", channel);
123
124 EXPECT_TRUE(dbus_service_.GetChannel(
125 &error_, false /* get_current_channel */, &channel));
126 EXPECT_EQ(nullptr, error_);
127 EXPECT_EQ("target", channel);
128}
129
130TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
131 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
132 EXPECT_TRUE(dbus_service_.ResetStatus(&error_));
133 EXPECT_EQ(nullptr, error_);
134}
135
136TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
137 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
138 EXPECT_FALSE(dbus_service_.ResetStatus(&error_));
139 ASSERT_NE(nullptr, error_);
140 EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed));
141}
142
143} // namespace chromeos_update_engine