blob: 107c6e2bc928a702abde31a12c6f4b92a7c939e1 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/payload_state.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080018
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080020#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070026#include "update_engine/common/excluder_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/fake_clock.h"
28#include "update_engine/common/fake_hardware.h"
29#include "update_engine/common/fake_prefs.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070030#include "update_engine/common/metrics_reporter_interface.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070031#include "update_engine/common/mock_excluder.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/mock_prefs.h"
33#include "update_engine/common/prefs.h"
34#include "update_engine/common/test_utils.h"
35#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070036#include "update_engine/cros/fake_system_state.h"
37#include "update_engine/cros/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038
Jay Srinivasan08262882012-12-28 19:29:43 -080039using base::Time;
40using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041using std::string;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080042using testing::_;
Alex Deymo42432912013-07-12 20:21:15 -070043using testing::AnyNumber;
44using testing::AtLeast;
45using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080047using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070048using testing::SetArgPointee;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070049using testing::StrictMock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050
51namespace chromeos_update_engine {
52
Jay Srinivasan19409b72013-04-12 19:23:36 -070053const char* kCurrentBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080054 "current-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070055const char* kTotalBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080056 "total-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070057const char* kCurrentBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080058 "current-bytes-downloaded-from-HttpServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070059const char* kTotalBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080060 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070061const char* kCurrentBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080062 "current-bytes-downloaded-from-HttpPeer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070063const char* kTotalBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070065
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080066static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070067 bool http_enabled,
Sen Jiangcdd52062017-05-18 15:33:10 -070068 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080069 PayloadState* payload_state,
70 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080071 response->packages.clear();
72 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
73 .size = 523456789,
74 .metadata_size = 558123,
75 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070076 .hash = hash,
77 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080078 response->max_failure_count_per_url = 3;
79 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080080 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070081
82 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080083 " NumURLs = 1\n"
84 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070085
86 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080087 " NumURLs = 2\n"
88 " Candidate Url0 = http://test\n"
89 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070090
Sen Jiang0affc2c2017-02-10 15:55:05 -080091 string expected_response_sign = base::StringPrintf(
92 "Payload 0:\n"
93 " Size = 523456789\n"
94 " Sha256 Hash = %s\n"
95 " Metadata Size = 558123\n"
96 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070097 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080098 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080099 "Max Failure Count Per Url = %d\n"
100 "Disable Payload Backoff = %d\n",
101 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -0700102 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800103 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800104 response->max_failure_count_per_url,
105 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800106 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800107}
108
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800109class PayloadStateTest : public ::testing::Test {};
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800110
Amin Hassani5ff76a22020-11-05 12:55:38 -0800111TEST_F(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800112 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700113 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800114 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700115 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700116 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800117 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700118 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800119 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700120 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
121 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800123 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700124 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800125 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700126 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800127 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700128 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800129 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700130 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700132 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800133 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700134 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800135 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700136 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800137 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800138 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800139 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800140 "Max Failure Count Per Url = 0\n"
141 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700143 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800144 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
145 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700146 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800147}
148
Amin Hassani5ff76a22020-11-05 12:55:38 -0800149TEST_F(PayloadStateTest, SetResponseWorksWithSingleUrl) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800150 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800151 response.packages.push_back({.payload_urls = {"https://single.url.test"},
152 .size = 123456789,
153 .metadata_size = 58123,
154 .metadata_signature = "msign",
155 .hash = "hash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700156 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800157 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700158 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700159 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700160 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800161 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
162 .Times(AtLeast(1));
163 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
165 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
166 .Times(AtLeast(1));
167 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
168 .Times(AtLeast(1));
169 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
170 .Times(AtLeast(1));
171 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
172 .Times(AtLeast(1));
173 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
174 .Times(AtLeast(1));
175 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
176 .Times(AtLeast(1));
177 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700179 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800181 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800182 string expected_response_sign =
183 "Payload 0:\n"
184 " Size = 123456789\n"
185 " Sha256 Hash = hash\n"
186 " Metadata Size = 58123\n"
187 " Metadata Signature = msign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700188 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800189 " NumURLs = 1\n"
190 " Candidate Url0 = https://single.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800191 "Max Failure Count Per Url = 0\n"
192 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800193 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700194 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800195 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
196 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700197 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198}
199
Amin Hassani5ff76a22020-11-05 12:55:38 -0800200TEST_F(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800201 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800202 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
203 "https://multiple.url.test"},
204 .size = 523456789,
205 .metadata_size = 558123,
206 .metadata_signature = "metasign",
207 .hash = "rhash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700208 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800209 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700210 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700211 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700212 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800213 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
214 .Times(AtLeast(1));
215 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
216 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
222 .Times(AtLeast(1));
223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
224 .Times(AtLeast(1));
225 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700226
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800227 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700228 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800230 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800231 string expected_response_sign =
232 "Payload 0:\n"
233 " Size = 523456789\n"
234 " Sha256 Hash = rhash\n"
235 " Metadata Size = 558123\n"
236 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700237 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800238 " NumURLs = 2\n"
239 " Candidate Url0 = http://multiple.url.test\n"
240 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800241 "Max Failure Count Per Url = 0\n"
242 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800243 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700244 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800245 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
246 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700247 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800248}
249
Amin Hassani5ff76a22020-11-05 12:55:38 -0800250TEST_F(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800251 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700252 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800253 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800254 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800255
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700256 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800257 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700258 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800259 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700260 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800261 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700262 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800263 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700264 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800265 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700266 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700267
Chris Sosabe45bef2013-04-09 18:25:12 -0700268 // Reboots will be set
269 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
270
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800271 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700272 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
273 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800274
275 // Failure count should be called each times url index is set, so that's
276 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700277 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800278 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800279
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700280 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800281
282 // This does a SetResponse which causes all the states to be set to 0 for
283 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700284 SetupPayloadStateWith2Urls(
285 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700286 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287
288 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700289 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700291 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292
293 // Verify that on the next error, the URL index wraps around to 0.
294 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296
297 // Verify that on the next error, it again advances to 1.
298 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700300
301 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800302 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800303}
304
Amin Hassani5ff76a22020-11-05 12:55:38 -0800305TEST_F(PayloadStateTest, NewResponseResetsPayloadState) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700307 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800311
312 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700313 SetupPayloadStateWith2Urls(
314 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700315 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316
317 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700318 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700320 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800321 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322
323 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700324 SetupPayloadStateWith2Urls(
325 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700326 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327
Alex Deymob33b0f02013-08-08 21:10:02 -0700328 // Fake an error again.
329 payload_state.UpdateFailed(error);
330 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800331 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700332
Alex Deymob33b0f02013-08-08 21:10:02 -0700333 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700334 SetupPayloadStateWith2Urls(
335 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700336 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
337
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800338 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700339 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800340 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
341 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
342 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700343 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800344 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700345 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800346 EXPECT_EQ(
347 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
348 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700349 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800350}
351
Amin Hassani5ff76a22020-11-05 12:55:38 -0800352TEST_F(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800353 OmahaResponse response;
354 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700355 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700356 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800357 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800358
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700359 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700360 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800361 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700362 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800363 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700364 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800365 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800366
Alex Deymo820cc702013-06-28 15:43:46 -0700367 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800368 .Times(AtLeast(2));
Alex Deymo820cc702013-06-28 15:43:46 -0700369 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800370 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700371 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800372 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700373
Jay Srinivasan19409b72013-04-12 19:23:36 -0700374 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800375
Jay Srinivasan19409b72013-04-12 19:23:36 -0700376 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
377 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800378
Jay Srinivasan19409b72013-04-12 19:23:36 -0700379 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800380 .Times(AtLeast(7));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700381 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800382 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700383 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800384 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800385
Jay Srinivasan19409b72013-04-12 19:23:36 -0700386 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800387 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700388 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800389 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700390
Jay Srinivasan19409b72013-04-12 19:23:36 -0700391 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700392 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800393 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
394 .Times(AtLeast(1));
395 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
396 .Times(AtLeast(1));
397 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
398 .Times(AtLeast(1));
399 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
400 .Times(AtLeast(1));
401 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700402
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700403 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800404
Sen Jiangcdd52062017-05-18 15:33:10 -0700405 SetupPayloadStateWith2Urls(
406 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700407 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800408
409 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700410 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800411 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700412 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700413 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800414 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
415 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800416
417 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700418 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800419 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700420 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700421 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800422 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
423 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800424
425 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700426 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800427 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700428 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700429 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800430 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
431 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800432
433 // This should advance the URL index as we've reached the
434 // max failure count and reset the failure count for the new URL index.
435 // This should also wrap around the URL index and thus cause the payload
436 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700437 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800438 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700439 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700440 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800441 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
442 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800443 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800444
445 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700446 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800447 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700448 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700449 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800450 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
451 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800452 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800453
454 // This should advance the URL index and payload attempt number due to
455 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700456 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800457 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700458 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700459 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800460 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
461 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800462 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800463
464 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700465 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700466 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800467 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700468 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700469 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800470 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
471 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800472 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800473
474 // And that failure count should be reset when we download some bytes
475 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700476 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800477 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700478 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700479 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800480 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
481 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800482 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800483
484 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700485 SetupPayloadStateWith2Urls(
486 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700487 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800488
489 // Make sure the url index was reset to 0 because of the new response.
490 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700491 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700492 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800493 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
494 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800495 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800496}
497
Amin Hassani5ff76a22020-11-05 12:55:38 -0800498TEST_F(PayloadStateTest,
499 PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800500 OmahaResponse response;
501 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700502 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800503 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800504
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700505 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700506 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800507 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700508 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800509 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800510
Alex Deymo820cc702013-06-28 15:43:46 -0700511 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800512 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700513 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800514 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700515
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800516 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800517
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800518 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700519 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800520 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800521
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700522 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800523
Sen Jiangcdd52062017-05-18 15:33:10 -0700524 SetupPayloadStateWith2Urls(
525 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800526
527 // This should just advance the payload attempt number;
528 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700529 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800530 payload_state.DownloadComplete();
531 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700532 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
533 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800534 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
535 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700536}
537
Amin Hassani5ff76a22020-11-05 12:55:38 -0800538TEST_F(PayloadStateTest,
539 PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
Alex Deymo820cc702013-06-28 15:43:46 -0700540 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700541 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700542 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800543 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700544
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700545 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700546 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800547 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700548 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800549 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700550
551 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
552 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800553 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700554
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800555 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(1);
Alex Deymo820cc702013-06-28 15:43:46 -0700556
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800557 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700558 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800559 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700560
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700561 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700562
Sen Jiangcdd52062017-05-18 15:33:10 -0700563 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700564
565 // This should just advance the payload attempt number;
566 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
567 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
568 payload_state.DownloadComplete();
569 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
570 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700571 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800572 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
573 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800574}
575
Amin Hassani5ff76a22020-11-05 12:55:38 -0800576TEST_F(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800577 OmahaResponse response;
578 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700579 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800580
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700581 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700582 SetupPayloadStateWith2Urls(
583 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800584
585 // Generate enough events to advance URL index, failure count and
586 // payload attempt number all to 1.
587 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700588 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
589 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800590 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700591 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700592 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800593 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
594 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800595
596 // Now, simulate a corrupted url index on persisted store which gets
597 // loaded when update_engine restarts. Using a different prefs object
598 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700599 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800600 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700601 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700602 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700603 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800604 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700605 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800606 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700607 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
Ben Chan672c1f52017-10-23 15:41:39 -0700608 .WillRepeatedly(DoAll(SetArgPointee<1>(2), Return(true)));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700609 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800610 .Times(AtLeast(1));
611 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800612
613 // Note: This will be a different payload object, but the response should
614 // have the same hash as before so as to not trivially reset because the
615 // response was different. We want to specifically test that even if the
616 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700617 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Sen Jiangcdd52062017-05-18 15:33:10 -0700618 SetupPayloadStateWith2Urls(
619 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800620
621 // Make sure all counters get reset to 0 because of the corrupted URL index
622 // we supplied above.
623 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700624 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700625 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800626 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
627 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800628}
Jay Srinivasan08262882012-12-28 19:29:43 -0800629
Amin Hassani5ff76a22020-11-05 12:55:38 -0800630TEST_F(PayloadStateTest, NoBackoffInteractiveChecks) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700631 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700632 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700633 FakeSystemState fake_system_state;
634 OmahaRequestParams params(&fake_system_state);
Amin Hassanie8153632020-10-27 15:11:28 -0700635 params.Init("", "", {.interactive = true});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700636 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700637
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700638 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700639 SetupPayloadStateWith2Urls(
640 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700641
642 // Simulate two failures (enough to cause payload backoff) and check
643 // again that we're ready to re-download without any backoff as this is
644 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700645 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
646 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700647 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
648 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
649 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
650 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
651}
652
Amin Hassani5ff76a22020-11-05 12:55:38 -0800653TEST_F(PayloadStateTest, NoBackoffForP2PUpdates) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700654 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700655 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700656 FakeSystemState fake_system_state;
657 OmahaRequestParams params(&fake_system_state);
Amin Hassanie8153632020-10-27 15:11:28 -0700658 params.Init("", "", {});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700660
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700661 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700662 SetupPayloadStateWith2Urls(
663 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700664
665 // Simulate two failures (enough to cause payload backoff) and check
666 // again that we're ready to re-download without any backoff as this is
667 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700668 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
669 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700670 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
671 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
672 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
673 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700674 payload_state.SetUsingP2PForDownloading(true);
675 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700676 // Should not backoff for p2p updates.
677 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
678
Gilad Arnold74b5f552014-10-07 08:17:16 -0700679 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700680 // No actual p2p update if no url is provided.
681 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
682}
683
Amin Hassani5ff76a22020-11-05 12:55:38 -0800684TEST_F(PayloadStateTest, NoBackoffForDeltaPayloads) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800685 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800686 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700687 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800688
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700689 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700690 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800691
692 // Simulate a successful download and see that we're ready to download
693 // again without any backoff as this is a delta payload.
694 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700695 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
696 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800697 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
698
699 // Simulate two failures (enough to cause payload backoff) and check
700 // again that we're ready to re-download without any backoff as this is
701 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700702 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
703 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700704 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700705 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
706 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800707 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
708}
709
710static void CheckPayloadBackoffState(PayloadState* payload_state,
711 int expected_attempt_number,
712 TimeDelta expected_days) {
713 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700714 EXPECT_EQ(expected_attempt_number,
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800715 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800716 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
717 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
718 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
719 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
720 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
721 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
722 EXPECT_LT(expected_min_time.ToInternalValue(),
723 backoff_expiry_time.ToInternalValue());
724 EXPECT_GT(expected_max_time.ToInternalValue(),
725 backoff_expiry_time.ToInternalValue());
726}
727
Amin Hassani5ff76a22020-11-05 12:55:38 -0800728TEST_F(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800729 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800730 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700731 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800732
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700733 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700734 SetupPayloadStateWith2Urls(
735 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800736
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800737 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
738 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
739 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
740 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
741 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
742 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
743 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
744 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
745 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
746 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
Jay Srinivasan08262882012-12-28 19:29:43 -0800747}
748
Amin Hassani5ff76a22020-11-05 12:55:38 -0800749TEST_F(PayloadStateTest, BackoffLogicCanBeDisabled) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800750 OmahaResponse response;
751 response.disable_payload_backoff = true;
752 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700753 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800754
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700755 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700756 SetupPayloadStateWith2Urls(
757 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800758
759 // Simulate a successful download and see that we are ready to download
760 // again without any backoff.
761 payload_state.DownloadComplete();
762 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700763 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800764 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
765
766 // Test again, this time by simulating two errors that would cause
767 // the payload attempt number to increment due to wrap around. And
768 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700769 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
770 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800771 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700772 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800773 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
774}
775
Amin Hassani5ff76a22020-11-05 12:55:38 -0800776TEST_F(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700777 OmahaResponse response;
778 response.disable_payload_backoff = true;
779 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700780 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800781 uint64_t https_total = 0;
782 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700783
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700784 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700785 SetupPayloadStateWith2Urls(
786 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700787 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700788
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700789 // Simulate a previous attempt with in order to set an initial non-zero value
790 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800791 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700792 http_total += prev_chunk;
793 payload_state.DownloadProgress(prev_chunk);
794
795 // Ensure that the initial values for HTTP reflect this attempt.
796 EXPECT_EQ(prev_chunk,
797 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
798 EXPECT_EQ(http_total,
799 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
800
801 // Change the response hash so as to simulate a new response which will
802 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700803 SetupPayloadStateWith2Urls(
804 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700805 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700806
807 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800808 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700809 http_total += first_chunk;
810 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700811 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700812 EXPECT_EQ(first_chunk,
813 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
814 EXPECT_EQ(http_total,
815 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800816 EXPECT_EQ(
817 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700818 EXPECT_EQ(https_total,
819 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
820
821 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700822 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700823 payload_state.UpdateFailed(error);
824
Jay Srinivasan53173b92013-05-17 17:13:01 -0700825 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800826 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700827 https_total += second_chunk;
828 payload_state.DownloadProgress(second_chunk);
829 EXPECT_EQ(first_chunk,
830 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
831 EXPECT_EQ(http_total,
832 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800833 EXPECT_EQ(
834 second_chunk,
835 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700836 EXPECT_EQ(https_total,
837 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
838
839 // Simulate error to go back to http.
840 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800841 uint64_t third_chunk = 32345678;
842 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700843 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700844 payload_state.DownloadProgress(third_chunk);
845
846 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
847 EXPECT_EQ(http_chunk,
848 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700849 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700850 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800851 EXPECT_EQ(
852 second_chunk,
853 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700854 EXPECT_EQ(https_total,
855 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
856
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700857 // Simulate error (will cause URL switch), set p2p is to be used and
858 // then do 42MB worth of progress
859 payload_state.UpdateFailed(error);
860 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800861 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700862 payload_state.DownloadProgress(p2p_total);
863
864 EXPECT_EQ(p2p_total,
865 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
866
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700867 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
868 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -0700869 1, _, kPayloadTypeFull, _, _, 314, _, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700870
871 payload_state.UpdateSucceeded();
872
873 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800874 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700875 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800876 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700877 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800878 EXPECT_EQ(
879 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800880 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700881 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700882 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700883}
884
Amin Hassani5ff76a22020-11-05 12:55:38 -0800885TEST_F(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700886 OmahaResponse response;
887 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700888 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700889
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700890 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700891 SetupPayloadStateWith2Urls(
892 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700893
894 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800895 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700896 payload_state.DownloadProgress(num_bytes);
897
898 // Check that this was done via HTTP.
899 EXPECT_EQ(num_bytes,
900 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
901 EXPECT_EQ(num_bytes,
902 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
903
904 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700905 int64_t total_bytes[kNumDownloadSources] = {};
906 total_bytes[kDownloadSourceHttpServer] = num_bytes;
907
908 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
909 ReportSuccessfulUpdateMetrics(
Tianjie Xud4777a12017-10-24 14:54:18 -0700910 _,
911 _,
912 _,
913 _,
914 test_utils::DownloadSourceMatcher(total_bytes),
915 _,
916 _,
917 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700918 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700919 _))
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700920 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700921
922 payload_state.UpdateSucceeded();
923}
924
Amin Hassani5ff76a22020-11-05 12:55:38 -0800925TEST_F(PayloadStateTest, RestartingUpdateResetsMetrics) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700926 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700927 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700928 PayloadState payload_state;
929
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700930 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700931
932 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700933 SetupPayloadStateWith2Urls(
934 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700935
Alex Deymo80f70ff2016-02-10 16:08:11 -0800936 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700937 payload_state.DownloadProgress(num_bytes);
938 EXPECT_EQ(num_bytes,
939 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
940 EXPECT_EQ(num_bytes,
941 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800942 EXPECT_EQ(
943 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800944 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700945 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
946
947 payload_state.UpdateRestarted();
948 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800949 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700950 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
951 EXPECT_EQ(num_bytes,
952 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
953}
954
Amin Hassani5ff76a22020-11-05 12:55:38 -0800955TEST_F(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700956 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700957 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700958
Alex Deymo8427b4a2014-11-05 14:00:32 -0800959 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700960 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700961 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
962
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700963 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700964
965 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800966 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700967
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700968 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700969 payload_state.UpdateResumed();
970 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800971 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700972
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700973 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700974 payload_state.UpdateResumed();
975 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800976 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700977
978 // Restart the update again to verify we set the num of reboots back to 0.
979 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800980 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700981}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700982
Amin Hassani5ff76a22020-11-05 12:55:38 -0800983TEST_F(PayloadStateTest, RollbackHappened) {
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200984 FakeSystemState fake_system_state;
985 PayloadState payload_state;
986
987 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
988 fake_system_state.mock_powerwash_safe_prefs();
989 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
990
991 // Verify pre-conditions are good.
992 EXPECT_FALSE(payload_state.GetRollbackHappened());
993
994 // Set to true.
995 EXPECT_CALL(*mock_powerwash_safe_prefs,
996 SetBoolean(kPrefsRollbackHappened, true));
997 payload_state.SetRollbackHappened(true);
998 EXPECT_TRUE(payload_state.GetRollbackHappened());
999
1000 // Set to false.
1001 EXPECT_CALL(*mock_powerwash_safe_prefs, Delete(kPrefsRollbackHappened));
1002 payload_state.SetRollbackHappened(false);
1003 EXPECT_FALSE(payload_state.GetRollbackHappened());
1004
1005 // Let's verify we can reload it correctly.
1006 EXPECT_CALL(*mock_powerwash_safe_prefs, GetBoolean(kPrefsRollbackHappened, _))
1007 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
1008 EXPECT_CALL(*mock_powerwash_safe_prefs,
1009 SetBoolean(kPrefsRollbackHappened, true));
1010 payload_state.LoadRollbackHappened();
1011 EXPECT_TRUE(payload_state.GetRollbackHappened());
1012}
1013
Amin Hassani5ff76a22020-11-05 12:55:38 -08001014TEST_F(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001015 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -07001016 PayloadState payload_state;
1017
Alex Deymo8427b4a2014-11-05 14:00:32 -08001018 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001019 fake_system_state.mock_powerwash_safe_prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -07001020
Andrewcc6ab9f2020-06-25 07:41:40 -07001021 // Mock out the os version and make sure it's excluded correctly.
Chris Sosaaa18e162013-06-20 13:20:30 -07001022 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001023 OmahaRequestParams params(&fake_system_state);
Amin Hassanie8153632020-10-27 15:11:28 -07001024 params.Init(rollback_version, "", {});
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001025 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001026
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001027 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1028
1029 // Verify pre-conditions are good.
1030 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1031
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001032 EXPECT_CALL(*mock_powerwash_safe_prefs,
1033 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosaaa18e162013-06-20 13:20:30 -07001034 payload_state.Rollback();
1035
1036 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001037
1038 // Change it up a little and verify we load it correctly.
1039 rollback_version = "2345.0.1";
1040 // Let's verify we can reload it correctly.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001041 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(kPrefsRollbackVersion, _))
1042 .WillOnce(DoAll(SetArgPointee<1>(rollback_version), Return(true)));
1043 EXPECT_CALL(*mock_powerwash_safe_prefs,
1044 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001045 payload_state.LoadRollbackVersion();
1046 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001047
David Zeuthen96197df2014-04-16 12:22:39 -07001048 // Check that we report only UpdateEngine.Rollback.* metrics in
1049 // UpdateSucceeded().
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001050 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1051 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1052 .Times(1);
1053
David Zeuthenafed4a12014-04-09 15:28:44 -07001054 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001055}
1056
Amin Hassani5ff76a22020-11-05 12:55:38 -08001057TEST_F(PayloadStateTest, DurationsAreCorrect) {
David Zeuthenf413fe52013-04-22 14:04:39 -07001058 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001059 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001060 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001061 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001062 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001063 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001064
1065 // Set the clock to a well-known time - 1 second on the wall-clock
1066 // and 2 seconds on the monotonic clock
1067 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1068 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1069
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001070 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001071 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001072 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001073
1074 // Check that durations are correct for a successful update where
1075 // time has advanced 7 seconds on the wall clock and 4 seconds on
1076 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001077 SetupPayloadStateWith2Urls(
1078 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001079 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1080 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1081 payload_state.UpdateSucceeded();
1082 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1083 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1084
1085 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001086 SetupPayloadStateWith2Urls(
1087 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001088 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1089 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1090
1091 // Advance time a bit (10 secs), simulate download progress and
1092 // check that durations are updated.
1093 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1094 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1095 payload_state.DownloadProgress(10);
1096 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1097 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1098
1099 // Now simulate a reboot by resetting monotonic time (to 5000) and
1100 // creating a new PayloadState object and check that we load the
1101 // durations correctly (e.g. they are the same as before).
1102 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1103 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001104 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001105 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001106 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001107 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1108 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001109
1110 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1111 // and check that the durations are increased accordingly.
1112 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1113 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1114 payload_state2.UpdateSucceeded();
1115 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001116 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1117 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001118}
1119
Amin Hassani5ff76a22020-11-05 12:55:38 -08001120TEST_F(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
David Zeuthene4c58bf2013-06-18 17:26:50 -07001121 OmahaResponse response;
1122 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001123 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001124 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001125 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001126
1127 // Set the clock to a well-known time (t = 30 seconds).
Tianjie Xu90aaa102017-10-10 17:39:03 -07001128 fake_clock.SetMonotonicTime(
1129 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001130
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001131 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001132 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001133 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001134
1135 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001136 SetupPayloadStateWith2Urls(
1137 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001138 payload_state.UpdateSucceeded();
1139
1140 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001141 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001142
1143 // Now simulate a reboot and set the wallclock time to a later point
1144 // (t = 500 seconds). We do this by using a new PayloadState object
1145 // and checking that it emits the right UMA metric with the right
1146 // value.
Tianjie Xu90aaa102017-10-10 17:39:03 -07001147 fake_clock.SetMonotonicTime(
1148 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001149 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001150 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001151
1152 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001153 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1154 ReportTimeToReboot(7));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001155 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001156
1157 payload_state2.UpdateEngineStarted();
1158
1159 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001160 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001161}
1162
Amin Hassani5ff76a22020-11-05 12:55:38 -08001163TEST_F(PayloadStateTest, RestartAfterCrash) {
Alex Deymo569c4242013-07-24 12:01:01 -07001164 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001165 FakeSystemState fake_system_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001166 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001167 fake_system_state.set_metrics_reporter(&mock_metrics_reporter);
Alex Deymo8427b4a2014-11-05 14:00:32 -08001168 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001169
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001170 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001171
David Zeuthen4e1d1492014-04-25 13:12:27 -07001172 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001173 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1174 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1175 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1176 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1177 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1178 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1179 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001180 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001181
Alex Deymo569c4242013-07-24 12:01:01 -07001182 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001183 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001184
1185 payload_state.UpdateEngineStarted();
1186}
1187
Amin Hassani5ff76a22020-11-05 12:55:38 -08001188TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001189 PayloadState payload_state;
1190 FakeSystemState fake_system_state;
1191
1192 // If there's no marker at startup, ensure we don't report a metric.
1193 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001194 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1195 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1196 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001197 payload_state.UpdateEngineStarted();
1198}
1199
Amin Hassani5ff76a22020-11-05 12:55:38 -08001200TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001201 PayloadState payload_state;
1202 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001203 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001204
1205 // If we have a marker at startup, ensure it's reported and the
1206 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001207 fake_system_state.set_prefs(&fake_prefs);
1208 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001209
1210 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1211
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001212 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1213 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1214 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001215 payload_state.UpdateEngineStarted();
1216
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001217 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001218}
1219
Amin Hassani5ff76a22020-11-05 12:55:38 -08001220TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001221 PayloadState payload_state;
1222 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001223 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001224
1225 // Make sure the marker is written and cleared during an attempt and
1226 // also that we DO NOT emit the metric (since the attempt didn't end
1227 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001228 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001229 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001230 OmahaResponse response;
1231 response.packages.resize(1);
1232 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001233
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001234 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1235 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1236 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001237
1238 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001239 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001240
1241 payload_state.UpdateRestarted();
1242
1243 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001244 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001245
1246 payload_state.UpdateSucceeded();
1247
1248 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001249 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001250}
1251
Amin Hassani5ff76a22020-11-05 12:55:38 -08001252TEST_F(PayloadStateTest, CandidateUrlsComputedCorrectly) {
Jay Srinivasan53173b92013-05-17 17:13:01 -07001253 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001254 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001255 PayloadState payload_state;
1256
Jay Srinivasan53173b92013-05-17 17:13:01 -07001257 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001258 fake_system_state.set_device_policy(&disable_http_policy);
1259 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001260
1261 // Test with no device policy. Should default to allowing http.
1262 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1263 .WillRepeatedly(Return(false));
1264
1265 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001266 SetupPayloadStateWith2Urls(
1267 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001268
1269 // Check that we use the HTTP URL since there is no value set for allowing
1270 // http.
1271 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1272
1273 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001274 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001275 .WillRepeatedly(DoAll(SetArgPointee<0>(false), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001276
Chris Sosaf7d80042013-08-22 16:45:17 -07001277 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001278 SetupPayloadStateWith2Urls(
1279 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001280
1281 // Check that we skip the HTTP URL and use only the HTTPS url.
1282 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1283
1284 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001285 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001286 payload_state.UpdateFailed(error);
1287
1288 // Check that we still skip the HTTP URL and use only the HTTPS url.
1289 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001290 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001291
1292 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001293 SetupPayloadStateWith2Urls(
1294 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001295
1296 // Check that we still skip the HTTP URL and use only the HTTPS url.
1297 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1298
1299 // Now, pretend that the HTTP policy is turned on. We want to make sure
1300 // the new policy is honored.
1301 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001302 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001303 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001304 .WillRepeatedly(DoAll(SetArgPointee<0>(true), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001305
1306 // Now, set the same response using the same hash
1307 // so that we can test that the state is reset not because of the
1308 // hash but because of the policy change which results in candidate url
1309 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001310 SetupPayloadStateWith2Urls(
1311 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001312
1313 // Check that we use the HTTP URL now and the failure count is reset.
1314 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001315 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001316
1317 // Fake a failure and see if we're moving over to the HTTPS url and update
1318 // the URL switch count properly.
1319 payload_state.UpdateFailed(error);
1320 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001321 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1322 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001323}
1324
Amin Hassani5ff76a22020-11-05 12:55:38 -08001325TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001326 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001327 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001328 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001329
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001330 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001331 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001332
1333 // Simulate a successful download and update.
1334 payload_state.DownloadComplete();
Sen Jiang8712e962018-05-08 12:12:28 -07001335 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1336 ReportSuccessfulUpdateMetrics(
1337 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001338 payload_state.UpdateSucceeded();
1339
1340 // Mock the request to a request where the delta was disabled but Omaha sends
1341 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001342 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001343 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001344 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001345
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001346 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001347 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001348
1349 payload_state.DownloadComplete();
1350
Sen Jiang8712e962018-05-08 12:12:28 -07001351 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1352 ReportSuccessfulUpdateMetrics(
1353 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001354 payload_state.UpdateSucceeded();
1355}
1356
Amin Hassani5ff76a22020-11-05 12:55:38 -08001357TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001358 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001359 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001360 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001361
Alex Deymo1c656c42013-06-28 11:02:14 -07001362 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001363 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001364 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001365 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001366
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001367 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1368 SetupPayloadStateWith2Urls(
1369 "Hash6437", true, false, &payload_state, &response);
1370
Alex Deymo1c656c42013-06-28 11:02:14 -07001371 // Simulate a successful download and update.
1372 payload_state.DownloadComplete();
1373
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001374 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1375 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -07001376 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001377 payload_state.UpdateSucceeded();
1378}
1379
Amin Hassani5ff76a22020-11-05 12:55:38 -08001380TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001381 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001382 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001383 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001384
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001385 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001386 SetupPayloadStateWith2Urls(
1387 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001388
Alex Deymo820cc702013-06-28 15:43:46 -07001389 // Mock the request to a request where the delta is enabled, although the
1390 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001391 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001392 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001393 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001394
1395 // Simulate a successful download and update.
1396 payload_state.DownloadComplete();
1397
Sen Jiang8712e962018-05-08 12:12:28 -07001398 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1399 ReportSuccessfulUpdateMetrics(
1400 _, _, kPayloadTypeFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001401 payload_state.UpdateSucceeded();
1402}
1403
Amin Hassani5ff76a22020-11-05 12:55:38 -08001404TEST_F(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001405 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001406 OmahaResponse response;
1407 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001408 FakePrefs fake_prefs;
1409 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001410
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001411 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001412 SetupPayloadStateWith2Urls(
1413 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001414
1415 // Simulate a successful download and update.
1416 payload_state.DownloadComplete();
1417 payload_state.UpdateSucceeded();
1418 payload_state.ExpectRebootInNewVersion("Version:12345678");
1419
1420 // Reboot into the same environment to get an UMA metric with a value of 1.
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001421 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1422 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001423 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001424 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001425
1426 // Simulate a second update and reboot into the same environment, this should
1427 // send a value of 2.
1428 payload_state.ExpectRebootInNewVersion("Version:12345678");
1429
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001430 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1431 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001432 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001433 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001434
1435 // Simulate a third failed reboot to new version, but this time for a
1436 // different payload. This should send a value of 1 this time.
1437 payload_state.ExpectRebootInNewVersion("Version:3141592");
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001438 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1439 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001440 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001441 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001442}
1443
Amin Hassani5ff76a22020-11-05 12:55:38 -08001444TEST_F(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001445 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001446 OmahaResponse response;
1447 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001448 FakePrefs fake_prefs;
1449 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001450
Alex Deymo763e7db2015-08-27 21:08:08 -07001451 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1452 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001453
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001454 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001455 SetupPayloadStateWith2Urls(
1456 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001457
1458 // Simulate a successful download and update.
1459 payload_state.DownloadComplete();
1460 payload_state.UpdateSucceeded();
1461 payload_state.ExpectRebootInNewVersion("Version:12345678");
1462
1463 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001464 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001465
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001466 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1467 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001468 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001469 payload_state.ReportFailedBootIfNeeded();
1470
Alex Deymo763e7db2015-08-27 21:08:08 -07001471 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001472 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001473 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001474 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001475}
1476
Amin Hassani5ff76a22020-11-05 12:55:38 -08001477TEST_F(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001478 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001479 OmahaResponse response;
1480 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001481 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001482
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001483 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001484 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001485 SetupPayloadStateWith2Urls(
1486 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001487
1488 // Simulate a successful download and update.
1489 payload_state.DownloadComplete();
1490 payload_state.UpdateSucceeded();
1491 payload_state.ExpectRebootInNewVersion("Version:12345678");
1492
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001493 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1494 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001495 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001496
1497 // Cancel the applied update.
1498 payload_state.ResetUpdateStatus();
1499
1500 // Simulate a reboot.
1501 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001502}
1503
Amin Hassani5ff76a22020-11-05 12:55:38 -08001504TEST_F(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001505 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001506 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001507 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001508
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001509 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001510 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001511
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001512 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1513 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001514 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001515
1516 // Simulate a reboot in this environment.
1517 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001518}
1519
Amin Hassani5ff76a22020-11-05 12:55:38 -08001520TEST_F(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
David Zeuthendcba8092013-08-06 12:16:35 -07001521 OmahaResponse response;
1522 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001523 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001524 FakePrefs fake_prefs;
1525 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001526
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001527 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001528 SetupPayloadStateWith2Urls(
1529 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001530
1531 // Should allow exactly kMaxP2PAttempts...
1532 for (int n = 0; n < kMaxP2PAttempts; n++) {
1533 payload_state.P2PNewAttempt();
1534 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1535 }
1536 // ... but not more than that.
1537 payload_state.P2PNewAttempt();
1538 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001539}
1540
Amin Hassani5ff76a22020-11-05 12:55:38 -08001541TEST_F(PayloadStateTest, DisallowP2PAfterDeadline) {
David Zeuthendcba8092013-08-06 12:16:35 -07001542 OmahaResponse response;
1543 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001544 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001545 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001546 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001547
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001548 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001549 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001550 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001551 SetupPayloadStateWith2Urls(
1552 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001553
1554 // Set the clock to 1 second.
1555 Time epoch = Time::FromInternalValue(1000000);
1556 fake_clock.SetWallclockTime(epoch);
1557
1558 // Do an attempt - this will set the timestamp.
1559 payload_state.P2PNewAttempt();
1560
1561 // Check that the timestamp equals what we just set.
1562 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1563
1564 // Time hasn't advanced - this should work.
1565 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1566
1567 // Set clock to half the deadline - this should work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001568 fake_clock.SetWallclockTime(
1569 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
David Zeuthendcba8092013-08-06 12:16:35 -07001570 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1571
1572 // Check that the first attempt timestamp hasn't changed just
1573 // because the wall-clock time changed.
1574 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1575
1576 // Set clock to _just_ before the deadline - this should work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001577 fake_clock.SetWallclockTime(
1578 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001579 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1580
1581 // Set clock to _just_ after the deadline - this should not work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001582 fake_clock.SetWallclockTime(
1583 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001584 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001585}
1586
Amin Hassani5ff76a22020-11-05 12:55:38 -08001587TEST_F(PayloadStateTest, P2PStateVarsInitialValue) {
David Zeuthendcba8092013-08-06 12:16:35 -07001588 OmahaResponse response;
1589 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001590 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001591 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001592
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001593 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001594 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001595 SetupPayloadStateWith2Urls(
1596 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001597
1598 Time null_time = Time();
1599 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1600 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001601}
1602
Amin Hassani5ff76a22020-11-05 12:55:38 -08001603TEST_F(PayloadStateTest, P2PStateVarsArePersisted) {
David Zeuthendcba8092013-08-06 12:16:35 -07001604 OmahaResponse response;
1605 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001606 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001607 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001608 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001609 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001610 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001611 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001612 SetupPayloadStateWith2Urls(
1613 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001614
1615 // Set the clock to something known.
1616 Time time = Time::FromInternalValue(12345);
1617 fake_clock.SetWallclockTime(time);
1618
1619 // New p2p attempt - as a side-effect this will update the p2p state vars.
1620 payload_state.P2PNewAttempt();
1621 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1622 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1623
1624 // Now create a new PayloadState and check that it loads the state
1625 // vars correctly.
1626 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001627 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001628 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1629 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001630}
1631
Amin Hassani5ff76a22020-11-05 12:55:38 -08001632TEST_F(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
David Zeuthendcba8092013-08-06 12:16:35 -07001633 OmahaResponse response;
1634 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001635 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001636 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001637 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001638 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001639 fake_system_state.set_prefs(&fake_prefs);
1640
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001641 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001642 SetupPayloadStateWith2Urls(
1643 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001644
1645 // Set the clock to something known.
1646 Time time = Time::FromInternalValue(12345);
1647 fake_clock.SetWallclockTime(time);
1648
1649 // New p2p attempt - as a side-effect this will update the p2p state vars.
1650 payload_state.P2PNewAttempt();
1651 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1652 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1653
1654 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001655 SetupPayloadStateWith2Urls(
1656 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001657
1658 // ... and check that it clears the P2P state vars.
1659 Time null_time = Time();
1660 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1661 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001662}
1663
Amin Hassani5ff76a22020-11-05 12:55:38 -08001664TEST_F(PayloadStateTest, NextPayloadResetsUrlIndex) {
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001665 PayloadState payload_state;
1666 FakeSystemState fake_system_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001667 StrictMock<MockExcluder> mock_excluder;
1668 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1669 .WillOnce(Return(&mock_excluder));
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001670 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1671
1672 OmahaResponse response;
1673 response.packages.push_back(
1674 {.payload_urls = {"http://test1a", "http://test2a"},
1675 .size = 123456789,
1676 .metadata_size = 58123,
1677 .metadata_signature = "msign",
1678 .hash = "hash"});
1679 response.packages.push_back({.payload_urls = {"http://test1b"},
1680 .size = 123456789,
1681 .metadata_size = 58123,
1682 .metadata_signature = "msign",
1683 .hash = "hash"});
1684 payload_state.SetResponse(response);
1685
1686 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1a");
1687 payload_state.IncrementUrlIndex();
1688 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test2a");
1689
1690 EXPECT_TRUE(payload_state.NextPayload());
1691 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1b");
1692}
1693
Amin Hassani5ff76a22020-11-05 12:55:38 -08001694TEST_F(PayloadStateTest, ExcludeNoopForNonExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001695 PayloadState payload_state;
1696 FakeSystemState fake_system_state;
1697 StrictMock<MockExcluder> mock_excluder;
1698 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1699 .WillOnce(Return(&mock_excluder));
1700 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1701
1702 OmahaResponse response;
1703 response.packages.push_back(
1704 {.payload_urls = {"http://test1a", "http://test2a"},
1705 .size = 123456789,
1706 .metadata_size = 58123,
1707 .metadata_signature = "msign",
1708 .hash = "hash",
1709 .can_exclude = false});
1710 payload_state.SetResponse(response);
1711
1712 EXPECT_CALL(mock_excluder, Exclude(_)).Times(0);
1713 payload_state.ExcludeCurrentPayload();
1714}
1715
Amin Hassani5ff76a22020-11-05 12:55:38 -08001716TEST_F(PayloadStateTest, ExcludeOnlyCanExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001717 PayloadState payload_state;
1718 FakeSystemState fake_system_state;
1719 StrictMock<MockExcluder> mock_excluder;
1720 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1721 .WillOnce(Return(&mock_excluder));
1722 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1723
1724 OmahaResponse response;
1725 response.packages.push_back(
1726 {.payload_urls = {"http://test1a", "http://test2a"},
1727 .size = 123456789,
1728 .metadata_size = 58123,
1729 .metadata_signature = "msign",
1730 .hash = "hash",
1731 .can_exclude = true});
1732 payload_state.SetResponse(response);
1733
1734 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1735 .WillOnce(Return(true));
1736 payload_state.ExcludeCurrentPayload();
1737}
1738
Amin Hassani5ff76a22020-11-05 12:55:38 -08001739TEST_F(PayloadStateTest, IncrementFailureExclusionTest) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001740 PayloadState payload_state;
1741 FakeSystemState fake_system_state;
1742 StrictMock<MockExcluder> mock_excluder;
1743 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1744 .WillOnce(Return(&mock_excluder));
1745 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1746
1747 OmahaResponse response;
1748 // Critical package.
1749 response.packages.push_back(
1750 {.payload_urls = {"http://crit-test1a", "http://crit-test2a"},
1751 .size = 123456789,
1752 .metadata_size = 58123,
1753 .metadata_signature = "msign",
1754 .hash = "hash",
1755 .can_exclude = false});
1756 // Non-critical package.
1757 response.packages.push_back(
1758 {.payload_urls = {"http://test1a", "http://test2a"},
1759 .size = 123456789,
1760 .metadata_size = 58123,
1761 .metadata_signature = "msign",
1762 .hash = "hash",
1763 .can_exclude = true});
1764 response.max_failure_count_per_url = 2;
1765 payload_state.SetResponse(response);
1766
1767 // Critical package won't be excluded.
1768 // Increment twice as failure count allowed per URL is set to 2.
1769 payload_state.IncrementFailureCount();
1770 payload_state.IncrementFailureCount();
1771
1772 EXPECT_TRUE(payload_state.NextPayload());
1773
1774 // First increment failure should not exclude.
1775 payload_state.IncrementFailureCount();
1776
1777 // Second increment failure should exclude.
1778 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1779 .WillOnce(Return(true));
1780 payload_state.IncrementFailureCount();
1781}
1782
Amin Hassani5ff76a22020-11-05 12:55:38 -08001783TEST_F(PayloadStateTest, HaltExclusionPostPayloadExhaustion) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001784 PayloadState payload_state;
1785 FakeSystemState fake_system_state;
1786 StrictMock<MockExcluder> mock_excluder;
1787 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1788 .WillOnce(Return(&mock_excluder));
1789 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1790
1791 OmahaResponse response;
1792 // Non-critical package.
1793 response.packages.push_back(
1794 {.payload_urls = {"http://test1a", "http://test2a"},
1795 .size = 123456789,
1796 .metadata_size = 58123,
1797 .metadata_signature = "msign",
1798 .hash = "hash",
1799 .can_exclude = true});
1800 payload_state.SetResponse(response);
1801
1802 // Exclusion should be called when excluded.
1803 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1804 .WillOnce(Return(true));
1805 payload_state.ExcludeCurrentPayload();
1806
1807 // No more paylods to go through.
1808 EXPECT_FALSE(payload_state.NextPayload());
1809
1810 // Exclusion should not be called as all |Payload|s are exhausted.
1811 payload_state.ExcludeCurrentPayload();
1812}
1813
Amin Hassani5ff76a22020-11-05 12:55:38 -08001814TEST_F(PayloadStateTest, NonInfinitePayloadIndexIncrement) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001815 PayloadState payload_state;
1816 FakeSystemState fake_system_state;
1817 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1818
1819 payload_state.SetResponse({});
1820
1821 EXPECT_FALSE(payload_state.NextPayload());
1822 int payload_index = payload_state.payload_index_;
1823
1824 EXPECT_FALSE(payload_state.NextPayload());
1825 EXPECT_EQ(payload_index, payload_state.payload_index_);
1826}
1827
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001828} // namespace chromeos_update_engine