• 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 
22 #include <brillo/errors/error.h>
23 #include <policy/libpolicy.h>
24 #include <policy/mock_device_policy.h>
25 
26 #include "update_engine/common/fake_prefs.h"
27 #include "update_engine/fake_system_state.h"
28 #include "update_engine/omaha_utils.h"
29 
30 using std::string;
31 using testing::Return;
32 using testing::SetArgumentPointee;
33 using testing::_;
34 
35 namespace chromeos_update_engine {
36 
37 class UpdateEngineServiceTest : public ::testing::Test {
38  protected:
UpdateEngineServiceTest()39   UpdateEngineServiceTest()
40       : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
41         common_service_(&fake_system_state_) {}
42 
SetUp()43   void SetUp() override {
44     fake_system_state_.set_device_policy(nullptr);
45   }
46 
47   // Fake/mock infrastructure.
48   FakeSystemState fake_system_state_;
49   policy::MockDevicePolicy mock_device_policy_;
50 
51   // Shortcut for fake_system_state_.mock_update_attempter().
52   MockUpdateAttempter* mock_update_attempter_;
53 
54   brillo::ErrorPtr error_;
55   UpdateEngineService common_service_;
56 };
57 
TEST_F(UpdateEngineServiceTest,AttemptUpdate)58 TEST_F(UpdateEngineServiceTest, AttemptUpdate) {
59   EXPECT_CALL(*mock_update_attempter_, CheckForUpdate(
60       "app_ver", "url", false /* interactive */));
61   // The update is non-interactive when we pass the non-interactive flag.
62   EXPECT_TRUE(common_service_.AttemptUpdate(
63       &error_, "app_ver", "url",
64       UpdateEngineService::kAttemptUpdateFlagNonInteractive));
65   EXPECT_EQ(nullptr, error_);
66 }
67 
68 // SetChannel is allowed when there's no device policy (the device is not
69 // enterprise enrolled).
TEST_F(UpdateEngineServiceTest,SetChannelWithNoPolicy)70 TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
71   EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
72   // If SetTargetChannel is called it means the policy check passed.
73   EXPECT_CALL(*fake_system_state_.mock_request_params(),
74               SetTargetChannel("stable-channel", true, _))
75       .WillOnce(Return(true));
76   EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
77   ASSERT_EQ(nullptr, error_);
78 }
79 
80 // When the policy is present, the delegated value should be checked.
TEST_F(UpdateEngineServiceTest,SetChannelWithDelegatedPolicy)81 TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
82   policy::MockDevicePolicy mock_device_policy;
83   fake_system_state_.set_device_policy(&mock_device_policy);
84   EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
85       .WillOnce(DoAll(SetArgumentPointee<0>(true), Return(true)));
86   EXPECT_CALL(*fake_system_state_.mock_request_params(),
87               SetTargetChannel("beta-channel", true, _))
88       .WillOnce(Return(true));
89 
90   EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true));
91   ASSERT_EQ(nullptr, error_);
92 }
93 
94 // When passing an invalid value (SetTargetChannel fails) an error should be
95 // raised.
TEST_F(UpdateEngineServiceTest,SetChannelWithInvalidChannel)96 TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
97   EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
98   EXPECT_CALL(*fake_system_state_.mock_request_params(),
99               SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false));
100 
101   EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true));
102   ASSERT_NE(nullptr, error_);
103   EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
104                                UpdateEngineService::kErrorFailed));
105 }
106 
TEST_F(UpdateEngineServiceTest,GetChannel)107 TEST_F(UpdateEngineServiceTest, GetChannel) {
108   fake_system_state_.mock_request_params()->set_current_channel("current");
109   fake_system_state_.mock_request_params()->set_target_channel("target");
110   string channel;
111   EXPECT_TRUE(common_service_.GetChannel(
112       &error_, true /* get_current_channel */, &channel));
113   EXPECT_EQ(nullptr, error_);
114   EXPECT_EQ("current", channel);
115 
116   EXPECT_TRUE(common_service_.GetChannel(
117       &error_, false /* get_current_channel */, &channel));
118   EXPECT_EQ(nullptr, error_);
119   EXPECT_EQ("target", channel);
120 }
121 
TEST_F(UpdateEngineServiceTest,ResetStatusSucceeds)122 TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) {
123   EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true));
124   EXPECT_TRUE(common_service_.ResetStatus(&error_));
125   EXPECT_EQ(nullptr, error_);
126 }
127 
TEST_F(UpdateEngineServiceTest,ResetStatusFails)128 TEST_F(UpdateEngineServiceTest, ResetStatusFails) {
129   EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false));
130   EXPECT_FALSE(common_service_.ResetStatus(&error_));
131   ASSERT_NE(nullptr, error_);
132   EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain,
133                                UpdateEngineService::kErrorFailed));
134 }
135 
TEST_F(UpdateEngineServiceTest,GetEolStatusTest)136 TEST_F(UpdateEngineServiceTest, GetEolStatusTest) {
137   FakePrefs fake_prefs;
138   fake_system_state_.set_prefs(&fake_prefs);
139   // The default value should be "supported".
140   int32_t eol_status = static_cast<int32_t>(EolStatus::kEol);
141   EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status));
142   EXPECT_EQ(nullptr, error_);
143   EXPECT_EQ(EolStatus::kSupported, static_cast<EolStatus>(eol_status));
144 
145   fake_prefs.SetString(kPrefsOmahaEolStatus, "security-only");
146   EXPECT_TRUE(common_service_.GetEolStatus(&error_, &eol_status));
147   EXPECT_EQ(nullptr, error_);
148   EXPECT_EQ(EolStatus::kSecurityOnly, static_cast<EolStatus>(eol_status));
149 }
150 
151 }  // namespace chromeos_update_engine
152