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