common_service_unittest.cc 6.6 KB

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