blob: edcb9d6d2b854b9222db1f41a4af48184e03f0a4 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/payload_state.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080018
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080020#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070026#include "update_engine/common/excluder_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/fake_clock.h"
28#include "update_engine/common/fake_hardware.h"
29#include "update_engine/common/fake_prefs.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070030#include "update_engine/common/metrics_reporter_interface.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070031#include "update_engine/common/mock_excluder.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/mock_prefs.h"
33#include "update_engine/common/prefs.h"
34#include "update_engine/common/test_utils.h"
35#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070036#include "update_engine/cros/fake_system_state.h"
37#include "update_engine/cros/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038
Jay Srinivasan08262882012-12-28 19:29:43 -080039using base::Time;
40using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041using std::string;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080042using testing::_;
Alex Deymo42432912013-07-12 20:21:15 -070043using testing::AnyNumber;
44using testing::AtLeast;
45using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080047using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070048using testing::SetArgPointee;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070049using testing::StrictMock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050
51namespace chromeos_update_engine {
52
Jay Srinivasan19409b72013-04-12 19:23:36 -070053const char* kCurrentBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080054 "current-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070055const char* kTotalBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080056 "total-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070057const char* kCurrentBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080058 "current-bytes-downloaded-from-HttpServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070059const char* kTotalBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080060 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070061const char* kCurrentBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080062 "current-bytes-downloaded-from-HttpPeer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070063const char* kTotalBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070065
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080066static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070067 bool http_enabled,
Sen Jiangcdd52062017-05-18 15:33:10 -070068 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080069 PayloadState* payload_state,
70 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080071 response->packages.clear();
72 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
73 .size = 523456789,
74 .metadata_size = 558123,
75 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070076 .hash = hash,
77 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080078 response->max_failure_count_per_url = 3;
79 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080080 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070081
82 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080083 " NumURLs = 1\n"
84 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070085
86 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080087 " NumURLs = 2\n"
88 " Candidate Url0 = http://test\n"
89 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070090
Sen Jiang0affc2c2017-02-10 15:55:05 -080091 string expected_response_sign = base::StringPrintf(
92 "Payload 0:\n"
93 " Size = 523456789\n"
94 " Sha256 Hash = %s\n"
95 " Metadata Size = 558123\n"
96 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070097 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080098 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080099 "Max Failure Count Per Url = %d\n"
100 "Disable Payload Backoff = %d\n",
101 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -0700102 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800103 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800104 response->max_failure_count_per_url,
105 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800106 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800107}
108
Amin Hassani538bd592020-11-04 20:46:08 -0800109class PayloadStateTest : public ::testing::Test {
110 public:
111 void SetUp() { FakeSystemState::CreateInstance(); }
112};
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800113
Amin Hassani5ff76a22020-11-05 12:55:38 -0800114TEST_F(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800115 OmahaResponse response;
Amin Hassani538bd592020-11-04 20:46:08 -0800116 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700117 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700118 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800119 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700120 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800121 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700122 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
123 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
124 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800125 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700126 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800127 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700128 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800129 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700130 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700132 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800133 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700134 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800135 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700136 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800137 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800138 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800140 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800141 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800142 "Max Failure Count Per Url = 0\n"
143 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800144 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700145 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800146 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
147 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700148 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800149}
150
Amin Hassani5ff76a22020-11-05 12:55:38 -0800151TEST_F(PayloadStateTest, SetResponseWorksWithSingleUrl) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800152 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800153 response.packages.push_back({.payload_urls = {"https://single.url.test"},
154 .size = 123456789,
155 .metadata_size = 58123,
156 .metadata_signature = "msign",
157 .hash = "hash"});
Amin Hassani538bd592020-11-04 20:46:08 -0800158 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700159 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700160 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700161 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800162 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
163 .Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
165 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
166 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
167 .Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
169 .Times(AtLeast(1));
170 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
171 .Times(AtLeast(1));
172 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
173 .Times(AtLeast(1));
174 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
175 .Times(AtLeast(1));
176 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
177 .Times(AtLeast(1));
178 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800180 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800181 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800182 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800183 string expected_response_sign =
184 "Payload 0:\n"
185 " Size = 123456789\n"
186 " Sha256 Hash = hash\n"
187 " Metadata Size = 58123\n"
188 " Metadata Signature = msign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700189 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800190 " NumURLs = 1\n"
191 " Candidate Url0 = https://single.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800192 "Max Failure Count Per Url = 0\n"
193 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800194 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700195 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800196 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
197 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700198 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800199}
200
Amin Hassani5ff76a22020-11-05 12:55:38 -0800201TEST_F(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800202 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800203 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
204 "https://multiple.url.test"},
205 .size = 523456789,
206 .metadata_size = 558123,
207 .metadata_signature = "metasign",
208 .hash = "rhash"});
Amin Hassani538bd592020-11-04 20:46:08 -0800209 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700210 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700211 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700212 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800213 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
214 .Times(AtLeast(1));
215 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
216 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
222 .Times(AtLeast(1));
223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
224 .Times(AtLeast(1));
225 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700226
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800227 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800228 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800230 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800231 string expected_response_sign =
232 "Payload 0:\n"
233 " Size = 523456789\n"
234 " Sha256 Hash = rhash\n"
235 " Metadata Size = 558123\n"
236 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700237 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800238 " NumURLs = 2\n"
239 " Candidate Url0 = http://multiple.url.test\n"
240 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800241 "Max Failure Count Per Url = 0\n"
242 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800243 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700244 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800245 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
246 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700247 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800248}
249
Amin Hassani5ff76a22020-11-05 12:55:38 -0800250TEST_F(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800251 OmahaResponse response;
Amin Hassani538bd592020-11-04 20:46:08 -0800252 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800254
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700255 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700257 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800258 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700259 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800260 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700261 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800262 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700263 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800264 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700265 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700266
Chris Sosabe45bef2013-04-09 18:25:12 -0700267 // Reboots will be set
268 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
269
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800270 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700271 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
272 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800273
274 // Failure count should be called each times url index is set, so that's
275 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700276 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800277 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800278
Amin Hassani538bd592020-11-04 20:46:08 -0800279 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800280
281 // This does a SetResponse which causes all the states to be set to 0 for
282 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700283 SetupPayloadStateWith2Urls(
284 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700285 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800286
287 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700288 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700290 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800291
292 // Verify that on the next error, the URL index wraps around to 0.
293 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700294 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295
296 // Verify that on the next error, it again advances to 1.
297 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700298 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700299
300 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800301 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800302}
303
Amin Hassani5ff76a22020-11-05 12:55:38 -0800304TEST_F(PayloadStateTest, NewResponseResetsPayloadState) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800305 OmahaResponse response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800307
Amin Hassani538bd592020-11-04 20:46:08 -0800308 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309
310 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700311 SetupPayloadStateWith2Urls(
312 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700313 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800314
315 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700316 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800317 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700318 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800319 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800320
321 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700322 SetupPayloadStateWith2Urls(
323 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700324 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325
Alex Deymob33b0f02013-08-08 21:10:02 -0700326 // Fake an error again.
327 payload_state.UpdateFailed(error);
328 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800329 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700330
Alex Deymob33b0f02013-08-08 21:10:02 -0700331 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700332 SetupPayloadStateWith2Urls(
333 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700334 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
335
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800336 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700337 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800338 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
339 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
340 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700341 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800342 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700343 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800344 EXPECT_EQ(
345 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
346 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700347 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800348}
349
Amin Hassani5ff76a22020-11-05 12:55:38 -0800350TEST_F(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800351 OmahaResponse response;
352 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700353 int progress_bytes = 100;
Amin Hassani538bd592020-11-04 20:46:08 -0800354 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800355
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700356 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700357 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800358 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700359 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800360 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700361 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800362 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800363
Alex Deymo820cc702013-06-28 15:43:46 -0700364 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800365 .Times(AtLeast(2));
Alex Deymo820cc702013-06-28 15:43:46 -0700366 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800367 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700368 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800369 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700370
Jay Srinivasan19409b72013-04-12 19:23:36 -0700371 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800372
Jay Srinivasan19409b72013-04-12 19:23:36 -0700373 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
374 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800375
Jay Srinivasan19409b72013-04-12 19:23:36 -0700376 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800377 .Times(AtLeast(7));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700378 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800379 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700380 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800381 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800382
Jay Srinivasan19409b72013-04-12 19:23:36 -0700383 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800384 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700385 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800386 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700387
Jay Srinivasan19409b72013-04-12 19:23:36 -0700388 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700389 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800390 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
391 .Times(AtLeast(1));
392 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
393 .Times(AtLeast(1));
394 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
395 .Times(AtLeast(1));
396 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
397 .Times(AtLeast(1));
398 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700399
Amin Hassani538bd592020-11-04 20:46:08 -0800400 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800401
Sen Jiangcdd52062017-05-18 15:33:10 -0700402 SetupPayloadStateWith2Urls(
403 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700404 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800405
406 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700407 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800408 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700409 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700410 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800411 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
412 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800413
414 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700415 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800416 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700417 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700418 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800419 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
420 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800421
422 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700423 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800424 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700425 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700426 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800427 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
428 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800429
430 // This should advance the URL index as we've reached the
431 // max failure count and reset the failure count for the new URL index.
432 // This should also wrap around the URL index and thus cause the payload
433 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700434 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800435 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700436 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700437 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800438 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
439 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800440 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800441
442 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700443 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800444 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700445 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700446 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800447 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
448 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800449 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800450
451 // This should advance the URL index and payload attempt number due to
452 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700453 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800454 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700455 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700456 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800457 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
458 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800459 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800460
461 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700462 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700463 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800464 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700465 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700466 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800467 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
468 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800469 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800470
471 // And that failure count should be reset when we download some bytes
472 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700473 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800474 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700475 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700476 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800477 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
478 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800479 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800480
481 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700482 SetupPayloadStateWith2Urls(
483 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700484 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800485
486 // Make sure the url index was reset to 0 because of the new response.
487 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700488 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700489 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800490 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
491 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800492 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800493}
494
Amin Hassani5ff76a22020-11-05 12:55:38 -0800495TEST_F(PayloadStateTest,
496 PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800497 OmahaResponse response;
498 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800499 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800500
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700501 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700502 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800503 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700504 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800505 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800506
Alex Deymo820cc702013-06-28 15:43:46 -0700507 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800508 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700509 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800510 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700511
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800512 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800513
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800514 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700515 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800516 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800517
Amin Hassani538bd592020-11-04 20:46:08 -0800518 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800519
Sen Jiangcdd52062017-05-18 15:33:10 -0700520 SetupPayloadStateWith2Urls(
521 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800522
523 // This should just advance the payload attempt number;
524 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700525 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800526 payload_state.DownloadComplete();
527 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700528 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
529 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800530 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
531 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700532}
533
Amin Hassani5ff76a22020-11-05 12:55:38 -0800534TEST_F(PayloadStateTest,
535 PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
Alex Deymo820cc702013-06-28 15:43:46 -0700536 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700537 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800538 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700539
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700540 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700541 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800542 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700543 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800544 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700545
546 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
547 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800548 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700549
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800550 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(1);
Alex Deymo820cc702013-06-28 15:43:46 -0700551
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800552 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700553 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800554 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700555
Amin Hassani538bd592020-11-04 20:46:08 -0800556 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo820cc702013-06-28 15:43:46 -0700557
Sen Jiangcdd52062017-05-18 15:33:10 -0700558 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700559
560 // This should just advance the payload attempt number;
561 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
562 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
563 payload_state.DownloadComplete();
564 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
565 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700566 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800567 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
568 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800569}
570
Amin Hassani5ff76a22020-11-05 12:55:38 -0800571TEST_F(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800572 OmahaResponse response;
573 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800574
Amin Hassani538bd592020-11-04 20:46:08 -0800575 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700576 SetupPayloadStateWith2Urls(
577 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800578
579 // Generate enough events to advance URL index, failure count and
580 // payload attempt number all to 1.
581 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700582 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
583 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800584 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700585 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700586 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800587 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
588 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800589
590 // Now, simulate a corrupted url index on persisted store which gets
591 // loaded when update_engine restarts. Using a different prefs object
592 // so as to not bother accounting for the uninteresting calls above.
Amin Hassani538bd592020-11-04 20:46:08 -0800593 NiceMock<MockPrefs>* prefs2 = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700594 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700595 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700596 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800597 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700598 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800599 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700600 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
Ben Chan672c1f52017-10-23 15:41:39 -0700601 .WillRepeatedly(DoAll(SetArgPointee<1>(2), Return(true)));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700602 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800603 .Times(AtLeast(1));
604 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800605
606 // Note: This will be a different payload object, but the response should
607 // have the same hash as before so as to not trivially reset because the
608 // response was different. We want to specifically test that even if the
609 // response is same, we should reset the state if we find it corrupted.
Amin Hassani538bd592020-11-04 20:46:08 -0800610 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700611 SetupPayloadStateWith2Urls(
612 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800613
614 // Make sure all counters get reset to 0 because of the corrupted URL index
615 // we supplied above.
616 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700617 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700618 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800619 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
620 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800621}
Jay Srinivasan08262882012-12-28 19:29:43 -0800622
Amin Hassani5ff76a22020-11-05 12:55:38 -0800623TEST_F(PayloadStateTest, NoBackoffInteractiveChecks) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700624 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700625 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800626 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -0700627 params.Init("", "", {.interactive = true});
Amin Hassani538bd592020-11-04 20:46:08 -0800628 FakeSystemState::Get()->set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700629
Amin Hassani538bd592020-11-04 20:46:08 -0800630 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700631 SetupPayloadStateWith2Urls(
632 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700633
634 // Simulate two failures (enough to cause payload backoff) and check
635 // again that we're ready to re-download without any backoff as this is
636 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700637 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
638 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700639 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
640 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
641 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
642 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
643}
644
Amin Hassani5ff76a22020-11-05 12:55:38 -0800645TEST_F(PayloadStateTest, NoBackoffForP2PUpdates) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700646 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700647 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800648 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -0700649 params.Init("", "", {});
Amin Hassani538bd592020-11-04 20:46:08 -0800650 FakeSystemState::Get()->set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700651
Amin Hassani538bd592020-11-04 20:46:08 -0800652 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700653 SetupPayloadStateWith2Urls(
654 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700655
656 // Simulate two failures (enough to cause payload backoff) and check
657 // again that we're ready to re-download without any backoff as this is
658 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700659 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
660 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700661 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
662 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
663 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
664 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700665 payload_state.SetUsingP2PForDownloading(true);
666 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700667 // Should not backoff for p2p updates.
668 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
669
Gilad Arnold74b5f552014-10-07 08:17:16 -0700670 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700671 // No actual p2p update if no url is provided.
672 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
673}
674
Amin Hassani5ff76a22020-11-05 12:55:38 -0800675TEST_F(PayloadStateTest, NoBackoffForDeltaPayloads) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800676 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800677 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800678
Amin Hassani538bd592020-11-04 20:46:08 -0800679 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700680 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800681
682 // Simulate a successful download and see that we're ready to download
683 // again without any backoff as this is a delta payload.
684 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700685 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
686 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800687 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
688
689 // Simulate two failures (enough to cause payload backoff) and check
690 // again that we're ready to re-download without any backoff as this is
691 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700692 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
693 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700694 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700695 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
696 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800697 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
698}
699
700static void CheckPayloadBackoffState(PayloadState* payload_state,
701 int expected_attempt_number,
702 TimeDelta expected_days) {
703 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700704 EXPECT_EQ(expected_attempt_number,
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800705 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800706 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
707 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
708 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
709 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
710 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
711 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
712 EXPECT_LT(expected_min_time.ToInternalValue(),
713 backoff_expiry_time.ToInternalValue());
714 EXPECT_GT(expected_max_time.ToInternalValue(),
715 backoff_expiry_time.ToInternalValue());
716}
717
Amin Hassani5ff76a22020-11-05 12:55:38 -0800718TEST_F(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800719 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800720 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800721
Amin Hassani538bd592020-11-04 20:46:08 -0800722 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700723 SetupPayloadStateWith2Urls(
724 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800725
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800726 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
727 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
728 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
729 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
730 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
731 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
732 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
733 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
734 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
735 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
Jay Srinivasan08262882012-12-28 19:29:43 -0800736}
737
Amin Hassani5ff76a22020-11-05 12:55:38 -0800738TEST_F(PayloadStateTest, BackoffLogicCanBeDisabled) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800739 OmahaResponse response;
740 response.disable_payload_backoff = true;
741 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800742
Amin Hassani538bd592020-11-04 20:46:08 -0800743 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700744 SetupPayloadStateWith2Urls(
745 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800746
747 // Simulate a successful download and see that we are ready to download
748 // again without any backoff.
749 payload_state.DownloadComplete();
750 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700751 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800752 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
753
754 // Test again, this time by simulating two errors that would cause
755 // the payload attempt number to increment due to wrap around. And
756 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700757 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
758 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800759 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700760 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800761 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
762}
763
Amin Hassani5ff76a22020-11-05 12:55:38 -0800764TEST_F(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700765 OmahaResponse response;
766 response.disable_payload_backoff = true;
767 PayloadState payload_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800768 uint64_t https_total = 0;
769 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700770
Amin Hassani538bd592020-11-04 20:46:08 -0800771 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700772 SetupPayloadStateWith2Urls(
773 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700774 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700775
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700776 // Simulate a previous attempt with in order to set an initial non-zero value
777 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800778 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700779 http_total += prev_chunk;
780 payload_state.DownloadProgress(prev_chunk);
781
782 // Ensure that the initial values for HTTP reflect this attempt.
783 EXPECT_EQ(prev_chunk,
784 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
785 EXPECT_EQ(http_total,
786 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
787
788 // Change the response hash so as to simulate a new response which will
789 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700790 SetupPayloadStateWith2Urls(
791 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700792 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700793
794 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800795 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700796 http_total += first_chunk;
797 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700798 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700799 EXPECT_EQ(first_chunk,
800 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
801 EXPECT_EQ(http_total,
802 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800803 EXPECT_EQ(
804 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700805 EXPECT_EQ(https_total,
806 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
807
808 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700809 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700810 payload_state.UpdateFailed(error);
811
Jay Srinivasan53173b92013-05-17 17:13:01 -0700812 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800813 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700814 https_total += second_chunk;
815 payload_state.DownloadProgress(second_chunk);
816 EXPECT_EQ(first_chunk,
817 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
818 EXPECT_EQ(http_total,
819 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800820 EXPECT_EQ(
821 second_chunk,
822 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700823 EXPECT_EQ(https_total,
824 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
825
826 // Simulate error to go back to http.
827 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800828 uint64_t third_chunk = 32345678;
829 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700830 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700831 payload_state.DownloadProgress(third_chunk);
832
833 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
834 EXPECT_EQ(http_chunk,
835 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700836 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700837 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800838 EXPECT_EQ(
839 second_chunk,
840 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700841 EXPECT_EQ(https_total,
842 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
843
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700844 // Simulate error (will cause URL switch), set p2p is to be used and
845 // then do 42MB worth of progress
846 payload_state.UpdateFailed(error);
847 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800848 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700849 payload_state.DownloadProgress(p2p_total);
850
851 EXPECT_EQ(p2p_total,
852 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
853
Amin Hassani538bd592020-11-04 20:46:08 -0800854 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700855 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -0700856 1, _, kPayloadTypeFull, _, _, 314, _, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700857
858 payload_state.UpdateSucceeded();
859
860 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800861 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700862 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800863 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700864 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800865 EXPECT_EQ(
866 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800867 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700868 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700869 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700870}
871
Amin Hassani5ff76a22020-11-05 12:55:38 -0800872TEST_F(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700873 OmahaResponse response;
874 PayloadState payload_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700875
Amin Hassani538bd592020-11-04 20:46:08 -0800876 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700877 SetupPayloadStateWith2Urls(
878 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700879
880 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800881 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700882 payload_state.DownloadProgress(num_bytes);
883
884 // Check that this was done via HTTP.
885 EXPECT_EQ(num_bytes,
886 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
887 EXPECT_EQ(num_bytes,
888 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
889
890 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700891 int64_t total_bytes[kNumDownloadSources] = {};
892 total_bytes[kDownloadSourceHttpServer] = num_bytes;
893
Amin Hassani538bd592020-11-04 20:46:08 -0800894 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700895 ReportSuccessfulUpdateMetrics(
Tianjie Xud4777a12017-10-24 14:54:18 -0700896 _,
897 _,
898 _,
899 _,
900 test_utils::DownloadSourceMatcher(total_bytes),
901 _,
902 _,
903 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700904 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700905 _))
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700906 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700907
908 payload_state.UpdateSucceeded();
909}
910
Amin Hassani5ff76a22020-11-05 12:55:38 -0800911TEST_F(PayloadStateTest, RestartingUpdateResetsMetrics) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700912 OmahaResponse response;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700913 PayloadState payload_state;
914
Amin Hassani538bd592020-11-04 20:46:08 -0800915 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700916
917 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700918 SetupPayloadStateWith2Urls(
919 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700920
Alex Deymo80f70ff2016-02-10 16:08:11 -0800921 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700922 payload_state.DownloadProgress(num_bytes);
923 EXPECT_EQ(num_bytes,
924 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
925 EXPECT_EQ(num_bytes,
926 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800927 EXPECT_EQ(
928 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800929 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700930 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
931
932 payload_state.UpdateRestarted();
933 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800934 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700935 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
936 EXPECT_EQ(num_bytes,
937 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
938}
939
Amin Hassani5ff76a22020-11-05 12:55:38 -0800940TEST_F(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Chris Sosabe45bef2013-04-09 18:25:12 -0700941 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700942
Amin Hassani538bd592020-11-04 20:46:08 -0800943 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700944 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700945 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
946
Amin Hassani538bd592020-11-04 20:46:08 -0800947 EXPECT_TRUE(payload_state.Initialize());
Chris Sosabe45bef2013-04-09 18:25:12 -0700948
949 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800950 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700951
Amin Hassani538bd592020-11-04 20:46:08 -0800952 FakeSystemState::Get()->set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700953 payload_state.UpdateResumed();
954 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800955 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700956
Amin Hassani538bd592020-11-04 20:46:08 -0800957 FakeSystemState::Get()->set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700958 payload_state.UpdateResumed();
959 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800960 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700961
962 // Restart the update again to verify we set the num of reboots back to 0.
963 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800964 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700965}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700966
Amin Hassani5ff76a22020-11-05 12:55:38 -0800967TEST_F(PayloadStateTest, RollbackHappened) {
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200968 PayloadState payload_state;
969
970 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Amin Hassani538bd592020-11-04 20:46:08 -0800971 FakeSystemState::Get()->mock_powerwash_safe_prefs();
972 EXPECT_TRUE(payload_state.Initialize());
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200973
974 // Verify pre-conditions are good.
975 EXPECT_FALSE(payload_state.GetRollbackHappened());
976
977 // Set to true.
978 EXPECT_CALL(*mock_powerwash_safe_prefs,
979 SetBoolean(kPrefsRollbackHappened, true));
980 payload_state.SetRollbackHappened(true);
981 EXPECT_TRUE(payload_state.GetRollbackHappened());
982
983 // Set to false.
984 EXPECT_CALL(*mock_powerwash_safe_prefs, Delete(kPrefsRollbackHappened));
985 payload_state.SetRollbackHappened(false);
986 EXPECT_FALSE(payload_state.GetRollbackHappened());
987
988 // Let's verify we can reload it correctly.
989 EXPECT_CALL(*mock_powerwash_safe_prefs, GetBoolean(kPrefsRollbackHappened, _))
990 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
991 EXPECT_CALL(*mock_powerwash_safe_prefs,
992 SetBoolean(kPrefsRollbackHappened, true));
993 payload_state.LoadRollbackHappened();
994 EXPECT_TRUE(payload_state.GetRollbackHappened());
995}
996
Amin Hassani5ff76a22020-11-05 12:55:38 -0800997TEST_F(PayloadStateTest, RollbackVersion) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700998 PayloadState payload_state;
999
Alex Deymo8427b4a2014-11-05 14:00:32 -08001000 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Amin Hassani538bd592020-11-04 20:46:08 -08001001 FakeSystemState::Get()->mock_powerwash_safe_prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -07001002
Andrewcc6ab9f2020-06-25 07:41:40 -07001003 // Mock out the os version and make sure it's excluded correctly.
Chris Sosaaa18e162013-06-20 13:20:30 -07001004 string rollback_version = "2345.0.0";
Amin Hassani538bd592020-11-04 20:46:08 -08001005 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -07001006 params.Init(rollback_version, "", {});
Amin Hassani538bd592020-11-04 20:46:08 -08001007 FakeSystemState::Get()->set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001008
Amin Hassani538bd592020-11-04 20:46:08 -08001009 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001010
1011 // Verify pre-conditions are good.
1012 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1013
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001014 EXPECT_CALL(*mock_powerwash_safe_prefs,
1015 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosaaa18e162013-06-20 13:20:30 -07001016 payload_state.Rollback();
1017
1018 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001019
1020 // Change it up a little and verify we load it correctly.
1021 rollback_version = "2345.0.1";
1022 // Let's verify we can reload it correctly.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001023 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(kPrefsRollbackVersion, _))
1024 .WillOnce(DoAll(SetArgPointee<1>(rollback_version), Return(true)));
1025 EXPECT_CALL(*mock_powerwash_safe_prefs,
1026 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001027 payload_state.LoadRollbackVersion();
1028 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001029
David Zeuthen96197df2014-04-16 12:22:39 -07001030 // Check that we report only UpdateEngine.Rollback.* metrics in
1031 // UpdateSucceeded().
Amin Hassani538bd592020-11-04 20:46:08 -08001032 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001033 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1034 .Times(1);
1035
David Zeuthenafed4a12014-04-09 15:28:44 -07001036 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001037}
1038
Amin Hassani5ff76a22020-11-05 12:55:38 -08001039TEST_F(PayloadStateTest, DurationsAreCorrect) {
David Zeuthenf413fe52013-04-22 14:04:39 -07001040 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001041 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001042 PayloadState payload_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001043 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001044 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001045
1046 // Set the clock to a well-known time - 1 second on the wall-clock
1047 // and 2 seconds on the monotonic clock
1048 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1049 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1050
Amin Hassani538bd592020-11-04 20:46:08 -08001051 FakeSystemState::Get()->set_clock(&fake_clock);
1052 FakeSystemState::Get()->set_prefs(&fake_prefs);
1053 EXPECT_TRUE(payload_state.Initialize());
David Zeuthenf413fe52013-04-22 14:04:39 -07001054
1055 // Check that durations are correct for a successful update where
1056 // time has advanced 7 seconds on the wall clock and 4 seconds on
1057 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001058 SetupPayloadStateWith2Urls(
1059 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001060 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1061 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1062 payload_state.UpdateSucceeded();
1063 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1064 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1065
1066 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001067 SetupPayloadStateWith2Urls(
1068 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001069 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1070 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1071
1072 // Advance time a bit (10 secs), simulate download progress and
1073 // check that durations are updated.
1074 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1075 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1076 payload_state.DownloadProgress(10);
1077 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1078 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1079
1080 // Now simulate a reboot by resetting monotonic time (to 5000) and
1081 // creating a new PayloadState object and check that we load the
1082 // durations correctly (e.g. they are the same as before).
1083 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1084 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001085 EXPECT_TRUE(payload_state2.Initialize());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001086 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001087 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001088 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1089 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001090
1091 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1092 // and check that the durations are increased accordingly.
1093 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1094 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1095 payload_state2.UpdateSucceeded();
1096 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001097 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1098 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001099}
1100
Amin Hassani5ff76a22020-11-05 12:55:38 -08001101TEST_F(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
David Zeuthene4c58bf2013-06-18 17:26:50 -07001102 OmahaResponse response;
1103 PayloadState payload_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001104 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001105 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001106
1107 // Set the clock to a well-known time (t = 30 seconds).
Tianjie Xu90aaa102017-10-10 17:39:03 -07001108 fake_clock.SetMonotonicTime(
1109 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001110
Amin Hassani538bd592020-11-04 20:46:08 -08001111 FakeSystemState::Get()->set_clock(&fake_clock);
1112 FakeSystemState::Get()->set_prefs(&fake_prefs);
1113 EXPECT_TRUE(payload_state.Initialize());
David Zeuthene4c58bf2013-06-18 17:26:50 -07001114
1115 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001116 SetupPayloadStateWith2Urls(
1117 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001118 payload_state.UpdateSucceeded();
1119
1120 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001121 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001122
1123 // Now simulate a reboot and set the wallclock time to a later point
1124 // (t = 500 seconds). We do this by using a new PayloadState object
1125 // and checking that it emits the right UMA metric with the right
1126 // value.
Tianjie Xu90aaa102017-10-10 17:39:03 -07001127 fake_clock.SetMonotonicTime(
1128 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001129 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001130 EXPECT_TRUE(payload_state2.Initialize());
David Zeuthene4c58bf2013-06-18 17:26:50 -07001131
1132 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Amin Hassani538bd592020-11-04 20:46:08 -08001133 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001134 ReportTimeToReboot(7));
Amin Hassani538bd592020-11-04 20:46:08 -08001135 FakeSystemState::Get()->set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001136
1137 payload_state2.UpdateEngineStarted();
1138
1139 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001140 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001141}
1142
Amin Hassani5ff76a22020-11-05 12:55:38 -08001143TEST_F(PayloadStateTest, RestartAfterCrash) {
Alex Deymo569c4242013-07-24 12:01:01 -07001144 PayloadState payload_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001145 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Amin Hassani538bd592020-11-04 20:46:08 -08001146 FakeSystemState::Get()->set_metrics_reporter(&mock_metrics_reporter);
1147 NiceMock<MockPrefs>* prefs = FakeSystemState::Get()->mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001148
Amin Hassani538bd592020-11-04 20:46:08 -08001149 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo569c4242013-07-24 12:01:01 -07001150
David Zeuthen4e1d1492014-04-25 13:12:27 -07001151 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001152 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1153 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1154 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1155 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1156 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1157 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1158 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001159 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001160
Alex Deymo569c4242013-07-24 12:01:01 -07001161 // Simulate an update_engine restart without a reboot.
Amin Hassani538bd592020-11-04 20:46:08 -08001162 FakeSystemState::Get()->set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001163
1164 payload_state.UpdateEngineStarted();
1165}
1166
Amin Hassani5ff76a22020-11-05 12:55:38 -08001167TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001168 PayloadState payload_state;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001169
1170 // If there's no marker at startup, ensure we don't report a metric.
Amin Hassani538bd592020-11-04 20:46:08 -08001171 EXPECT_TRUE(payload_state.Initialize());
1172 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001173 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1174 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001175 payload_state.UpdateEngineStarted();
1176}
1177
Amin Hassani5ff76a22020-11-05 12:55:38 -08001178TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001179 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001180 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001181
1182 // If we have a marker at startup, ensure it's reported and the
1183 // marker is then cleared.
Amin Hassani538bd592020-11-04 20:46:08 -08001184 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001185 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001186
Amin Hassani538bd592020-11-04 20:46:08 -08001187 EXPECT_TRUE(payload_state.Initialize());
David Zeuthen4e1d1492014-04-25 13:12:27 -07001188
Amin Hassani538bd592020-11-04 20:46:08 -08001189 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001190 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1191 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001192 payload_state.UpdateEngineStarted();
1193
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001194 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001195}
1196
Amin Hassani5ff76a22020-11-05 12:55:38 -08001197TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001198 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001199 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001200
1201 // Make sure the marker is written and cleared during an attempt and
1202 // also that we DO NOT emit the metric (since the attempt didn't end
1203 // abnormally).
Amin Hassani538bd592020-11-04 20:46:08 -08001204 FakeSystemState::Get()->set_prefs(&fake_prefs);
1205 EXPECT_TRUE(payload_state.Initialize());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001206 OmahaResponse response;
1207 response.packages.resize(1);
1208 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001209
Amin Hassani538bd592020-11-04 20:46:08 -08001210 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001211 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1212 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001213
1214 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001215 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001216
1217 payload_state.UpdateRestarted();
1218
1219 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001220 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001221
1222 payload_state.UpdateSucceeded();
1223
1224 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001225 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001226}
1227
Amin Hassani5ff76a22020-11-05 12:55:38 -08001228TEST_F(PayloadStateTest, CandidateUrlsComputedCorrectly) {
Jay Srinivasan53173b92013-05-17 17:13:01 -07001229 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001230 PayloadState payload_state;
1231
Jay Srinivasan53173b92013-05-17 17:13:01 -07001232 policy::MockDevicePolicy disable_http_policy;
Amin Hassani538bd592020-11-04 20:46:08 -08001233 FakeSystemState::Get()->set_device_policy(&disable_http_policy);
1234 EXPECT_TRUE(payload_state.Initialize());
Chris Sosaf7d80042013-08-22 16:45:17 -07001235
1236 // Test with no device policy. Should default to allowing http.
1237 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1238 .WillRepeatedly(Return(false));
1239
1240 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001241 SetupPayloadStateWith2Urls(
1242 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001243
1244 // Check that we use the HTTP URL since there is no value set for allowing
1245 // http.
1246 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1247
1248 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001249 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001250 .WillRepeatedly(DoAll(SetArgPointee<0>(false), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001251
Chris Sosaf7d80042013-08-22 16:45:17 -07001252 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001253 SetupPayloadStateWith2Urls(
1254 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001255
1256 // Check that we skip the HTTP URL and use only the HTTPS url.
1257 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1258
1259 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001260 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001261 payload_state.UpdateFailed(error);
1262
1263 // Check that we still skip the HTTP URL and use only the HTTPS url.
1264 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001265 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001266
1267 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001268 SetupPayloadStateWith2Urls(
1269 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001270
1271 // Check that we still skip the HTTP URL and use only the HTTPS url.
1272 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1273
1274 // Now, pretend that the HTTP policy is turned on. We want to make sure
1275 // the new policy is honored.
1276 policy::MockDevicePolicy enable_http_policy;
Amin Hassani538bd592020-11-04 20:46:08 -08001277 FakeSystemState::Get()->set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001278 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001279 .WillRepeatedly(DoAll(SetArgPointee<0>(true), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001280
1281 // Now, set the same response using the same hash
1282 // so that we can test that the state is reset not because of the
1283 // hash but because of the policy change which results in candidate url
1284 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001285 SetupPayloadStateWith2Urls(
1286 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001287
1288 // Check that we use the HTTP URL now and the failure count is reset.
1289 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001290 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001291
1292 // Fake a failure and see if we're moving over to the HTTPS url and update
1293 // the URL switch count properly.
1294 payload_state.UpdateFailed(error);
1295 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001296 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1297 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001298}
1299
Amin Hassani5ff76a22020-11-05 12:55:38 -08001300TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001301 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001302 PayloadState payload_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001303
Amin Hassani538bd592020-11-04 20:46:08 -08001304 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001305 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001306
1307 // Simulate a successful download and update.
1308 payload_state.DownloadComplete();
Amin Hassani538bd592020-11-04 20:46:08 -08001309 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Sen Jiang8712e962018-05-08 12:12:28 -07001310 ReportSuccessfulUpdateMetrics(
1311 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001312 payload_state.UpdateSucceeded();
1313
1314 // Mock the request to a request where the delta was disabled but Omaha sends
1315 // a delta anyway and test again.
Amin Hassani538bd592020-11-04 20:46:08 -08001316 OmahaRequestParams params;
Alex Deymo1c656c42013-06-28 11:02:14 -07001317 params.set_delta_okay(false);
Amin Hassani538bd592020-11-04 20:46:08 -08001318 FakeSystemState::Get()->set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001319
Amin Hassani538bd592020-11-04 20:46:08 -08001320 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001321 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001322
1323 payload_state.DownloadComplete();
1324
Amin Hassani538bd592020-11-04 20:46:08 -08001325 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Sen Jiang8712e962018-05-08 12:12:28 -07001326 ReportSuccessfulUpdateMetrics(
1327 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001328 payload_state.UpdateSucceeded();
1329}
1330
Amin Hassani5ff76a22020-11-05 12:55:38 -08001331TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001332 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001333 PayloadState payload_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001334
Alex Deymo1c656c42013-06-28 11:02:14 -07001335 // Mock the request to a request where the delta was disabled.
Amin Hassani538bd592020-11-04 20:46:08 -08001336 OmahaRequestParams params;
Alex Deymo1c656c42013-06-28 11:02:14 -07001337 params.set_delta_okay(false);
Amin Hassani538bd592020-11-04 20:46:08 -08001338 FakeSystemState::Get()->set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001339
Amin Hassani538bd592020-11-04 20:46:08 -08001340 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001341 SetupPayloadStateWith2Urls(
1342 "Hash6437", true, false, &payload_state, &response);
1343
Alex Deymo1c656c42013-06-28 11:02:14 -07001344 // Simulate a successful download and update.
1345 payload_state.DownloadComplete();
1346
Amin Hassani538bd592020-11-04 20:46:08 -08001347 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001348 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -07001349 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001350 payload_state.UpdateSucceeded();
1351}
1352
Amin Hassani5ff76a22020-11-05 12:55:38 -08001353TEST_F(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
Alex Deymo1c656c42013-06-28 11:02:14 -07001354 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001355 PayloadState payload_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001356
Amin Hassani538bd592020-11-04 20:46:08 -08001357 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001358 SetupPayloadStateWith2Urls(
1359 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001360
Alex Deymo820cc702013-06-28 15:43:46 -07001361 // Mock the request to a request where the delta is enabled, although the
1362 // result is full.
Amin Hassani538bd592020-11-04 20:46:08 -08001363 OmahaRequestParams params;
Alex Deymo1c656c42013-06-28 11:02:14 -07001364 params.set_delta_okay(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001365 FakeSystemState::Get()->set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001366
1367 // Simulate a successful download and update.
1368 payload_state.DownloadComplete();
1369
Amin Hassani538bd592020-11-04 20:46:08 -08001370 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Sen Jiang8712e962018-05-08 12:12:28 -07001371 ReportSuccessfulUpdateMetrics(
1372 _, _, kPayloadTypeFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001373 payload_state.UpdateSucceeded();
1374}
1375
Amin Hassani5ff76a22020-11-05 12:55:38 -08001376TEST_F(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Alex Deymo42432912013-07-12 20:21:15 -07001377 OmahaResponse response;
1378 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001379 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001380 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001381
Amin Hassani538bd592020-11-04 20:46:08 -08001382 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001383 SetupPayloadStateWith2Urls(
1384 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001385
1386 // Simulate a successful download and update.
1387 payload_state.DownloadComplete();
1388 payload_state.UpdateSucceeded();
1389 payload_state.ExpectRebootInNewVersion("Version:12345678");
1390
1391 // Reboot into the same environment to get an UMA metric with a value of 1.
Amin Hassani538bd592020-11-04 20:46:08 -08001392 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001393 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001394 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001395 Mock::VerifyAndClearExpectations(
1396 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001397
1398 // Simulate a second update and reboot into the same environment, this should
1399 // send a value of 2.
1400 payload_state.ExpectRebootInNewVersion("Version:12345678");
1401
Amin Hassani538bd592020-11-04 20:46:08 -08001402 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001403 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001404 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001405 Mock::VerifyAndClearExpectations(
1406 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001407
1408 // Simulate a third failed reboot to new version, but this time for a
1409 // different payload. This should send a value of 1 this time.
1410 payload_state.ExpectRebootInNewVersion("Version:3141592");
Amin Hassani538bd592020-11-04 20:46:08 -08001411 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001412 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001413 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001414 Mock::VerifyAndClearExpectations(
1415 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001416}
1417
Amin Hassani5ff76a22020-11-05 12:55:38 -08001418TEST_F(PayloadStateTest, RebootAfterUpdateSucceed) {
Alex Deymo42432912013-07-12 20:21:15 -07001419 OmahaResponse response;
1420 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001421 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001422 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001423
Amin Hassani538bd592020-11-04 20:46:08 -08001424 FakeBootControl* fake_boot_control =
1425 FakeSystemState::Get()->fake_boot_control();
Alex Deymo763e7db2015-08-27 21:08:08 -07001426 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001427
Amin Hassani538bd592020-11-04 20:46:08 -08001428 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001429 SetupPayloadStateWith2Urls(
1430 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001431
1432 // Simulate a successful download and update.
1433 payload_state.DownloadComplete();
1434 payload_state.UpdateSucceeded();
1435 payload_state.ExpectRebootInNewVersion("Version:12345678");
1436
1437 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001438 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001439
Amin Hassani538bd592020-11-04 20:46:08 -08001440 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001441 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001442 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001443 payload_state.ReportFailedBootIfNeeded();
1444
Alex Deymo763e7db2015-08-27 21:08:08 -07001445 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001446 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001447 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001448 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001449}
1450
Amin Hassani5ff76a22020-11-05 12:55:38 -08001451TEST_F(PayloadStateTest, RebootAfterCanceledUpdate) {
Alex Deymo42432912013-07-12 20:21:15 -07001452 OmahaResponse response;
1453 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001454 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001455
Amin Hassani538bd592020-11-04 20:46:08 -08001456 FakeSystemState::Get()->set_prefs(&fake_prefs);
1457 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001458 SetupPayloadStateWith2Urls(
1459 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001460
1461 // Simulate a successful download and update.
1462 payload_state.DownloadComplete();
1463 payload_state.UpdateSucceeded();
1464 payload_state.ExpectRebootInNewVersion("Version:12345678");
1465
Amin Hassani538bd592020-11-04 20:46:08 -08001466 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001467 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001468 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001469
1470 // Cancel the applied update.
1471 payload_state.ResetUpdateStatus();
1472
1473 // Simulate a reboot.
1474 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001475}
1476
Amin Hassani5ff76a22020-11-05 12:55:38 -08001477TEST_F(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Alex Deymo42432912013-07-12 20:21:15 -07001478 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001479 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001480
Amin Hassani538bd592020-11-04 20:46:08 -08001481 FakeSystemState::Get()->set_prefs(&fake_prefs);
1482 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo42432912013-07-12 20:21:15 -07001483
Amin Hassani538bd592020-11-04 20:46:08 -08001484 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001485 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001486 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001487
1488 // Simulate a reboot in this environment.
1489 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001490}
1491
Amin Hassani5ff76a22020-11-05 12:55:38 -08001492TEST_F(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
David Zeuthendcba8092013-08-06 12:16:35 -07001493 OmahaResponse response;
1494 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001495 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001496 FakeSystemState::Get()->set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001497
Amin Hassani538bd592020-11-04 20:46:08 -08001498 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001499 SetupPayloadStateWith2Urls(
1500 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001501
1502 // Should allow exactly kMaxP2PAttempts...
1503 for (int n = 0; n < kMaxP2PAttempts; n++) {
1504 payload_state.P2PNewAttempt();
1505 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1506 }
1507 // ... but not more than that.
1508 payload_state.P2PNewAttempt();
1509 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001510}
1511
Amin Hassani5ff76a22020-11-05 12:55:38 -08001512TEST_F(PayloadStateTest, DisallowP2PAfterDeadline) {
David Zeuthendcba8092013-08-06 12:16:35 -07001513 OmahaResponse response;
1514 PayloadState payload_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001515 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001516 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001517
Amin Hassani538bd592020-11-04 20:46:08 -08001518 FakeSystemState::Get()->set_clock(&fake_clock);
1519 FakeSystemState::Get()->set_prefs(&fake_prefs);
1520 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001521 SetupPayloadStateWith2Urls(
1522 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001523
1524 // Set the clock to 1 second.
1525 Time epoch = Time::FromInternalValue(1000000);
1526 fake_clock.SetWallclockTime(epoch);
1527
1528 // Do an attempt - this will set the timestamp.
1529 payload_state.P2PNewAttempt();
1530
1531 // Check that the timestamp equals what we just set.
1532 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1533
1534 // Time hasn't advanced - this should work.
1535 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1536
1537 // Set clock to half the deadline - this should work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001538 fake_clock.SetWallclockTime(
1539 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
David Zeuthendcba8092013-08-06 12:16:35 -07001540 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1541
1542 // Check that the first attempt timestamp hasn't changed just
1543 // because the wall-clock time changed.
1544 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1545
1546 // Set clock to _just_ before the deadline - this should work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001547 fake_clock.SetWallclockTime(
1548 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001549 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1550
1551 // Set clock to _just_ after the deadline - this should not work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001552 fake_clock.SetWallclockTime(
1553 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001554 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001555}
1556
Amin Hassani5ff76a22020-11-05 12:55:38 -08001557TEST_F(PayloadStateTest, P2PStateVarsInitialValue) {
David Zeuthendcba8092013-08-06 12:16:35 -07001558 OmahaResponse response;
1559 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001560 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001561
Amin Hassani538bd592020-11-04 20:46:08 -08001562 FakeSystemState::Get()->set_prefs(&fake_prefs);
1563 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001564 SetupPayloadStateWith2Urls(
1565 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001566
1567 Time null_time = Time();
1568 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1569 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001570}
1571
Amin Hassani5ff76a22020-11-05 12:55:38 -08001572TEST_F(PayloadStateTest, P2PStateVarsArePersisted) {
David Zeuthendcba8092013-08-06 12:16:35 -07001573 OmahaResponse response;
1574 PayloadState payload_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001575 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001576 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001577 FakeSystemState::Get()->set_clock(&fake_clock);
1578 FakeSystemState::Get()->set_prefs(&fake_prefs);
1579 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001580 SetupPayloadStateWith2Urls(
1581 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001582
1583 // Set the clock to something known.
1584 Time time = Time::FromInternalValue(12345);
1585 fake_clock.SetWallclockTime(time);
1586
1587 // New p2p attempt - as a side-effect this will update the p2p state vars.
1588 payload_state.P2PNewAttempt();
1589 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1590 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1591
1592 // Now create a new PayloadState and check that it loads the state
1593 // vars correctly.
1594 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001595 EXPECT_TRUE(payload_state2.Initialize());
David Zeuthendcba8092013-08-06 12:16:35 -07001596 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1597 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001598}
1599
Amin Hassani5ff76a22020-11-05 12:55:38 -08001600TEST_F(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
David Zeuthendcba8092013-08-06 12:16:35 -07001601 OmahaResponse response;
1602 PayloadState payload_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001603 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001604 FakePrefs fake_prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001605 FakeSystemState::Get()->set_clock(&fake_clock);
1606 FakeSystemState::Get()->set_prefs(&fake_prefs);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001607
Amin Hassani538bd592020-11-04 20:46:08 -08001608 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001609 SetupPayloadStateWith2Urls(
1610 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001611
1612 // Set the clock to something known.
1613 Time time = Time::FromInternalValue(12345);
1614 fake_clock.SetWallclockTime(time);
1615
1616 // New p2p attempt - as a side-effect this will update the p2p state vars.
1617 payload_state.P2PNewAttempt();
1618 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1619 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1620
1621 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001622 SetupPayloadStateWith2Urls(
1623 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001624
1625 // ... and check that it clears the P2P state vars.
1626 Time null_time = Time();
1627 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1628 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001629}
1630
Amin Hassani5ff76a22020-11-05 12:55:38 -08001631TEST_F(PayloadStateTest, NextPayloadResetsUrlIndex) {
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001632 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001633 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001634 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001635 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001636 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001637
1638 OmahaResponse response;
1639 response.packages.push_back(
1640 {.payload_urls = {"http://test1a", "http://test2a"},
1641 .size = 123456789,
1642 .metadata_size = 58123,
1643 .metadata_signature = "msign",
1644 .hash = "hash"});
1645 response.packages.push_back({.payload_urls = {"http://test1b"},
1646 .size = 123456789,
1647 .metadata_size = 58123,
1648 .metadata_signature = "msign",
1649 .hash = "hash"});
1650 payload_state.SetResponse(response);
1651
1652 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1a");
1653 payload_state.IncrementUrlIndex();
1654 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test2a");
1655
1656 EXPECT_TRUE(payload_state.NextPayload());
1657 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1b");
1658}
1659
Amin Hassani5ff76a22020-11-05 12:55:38 -08001660TEST_F(PayloadStateTest, ExcludeNoopForNonExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001661 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001662 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001663 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001664 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001665 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001666
1667 OmahaResponse response;
1668 response.packages.push_back(
1669 {.payload_urls = {"http://test1a", "http://test2a"},
1670 .size = 123456789,
1671 .metadata_size = 58123,
1672 .metadata_signature = "msign",
1673 .hash = "hash",
1674 .can_exclude = false});
1675 payload_state.SetResponse(response);
1676
1677 EXPECT_CALL(mock_excluder, Exclude(_)).Times(0);
1678 payload_state.ExcludeCurrentPayload();
1679}
1680
Amin Hassani5ff76a22020-11-05 12:55:38 -08001681TEST_F(PayloadStateTest, ExcludeOnlyCanExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001682 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001683 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001684 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001685 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001686 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001687
1688 OmahaResponse response;
1689 response.packages.push_back(
1690 {.payload_urls = {"http://test1a", "http://test2a"},
1691 .size = 123456789,
1692 .metadata_size = 58123,
1693 .metadata_signature = "msign",
1694 .hash = "hash",
1695 .can_exclude = true});
1696 payload_state.SetResponse(response);
1697
1698 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1699 .WillOnce(Return(true));
1700 payload_state.ExcludeCurrentPayload();
1701}
1702
Amin Hassani5ff76a22020-11-05 12:55:38 -08001703TEST_F(PayloadStateTest, IncrementFailureExclusionTest) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001704 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001705 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001706 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001707 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001708 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001709
1710 OmahaResponse response;
1711 // Critical package.
1712 response.packages.push_back(
1713 {.payload_urls = {"http://crit-test1a", "http://crit-test2a"},
1714 .size = 123456789,
1715 .metadata_size = 58123,
1716 .metadata_signature = "msign",
1717 .hash = "hash",
1718 .can_exclude = false});
1719 // Non-critical package.
1720 response.packages.push_back(
1721 {.payload_urls = {"http://test1a", "http://test2a"},
1722 .size = 123456789,
1723 .metadata_size = 58123,
1724 .metadata_signature = "msign",
1725 .hash = "hash",
1726 .can_exclude = true});
1727 response.max_failure_count_per_url = 2;
1728 payload_state.SetResponse(response);
1729
1730 // Critical package won't be excluded.
1731 // Increment twice as failure count allowed per URL is set to 2.
1732 payload_state.IncrementFailureCount();
1733 payload_state.IncrementFailureCount();
1734
1735 EXPECT_TRUE(payload_state.NextPayload());
1736
1737 // First increment failure should not exclude.
1738 payload_state.IncrementFailureCount();
1739
1740 // Second increment failure should exclude.
1741 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1742 .WillOnce(Return(true));
1743 payload_state.IncrementFailureCount();
1744}
1745
Amin Hassani5ff76a22020-11-05 12:55:38 -08001746TEST_F(PayloadStateTest, HaltExclusionPostPayloadExhaustion) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001747 PayloadState payload_state;
Jae Hoon Kim76583912020-06-23 10:24:03 -07001748 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001749 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim76583912020-06-23 10:24:03 -07001750 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001751 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim76583912020-06-23 10:24:03 -07001752
1753 OmahaResponse response;
1754 // Non-critical package.
1755 response.packages.push_back(
1756 {.payload_urls = {"http://test1a", "http://test2a"},
1757 .size = 123456789,
1758 .metadata_size = 58123,
1759 .metadata_signature = "msign",
1760 .hash = "hash",
1761 .can_exclude = true});
1762 payload_state.SetResponse(response);
1763
1764 // Exclusion should be called when excluded.
1765 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1766 .WillOnce(Return(true));
1767 payload_state.ExcludeCurrentPayload();
1768
1769 // No more paylods to go through.
1770 EXPECT_FALSE(payload_state.NextPayload());
1771
1772 // Exclusion should not be called as all |Payload|s are exhausted.
1773 payload_state.ExcludeCurrentPayload();
1774}
1775
Amin Hassani5ff76a22020-11-05 12:55:38 -08001776TEST_F(PayloadStateTest, NonInfinitePayloadIndexIncrement) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001777 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001778 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim76583912020-06-23 10:24:03 -07001779
1780 payload_state.SetResponse({});
1781
1782 EXPECT_FALSE(payload_state.NextPayload());
1783 int payload_index = payload_state.payload_index_;
1784
1785 EXPECT_FALSE(payload_state.NextPayload());
1786 EXPECT_EQ(payload_index, payload_state.payload_index_);
1787}
1788
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001789} // namespace chromeos_update_engine