1 //
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 //
16
17 #include "update_engine/common_service.h"
18
19 #include <gtest/gtest.h>
20 #include <string>
21
22 #include <brillo/errors/error.h>
23 #include <policy/libpolicy.h>
24 #include <policy/mock_device_policy.h>
25
26 #include "update_engine/fake_system_state.h"
27
28 using std::string;
29 using testing::Return;
30 using testing::SetArgumentPointee;
31 using testing::_;
32
33 namespace chromeos_update_engine {
34
35 class UpdateEngineServiceTest : public ::testing::Test {
36 protected:
UpdateEngineServiceTest()37 UpdateEngineServiceTest()
38 : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
39 common_service_(&fake_system_state_) {}
40
SetUp()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
52 brillo::ErrorPtr error_;
53 UpdateEngineService common_service_;
54 };
55
TEST_F(UpdateEngineServiceTest,AttemptUpdate)56 TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
57 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.
60 EXPECT_TRUE(common_service_.AttemptUpdate(
61 &error_, "app_ver", "url",
62 UpdateEngineService::kAttemptUpdateFlagNonInteractive));
63 EXPECT_EQ(nullptr, error_);
64 }
65
66 // SetChannel is allowed when there's no device policy (the device is not
67 // enterprise enrolled).
TEST_F(UpdateEngineServiceTest,SetChannelWithNoPolicy)68 TEST_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(),
72 SetTargetChannel("stable-channel", true, _))
73 .WillOnce(Return(true));
74 EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
75 ASSERT_EQ(nullptr, error_);
76 }
77
78 // When the policy is present, the delegated value should be checked.
TEST_F(UpdateEngineServiceTest,SetChannelWithDelegatedPolicy)79 TEST_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(),
85 SetTargetChannel("beta-channel", true, _))
86 .WillOnce(Return(true));
87
88 EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
89 ASSERT_EQ(nullptr, error_);
90 }
91
92 // When passing an invalid value (SetTargetChannel fails) an error should be
93 // raised.
TEST_F(UpdateEngineServiceTest,SetChannelWithInvalidChannel)94 TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
95 EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
96 EXPECT_CALL(*fake_system_state_.mock_request_params(),
97 SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false));
98
99 EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
100 ASSERT_NE(nullptr, error_);
101 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
102 UpdateEngineService::kErrorFailed));
103 }
104
TEST_F(UpdateEngineServiceTest,GetChannel)105 TEST_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;
109 EXPECT_TRUE(common_service_.GetChannel(
110 &error_, true /* get_current_channel */, &channel));
111 EXPECT_EQ(nullptr, error_);
112 EXPECT_EQ("current", channel);
113
114 EXPECT_TRUE(common_service_.GetChannel(
115 &error_, false /* get_current_channel */, &channel));
116 EXPECT_EQ(nullptr, error_);
117 EXPECT_EQ("target", channel);
118 }
119
TEST_F(UpdateEngineServiceTest,ResetStatusSucceeds)120 TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
121 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
122 EXPECT_TRUE(common_service_.ResetStatus(&error_));
123 EXPECT_EQ(nullptr, error_);
124 }
125
TEST_F(UpdateEngineServiceTest,ResetStatusFails)126 TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
127 EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
128 EXPECT_FALSE(common_service_.ResetStatus(&error_));
129 ASSERT_NE(nullptr, error_);
130 EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
131 UpdateEngineService::kErrorFailed));
132 }
133
134 } // namespace chromeos_update_engine
135