blob: 473eaf80db3b4be15f795595f2e63806ee2c976d [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/omaha_response_handler_action.h"
18
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -070020
Alex Deymo110e0302015-10-19 20:35:21 -070021#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070022#include <base/files/scoped_temp_dir.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000023#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
26#include "update_engine/common/platform_constants.h"
27#include "update_engine/common/test_utils.h"
28#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070029#include "update_engine/fake_system_state.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070030#include "update_engine/mock_payload_state.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/payload_consumer/payload_constants.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000032
Alex Deymo10875d92014-11-10 21:52:57 -080033using chromeos_update_engine::test_utils::System;
34using chromeos_update_engine::test_utils::WriteFileString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000035using std::string;
Darin Petkov73058b42010-10-06 16:32:19 -070036using testing::Return;
Sen Jiang297e5832016-03-17 14:45:51 -070037using testing::_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000038
39namespace chromeos_update_engine {
40
41class OmahaResponseHandlerActionTest : public ::testing::Test {
Alex Deymo763e7db2015-08-27 21:08:08 -070042 protected:
43 void SetUp() override {
44 FakeBootControl* fake_boot_control = fake_system_state_.fake_boot_control();
45 fake_boot_control->SetPartitionDevice(
46 kLegacyPartitionNameKernel, 0, "/dev/sdz2");
47 fake_boot_control->SetPartitionDevice(
48 kLegacyPartitionNameRoot, 0, "/dev/sdz3");
49 fake_boot_control->SetPartitionDevice(
50 kLegacyPartitionNameKernel, 1, "/dev/sdz4");
51 fake_boot_control->SetPartitionDevice(
52 kLegacyPartitionNameRoot, 1, "/dev/sdz5");
53 }
54
adlr@google.com3defe6a2009-12-04 20:57:17 +000055 // Return true iff the OmahaResponseHandlerAction succeeded.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070056 // If out is non-null, it's set w/ the response from the action.
Darin Petkov6a5b3222010-07-13 14:55:28 -070057 bool DoTest(const OmahaResponse& in,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070058 const string& deadline_file,
adlr@google.com3defe6a2009-12-04 20:57:17 +000059 InstallPlan* out);
Alex Deymo763e7db2015-08-27 21:08:08 -070060
61 FakeSystemState fake_system_state_;
Sen Jiang2703ef42017-03-16 13:36:21 -070062 // "Hash+"
63 const brillo::Blob expected_hash_ = {0x48, 0x61, 0x73, 0x68, 0x2b};
adlr@google.com3defe6a2009-12-04 20:57:17 +000064};
65
66class OmahaResponseHandlerActionProcessorDelegate
67 : public ActionProcessorDelegate {
68 public:
69 OmahaResponseHandlerActionProcessorDelegate()
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070070 : code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070071 code_set_(false) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 void ActionCompleted(ActionProcessor* processor,
73 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070074 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000075 if (action->Type() == OmahaResponseHandlerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070076 code_ = code;
77 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000078 }
79 }
David Zeuthena99981f2013-04-29 13:42:47 -070080 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070081 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000082};
83
84namespace {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070085const char* const kLongName =
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
87 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
88 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
89 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
90 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
91 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
92 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
93 "-the_update_a.b.c.d_DELTA_.tgz";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070094const char* const kBadVersion = "don't update me";
Sen Jiang2703ef42017-03-16 13:36:21 -070095const char* const kPayloadHashHex = "486173682b";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070096} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +000097
Alex Deymo763e7db2015-08-27 21:08:08 -070098bool OmahaResponseHandlerActionTest::DoTest(
Jay Srinivasanae4697c2013-03-18 17:08:08 -070099 const OmahaResponse& in,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700100 const string& test_deadline_file,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700101 InstallPlan* out) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000102 ActionProcessor processor;
103 OmahaResponseHandlerActionProcessorDelegate delegate;
104 processor.set_delegate(&delegate);
105
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106 ObjectFeederAction<OmahaResponse> feeder_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107 feeder_action.set_obj(in);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700108 if (in.update_exists && in.version != kBadVersion) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700109 EXPECT_CALL(*(fake_system_state_.mock_prefs()),
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800110 SetString(kPrefsUpdateCheckResponseHash, in.hash))
Darin Petkov73058b42010-10-06 16:32:19 -0700111 .WillOnce(Return(true));
Alex Deymo85616652015-10-15 18:48:31 -0700112
113 int slot = 1 - fake_system_state_.fake_boot_control()->GetCurrentSlot();
114 string key = kPrefsChannelOnSlotPrefix + std::to_string(slot);
115 EXPECT_CALL(*(fake_system_state_.mock_prefs()), SetString(key, testing::_))
116 .WillOnce(Return(true));
Darin Petkov73058b42010-10-06 16:32:19 -0700117 }
Jay Srinivasan53173b92013-05-17 17:13:01 -0700118
119 string current_url = in.payload_urls.size() ? in.payload_urls[0] : "";
Alex Deymo763e7db2015-08-27 21:08:08 -0700120 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700121 .WillRepeatedly(Return(current_url));
122
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700123 OmahaResponseHandlerAction response_handler_action(
Alex Deymo763e7db2015-08-27 21:08:08 -0700124 &fake_system_state_,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700125 (test_deadline_file.empty() ?
Alex Deymo6dd160a2015-10-09 18:45:14 -0700126 constants::kOmahaResponseDeadlineFile : test_deadline_file));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000127 BondActions(&feeder_action, &response_handler_action);
128 ObjectCollectorAction<InstallPlan> collector_action;
129 BondActions(&response_handler_action, &collector_action);
130 processor.EnqueueAction(&feeder_action);
131 processor.EnqueueAction(&response_handler_action);
132 processor.EnqueueAction(&collector_action);
133 processor.StartProcessing();
134 EXPECT_TRUE(!processor.IsRunning())
Alex Vakulenko072359c2014-07-18 11:41:07 -0700135 << "Update test to handle non-async actions";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000136 if (out)
137 *out = collector_action.object();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700138 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700139 return delegate.code_ == ErrorCode::kSuccess;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140}
141
142TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700143 string test_deadline_file;
144 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800145 "omaha_response_handler_action_unittest-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700146 &test_deadline_file, nullptr));
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700147 ScopedPathUnlinker deadline_unlinker(test_deadline_file);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000148 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700149 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700151 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800152 in.payload_urls.push_back("http://foo/the_update_a.b.c.d.tgz");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000153 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700154 in.hash = kPayloadHashHex;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155 in.size = 12;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000156 in.prompt = false;
Darin Petkov6c118642010-10-21 12:06:30 -0700157 in.deadline = "20101020";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000158 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700159 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800160 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700161 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800162 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700163 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700164 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700165 EXPECT_EQ("20101020", deadline);
166 struct stat deadline_stat;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700167 EXPECT_EQ(0, stat(test_deadline_file.c_str(), &deadline_stat));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800168 EXPECT_EQ(
169 static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
170 deadline_stat.st_mode);
Chris Sosafb1020e2013-07-29 17:27:33 -0700171 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000172 }
173 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700174 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700176 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177 in.payload_urls.push_back("http://foo/the_update_a.b.c.d.tgz");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000178 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700179 in.hash = kPayloadHashHex;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000180 in.size = 12;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000181 in.prompt = true;
182 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700183 // Set the other slot as current.
184 fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
185 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800186 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700187 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800188 EXPECT_EQ(0U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700189 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700190 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline) &&
191 deadline.empty());
Chris Sosafb1020e2013-07-29 17:27:33 -0700192 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000193 }
194 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700195 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000196 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700197 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198 in.payload_urls.push_back(kLongName);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700200 in.hash = kPayloadHashHex;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000201 in.size = 12;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000202 in.prompt = true;
Darin Petkov6c118642010-10-21 12:06:30 -0700203 in.deadline = "some-deadline";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000204 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700205 fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
206 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800207 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700208 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800209 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700210 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700211 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700212 EXPECT_EQ("some-deadline", deadline);
Chris Sosafb1020e2013-07-29 17:27:33 -0700213 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214 }
215}
216
217TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700218 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000219 in.update_exists = false;
220 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700221 EXPECT_FALSE(DoTest(in, "", &install_plan));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700222 EXPECT_TRUE(install_plan.partitions.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223}
224
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800225TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) {
226 OmahaResponse in;
227 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700228 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 in.payload_urls.push_back("http://test.should/need/hash.checks.signed");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800230 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700231 in.hash = kPayloadHashHex;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800232 in.size = 12;
David Pursell02c18642014-11-06 11:26:11 -0800233 // Hash checks are always skipped for non-official update URLs.
Alex Deymo763e7db2015-08-27 21:08:08 -0700234 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800235 IsUpdateUrlOfficial())
236 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800237 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700238 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800239 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700240 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800241 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700242 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800243}
244
David Pursell02c18642014-11-06 11:26:11 -0800245TEST_F(OmahaResponseHandlerActionTest, HashChecksForUnofficialUpdateUrl) {
246 OmahaResponse in;
247 in.update_exists = true;
248 in.version = "a.b.c.d";
249 in.payload_urls.push_back("http://url.normally/needs/hash.checks.signed");
250 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700251 in.hash = kPayloadHashHex;
David Pursell02c18642014-11-06 11:26:11 -0800252 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700253 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800254 IsUpdateUrlOfficial())
255 .WillRepeatedly(Return(false));
256 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700257 EXPECT_TRUE(DoTest(in, "", &install_plan));
David Pursell02c18642014-11-06 11:26:11 -0800258 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700259 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
David Pursell02c18642014-11-06 11:26:11 -0800260 EXPECT_FALSE(install_plan.hash_checks_mandatory);
261 EXPECT_EQ(in.version, install_plan.version);
262}
263
David Pursell907b4fa2015-01-27 10:27:38 -0800264TEST_F(OmahaResponseHandlerActionTest,
265 HashChecksForOfficialUrlUnofficialBuildTest) {
266 // Official URLs for unofficial builds (dev/test images) don't require hash.
267 OmahaResponse in;
268 in.update_exists = true;
269 in.version = "a.b.c.d";
270 in.payload_urls.push_back("http://url.normally/needs/hash.checks.signed");
271 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700272 in.hash = kPayloadHashHex;
David Pursell907b4fa2015-01-27 10:27:38 -0800273 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700274 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell907b4fa2015-01-27 10:27:38 -0800275 IsUpdateUrlOfficial())
276 .WillRepeatedly(Return(true));
Alex Deymo763e7db2015-08-27 21:08:08 -0700277 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
David Pursell907b4fa2015-01-27 10:27:38 -0800278 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700279 EXPECT_TRUE(DoTest(in, "", &install_plan));
David Pursell907b4fa2015-01-27 10:27:38 -0800280 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700281 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
David Pursell907b4fa2015-01-27 10:27:38 -0800282 EXPECT_FALSE(install_plan.hash_checks_mandatory);
283 EXPECT_EQ(in.version, install_plan.version);
284}
285
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800286TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) {
287 OmahaResponse in;
288 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700289 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290 in.payload_urls.push_back("https://test.should.not/need/hash.checks.signed");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800291 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700292 in.hash = kPayloadHashHex;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800293 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700294 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800295 IsUpdateUrlOfficial())
296 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800297 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700298 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800299 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700300 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800301 EXPECT_FALSE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700302 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800303}
304
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800305TEST_F(OmahaResponseHandlerActionTest, HashChecksForBothHttpAndHttpsTest) {
306 OmahaResponse in;
307 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700308 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800309 in.payload_urls.push_back("http://test.should.still/need/hash.checks");
310 in.payload_urls.push_back("https://test.should.still/need/hash.checks");
311 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700312 in.hash = kPayloadHashHex;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800313 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700314 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800315 IsUpdateUrlOfficial())
316 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800317 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700318 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Sen Jiang2703ef42017-03-16 13:36:21 -0700320 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800321 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700322 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800323}
324
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700325TEST_F(OmahaResponseHandlerActionTest, ChangeToMoreStableChannelTest) {
326 OmahaResponse in;
327 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700328 in.version = "a.b.c.d";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700329 in.payload_urls.push_back("https://MoreStableChannelTest");
330 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700331 in.hash = kPayloadHashHex;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700332 in.size = 15;
333
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700334 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700335 base::ScopedTempDir tempdir;
336 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700337
Alex Deymo763e7db2015-08-27 21:08:08 -0700338 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700339 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Sen Jiang297e5832016-03-17 14:45:51 -0700340 params.set_root(tempdir.path().value());
341 params.set_current_channel("canary-channel");
342 // The ImageProperties in Android uses prefs to store MutableImageProperties.
343#ifdef __ANDROID__
344 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetString(_, "stable-channel"))
345 .WillOnce(Return(true));
346 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, true))
347 .WillOnce(Return(true));
348#endif // __ANDROID__
349 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
350 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700351 EXPECT_TRUE(params.to_more_stable_channel());
352 EXPECT_TRUE(params.is_powerwash_allowed());
353
Alex Deymo763e7db2015-08-27 21:08:08 -0700354 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700355 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700356 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700357 EXPECT_TRUE(install_plan.powerwash_required);
358}
359
360TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
361 OmahaResponse in;
362 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700363 in.version = "a.b.c.d";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700364 in.payload_urls.push_back("https://LessStableChannelTest");
365 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700366 in.hash = kPayloadHashHex;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700367 in.size = 15;
368
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700369 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700370 base::ScopedTempDir tempdir;
371 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700372
Alex Deymo763e7db2015-08-27 21:08:08 -0700373 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700374 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Sen Jiang297e5832016-03-17 14:45:51 -0700375 params.set_root(tempdir.path().value());
376 params.set_current_channel("stable-channel");
377 // The ImageProperties in Android uses prefs to store MutableImageProperties.
378#ifdef __ANDROID__
379 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetString(_, "canary-channel"))
380 .WillOnce(Return(true));
381 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, false))
382 .WillOnce(Return(true));
383#endif // __ANDROID__
384 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
385 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700386 EXPECT_FALSE(params.to_more_stable_channel());
387 EXPECT_FALSE(params.is_powerwash_allowed());
388
Alex Deymo763e7db2015-08-27 21:08:08 -0700389 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700390 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700391 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700392 EXPECT_FALSE(install_plan.powerwash_required);
393}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800394
David Zeuthen8f191b22013-08-06 12:27:50 -0700395TEST_F(OmahaResponseHandlerActionTest, P2PUrlIsUsedAndHashChecksMandatory) {
396 OmahaResponse in;
397 in.update_exists = true;
398 in.version = "a.b.c.d";
399 in.payload_urls.push_back("https://would.not/cause/hash/checks");
400 in.more_info_url = "http://more/info";
Sen Jiang2703ef42017-03-16 13:36:21 -0700401 in.hash = kPayloadHashHex;
David Zeuthen8f191b22013-08-06 12:27:50 -0700402 in.size = 12;
403
Alex Deymo763e7db2015-08-27 21:08:08 -0700404 OmahaRequestParams params(&fake_system_state_);
David Pursell02c18642014-11-06 11:26:11 -0800405 // We're using a real OmahaRequestParams object here so we can't mock
406 // IsUpdateUrlOfficial(), but setting the update URL to the AutoUpdate test
407 // server will cause IsUpdateUrlOfficial() to return true.
Alex Deymoac41a822015-09-15 20:52:53 -0700408 params.set_update_url(constants::kOmahaDefaultAUTestURL);
Alex Deymo763e7db2015-08-27 21:08:08 -0700409 fake_system_state_.set_request_params(&params);
David Zeuthen8f191b22013-08-06 12:27:50 -0700410
Alex Deymo763e7db2015-08-27 21:08:08 -0700411 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700412 SetUsingP2PForDownloading(true));
413
David Zeuthen8f191b22013-08-06 12:27:50 -0700414 string p2p_url = "http://9.8.7.6/p2p";
Alex Deymo763e7db2015-08-27 21:08:08 -0700415 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetP2PUrl())
Gilad Arnold74b5f552014-10-07 08:17:16 -0700416 .WillRepeatedly(Return(p2p_url));
Alex Deymo763e7db2015-08-27 21:08:08 -0700417 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700418 GetUsingP2PForDownloading()).WillRepeatedly(Return(true));
David Zeuthen8f191b22013-08-06 12:27:50 -0700419
420 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700421 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang2703ef42017-03-16 13:36:21 -0700422 EXPECT_EQ(expected_hash_, install_plan.payload_hash);
423 EXPECT_EQ(p2p_url, install_plan.download_url);
David Zeuthen8f191b22013-08-06 12:27:50 -0700424 EXPECT_TRUE(install_plan.hash_checks_mandatory);
425}
426
adlr@google.com3defe6a2009-12-04 20:57:17 +0000427} // namespace chromeos_update_engine