blob: 4e9c03f2d816b8fcf12d0f275a751d09fdc2d1be [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
Aaron Woodc73fdc12017-12-06 11:09:15 -080019#include <memory>
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -070021
Alex Deymo110e0302015-10-19 20:35:21 -070022#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070023#include <base/files/scoped_temp_dir.h>
Aaron Wood23bd3392017-10-06 14:48:25 -070024#include <brillo/message_loops/fake_message_loop.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000025#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/constants.h"
28#include "update_engine/common/platform_constants.h"
29#include "update_engine/common/test_utils.h"
30#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070031#include "update_engine/fake_system_state.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070032#include "update_engine/mock_payload_state.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/payload_consumer/payload_constants.h"
Aaron Wood23bd3392017-10-06 14:48:25 -070034#include "update_engine/update_manager/mock_policy.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000035
Alex Deymo10875d92014-11-10 21:52:57 -080036using chromeos_update_engine::test_utils::System;
37using chromeos_update_engine::test_utils::WriteFileString;
Aaron Wood23bd3392017-10-06 14:48:25 -070038using chromeos_update_manager::EvalStatus;
39using chromeos_update_manager::FakeUpdateManager;
40using chromeos_update_manager::MockPolicy;
adlr@google.com3defe6a2009-12-04 20:57:17 +000041using std::string;
Aaron Woodc73fdc12017-12-06 11:09:15 -080042using testing::_;
Aaron Wood23bd3392017-10-06 14:48:25 -070043using testing::DoAll;
Darin Petkov73058b42010-10-06 16:32:19 -070044using testing::Return;
Aaron Wood23bd3392017-10-06 14:48:25 -070045using testing::SetArgPointee;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046
47namespace chromeos_update_engine {
48
49class OmahaResponseHandlerActionTest : public ::testing::Test {
Alex Deymo763e7db2015-08-27 21:08:08 -070050 protected:
51 void SetUp() override {
52 FakeBootControl* fake_boot_control = fake_system_state_.fake_boot_control();
53 fake_boot_control->SetPartitionDevice(
54 kLegacyPartitionNameKernel, 0, "/dev/sdz2");
55 fake_boot_control->SetPartitionDevice(
56 kLegacyPartitionNameRoot, 0, "/dev/sdz3");
57 fake_boot_control->SetPartitionDevice(
58 kLegacyPartitionNameKernel, 1, "/dev/sdz4");
59 fake_boot_control->SetPartitionDevice(
60 kLegacyPartitionNameRoot, 1, "/dev/sdz5");
61 }
62
adlr@google.com3defe6a2009-12-04 20:57:17 +000063 // Return true iff the OmahaResponseHandlerAction succeeded.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070064 // If out is non-null, it's set w/ the response from the action.
Darin Petkov6a5b3222010-07-13 14:55:28 -070065 bool DoTest(const OmahaResponse& in,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070066 const string& deadline_file,
adlr@google.com3defe6a2009-12-04 20:57:17 +000067 InstallPlan* out);
Alex Deymo763e7db2015-08-27 21:08:08 -070068
Aaron Wood23bd3392017-10-06 14:48:25 -070069 // Pointer to the Action, valid after |DoTest|, released when the test is
70 // finished.
71 std::unique_ptr<OmahaResponseHandlerAction> action_;
72 // Captures the action's result code, for tests that need to directly verify
73 // it in non-success cases.
74 ErrorCode action_result_code_;
75
Alex Deymo763e7db2015-08-27 21:08:08 -070076 FakeSystemState fake_system_state_;
Sen Jiang2703ef42017-03-16 13:36:21 -070077 // "Hash+"
78 const brillo::Blob expected_hash_ = {0x48, 0x61, 0x73, 0x68, 0x2b};
adlr@google.com3defe6a2009-12-04 20:57:17 +000079};
80
81class OmahaResponseHandlerActionProcessorDelegate
82 : public ActionProcessorDelegate {
83 public:
84 OmahaResponseHandlerActionProcessorDelegate()
Aaron Woodc73fdc12017-12-06 11:09:15 -080085 : code_(ErrorCode::kError), code_set_(false) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 void ActionCompleted(ActionProcessor* processor,
87 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070088 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000089 if (action->Type() == OmahaResponseHandlerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070090 code_ = code;
91 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000092 }
93 }
David Zeuthena99981f2013-04-29 13:42:47 -070094 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070095 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000096};
97
98namespace {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070099const char* const kLongName =
adlr@google.com3defe6a2009-12-04 20:57:17 +0000100 "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 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
104 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
105 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
106 "very_long_name_and_no_slashes-very_long_name_and_no_slashes"
107 "-the_update_a.b.c.d_DELTA_.tgz";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700108const char* const kBadVersion = "don't update me";
Sen Jiang2703ef42017-03-16 13:36:21 -0700109const char* const kPayloadHashHex = "486173682b";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700110} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111
Aaron Woodc73fdc12017-12-06 11:09:15 -0800112bool OmahaResponseHandlerActionTest::DoTest(const OmahaResponse& in,
113 const string& test_deadline_file,
114 InstallPlan* out) {
Aaron Wood23bd3392017-10-06 14:48:25 -0700115 brillo::FakeMessageLoop loop(nullptr);
116 loop.SetAsCurrent();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117 ActionProcessor processor;
118 OmahaResponseHandlerActionProcessorDelegate delegate;
119 processor.set_delegate(&delegate);
120
Darin Petkov6a5b3222010-07-13 14:55:28 -0700121 ObjectFeederAction<OmahaResponse> feeder_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000122 feeder_action.set_obj(in);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700123 if (in.update_exists && in.version != kBadVersion) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800124 string expected_hash;
125 for (const auto& package : in.packages)
126 expected_hash += package.hash + ":";
Alex Deymo763e7db2015-08-27 21:08:08 -0700127 EXPECT_CALL(*(fake_system_state_.mock_prefs()),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800128 SetString(kPrefsUpdateCheckResponseHash, expected_hash))
Darin Petkov73058b42010-10-06 16:32:19 -0700129 .WillOnce(Return(true));
Alex Deymo85616652015-10-15 18:48:31 -0700130
131 int slot = 1 - fake_system_state_.fake_boot_control()->GetCurrentSlot();
132 string key = kPrefsChannelOnSlotPrefix + std::to_string(slot);
133 EXPECT_CALL(*(fake_system_state_.mock_prefs()), SetString(key, testing::_))
134 .WillOnce(Return(true));
Darin Petkov73058b42010-10-06 16:32:19 -0700135 }
Jay Srinivasan53173b92013-05-17 17:13:01 -0700136
Sen Jiang0affc2c2017-02-10 15:55:05 -0800137 string current_url = in.packages.size() ? in.packages[0].payload_urls[0] : "";
Alex Deymo763e7db2015-08-27 21:08:08 -0700138 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700139 .WillRepeatedly(Return(current_url));
140
Aaron Wood23bd3392017-10-06 14:48:25 -0700141 action_.reset(new OmahaResponseHandlerAction(
Alex Deymo763e7db2015-08-27 21:08:08 -0700142 &fake_system_state_,
Aaron Wood23bd3392017-10-06 14:48:25 -0700143 (test_deadline_file.empty() ? constants::kOmahaResponseDeadlineFile
144 : test_deadline_file)));
145 BondActions(&feeder_action, action_.get());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000146 ObjectCollectorAction<InstallPlan> collector_action;
Aaron Wood23bd3392017-10-06 14:48:25 -0700147 BondActions(action_.get(), &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000148 processor.EnqueueAction(&feeder_action);
Aaron Wood23bd3392017-10-06 14:48:25 -0700149 processor.EnqueueAction(action_.get());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150 processor.EnqueueAction(&collector_action);
151 processor.StartProcessing();
152 EXPECT_TRUE(!processor.IsRunning())
Alex Vakulenko072359c2014-07-18 11:41:07 -0700153 << "Update test to handle non-async actions";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000154 if (out)
155 *out = collector_action.object();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700156 EXPECT_TRUE(delegate.code_set_);
Aaron Wood23bd3392017-10-06 14:48:25 -0700157 action_result_code_ = delegate.code_;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700158 return delegate.code_ == ErrorCode::kSuccess;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159}
160
161TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700162 string test_deadline_file;
Aaron Woodc73fdc12017-12-06 11:09:15 -0800163 CHECK(utils::MakeTempFile("omaha_response_handler_action_unittest-XXXXXX",
164 &test_deadline_file,
165 nullptr));
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700166 ScopedPathUnlinker deadline_unlinker(test_deadline_file);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000167 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700168 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000169 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700170 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800171 in.packages.push_back(
172 {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
173 .size = 12,
174 .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176 in.prompt = false;
Darin Petkov6c118642010-10-21 12:06:30 -0700177 in.deadline = "20101020";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000178 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700179 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800180 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
181 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800182 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700183 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700184 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700185 EXPECT_EQ("20101020", deadline);
186 struct stat deadline_stat;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700187 EXPECT_EQ(0, stat(test_deadline_file.c_str(), &deadline_stat));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800188 EXPECT_EQ(
189 static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
190 deadline_stat.st_mode);
Chris Sosafb1020e2013-07-29 17:27:33 -0700191 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000192 }
193 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700194 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700196 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800197 in.packages.push_back(
198 {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
199 .size = 12,
200 .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000201 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000202 in.prompt = true;
203 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700204 // Set the other slot as current.
205 fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
206 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800207 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
208 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800209 EXPECT_EQ(0U, 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) &&
212 deadline.empty());
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 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700216 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000217 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700218 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800219 in.packages.push_back(
220 {.payload_urls = {kLongName}, .size = 12, .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000222 in.prompt = true;
Darin Petkov6c118642010-10-21 12:06:30 -0700223 in.deadline = "some-deadline";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000224 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700225 fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
226 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800227 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
228 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800229 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700230 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700231 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700232 EXPECT_EQ("some-deadline", deadline);
Chris Sosafb1020e2013-07-29 17:27:33 -0700233 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000234 }
235}
236
237TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000239 in.update_exists = false;
240 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700241 EXPECT_FALSE(DoTest(in, "", &install_plan));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700242 EXPECT_TRUE(install_plan.partitions.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000243}
244
Sen Jiang0affc2c2017-02-10 15:55:05 -0800245TEST_F(OmahaResponseHandlerActionTest, MultiPackageTest) {
246 OmahaResponse in;
247 in.update_exists = true;
248 in.version = "a.b.c.d";
249 in.packages.push_back({.payload_urls = {"http://package/1"},
250 .size = 1,
251 .hash = kPayloadHashHex});
252 in.packages.push_back({.payload_urls = {"http://package/2"},
253 .size = 2,
254 .hash = kPayloadHashHex});
255 in.more_info_url = "http://more/info";
256 InstallPlan install_plan;
257 EXPECT_TRUE(DoTest(in, "", &install_plan));
258 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
259 EXPECT_EQ(2u, install_plan.payloads.size());
260 EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
261 EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
262 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
263 EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
264 EXPECT_EQ(in.version, install_plan.version);
265}
266
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800267TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) {
268 OmahaResponse in;
269 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700270 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800271 in.packages.push_back(
272 {.payload_urls = {"http://test.should/need/hash.checks.signed"},
273 .size = 12,
274 .hash = kPayloadHashHex});
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800275 in.more_info_url = "http://more/info";
David Pursell02c18642014-11-06 11:26:11 -0800276 // Hash checks are always skipped for non-official update URLs.
Alex Deymo763e7db2015-08-27 21:08:08 -0700277 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800278 IsUpdateUrlOfficial())
279 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800280 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700281 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800282 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
283 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800284 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700285 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800286}
287
David Pursell02c18642014-11-06 11:26:11 -0800288TEST_F(OmahaResponseHandlerActionTest, HashChecksForUnofficialUpdateUrl) {
289 OmahaResponse in;
290 in.update_exists = true;
291 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800292 in.packages.push_back(
293 {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
294 .size = 12,
295 .hash = kPayloadHashHex});
David Pursell02c18642014-11-06 11:26:11 -0800296 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700297 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800298 IsUpdateUrlOfficial())
299 .WillRepeatedly(Return(false));
300 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700301 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800302 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
303 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
David Pursell02c18642014-11-06 11:26:11 -0800304 EXPECT_FALSE(install_plan.hash_checks_mandatory);
305 EXPECT_EQ(in.version, install_plan.version);
306}
307
David Pursell907b4fa2015-01-27 10:27:38 -0800308TEST_F(OmahaResponseHandlerActionTest,
309 HashChecksForOfficialUrlUnofficialBuildTest) {
310 // Official URLs for unofficial builds (dev/test images) don't require hash.
311 OmahaResponse in;
312 in.update_exists = true;
313 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800314 in.packages.push_back(
315 {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
316 .size = 12,
317 .hash = kPayloadHashHex});
David Pursell907b4fa2015-01-27 10:27:38 -0800318 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700319 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell907b4fa2015-01-27 10:27:38 -0800320 IsUpdateUrlOfficial())
321 .WillRepeatedly(Return(true));
Alex Deymo763e7db2015-08-27 21:08:08 -0700322 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
David Pursell907b4fa2015-01-27 10:27:38 -0800323 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700324 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800325 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
326 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
David Pursell907b4fa2015-01-27 10:27:38 -0800327 EXPECT_FALSE(install_plan.hash_checks_mandatory);
328 EXPECT_EQ(in.version, install_plan.version);
329}
330
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800331TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) {
332 OmahaResponse in;
333 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700334 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800335 in.packages.push_back(
336 {.payload_urls = {"https://test.should/need/hash.checks.signed"},
337 .size = 12,
338 .hash = kPayloadHashHex});
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800339 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700340 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800341 IsUpdateUrlOfficial())
342 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800343 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700344 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800345 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
346 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800347 EXPECT_FALSE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700348 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800349}
350
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800351TEST_F(OmahaResponseHandlerActionTest, HashChecksForBothHttpAndHttpsTest) {
352 OmahaResponse in;
353 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700354 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800355 in.packages.push_back(
356 {.payload_urls = {"http://test.should.still/need/hash.checks",
357 "https://test.should.still/need/hash.checks"},
358 .size = 12,
359 .hash = kPayloadHashHex});
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800360 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700361 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800362 IsUpdateUrlOfficial())
363 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800364 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700365 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800366 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
367 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800368 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700369 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800370}
371
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700372TEST_F(OmahaResponseHandlerActionTest, ChangeToMoreStableChannelTest) {
373 OmahaResponse in;
374 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700375 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800376 in.packages.push_back({.payload_urls = {"https://MoreStableChannelTest"},
377 .size = 1,
378 .hash = kPayloadHashHex});
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700379 in.more_info_url = "http://more/info";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700380
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700381 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700382 base::ScopedTempDir tempdir;
383 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700384
Alex Deymo763e7db2015-08-27 21:08:08 -0700385 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700386 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900387 params.set_root(tempdir.GetPath().value());
Sen Jiang297e5832016-03-17 14:45:51 -0700388 params.set_current_channel("canary-channel");
389 // The ImageProperties in Android uses prefs to store MutableImageProperties.
390#ifdef __ANDROID__
Sen Jiang297e5832016-03-17 14:45:51 -0700391 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, true))
392 .WillOnce(Return(true));
393#endif // __ANDROID__
394 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
395 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700396 EXPECT_TRUE(params.to_more_stable_channel());
397 EXPECT_TRUE(params.is_powerwash_allowed());
398
Alex Deymo763e7db2015-08-27 21:08:08 -0700399 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700400 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700401 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700402 EXPECT_TRUE(install_plan.powerwash_required);
403}
404
405TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
406 OmahaResponse in;
407 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700408 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800409 in.packages.push_back({.payload_urls = {"https://LessStableChannelTest"},
410 .size = 15,
411 .hash = kPayloadHashHex});
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700412 in.more_info_url = "http://more/info";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700413
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700414 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700415 base::ScopedTempDir tempdir;
416 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700417
Alex Deymo763e7db2015-08-27 21:08:08 -0700418 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700419 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900420 params.set_root(tempdir.GetPath().value());
Sen Jiang297e5832016-03-17 14:45:51 -0700421 params.set_current_channel("stable-channel");
422 // The ImageProperties in Android uses prefs to store MutableImageProperties.
423#ifdef __ANDROID__
Sen Jiang297e5832016-03-17 14:45:51 -0700424 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, false))
425 .WillOnce(Return(true));
426#endif // __ANDROID__
427 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
428 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700429 EXPECT_FALSE(params.to_more_stable_channel());
430 EXPECT_FALSE(params.is_powerwash_allowed());
431
Alex Deymo763e7db2015-08-27 21:08:08 -0700432 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700433 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700434 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700435 EXPECT_FALSE(install_plan.powerwash_required);
436}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800437
David Zeuthen8f191b22013-08-06 12:27:50 -0700438TEST_F(OmahaResponseHandlerActionTest, P2PUrlIsUsedAndHashChecksMandatory) {
439 OmahaResponse in;
440 in.update_exists = true;
441 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800442 in.packages.push_back(
443 {.payload_urls = {"https://would.not/cause/hash/checks"},
444 .size = 12,
445 .hash = kPayloadHashHex});
David Zeuthen8f191b22013-08-06 12:27:50 -0700446 in.more_info_url = "http://more/info";
David Zeuthen8f191b22013-08-06 12:27:50 -0700447
Alex Deymo763e7db2015-08-27 21:08:08 -0700448 OmahaRequestParams params(&fake_system_state_);
David Pursell02c18642014-11-06 11:26:11 -0800449 // We're using a real OmahaRequestParams object here so we can't mock
450 // IsUpdateUrlOfficial(), but setting the update URL to the AutoUpdate test
451 // server will cause IsUpdateUrlOfficial() to return true.
Alex Deymoac41a822015-09-15 20:52:53 -0700452 params.set_update_url(constants::kOmahaDefaultAUTestURL);
Alex Deymo763e7db2015-08-27 21:08:08 -0700453 fake_system_state_.set_request_params(&params);
David Zeuthen8f191b22013-08-06 12:27:50 -0700454
Alex Deymo763e7db2015-08-27 21:08:08 -0700455 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700456 SetUsingP2PForDownloading(true));
457
David Zeuthen8f191b22013-08-06 12:27:50 -0700458 string p2p_url = "http://9.8.7.6/p2p";
Alex Deymo763e7db2015-08-27 21:08:08 -0700459 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetP2PUrl())
Gilad Arnold74b5f552014-10-07 08:17:16 -0700460 .WillRepeatedly(Return(p2p_url));
Alex Deymo763e7db2015-08-27 21:08:08 -0700461 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
Aaron Woodc73fdc12017-12-06 11:09:15 -0800462 GetUsingP2PForDownloading())
463 .WillRepeatedly(Return(true));
David Zeuthen8f191b22013-08-06 12:27:50 -0700464
465 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700466 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800467 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Sen Jiang2703ef42017-03-16 13:36:21 -0700468 EXPECT_EQ(p2p_url, install_plan.download_url);
David Zeuthen8f191b22013-08-06 12:27:50 -0700469 EXPECT_TRUE(install_plan.hash_checks_mandatory);
470}
471
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700472TEST_F(OmahaResponseHandlerActionTest, SystemVersionTest) {
473 OmahaResponse in;
474 in.update_exists = true;
475 in.version = "a.b.c.d";
476 in.system_version = "b.c.d.e";
477 in.packages.push_back({.payload_urls = {"http://package/1"},
478 .size = 1,
479 .hash = kPayloadHashHex});
480 in.packages.push_back({.payload_urls = {"http://package/2"},
481 .size = 2,
482 .hash = kPayloadHashHex});
483 in.more_info_url = "http://more/info";
484 InstallPlan install_plan;
485 EXPECT_TRUE(DoTest(in, "", &install_plan));
486 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
487 EXPECT_EQ(2u, install_plan.payloads.size());
488 EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
489 EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
490 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
491 EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
492 EXPECT_EQ(in.version, install_plan.version);
493 EXPECT_EQ(in.system_version, install_plan.system_version);
494}
495
Aaron Wood23bd3392017-10-06 14:48:25 -0700496TEST_F(OmahaResponseHandlerActionTest, TestDeferredByPolicy) {
497 OmahaResponse in;
498 in.update_exists = true;
499 in.version = "a.b.c.d";
500 in.packages.push_back({.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
501 .size = 12,
502 .hash = kPayloadHashHex});
503 // Setup the UpdateManager to disallow the update.
504 FakeClock fake_clock;
505 MockPolicy* mock_policy = new MockPolicy(&fake_clock);
506 FakeUpdateManager* fake_update_manager =
507 fake_system_state_.fake_update_manager();
508 fake_update_manager->set_policy(mock_policy);
509 EXPECT_CALL(*mock_policy, UpdateCanBeApplied(_, _, _, _, _))
510 .WillOnce(
511 DoAll(SetArgPointee<3>(ErrorCode::kOmahaUpdateDeferredPerPolicy),
512 Return(EvalStatus::kSucceeded)));
513 // Perform the Action. It should "fail" with kOmahaUpdateDeferredPerPolicy.
514 InstallPlan install_plan;
515 EXPECT_FALSE(DoTest(in, "", &install_plan));
516 EXPECT_EQ(ErrorCode::kOmahaUpdateDeferredPerPolicy, action_result_code_);
517 // Verify that DoTest() didn't set the output install plan.
518 EXPECT_EQ("", install_plan.version);
519 // Copy the underlying InstallPlan from the Action (like a real Delegate).
520 install_plan = action_->install_plan();
521 // Now verify the InstallPlan that was generated.
522 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
523 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
524 EXPECT_EQ(1U, install_plan.target_slot);
525 EXPECT_EQ(in.version, install_plan.version);
526}
527
adlr@google.com3defe6a2009-12-04 20:57:17 +0000528} // namespace chromeos_update_engine