blob: 60b139b7d50633dd87d2e41fa0d046536b9441aa [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_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000062};
63
64class OmahaResponseHandlerActionProcessorDelegate
65 : public ActionProcessorDelegate {
66 public:
67 OmahaResponseHandlerActionProcessorDelegate()
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070068 : code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070069 code_set_(false) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +000070 void ActionCompleted(ActionProcessor* processor,
71 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070072 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000073 if (action->Type() == OmahaResponseHandlerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070074 code_ = code;
75 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000076 }
77 }
David Zeuthena99981f2013-04-29 13:42:47 -070078 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070079 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000080};
81
82namespace {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070083const char* const kLongName =
adlr@google.com3defe6a2009-12-04 20:57:17 +000084 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
85 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
86 "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 "-the_update_a.b.c.d_DELTA_.tgz";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070092const char* const kBadVersion = "don't update me";
93} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +000094
Alex Deymo763e7db2015-08-27 21:08:08 -070095bool OmahaResponseHandlerActionTest::DoTest(
Jay Srinivasanae4697c2013-03-18 17:08:08 -070096 const OmahaResponse& in,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070097 const string& test_deadline_file,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070098 InstallPlan* out) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000099 ActionProcessor processor;
100 OmahaResponseHandlerActionProcessorDelegate delegate;
101 processor.set_delegate(&delegate);
102
Darin Petkov6a5b3222010-07-13 14:55:28 -0700103 ObjectFeederAction<OmahaResponse> feeder_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000104 feeder_action.set_obj(in);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700105 if (in.update_exists && in.version != kBadVersion) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700106 EXPECT_CALL(*(fake_system_state_.mock_prefs()),
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800107 SetString(kPrefsUpdateCheckResponseHash, in.hash))
Darin Petkov73058b42010-10-06 16:32:19 -0700108 .WillOnce(Return(true));
Alex Deymo85616652015-10-15 18:48:31 -0700109
110 int slot = 1 - fake_system_state_.fake_boot_control()->GetCurrentSlot();
111 string key = kPrefsChannelOnSlotPrefix + std::to_string(slot);
112 EXPECT_CALL(*(fake_system_state_.mock_prefs()), SetString(key, testing::_))
113 .WillOnce(Return(true));
Darin Petkov73058b42010-10-06 16:32:19 -0700114 }
Jay Srinivasan53173b92013-05-17 17:13:01 -0700115
116 string current_url = in.payload_urls.size() ? in.payload_urls[0] : "";
Alex Deymo763e7db2015-08-27 21:08:08 -0700117 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700118 .WillRepeatedly(Return(current_url));
119
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700120 OmahaResponseHandlerAction response_handler_action(
Alex Deymo763e7db2015-08-27 21:08:08 -0700121 &fake_system_state_,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700122 (test_deadline_file.empty() ?
Alex Deymo6dd160a2015-10-09 18:45:14 -0700123 constants::kOmahaResponseDeadlineFile : test_deadline_file));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000124 BondActions(&feeder_action, &response_handler_action);
125 ObjectCollectorAction<InstallPlan> collector_action;
126 BondActions(&response_handler_action, &collector_action);
127 processor.EnqueueAction(&feeder_action);
128 processor.EnqueueAction(&response_handler_action);
129 processor.EnqueueAction(&collector_action);
130 processor.StartProcessing();
131 EXPECT_TRUE(!processor.IsRunning())
Alex Vakulenko072359c2014-07-18 11:41:07 -0700132 << "Update test to handle non-async actions";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000133 if (out)
134 *out = collector_action.object();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700135 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700136 return delegate.code_ == ErrorCode::kSuccess;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000137}
138
139TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700140 string test_deadline_file;
141 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800142 "omaha_response_handler_action_unittest-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700143 &test_deadline_file, nullptr));
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700144 ScopedPathUnlinker deadline_unlinker(test_deadline_file);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000145 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700146 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000147 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700148 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800149 in.payload_urls.push_back("http://foo/the_update_a.b.c.d.tgz");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150 in.more_info_url = "http://more/info";
151 in.hash = "HASH+";
152 in.size = 12;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000153 in.prompt = false;
Darin Petkov6c118642010-10-21 12:06:30 -0700154 in.deadline = "20101020";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700156 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800157 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700158 EXPECT_EQ(in.hash, install_plan.payload_hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800159 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700160 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700161 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700162 EXPECT_EQ("20101020", deadline);
163 struct stat deadline_stat;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700164 EXPECT_EQ(0, stat(test_deadline_file.c_str(), &deadline_stat));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800165 EXPECT_EQ(
166 static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
167 deadline_stat.st_mode);
Chris Sosafb1020e2013-07-29 17:27:33 -0700168 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000169 }
170 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700171 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000172 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700173 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800174 in.payload_urls.push_back("http://foo/the_update_a.b.c.d.tgz");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175 in.more_info_url = "http://more/info";
176 in.hash = "HASHj+";
177 in.size = 12;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000178 in.prompt = true;
179 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700180 // Set the other slot as current.
181 fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
182 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800183 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700184 EXPECT_EQ(in.hash, install_plan.payload_hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800185 EXPECT_EQ(0U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700186 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700187 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline) &&
188 deadline.empty());
Chris Sosafb1020e2013-07-29 17:27:33 -0700189 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000190 }
191 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700192 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000193 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700194 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800195 in.payload_urls.push_back(kLongName);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000196 in.more_info_url = "http://more/info";
197 in.hash = "HASHj+";
198 in.size = 12;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199 in.prompt = true;
Darin Petkov6c118642010-10-21 12:06:30 -0700200 in.deadline = "some-deadline";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000201 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700202 fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
203 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800204 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700205 EXPECT_EQ(in.hash, install_plan.payload_hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800206 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700207 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700208 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700209 EXPECT_EQ("some-deadline", deadline);
Chris Sosafb1020e2013-07-29 17:27:33 -0700210 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000211 }
212}
213
214TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700215 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000216 in.update_exists = false;
217 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700218 EXPECT_FALSE(DoTest(in, "", &install_plan));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700219 EXPECT_TRUE(install_plan.partitions.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000220}
221
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800222TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) {
223 OmahaResponse in;
224 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700225 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800226 in.payload_urls.push_back("http://test.should/need/hash.checks.signed");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800227 in.more_info_url = "http://more/info";
228 in.hash = "HASHj+";
229 in.size = 12;
David Pursell02c18642014-11-06 11:26:11 -0800230 // Hash checks are always skipped for non-official update URLs.
Alex Deymo763e7db2015-08-27 21:08:08 -0700231 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800232 IsUpdateUrlOfficial())
233 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800234 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700235 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800236 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800237 EXPECT_EQ(in.hash, install_plan.payload_hash);
238 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700239 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800240}
241
David Pursell02c18642014-11-06 11:26:11 -0800242TEST_F(OmahaResponseHandlerActionTest, HashChecksForUnofficialUpdateUrl) {
243 OmahaResponse in;
244 in.update_exists = true;
245 in.version = "a.b.c.d";
246 in.payload_urls.push_back("http://url.normally/needs/hash.checks.signed");
247 in.more_info_url = "http://more/info";
248 in.hash = "HASHj+";
249 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700250 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800251 IsUpdateUrlOfficial())
252 .WillRepeatedly(Return(false));
253 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700254 EXPECT_TRUE(DoTest(in, "", &install_plan));
David Pursell02c18642014-11-06 11:26:11 -0800255 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
256 EXPECT_EQ(in.hash, install_plan.payload_hash);
257 EXPECT_FALSE(install_plan.hash_checks_mandatory);
258 EXPECT_EQ(in.version, install_plan.version);
259}
260
David Pursell907b4fa2015-01-27 10:27:38 -0800261TEST_F(OmahaResponseHandlerActionTest,
262 HashChecksForOfficialUrlUnofficialBuildTest) {
263 // Official URLs for unofficial builds (dev/test images) don't require hash.
264 OmahaResponse in;
265 in.update_exists = true;
266 in.version = "a.b.c.d";
267 in.payload_urls.push_back("http://url.normally/needs/hash.checks.signed");
268 in.more_info_url = "http://more/info";
269 in.hash = "HASHj+";
270 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700271 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell907b4fa2015-01-27 10:27:38 -0800272 IsUpdateUrlOfficial())
273 .WillRepeatedly(Return(true));
Alex Deymo763e7db2015-08-27 21:08:08 -0700274 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
David Pursell907b4fa2015-01-27 10:27:38 -0800275 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700276 EXPECT_TRUE(DoTest(in, "", &install_plan));
David Pursell907b4fa2015-01-27 10:27:38 -0800277 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
278 EXPECT_EQ(in.hash, install_plan.payload_hash);
279 EXPECT_FALSE(install_plan.hash_checks_mandatory);
280 EXPECT_EQ(in.version, install_plan.version);
281}
282
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800283TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) {
284 OmahaResponse in;
285 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700286 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287 in.payload_urls.push_back("https://test.should.not/need/hash.checks.signed");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800288 in.more_info_url = "http://more/info";
289 in.hash = "HASHj+";
290 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700291 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800292 IsUpdateUrlOfficial())
293 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800294 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700295 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800297 EXPECT_EQ(in.hash, install_plan.payload_hash);
298 EXPECT_FALSE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700299 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800300}
301
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800302TEST_F(OmahaResponseHandlerActionTest, HashChecksForBothHttpAndHttpsTest) {
303 OmahaResponse in;
304 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700305 in.version = "a.b.c.d";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306 in.payload_urls.push_back("http://test.should.still/need/hash.checks");
307 in.payload_urls.push_back("https://test.should.still/need/hash.checks");
308 in.more_info_url = "http://more/info";
309 in.hash = "HASHj+";
310 in.size = 12;
Alex Deymo763e7db2015-08-27 21:08:08 -0700311 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800312 IsUpdateUrlOfficial())
313 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800314 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700315 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316 EXPECT_EQ(in.payload_urls[0], install_plan.download_url);
317 EXPECT_EQ(in.hash, install_plan.payload_hash);
318 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700319 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800320}
321
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700322TEST_F(OmahaResponseHandlerActionTest, ChangeToMoreStableChannelTest) {
323 OmahaResponse in;
324 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700325 in.version = "a.b.c.d";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700326 in.payload_urls.push_back("https://MoreStableChannelTest");
327 in.more_info_url = "http://more/info";
328 in.hash = "HASHjk";
329 in.size = 15;
330
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700331 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700332 base::ScopedTempDir tempdir;
333 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700334
Alex Deymo763e7db2015-08-27 21:08:08 -0700335 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700336 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Sen Jiang297e5832016-03-17 14:45:51 -0700337 params.set_root(tempdir.path().value());
338 params.set_current_channel("canary-channel");
339 // The ImageProperties in Android uses prefs to store MutableImageProperties.
340#ifdef __ANDROID__
341 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetString(_, "stable-channel"))
342 .WillOnce(Return(true));
343 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, true))
344 .WillOnce(Return(true));
345#endif // __ANDROID__
346 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
347 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700348 EXPECT_TRUE(params.to_more_stable_channel());
349 EXPECT_TRUE(params.is_powerwash_allowed());
350
Alex Deymo763e7db2015-08-27 21:08:08 -0700351 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700352 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700353 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700354 EXPECT_TRUE(install_plan.powerwash_required);
355}
356
357TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
358 OmahaResponse in;
359 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700360 in.version = "a.b.c.d";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700361 in.payload_urls.push_back("https://LessStableChannelTest");
362 in.more_info_url = "http://more/info";
363 in.hash = "HASHjk";
364 in.size = 15;
365
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700366 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700367 base::ScopedTempDir tempdir;
368 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700369
Alex Deymo763e7db2015-08-27 21:08:08 -0700370 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700371 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Sen Jiang297e5832016-03-17 14:45:51 -0700372 params.set_root(tempdir.path().value());
373 params.set_current_channel("stable-channel");
374 // The ImageProperties in Android uses prefs to store MutableImageProperties.
375#ifdef __ANDROID__
376 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetString(_, "canary-channel"))
377 .WillOnce(Return(true));
378 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, false))
379 .WillOnce(Return(true));
380#endif // __ANDROID__
381 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
382 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700383 EXPECT_FALSE(params.to_more_stable_channel());
384 EXPECT_FALSE(params.is_powerwash_allowed());
385
Alex Deymo763e7db2015-08-27 21:08:08 -0700386 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700387 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700388 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700389 EXPECT_FALSE(install_plan.powerwash_required);
390}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800391
David Zeuthen8f191b22013-08-06 12:27:50 -0700392TEST_F(OmahaResponseHandlerActionTest, P2PUrlIsUsedAndHashChecksMandatory) {
393 OmahaResponse in;
394 in.update_exists = true;
395 in.version = "a.b.c.d";
396 in.payload_urls.push_back("https://would.not/cause/hash/checks");
397 in.more_info_url = "http://more/info";
398 in.hash = "HASHj+";
399 in.size = 12;
400
Alex Deymo763e7db2015-08-27 21:08:08 -0700401 OmahaRequestParams params(&fake_system_state_);
David Pursell02c18642014-11-06 11:26:11 -0800402 // We're using a real OmahaRequestParams object here so we can't mock
403 // IsUpdateUrlOfficial(), but setting the update URL to the AutoUpdate test
404 // server will cause IsUpdateUrlOfficial() to return true.
Alex Deymoac41a822015-09-15 20:52:53 -0700405 params.set_update_url(constants::kOmahaDefaultAUTestURL);
Alex Deymo763e7db2015-08-27 21:08:08 -0700406 fake_system_state_.set_request_params(&params);
David Zeuthen8f191b22013-08-06 12:27:50 -0700407
Alex Deymo763e7db2015-08-27 21:08:08 -0700408 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700409 SetUsingP2PForDownloading(true));
410
David Zeuthen8f191b22013-08-06 12:27:50 -0700411 string p2p_url = "http://9.8.7.6/p2p";
Alex Deymo763e7db2015-08-27 21:08:08 -0700412 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetP2PUrl())
Gilad Arnold74b5f552014-10-07 08:17:16 -0700413 .WillRepeatedly(Return(p2p_url));
Alex Deymo763e7db2015-08-27 21:08:08 -0700414 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700415 GetUsingP2PForDownloading()).WillRepeatedly(Return(true));
David Zeuthen8f191b22013-08-06 12:27:50 -0700416
417 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700418 EXPECT_TRUE(DoTest(in, "", &install_plan));
David Zeuthen8f191b22013-08-06 12:27:50 -0700419 EXPECT_EQ(in.hash, install_plan.payload_hash);
420 EXPECT_EQ(install_plan.download_url, p2p_url);
421 EXPECT_TRUE(install_plan.hash_checks_mandatory);
422}
423
adlr@google.com3defe6a2009-12-04 20:57:17 +0000424} // namespace chromeos_update_engine