blob: 909452a8634a2fd7c1b5477dc39f916be7209a3a [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) {
Sen Jiang0779a152018-07-02 17:34:56 -0700162 test_utils::ScopedTempFile test_deadline_file(
163 "omaha_response_handler_action_unittest-XXXXXX");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000164 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700165 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000166 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700167 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800168 in.packages.push_back(
169 {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
170 .size = 12,
171 .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000172 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000173 in.prompt = false;
Darin Petkov6c118642010-10-21 12:06:30 -0700174 in.deadline = "20101020";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175 InstallPlan install_plan;
Sen Jiang0779a152018-07-02 17:34:56 -0700176 EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800177 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
178 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800179 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700180 string deadline;
Sen Jiang0779a152018-07-02 17:34:56 -0700181 EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700182 EXPECT_EQ("20101020", deadline);
183 struct stat deadline_stat;
Sen Jiang0779a152018-07-02 17:34:56 -0700184 EXPECT_EQ(0, stat(test_deadline_file.path().c_str(), &deadline_stat));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800185 EXPECT_EQ(
186 static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
187 deadline_stat.st_mode);
Chris Sosafb1020e2013-07-29 17:27:33 -0700188 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000189 }
190 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700191 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000192 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700193 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800194 in.packages.push_back(
195 {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
196 .size = 12,
197 .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000198 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199 in.prompt = true;
200 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700201 // Set the other slot as current.
202 fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
Sen Jiang0779a152018-07-02 17:34:56 -0700203 EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800204 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
205 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800206 EXPECT_EQ(0U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700207 string deadline;
Sen Jiang0779a152018-07-02 17:34:56 -0700208 EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline) &&
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700209 deadline.empty());
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 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700213 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700215 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800216 in.packages.push_back(
217 {.payload_urls = {kLongName}, .size = 12, .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000218 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000219 in.prompt = true;
Darin Petkov6c118642010-10-21 12:06:30 -0700220 in.deadline = "some-deadline";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700222 fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
Sen Jiang0779a152018-07-02 17:34:56 -0700223 EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800224 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
225 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800226 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700227 string deadline;
Sen Jiang0779a152018-07-02 17:34:56 -0700228 EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700229 EXPECT_EQ("some-deadline", deadline);
Chris Sosafb1020e2013-07-29 17:27:33 -0700230 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000231 }
232}
233
234TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700235 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000236 in.update_exists = false;
237 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700238 EXPECT_FALSE(DoTest(in, "", &install_plan));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700239 EXPECT_TRUE(install_plan.partitions.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000240}
241
Sen Jiang0affc2c2017-02-10 15:55:05 -0800242TEST_F(OmahaResponseHandlerActionTest, MultiPackageTest) {
243 OmahaResponse in;
244 in.update_exists = true;
245 in.version = "a.b.c.d";
246 in.packages.push_back({.payload_urls = {"http://package/1"},
247 .size = 1,
248 .hash = kPayloadHashHex});
249 in.packages.push_back({.payload_urls = {"http://package/2"},
250 .size = 2,
251 .hash = kPayloadHashHex});
252 in.more_info_url = "http://more/info";
253 InstallPlan install_plan;
254 EXPECT_TRUE(DoTest(in, "", &install_plan));
255 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
256 EXPECT_EQ(2u, install_plan.payloads.size());
257 EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
258 EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
259 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
260 EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
261 EXPECT_EQ(in.version, install_plan.version);
262}
263
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800264TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) {
265 OmahaResponse in;
266 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700267 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800268 in.packages.push_back(
269 {.payload_urls = {"http://test.should/need/hash.checks.signed"},
270 .size = 12,
271 .hash = kPayloadHashHex});
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800272 in.more_info_url = "http://more/info";
David Pursell02c18642014-11-06 11:26:11 -0800273 // Hash checks are always skipped for non-official update URLs.
Alex Deymo763e7db2015-08-27 21:08:08 -0700274 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800275 IsUpdateUrlOfficial())
276 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800277 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700278 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800279 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
280 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800281 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700282 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800283}
284
David Pursell02c18642014-11-06 11:26:11 -0800285TEST_F(OmahaResponseHandlerActionTest, HashChecksForUnofficialUpdateUrl) {
286 OmahaResponse in;
287 in.update_exists = true;
288 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800289 in.packages.push_back(
290 {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
291 .size = 12,
292 .hash = kPayloadHashHex});
David Pursell02c18642014-11-06 11:26:11 -0800293 in.more_info_url = "http://more/info";
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(false));
297 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700298 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800299 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
300 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
David Pursell02c18642014-11-06 11:26:11 -0800301 EXPECT_FALSE(install_plan.hash_checks_mandatory);
302 EXPECT_EQ(in.version, install_plan.version);
303}
304
David Pursell907b4fa2015-01-27 10:27:38 -0800305TEST_F(OmahaResponseHandlerActionTest,
306 HashChecksForOfficialUrlUnofficialBuildTest) {
307 // Official URLs for unofficial builds (dev/test images) don't require hash.
308 OmahaResponse in;
309 in.update_exists = true;
310 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800311 in.packages.push_back(
312 {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
313 .size = 12,
314 .hash = kPayloadHashHex});
David Pursell907b4fa2015-01-27 10:27:38 -0800315 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700316 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell907b4fa2015-01-27 10:27:38 -0800317 IsUpdateUrlOfficial())
318 .WillRepeatedly(Return(true));
Alex Deymo763e7db2015-08-27 21:08:08 -0700319 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
David Pursell907b4fa2015-01-27 10:27:38 -0800320 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700321 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800322 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
323 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
David Pursell907b4fa2015-01-27 10:27:38 -0800324 EXPECT_FALSE(install_plan.hash_checks_mandatory);
325 EXPECT_EQ(in.version, install_plan.version);
326}
327
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800328TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) {
329 OmahaResponse in;
330 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700331 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800332 in.packages.push_back(
333 {.payload_urls = {"https://test.should/need/hash.checks.signed"},
334 .size = 12,
335 .hash = kPayloadHashHex});
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800336 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700337 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800338 IsUpdateUrlOfficial())
339 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800340 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700341 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800342 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
343 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800344 EXPECT_FALSE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700345 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800346}
347
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800348TEST_F(OmahaResponseHandlerActionTest, HashChecksForBothHttpAndHttpsTest) {
349 OmahaResponse in;
350 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700351 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800352 in.packages.push_back(
353 {.payload_urls = {"http://test.should.still/need/hash.checks",
354 "https://test.should.still/need/hash.checks"},
355 .size = 12,
356 .hash = kPayloadHashHex});
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800357 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700358 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800359 IsUpdateUrlOfficial())
360 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800361 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700362 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800363 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
364 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800365 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700366 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800367}
368
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700369TEST_F(OmahaResponseHandlerActionTest, ChangeToMoreStableChannelTest) {
370 OmahaResponse in;
371 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700372 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800373 in.packages.push_back({.payload_urls = {"https://MoreStableChannelTest"},
374 .size = 1,
375 .hash = kPayloadHashHex});
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700376 in.more_info_url = "http://more/info";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700377
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700378 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700379 base::ScopedTempDir tempdir;
380 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700381
Alex Deymo763e7db2015-08-27 21:08:08 -0700382 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700383 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900384 params.set_root(tempdir.GetPath().value());
Sen Jiang297e5832016-03-17 14:45:51 -0700385 params.set_current_channel("canary-channel");
386 // The ImageProperties in Android uses prefs to store MutableImageProperties.
387#ifdef __ANDROID__
Sen Jiang297e5832016-03-17 14:45:51 -0700388 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, true))
389 .WillOnce(Return(true));
390#endif // __ANDROID__
391 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
392 params.UpdateDownloadChannel();
Sen Jiang8500d3a2018-02-08 12:04:05 -0800393 EXPECT_TRUE(params.ShouldPowerwash());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700394
Alex Deymo763e7db2015-08-27 21:08:08 -0700395 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700396 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700397 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700398 EXPECT_TRUE(install_plan.powerwash_required);
399}
400
401TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
402 OmahaResponse in;
403 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700404 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800405 in.packages.push_back({.payload_urls = {"https://LessStableChannelTest"},
406 .size = 15,
407 .hash = kPayloadHashHex});
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700408 in.more_info_url = "http://more/info";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700409
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700410 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700411 base::ScopedTempDir tempdir;
412 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700413
Alex Deymo763e7db2015-08-27 21:08:08 -0700414 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700415 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900416 params.set_root(tempdir.GetPath().value());
Sen Jiang297e5832016-03-17 14:45:51 -0700417 params.set_current_channel("stable-channel");
418 // The ImageProperties in Android uses prefs to store MutableImageProperties.
419#ifdef __ANDROID__
Sen Jiang297e5832016-03-17 14:45:51 -0700420 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, false))
421 .WillOnce(Return(true));
422#endif // __ANDROID__
423 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
424 params.UpdateDownloadChannel();
Sen Jiang8500d3a2018-02-08 12:04:05 -0800425 EXPECT_FALSE(params.ShouldPowerwash());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700426
Alex Deymo763e7db2015-08-27 21:08:08 -0700427 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700428 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700429 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700430 EXPECT_FALSE(install_plan.powerwash_required);
431}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800432
David Zeuthen8f191b22013-08-06 12:27:50 -0700433TEST_F(OmahaResponseHandlerActionTest, P2PUrlIsUsedAndHashChecksMandatory) {
434 OmahaResponse in;
435 in.update_exists = true;
436 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800437 in.packages.push_back(
438 {.payload_urls = {"https://would.not/cause/hash/checks"},
439 .size = 12,
440 .hash = kPayloadHashHex});
David Zeuthen8f191b22013-08-06 12:27:50 -0700441 in.more_info_url = "http://more/info";
David Zeuthen8f191b22013-08-06 12:27:50 -0700442
Alex Deymo763e7db2015-08-27 21:08:08 -0700443 OmahaRequestParams params(&fake_system_state_);
David Pursell02c18642014-11-06 11:26:11 -0800444 // We're using a real OmahaRequestParams object here so we can't mock
445 // IsUpdateUrlOfficial(), but setting the update URL to the AutoUpdate test
446 // server will cause IsUpdateUrlOfficial() to return true.
Alex Deymoac41a822015-09-15 20:52:53 -0700447 params.set_update_url(constants::kOmahaDefaultAUTestURL);
Alex Deymo763e7db2015-08-27 21:08:08 -0700448 fake_system_state_.set_request_params(&params);
David Zeuthen8f191b22013-08-06 12:27:50 -0700449
Alex Deymo763e7db2015-08-27 21:08:08 -0700450 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700451 SetUsingP2PForDownloading(true));
452
David Zeuthen8f191b22013-08-06 12:27:50 -0700453 string p2p_url = "http://9.8.7.6/p2p";
Alex Deymo763e7db2015-08-27 21:08:08 -0700454 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetP2PUrl())
Gilad Arnold74b5f552014-10-07 08:17:16 -0700455 .WillRepeatedly(Return(p2p_url));
Alex Deymo763e7db2015-08-27 21:08:08 -0700456 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
Aaron Woodc73fdc12017-12-06 11:09:15 -0800457 GetUsingP2PForDownloading())
458 .WillRepeatedly(Return(true));
David Zeuthen8f191b22013-08-06 12:27:50 -0700459
460 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700461 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800462 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Sen Jiang2703ef42017-03-16 13:36:21 -0700463 EXPECT_EQ(p2p_url, install_plan.download_url);
David Zeuthen8f191b22013-08-06 12:27:50 -0700464 EXPECT_TRUE(install_plan.hash_checks_mandatory);
465}
466
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700467TEST_F(OmahaResponseHandlerActionTest, SystemVersionTest) {
468 OmahaResponse in;
469 in.update_exists = true;
470 in.version = "a.b.c.d";
471 in.system_version = "b.c.d.e";
472 in.packages.push_back({.payload_urls = {"http://package/1"},
473 .size = 1,
474 .hash = kPayloadHashHex});
475 in.packages.push_back({.payload_urls = {"http://package/2"},
476 .size = 2,
477 .hash = kPayloadHashHex});
478 in.more_info_url = "http://more/info";
479 InstallPlan install_plan;
480 EXPECT_TRUE(DoTest(in, "", &install_plan));
481 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
482 EXPECT_EQ(2u, install_plan.payloads.size());
483 EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
484 EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
485 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
486 EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
487 EXPECT_EQ(in.version, install_plan.version);
488 EXPECT_EQ(in.system_version, install_plan.system_version);
489}
490
Aaron Wood23bd3392017-10-06 14:48:25 -0700491TEST_F(OmahaResponseHandlerActionTest, TestDeferredByPolicy) {
492 OmahaResponse in;
493 in.update_exists = true;
494 in.version = "a.b.c.d";
495 in.packages.push_back({.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
496 .size = 12,
497 .hash = kPayloadHashHex});
498 // Setup the UpdateManager to disallow the update.
499 FakeClock fake_clock;
500 MockPolicy* mock_policy = new MockPolicy(&fake_clock);
501 FakeUpdateManager* fake_update_manager =
502 fake_system_state_.fake_update_manager();
503 fake_update_manager->set_policy(mock_policy);
504 EXPECT_CALL(*mock_policy, UpdateCanBeApplied(_, _, _, _, _))
505 .WillOnce(
506 DoAll(SetArgPointee<3>(ErrorCode::kOmahaUpdateDeferredPerPolicy),
507 Return(EvalStatus::kSucceeded)));
508 // Perform the Action. It should "fail" with kOmahaUpdateDeferredPerPolicy.
509 InstallPlan install_plan;
510 EXPECT_FALSE(DoTest(in, "", &install_plan));
511 EXPECT_EQ(ErrorCode::kOmahaUpdateDeferredPerPolicy, action_result_code_);
512 // Verify that DoTest() didn't set the output install plan.
513 EXPECT_EQ("", install_plan.version);
514 // Copy the underlying InstallPlan from the Action (like a real Delegate).
515 install_plan = action_->install_plan();
516 // Now verify the InstallPlan that was generated.
517 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
518 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
519 EXPECT_EQ(1U, install_plan.target_slot);
520 EXPECT_EQ(in.version, install_plan.version);
521}
522
adlr@google.com3defe6a2009-12-04 20:57:17 +0000523} // namespace chromeos_update_engine