omaha_response_handler_action_unittest.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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_response_handler_action.h"
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #include <base/files/file_util.h>
  21. #include <base/files/scoped_temp_dir.h>
  22. #include <brillo/message_loops/fake_message_loop.h>
  23. #include <gtest/gtest.h>
  24. #include "update_engine/common/constants.h"
  25. #include "update_engine/common/platform_constants.h"
  26. #include "update_engine/common/test_utils.h"
  27. #include "update_engine/common/utils.h"
  28. #include "update_engine/fake_system_state.h"
  29. #include "update_engine/mock_payload_state.h"
  30. #include "update_engine/payload_consumer/payload_constants.h"
  31. #include "update_engine/update_manager/mock_policy.h"
  32. using chromeos_update_engine::test_utils::System;
  33. using chromeos_update_engine::test_utils::WriteFileString;
  34. using chromeos_update_manager::EvalStatus;
  35. using chromeos_update_manager::FakeUpdateManager;
  36. using chromeos_update_manager::MockPolicy;
  37. using std::string;
  38. using testing::_;
  39. using testing::DoAll;
  40. using testing::Return;
  41. using testing::SetArgPointee;
  42. namespace chromeos_update_engine {
  43. class OmahaResponseHandlerActionProcessorDelegate
  44. : public ActionProcessorDelegate {
  45. public:
  46. OmahaResponseHandlerActionProcessorDelegate()
  47. : code_(ErrorCode::kError), code_set_(false) {}
  48. void ActionCompleted(ActionProcessor* processor,
  49. AbstractAction* action,
  50. ErrorCode code) {
  51. if (action->Type() == OmahaResponseHandlerAction::StaticType()) {
  52. auto response_handler_action =
  53. static_cast<OmahaResponseHandlerAction*>(action);
  54. code_ = code;
  55. code_set_ = true;
  56. response_handler_action_install_plan_.reset(
  57. new InstallPlan(response_handler_action->install_plan_));
  58. } else if (action->Type() ==
  59. ObjectCollectorAction<InstallPlan>::StaticType()) {
  60. auto collector_action =
  61. static_cast<ObjectCollectorAction<InstallPlan>*>(action);
  62. collector_action_install_plan_.reset(
  63. new InstallPlan(collector_action->object()));
  64. }
  65. }
  66. ErrorCode code_;
  67. bool code_set_;
  68. std::unique_ptr<InstallPlan> collector_action_install_plan_;
  69. std::unique_ptr<InstallPlan> response_handler_action_install_plan_;
  70. };
  71. class OmahaResponseHandlerActionTest : public ::testing::Test {
  72. protected:
  73. void SetUp() override {
  74. FakeBootControl* fake_boot_control = fake_system_state_.fake_boot_control();
  75. fake_boot_control->SetPartitionDevice(kPartitionNameKernel, 0, "/dev/sdz2");
  76. fake_boot_control->SetPartitionDevice(kPartitionNameRoot, 0, "/dev/sdz3");
  77. fake_boot_control->SetPartitionDevice(kPartitionNameKernel, 1, "/dev/sdz4");
  78. fake_boot_control->SetPartitionDevice(kPartitionNameRoot, 1, "/dev/sdz5");
  79. }
  80. // Return true iff the OmahaResponseHandlerAction succeeded.
  81. // If out is non-null, it's set w/ the response from the action.
  82. bool DoTest(const OmahaResponse& in,
  83. const string& deadline_file,
  84. InstallPlan* out);
  85. // Delegate passed to the ActionProcessor.
  86. OmahaResponseHandlerActionProcessorDelegate delegate_;
  87. // Captures the action's result code, for tests that need to directly verify
  88. // it in non-success cases.
  89. ErrorCode action_result_code_;
  90. FakeSystemState fake_system_state_;
  91. // "Hash+"
  92. const brillo::Blob expected_hash_ = {0x48, 0x61, 0x73, 0x68, 0x2b};
  93. };
  94. namespace {
  95. const char* const kLongName =
  96. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  97. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  98. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  99. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  100. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  101. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  102. "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
  103. "-the_update_a.b.c.d_DELTA_.tgz";
  104. const char* const kBadVersion = "don't update me";
  105. const char* const kPayloadHashHex = "486173682b";
  106. } // namespace
  107. bool OmahaResponseHandlerActionTest::DoTest(const OmahaResponse& in,
  108. const string& test_deadline_file,
  109. InstallPlan* out) {
  110. brillo::FakeMessageLoop loop(nullptr);
  111. loop.SetAsCurrent();
  112. ActionProcessor processor;
  113. processor.set_delegate(&delegate_);
  114. auto feeder_action = std::make_unique<ObjectFeederAction<OmahaResponse>>();
  115. feeder_action->set_obj(in);
  116. if (in.update_exists && in.version != kBadVersion) {
  117. string expected_hash;
  118. for (const auto& package : in.packages)
  119. expected_hash += package.hash + ":";
  120. EXPECT_CALL(*(fake_system_state_.mock_prefs()),
  121. SetString(kPrefsUpdateCheckResponseHash, expected_hash))
  122. .WillOnce(Return(true));
  123. int slot =
  124. fake_system_state_.request_params()->is_install()
  125. ? fake_system_state_.fake_boot_control()->GetCurrentSlot()
  126. : 1 - fake_system_state_.fake_boot_control()->GetCurrentSlot();
  127. string key = kPrefsChannelOnSlotPrefix + std::to_string(slot);
  128. EXPECT_CALL(*(fake_system_state_.mock_prefs()), SetString(key, testing::_))
  129. .WillOnce(Return(true));
  130. }
  131. string current_url = in.packages.size() ? in.packages[0].payload_urls[0] : "";
  132. EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
  133. .WillRepeatedly(Return(current_url));
  134. auto response_handler_action =
  135. std::make_unique<OmahaResponseHandlerAction>(&fake_system_state_);
  136. if (!test_deadline_file.empty())
  137. response_handler_action->deadline_file_ = test_deadline_file;
  138. auto collector_action =
  139. std::make_unique<ObjectCollectorAction<InstallPlan>>();
  140. BondActions(feeder_action.get(), response_handler_action.get());
  141. BondActions(response_handler_action.get(), collector_action.get());
  142. processor.EnqueueAction(std::move(feeder_action));
  143. processor.EnqueueAction(std::move(response_handler_action));
  144. processor.EnqueueAction(std::move(collector_action));
  145. processor.StartProcessing();
  146. EXPECT_TRUE(!processor.IsRunning())
  147. << "Update test to handle non-async actions";
  148. if (out && delegate_.collector_action_install_plan_)
  149. *out = *delegate_.collector_action_install_plan_;
  150. EXPECT_TRUE(delegate_.code_set_);
  151. action_result_code_ = delegate_.code_;
  152. return delegate_.code_ == ErrorCode::kSuccess;
  153. }
  154. TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
  155. test_utils::ScopedTempFile test_deadline_file(
  156. "omaha_response_handler_action_unittest-XXXXXX");
  157. {
  158. OmahaResponse in;
  159. in.update_exists = true;
  160. in.version = "a.b.c.d";
  161. in.packages.push_back(
  162. {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
  163. .size = 12,
  164. .hash = kPayloadHashHex});
  165. in.more_info_url = "http://more/info";
  166. in.prompt = false;
  167. in.deadline = "20101020";
  168. InstallPlan install_plan;
  169. EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
  170. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  171. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  172. EXPECT_EQ(1U, install_plan.target_slot);
  173. string deadline;
  174. EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
  175. EXPECT_EQ("20101020", deadline);
  176. struct stat deadline_stat;
  177. EXPECT_EQ(0, stat(test_deadline_file.path().c_str(), &deadline_stat));
  178. EXPECT_EQ(
  179. static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
  180. deadline_stat.st_mode);
  181. EXPECT_EQ(in.version, install_plan.version);
  182. }
  183. {
  184. OmahaResponse in;
  185. in.update_exists = true;
  186. in.version = "a.b.c.d";
  187. in.packages.push_back(
  188. {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
  189. .size = 12,
  190. .hash = kPayloadHashHex});
  191. in.more_info_url = "http://more/info";
  192. in.prompt = true;
  193. InstallPlan install_plan;
  194. // Set the other slot as current.
  195. fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
  196. EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
  197. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  198. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  199. EXPECT_EQ(0U, install_plan.target_slot);
  200. string deadline;
  201. EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline) &&
  202. deadline.empty());
  203. EXPECT_EQ(in.version, install_plan.version);
  204. }
  205. {
  206. OmahaResponse in;
  207. in.update_exists = true;
  208. in.version = "a.b.c.d";
  209. in.packages.push_back(
  210. {.payload_urls = {kLongName}, .size = 12, .hash = kPayloadHashHex});
  211. in.more_info_url = "http://more/info";
  212. in.prompt = true;
  213. in.deadline = "some-deadline";
  214. InstallPlan install_plan;
  215. fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
  216. // Because rollback happened, the deadline shouldn't be written into the
  217. // file.
  218. EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
  219. GetRollbackHappened())
  220. .WillOnce(Return(true));
  221. EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
  222. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  223. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  224. EXPECT_EQ(1U, install_plan.target_slot);
  225. string deadline;
  226. EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
  227. EXPECT_TRUE(deadline.empty());
  228. EXPECT_EQ(in.version, install_plan.version);
  229. }
  230. {
  231. OmahaResponse in;
  232. in.update_exists = true;
  233. in.version = "a.b.c.d";
  234. in.packages.push_back(
  235. {.payload_urls = {kLongName}, .size = 12, .hash = kPayloadHashHex});
  236. in.more_info_url = "http://more/info";
  237. in.prompt = true;
  238. in.deadline = "some-deadline";
  239. InstallPlan install_plan;
  240. fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
  241. EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
  242. GetRollbackHappened())
  243. .WillOnce(Return(false));
  244. EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
  245. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  246. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  247. EXPECT_EQ(1U, install_plan.target_slot);
  248. string deadline;
  249. EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
  250. EXPECT_EQ("some-deadline", deadline);
  251. EXPECT_EQ(in.version, install_plan.version);
  252. }
  253. }
  254. TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
  255. OmahaResponse in;
  256. in.update_exists = false;
  257. InstallPlan install_plan;
  258. EXPECT_FALSE(DoTest(in, "", &install_plan));
  259. EXPECT_TRUE(install_plan.partitions.empty());
  260. }
  261. TEST_F(OmahaResponseHandlerActionTest, InstallTest) {
  262. OmahaResponse in;
  263. in.update_exists = true;
  264. in.version = "a.b.c.d";
  265. in.packages.push_back(
  266. {.payload_urls = {kLongName}, .size = 1, .hash = kPayloadHashHex});
  267. in.packages.push_back(
  268. {.payload_urls = {kLongName}, .size = 2, .hash = kPayloadHashHex});
  269. in.more_info_url = "http://more/info";
  270. OmahaRequestParams params(&fake_system_state_);
  271. params.set_is_install(true);
  272. fake_system_state_.set_request_params(&params);
  273. InstallPlan install_plan;
  274. EXPECT_TRUE(DoTest(in, "", &install_plan));
  275. EXPECT_EQ(install_plan.source_slot, UINT_MAX);
  276. }
  277. TEST_F(OmahaResponseHandlerActionTest, MultiPackageTest) {
  278. OmahaResponse in;
  279. in.update_exists = true;
  280. in.version = "a.b.c.d";
  281. in.packages.push_back({.payload_urls = {"http://package/1"},
  282. .size = 1,
  283. .hash = kPayloadHashHex});
  284. in.packages.push_back({.payload_urls = {"http://package/2"},
  285. .size = 2,
  286. .hash = kPayloadHashHex});
  287. in.more_info_url = "http://more/info";
  288. InstallPlan install_plan;
  289. EXPECT_TRUE(DoTest(in, "", &install_plan));
  290. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  291. EXPECT_EQ(2u, install_plan.payloads.size());
  292. EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
  293. EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
  294. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  295. EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
  296. EXPECT_EQ(in.version, install_plan.version);
  297. }
  298. TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) {
  299. OmahaResponse in;
  300. in.update_exists = true;
  301. in.version = "a.b.c.d";
  302. in.packages.push_back(
  303. {.payload_urls = {"http://test.should/need/hash.checks.signed"},
  304. .size = 12,
  305. .hash = kPayloadHashHex});
  306. in.more_info_url = "http://more/info";
  307. // Hash checks are always skipped for non-official update URLs.
  308. EXPECT_CALL(*(fake_system_state_.mock_request_params()),
  309. IsUpdateUrlOfficial())
  310. .WillRepeatedly(Return(true));
  311. InstallPlan install_plan;
  312. EXPECT_TRUE(DoTest(in, "", &install_plan));
  313. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  314. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  315. EXPECT_TRUE(install_plan.hash_checks_mandatory);
  316. EXPECT_EQ(in.version, install_plan.version);
  317. }
  318. TEST_F(OmahaResponseHandlerActionTest, HashChecksForUnofficialUpdateUrl) {
  319. OmahaResponse in;
  320. in.update_exists = true;
  321. in.version = "a.b.c.d";
  322. in.packages.push_back(
  323. {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
  324. .size = 12,
  325. .hash = kPayloadHashHex});
  326. in.more_info_url = "http://more/info";
  327. EXPECT_CALL(*(fake_system_state_.mock_request_params()),
  328. IsUpdateUrlOfficial())
  329. .WillRepeatedly(Return(false));
  330. InstallPlan install_plan;
  331. EXPECT_TRUE(DoTest(in, "", &install_plan));
  332. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  333. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  334. EXPECT_FALSE(install_plan.hash_checks_mandatory);
  335. EXPECT_EQ(in.version, install_plan.version);
  336. }
  337. TEST_F(OmahaResponseHandlerActionTest,
  338. HashChecksForOfficialUrlUnofficialBuildTest) {
  339. // Official URLs for unofficial builds (dev/test images) don't require hash.
  340. OmahaResponse in;
  341. in.update_exists = true;
  342. in.version = "a.b.c.d";
  343. in.packages.push_back(
  344. {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
  345. .size = 12,
  346. .hash = kPayloadHashHex});
  347. in.more_info_url = "http://more/info";
  348. EXPECT_CALL(*(fake_system_state_.mock_request_params()),
  349. IsUpdateUrlOfficial())
  350. .WillRepeatedly(Return(true));
  351. fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
  352. InstallPlan install_plan;
  353. EXPECT_TRUE(DoTest(in, "", &install_plan));
  354. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  355. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  356. EXPECT_FALSE(install_plan.hash_checks_mandatory);
  357. EXPECT_EQ(in.version, install_plan.version);
  358. }
  359. TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) {
  360. OmahaResponse in;
  361. in.update_exists = true;
  362. in.version = "a.b.c.d";
  363. in.packages.push_back(
  364. {.payload_urls = {"https://test.should/need/hash.checks.signed"},
  365. .size = 12,
  366. .hash = kPayloadHashHex});
  367. in.more_info_url = "http://more/info";
  368. EXPECT_CALL(*(fake_system_state_.mock_request_params()),
  369. IsUpdateUrlOfficial())
  370. .WillRepeatedly(Return(true));
  371. InstallPlan install_plan;
  372. EXPECT_TRUE(DoTest(in, "", &install_plan));
  373. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  374. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  375. EXPECT_TRUE(install_plan.hash_checks_mandatory);
  376. EXPECT_EQ(in.version, install_plan.version);
  377. }
  378. TEST_F(OmahaResponseHandlerActionTest, HashChecksForBothHttpAndHttpsTest) {
  379. OmahaResponse in;
  380. in.update_exists = true;
  381. in.version = "a.b.c.d";
  382. in.packages.push_back(
  383. {.payload_urls = {"http://test.should.still/need/hash.checks",
  384. "https://test.should.still/need/hash.checks"},
  385. .size = 12,
  386. .hash = kPayloadHashHex});
  387. in.more_info_url = "http://more/info";
  388. EXPECT_CALL(*(fake_system_state_.mock_request_params()),
  389. IsUpdateUrlOfficial())
  390. .WillRepeatedly(Return(true));
  391. InstallPlan install_plan;
  392. EXPECT_TRUE(DoTest(in, "", &install_plan));
  393. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  394. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  395. EXPECT_TRUE(install_plan.hash_checks_mandatory);
  396. EXPECT_EQ(in.version, install_plan.version);
  397. }
  398. TEST_F(OmahaResponseHandlerActionTest, ChangeToMoreStableChannelTest) {
  399. OmahaResponse in;
  400. in.update_exists = true;
  401. in.version = "a.b.c.d";
  402. in.packages.push_back({.payload_urls = {"https://MoreStableChannelTest"},
  403. .size = 1,
  404. .hash = kPayloadHashHex});
  405. in.more_info_url = "http://more/info";
  406. // Create a uniquely named test directory.
  407. base::ScopedTempDir tempdir;
  408. ASSERT_TRUE(tempdir.CreateUniqueTempDir());
  409. OmahaRequestParams params(&fake_system_state_);
  410. fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
  411. params.set_root(tempdir.GetPath().value());
  412. params.set_current_channel("canary-channel");
  413. // The ImageProperties in Android uses prefs to store MutableImageProperties.
  414. #ifdef __ANDROID__
  415. EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, true))
  416. .WillOnce(Return(true));
  417. #endif // __ANDROID__
  418. EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
  419. params.UpdateDownloadChannel();
  420. EXPECT_TRUE(params.ShouldPowerwash());
  421. fake_system_state_.set_request_params(&params);
  422. InstallPlan install_plan;
  423. EXPECT_TRUE(DoTest(in, "", &install_plan));
  424. EXPECT_TRUE(install_plan.powerwash_required);
  425. }
  426. TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
  427. OmahaResponse in;
  428. in.update_exists = true;
  429. in.version = "a.b.c.d";
  430. in.packages.push_back({.payload_urls = {"https://LessStableChannelTest"},
  431. .size = 15,
  432. .hash = kPayloadHashHex});
  433. in.more_info_url = "http://more/info";
  434. // Create a uniquely named test directory.
  435. base::ScopedTempDir tempdir;
  436. ASSERT_TRUE(tempdir.CreateUniqueTempDir());
  437. OmahaRequestParams params(&fake_system_state_);
  438. fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
  439. params.set_root(tempdir.GetPath().value());
  440. params.set_current_channel("stable-channel");
  441. // The ImageProperties in Android uses prefs to store MutableImageProperties.
  442. #ifdef __ANDROID__
  443. EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, false))
  444. .WillOnce(Return(true));
  445. #endif // __ANDROID__
  446. EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
  447. params.UpdateDownloadChannel();
  448. EXPECT_FALSE(params.ShouldPowerwash());
  449. fake_system_state_.set_request_params(&params);
  450. InstallPlan install_plan;
  451. EXPECT_TRUE(DoTest(in, "", &install_plan));
  452. EXPECT_FALSE(install_plan.powerwash_required);
  453. }
  454. TEST_F(OmahaResponseHandlerActionTest, P2PUrlIsUsedAndHashChecksMandatory) {
  455. OmahaResponse in;
  456. in.update_exists = true;
  457. in.version = "a.b.c.d";
  458. in.packages.push_back(
  459. {.payload_urls = {"https://would.not/cause/hash/checks"},
  460. .size = 12,
  461. .hash = kPayloadHashHex});
  462. in.more_info_url = "http://more/info";
  463. OmahaRequestParams params(&fake_system_state_);
  464. // We're using a real OmahaRequestParams object here so we can't mock
  465. // IsUpdateUrlOfficial(), but setting the update URL to the AutoUpdate test
  466. // server will cause IsUpdateUrlOfficial() to return true.
  467. params.set_update_url(constants::kOmahaDefaultAUTestURL);
  468. fake_system_state_.set_request_params(&params);
  469. EXPECT_CALL(*fake_system_state_.mock_payload_state(),
  470. SetUsingP2PForDownloading(true));
  471. string p2p_url = "http://9.8.7.6/p2p";
  472. EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetP2PUrl())
  473. .WillRepeatedly(Return(p2p_url));
  474. EXPECT_CALL(*fake_system_state_.mock_payload_state(),
  475. GetUsingP2PForDownloading())
  476. .WillRepeatedly(Return(true));
  477. InstallPlan install_plan;
  478. EXPECT_TRUE(DoTest(in, "", &install_plan));
  479. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  480. EXPECT_EQ(p2p_url, install_plan.download_url);
  481. EXPECT_TRUE(install_plan.hash_checks_mandatory);
  482. }
  483. TEST_F(OmahaResponseHandlerActionTest, RollbackTest) {
  484. OmahaResponse in;
  485. in.update_exists = true;
  486. in.packages.push_back({.payload_urls = {"https://RollbackTest"},
  487. .size = 1,
  488. .hash = kPayloadHashHex});
  489. in.is_rollback = true;
  490. in.rollback_key_version.kernel = 1;
  491. in.rollback_key_version.kernel = 2;
  492. in.rollback_key_version.firmware_key = 3;
  493. in.rollback_key_version.firmware = 4;
  494. fake_system_state_.fake_hardware()->SetMinKernelKeyVersion(0x00010002);
  495. fake_system_state_.fake_hardware()->SetMinFirmwareKeyVersion(0x00030004);
  496. OmahaRequestParams params(&fake_system_state_);
  497. params.set_rollback_allowed(true);
  498. fake_system_state_.set_request_params(&params);
  499. InstallPlan install_plan;
  500. EXPECT_TRUE(DoTest(in, "", &install_plan));
  501. EXPECT_TRUE(install_plan.is_rollback);
  502. }
  503. TEST_F(OmahaResponseHandlerActionTest, RollbackKernelVersionErrorTest) {
  504. OmahaResponse in;
  505. in.update_exists = true;
  506. in.packages.push_back({.payload_urls = {"https://RollbackTest"},
  507. .size = 1,
  508. .hash = kPayloadHashHex});
  509. in.is_rollback = true;
  510. in.rollback_key_version.kernel_key = 1;
  511. in.rollback_key_version.kernel = 1; // This is lower than the minimum.
  512. in.rollback_key_version.firmware_key = 3;
  513. in.rollback_key_version.firmware = 4;
  514. fake_system_state_.fake_hardware()->SetMinKernelKeyVersion(0x00010002);
  515. fake_system_state_.fake_hardware()->SetMinFirmwareKeyVersion(0x00030004);
  516. OmahaRequestParams params(&fake_system_state_);
  517. params.set_rollback_allowed(true);
  518. fake_system_state_.set_request_params(&params);
  519. InstallPlan install_plan;
  520. EXPECT_FALSE(DoTest(in, "", &install_plan));
  521. }
  522. TEST_F(OmahaResponseHandlerActionTest, RollbackFirmwareVersionErrorTest) {
  523. OmahaResponse in;
  524. in.update_exists = true;
  525. in.packages.push_back({.payload_urls = {"https://RollbackTest"},
  526. .size = 1,
  527. .hash = kPayloadHashHex});
  528. in.is_rollback = true;
  529. in.rollback_key_version.kernel_key = 1;
  530. in.rollback_key_version.kernel = 2;
  531. in.rollback_key_version.firmware_key = 3;
  532. in.rollback_key_version.firmware = 3; // This is lower than the minimum.
  533. fake_system_state_.fake_hardware()->SetMinKernelKeyVersion(0x00010002);
  534. fake_system_state_.fake_hardware()->SetMinFirmwareKeyVersion(0x00030004);
  535. OmahaRequestParams params(&fake_system_state_);
  536. params.set_rollback_allowed(true);
  537. fake_system_state_.set_request_params(&params);
  538. InstallPlan install_plan;
  539. EXPECT_FALSE(DoTest(in, "", &install_plan));
  540. }
  541. TEST_F(OmahaResponseHandlerActionTest, RollbackNotRollbackTest) {
  542. OmahaResponse in;
  543. in.update_exists = true;
  544. in.packages.push_back({.payload_urls = {"https://RollbackTest"},
  545. .size = 1,
  546. .hash = kPayloadHashHex});
  547. in.is_rollback = false;
  548. OmahaRequestParams params(&fake_system_state_);
  549. params.set_rollback_allowed(true);
  550. fake_system_state_.set_request_params(&params);
  551. InstallPlan install_plan;
  552. EXPECT_TRUE(DoTest(in, "", &install_plan));
  553. EXPECT_FALSE(install_plan.is_rollback);
  554. }
  555. TEST_F(OmahaResponseHandlerActionTest, RollbackNotAllowedTest) {
  556. OmahaResponse in;
  557. in.update_exists = true;
  558. in.packages.push_back({.payload_urls = {"https://RollbackTest"},
  559. .size = 1,
  560. .hash = kPayloadHashHex});
  561. in.is_rollback = true;
  562. OmahaRequestParams params(&fake_system_state_);
  563. params.set_rollback_allowed(false);
  564. fake_system_state_.set_request_params(&params);
  565. InstallPlan install_plan;
  566. EXPECT_FALSE(DoTest(in, "", &install_plan));
  567. }
  568. TEST_F(OmahaResponseHandlerActionTest, SystemVersionTest) {
  569. OmahaResponse in;
  570. in.update_exists = true;
  571. in.version = "a.b.c.d";
  572. in.system_version = "b.c.d.e";
  573. in.packages.push_back({.payload_urls = {"http://package/1"},
  574. .size = 1,
  575. .hash = kPayloadHashHex});
  576. in.packages.push_back({.payload_urls = {"http://package/2"},
  577. .size = 2,
  578. .hash = kPayloadHashHex});
  579. in.more_info_url = "http://more/info";
  580. InstallPlan install_plan;
  581. EXPECT_TRUE(DoTest(in, "", &install_plan));
  582. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  583. EXPECT_EQ(2u, install_plan.payloads.size());
  584. EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
  585. EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
  586. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  587. EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
  588. EXPECT_EQ(in.version, install_plan.version);
  589. EXPECT_EQ(in.system_version, install_plan.system_version);
  590. }
  591. TEST_F(OmahaResponseHandlerActionTest, TestDeferredByPolicy) {
  592. OmahaResponse in;
  593. in.update_exists = true;
  594. in.version = "a.b.c.d";
  595. in.packages.push_back({.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
  596. .size = 12,
  597. .hash = kPayloadHashHex});
  598. // Setup the UpdateManager to disallow the update.
  599. FakeClock fake_clock;
  600. MockPolicy* mock_policy = new MockPolicy(&fake_clock);
  601. FakeUpdateManager* fake_update_manager =
  602. fake_system_state_.fake_update_manager();
  603. fake_update_manager->set_policy(mock_policy);
  604. EXPECT_CALL(*mock_policy, UpdateCanBeApplied(_, _, _, _, _))
  605. .WillOnce(
  606. DoAll(SetArgPointee<3>(ErrorCode::kOmahaUpdateDeferredPerPolicy),
  607. Return(EvalStatus::kSucceeded)));
  608. // Perform the Action. It should "fail" with kOmahaUpdateDeferredPerPolicy.
  609. InstallPlan install_plan;
  610. EXPECT_FALSE(DoTest(in, "", &install_plan));
  611. EXPECT_EQ(ErrorCode::kOmahaUpdateDeferredPerPolicy, action_result_code_);
  612. // Verify that DoTest() didn't set the output install plan.
  613. EXPECT_EQ("", install_plan.version);
  614. // Now verify the InstallPlan that was generated.
  615. install_plan = *delegate_.response_handler_action_install_plan_;
  616. EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
  617. EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
  618. EXPECT_EQ(1U, install_plan.target_slot);
  619. EXPECT_EQ(in.version, install_plan.version);
  620. }
  621. } // namespace chromeos_update_engine