blob: a00cd379114331d358e6c7b027172e02f9d0a26f [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>
25
26#include "update_engine/dbus_constants.h"
27#include "update_engine/fake_system_state.h"
28
29using std::string;
30using testing::Return;
31using testing::SetArgumentPointee;
32using testing::_;
33
34using chromeos::errors::dbus::kDomain;
35
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(
72 &error_, "app_ver", "url", kAttemptUpdateFlagNonInteractive));
73 EXPECT_EQ(nullptr, error_);
74}
75
76// SetChannel is allowed when there's no device policy (the device is not
77// enterprise enrolled).
78TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
79 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
80 // If SetTargetChannel is called it means the policy check passed.
81 EXPECT_CALL(*fake_system_state_.mock_request_params(),
82 SetTargetChannel("stable-channel", true))
83 .WillOnce(Return(true));
84 EXPECT_TRUE(dbus_service_.SetChannel(&error_, "stable-channel", true));
85 ASSERT_EQ(nullptr, error_);
86}
87
88// When the policy is present, the delegated value should be checked.
89TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
90 policy::MockDevicePolicy mock_device_policy;
91 fake_system_state_.set_device_policy(&mock_device_policy);
92 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
93 .WillOnce(DoAll(SetArgumentPointee<0>(true), Return(true)));
94 EXPECT_CALL(*fake_system_state_.mock_request_params(),
95 SetTargetChannel("beta-channel", true))
96 .WillOnce(Return(true));
97
98 EXPECT_TRUE(dbus_service_.SetChannel(&error_, "beta-channel", true));
99 ASSERT_EQ(nullptr, error_);
100}
101
102// When passing an invalid value (SetTargetChannel fails) an error should be
103// raised.
104TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
105 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
106 EXPECT_CALL(*fake_system_state_.mock_request_params(),
107 SetTargetChannel("foo-channel", true)).WillOnce(Return(false));
108
109 EXPECT_FALSE(dbus_service_.SetChannel(&error_, "foo-channel", true));
110 ASSERT_NE(nullptr, error_);
111 EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed));
112}
113
114TEST_F(UpdateEngineServiceTest, GetChannel) {
115 fake_system_state_.mock_request_params()->set_current_channel("current");
116 fake_system_state_.mock_request_params()->set_target_channel("target");
117 string channel;
118 EXPECT_TRUE(dbus_service_.GetChannel(
119 &error_, true /* get_current_channel */, &channel));
120 EXPECT_EQ(nullptr, error_);
121 EXPECT_EQ("current", channel);
122
123 EXPECT_TRUE(dbus_service_.GetChannel(
124 &error_, false /* get_current_channel */, &channel));
125 EXPECT_EQ(nullptr, error_);
126 EXPECT_EQ("target", channel);
127}
128
129TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
130 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
131 EXPECT_TRUE(dbus_service_.ResetStatus(&error_));
132 EXPECT_EQ(nullptr, error_);
133}
134
135TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
136 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
137 EXPECT_FALSE(dbus_service_.ResetStatus(&error_));
138 ASSERT_NE(nullptr, error_);
139 EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed));
140}
141
142} // namespace chromeos_update_engine