blob: 9370a026979ec344c75b14615b9268962431a623 [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_hardware.h"
28#include "update_engine/common/fake_prefs.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070029#include "update_engine/common/metrics_reporter_interface.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070030#include "update_engine/common/mock_excluder.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/common/mock_prefs.h"
32#include "update_engine/common/prefs.h"
33#include "update_engine/common/test_utils.h"
34#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070035#include "update_engine/cros/fake_system_state.h"
36#include "update_engine/cros/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080037
Jay Srinivasan08262882012-12-28 19:29:43 -080038using base::Time;
39using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080040using std::string;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080041using testing::_;
Alex Deymo42432912013-07-12 20:21:15 -070042using testing::AnyNumber;
43using testing::AtLeast;
44using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080045using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080046using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070047using testing::SetArgPointee;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070048using testing::StrictMock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049
50namespace chromeos_update_engine {
51
Jay Srinivasan19409b72013-04-12 19:23:36 -070052const char* kCurrentBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080053 "current-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070054const char* kTotalBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080055 "total-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070056const char* kCurrentBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080057 "current-bytes-downloaded-from-HttpServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070058const char* kTotalBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080059 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070060const char* kCurrentBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080061 "current-bytes-downloaded-from-HttpPeer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070062const char* kTotalBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080063 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070064
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080065static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070066 bool http_enabled,
Sen Jiangcdd52062017-05-18 15:33:10 -070067 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080068 PayloadState* payload_state,
69 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080070 response->packages.clear();
71 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
72 .size = 523456789,
73 .metadata_size = 558123,
74 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070075 .hash = hash,
76 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080077 response->max_failure_count_per_url = 3;
78 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080079 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070080
81 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080082 " NumURLs = 1\n"
83 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070084
85 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080086 " NumURLs = 2\n"
87 " Candidate Url0 = http://test\n"
88 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070089
Sen Jiang0affc2c2017-02-10 15:55:05 -080090 string expected_response_sign = base::StringPrintf(
91 "Payload 0:\n"
92 " Size = 523456789\n"
93 " Sha256 Hash = %s\n"
94 " Metadata Size = 558123\n"
95 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070096 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080097 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080098 "Max Failure Count Per Url = %d\n"
99 "Disable Payload Backoff = %d\n",
100 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -0700101 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800102 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800103 response->max_failure_count_per_url,
104 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800105 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800106}
107
Amin Hassani538bd592020-11-04 20:46:08 -0800108class PayloadStateTest : public ::testing::Test {
109 public:
110 void SetUp() { FakeSystemState::CreateInstance(); }
111};
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800112
Amin Hassani5ff76a22020-11-05 12:55:38 -0800113TEST_F(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800114 OmahaResponse response;
Amin Hassani538bd592020-11-04 20:46:08 -0800115 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700116 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700117 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800118 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700119 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800120 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700121 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
123 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800124 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700125 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800126 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700127 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800128 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700129 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800130 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700131 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800132 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700133 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800134 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700135 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800136 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800137 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800138 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800139 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800140 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800141 "Max Failure Count Per Url = 0\n"
142 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800143 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700144 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800145 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
146 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700147 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800148}
149
Amin Hassani5ff76a22020-11-05 12:55:38 -0800150TEST_F(PayloadStateTest, SetResponseWorksWithSingleUrl) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800151 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800152 response.packages.push_back({.payload_urls = {"https://single.url.test"},
153 .size = 123456789,
154 .metadata_size = 58123,
155 .metadata_signature = "msign",
156 .hash = "hash"});
Amin Hassani538bd592020-11-04 20:46:08 -0800157 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->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;
Amin Hassani538bd592020-11-04 20:46:08 -0800179 EXPECT_TRUE(payload_state.Initialize());
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"});
Amin Hassani538bd592020-11-04 20:46:08 -0800208 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700209 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700210 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700211 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800212 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
213 .Times(AtLeast(1));
214 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
215 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
216 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
217 .Times(AtLeast(1));
218 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
219 .Times(AtLeast(1));
220 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
221 .Times(AtLeast(1));
222 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
223 .Times(AtLeast(1));
224 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700225
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800226 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800227 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800229 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800230 string expected_response_sign =
231 "Payload 0:\n"
232 " Size = 523456789\n"
233 " Sha256 Hash = rhash\n"
234 " Metadata Size = 558123\n"
235 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700236 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800237 " NumURLs = 2\n"
238 " Candidate Url0 = http://multiple.url.test\n"
239 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800240 "Max Failure Count Per Url = 0\n"
241 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800242 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700243 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800244 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
245 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700246 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800247}
248
Amin Hassani5ff76a22020-11-05 12:55:38 -0800249TEST_F(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250 OmahaResponse response;
Amin Hassani538bd592020-11-04 20:46:08 -0800251 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800252 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800253
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700254 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800255 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700256 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800257 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700258 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800259 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700260 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800261 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700262 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800263 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700264 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700265
Chris Sosabe45bef2013-04-09 18:25:12 -0700266 // Reboots will be set
267 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
268
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800269 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700270 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
271 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800272
273 // Failure count should be called each times url index is set, so that's
274 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700275 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800276 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800277
Amin Hassani538bd592020-11-04 20:46:08 -0800278 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800279
280 // This does a SetResponse which causes all the states to be set to 0 for
281 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700282 SetupPayloadStateWith2Urls(
283 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700284 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800285
286 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700287 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800288 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700289 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290
291 // Verify that on the next error, the URL index wraps around to 0.
292 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700293 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294
295 // Verify that on the next error, it again advances to 1.
296 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700297 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700298
299 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800300 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800301}
302
Amin Hassani5ff76a22020-11-05 12:55:38 -0800303TEST_F(PayloadStateTest, NewResponseResetsPayloadState) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800304 OmahaResponse response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800305 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800306
Amin Hassani538bd592020-11-04 20:46:08 -0800307 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800308
309 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700310 SetupPayloadStateWith2Urls(
311 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700312 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800313
314 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700315 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700317 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800318 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319
320 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700321 SetupPayloadStateWith2Urls(
322 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700323 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800324
Alex Deymob33b0f02013-08-08 21:10:02 -0700325 // Fake an error again.
326 payload_state.UpdateFailed(error);
327 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800328 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700329
Alex Deymob33b0f02013-08-08 21:10:02 -0700330 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700331 SetupPayloadStateWith2Urls(
332 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700333 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
334
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800335 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700336 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800337 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
338 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
339 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700340 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800341 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700342 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800343 EXPECT_EQ(
344 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
345 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700346 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800347}
348
Amin Hassani5ff76a22020-11-05 12:55:38 -0800349TEST_F(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800350 OmahaResponse response;
351 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700352 int progress_bytes = 100;
Amin Hassani538bd592020-11-04 20:46:08 -0800353 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800354
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700355 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700356 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800357 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700358 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800359 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700360 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800361 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800362
Alex Deymo820cc702013-06-28 15:43:46 -0700363 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800364 .Times(AtLeast(2));
Alex Deymo820cc702013-06-28 15:43:46 -0700365 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800366 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700367 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800368 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700369
Jay Srinivasan19409b72013-04-12 19:23:36 -0700370 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800371
Jay Srinivasan19409b72013-04-12 19:23:36 -0700372 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
373 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800374
Jay Srinivasan19409b72013-04-12 19:23:36 -0700375 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800376 .Times(AtLeast(7));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700377 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800378 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700379 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800380 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800381
Jay Srinivasan19409b72013-04-12 19:23:36 -0700382 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800383 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700384 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800385 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700386
Jay Srinivasan19409b72013-04-12 19:23:36 -0700387 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700388 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800389 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
390 .Times(AtLeast(1));
391 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
392 .Times(AtLeast(1));
393 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
394 .Times(AtLeast(1));
395 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
396 .Times(AtLeast(1));
397 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700398
Amin Hassani538bd592020-11-04 20:46:08 -0800399 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800400
Sen Jiangcdd52062017-05-18 15:33:10 -0700401 SetupPayloadStateWith2Urls(
402 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700403 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800404
405 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700406 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800407 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700408 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700409 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800410 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
411 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800412
413 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700414 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800415 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700416 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700417 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800418 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
419 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800420
421 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700422 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800423 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700424 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700425 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800426 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
427 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800428
429 // This should advance the URL index as we've reached the
430 // max failure count and reset the failure count for the new URL index.
431 // This should also wrap around the URL index and thus cause the payload
432 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700433 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800434 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700435 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700436 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800437 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
438 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800439 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800440
441 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700442 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800443 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700444 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700445 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800446 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
447 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800448 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800449
450 // This should advance the URL index and payload attempt number due to
451 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700452 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800453 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700454 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700455 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800456 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
457 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800458 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800459
460 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700461 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700462 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800463 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700464 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700465 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800466 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
467 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800468 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800469
470 // And that failure count should be reset when we download some bytes
471 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700472 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800473 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700474 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700475 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800476 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
477 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800478 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800479
480 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700481 SetupPayloadStateWith2Urls(
482 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700483 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800484
485 // Make sure the url index was reset to 0 because of the new response.
486 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700487 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700488 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800489 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
490 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800491 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800492}
493
Amin Hassani5ff76a22020-11-05 12:55:38 -0800494TEST_F(PayloadStateTest,
495 PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800496 OmahaResponse response;
497 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800498 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800499
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700500 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700501 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800502 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700503 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800504 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800505
Alex Deymo820cc702013-06-28 15:43:46 -0700506 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800507 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700508 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800509 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700510
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800511 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800512
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800513 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700514 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800515 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800516
Amin Hassani538bd592020-11-04 20:46:08 -0800517 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800518
Sen Jiangcdd52062017-05-18 15:33:10 -0700519 SetupPayloadStateWith2Urls(
520 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800521
522 // This should just advance the payload attempt number;
523 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700524 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800525 payload_state.DownloadComplete();
526 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700527 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
528 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800529 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
530 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700531}
532
Amin Hassani5ff76a22020-11-05 12:55:38 -0800533TEST_F(PayloadStateTest,
534 PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
Alex Deymo820cc702013-06-28 15:43:46 -0700535 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700536 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800537 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700538
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700539 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700540 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800541 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700542 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800543 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700544
545 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
546 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800547 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700548
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800549 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(1);
Alex Deymo820cc702013-06-28 15:43:46 -0700550
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800551 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700552 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800553 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700554
Amin Hassani538bd592020-11-04 20:46:08 -0800555 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo820cc702013-06-28 15:43:46 -0700556
Sen Jiangcdd52062017-05-18 15:33:10 -0700557 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700558
559 // This should just advance the payload attempt number;
560 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
561 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
562 payload_state.DownloadComplete();
563 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
564 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700565 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800566 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
567 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800568}
569
Amin Hassani5ff76a22020-11-05 12:55:38 -0800570TEST_F(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800571 OmahaResponse response;
572 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800573
Amin Hassani538bd592020-11-04 20:46:08 -0800574 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700575 SetupPayloadStateWith2Urls(
576 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800577
578 // Generate enough events to advance URL index, failure count and
579 // payload attempt number all to 1.
580 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700581 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
582 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800583 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700584 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700585 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800586 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
587 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800588
589 // Now, simulate a corrupted url index on persisted store which gets
590 // loaded when update_engine restarts. Using a different prefs object
591 // so as to not bother accounting for the uninteresting calls above.
Amin Hassani538bd592020-11-04 20:46:08 -0800592 NiceMock<MockPrefs>* prefs2 = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700593 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700594 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700595 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800596 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700597 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800598 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700599 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
Ben Chan672c1f52017-10-23 15:41:39 -0700600 .WillRepeatedly(DoAll(SetArgPointee<1>(2), Return(true)));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700601 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800602 .Times(AtLeast(1));
603 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800604
605 // Note: This will be a different payload object, but the response should
606 // have the same hash as before so as to not trivially reset because the
607 // response was different. We want to specifically test that even if the
608 // response is same, we should reset the state if we find it corrupted.
Amin Hassani538bd592020-11-04 20:46:08 -0800609 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700610 SetupPayloadStateWith2Urls(
611 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800612
613 // Make sure all counters get reset to 0 because of the corrupted URL index
614 // we supplied above.
615 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700616 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700617 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800618 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
619 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800620}
Jay Srinivasan08262882012-12-28 19:29:43 -0800621
Amin Hassani5ff76a22020-11-05 12:55:38 -0800622TEST_F(PayloadStateTest, NoBackoffInteractiveChecks) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700623 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700624 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800625 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -0700626 params.Init("", "", {.interactive = true});
Amin Hassani538bd592020-11-04 20:46:08 -0800627 FakeSystemState::Get()->set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700628
Amin Hassani538bd592020-11-04 20:46:08 -0800629 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700630 SetupPayloadStateWith2Urls(
631 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700632
633 // Simulate two failures (enough to cause payload backoff) and check
634 // again that we're ready to re-download without any backoff as this is
635 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700636 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
637 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700638 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
639 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
640 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
641 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
642}
643
Amin Hassani5ff76a22020-11-05 12:55:38 -0800644TEST_F(PayloadStateTest, NoBackoffForP2PUpdates) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700645 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700646 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800647 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -0700648 params.Init("", "", {});
Amin Hassani538bd592020-11-04 20:46:08 -0800649 FakeSystemState::Get()->set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700650
Amin Hassani538bd592020-11-04 20:46:08 -0800651 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700652 SetupPayloadStateWith2Urls(
653 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700654
655 // Simulate two failures (enough to cause payload backoff) and check
656 // again that we're ready to re-download without any backoff as this is
657 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700658 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
659 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700660 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
661 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
662 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
663 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700664 payload_state.SetUsingP2PForDownloading(true);
665 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700666 // Should not backoff for p2p updates.
667 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
668
Gilad Arnold74b5f552014-10-07 08:17:16 -0700669 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700670 // No actual p2p update if no url is provided.
671 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
672}
673
Amin Hassani5ff76a22020-11-05 12:55:38 -0800674TEST_F(PayloadStateTest, NoBackoffForDeltaPayloads) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800675 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800676 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800677
Amin Hassani538bd592020-11-04 20:46:08 -0800678 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700679 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800680
681 // Simulate a successful download and see that we're ready to download
682 // again without any backoff as this is a delta payload.
683 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700684 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
685 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800686 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
687
688 // Simulate two failures (enough to cause payload backoff) and check
689 // again that we're ready to re-download without any backoff as this is
690 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700691 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
692 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700693 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700694 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
695 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800696 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
697}
698
699static void CheckPayloadBackoffState(PayloadState* payload_state,
700 int expected_attempt_number,
701 TimeDelta expected_days) {
702 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700703 EXPECT_EQ(expected_attempt_number,
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800704 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800705 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
706 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
707 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
708 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
709 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
710 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
711 EXPECT_LT(expected_min_time.ToInternalValue(),
712 backoff_expiry_time.ToInternalValue());
713 EXPECT_GT(expected_max_time.ToInternalValue(),
714 backoff_expiry_time.ToInternalValue());
715}
716
Amin Hassani5ff76a22020-11-05 12:55:38 -0800717TEST_F(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800718 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800719 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800720
Amin Hassani538bd592020-11-04 20:46:08 -0800721 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700722 SetupPayloadStateWith2Urls(
723 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800724
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800725 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
726 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
727 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
728 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
729 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
730 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
731 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
732 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
733 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
734 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
Jay Srinivasan08262882012-12-28 19:29:43 -0800735}
736
Amin Hassani5ff76a22020-11-05 12:55:38 -0800737TEST_F(PayloadStateTest, BackoffLogicCanBeDisabled) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800738 OmahaResponse response;
739 response.disable_payload_backoff = true;
740 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800741
Amin Hassani538bd592020-11-04 20:46:08 -0800742 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700743 SetupPayloadStateWith2Urls(
744 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800745
746 // Simulate a successful download and see that we are ready to download
747 // again without any backoff.
748 payload_state.DownloadComplete();
749 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700750 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800751 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
752
753 // Test again, this time by simulating two errors that would cause
754 // the payload attempt number to increment due to wrap around. And
755 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700756 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
757 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800758 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700759 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800760 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
761}
762
Amin Hassani5ff76a22020-11-05 12:55:38 -0800763TEST_F(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700764 OmahaResponse response;
765 response.disable_payload_backoff = true;
766 PayloadState payload_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800767 uint64_t https_total = 0;
768 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700769
Amin Hassani538bd592020-11-04 20:46:08 -0800770 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700771 SetupPayloadStateWith2Urls(
772 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700773 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700774
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700775 // Simulate a previous attempt with in order to set an initial non-zero value
776 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800777 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700778 http_total += prev_chunk;
779 payload_state.DownloadProgress(prev_chunk);
780
781 // Ensure that the initial values for HTTP reflect this attempt.
782 EXPECT_EQ(prev_chunk,
783 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
784 EXPECT_EQ(http_total,
785 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
786
787 // Change the response hash so as to simulate a new response which will
788 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700789 SetupPayloadStateWith2Urls(
790 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700791 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700792
793 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800794 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700795 http_total += first_chunk;
796 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700797 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700798 EXPECT_EQ(first_chunk,
799 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
800 EXPECT_EQ(http_total,
801 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800802 EXPECT_EQ(
803 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700804 EXPECT_EQ(https_total,
805 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
806
807 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700808 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700809 payload_state.UpdateFailed(error);
810
Jay Srinivasan53173b92013-05-17 17:13:01 -0700811 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800812 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700813 https_total += second_chunk;
814 payload_state.DownloadProgress(second_chunk);
815 EXPECT_EQ(first_chunk,
816 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
817 EXPECT_EQ(http_total,
818 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800819 EXPECT_EQ(
820 second_chunk,
821 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700822 EXPECT_EQ(https_total,
823 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
824
825 // Simulate error to go back to http.
826 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800827 uint64_t third_chunk = 32345678;
828 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700829 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700830 payload_state.DownloadProgress(third_chunk);
831
832 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
833 EXPECT_EQ(http_chunk,
834 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700835 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700836 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800837 EXPECT_EQ(
838 second_chunk,
839 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700840 EXPECT_EQ(https_total,
841 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
842
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700843 // Simulate error (will cause URL switch), set p2p is to be used and
844 // then do 42MB worth of progress
845 payload_state.UpdateFailed(error);
846 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800847 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700848 payload_state.DownloadProgress(p2p_total);
849
850 EXPECT_EQ(p2p_total,
851 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
852
Amin Hassani538bd592020-11-04 20:46:08 -0800853 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700854 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -0700855 1, _, kPayloadTypeFull, _, _, 314, _, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700856
857 payload_state.UpdateSucceeded();
858
859 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800860 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700861 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800862 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700863 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800864 EXPECT_EQ(
865 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800866 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700867 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700868 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700869}
870
Amin Hassani5ff76a22020-11-05 12:55:38 -0800871TEST_F(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700872 OmahaResponse response;
873 PayloadState payload_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700874
Amin Hassani538bd592020-11-04 20:46:08 -0800875 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700876 SetupPayloadStateWith2Urls(
877 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700878
879 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800880 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700881 payload_state.DownloadProgress(num_bytes);
882
883 // Check that this was done via HTTP.
884 EXPECT_EQ(num_bytes,
885 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
886 EXPECT_EQ(num_bytes,
887 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
888
889 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700890 int64_t total_bytes[kNumDownloadSources] = {};
891 total_bytes[kDownloadSourceHttpServer] = num_bytes;
892
Amin Hassani538bd592020-11-04 20:46:08 -0800893 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700894 ReportSuccessfulUpdateMetrics(
Tianjie Xud4777a12017-10-24 14:54:18 -0700895 _,
896 _,
897 _,
898 _,
899 test_utils::DownloadSourceMatcher(total_bytes),
900 _,
901 _,
902 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700903 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700904 _))
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700905 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700906
907 payload_state.UpdateSucceeded();
908}
909
Amin Hassani5ff76a22020-11-05 12:55:38 -0800910TEST_F(PayloadStateTest, RestartingUpdateResetsMetrics) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700911 OmahaResponse response;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700912 PayloadState payload_state;
913
Amin Hassani538bd592020-11-04 20:46:08 -0800914 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700915
916 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700917 SetupPayloadStateWith2Urls(
918 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700919
Alex Deymo80f70ff2016-02-10 16:08:11 -0800920 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700921 payload_state.DownloadProgress(num_bytes);
922 EXPECT_EQ(num_bytes,
923 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
924 EXPECT_EQ(num_bytes,
925 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800926 EXPECT_EQ(
927 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800928 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700929 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
930
931 payload_state.UpdateRestarted();
932 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800933 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700934 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
935 EXPECT_EQ(num_bytes,
936 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
937}
938
Amin Hassani5ff76a22020-11-05 12:55:38 -0800939TEST_F(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Chris Sosabe45bef2013-04-09 18:25:12 -0700940 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700941
Amin Hassani538bd592020-11-04 20:46:08 -0800942 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700943 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700944 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
945
Amin Hassani538bd592020-11-04 20:46:08 -0800946 EXPECT_TRUE(payload_state.Initialize());
Chris Sosabe45bef2013-04-09 18:25:12 -0700947
948 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800949 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700950
Amin Hassani538bd592020-11-04 20:46:08 -0800951 FakeSystemState::Get()->set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700952 payload_state.UpdateResumed();
953 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800954 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700955
Amin Hassani538bd592020-11-04 20:46:08 -0800956 FakeSystemState::Get()->set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700957 payload_state.UpdateResumed();
958 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800959 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700960
961 // Restart the update again to verify we set the num of reboots back to 0.
962 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800963 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700964}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700965
Amin Hassani5ff76a22020-11-05 12:55:38 -0800966TEST_F(PayloadStateTest, RollbackHappened) {
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200967 PayloadState payload_state;
968
969 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Amin Hassani538bd592020-11-04 20:46:08 -0800970 FakeSystemState::Get()->mock_powerwash_safe_prefs();
971 EXPECT_TRUE(payload_state.Initialize());
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200972
973 // Verify pre-conditions are good.
974 EXPECT_FALSE(payload_state.GetRollbackHappened());
975
976 // Set to true.
977 EXPECT_CALL(*mock_powerwash_safe_prefs,
978 SetBoolean(kPrefsRollbackHappened, true));
979 payload_state.SetRollbackHappened(true);
980 EXPECT_TRUE(payload_state.GetRollbackHappened());
981
982 // Set to false.
983 EXPECT_CALL(*mock_powerwash_safe_prefs, Delete(kPrefsRollbackHappened));
984 payload_state.SetRollbackHappened(false);
985 EXPECT_FALSE(payload_state.GetRollbackHappened());
986
987 // Let's verify we can reload it correctly.
988 EXPECT_CALL(*mock_powerwash_safe_prefs, GetBoolean(kPrefsRollbackHappened, _))
989 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
990 EXPECT_CALL(*mock_powerwash_safe_prefs,
991 SetBoolean(kPrefsRollbackHappened, true));
992 payload_state.LoadRollbackHappened();
993 EXPECT_TRUE(payload_state.GetRollbackHappened());
994}
995
Amin Hassani5ff76a22020-11-05 12:55:38 -0800996TEST_F(PayloadStateTest, RollbackVersion) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700997 PayloadState payload_state;
998
Alex Deymo8427b4a2014-11-05 14:00:32 -0800999 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Amin Hassani538bd592020-11-04 20:46:08 -08001000 FakeSystemState::Get()->mock_powerwash_safe_prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -07001001
Andrewcc6ab9f2020-06-25 07:41:40 -07001002 // Mock out the os version and make sure it's excluded correctly.
Chris Sosaaa18e162013-06-20 13:20:30 -07001003 string rollback_version = "2345.0.0";
Amin Hassani538bd592020-11-04 20:46:08 -08001004 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -07001005 params.Init(rollback_version, "", {});
Amin Hassani538bd592020-11-04 20:46:08 -08001006 FakeSystemState::Get()->set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001007
Amin Hassani538bd592020-11-04 20:46:08 -08001008 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001009
1010 // Verify pre-conditions are good.
1011 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1012
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001013 EXPECT_CALL(*mock_powerwash_safe_prefs,
1014 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosaaa18e162013-06-20 13:20:30 -07001015 payload_state.Rollback();
1016
1017 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001018
1019 // Change it up a little and verify we load it correctly.
1020 rollback_version = "2345.0.1";
1021 // Let's verify we can reload it correctly.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001022 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(kPrefsRollbackVersion, _))
1023 .WillOnce(DoAll(SetArgPointee<1>(rollback_version), Return(true)));
1024 EXPECT_CALL(*mock_powerwash_safe_prefs,
1025 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001026 payload_state.LoadRollbackVersion();
1027 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001028
David Zeuthen96197df2014-04-16 12:22:39 -07001029 // Check that we report only UpdateEngine.Rollback.* metrics in
1030 // UpdateSucceeded().
Amin Hassani538bd592020-11-04 20:46:08 -08001031 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001032 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1033 .Times(1);
1034
David Zeuthenafed4a12014-04-09 15:28:44 -07001035 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001036}
1037
Amin Hassani5ff76a22020-11-05 12:55:38 -08001038TEST_F(PayloadStateTest, DurationsAreCorrect) {
David Zeuthenf413fe52013-04-22 14:04:39 -07001039 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001040 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001041 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001042 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001043
1044 // Set the clock to a well-known time - 1 second on the wall-clock
1045 // and 2 seconds on the monotonic clock
Amin Hassani0468a762020-11-17 23:53:48 -08001046 auto* fake_clock = FakeSystemState::Get()->fake_clock();
1047 fake_clock->SetWallclockTime(Time::FromInternalValue(1000000));
1048 fake_clock->SetMonotonicTime(Time::FromInternalValue(2000000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001049
Amin Hassani538bd592020-11-04 20:46:08 -08001050 FakeSystemState::Get()->set_prefs(&fake_prefs);
1051 EXPECT_TRUE(payload_state.Initialize());
David Zeuthenf413fe52013-04-22 14:04:39 -07001052
1053 // Check that durations are correct for a successful update where
1054 // time has advanced 7 seconds on the wall clock and 4 seconds on
1055 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001056 SetupPayloadStateWith2Urls(
1057 "Hash8593", true, false, &payload_state, &response);
Amin Hassani0468a762020-11-17 23:53:48 -08001058 fake_clock->SetWallclockTime(Time::FromInternalValue(8000000));
1059 fake_clock->SetMonotonicTime(Time::FromInternalValue(6000000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001060 payload_state.UpdateSucceeded();
1061 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1062 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1063
1064 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001065 SetupPayloadStateWith2Urls(
1066 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001067 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1068 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1069
1070 // Advance time a bit (10 secs), simulate download progress and
1071 // check that durations are updated.
Amin Hassani0468a762020-11-17 23:53:48 -08001072 fake_clock->SetWallclockTime(Time::FromInternalValue(18000000));
1073 fake_clock->SetMonotonicTime(Time::FromInternalValue(16000000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001074 payload_state.DownloadProgress(10);
1075 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1076 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1077
1078 // Now simulate a reboot by resetting monotonic time (to 5000) and
1079 // creating a new PayloadState object and check that we load the
1080 // durations correctly (e.g. they are the same as before).
Amin Hassani0468a762020-11-17 23:53:48 -08001081 fake_clock->SetMonotonicTime(Time::FromInternalValue(5000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001082 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001083 EXPECT_TRUE(payload_state2.Initialize());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001084 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001085 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001086 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1087 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001088
1089 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1090 // and check that the durations are increased accordingly.
Amin Hassani0468a762020-11-17 23:53:48 -08001091 fake_clock->SetWallclockTime(Time::FromInternalValue(25000000));
1092 fake_clock->SetMonotonicTime(Time::FromInternalValue(6005000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001093 payload_state2.UpdateSucceeded();
1094 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001095 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1096 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001097}
1098
Amin Hassani5ff76a22020-11-05 12:55:38 -08001099TEST_F(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
David Zeuthene4c58bf2013-06-18 17:26:50 -07001100 OmahaResponse response;
1101 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001102 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001103
1104 // Set the clock to a well-known time (t = 30 seconds).
Amin Hassani0468a762020-11-17 23:53:48 -08001105 auto* fake_clock = FakeSystemState::Get()->fake_clock();
1106 fake_clock->SetMonotonicTime(
Tianjie Xu90aaa102017-10-10 17:39:03 -07001107 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001108
Amin Hassani538bd592020-11-04 20:46:08 -08001109 FakeSystemState::Get()->set_prefs(&fake_prefs);
1110 EXPECT_TRUE(payload_state.Initialize());
David Zeuthene4c58bf2013-06-18 17:26:50 -07001111
1112 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001113 SetupPayloadStateWith2Urls(
1114 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001115 payload_state.UpdateSucceeded();
1116
1117 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001118 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001119
1120 // Now simulate a reboot and set the wallclock time to a later point
1121 // (t = 500 seconds). We do this by using a new PayloadState object
1122 // and checking that it emits the right UMA metric with the right
1123 // value.
Amin Hassani0468a762020-11-17 23:53:48 -08001124 fake_clock->SetMonotonicTime(
Tianjie Xu90aaa102017-10-10 17:39:03 -07001125 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001126 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001127 EXPECT_TRUE(payload_state2.Initialize());
David Zeuthene4c58bf2013-06-18 17:26:50 -07001128
1129 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Amin Hassani538bd592020-11-04 20:46:08 -08001130 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001131 ReportTimeToReboot(7));
Amin Hassani538bd592020-11-04 20:46:08 -08001132 FakeSystemState::Get()->set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001133
1134 payload_state2.UpdateEngineStarted();
1135
1136 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001137 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001138}
1139
Amin Hassani5ff76a22020-11-05 12:55:38 -08001140TEST_F(PayloadStateTest, RestartAfterCrash) {
Alex Deymo569c4242013-07-24 12:01:01 -07001141 PayloadState payload_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001142 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Amin Hassani538bd592020-11-04 20:46:08 -08001143 FakeSystemState::Get()->set_metrics_reporter(&mock_metrics_reporter);
1144 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001145
Amin Hassani538bd592020-11-04 20:46:08 -08001146 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo569c4242013-07-24 12:01:01 -07001147
David Zeuthen4e1d1492014-04-25 13:12:27 -07001148 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001149 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1150 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1151 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1152 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1153 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1154 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1155 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001156 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001157
Alex Deymo569c4242013-07-24 12:01:01 -07001158 // Simulate an update_engine restart without a reboot.
Amin Hassani538bd592020-11-04 20:46:08 -08001159 FakeSystemState::Get()->set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001160
1161 payload_state.UpdateEngineStarted();
1162}
1163
Amin Hassani5ff76a22020-11-05 12:55:38 -08001164TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001165 PayloadState payload_state;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001166
1167 // If there's no marker at startup, ensure we don't report a metric.
Amin Hassani538bd592020-11-04 20:46:08 -08001168 EXPECT_TRUE(payload_state.Initialize());
1169 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001170 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1171 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001172 payload_state.UpdateEngineStarted();
1173}
1174
Amin Hassani5ff76a22020-11-05 12:55:38 -08001175TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001176 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001177 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001178
1179 // If we have a marker at startup, ensure it's reported and the
1180 // marker is then cleared.
Amin Hassani538bd592020-11-04 20:46:08 -08001181 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001182 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001183
Amin Hassani538bd592020-11-04 20:46:08 -08001184 EXPECT_TRUE(payload_state.Initialize());
David Zeuthen4e1d1492014-04-25 13:12:27 -07001185
Amin Hassani538bd592020-11-04 20:46:08 -08001186 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001187 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1188 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001189 payload_state.UpdateEngineStarted();
1190
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001191 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001192}
1193
Amin Hassani5ff76a22020-11-05 12:55:38 -08001194TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001195 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001196 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001197
1198 // Make sure the marker is written and cleared during an attempt and
1199 // also that we DO NOT emit the metric (since the attempt didn't end
1200 // abnormally).
Amin Hassani538bd592020-11-04 20:46:08 -08001201 FakeSystemState::Get()->set_prefs(&fake_prefs);
1202 EXPECT_TRUE(payload_state.Initialize());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001203 OmahaResponse response;
1204 response.packages.resize(1);
1205 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001206
Amin Hassani538bd592020-11-04 20:46:08 -08001207 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001208 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1209 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001210
1211 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001212 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001213
1214 payload_state.UpdateRestarted();
1215
1216 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001217 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001218
1219 payload_state.UpdateSucceeded();
1220
1221 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001222 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001223}
1224
Amin Hassani5ff76a22020-11-05 12:55:38 -08001225TEST_F(PayloadStateTest, CandidateUrlsComputedCorrectly) {
Jay Srinivasan53173b92013-05-17 17:13:01 -07001226 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001227 PayloadState payload_state;
1228
Jay Srinivasan53173b92013-05-17 17:13:01 -07001229 policy::MockDevicePolicy disable_http_policy;
Amin Hassani538bd592020-11-04 20:46:08 -08001230 FakeSystemState::Get()->set_device_policy(&disable_http_policy);
1231 EXPECT_TRUE(payload_state.Initialize());
Chris Sosaf7d80042013-08-22 16:45:17 -07001232
1233 // Test with no device policy. Should default to allowing http.
1234 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1235 .WillRepeatedly(Return(false));
1236
1237 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001238 SetupPayloadStateWith2Urls(
1239 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001240
1241 // Check that we use the HTTP URL since there is no value set for allowing
1242 // http.
1243 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1244
1245 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001246 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001247 .WillRepeatedly(DoAll(SetArgPointee<0>(false), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001248
Chris Sosaf7d80042013-08-22 16:45:17 -07001249 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001250 SetupPayloadStateWith2Urls(
1251 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001252
1253 // Check that we skip the HTTP URL and use only the HTTPS url.
1254 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1255
1256 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001257 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001258 payload_state.UpdateFailed(error);
1259
1260 // Check that we still skip the HTTP URL and use only the HTTPS url.
1261 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001262 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001263
1264 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001265 SetupPayloadStateWith2Urls(
1266 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001267
1268 // Check that we still skip the HTTP URL and use only the HTTPS url.
1269 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1270
1271 // Now, pretend that the HTTP policy is turned on. We want to make sure
1272 // the new policy is honored.
1273 policy::MockDevicePolicy enable_http_policy;
Amin Hassani538bd592020-11-04 20:46:08 -08001274 FakeSystemState::Get()->set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001275 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001276 .WillRepeatedly(DoAll(SetArgPointee<0>(true), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001277
1278 // Now, set the same response using the same hash
1279 // so that we can test that the state is reset not because of the
1280 // hash but because of the policy change which results in candidate url
1281 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001282 SetupPayloadStateWith2Urls(
1283 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001284
1285 // Check that we use the HTTP URL now and the failure count is reset.
1286 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001287 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001288
1289 // Fake a failure and see if we're moving over to the HTTPS url and update
1290 // the URL switch count properly.
1291 payload_state.UpdateFailed(error);
1292 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001293 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1294 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001295}
1296
Amin Hassani5ff76a22020-11-05 12:55:38 -08001297TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001298 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001299 PayloadState payload_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001300
Amin Hassani538bd592020-11-04 20:46:08 -08001301 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001302 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001303
1304 // Simulate a successful download and update.
1305 payload_state.DownloadComplete();
Amin Hassani538bd592020-11-04 20:46:08 -08001306 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Sen Jiang8712e962018-05-08 12:12:28 -07001307 ReportSuccessfulUpdateMetrics(
1308 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001309 payload_state.UpdateSucceeded();
1310
1311 // Mock the request to a request where the delta was disabled but Omaha sends
1312 // a delta anyway and test again.
Amin Hassani538bd592020-11-04 20:46:08 -08001313 OmahaRequestParams params;
Alex Deymo1c656c42013-06-28 11:02:14 -07001314 params.set_delta_okay(false);
Amin Hassani538bd592020-11-04 20:46:08 -08001315 FakeSystemState::Get()->set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001316
Amin Hassani538bd592020-11-04 20:46:08 -08001317 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001318 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001319
1320 payload_state.DownloadComplete();
1321
Amin Hassani538bd592020-11-04 20:46:08 -08001322 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Sen Jiang8712e962018-05-08 12:12:28 -07001323 ReportSuccessfulUpdateMetrics(
1324 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001325 payload_state.UpdateSucceeded();
1326}
1327
Amin Hassani5ff76a22020-11-05 12:55:38 -08001328TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001329 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001330 PayloadState payload_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001331
Alex Deymo1c656c42013-06-28 11:02:14 -07001332 // Mock the request to a request where the delta was disabled.
Amin Hassani538bd592020-11-04 20:46:08 -08001333 OmahaRequestParams params;
Alex Deymo1c656c42013-06-28 11:02:14 -07001334 params.set_delta_okay(false);
Amin Hassani538bd592020-11-04 20:46:08 -08001335 FakeSystemState::Get()->set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001336
Amin Hassani538bd592020-11-04 20:46:08 -08001337 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001338 SetupPayloadStateWith2Urls(
1339 "Hash6437", true, false, &payload_state, &response);
1340
Alex Deymo1c656c42013-06-28 11:02:14 -07001341 // Simulate a successful download and update.
1342 payload_state.DownloadComplete();
1343
Amin Hassani538bd592020-11-04 20:46:08 -08001344 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001345 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -07001346 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001347 payload_state.UpdateSucceeded();
1348}
1349
Amin Hassani5ff76a22020-11-05 12:55:38 -08001350TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001351 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001352 PayloadState payload_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001353
Amin Hassani538bd592020-11-04 20:46:08 -08001354 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001355 SetupPayloadStateWith2Urls(
1356 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001357
Alex Deymo820cc702013-06-28 15:43:46 -07001358 // Mock the request to a request where the delta is enabled, although the
1359 // result is full.
Amin Hassani538bd592020-11-04 20:46:08 -08001360 OmahaRequestParams params;
Alex Deymo1c656c42013-06-28 11:02:14 -07001361 params.set_delta_okay(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001362 FakeSystemState::Get()->set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001363
1364 // Simulate a successful download and update.
1365 payload_state.DownloadComplete();
1366
Amin Hassani538bd592020-11-04 20:46:08 -08001367 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Sen Jiang8712e962018-05-08 12:12:28 -07001368 ReportSuccessfulUpdateMetrics(
1369 _, _, kPayloadTypeFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001370 payload_state.UpdateSucceeded();
1371}
1372
Amin Hassani5ff76a22020-11-05 12:55:38 -08001373TEST_F(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Alex Deymo42432912013-07-12 20:21:15 -07001374 OmahaResponse response;
1375 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001376 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001377 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001378
Amin Hassani538bd592020-11-04 20:46:08 -08001379 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001380 SetupPayloadStateWith2Urls(
1381 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001382
1383 // Simulate a successful download and update.
1384 payload_state.DownloadComplete();
1385 payload_state.UpdateSucceeded();
1386 payload_state.ExpectRebootInNewVersion("Version:12345678");
1387
1388 // Reboot into the same environment to get an UMA metric with a value of 1.
Amin Hassani538bd592020-11-04 20:46:08 -08001389 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001390 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001391 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001392 Mock::VerifyAndClearExpectations(
1393 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001394
1395 // Simulate a second update and reboot into the same environment, this should
1396 // send a value of 2.
1397 payload_state.ExpectRebootInNewVersion("Version:12345678");
1398
Amin Hassani538bd592020-11-04 20:46:08 -08001399 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001400 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001401 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001402 Mock::VerifyAndClearExpectations(
1403 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001404
1405 // Simulate a third failed reboot to new version, but this time for a
1406 // different payload. This should send a value of 1 this time.
1407 payload_state.ExpectRebootInNewVersion("Version:3141592");
Amin Hassani538bd592020-11-04 20:46:08 -08001408 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001409 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001410 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001411 Mock::VerifyAndClearExpectations(
1412 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001413}
1414
Amin Hassani5ff76a22020-11-05 12:55:38 -08001415TEST_F(PayloadStateTest, RebootAfterUpdateSucceed) {
Alex Deymo42432912013-07-12 20:21:15 -07001416 OmahaResponse response;
1417 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001418 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001419 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001420
Amin Hassani538bd592020-11-04 20:46:08 -08001421 FakeBootControl* fake_boot_control =
1422 FakeSystemState::Get()->fake_boot_control();
Alex Deymo763e7db2015-08-27 21:08:08 -07001423 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001424
Amin Hassani538bd592020-11-04 20:46:08 -08001425 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001426 SetupPayloadStateWith2Urls(
1427 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001428
1429 // Simulate a successful download and update.
1430 payload_state.DownloadComplete();
1431 payload_state.UpdateSucceeded();
1432 payload_state.ExpectRebootInNewVersion("Version:12345678");
1433
1434 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001435 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001436
Amin Hassani538bd592020-11-04 20:46:08 -08001437 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001438 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001439 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001440 payload_state.ReportFailedBootIfNeeded();
1441
Alex Deymo763e7db2015-08-27 21:08:08 -07001442 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001443 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001444 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001445 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001446}
1447
Amin Hassani5ff76a22020-11-05 12:55:38 -08001448TEST_F(PayloadStateTest, RebootAfterCanceledUpdate) {
Alex Deymo42432912013-07-12 20:21:15 -07001449 OmahaResponse response;
1450 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001451 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001452
Amin Hassani538bd592020-11-04 20:46:08 -08001453 FakeSystemState::Get()->set_prefs(&fake_prefs);
1454 EXPECT_TRUE(payload_state.Initialize());
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
Amin Hassani538bd592020-11-04 20:46:08 -08001463 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001464 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001465 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001466
1467 // Cancel the applied update.
1468 payload_state.ResetUpdateStatus();
1469
1470 // Simulate a reboot.
1471 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001472}
1473
Amin Hassani5ff76a22020-11-05 12:55:38 -08001474TEST_F(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Alex Deymo42432912013-07-12 20:21:15 -07001475 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001476 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001477
Amin Hassani538bd592020-11-04 20:46:08 -08001478 FakeSystemState::Get()->set_prefs(&fake_prefs);
1479 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo42432912013-07-12 20:21:15 -07001480
Amin Hassani538bd592020-11-04 20:46:08 -08001481 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001482 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001483 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001484
1485 // Simulate a reboot in this environment.
1486 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001487}
1488
Amin Hassani5ff76a22020-11-05 12:55:38 -08001489TEST_F(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
David Zeuthendcba8092013-08-06 12:16:35 -07001490 OmahaResponse response;
1491 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001492 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001493 FakeSystemState::Get()->set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001494
Amin Hassani538bd592020-11-04 20:46:08 -08001495 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001496 SetupPayloadStateWith2Urls(
1497 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001498
1499 // Should allow exactly kMaxP2PAttempts...
1500 for (int n = 0; n < kMaxP2PAttempts; n++) {
1501 payload_state.P2PNewAttempt();
1502 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1503 }
1504 // ... but not more than that.
1505 payload_state.P2PNewAttempt();
1506 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001507}
1508
Amin Hassani5ff76a22020-11-05 12:55:38 -08001509TEST_F(PayloadStateTest, DisallowP2PAfterDeadline) {
David Zeuthendcba8092013-08-06 12:16:35 -07001510 OmahaResponse response;
1511 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001512 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001513
Amin Hassani538bd592020-11-04 20:46:08 -08001514 FakeSystemState::Get()->set_prefs(&fake_prefs);
1515 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001516 SetupPayloadStateWith2Urls(
1517 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001518
1519 // Set the clock to 1 second.
1520 Time epoch = Time::FromInternalValue(1000000);
Amin Hassani0468a762020-11-17 23:53:48 -08001521 auto* fake_clock = FakeSystemState::Get()->fake_clock();
1522 fake_clock->SetWallclockTime(epoch);
David Zeuthendcba8092013-08-06 12:16:35 -07001523
1524 // Do an attempt - this will set the timestamp.
1525 payload_state.P2PNewAttempt();
1526
1527 // Check that the timestamp equals what we just set.
1528 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1529
1530 // Time hasn't advanced - this should work.
1531 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1532
1533 // Set clock to half the deadline - this should work.
Amin Hassani0468a762020-11-17 23:53:48 -08001534 fake_clock->SetWallclockTime(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001535 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
David Zeuthendcba8092013-08-06 12:16:35 -07001536 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1537
1538 // Check that the first attempt timestamp hasn't changed just
1539 // because the wall-clock time changed.
1540 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1541
1542 // Set clock to _just_ before the deadline - this should work.
Amin Hassani0468a762020-11-17 23:53:48 -08001543 fake_clock->SetWallclockTime(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001544 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001545 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1546
1547 // Set clock to _just_ after the deadline - this should not work.
Amin Hassani0468a762020-11-17 23:53:48 -08001548 fake_clock->SetWallclockTime(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001549 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001550 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001551}
1552
Amin Hassani5ff76a22020-11-05 12:55:38 -08001553TEST_F(PayloadStateTest, P2PStateVarsInitialValue) {
David Zeuthendcba8092013-08-06 12:16:35 -07001554 OmahaResponse response;
1555 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001556 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001557
Amin Hassani538bd592020-11-04 20:46:08 -08001558 FakeSystemState::Get()->set_prefs(&fake_prefs);
1559 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001560 SetupPayloadStateWith2Urls(
1561 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001562
1563 Time null_time = Time();
1564 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1565 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001566}
1567
Amin Hassani5ff76a22020-11-05 12:55:38 -08001568TEST_F(PayloadStateTest, P2PStateVarsArePersisted) {
David Zeuthendcba8092013-08-06 12:16:35 -07001569 OmahaResponse response;
1570 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001571 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001572 FakeSystemState::Get()->set_prefs(&fake_prefs);
1573 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001574 SetupPayloadStateWith2Urls(
1575 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001576
1577 // Set the clock to something known.
1578 Time time = Time::FromInternalValue(12345);
Amin Hassani0468a762020-11-17 23:53:48 -08001579 FakeSystemState::Get()->fake_clock()->SetWallclockTime(time);
David Zeuthendcba8092013-08-06 12:16:35 -07001580
1581 // New p2p attempt - as a side-effect this will update the p2p state vars.
1582 payload_state.P2PNewAttempt();
1583 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1584 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1585
1586 // Now create a new PayloadState and check that it loads the state
1587 // vars correctly.
1588 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001589 EXPECT_TRUE(payload_state2.Initialize());
David Zeuthendcba8092013-08-06 12:16:35 -07001590 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1591 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001592}
1593
Amin Hassani5ff76a22020-11-05 12:55:38 -08001594TEST_F(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
David Zeuthendcba8092013-08-06 12:16:35 -07001595 OmahaResponse response;
1596 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001597 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001598 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001599
Amin Hassani538bd592020-11-04 20:46:08 -08001600 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001601 SetupPayloadStateWith2Urls(
1602 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001603
1604 // Set the clock to something known.
1605 Time time = Time::FromInternalValue(12345);
Amin Hassani0468a762020-11-17 23:53:48 -08001606 FakeSystemState::Get()->fake_clock()->SetWallclockTime(time);
David Zeuthendcba8092013-08-06 12:16:35 -07001607
1608 // New p2p attempt - as a side-effect this will update the p2p state vars.
1609 payload_state.P2PNewAttempt();
1610 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1611 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1612
1613 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001614 SetupPayloadStateWith2Urls(
1615 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001616
1617 // ... and check that it clears the P2P state vars.
1618 Time null_time = Time();
1619 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1620 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001621}
1622
Amin Hassani5ff76a22020-11-05 12:55:38 -08001623TEST_F(PayloadStateTest, NextPayloadResetsUrlIndex) {
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001624 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001625 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001626 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001627 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001628 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001629
1630 OmahaResponse response;
1631 response.packages.push_back(
1632 {.payload_urls = {"http://test1a", "http://test2a"},
1633 .size = 123456789,
1634 .metadata_size = 58123,
1635 .metadata_signature = "msign",
1636 .hash = "hash"});
1637 response.packages.push_back({.payload_urls = {"http://test1b"},
1638 .size = 123456789,
1639 .metadata_size = 58123,
1640 .metadata_signature = "msign",
1641 .hash = "hash"});
1642 payload_state.SetResponse(response);
1643
1644 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1a");
1645 payload_state.IncrementUrlIndex();
1646 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test2a");
1647
1648 EXPECT_TRUE(payload_state.NextPayload());
1649 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1b");
1650}
1651
Amin Hassani5ff76a22020-11-05 12:55:38 -08001652TEST_F(PayloadStateTest, ExcludeNoopForNonExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001653 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001654 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001655 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001656 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001657 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001658
1659 OmahaResponse response;
1660 response.packages.push_back(
1661 {.payload_urls = {"http://test1a", "http://test2a"},
1662 .size = 123456789,
1663 .metadata_size = 58123,
1664 .metadata_signature = "msign",
1665 .hash = "hash",
1666 .can_exclude = false});
1667 payload_state.SetResponse(response);
1668
1669 EXPECT_CALL(mock_excluder, Exclude(_)).Times(0);
1670 payload_state.ExcludeCurrentPayload();
1671}
1672
Amin Hassani5ff76a22020-11-05 12:55:38 -08001673TEST_F(PayloadStateTest, ExcludeOnlyCanExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001674 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001675 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001676 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001677 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001678 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001679
1680 OmahaResponse response;
1681 response.packages.push_back(
1682 {.payload_urls = {"http://test1a", "http://test2a"},
1683 .size = 123456789,
1684 .metadata_size = 58123,
1685 .metadata_signature = "msign",
1686 .hash = "hash",
1687 .can_exclude = true});
1688 payload_state.SetResponse(response);
1689
1690 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1691 .WillOnce(Return(true));
1692 payload_state.ExcludeCurrentPayload();
1693}
1694
Amin Hassani5ff76a22020-11-05 12:55:38 -08001695TEST_F(PayloadStateTest, IncrementFailureExclusionTest) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001696 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001697 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001698 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001699 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001700 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001701
1702 OmahaResponse response;
1703 // Critical package.
1704 response.packages.push_back(
1705 {.payload_urls = {"http://crit-test1a", "http://crit-test2a"},
1706 .size = 123456789,
1707 .metadata_size = 58123,
1708 .metadata_signature = "msign",
1709 .hash = "hash",
1710 .can_exclude = false});
1711 // Non-critical package.
1712 response.packages.push_back(
1713 {.payload_urls = {"http://test1a", "http://test2a"},
1714 .size = 123456789,
1715 .metadata_size = 58123,
1716 .metadata_signature = "msign",
1717 .hash = "hash",
1718 .can_exclude = true});
1719 response.max_failure_count_per_url = 2;
1720 payload_state.SetResponse(response);
1721
1722 // Critical package won't be excluded.
1723 // Increment twice as failure count allowed per URL is set to 2.
1724 payload_state.IncrementFailureCount();
1725 payload_state.IncrementFailureCount();
1726
1727 EXPECT_TRUE(payload_state.NextPayload());
1728
1729 // First increment failure should not exclude.
1730 payload_state.IncrementFailureCount();
1731
1732 // Second increment failure should exclude.
1733 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1734 .WillOnce(Return(true));
1735 payload_state.IncrementFailureCount();
1736}
1737
Amin Hassani5ff76a22020-11-05 12:55:38 -08001738TEST_F(PayloadStateTest, HaltExclusionPostPayloadExhaustion) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001739 PayloadState payload_state;
Jae Hoon Kim76583912020-06-23 10:24:03 -07001740 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001741 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim76583912020-06-23 10:24:03 -07001742 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001743 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim76583912020-06-23 10:24:03 -07001744
1745 OmahaResponse response;
1746 // Non-critical package.
1747 response.packages.push_back(
1748 {.payload_urls = {"http://test1a", "http://test2a"},
1749 .size = 123456789,
1750 .metadata_size = 58123,
1751 .metadata_signature = "msign",
1752 .hash = "hash",
1753 .can_exclude = true});
1754 payload_state.SetResponse(response);
1755
1756 // Exclusion should be called when excluded.
1757 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1758 .WillOnce(Return(true));
1759 payload_state.ExcludeCurrentPayload();
1760
1761 // No more paylods to go through.
1762 EXPECT_FALSE(payload_state.NextPayload());
1763
1764 // Exclusion should not be called as all |Payload|s are exhausted.
1765 payload_state.ExcludeCurrentPayload();
1766}
1767
Amin Hassani5ff76a22020-11-05 12:55:38 -08001768TEST_F(PayloadStateTest, NonInfinitePayloadIndexIncrement) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001769 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001770 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim76583912020-06-23 10:24:03 -07001771
1772 payload_state.SetResponse({});
1773
1774 EXPECT_FALSE(payload_state.NextPayload());
1775 int payload_index = payload_state.payload_index_;
1776
1777 EXPECT_FALSE(payload_state.NextPayload());
1778 EXPECT_EQ(payload_index, payload_state.payload_index_);
1779}
1780
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001781} // namespace chromeos_update_engine