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 | |
| 17 | #include "update_engine/dbus_service.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <string> |
| 21 | |
| 22 | #include <chromeos/errors/error.h> |
| 23 | #include <policy/libpolicy.h> |
| 24 | #include <policy/mock_device_policy.h> |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame^] | 25 | #include <update_engine/dbus-constants.h> |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 26 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 27 | #include "update_engine/fake_system_state.h" |
| 28 | |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame^] | 29 | using chromeos::errors::dbus::kDomain; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 30 | using std::string; |
| 31 | using testing::Return; |
| 32 | using testing::SetArgumentPointee; |
| 33 | using testing::_; |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame^] | 34 | using update_engine::kUpdateEngineServiceErrorFailed; |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
| 38 | class UpdateEngineServiceTest : public ::testing::Test { |
| 39 | protected: |
| 40 | UpdateEngineServiceTest() |
| 41 | : mock_update_attempter_(fake_system_state_.mock_update_attempter()), |
| 42 | dbus_service_(&fake_system_state_) {} |
| 43 | |
| 44 | void SetUp() override { |
| 45 | fake_system_state_.set_device_policy(nullptr); |
| 46 | } |
| 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 | chromeos::ErrorPtr error_; |
| 56 | UpdateEngineService dbus_service_; |
| 57 | }; |
| 58 | |
| 59 | TEST_F(UpdateEngineServiceTest, AttemptUpdate) { |
| 60 | // Simple test to ensure that the default is an interactive check. |
| 61 | EXPECT_CALL(*mock_update_attempter_, |
| 62 | CheckForUpdate("app_ver", "url", true /* interactive */)); |
| 63 | EXPECT_TRUE(dbus_service_.AttemptUpdate(&error_, "app_ver", "url")); |
| 64 | EXPECT_EQ(nullptr, error_); |
| 65 | } |
| 66 | |
| 67 | TEST_F(UpdateEngineServiceTest, AttemptUpdateWithFlags) { |
| 68 | EXPECT_CALL(*mock_update_attempter_, CheckForUpdate( |
| 69 | "app_ver", "url", false /* interactive */)); |
| 70 | // The update is non-interactive when we pass the non-interactive flag. |
| 71 | EXPECT_TRUE(dbus_service_.AttemptUpdateWithFlags( |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame^] | 72 | &error_, "app_ver", "url", |
| 73 | update_engine::kAttemptUpdateFlagNonInteractive)); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 74 | EXPECT_EQ(nullptr, error_); |
| 75 | } |
| 76 | |
| 77 | // SetChannel is allowed when there's no device policy (the device is not |
| 78 | // enterprise enrolled). |
| 79 | TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) { |
| 80 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); |
| 81 | // If SetTargetChannel is called it means the policy check passed. |
| 82 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
| 83 | SetTargetChannel("stable-channel", true)) |
| 84 | .WillOnce(Return(true)); |
| 85 | EXPECT_TRUE(dbus_service_.SetChannel(&error_, "stable-channel", true)); |
| 86 | ASSERT_EQ(nullptr, error_); |
| 87 | } |
| 88 | |
| 89 | // When the policy is present, the delegated value should be checked. |
| 90 | TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) { |
| 91 | policy::MockDevicePolicy mock_device_policy; |
| 92 | fake_system_state_.set_device_policy(&mock_device_policy); |
| 93 | EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_)) |
| 94 | .WillOnce(DoAll(SetArgumentPointee<0>(true), Return(true))); |
| 95 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
| 96 | SetTargetChannel("beta-channel", true)) |
| 97 | .WillOnce(Return(true)); |
| 98 | |
| 99 | EXPECT_TRUE(dbus_service_.SetChannel(&error_, "beta-channel", true)); |
| 100 | ASSERT_EQ(nullptr, error_); |
| 101 | } |
| 102 | |
| 103 | // When passing an invalid value (SetTargetChannel fails) an error should be |
| 104 | // raised. |
| 105 | TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) { |
| 106 | EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy()); |
| 107 | EXPECT_CALL(*fake_system_state_.mock_request_params(), |
| 108 | SetTargetChannel("foo-channel", true)).WillOnce(Return(false)); |
| 109 | |
| 110 | EXPECT_FALSE(dbus_service_.SetChannel(&error_, "foo-channel", true)); |
| 111 | ASSERT_NE(nullptr, error_); |
| 112 | EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed)); |
| 113 | } |
| 114 | |
| 115 | TEST_F(UpdateEngineServiceTest, GetChannel) { |
| 116 | fake_system_state_.mock_request_params()->set_current_channel("current"); |
| 117 | fake_system_state_.mock_request_params()->set_target_channel("target"); |
| 118 | string channel; |
| 119 | EXPECT_TRUE(dbus_service_.GetChannel( |
| 120 | &error_, true /* get_current_channel */, &channel)); |
| 121 | EXPECT_EQ(nullptr, error_); |
| 122 | EXPECT_EQ("current", channel); |
| 123 | |
| 124 | EXPECT_TRUE(dbus_service_.GetChannel( |
| 125 | &error_, false /* get_current_channel */, &channel)); |
| 126 | EXPECT_EQ(nullptr, error_); |
| 127 | EXPECT_EQ("target", channel); |
| 128 | } |
| 129 | |
| 130 | TEST_F(UpdateEngineServiceTest, ResetStatusSucceeds) { |
| 131 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(true)); |
| 132 | EXPECT_TRUE(dbus_service_.ResetStatus(&error_)); |
| 133 | EXPECT_EQ(nullptr, error_); |
| 134 | } |
| 135 | |
| 136 | TEST_F(UpdateEngineServiceTest, ResetStatusFails) { |
| 137 | EXPECT_CALL(*mock_update_attempter_, ResetStatus()).WillOnce(Return(false)); |
| 138 | EXPECT_FALSE(dbus_service_.ResetStatus(&error_)); |
| 139 | ASSERT_NE(nullptr, error_); |
| 140 | EXPECT_TRUE(error_->HasError(kDomain, kUpdateEngineServiceErrorFailed)); |
| 141 | } |
| 142 | |
| 143 | } // namespace chromeos_update_engine |