blob: 0887e1fd0fe89d79af1738b8fe3be1f6e4fed7f7 [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>
Aaron Wood23bd3392017-10-06 14:48:25 -070023#include <brillo/message_loops/fake_message_loop.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/constants.h"
27#include "update_engine/common/platform_constants.h"
28#include "update_engine/common/test_utils.h"
29#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070030#include "update_engine/fake_system_state.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070031#include "update_engine/mock_payload_state.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/payload_consumer/payload_constants.h"
Aaron Wood23bd3392017-10-06 14:48:25 -070033#include "update_engine/update_manager/mock_policy.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000034
Alex Deymo10875d92014-11-10 21:52:57 -080035using chromeos_update_engine::test_utils::System;
36using chromeos_update_engine::test_utils::WriteFileString;
Aaron Wood23bd3392017-10-06 14:48:25 -070037using chromeos_update_manager::EvalStatus;
38using chromeos_update_manager::FakeUpdateManager;
39using chromeos_update_manager::MockPolicy;
adlr@google.com3defe6a2009-12-04 20:57:17 +000040using std::string;
Aaron Wood23bd3392017-10-06 14:48:25 -070041using testing::DoAll;
Darin Petkov73058b42010-10-06 16:32:19 -070042using testing::Return;
Aaron Wood23bd3392017-10-06 14:48:25 -070043using testing::SetArgPointee;
Sen Jiang297e5832016-03-17 14:45:51 -070044using testing::_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000045
46namespace chromeos_update_engine {
47
48class OmahaResponseHandlerActionTest : public ::testing::Test {
Alex Deymo763e7db2015-08-27 21:08:08 -070049 protected:
50 void SetUp() override {
51 FakeBootControl* fake_boot_control = fake_system_state_.fake_boot_control();
52 fake_boot_control->SetPartitionDevice(
53 kLegacyPartitionNameKernel, 0, "/dev/sdz2");
54 fake_boot_control->SetPartitionDevice(
55 kLegacyPartitionNameRoot, 0, "/dev/sdz3");
56 fake_boot_control->SetPartitionDevice(
57 kLegacyPartitionNameKernel, 1, "/dev/sdz4");
58 fake_boot_control->SetPartitionDevice(
59 kLegacyPartitionNameRoot, 1, "/dev/sdz5");
60 }
61
adlr@google.com3defe6a2009-12-04 20:57:17 +000062 // Return true iff the OmahaResponseHandlerAction succeeded.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070063 // If out is non-null, it's set w/ the response from the action.
Darin Petkov6a5b3222010-07-13 14:55:28 -070064 bool DoTest(const OmahaResponse& in,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070065 const string& deadline_file,
adlr@google.com3defe6a2009-12-04 20:57:17 +000066 InstallPlan* out);
Alex Deymo763e7db2015-08-27 21:08:08 -070067
Aaron Wood23bd3392017-10-06 14:48:25 -070068 // Pointer to the Action, valid after |DoTest|, released when the test is
69 // finished.
70 std::unique_ptr<OmahaResponseHandlerAction> action_;
71 // Captures the action's result code, for tests that need to directly verify
72 // it in non-success cases.
73 ErrorCode action_result_code_;
74
Alex Deymo763e7db2015-08-27 21:08:08 -070075 FakeSystemState fake_system_state_;
Sen Jiang2703ef42017-03-16 13:36:21 -070076 // "Hash+"
77 const brillo::Blob expected_hash_ = {0x48, 0x61, 0x73, 0x68, 0x2b};
adlr@google.com3defe6a2009-12-04 20:57:17 +000078};
79
80class OmahaResponseHandlerActionProcessorDelegate
81 : public ActionProcessorDelegate {
82 public:
83 OmahaResponseHandlerActionProcessorDelegate()
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070084 : code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 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
Alex Deymo763e7db2015-08-27 21:08:08 -0700112bool OmahaResponseHandlerActionTest::DoTest(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 const OmahaResponse& in,
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700114 const string& test_deadline_file,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115 InstallPlan* out) {
Aaron Wood23bd3392017-10-06 14:48:25 -0700116 brillo::FakeMessageLoop loop(nullptr);
117 loop.SetAsCurrent();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000118 ActionProcessor processor;
119 OmahaResponseHandlerActionProcessorDelegate delegate;
120 processor.set_delegate(&delegate);
121
Darin Petkov6a5b3222010-07-13 14:55:28 -0700122 ObjectFeederAction<OmahaResponse> feeder_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000123 feeder_action.set_obj(in);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700124 if (in.update_exists && in.version != kBadVersion) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800125 string expected_hash;
126 for (const auto& package : in.packages)
127 expected_hash += package.hash + ":";
Alex Deymo763e7db2015-08-27 21:08:08 -0700128 EXPECT_CALL(*(fake_system_state_.mock_prefs()),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800129 SetString(kPrefsUpdateCheckResponseHash, expected_hash))
Darin Petkov73058b42010-10-06 16:32:19 -0700130 .WillOnce(Return(true));
Alex Deymo85616652015-10-15 18:48:31 -0700131
132 int slot = 1 - fake_system_state_.fake_boot_control()->GetCurrentSlot();
133 string key = kPrefsChannelOnSlotPrefix + std::to_string(slot);
134 EXPECT_CALL(*(fake_system_state_.mock_prefs()), SetString(key, testing::_))
135 .WillOnce(Return(true));
Darin Petkov73058b42010-10-06 16:32:19 -0700136 }
Jay Srinivasan53173b92013-05-17 17:13:01 -0700137
Sen Jiang0affc2c2017-02-10 15:55:05 -0800138 string current_url = in.packages.size() ? in.packages[0].payload_urls[0] : "";
Alex Deymo763e7db2015-08-27 21:08:08 -0700139 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700140 .WillRepeatedly(Return(current_url));
141
Aaron Wood23bd3392017-10-06 14:48:25 -0700142 action_.reset(new OmahaResponseHandlerAction(
Alex Deymo763e7db2015-08-27 21:08:08 -0700143 &fake_system_state_,
Aaron Wood23bd3392017-10-06 14:48:25 -0700144 (test_deadline_file.empty() ? constants::kOmahaResponseDeadlineFile
145 : test_deadline_file)));
146 BondActions(&feeder_action, action_.get());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000147 ObjectCollectorAction<InstallPlan> collector_action;
Aaron Wood23bd3392017-10-06 14:48:25 -0700148 BondActions(action_.get(), &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000149 processor.EnqueueAction(&feeder_action);
Aaron Wood23bd3392017-10-06 14:48:25 -0700150 processor.EnqueueAction(action_.get());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000151 processor.EnqueueAction(&collector_action);
152 processor.StartProcessing();
153 EXPECT_TRUE(!processor.IsRunning())
Alex Vakulenko072359c2014-07-18 11:41:07 -0700154 << "Update test to handle non-async actions";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155 if (out)
156 *out = collector_action.object();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700157 EXPECT_TRUE(delegate.code_set_);
Aaron Wood23bd3392017-10-06 14:48:25 -0700158 action_result_code_ = delegate.code_;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700159 return delegate.code_ == ErrorCode::kSuccess;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000160}
161
162TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700163 string test_deadline_file;
164 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800165 "omaha_response_handler_action_unittest-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700166 &test_deadline_file, nullptr));
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700167 ScopedPathUnlinker deadline_unlinker(test_deadline_file);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000168 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700169 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700171 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800172 in.packages.push_back(
173 {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
174 .size = 12,
175 .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000177 in.prompt = false;
Darin Petkov6c118642010-10-21 12:06:30 -0700178 in.deadline = "20101020";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000179 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700180 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800181 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
182 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800183 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700184 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700185 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700186 EXPECT_EQ("20101020", deadline);
187 struct stat deadline_stat;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700188 EXPECT_EQ(0, stat(test_deadline_file.c_str(), &deadline_stat));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800189 EXPECT_EQ(
190 static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
191 deadline_stat.st_mode);
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";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800198 in.packages.push_back(
199 {.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
200 .size = 12,
201 .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000202 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000203 in.prompt = true;
204 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700205 // Set the other slot as current.
206 fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
207 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800208 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
209 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800210 EXPECT_EQ(0U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700211 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700212 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline) &&
213 deadline.empty());
Chris Sosafb1020e2013-07-29 17:27:33 -0700214 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000215 }
216 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700217 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000218 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700219 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800220 in.packages.push_back(
221 {.payload_urls = {kLongName}, .size = 12, .hash = kPayloadHashHex});
adlr@google.com3defe6a2009-12-04 20:57:17 +0000222 in.more_info_url = "http://more/info";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223 in.prompt = true;
Darin Petkov6c118642010-10-21 12:06:30 -0700224 in.deadline = "some-deadline";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000225 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700226 fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
227 EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800228 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
229 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800230 EXPECT_EQ(1U, install_plan.target_slot);
Darin Petkov6c118642010-10-21 12:06:30 -0700231 string deadline;
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700232 EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
Darin Petkov6c118642010-10-21 12:06:30 -0700233 EXPECT_EQ("some-deadline", deadline);
Chris Sosafb1020e2013-07-29 17:27:33 -0700234 EXPECT_EQ(in.version, install_plan.version);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000235 }
236}
237
238TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000240 in.update_exists = false;
241 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700242 EXPECT_FALSE(DoTest(in, "", &install_plan));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700243 EXPECT_TRUE(install_plan.partitions.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000244}
245
Sen Jiang0affc2c2017-02-10 15:55:05 -0800246TEST_F(OmahaResponseHandlerActionTest, MultiPackageTest) {
247 OmahaResponse in;
248 in.update_exists = true;
249 in.version = "a.b.c.d";
250 in.packages.push_back({.payload_urls = {"http://package/1"},
251 .size = 1,
252 .hash = kPayloadHashHex});
253 in.packages.push_back({.payload_urls = {"http://package/2"},
254 .size = 2,
255 .hash = kPayloadHashHex});
256 in.more_info_url = "http://more/info";
257 InstallPlan install_plan;
258 EXPECT_TRUE(DoTest(in, "", &install_plan));
259 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
260 EXPECT_EQ(2u, install_plan.payloads.size());
261 EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
262 EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
263 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
264 EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
265 EXPECT_EQ(in.version, install_plan.version);
266}
267
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800268TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) {
269 OmahaResponse in;
270 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700271 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800272 in.packages.push_back(
273 {.payload_urls = {"http://test.should/need/hash.checks.signed"},
274 .size = 12,
275 .hash = kPayloadHashHex});
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800276 in.more_info_url = "http://more/info";
David Pursell02c18642014-11-06 11:26:11 -0800277 // Hash checks are always skipped for non-official update URLs.
Alex Deymo763e7db2015-08-27 21:08:08 -0700278 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800279 IsUpdateUrlOfficial())
280 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800281 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700282 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800283 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
284 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800285 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700286 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800287}
288
David Pursell02c18642014-11-06 11:26:11 -0800289TEST_F(OmahaResponseHandlerActionTest, HashChecksForUnofficialUpdateUrl) {
290 OmahaResponse in;
291 in.update_exists = true;
292 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800293 in.packages.push_back(
294 {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
295 .size = 12,
296 .hash = kPayloadHashHex});
David Pursell02c18642014-11-06 11:26:11 -0800297 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700298 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800299 IsUpdateUrlOfficial())
300 .WillRepeatedly(Return(false));
301 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700302 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800303 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
304 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
David Pursell02c18642014-11-06 11:26:11 -0800305 EXPECT_FALSE(install_plan.hash_checks_mandatory);
306 EXPECT_EQ(in.version, install_plan.version);
307}
308
David Pursell907b4fa2015-01-27 10:27:38 -0800309TEST_F(OmahaResponseHandlerActionTest,
310 HashChecksForOfficialUrlUnofficialBuildTest) {
311 // Official URLs for unofficial builds (dev/test images) don't require hash.
312 OmahaResponse in;
313 in.update_exists = true;
314 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800315 in.packages.push_back(
316 {.payload_urls = {"http://url.normally/needs/hash.checks.signed"},
317 .size = 12,
318 .hash = kPayloadHashHex});
David Pursell907b4fa2015-01-27 10:27:38 -0800319 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700320 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell907b4fa2015-01-27 10:27:38 -0800321 IsUpdateUrlOfficial())
322 .WillRepeatedly(Return(true));
Alex Deymo763e7db2015-08-27 21:08:08 -0700323 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
David Pursell907b4fa2015-01-27 10:27:38 -0800324 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700325 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800326 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
327 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
David Pursell907b4fa2015-01-27 10:27:38 -0800328 EXPECT_FALSE(install_plan.hash_checks_mandatory);
329 EXPECT_EQ(in.version, install_plan.version);
330}
331
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800332TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) {
333 OmahaResponse in;
334 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700335 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800336 in.packages.push_back(
337 {.payload_urls = {"https://test.should/need/hash.checks.signed"},
338 .size = 12,
339 .hash = kPayloadHashHex});
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800340 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700341 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800342 IsUpdateUrlOfficial())
343 .WillRepeatedly(Return(true));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800344 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700345 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800346 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
347 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800348 EXPECT_FALSE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700349 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800350}
351
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800352TEST_F(OmahaResponseHandlerActionTest, HashChecksForBothHttpAndHttpsTest) {
353 OmahaResponse in;
354 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700355 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800356 in.packages.push_back(
357 {.payload_urls = {"http://test.should.still/need/hash.checks",
358 "https://test.should.still/need/hash.checks"},
359 .size = 12,
360 .hash = kPayloadHashHex});
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800361 in.more_info_url = "http://more/info";
Alex Deymo763e7db2015-08-27 21:08:08 -0700362 EXPECT_CALL(*(fake_system_state_.mock_request_params()),
David Pursell02c18642014-11-06 11:26:11 -0800363 IsUpdateUrlOfficial())
364 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800365 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700366 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800367 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
368 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800369 EXPECT_TRUE(install_plan.hash_checks_mandatory);
Chris Sosafb1020e2013-07-29 17:27:33 -0700370 EXPECT_EQ(in.version, install_plan.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800371}
372
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700373TEST_F(OmahaResponseHandlerActionTest, ChangeToMoreStableChannelTest) {
374 OmahaResponse in;
375 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700376 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800377 in.packages.push_back({.payload_urls = {"https://MoreStableChannelTest"},
378 .size = 1,
379 .hash = kPayloadHashHex});
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700380 in.more_info_url = "http://more/info";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700381
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700382 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700383 base::ScopedTempDir tempdir;
384 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700385
Alex Deymo763e7db2015-08-27 21:08:08 -0700386 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700387 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Sen Jiang297e5832016-03-17 14:45:51 -0700388 params.set_root(tempdir.path().value());
389 params.set_current_channel("canary-channel");
390 // The ImageProperties in Android uses prefs to store MutableImageProperties.
391#ifdef __ANDROID__
392 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetString(_, "stable-channel"))
393 .WillOnce(Return(true));
394 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, true))
395 .WillOnce(Return(true));
396#endif // __ANDROID__
397 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
398 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700399 EXPECT_TRUE(params.to_more_stable_channel());
400 EXPECT_TRUE(params.is_powerwash_allowed());
401
Alex Deymo763e7db2015-08-27 21:08:08 -0700402 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700403 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700404 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700405 EXPECT_TRUE(install_plan.powerwash_required);
406}
407
408TEST_F(OmahaResponseHandlerActionTest, ChangeToLessStableChannelTest) {
409 OmahaResponse in;
410 in.update_exists = true;
Chris Sosa3b748432013-06-20 16:42:59 -0700411 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800412 in.packages.push_back({.payload_urls = {"https://LessStableChannelTest"},
413 .size = 15,
414 .hash = kPayloadHashHex});
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700415 in.more_info_url = "http://more/info";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700416
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700417 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -0700418 base::ScopedTempDir tempdir;
419 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700420
Alex Deymo763e7db2015-08-27 21:08:08 -0700421 OmahaRequestParams params(&fake_system_state_);
Alex Deymo85616652015-10-15 18:48:31 -0700422 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
Sen Jiang297e5832016-03-17 14:45:51 -0700423 params.set_root(tempdir.path().value());
424 params.set_current_channel("stable-channel");
425 // The ImageProperties in Android uses prefs to store MutableImageProperties.
426#ifdef __ANDROID__
427 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetString(_, "canary-channel"))
428 .WillOnce(Return(true));
429 EXPECT_CALL(*fake_system_state_.mock_prefs(), SetBoolean(_, false))
430 .WillOnce(Return(true));
431#endif // __ANDROID__
432 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
433 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700434 EXPECT_FALSE(params.to_more_stable_channel());
435 EXPECT_FALSE(params.is_powerwash_allowed());
436
Alex Deymo763e7db2015-08-27 21:08:08 -0700437 fake_system_state_.set_request_params(&params);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700438 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700439 EXPECT_TRUE(DoTest(in, "", &install_plan));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700440 EXPECT_FALSE(install_plan.powerwash_required);
441}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800442
David Zeuthen8f191b22013-08-06 12:27:50 -0700443TEST_F(OmahaResponseHandlerActionTest, P2PUrlIsUsedAndHashChecksMandatory) {
444 OmahaResponse in;
445 in.update_exists = true;
446 in.version = "a.b.c.d";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800447 in.packages.push_back(
448 {.payload_urls = {"https://would.not/cause/hash/checks"},
449 .size = 12,
450 .hash = kPayloadHashHex});
David Zeuthen8f191b22013-08-06 12:27:50 -0700451 in.more_info_url = "http://more/info";
David Zeuthen8f191b22013-08-06 12:27:50 -0700452
Alex Deymo763e7db2015-08-27 21:08:08 -0700453 OmahaRequestParams params(&fake_system_state_);
David Pursell02c18642014-11-06 11:26:11 -0800454 // We're using a real OmahaRequestParams object here so we can't mock
455 // IsUpdateUrlOfficial(), but setting the update URL to the AutoUpdate test
456 // server will cause IsUpdateUrlOfficial() to return true.
Alex Deymoac41a822015-09-15 20:52:53 -0700457 params.set_update_url(constants::kOmahaDefaultAUTestURL);
Alex Deymo763e7db2015-08-27 21:08:08 -0700458 fake_system_state_.set_request_params(&params);
David Zeuthen8f191b22013-08-06 12:27:50 -0700459
Alex Deymo763e7db2015-08-27 21:08:08 -0700460 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700461 SetUsingP2PForDownloading(true));
462
David Zeuthen8f191b22013-08-06 12:27:50 -0700463 string p2p_url = "http://9.8.7.6/p2p";
Alex Deymo763e7db2015-08-27 21:08:08 -0700464 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetP2PUrl())
Gilad Arnold74b5f552014-10-07 08:17:16 -0700465 .WillRepeatedly(Return(p2p_url));
Alex Deymo763e7db2015-08-27 21:08:08 -0700466 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700467 GetUsingP2PForDownloading()).WillRepeatedly(Return(true));
David Zeuthen8f191b22013-08-06 12:27:50 -0700468
469 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700470 EXPECT_TRUE(DoTest(in, "", &install_plan));
Sen Jiang0affc2c2017-02-10 15:55:05 -0800471 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
Sen Jiang2703ef42017-03-16 13:36:21 -0700472 EXPECT_EQ(p2p_url, install_plan.download_url);
David Zeuthen8f191b22013-08-06 12:27:50 -0700473 EXPECT_TRUE(install_plan.hash_checks_mandatory);
474}
475
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700476TEST_F(OmahaResponseHandlerActionTest, SystemVersionTest) {
477 OmahaResponse in;
478 in.update_exists = true;
479 in.version = "a.b.c.d";
480 in.system_version = "b.c.d.e";
481 in.packages.push_back({.payload_urls = {"http://package/1"},
482 .size = 1,
483 .hash = kPayloadHashHex});
484 in.packages.push_back({.payload_urls = {"http://package/2"},
485 .size = 2,
486 .hash = kPayloadHashHex});
487 in.more_info_url = "http://more/info";
488 InstallPlan install_plan;
489 EXPECT_TRUE(DoTest(in, "", &install_plan));
490 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
491 EXPECT_EQ(2u, install_plan.payloads.size());
492 EXPECT_EQ(in.packages[0].size, install_plan.payloads[0].size);
493 EXPECT_EQ(in.packages[1].size, install_plan.payloads[1].size);
494 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
495 EXPECT_EQ(expected_hash_, install_plan.payloads[1].hash);
496 EXPECT_EQ(in.version, install_plan.version);
497 EXPECT_EQ(in.system_version, install_plan.system_version);
498}
499
Aaron Wood23bd3392017-10-06 14:48:25 -0700500TEST_F(OmahaResponseHandlerActionTest, TestDeferredByPolicy) {
501 OmahaResponse in;
502 in.update_exists = true;
503 in.version = "a.b.c.d";
504 in.packages.push_back({.payload_urls = {"http://foo/the_update_a.b.c.d.tgz"},
505 .size = 12,
506 .hash = kPayloadHashHex});
507 // Setup the UpdateManager to disallow the update.
508 FakeClock fake_clock;
509 MockPolicy* mock_policy = new MockPolicy(&fake_clock);
510 FakeUpdateManager* fake_update_manager =
511 fake_system_state_.fake_update_manager();
512 fake_update_manager->set_policy(mock_policy);
513 EXPECT_CALL(*mock_policy, UpdateCanBeApplied(_, _, _, _, _))
514 .WillOnce(
515 DoAll(SetArgPointee<3>(ErrorCode::kOmahaUpdateDeferredPerPolicy),
516 Return(EvalStatus::kSucceeded)));
517 // Perform the Action. It should "fail" with kOmahaUpdateDeferredPerPolicy.
518 InstallPlan install_plan;
519 EXPECT_FALSE(DoTest(in, "", &install_plan));
520 EXPECT_EQ(ErrorCode::kOmahaUpdateDeferredPerPolicy, action_result_code_);
521 // Verify that DoTest() didn't set the output install plan.
522 EXPECT_EQ("", install_plan.version);
523 // Copy the underlying InstallPlan from the Action (like a real Delegate).
524 install_plan = action_->install_plan();
525 // Now verify the InstallPlan that was generated.
526 EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
527 EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
528 EXPECT_EQ(1U, install_plan.target_slot);
529 EXPECT_EQ(in.version, install_plan.version);
530}
531
adlr@google.com3defe6a2009-12-04 20:57:17 +0000532} // namespace chromeos_update_engine