omaha_request_params_unittest.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // Copyright (C) 2011 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/omaha_request_params.h"
  17. #include <stdio.h>
  18. #include <string>
  19. #include <base/files/file_util.h>
  20. #include <base/files/scoped_temp_dir.h>
  21. #include <gtest/gtest.h>
  22. #include "update_engine/common/constants.h"
  23. #include "update_engine/common/fake_prefs.h"
  24. #include "update_engine/common/platform_constants.h"
  25. #include "update_engine/common/test_utils.h"
  26. #include "update_engine/common/utils.h"
  27. #include "update_engine/fake_system_state.h"
  28. using chromeos_update_engine::test_utils::WriteFileString;
  29. using std::string;
  30. namespace chromeos_update_engine {
  31. class OmahaRequestParamsTest : public ::testing::Test {
  32. public:
  33. OmahaRequestParamsTest() : params_(&fake_system_state_) {}
  34. protected:
  35. void SetUp() override {
  36. // Create a uniquely named test directory.
  37. ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
  38. params_.set_root(tempdir_.GetPath().value());
  39. SetLockDown(false);
  40. fake_system_state_.set_prefs(&fake_prefs_);
  41. }
  42. void SetLockDown(bool locked_down) {
  43. fake_system_state_.fake_hardware()->SetIsOfficialBuild(locked_down);
  44. fake_system_state_.fake_hardware()->SetIsNormalBootMode(locked_down);
  45. }
  46. FakeSystemState fake_system_state_;
  47. OmahaRequestParams params_{&fake_system_state_};
  48. FakePrefs fake_prefs_;
  49. base::ScopedTempDir tempdir_;
  50. };
  51. namespace {
  52. string GetMachineType() {
  53. string machine_type;
  54. if (!utils::ReadPipe("uname -m", &machine_type))
  55. return "";
  56. // Strip anything from the first newline char.
  57. size_t newline_pos = machine_type.find('\n');
  58. if (newline_pos != string::npos)
  59. machine_type.erase(newline_pos);
  60. return machine_type;
  61. }
  62. } // namespace
  63. TEST_F(OmahaRequestParamsTest, MissingChannelTest) {
  64. EXPECT_TRUE(params_.Init("", "", false));
  65. // By default, if no channel is set, we should track the stable-channel.
  66. EXPECT_EQ("stable-channel", params_.target_channel());
  67. }
  68. TEST_F(OmahaRequestParamsTest, ForceVersionTest) {
  69. EXPECT_TRUE(params_.Init("ForcedVersion", "", false));
  70. EXPECT_EQ(string("ForcedVersion_") + GetMachineType(), params_.os_sp());
  71. EXPECT_EQ("ForcedVersion", params_.app_version());
  72. }
  73. TEST_F(OmahaRequestParamsTest, ForcedURLTest) {
  74. EXPECT_TRUE(params_.Init("", "http://forced.google.com", false));
  75. EXPECT_EQ("http://forced.google.com", params_.update_url());
  76. }
  77. TEST_F(OmahaRequestParamsTest, MissingURLTest) {
  78. EXPECT_TRUE(params_.Init("", "", false));
  79. EXPECT_EQ(constants::kOmahaDefaultProductionURL, params_.update_url());
  80. }
  81. TEST_F(OmahaRequestParamsTest, DeltaOKTest) {
  82. EXPECT_TRUE(params_.Init("", "", false));
  83. EXPECT_TRUE(params_.delta_okay());
  84. }
  85. TEST_F(OmahaRequestParamsTest, NoDeltasTest) {
  86. ASSERT_TRUE(
  87. WriteFileString(tempdir_.GetPath().Append(".nodelta").value(), ""));
  88. EXPECT_TRUE(params_.Init("", "", false));
  89. EXPECT_FALSE(params_.delta_okay());
  90. }
  91. TEST_F(OmahaRequestParamsTest, SetTargetChannelTest) {
  92. {
  93. OmahaRequestParams params(&fake_system_state_);
  94. params.set_root(tempdir_.GetPath().value());
  95. EXPECT_TRUE(params.Init("", "", false));
  96. EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
  97. EXPECT_FALSE(params.mutable_image_props_.is_powerwash_allowed);
  98. }
  99. params_.set_root(tempdir_.GetPath().value());
  100. EXPECT_TRUE(params_.Init("", "", false));
  101. EXPECT_EQ("canary-channel", params_.target_channel());
  102. EXPECT_FALSE(params_.mutable_image_props_.is_powerwash_allowed);
  103. }
  104. TEST_F(OmahaRequestParamsTest, SetIsPowerwashAllowedTest) {
  105. {
  106. OmahaRequestParams params(&fake_system_state_);
  107. params.set_root(tempdir_.GetPath().value());
  108. EXPECT_TRUE(params.Init("", "", false));
  109. EXPECT_TRUE(params.SetTargetChannel("canary-channel", true, nullptr));
  110. EXPECT_TRUE(params.mutable_image_props_.is_powerwash_allowed);
  111. }
  112. params_.set_root(tempdir_.GetPath().value());
  113. EXPECT_TRUE(params_.Init("", "", false));
  114. EXPECT_EQ("canary-channel", params_.target_channel());
  115. EXPECT_TRUE(params_.mutable_image_props_.is_powerwash_allowed);
  116. }
  117. TEST_F(OmahaRequestParamsTest, SetTargetChannelInvalidTest) {
  118. {
  119. OmahaRequestParams params(&fake_system_state_);
  120. params.set_root(tempdir_.GetPath().value());
  121. SetLockDown(true);
  122. EXPECT_TRUE(params.Init("", "", false));
  123. params.image_props_.allow_arbitrary_channels = false;
  124. string error_message;
  125. EXPECT_FALSE(
  126. params.SetTargetChannel("dogfood-channel", true, &error_message));
  127. // The error message should include a message about the valid channels.
  128. EXPECT_NE(string::npos, error_message.find("stable-channel"));
  129. EXPECT_FALSE(params.mutable_image_props_.is_powerwash_allowed);
  130. }
  131. params_.set_root(tempdir_.GetPath().value());
  132. EXPECT_TRUE(params_.Init("", "", false));
  133. EXPECT_EQ("stable-channel", params_.target_channel());
  134. EXPECT_FALSE(params_.mutable_image_props_.is_powerwash_allowed);
  135. }
  136. TEST_F(OmahaRequestParamsTest, IsValidChannelTest) {
  137. EXPECT_TRUE(params_.IsValidChannel("canary-channel"));
  138. EXPECT_TRUE(params_.IsValidChannel("stable-channel"));
  139. EXPECT_TRUE(params_.IsValidChannel("beta-channel"));
  140. EXPECT_TRUE(params_.IsValidChannel("dev-channel"));
  141. EXPECT_FALSE(params_.IsValidChannel("testimage-channel"));
  142. EXPECT_FALSE(params_.IsValidChannel("dogfood-channel"));
  143. EXPECT_FALSE(params_.IsValidChannel("some-channel"));
  144. EXPECT_FALSE(params_.IsValidChannel(""));
  145. params_.image_props_.allow_arbitrary_channels = true;
  146. EXPECT_TRUE(params_.IsValidChannel("some-channel"));
  147. EXPECT_FALSE(params_.IsValidChannel("wrong-suffix"));
  148. EXPECT_FALSE(params_.IsValidChannel(""));
  149. }
  150. TEST_F(OmahaRequestParamsTest, SetTargetChannelWorks) {
  151. params_.set_target_channel("dev-channel");
  152. EXPECT_EQ("dev-channel", params_.target_channel());
  153. // When an invalid value is set, it should be ignored.
  154. EXPECT_FALSE(params_.SetTargetChannel("invalid-channel", false, nullptr));
  155. EXPECT_EQ("dev-channel", params_.target_channel());
  156. // When set to a valid value, it should take effect.
  157. EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr));
  158. EXPECT_EQ("beta-channel", params_.target_channel());
  159. // When set to the same value, it should be idempotent.
  160. EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr));
  161. EXPECT_EQ("beta-channel", params_.target_channel());
  162. // When set to a valid value while a change is already pending, it should
  163. // succeed.
  164. EXPECT_TRUE(params_.SetTargetChannel("stable-channel", true, nullptr));
  165. EXPECT_EQ("stable-channel", params_.target_channel());
  166. // Set a different channel in mutable_image_props_.
  167. params_.set_target_channel("stable-channel");
  168. // When set to a valid value while a change is already pending, it should
  169. // succeed.
  170. params_.Init("", "", false);
  171. EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr));
  172. // The target channel should reflect the change, but the download channel
  173. // should continue to retain the old value ...
  174. EXPECT_EQ("beta-channel", params_.target_channel());
  175. EXPECT_EQ("stable-channel", params_.download_channel());
  176. // ... until we update the download channel explicitly.
  177. params_.UpdateDownloadChannel();
  178. EXPECT_EQ("beta-channel", params_.download_channel());
  179. EXPECT_EQ("beta-channel", params_.target_channel());
  180. }
  181. TEST_F(OmahaRequestParamsTest, ChannelIndexTest) {
  182. int canary = params_.GetChannelIndex("canary-channel");
  183. int dev = params_.GetChannelIndex("dev-channel");
  184. int beta = params_.GetChannelIndex("beta-channel");
  185. int stable = params_.GetChannelIndex("stable-channel");
  186. EXPECT_LE(canary, dev);
  187. EXPECT_LE(dev, beta);
  188. EXPECT_LE(beta, stable);
  189. // testimage-channel or other names are not recognized, so index will be -1.
  190. int testimage = params_.GetChannelIndex("testimage-channel");
  191. int bogus = params_.GetChannelIndex("bogus-channel");
  192. EXPECT_EQ(-1, testimage);
  193. EXPECT_EQ(-1, bogus);
  194. }
  195. TEST_F(OmahaRequestParamsTest, ToMoreStableChannelFlagTest) {
  196. params_.image_props_.current_channel = "canary-channel";
  197. params_.download_channel_ = "stable-channel";
  198. EXPECT_TRUE(params_.ToMoreStableChannel());
  199. params_.image_props_.current_channel = "stable-channel";
  200. EXPECT_FALSE(params_.ToMoreStableChannel());
  201. params_.download_channel_ = "beta-channel";
  202. EXPECT_FALSE(params_.ToMoreStableChannel());
  203. }
  204. TEST_F(OmahaRequestParamsTest, ShouldPowerwashTest) {
  205. params_.mutable_image_props_.is_powerwash_allowed = false;
  206. EXPECT_FALSE(params_.ShouldPowerwash());
  207. params_.mutable_image_props_.is_powerwash_allowed = true;
  208. params_.image_props_.allow_arbitrary_channels = true;
  209. params_.image_props_.current_channel = "foo-channel";
  210. params_.download_channel_ = "bar-channel";
  211. EXPECT_TRUE(params_.ShouldPowerwash());
  212. params_.image_props_.allow_arbitrary_channels = false;
  213. params_.image_props_.current_channel = "canary-channel";
  214. params_.download_channel_ = "stable-channel";
  215. EXPECT_TRUE(params_.ShouldPowerwash());
  216. }
  217. TEST_F(OmahaRequestParamsTest, CollectECFWVersionsTest) {
  218. params_.hwid_ = string("STUMPY ALEX 12345");
  219. EXPECT_FALSE(params_.CollectECFWVersions());
  220. params_.hwid_ = string("SNOW 12345");
  221. EXPECT_TRUE(params_.CollectECFWVersions());
  222. }
  223. } // namespace chromeos_update_engine