blob: 441f89ded6bb3ba4a626a777fcefe508e58400f9 [file] [log] [blame]
Alex Deymob7ca0962014-10-01 17:58:07 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/dbus_service.h"
6
7#include <gtest/gtest.h>
8#include <string>
9
10#include <chromeos/errors/error.h>
11#include <policy/libpolicy.h>
12#include <policy/mock_device_policy.h>
13
14#include "update_engine/dbus_constants.h"
15#include "update_engine/fake_system_state.h"
16
17using std::string;
18using testing::Return;
19using testing::SetArgumentPointee;
20using testing::_;
21
22using chromeos::errors::dbus::kDomain;
23
24namespace chromeos_update_engine {
25
26class UpdateEngineServiceTest : public ::testing::Test {
27 protected:
28 UpdateEngineServiceTest()
29 : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
30 dbus_service_(&fake_system_state_) {}
31
32 void SetUp() override {
33 fake_system_state_.set_device_policy(nullptr);
34 }
35
36 // Fake/mock infrastructure.
37 FakeSystemState fake_system_state_;
38 policy::MockDevicePolicy mock_device_policy_;
39
40 // Shortcut for fake_system_state_.mock_update_attempter().
41 MockUpdateAttempter* mock_update_attempter_;
42
43 chromeos::ErrorPtr error_;
44 UpdateEngineService dbus_service_;
45};
46
47TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
48 // Simple test to ensure that the default is an interactive check.
49 EXPECT_CALL(*mock_update_attempter_,
50 CheckForUpdate("app_ver", "url", true /* interactive */));
51 EXPECT_TRUE(dbus_service_.AttemptUpdate(&error_, "app_ver", "url"));
52 EXPECT_EQ(nullptr, error_);
53}
54
55TEST_F(UpdateEngineServiceTest, AttemptUpdateWithFlags) {
56 EXPECT_CALL(*mock_update_attempter_, CheckForUpdate(
57 "app_ver", "url", false /* interactive */));
58 // The update is non-interactive when we pass the non-interactive flag.
59 EXPECT_TRUE(dbus_service_.AttemptUpdateWithFlags(
60 &error_, "app_ver", "url", kAttemptUpdateFlagNonInteractive));
61 EXPECT_EQ(nullptr, error_);
62}
63
64// SetChannel is allowed when there's no device policy (the device is not
65// enterprise enrolled).
66TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
67 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
68 // If SetTargetChannel is called it means the policy check passed.
69 EXPECT_CALL(*fake_system_state_.mock_request_params(),
70 SetTargetChannel("stable-channel", true))
71 .WillOnce(Return(true));
72 EXPECT_TRUE(dbus_service_.SetChannel(&error_, "stable-channel", true));
73 ASSERT_EQ(nullptr, error_);
74}
75
76// When the policy is present, the delegated value should be checked.
77TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
78 policy::MockDevicePolicy mock_device_policy;
79 fake_system_state_.set_device_policy(&mock_device_policy);
80 EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
81 .WillOnce(DoAll(SetArgumentPointee<0>(true), Return(true)));
82 EXPECT_CALL(*fake_system_state_.mock_request_params(),
83 SetTargetChannel("beta-channel", true))
84 .WillOnce(Return(true));
85
86 EXPECT_TRUE(dbus_service_.SetChannel(&error_, "beta-channel", true));
87 ASSERT_EQ(nullptr, error_);
88}
89
90// When passing an invalid value (SetTargetChannel fails) an error should be
91// raised.
92TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
93 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
94 EXPECT_CALL(*fake_system_state_.mock_request_params(),
95 SetTargetChannel("foo-channel", true)).WillOnce(Return(false));
96
97 EXPECT_FALSE(dbus_service_.SetChannel(&error_, "foo-channel", true));
98 ASSERT_NE(nullptr, error_);
99 EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed));
100}
101
102TEST_F(UpdateEngineServiceTest, GetChannel) {
103 fake_system_state_.mock_request_params()->set_current_channel("current");
104 fake_system_state_.mock_request_params()->set_target_channel("target");
105 string channel;
106 EXPECT_TRUE(dbus_service_.GetChannel(
107 &error_, true /* get_current_channel */, &channel));
108 EXPECT_EQ(nullptr, error_);
109 EXPECT_EQ("current", channel);
110
111 EXPECT_TRUE(dbus_service_.GetChannel(
112 &error_, false /* get_current_channel */, &channel));
113 EXPECT_EQ(nullptr, error_);
114 EXPECT_EQ("target", channel);
115}
116
117TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
118 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
119 EXPECT_TRUE(dbus_service_.ResetStatus(&error_));
120 EXPECT_EQ(nullptr, error_);
121}
122
123TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
124 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
125 EXPECT_FALSE(dbus_service_.ResetStatus(&error_));
126 ASSERT_NE(nullptr, error_);
127 EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed));
128}
129
130} // namespace chromeos_update_engine