| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 16 |  | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 17 | #include "update_engine/common_service.h" | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <gtest/gtest.h> | 
|  | 20 | #include <string> | 
|  | 21 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 22 | #include <brillo/errors/error.h> | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 23 | #include <policy/libpolicy.h> | 
|  | 24 | #include <policy/mock_device_policy.h> | 
|  | 25 |  | 
| Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 26 | #include "update_engine/common/fake_prefs.h" | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 27 | #include "update_engine/fake_system_state.h" | 
| Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 28 | #include "update_engine/omaha_utils.h" | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | using std::string; | 
|  | 31 | using testing::Return; | 
|  | 32 | using testing::SetArgumentPointee; | 
|  | 33 | using testing::_; | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | namespace chromeos_update_engine { | 
|  | 36 |  | 
|  | 37 | class UpdateEngineServiceTest : public ::testing::Test { | 
|  | 38 | protected: | 
|  | 39 | UpdateEngineServiceTest() | 
|  | 40 | : mock_update_attempter_(fake_system_state_.mock_update_attempter()), | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 41 | common_service_(&fake_system_state_) {} | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 42 |  | 
|  | 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 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 54 | brillo::ErrorPtr error_; | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 55 | UpdateEngineService common_service_; | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
|  | 58 | TEST_F(UpdateEngineServiceTest, AttemptUpdate) { | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 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. | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 62 | EXPECT_TRUE(common_service_.AttemptUpdate( | 
| Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 63 | &error_, "app_ver", "url", | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 64 | UpdateEngineService::kAttemptUpdateFlagNonInteractive)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 65 | EXPECT_EQ(nullptr, error_); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // SetChannel is allowed when there's no device policy (the device is not | 
|  | 69 | // enterprise enrolled). | 
|  | 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(), | 
| Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 74 | SetTargetChannel("stable-channel", true, _)) | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 75 | .WillOnce(Return(true)); | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 76 | EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 77 | ASSERT_EQ(nullptr, error_); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | // When the policy is present, the delegated value should be checked. | 
|  | 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(), | 
| Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 87 | SetTargetChannel("beta-channel", true, _)) | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 88 | .WillOnce(Return(true)); | 
|  | 89 |  | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 90 | EXPECT_TRUE(common_service_.SetChannel(&error_, "beta-channel", true)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 91 | ASSERT_EQ(nullptr, error_); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | // When passing an invalid value (SetTargetChannel fails) an error should be | 
|  | 95 | // raised. | 
|  | 96 | TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) { | 
|  | 97 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); | 
|  | 98 | EXPECT_CALL(*fake_system_state_.mock_request_params(), | 
| Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 99 | SetTargetChannel("foo-channel", true, _)).WillOnce(Return(false)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 100 |  | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 101 | EXPECT_FALSE(common_service_.SetChannel(&error_, "foo-channel", true)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 102 | ASSERT_NE(nullptr, error_); | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 103 | EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain, | 
|  | 104 | UpdateEngineService::kErrorFailed)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 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; | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 111 | EXPECT_TRUE(common_service_.GetChannel( | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 112 | &error_, true /* get_current_channel */, &channel)); | 
|  | 113 | EXPECT_EQ(nullptr, error_); | 
|  | 114 | EXPECT_EQ("current", channel); | 
|  | 115 |  | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 116 | EXPECT_TRUE(common_service_.GetChannel( | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 117 | &error_, false /* get_current_channel */, &channel)); | 
|  | 118 | EXPECT_EQ(nullptr, error_); | 
|  | 119 | EXPECT_EQ("target", channel); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) { | 
|  | 123 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true)); | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 124 | EXPECT_TRUE(common_service_.ResetStatus(&error_)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 125 | EXPECT_EQ(nullptr, error_); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | TEST_F(UpdateEngineServiceTest, ResetStatusFails) { | 
|  | 129 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false)); | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 130 | EXPECT_FALSE(common_service_.ResetStatus(&error_)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 131 | ASSERT_NE(nullptr, error_); | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 132 | EXPECT_TRUE(error_->HasError(UpdateEngineService::kErrorDomain, | 
|  | 133 | UpdateEngineService::kErrorFailed)); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 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 |  | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 151 | }  // namespace chromeos_update_engine |