blob: 5478fcae87c3284e9541e45835c7af17f36e6175 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/payload_state.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080018
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080020#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070026#include "update_engine/common/excluder_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/fake_hardware.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/common/metrics_reporter_interface.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070029#include "update_engine/common/mock_excluder.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/mock_prefs.h"
31#include "update_engine/common/prefs.h"
32#include "update_engine/common/test_utils.h"
33#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070034#include "update_engine/cros/fake_system_state.h"
35#include "update_engine/cros/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036
Jay Srinivasan08262882012-12-28 19:29:43 -080037using base::Time;
38using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080039using std::string;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080040using testing::_;
Alex Deymo42432912013-07-12 20:21:15 -070041using testing::AnyNumber;
42using testing::AtLeast;
43using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080045using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070046using testing::SetArgPointee;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070047using testing::StrictMock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080048
49namespace chromeos_update_engine {
50
Jay Srinivasan19409b72013-04-12 19:23:36 -070051const char* kCurrentBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080052 "current-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070053const char* kTotalBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080054 "total-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070055const char* kCurrentBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080056 "current-bytes-downloaded-from-HttpServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070057const char* kTotalBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080058 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070059const char* kCurrentBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080060 "current-bytes-downloaded-from-HttpPeer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070061const char* kTotalBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080062 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070063
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080064static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070065 bool http_enabled,
Sen Jiangcdd52062017-05-18 15:33:10 -070066 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080067 PayloadState* payload_state,
68 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080069 response->packages.clear();
70 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
71 .size = 523456789,
72 .metadata_size = 558123,
73 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070074 .hash = hash,
75 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080076 response->max_failure_count_per_url = 3;
77 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080078 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070079
80 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080081 " NumURLs = 1\n"
82 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070083
84 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080085 " NumURLs = 2\n"
86 " Candidate Url0 = http://test\n"
87 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070088
Sen Jiang0affc2c2017-02-10 15:55:05 -080089 string expected_response_sign = base::StringPrintf(
90 "Payload 0:\n"
91 " Size = 523456789\n"
92 " Sha256 Hash = %s\n"
93 " Metadata Size = 558123\n"
94 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070095 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080096 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080097 "Max Failure Count Per Url = %d\n"
98 "Disable Payload Backoff = %d\n",
99 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -0700100 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800101 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800102 response->max_failure_count_per_url,
103 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800104 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800105}
106
Amin Hassani538bd592020-11-04 20:46:08 -0800107class PayloadStateTest : public ::testing::Test {
108 public:
109 void SetUp() { FakeSystemState::CreateInstance(); }
Amin Hassani90e9f192020-11-18 14:20:56 -0800110
111 // TODO(b/171829801): Replace all the |MockPrefs| in this file with
112 // |FakePrefs| so we don't have to catch every single unimportant mock call.
Amin Hassani538bd592020-11-04 20:46:08 -0800113};
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800114
Amin Hassani5ff76a22020-11-05 12:55:38 -0800115TEST_F(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800116 OmahaResponse response;
Amin Hassani90e9f192020-11-18 14:20:56 -0800117 FakeSystemState::Get()->set_prefs(nullptr);
118 auto* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700119 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700120 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800121 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700122 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800123 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700124 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
125 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
126 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800127 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700128 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800129 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700130 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700132 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800133 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700134 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800135 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700136 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800137 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700138 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800140 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800141 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800143 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800144 "Max Failure Count Per Url = 0\n"
145 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800146 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700147 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800148 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
149 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700150 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800151}
152
Amin Hassani5ff76a22020-11-05 12:55:38 -0800153TEST_F(PayloadStateTest, SetResponseWorksWithSingleUrl) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800154 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800155 response.packages.push_back({.payload_urls = {"https://single.url.test"},
156 .size = 123456789,
157 .metadata_size = 58123,
158 .metadata_signature = "msign",
159 .hash = "hash"});
Amin Hassani90e9f192020-11-18 14:20:56 -0800160 FakeSystemState::Get()->set_prefs(nullptr);
161 auto* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700162 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700163 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700164 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800165 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
166 .Times(AtLeast(1));
167 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
169 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
170 .Times(AtLeast(1));
171 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
172 .Times(AtLeast(1));
173 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
174 .Times(AtLeast(1));
175 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
176 .Times(AtLeast(1));
177 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
178 .Times(AtLeast(1));
179 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
180 .Times(AtLeast(1));
181 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800182 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800183 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800184 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800185 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800186 string expected_response_sign =
187 "Payload 0:\n"
188 " Size = 123456789\n"
189 " Sha256 Hash = hash\n"
190 " Metadata Size = 58123\n"
191 " Metadata Signature = msign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700192 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800193 " NumURLs = 1\n"
194 " Candidate Url0 = https://single.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800195 "Max Failure Count Per Url = 0\n"
196 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800197 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700198 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800199 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
200 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700201 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800202}
203
Amin Hassani5ff76a22020-11-05 12:55:38 -0800204TEST_F(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800205 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800206 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
207 "https://multiple.url.test"},
208 .size = 523456789,
209 .metadata_size = 558123,
210 .metadata_signature = "metasign",
211 .hash = "rhash"});
Amin Hassani90e9f192020-11-18 14:20:56 -0800212 FakeSystemState::Get()->set_prefs(nullptr);
213 auto* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700214 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700215 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700216 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800217 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
220 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
222 .Times(AtLeast(1));
223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
224 .Times(AtLeast(1));
225 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
226 .Times(AtLeast(1));
227 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
228 .Times(AtLeast(1));
229 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700230
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800231 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800232 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800233 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800234 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800235 string expected_response_sign =
236 "Payload 0:\n"
237 " Size = 523456789\n"
238 " Sha256 Hash = rhash\n"
239 " Metadata Size = 558123\n"
240 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700241 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800242 " NumURLs = 2\n"
243 " Candidate Url0 = http://multiple.url.test\n"
244 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800245 "Max Failure Count Per Url = 0\n"
246 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800247 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700248 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800249 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
250 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700251 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800252}
253
Amin Hassani5ff76a22020-11-05 12:55:38 -0800254TEST_F(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800255 OmahaResponse response;
Amin Hassani90e9f192020-11-18 14:20:56 -0800256 FakeSystemState::Get()->set_prefs(nullptr);
257 auto* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800258 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800259
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700260 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800261 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700262 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800263 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700264 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800265 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700266 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800267 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700268 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800269 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700270 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700271
Chris Sosabe45bef2013-04-09 18:25:12 -0700272 // Reboots will be set
273 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
274
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800275 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700276 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
277 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800278
279 // Failure count should be called each times url index is set, so that's
280 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700281 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800282 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800283
Amin Hassani538bd592020-11-04 20:46:08 -0800284 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800285
286 // This does a SetResponse which causes all the states to be set to 0 for
287 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700288 SetupPayloadStateWith2Urls(
289 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700290 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800291
292 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700293 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296
297 // Verify that on the next error, the URL index wraps around to 0.
298 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300
301 // Verify that on the next error, it again advances to 1.
302 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700303 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700304
305 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800306 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800307}
308
Amin Hassani5ff76a22020-11-05 12:55:38 -0800309TEST_F(PayloadStateTest, NewResponseResetsPayloadState) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800310 OmahaResponse response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800311 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800312
Amin Hassani538bd592020-11-04 20:46:08 -0800313 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800314
315 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700316 SetupPayloadStateWith2Urls(
317 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700318 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319
320 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700321 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700323 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800324 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325
326 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700327 SetupPayloadStateWith2Urls(
328 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700329 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330
Alex Deymob33b0f02013-08-08 21:10:02 -0700331 // Fake an error again.
332 payload_state.UpdateFailed(error);
333 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800334 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700335
Alex Deymob33b0f02013-08-08 21:10:02 -0700336 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700337 SetupPayloadStateWith2Urls(
338 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700339 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
340
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800341 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700342 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800343 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
344 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
345 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700346 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800347 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700348 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800349 EXPECT_EQ(
350 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
351 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700352 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800353}
354
Amin Hassani5ff76a22020-11-05 12:55:38 -0800355TEST_F(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800356 OmahaResponse response;
357 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700358 int progress_bytes = 100;
Amin Hassani90e9f192020-11-18 14:20:56 -0800359 FakeSystemState::Get()->set_prefs(nullptr);
360 auto* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800361
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700362 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700363 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800364 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700365 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800366 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700367 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800368 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800369
Alex Deymo820cc702013-06-28 15:43:46 -0700370 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800371 .Times(AtLeast(2));
Alex Deymo820cc702013-06-28 15:43:46 -0700372 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800373 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700374 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800375 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700376
Jay Srinivasan19409b72013-04-12 19:23:36 -0700377 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800378
Jay Srinivasan19409b72013-04-12 19:23:36 -0700379 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
380 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800381
Jay Srinivasan19409b72013-04-12 19:23:36 -0700382 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800383 .Times(AtLeast(7));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700384 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800385 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700386 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800387 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800388
Jay Srinivasan19409b72013-04-12 19:23:36 -0700389 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800390 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700391 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800392 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700393
Jay Srinivasan19409b72013-04-12 19:23:36 -0700394 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700395 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800396 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
397 .Times(AtLeast(1));
398 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
399 .Times(AtLeast(1));
400 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
401 .Times(AtLeast(1));
402 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
403 .Times(AtLeast(1));
404 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700405
Amin Hassani538bd592020-11-04 20:46:08 -0800406 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800407
Sen Jiangcdd52062017-05-18 15:33:10 -0700408 SetupPayloadStateWith2Urls(
409 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700410 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800411
412 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700413 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800414 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700415 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700416 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800417 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
418 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800419
420 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700421 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800422 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700423 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700424 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800425 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
426 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800427
428 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700429 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800430 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700431 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700432 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800433 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
434 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800435
436 // This should advance the URL index as we've reached the
437 // max failure count and reset the failure count for the new URL index.
438 // This should also wrap around the URL index and thus cause the payload
439 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700440 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800441 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700442 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700443 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800444 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
445 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800446 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800447
448 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700449 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800450 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700451 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700452 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800453 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
454 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800455 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800456
457 // This should advance the URL index and payload attempt number due to
458 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700459 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800460 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700461 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700462 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800463 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
464 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800465 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800466
467 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700468 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700469 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800470 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700471 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700472 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800473 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
474 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800475 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800476
477 // And that failure count should be reset when we download some bytes
478 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700479 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800480 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700481 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700482 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800483 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
484 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800485 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800486
487 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700488 SetupPayloadStateWith2Urls(
489 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700490 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800491
492 // Make sure the url index was reset to 0 because of the new response.
493 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700494 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700495 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800496 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
497 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800498 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800499}
500
Amin Hassani5ff76a22020-11-05 12:55:38 -0800501TEST_F(PayloadStateTest,
502 PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800503 OmahaResponse response;
504 PayloadState payload_state;
Amin Hassani90e9f192020-11-18 14:20:56 -0800505 FakeSystemState::Get()->set_prefs(nullptr);
506 auto* prefs = FakeSystemState::Get()->mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800507
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700508 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700509 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800510 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700511 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800512 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800513
Alex Deymo820cc702013-06-28 15:43:46 -0700514 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800515 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700516 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800517 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700518
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800519 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800520
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800521 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700522 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800523 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800524
Amin Hassani538bd592020-11-04 20:46:08 -0800525 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800526
Sen Jiangcdd52062017-05-18 15:33:10 -0700527 SetupPayloadStateWith2Urls(
528 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800529
530 // This should just advance the payload attempt number;
531 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700532 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800533 payload_state.DownloadComplete();
534 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700535 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
536 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800537 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
538 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700539}
540
Amin Hassani5ff76a22020-11-05 12:55:38 -0800541TEST_F(PayloadStateTest,
542 PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
Alex Deymo820cc702013-06-28 15:43:46 -0700543 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700544 PayloadState payload_state;
Amin Hassani90e9f192020-11-18 14:20:56 -0800545 FakeSystemState::Get()->set_prefs(nullptr);
546 auto* prefs = FakeSystemState::Get()->mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700547
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700548 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700549 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800550 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700551 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800552 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700553
554 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
555 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800556 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700557
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800558 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(1);
Alex Deymo820cc702013-06-28 15:43:46 -0700559
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800560 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700561 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800562 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700563
Amin Hassani538bd592020-11-04 20:46:08 -0800564 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo820cc702013-06-28 15:43:46 -0700565
Sen Jiangcdd52062017-05-18 15:33:10 -0700566 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700567
568 // This should just advance the payload attempt number;
569 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
570 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
571 payload_state.DownloadComplete();
572 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
573 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700574 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800575 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
576 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800577}
578
Amin Hassani5ff76a22020-11-05 12:55:38 -0800579TEST_F(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800580 OmahaResponse response;
581 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800582
Amin Hassani538bd592020-11-04 20:46:08 -0800583 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700584 SetupPayloadStateWith2Urls(
585 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800586
587 // Generate enough events to advance URL index, failure count and
588 // payload attempt number all to 1.
589 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700590 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
591 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800592 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700593 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700594 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800595 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
596 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800597
598 // Now, simulate a corrupted url index on persisted store which gets
Amin Hassani90e9f192020-11-18 14:20:56 -0800599 // loaded when update_engine restarts.
600 FakeSystemState::Get()->set_prefs(nullptr);
601 auto* prefs = FakeSystemState::Get()->mock_prefs();
602 EXPECT_CALL(*prefs, Exists(_)).WillRepeatedly(Return(true));
603 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(AtLeast(1));
604 EXPECT_CALL(*prefs, GetInt64(kPrefsPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800605 .Times(AtLeast(1));
Amin Hassani90e9f192020-11-18 14:20:56 -0800606 EXPECT_CALL(*prefs, GetInt64(kPrefsFullPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800607 .Times(AtLeast(1));
Amin Hassani90e9f192020-11-18 14:20:56 -0800608 EXPECT_CALL(*prefs, GetInt64(kPrefsCurrentUrlIndex, _))
Ben Chan672c1f52017-10-23 15:41:39 -0700609 .WillRepeatedly(DoAll(SetArgPointee<1>(2), Return(true)));
Amin Hassani90e9f192020-11-18 14:20:56 -0800610 EXPECT_CALL(*prefs, GetInt64(kPrefsCurrentUrlFailureCount, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800611 .Times(AtLeast(1));
Amin Hassani90e9f192020-11-18 14:20:56 -0800612 EXPECT_CALL(*prefs, GetInt64(kPrefsUrlSwitchCount, _)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800613
614 // Note: This will be a different payload object, but the response should
615 // have the same hash as before so as to not trivially reset because the
616 // response was different. We want to specifically test that even if the
617 // response is same, we should reset the state if we find it corrupted.
Amin Hassani538bd592020-11-04 20:46:08 -0800618 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700619 SetupPayloadStateWith2Urls(
620 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800621
622 // Make sure all counters get reset to 0 because of the corrupted URL index
623 // we supplied above.
624 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700625 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700626 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800627 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
628 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800629}
Jay Srinivasan08262882012-12-28 19:29:43 -0800630
Amin Hassani5ff76a22020-11-05 12:55:38 -0800631TEST_F(PayloadStateTest, NoBackoffInteractiveChecks) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700632 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700633 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800634 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -0700635 params.Init("", "", {.interactive = true});
Amin Hassani538bd592020-11-04 20:46:08 -0800636 FakeSystemState::Get()->set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700637
Amin Hassani538bd592020-11-04 20:46:08 -0800638 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700639 SetupPayloadStateWith2Urls(
640 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700641
642 // Simulate two failures (enough to cause payload backoff) and check
643 // again that we're ready to re-download without any backoff as this is
644 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700645 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
646 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700647 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
648 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
649 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
650 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
651}
652
Amin Hassani5ff76a22020-11-05 12:55:38 -0800653TEST_F(PayloadStateTest, NoBackoffForP2PUpdates) {
Chris Sosa20f005c2013-09-05 13:53:08 -0700654 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700655 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800656 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -0700657 params.Init("", "", {});
Amin Hassani538bd592020-11-04 20:46:08 -0800658 FakeSystemState::Get()->set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700659
Amin Hassani538bd592020-11-04 20:46:08 -0800660 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700661 SetupPayloadStateWith2Urls(
662 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700663
664 // Simulate two failures (enough to cause payload backoff) and check
665 // again that we're ready to re-download without any backoff as this is
666 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700667 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
668 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700669 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
670 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
671 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
672 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700673 payload_state.SetUsingP2PForDownloading(true);
674 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700675 // Should not backoff for p2p updates.
676 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
677
Gilad Arnold74b5f552014-10-07 08:17:16 -0700678 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700679 // No actual p2p update if no url is provided.
680 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
681}
682
Amin Hassani5ff76a22020-11-05 12:55:38 -0800683TEST_F(PayloadStateTest, NoBackoffForDeltaPayloads) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800684 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800685 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800686
Amin Hassani538bd592020-11-04 20:46:08 -0800687 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700688 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800689
690 // Simulate a successful download and see that we're ready to download
691 // again without any backoff as this is a delta payload.
692 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700693 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
694 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800695 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
696
697 // Simulate two failures (enough to cause payload backoff) and check
698 // again that we're ready to re-download without any backoff as this is
699 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700700 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
701 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700702 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700703 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
704 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800705 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
706}
707
708static void CheckPayloadBackoffState(PayloadState* payload_state,
709 int expected_attempt_number,
710 TimeDelta expected_days) {
711 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700712 EXPECT_EQ(expected_attempt_number,
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800713 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800714 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
715 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
716 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
717 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
718 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
719 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
720 EXPECT_LT(expected_min_time.ToInternalValue(),
721 backoff_expiry_time.ToInternalValue());
722 EXPECT_GT(expected_max_time.ToInternalValue(),
723 backoff_expiry_time.ToInternalValue());
724}
725
Amin Hassani5ff76a22020-11-05 12:55:38 -0800726TEST_F(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800727 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800728 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800729
Amin Hassani538bd592020-11-04 20:46:08 -0800730 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700731 SetupPayloadStateWith2Urls(
732 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800733
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800734 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
735 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
736 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
737 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
738 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
739 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
740 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
741 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
742 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
743 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
Jay Srinivasan08262882012-12-28 19:29:43 -0800744}
745
Amin Hassani5ff76a22020-11-05 12:55:38 -0800746TEST_F(PayloadStateTest, BackoffLogicCanBeDisabled) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800747 OmahaResponse response;
748 response.disable_payload_backoff = true;
749 PayloadState payload_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800750
Amin Hassani538bd592020-11-04 20:46:08 -0800751 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700752 SetupPayloadStateWith2Urls(
753 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800754
755 // Simulate a successful download and see that we are ready to download
756 // again without any backoff.
757 payload_state.DownloadComplete();
758 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700759 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800760 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
761
762 // Test again, this time by simulating two errors that would cause
763 // the payload attempt number to increment due to wrap around. And
764 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700765 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
766 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800767 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700768 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800769 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
770}
771
Amin Hassani5ff76a22020-11-05 12:55:38 -0800772TEST_F(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700773 OmahaResponse response;
774 response.disable_payload_backoff = true;
775 PayloadState payload_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800776 uint64_t https_total = 0;
777 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700778
Amin Hassani538bd592020-11-04 20:46:08 -0800779 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700780 SetupPayloadStateWith2Urls(
781 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700782 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700783
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700784 // Simulate a previous attempt with in order to set an initial non-zero value
785 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800786 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700787 http_total += prev_chunk;
788 payload_state.DownloadProgress(prev_chunk);
789
790 // Ensure that the initial values for HTTP reflect this attempt.
791 EXPECT_EQ(prev_chunk,
792 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
793 EXPECT_EQ(http_total,
794 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
795
796 // Change the response hash so as to simulate a new response which will
797 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700798 SetupPayloadStateWith2Urls(
799 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700800 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700801
802 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800803 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700804 http_total += first_chunk;
805 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700806 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700807 EXPECT_EQ(first_chunk,
808 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
809 EXPECT_EQ(http_total,
810 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800811 EXPECT_EQ(
812 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700813 EXPECT_EQ(https_total,
814 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
815
816 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700817 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700818 payload_state.UpdateFailed(error);
819
Jay Srinivasan53173b92013-05-17 17:13:01 -0700820 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800821 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700822 https_total += second_chunk;
823 payload_state.DownloadProgress(second_chunk);
824 EXPECT_EQ(first_chunk,
825 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
826 EXPECT_EQ(http_total,
827 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800828 EXPECT_EQ(
829 second_chunk,
830 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700831 EXPECT_EQ(https_total,
832 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
833
834 // Simulate error to go back to http.
835 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800836 uint64_t third_chunk = 32345678;
837 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700838 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700839 payload_state.DownloadProgress(third_chunk);
840
841 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
842 EXPECT_EQ(http_chunk,
843 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700844 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700845 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800846 EXPECT_EQ(
847 second_chunk,
848 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700849 EXPECT_EQ(https_total,
850 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
851
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700852 // Simulate error (will cause URL switch), set p2p is to be used and
853 // then do 42MB worth of progress
854 payload_state.UpdateFailed(error);
855 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800856 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700857 payload_state.DownloadProgress(p2p_total);
858
859 EXPECT_EQ(p2p_total,
860 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
861
Amin Hassani538bd592020-11-04 20:46:08 -0800862 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700863 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -0700864 1, _, kPayloadTypeFull, _, _, 314, _, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700865
866 payload_state.UpdateSucceeded();
867
868 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800869 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700870 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800871 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700872 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800873 EXPECT_EQ(
874 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800875 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700876 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700877 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700878}
879
Amin Hassani5ff76a22020-11-05 12:55:38 -0800880TEST_F(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700881 OmahaResponse response;
882 PayloadState payload_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700883
Amin Hassani538bd592020-11-04 20:46:08 -0800884 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -0700885 SetupPayloadStateWith2Urls(
886 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700887
888 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800889 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700890 payload_state.DownloadProgress(num_bytes);
891
892 // Check that this was done via HTTP.
893 EXPECT_EQ(num_bytes,
894 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
895 EXPECT_EQ(num_bytes,
896 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
897
898 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700899 int64_t total_bytes[kNumDownloadSources] = {};
900 total_bytes[kDownloadSourceHttpServer] = num_bytes;
901
Amin Hassani538bd592020-11-04 20:46:08 -0800902 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700903 ReportSuccessfulUpdateMetrics(
Tianjie Xud4777a12017-10-24 14:54:18 -0700904 _,
905 _,
906 _,
907 _,
908 test_utils::DownloadSourceMatcher(total_bytes),
909 _,
910 _,
911 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700912 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700913 _))
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700914 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700915
916 payload_state.UpdateSucceeded();
917}
918
Amin Hassani5ff76a22020-11-05 12:55:38 -0800919TEST_F(PayloadStateTest, RestartingUpdateResetsMetrics) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700920 OmahaResponse response;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700921 PayloadState payload_state;
922
Amin Hassani538bd592020-11-04 20:46:08 -0800923 EXPECT_TRUE(payload_state.Initialize());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700924
925 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700926 SetupPayloadStateWith2Urls(
927 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700928
Alex Deymo80f70ff2016-02-10 16:08:11 -0800929 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700930 payload_state.DownloadProgress(num_bytes);
931 EXPECT_EQ(num_bytes,
932 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
933 EXPECT_EQ(num_bytes,
934 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800935 EXPECT_EQ(
936 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800937 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700938 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
939
940 payload_state.UpdateRestarted();
941 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800942 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700943 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
944 EXPECT_EQ(num_bytes,
945 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
946}
947
Amin Hassani5ff76a22020-11-05 12:55:38 -0800948TEST_F(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Amin Hassani90e9f192020-11-18 14:20:56 -0800949 FakeSystemState::Get()->set_prefs(nullptr);
950 auto* prefs = FakeSystemState::Get()->mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700951 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700952 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
953
Amin Hassani90e9f192020-11-18 14:20:56 -0800954 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800955 EXPECT_TRUE(payload_state.Initialize());
Chris Sosabe45bef2013-04-09 18:25:12 -0700956
957 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800958 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700959
Amin Hassani538bd592020-11-04 20:46:08 -0800960 FakeSystemState::Get()->set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700961 payload_state.UpdateResumed();
962 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800963 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700964
Amin Hassani538bd592020-11-04 20:46:08 -0800965 FakeSystemState::Get()->set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700966 payload_state.UpdateResumed();
967 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800968 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700969
970 // Restart the update again to verify we set the num of reboots back to 0.
971 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800972 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700973}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700974
Amin Hassani5ff76a22020-11-05 12:55:38 -0800975TEST_F(PayloadStateTest, RollbackHappened) {
Amin Hassani90e9f192020-11-18 14:20:56 -0800976 FakeSystemState::Get()->set_powerwash_safe_prefs(nullptr);
977 auto* mock_powerwash_safe_prefs =
Amin Hassani538bd592020-11-04 20:46:08 -0800978 FakeSystemState::Get()->mock_powerwash_safe_prefs();
Amin Hassani90e9f192020-11-18 14:20:56 -0800979 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800980 EXPECT_TRUE(payload_state.Initialize());
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200981
982 // Verify pre-conditions are good.
983 EXPECT_FALSE(payload_state.GetRollbackHappened());
984
985 // Set to true.
986 EXPECT_CALL(*mock_powerwash_safe_prefs,
987 SetBoolean(kPrefsRollbackHappened, true));
988 payload_state.SetRollbackHappened(true);
989 EXPECT_TRUE(payload_state.GetRollbackHappened());
990
991 // Set to false.
992 EXPECT_CALL(*mock_powerwash_safe_prefs, Delete(kPrefsRollbackHappened));
993 payload_state.SetRollbackHappened(false);
994 EXPECT_FALSE(payload_state.GetRollbackHappened());
995
996 // Let's verify we can reload it correctly.
997 EXPECT_CALL(*mock_powerwash_safe_prefs, GetBoolean(kPrefsRollbackHappened, _))
998 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
999 EXPECT_CALL(*mock_powerwash_safe_prefs,
1000 SetBoolean(kPrefsRollbackHappened, true));
1001 payload_state.LoadRollbackHappened();
1002 EXPECT_TRUE(payload_state.GetRollbackHappened());
1003}
1004
Amin Hassani5ff76a22020-11-05 12:55:38 -08001005TEST_F(PayloadStateTest, RollbackVersion) {
Amin Hassani90e9f192020-11-18 14:20:56 -08001006 FakeSystemState::Get()->set_powerwash_safe_prefs(nullptr);
1007 auto* mock_powerwash_safe_prefs =
Amin Hassani538bd592020-11-04 20:46:08 -08001008 FakeSystemState::Get()->mock_powerwash_safe_prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -07001009
Andrewcc6ab9f2020-06-25 07:41:40 -07001010 // Mock out the os version and make sure it's excluded correctly.
Chris Sosaaa18e162013-06-20 13:20:30 -07001011 string rollback_version = "2345.0.0";
Amin Hassani538bd592020-11-04 20:46:08 -08001012 OmahaRequestParams params;
Amin Hassanie8153632020-10-27 15:11:28 -07001013 params.Init(rollback_version, "", {});
Amin Hassani538bd592020-11-04 20:46:08 -08001014 FakeSystemState::Get()->set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001015
Amin Hassani90e9f192020-11-18 14:20:56 -08001016 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001017 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001018
1019 // Verify pre-conditions are good.
1020 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1021
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001022 EXPECT_CALL(*mock_powerwash_safe_prefs,
1023 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosaaa18e162013-06-20 13:20:30 -07001024 payload_state.Rollback();
1025
1026 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001027
1028 // Change it up a little and verify we load it correctly.
1029 rollback_version = "2345.0.1";
1030 // Let's verify we can reload it correctly.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001031 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(kPrefsRollbackVersion, _))
1032 .WillOnce(DoAll(SetArgPointee<1>(rollback_version), Return(true)));
1033 EXPECT_CALL(*mock_powerwash_safe_prefs,
1034 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001035 payload_state.LoadRollbackVersion();
1036 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001037
David Zeuthen96197df2014-04-16 12:22:39 -07001038 // Check that we report only UpdateEngine.Rollback.* metrics in
1039 // UpdateSucceeded().
Amin Hassani538bd592020-11-04 20:46:08 -08001040 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001041 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1042 .Times(1);
1043
David Zeuthenafed4a12014-04-09 15:28:44 -07001044 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001045}
1046
Amin Hassani5ff76a22020-11-05 12:55:38 -08001047TEST_F(PayloadStateTest, DurationsAreCorrect) {
David Zeuthenf413fe52013-04-22 14:04:39 -07001048 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001049 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001050
1051 // Set the clock to a well-known time - 1 second on the wall-clock
1052 // and 2 seconds on the monotonic clock
Amin Hassani0468a762020-11-17 23:53:48 -08001053 auto* fake_clock = FakeSystemState::Get()->fake_clock();
1054 fake_clock->SetWallclockTime(Time::FromInternalValue(1000000));
1055 fake_clock->SetMonotonicTime(Time::FromInternalValue(2000000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001056
Amin Hassani90e9f192020-11-18 14:20:56 -08001057 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001058 EXPECT_TRUE(payload_state.Initialize());
David Zeuthenf413fe52013-04-22 14:04:39 -07001059
1060 // Check that durations are correct for a successful update where
1061 // time has advanced 7 seconds on the wall clock and 4 seconds on
1062 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001063 SetupPayloadStateWith2Urls(
1064 "Hash8593", true, false, &payload_state, &response);
Amin Hassani0468a762020-11-17 23:53:48 -08001065 fake_clock->SetWallclockTime(Time::FromInternalValue(8000000));
1066 fake_clock->SetMonotonicTime(Time::FromInternalValue(6000000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001067 payload_state.UpdateSucceeded();
1068 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1069 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1070
1071 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001072 SetupPayloadStateWith2Urls(
1073 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001074 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1075 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1076
1077 // Advance time a bit (10 secs), simulate download progress and
1078 // check that durations are updated.
Amin Hassani0468a762020-11-17 23:53:48 -08001079 fake_clock->SetWallclockTime(Time::FromInternalValue(18000000));
1080 fake_clock->SetMonotonicTime(Time::FromInternalValue(16000000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001081 payload_state.DownloadProgress(10);
1082 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1083 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1084
1085 // Now simulate a reboot by resetting monotonic time (to 5000) and
1086 // creating a new PayloadState object and check that we load the
1087 // durations correctly (e.g. they are the same as before).
Amin Hassani0468a762020-11-17 23:53:48 -08001088 fake_clock->SetMonotonicTime(Time::FromInternalValue(5000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001089 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001090 EXPECT_TRUE(payload_state2.Initialize());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001091 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001092 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001093 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1094 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001095
1096 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1097 // and check that the durations are increased accordingly.
Amin Hassani0468a762020-11-17 23:53:48 -08001098 fake_clock->SetWallclockTime(Time::FromInternalValue(25000000));
1099 fake_clock->SetMonotonicTime(Time::FromInternalValue(6005000));
David Zeuthenf413fe52013-04-22 14:04:39 -07001100 payload_state2.UpdateSucceeded();
1101 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001102 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1103 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001104}
1105
Amin Hassani5ff76a22020-11-05 12:55:38 -08001106TEST_F(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
David Zeuthene4c58bf2013-06-18 17:26:50 -07001107 OmahaResponse response;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001108
1109 // Set the clock to a well-known time (t = 30 seconds).
Amin Hassani0468a762020-11-17 23:53:48 -08001110 auto* fake_clock = FakeSystemState::Get()->fake_clock();
1111 fake_clock->SetMonotonicTime(
Tianjie Xu90aaa102017-10-10 17:39:03 -07001112 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001113
Amin Hassani90e9f192020-11-18 14:20:56 -08001114 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001115 EXPECT_TRUE(payload_state.Initialize());
David Zeuthene4c58bf2013-06-18 17:26:50 -07001116
1117 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001118 SetupPayloadStateWith2Urls(
1119 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001120 payload_state.UpdateSucceeded();
1121
Amin Hassani90e9f192020-11-18 14:20:56 -08001122 auto* fake_prefs = FakeSystemState::Get()->fake_prefs();
David Zeuthene4c58bf2013-06-18 17:26:50 -07001123 // Check that the marker was written.
Amin Hassani90e9f192020-11-18 14:20:56 -08001124 EXPECT_TRUE(fake_prefs->Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001125
1126 // Now simulate a reboot and set the wallclock time to a later point
1127 // (t = 500 seconds). We do this by using a new PayloadState object
1128 // and checking that it emits the right UMA metric with the right
1129 // value.
Amin Hassani0468a762020-11-17 23:53:48 -08001130 fake_clock->SetMonotonicTime(
Tianjie Xu90aaa102017-10-10 17:39:03 -07001131 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001132 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001133 EXPECT_TRUE(payload_state2.Initialize());
David Zeuthene4c58bf2013-06-18 17:26:50 -07001134
1135 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Amin Hassani538bd592020-11-04 20:46:08 -08001136 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001137 ReportTimeToReboot(7));
Amin Hassani538bd592020-11-04 20:46:08 -08001138 FakeSystemState::Get()->set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001139
1140 payload_state2.UpdateEngineStarted();
1141
1142 // Check that the marker was nuked.
Amin Hassani90e9f192020-11-18 14:20:56 -08001143 EXPECT_FALSE(fake_prefs->Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001144}
1145
Amin Hassani5ff76a22020-11-05 12:55:38 -08001146TEST_F(PayloadStateTest, RestartAfterCrash) {
Alex Deymo569c4242013-07-24 12:01:01 -07001147 PayloadState payload_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001148 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Amin Hassani538bd592020-11-04 20:46:08 -08001149 FakeSystemState::Get()->set_metrics_reporter(&mock_metrics_reporter);
Amin Hassani90e9f192020-11-18 14:20:56 -08001150 FakeSystemState::Get()->set_prefs(nullptr);
1151 auto* prefs = FakeSystemState::Get()->mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001152
Amin Hassani538bd592020-11-04 20:46:08 -08001153 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo569c4242013-07-24 12:01:01 -07001154
David Zeuthen4e1d1492014-04-25 13:12:27 -07001155 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001156 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1157 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1158 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1159 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1160 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1161 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1162 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001163 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001164
Alex Deymo569c4242013-07-24 12:01:01 -07001165 // Simulate an update_engine restart without a reboot.
Amin Hassani538bd592020-11-04 20:46:08 -08001166 FakeSystemState::Get()->set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001167
1168 payload_state.UpdateEngineStarted();
1169}
1170
Amin Hassani5ff76a22020-11-05 12:55:38 -08001171TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001172 PayloadState payload_state;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001173
1174 // If there's no marker at startup, ensure we don't report a metric.
Amin Hassani538bd592020-11-04 20:46:08 -08001175 EXPECT_TRUE(payload_state.Initialize());
1176 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001177 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1178 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001179 payload_state.UpdateEngineStarted();
1180}
1181
Amin Hassani5ff76a22020-11-05 12:55:38 -08001182TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001183 // If we have a marker at startup, ensure it's reported and the
1184 // marker is then cleared.
Amin Hassani90e9f192020-11-18 14:20:56 -08001185 auto* fake_prefs = FakeSystemState::Get()->fake_prefs();
1186 fake_prefs->SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001187
Amin Hassani90e9f192020-11-18 14:20:56 -08001188 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001189 EXPECT_TRUE(payload_state.Initialize());
David Zeuthen4e1d1492014-04-25 13:12:27 -07001190
Amin Hassani538bd592020-11-04 20:46:08 -08001191 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001192 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1193 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001194 payload_state.UpdateEngineStarted();
1195
Amin Hassani90e9f192020-11-18 14:20:56 -08001196 EXPECT_FALSE(fake_prefs->Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001197}
1198
Amin Hassani5ff76a22020-11-05 12:55:38 -08001199TEST_F(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
David Zeuthen4e1d1492014-04-25 13:12:27 -07001200 // Make sure the marker is written and cleared during an attempt and
1201 // also that we DO NOT emit the metric (since the attempt didn't end
1202 // abnormally).
Amin Hassani90e9f192020-11-18 14:20:56 -08001203 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001204 EXPECT_TRUE(payload_state.Initialize());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001205 OmahaResponse response;
1206 response.packages.resize(1);
1207 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001208
Amin Hassani538bd592020-11-04 20:46:08 -08001209 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001210 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1211 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001212
Amin Hassani90e9f192020-11-18 14:20:56 -08001213 auto* fake_prefs = FakeSystemState::Get()->fake_prefs();
David Zeuthen4e1d1492014-04-25 13:12:27 -07001214 // Attempt not in progress, should be clear.
Amin Hassani90e9f192020-11-18 14:20:56 -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.
Amin Hassani90e9f192020-11-18 14:20:56 -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.
Amin Hassani90e9f192020-11-18 14:20:56 -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;
Amin Hassani538bd592020-11-04 20:46:08 -08001379 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001380 SetupPayloadStateWith2Urls(
1381 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001382
1383 // Simulate a successful download and update.
1384 payload_state.DownloadComplete();
1385 payload_state.UpdateSucceeded();
1386 payload_state.ExpectRebootInNewVersion("Version:12345678");
1387
1388 // Reboot into the same environment to get an UMA metric with a value of 1.
Amin Hassani538bd592020-11-04 20:46:08 -08001389 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001390 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001391 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001392 Mock::VerifyAndClearExpectations(
1393 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001394
1395 // Simulate a second update and reboot into the same environment, this should
1396 // send a value of 2.
1397 payload_state.ExpectRebootInNewVersion("Version:12345678");
1398
Amin Hassani538bd592020-11-04 20:46:08 -08001399 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001400 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001401 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001402 Mock::VerifyAndClearExpectations(
1403 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001404
1405 // Simulate a third failed reboot to new version, but this time for a
1406 // different payload. This should send a value of 1 this time.
1407 payload_state.ExpectRebootInNewVersion("Version:3141592");
Amin Hassani538bd592020-11-04 20:46:08 -08001408 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001409 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001410 payload_state.ReportFailedBootIfNeeded();
Amin Hassani538bd592020-11-04 20:46:08 -08001411 Mock::VerifyAndClearExpectations(
1412 FakeSystemState::Get()->mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001413}
1414
Amin Hassani5ff76a22020-11-05 12:55:38 -08001415TEST_F(PayloadStateTest, RebootAfterUpdateSucceed) {
Alex Deymo42432912013-07-12 20:21:15 -07001416 OmahaResponse response;
1417 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001418 FakeBootControl* fake_boot_control =
1419 FakeSystemState::Get()->fake_boot_control();
Alex Deymo763e7db2015-08-27 21:08:08 -07001420 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001421
Amin Hassani538bd592020-11-04 20:46:08 -08001422 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001423 SetupPayloadStateWith2Urls(
1424 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001425
1426 // Simulate a successful download and update.
1427 payload_state.DownloadComplete();
1428 payload_state.UpdateSucceeded();
1429 payload_state.ExpectRebootInNewVersion("Version:12345678");
1430
1431 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001432 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001433
Amin Hassani538bd592020-11-04 20:46:08 -08001434 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001435 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001436 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001437 payload_state.ReportFailedBootIfNeeded();
1438
Alex Deymo763e7db2015-08-27 21:08:08 -07001439 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001440 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001441 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001442 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001443}
1444
Amin Hassani5ff76a22020-11-05 12:55:38 -08001445TEST_F(PayloadStateTest, RebootAfterCanceledUpdate) {
Alex Deymo42432912013-07-12 20:21:15 -07001446 OmahaResponse response;
1447 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001448 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001449 SetupPayloadStateWith2Urls(
1450 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001451
1452 // Simulate a successful download and update.
1453 payload_state.DownloadComplete();
1454 payload_state.UpdateSucceeded();
1455 payload_state.ExpectRebootInNewVersion("Version:12345678");
1456
Amin Hassani538bd592020-11-04 20:46:08 -08001457 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001458 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001459 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001460
1461 // Cancel the applied update.
1462 payload_state.ResetUpdateStatus();
1463
1464 // Simulate a reboot.
1465 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001466}
1467
Amin Hassani5ff76a22020-11-05 12:55:38 -08001468TEST_F(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Alex Deymo42432912013-07-12 20:21:15 -07001469 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001470 EXPECT_TRUE(payload_state.Initialize());
Alex Deymo42432912013-07-12 20:21:15 -07001471
Amin Hassani538bd592020-11-04 20:46:08 -08001472 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001473 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001474 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001475
1476 // Simulate a reboot in this environment.
1477 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001478}
1479
Amin Hassani5ff76a22020-11-05 12:55:38 -08001480TEST_F(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
David Zeuthendcba8092013-08-06 12:16:35 -07001481 OmahaResponse response;
1482 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001483 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001484 SetupPayloadStateWith2Urls(
1485 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001486
1487 // Should allow exactly kMaxP2PAttempts...
1488 for (int n = 0; n < kMaxP2PAttempts; n++) {
1489 payload_state.P2PNewAttempt();
1490 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1491 }
1492 // ... but not more than that.
1493 payload_state.P2PNewAttempt();
1494 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001495}
1496
Amin Hassani5ff76a22020-11-05 12:55:38 -08001497TEST_F(PayloadStateTest, DisallowP2PAfterDeadline) {
David Zeuthendcba8092013-08-06 12:16:35 -07001498 OmahaResponse response;
1499 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001500 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001501 SetupPayloadStateWith2Urls(
1502 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001503
1504 // Set the clock to 1 second.
1505 Time epoch = Time::FromInternalValue(1000000);
Amin Hassani0468a762020-11-17 23:53:48 -08001506 auto* fake_clock = FakeSystemState::Get()->fake_clock();
1507 fake_clock->SetWallclockTime(epoch);
David Zeuthendcba8092013-08-06 12:16:35 -07001508
1509 // Do an attempt - this will set the timestamp.
1510 payload_state.P2PNewAttempt();
1511
1512 // Check that the timestamp equals what we just set.
1513 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1514
1515 // Time hasn't advanced - this should work.
1516 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1517
1518 // Set clock to half the deadline - this should work.
Amin Hassani0468a762020-11-17 23:53:48 -08001519 fake_clock->SetWallclockTime(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001520 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
David Zeuthendcba8092013-08-06 12:16:35 -07001521 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1522
1523 // Check that the first attempt timestamp hasn't changed just
1524 // because the wall-clock time changed.
1525 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1526
1527 // Set clock to _just_ before the deadline - this should work.
Amin Hassani0468a762020-11-17 23:53:48 -08001528 fake_clock->SetWallclockTime(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001529 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001530 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1531
1532 // Set clock to _just_ after the deadline - this should not work.
Amin Hassani0468a762020-11-17 23:53:48 -08001533 fake_clock->SetWallclockTime(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001534 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001535 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001536}
1537
Amin Hassani5ff76a22020-11-05 12:55:38 -08001538TEST_F(PayloadStateTest, P2PStateVarsInitialValue) {
David Zeuthendcba8092013-08-06 12:16:35 -07001539 OmahaResponse response;
1540 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001541 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001542 SetupPayloadStateWith2Urls(
1543 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001544
1545 Time null_time = Time();
1546 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1547 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001548}
1549
Amin Hassani5ff76a22020-11-05 12:55:38 -08001550TEST_F(PayloadStateTest, P2PStateVarsArePersisted) {
David Zeuthendcba8092013-08-06 12:16:35 -07001551 OmahaResponse response;
1552 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001553 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001554 SetupPayloadStateWith2Urls(
1555 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001556
1557 // Set the clock to something known.
1558 Time time = Time::FromInternalValue(12345);
Amin Hassani0468a762020-11-17 23:53:48 -08001559 FakeSystemState::Get()->fake_clock()->SetWallclockTime(time);
David Zeuthendcba8092013-08-06 12:16:35 -07001560
1561 // New p2p attempt - as a side-effect this will update the p2p state vars.
1562 payload_state.P2PNewAttempt();
1563 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1564 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1565
1566 // Now create a new PayloadState and check that it loads the state
1567 // vars correctly.
1568 PayloadState payload_state2;
Amin Hassani538bd592020-11-04 20:46:08 -08001569 EXPECT_TRUE(payload_state2.Initialize());
David Zeuthendcba8092013-08-06 12:16:35 -07001570 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1571 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001572}
1573
Amin Hassani5ff76a22020-11-05 12:55:38 -08001574TEST_F(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
David Zeuthendcba8092013-08-06 12:16:35 -07001575 OmahaResponse response;
1576 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001577 EXPECT_TRUE(payload_state.Initialize());
Sen Jiangcdd52062017-05-18 15:33:10 -07001578 SetupPayloadStateWith2Urls(
1579 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001580
1581 // Set the clock to something known.
1582 Time time = Time::FromInternalValue(12345);
Amin Hassani0468a762020-11-17 23:53:48 -08001583 FakeSystemState::Get()->fake_clock()->SetWallclockTime(time);
David Zeuthendcba8092013-08-06 12:16:35 -07001584
1585 // New p2p attempt - as a side-effect this will update the p2p state vars.
1586 payload_state.P2PNewAttempt();
1587 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1588 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1589
1590 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001591 SetupPayloadStateWith2Urls(
1592 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001593
1594 // ... and check that it clears the P2P state vars.
1595 Time null_time = Time();
1596 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1597 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001598}
1599
Amin Hassani5ff76a22020-11-05 12:55:38 -08001600TEST_F(PayloadStateTest, NextPayloadResetsUrlIndex) {
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001601 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001602 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001603 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001604 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001605 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001606
1607 OmahaResponse response;
1608 response.packages.push_back(
1609 {.payload_urls = {"http://test1a", "http://test2a"},
1610 .size = 123456789,
1611 .metadata_size = 58123,
1612 .metadata_signature = "msign",
1613 .hash = "hash"});
1614 response.packages.push_back({.payload_urls = {"http://test1b"},
1615 .size = 123456789,
1616 .metadata_size = 58123,
1617 .metadata_signature = "msign",
1618 .hash = "hash"});
1619 payload_state.SetResponse(response);
1620
1621 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1a");
1622 payload_state.IncrementUrlIndex();
1623 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test2a");
1624
1625 EXPECT_TRUE(payload_state.NextPayload());
1626 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1b");
1627}
1628
Amin Hassani5ff76a22020-11-05 12:55:38 -08001629TEST_F(PayloadStateTest, ExcludeNoopForNonExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001630 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001631 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001632 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001633 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001634 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001635
1636 OmahaResponse response;
1637 response.packages.push_back(
1638 {.payload_urls = {"http://test1a", "http://test2a"},
1639 .size = 123456789,
1640 .metadata_size = 58123,
1641 .metadata_signature = "msign",
1642 .hash = "hash",
1643 .can_exclude = false});
1644 payload_state.SetResponse(response);
1645
1646 EXPECT_CALL(mock_excluder, Exclude(_)).Times(0);
1647 payload_state.ExcludeCurrentPayload();
1648}
1649
Amin Hassani5ff76a22020-11-05 12:55:38 -08001650TEST_F(PayloadStateTest, ExcludeOnlyCanExcludables) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001651 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001652 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001653 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001654 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001655 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001656
1657 OmahaResponse response;
1658 response.packages.push_back(
1659 {.payload_urls = {"http://test1a", "http://test2a"},
1660 .size = 123456789,
1661 .metadata_size = 58123,
1662 .metadata_signature = "msign",
1663 .hash = "hash",
1664 .can_exclude = true});
1665 payload_state.SetResponse(response);
1666
1667 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1668 .WillOnce(Return(true));
1669 payload_state.ExcludeCurrentPayload();
1670}
1671
Amin Hassani5ff76a22020-11-05 12:55:38 -08001672TEST_F(PayloadStateTest, IncrementFailureExclusionTest) {
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001673 PayloadState payload_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001674 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001675 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001676 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001677 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001678
1679 OmahaResponse response;
1680 // Critical package.
1681 response.packages.push_back(
1682 {.payload_urls = {"http://crit-test1a", "http://crit-test2a"},
1683 .size = 123456789,
1684 .metadata_size = 58123,
1685 .metadata_signature = "msign",
1686 .hash = "hash",
1687 .can_exclude = false});
1688 // Non-critical package.
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 response.max_failure_count_per_url = 2;
1697 payload_state.SetResponse(response);
1698
1699 // Critical package won't be excluded.
1700 // Increment twice as failure count allowed per URL is set to 2.
1701 payload_state.IncrementFailureCount();
1702 payload_state.IncrementFailureCount();
1703
1704 EXPECT_TRUE(payload_state.NextPayload());
1705
1706 // First increment failure should not exclude.
1707 payload_state.IncrementFailureCount();
1708
1709 // Second increment failure should exclude.
1710 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1711 .WillOnce(Return(true));
1712 payload_state.IncrementFailureCount();
1713}
1714
Amin Hassani5ff76a22020-11-05 12:55:38 -08001715TEST_F(PayloadStateTest, HaltExclusionPostPayloadExhaustion) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001716 PayloadState payload_state;
Jae Hoon Kim76583912020-06-23 10:24:03 -07001717 StrictMock<MockExcluder> mock_excluder;
Amin Hassani538bd592020-11-04 20:46:08 -08001718 EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim76583912020-06-23 10:24:03 -07001719 .WillOnce(Return(&mock_excluder));
Amin Hassani538bd592020-11-04 20:46:08 -08001720 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim76583912020-06-23 10:24:03 -07001721
1722 OmahaResponse response;
1723 // Non-critical package.
1724 response.packages.push_back(
1725 {.payload_urls = {"http://test1a", "http://test2a"},
1726 .size = 123456789,
1727 .metadata_size = 58123,
1728 .metadata_signature = "msign",
1729 .hash = "hash",
1730 .can_exclude = true});
1731 payload_state.SetResponse(response);
1732
1733 // Exclusion should be called when excluded.
1734 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1735 .WillOnce(Return(true));
1736 payload_state.ExcludeCurrentPayload();
1737
1738 // No more paylods to go through.
1739 EXPECT_FALSE(payload_state.NextPayload());
1740
1741 // Exclusion should not be called as all |Payload|s are exhausted.
1742 payload_state.ExcludeCurrentPayload();
1743}
1744
Amin Hassani5ff76a22020-11-05 12:55:38 -08001745TEST_F(PayloadStateTest, NonInfinitePayloadIndexIncrement) {
Jae Hoon Kim76583912020-06-23 10:24:03 -07001746 PayloadState payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08001747 EXPECT_TRUE(payload_state.Initialize());
Jae Hoon Kim76583912020-06-23 10:24:03 -07001748
1749 payload_state.SetResponse({});
1750
1751 EXPECT_FALSE(payload_state.NextPayload());
1752 int payload_index = payload_state.payload_index_;
1753
1754 EXPECT_FALSE(payload_state.NextPayload());
1755 EXPECT_EQ(payload_index, payload_state.payload_index_);
1756}
1757
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001758} // namespace chromeos_update_engine