• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <vector>
22 
23 #include <brillo/errors/error.h>
24 #include <policy/libpolicy.h>
25 #include <policy/mock_device_policy.h>
26 
27 #include "update_engine/common/fake_prefs.h"
28 #include "update_engine/fake_system_state.h"
29 #include "update_engine/omaha_utils.h"
30 
31 using std::string;
32 using std::vector;
33 using testing::_;
34 using testing::Return;
35 using testing::SetArgPointee;
36 using update_engine::UpdateAttemptFlags;
37 
38 namespace chromeos_update_engine {
39 
40 class UpdateEngineServiceTest : public ::testing::Test {
41  protected:
UpdateEngineServiceTest()42   UpdateEngineServiceTest()
43       : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
44         common_service_(&fake_system_state_) {}
45 
SetUp()46   void SetUp() override { fake_system_state_.set_device_policy(nullptr); }
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   brillo::ErrorPtr error_;
56   UpdateEngineService common_service_;
57 };
58 
TEST_F(UpdateEngineServiceTest,AttemptUpdate)59 TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
60   EXPECT_CALL(
61       *mock_update_attempter_,
62       CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kFlagNonInteractive))
63       .WillOnce(Return(true));
64 
65   // The non-interactive flag needs to be passed through to CheckForUpdate.
66   bool result = false;
67   EXPECT_TRUE(
68       common_service_.AttemptUpdate(&error_,
69                                     "app_ver",
70                                     "url",
71                                     UpdateAttemptFlags::kFlagNonInteractive,
72                                     &result));
73   EXPECT_EQ(nullptr, error_);
74   EXPECT_TRUE(result);
75 }
76 
TEST_F(UpdateEngineServiceTest,AttemptUpdateReturnsFalse)77 TEST_F(UpdateEngineServiceTest, AttemptUpdateReturnsFalse) {
78   EXPECT_CALL(*mock_update_attempter_,
79               CheckForUpdate("app_ver", "url", UpdateAttemptFlags::kNone))
80       .WillOnce(Return(false));
81   bool result = true;
82   EXPECT_TRUE(common_service_.AttemptUpdate(
83       &error_, "app_ver", "url", UpdateAttemptFlags::kNone, &result));
84   EXPECT_EQ(nullptr, error_);
85   EXPECT_FALSE(result);
86 }
87 
TEST_F(UpdateEngineServiceTest,AttemptInstall)88 TEST_F(UpdateEngineServiceTest, AttemptInstall) {
89   EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _))
90       .WillOnce(Return(true));
91 
92   EXPECT_TRUE(common_service_.AttemptInstall(&error_, "", {}));
93   EXPECT_EQ(nullptr, error_);
94 }
95 
TEST_F(UpdateEngineServiceTest,AttemptInstallReturnsFalse)96 TEST_F(UpdateEngineServiceTest, AttemptInstallReturnsFalse) {
97   EXPECT_CALL(*mock_update_attempter_, CheckForInstall(_, _))
98       .WillOnce(Return(false));
99 
100   EXPECT_FALSE(common_service_.AttemptInstall(&error_, "", {}));
101 }
102 
103 // SetChannel is allowed when there's no device policy (the device is not
104 // enterprise enrolled).
TEST_F(UpdateEngineServiceTest,SetChannelWithNoPolicy)105 TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
106   EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
107   // If SetTargetChannel is called it means the policy check passed.
108   EXPECT_CALL(*fake_system_state_.mock_request_params(),
109               SetTargetChannel("stable-channel", true, _))
110       .WillOnce(Return(true));
111   EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
112   ASSERT_EQ(nullptr, error_);
113 }
114 
115 // When the policy is present, the delegated value should be checked.
TEST_F(UpdateEngineServiceTest,SetChannelWithDelegatedPolicy)116 TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
117   policy::MockDevicePolicy mock_device_policy;
118   fake_system_state_.set_device_policy(&mock_device_policy);
119   EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
120       .WillOnce(DoAll(SetArgPointee<0>(true), Return(true)));
121   EXPECT_CALL(*fake_system_state_.mock_request_params(),
122               SetTargetChannel("beta-channel", true, _))
123       .WillOnce(Return(true));
124 
125   EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
126   ASSERT_EQ(nullptr, error_);
127 }
128 
129 // When passing an invalid value (SetTargetChannel fails) an error should be
130 // raised.
TEST_F(UpdateEngineServiceTest,SetChannelWithInvalidChannel)131 TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
132   EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
133   EXPECT_CALL(*fake_system_state_.mock_request_params(),
134               SetTargetChannel("foo-channel", true, _))
135       .WillOnce(Return(false));
136 
137   EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
138   ASSERT_NE(nullptr, error_);
139   EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
140                                UpdateEngineService::kErrorFailed));
141 }
142 
TEST_F(UpdateEngineServiceTest,GetChannel)143 TEST_F(UpdateEngineServiceTest, GetChannel) {
144   fake_system_state_.mock_request_params()->set_current_channel("current");
145   fake_system_state_.mock_request_params()->set_target_channel("target");
146   string channel;
147   EXPECT_TRUE(common_service_.GetChannel(
148       &error_, true /* get_current_channel */, &channel));
149   EXPECT_EQ(nullptr, error_);
150   EXPECT_EQ("current", channel);
151 
152   EXPECT_TRUE(common_service_.GetChannel(
153       &error_, false /* get_current_channel */, &channel));
154   EXPECT_EQ(nullptr, error_);
155   EXPECT_EQ("target", channel);
156 }
157 
TEST_F(UpdateEngineServiceTest,ResetStatusSucceeds)158 TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
159   EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
160   EXPECT_TRUE(common_service_.ResetStatus(&error_));
161   EXPECT_EQ(nullptr, error_);
162 }
163 
TEST_F(UpdateEngineServiceTest,ResetStatusFails)164 TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
165   EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
166   EXPECT_FALSE(common_service_.ResetStatus(&error_));
167   ASSERT_NE(nullptr, error_);
168   EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
169                                UpdateEngineService::kErrorFailed));
170 }
171 
TEST_F(UpdateEngineServiceTest,GetEolStatusTest)172 TEST_F(UpdateEngineServiceTest, GetEolStatusTest) {
173   FakePrefs fake_prefs;
174   fake_system_state_.set_prefs(&fake_prefs);
175   // The default value should be "supported".
176   int32_t eol_status = static_cast<int32_t>(EolStatus::kEol);
177   EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status));
178   EXPECT_EQ(nullptr, error_);
179   EXPECT_EQ(EolStatus::kSupported, static_cast<EolStatus>(eol_status));
180 
181   fake_prefs.SetString(kPrefsOmahaEolStatus, "security-only");
182   EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status));
183   EXPECT_EQ(nullptr, error_);
184   EXPECT_EQ(EolStatus::kSecurityOnly, static_cast<EolStatus>(eol_status));
185 }
186 
187 }  // namespace chromeos_update_engine
188