blob: bf9aed447353f84ce525f6dfae09be51c73e65ea [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
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/payload_state.h"
18
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080020#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070026#include "update_engine/common/excluder_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/fake_clock.h"
28#include "update_engine/common/fake_hardware.h"
29#include "update_engine/common/fake_prefs.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070030#include "update_engine/common/mock_excluder.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/common/mock_prefs.h"
32#include "update_engine/common/prefs.h"
33#include "update_engine/common/test_utils.h"
34#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070035#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070036#include "update_engine/metrics_reporter_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080037#include "update_engine/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038
Jay Srinivasan08262882012-12-28 19:29:43 -080039using base::Time;
40using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041using std::string;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080042using testing::_;
Alex Deymo42432912013-07-12 20:21:15 -070043using testing::AnyNumber;
44using testing::AtLeast;
45using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080047using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070048using testing::SetArgPointee;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070049using testing::StrictMock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050
51namespace chromeos_update_engine {
52
Jay Srinivasan19409b72013-04-12 19:23:36 -070053const char* kCurrentBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080054 "current-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070055const char* kTotalBytesDownloadedFromHttps =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080056 "total-bytes-downloaded-from-HttpsServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070057const char* kCurrentBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080058 "current-bytes-downloaded-from-HttpServer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070059const char* kTotalBytesDownloadedFromHttp =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080060 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070061const char* kCurrentBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080062 "current-bytes-downloaded-from-HttpPeer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070063const char* kTotalBytesDownloadedFromHttpPeer =
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070065
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080066static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070067 bool http_enabled,
Sen Jiangcdd52062017-05-18 15:33:10 -070068 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080069 PayloadState* payload_state,
70 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080071 response->packages.clear();
72 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
73 .size = 523456789,
74 .metadata_size = 558123,
75 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070076 .hash = hash,
77 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080078 response->max_failure_count_per_url = 3;
79 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080080 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070081
82 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080083 " NumURLs = 1\n"
84 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070085
86 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080087 " NumURLs = 2\n"
88 " Candidate Url0 = http://test\n"
89 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070090
Sen Jiang0affc2c2017-02-10 15:55:05 -080091 string expected_response_sign = base::StringPrintf(
92 "Payload 0:\n"
93 " Size = 523456789\n"
94 " Sha256 Hash = %s\n"
95 " Metadata Size = 558123\n"
96 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070097 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080098 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080099 "Max Failure Count Per Url = %d\n"
100 "Disable Payload Backoff = %d\n",
101 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -0700102 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800103 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800104 response->max_failure_count_per_url,
105 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800106 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800107}
108
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800109class PayloadStateTest : public ::testing::Test {};
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800110
111TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
112 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700113 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800114 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700115 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700116 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800117 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700118 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800119 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700120 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
121 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800123 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700124 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800125 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700126 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800127 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700128 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800129 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700130 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700132 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800133 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700134 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800135 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700136 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800137 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800138 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800139 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800140 "Max Failure Count Per Url = 0\n"
141 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700143 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800144 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
145 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700146 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800147}
148
149TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
150 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800151 response.packages.push_back({.payload_urls = {"https://single.url.test"},
152 .size = 123456789,
153 .metadata_size = 58123,
154 .metadata_signature = "msign",
155 .hash = "hash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700156 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800157 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700158 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700159 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700160 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800161 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
162 .Times(AtLeast(1));
163 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
165 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
166 .Times(AtLeast(1));
167 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
168 .Times(AtLeast(1));
169 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
170 .Times(AtLeast(1));
171 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
172 .Times(AtLeast(1));
173 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
174 .Times(AtLeast(1));
175 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
176 .Times(AtLeast(1));
177 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700179 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800181 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800182 string expected_response_sign =
183 "Payload 0:\n"
184 " Size = 123456789\n"
185 " Sha256 Hash = hash\n"
186 " Metadata Size = 58123\n"
187 " Metadata Signature = msign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700188 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800189 " NumURLs = 1\n"
190 " Candidate Url0 = https://single.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800191 "Max Failure Count Per Url = 0\n"
192 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800193 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700194 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800195 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
196 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700197 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198}
199
200TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
201 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800202 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
203 "https://multiple.url.test"},
204 .size = 523456789,
205 .metadata_size = 558123,
206 .metadata_signature = "metasign",
207 .hash = "rhash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700208 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800209 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700210 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700211 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700212 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800213 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
214 .Times(AtLeast(1));
215 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
216 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
222 .Times(AtLeast(1));
223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
224 .Times(AtLeast(1));
225 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700226
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800227 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700228 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800230 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800231 string expected_response_sign =
232 "Payload 0:\n"
233 " Size = 523456789\n"
234 " Sha256 Hash = rhash\n"
235 " Metadata Size = 558123\n"
236 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700237 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800238 " NumURLs = 2\n"
239 " Candidate Url0 = http://multiple.url.test\n"
240 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800241 "Max Failure Count Per Url = 0\n"
242 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800243 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700244 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800245 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
246 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700247 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800248}
249
250TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
251 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700252 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800253 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800254 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800255
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700256 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800257 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700258 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800259 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700260 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800261 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700262 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800263 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700264 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800265 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700266 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700267
Chris Sosabe45bef2013-04-09 18:25:12 -0700268 // Reboots will be set
269 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
270
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800271 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700272 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
273 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800274
275 // Failure count should be called each times url index is set, so that's
276 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700277 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800278 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800279
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700280 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800281
282 // This does a SetResponse which causes all the states to be set to 0 for
283 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700284 SetupPayloadStateWith2Urls(
285 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700286 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287
288 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700289 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700291 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292
293 // Verify that on the next error, the URL index wraps around to 0.
294 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296
297 // Verify that on the next error, it again advances to 1.
298 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700300
301 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800302 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800303}
304
305TEST(PayloadStateTest, NewResponseResetsPayloadState) {
306 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700307 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800311
312 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700313 SetupPayloadStateWith2Urls(
314 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700315 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316
317 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700318 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700320 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800321 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322
323 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700324 SetupPayloadStateWith2Urls(
325 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700326 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327
Alex Deymob33b0f02013-08-08 21:10:02 -0700328 // Fake an error again.
329 payload_state.UpdateFailed(error);
330 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800331 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700332
Alex Deymob33b0f02013-08-08 21:10:02 -0700333 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700334 SetupPayloadStateWith2Urls(
335 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700336 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
337
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800338 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700339 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800340 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
341 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
342 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700343 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800344 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700345 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800346 EXPECT_EQ(
347 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
348 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700349 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800350}
351
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800352TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
353 OmahaResponse response;
354 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700355 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700356 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800357 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800358
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700359 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700360 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800361 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700362 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800363 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700364 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800365 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800366
Alex Deymo820cc702013-06-28 15:43:46 -0700367 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800368 .Times(AtLeast(2));
Alex Deymo820cc702013-06-28 15:43:46 -0700369 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800370 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700371 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800372 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700373
Jay Srinivasan19409b72013-04-12 19:23:36 -0700374 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800375
Jay Srinivasan19409b72013-04-12 19:23:36 -0700376 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
377 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800378
Jay Srinivasan19409b72013-04-12 19:23:36 -0700379 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800380 .Times(AtLeast(7));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700381 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800382 .Times(AtLeast(2));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700383 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800384 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800385
Jay Srinivasan19409b72013-04-12 19:23:36 -0700386 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800387 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700388 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800389 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700390
Jay Srinivasan19409b72013-04-12 19:23:36 -0700391 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
Chris Sosabe45bef2013-04-09 18:25:12 -0700392 .Times(AtLeast(1));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800393 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
394 .Times(AtLeast(1));
395 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
396 .Times(AtLeast(1));
397 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
398 .Times(AtLeast(1));
399 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
400 .Times(AtLeast(1));
401 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700402
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700403 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800404
Sen Jiangcdd52062017-05-18 15:33:10 -0700405 SetupPayloadStateWith2Urls(
406 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700407 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800408
409 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700410 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800411 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700412 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700413 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800414 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
415 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800416
417 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700418 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800419 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700420 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700421 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800422 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
423 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800424
425 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700426 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800427 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700428 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700429 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800430 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
431 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800432
433 // This should advance the URL index as we've reached the
434 // max failure count and reset the failure count for the new URL index.
435 // This should also wrap around the URL index and thus cause the payload
436 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700437 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800438 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700439 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700440 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800441 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
442 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800443 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800444
445 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700446 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800447 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700448 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700449 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800450 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
451 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800452 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800453
454 // This should advance the URL index and payload attempt number due to
455 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700456 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800457 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700458 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700459 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800460 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
461 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800462 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800463
464 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700465 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700466 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800467 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700468 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700469 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800470 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
471 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800472 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800473
474 // And that failure count should be reset when we download some bytes
475 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700476 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800477 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700478 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700479 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800480 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
481 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800482 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800483
484 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700485 SetupPayloadStateWith2Urls(
486 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700487 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800488
489 // Make sure the url index was reset to 0 because of the new response.
490 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700491 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700492 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800493 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
494 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800495 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800496}
497
Alex Deymo820cc702013-06-28 15:43:46 -0700498TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800499 OmahaResponse response;
500 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700501 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800502 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800503
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700504 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700505 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800506 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700507 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800508 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800509
Alex Deymo820cc702013-06-28 15:43:46 -0700510 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800511 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700512 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800513 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700514
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800515 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800516
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800517 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700518 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800519 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800520
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700521 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800522
Sen Jiangcdd52062017-05-18 15:33:10 -0700523 SetupPayloadStateWith2Urls(
524 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800525
526 // This should just advance the payload attempt number;
527 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700528 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800529 payload_state.DownloadComplete();
530 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700531 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
532 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800533 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
534 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700535}
536
537TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
538 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700539 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700540 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800541 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700542
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700543 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700544 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800545 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700546 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800547 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700548
549 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
550 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800551 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700552
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800553 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(1);
Alex Deymo820cc702013-06-28 15:43:46 -0700554
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800555 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700556 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800557 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700558
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700559 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700560
Sen Jiangcdd52062017-05-18 15:33:10 -0700561 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700562
563 // This should just advance the payload attempt number;
564 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
565 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
566 payload_state.DownloadComplete();
567 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
568 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700569 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800570 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
571 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800572}
573
574TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
575 OmahaResponse response;
576 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700577 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800578
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700579 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700580 SetupPayloadStateWith2Urls(
581 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800582
583 // Generate enough events to advance URL index, failure count and
584 // payload attempt number all to 1.
585 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700586 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
587 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800588 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700589 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700590 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800591 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
592 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800593
594 // Now, simulate a corrupted url index on persisted store which gets
595 // loaded when update_engine restarts. Using a different prefs object
596 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800598 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700599 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700600 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700601 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800602 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700603 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800604 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700605 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
Ben Chan672c1f52017-10-23 15:41:39 -0700606 .WillRepeatedly(DoAll(SetArgPointee<1>(2), Return(true)));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700607 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800608 .Times(AtLeast(1));
609 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800610
611 // Note: This will be a different payload object, but the response should
612 // have the same hash as before so as to not trivially reset because the
613 // response was different. We want to specifically test that even if the
614 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700615 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Sen Jiangcdd52062017-05-18 15:33:10 -0700616 SetupPayloadStateWith2Urls(
617 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800618
619 // Make sure all counters get reset to 0 because of the corrupted URL index
620 // we supplied above.
621 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700622 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700623 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800624 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
625 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800626}
Jay Srinivasan08262882012-12-28 19:29:43 -0800627
Chris Sosa20f005c2013-09-05 13:53:08 -0700628TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
629 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700630 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700631 FakeSystemState fake_system_state;
632 OmahaRequestParams params(&fake_system_state);
Amin Hassanied37d682018-04-06 13:22:00 -0700633 params.Init("", "", true); // interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700634 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700635
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700636 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700637 SetupPayloadStateWith2Urls(
638 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700639
640 // Simulate two failures (enough to cause payload backoff) and check
641 // again that we're ready to re-download without any backoff as this is
642 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700643 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
644 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700645 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
646 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
647 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
648 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
649}
650
651TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
652 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700653 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700654 FakeSystemState fake_system_state;
655 OmahaRequestParams params(&fake_system_state);
Amin Hassanied37d682018-04-06 13:22:00 -0700656 params.Init("", "", false); // interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700657 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700658
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700660 SetupPayloadStateWith2Urls(
661 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700662
663 // Simulate two failures (enough to cause payload backoff) and check
664 // again that we're ready to re-download without any backoff as this is
665 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700666 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
667 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700668 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
669 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
670 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
671 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700672 payload_state.SetUsingP2PForDownloading(true);
673 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700674 // Should not backoff for p2p updates.
675 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
676
Gilad Arnold74b5f552014-10-07 08:17:16 -0700677 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700678 // No actual p2p update if no url is provided.
679 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
680}
681
Jay Srinivasan08262882012-12-28 19:29:43 -0800682TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
683 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800684 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700685 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800686
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700687 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
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
726TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
727 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800728 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700729 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800730
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700731 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700732 SetupPayloadStateWith2Urls(
733 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800734
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800735 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
736 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
737 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
738 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
739 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
740 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
741 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
742 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
743 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
744 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
Jay Srinivasan08262882012-12-28 19:29:43 -0800745}
746
747TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
748 OmahaResponse response;
749 response.disable_payload_backoff = true;
750 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700751 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800752
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700753 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700754 SetupPayloadStateWith2Urls(
755 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800756
757 // Simulate a successful download and see that we are ready to download
758 // again without any backoff.
759 payload_state.DownloadComplete();
760 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700761 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800762 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
763
764 // Test again, this time by simulating two errors that would cause
765 // the payload attempt number to increment due to wrap around. And
766 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700767 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
768 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800769 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700770 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800771 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
772}
773
Jay Srinivasan19409b72013-04-12 19:23:36 -0700774TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
775 OmahaResponse response;
776 response.disable_payload_backoff = true;
777 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700778 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800779 uint64_t https_total = 0;
780 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700781
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700782 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700783 SetupPayloadStateWith2Urls(
784 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700785 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700786
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700787 // Simulate a previous attempt with in order to set an initial non-zero value
788 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800789 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700790 http_total += prev_chunk;
791 payload_state.DownloadProgress(prev_chunk);
792
793 // Ensure that the initial values for HTTP reflect this attempt.
794 EXPECT_EQ(prev_chunk,
795 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
796 EXPECT_EQ(http_total,
797 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
798
799 // Change the response hash so as to simulate a new response which will
800 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700801 SetupPayloadStateWith2Urls(
802 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700803 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700804
805 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800806 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700807 http_total += first_chunk;
808 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700809 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700810 EXPECT_EQ(first_chunk,
811 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
812 EXPECT_EQ(http_total,
813 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800814 EXPECT_EQ(
815 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700816 EXPECT_EQ(https_total,
817 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
818
819 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700820 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700821 payload_state.UpdateFailed(error);
822
Jay Srinivasan53173b92013-05-17 17:13:01 -0700823 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800824 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700825 https_total += second_chunk;
826 payload_state.DownloadProgress(second_chunk);
827 EXPECT_EQ(first_chunk,
828 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
829 EXPECT_EQ(http_total,
830 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800831 EXPECT_EQ(
832 second_chunk,
833 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700834 EXPECT_EQ(https_total,
835 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
836
837 // Simulate error to go back to http.
838 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800839 uint64_t third_chunk = 32345678;
840 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700841 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700842 payload_state.DownloadProgress(third_chunk);
843
844 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
845 EXPECT_EQ(http_chunk,
846 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700847 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700848 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800849 EXPECT_EQ(
850 second_chunk,
851 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700852 EXPECT_EQ(https_total,
853 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
854
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700855 // Simulate error (will cause URL switch), set p2p is to be used and
856 // then do 42MB worth of progress
857 payload_state.UpdateFailed(error);
858 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800859 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700860 payload_state.DownloadProgress(p2p_total);
861
862 EXPECT_EQ(p2p_total,
863 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
864
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700865 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
866 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -0700867 1, _, kPayloadTypeFull, _, _, 314, _, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700868
869 payload_state.UpdateSucceeded();
870
871 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800872 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700873 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800874 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700875 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800876 EXPECT_EQ(
877 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800878 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700879 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700880 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700881}
882
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700883TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
884 OmahaResponse response;
885 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700886 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700887
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700888 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700889 SetupPayloadStateWith2Urls(
890 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700891
892 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800893 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700894 payload_state.DownloadProgress(num_bytes);
895
896 // Check that this was done via HTTP.
897 EXPECT_EQ(num_bytes,
898 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
899 EXPECT_EQ(num_bytes,
900 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
901
902 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700903 int64_t total_bytes[kNumDownloadSources] = {};
904 total_bytes[kDownloadSourceHttpServer] = num_bytes;
905
906 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
907 ReportSuccessfulUpdateMetrics(
Tianjie Xud4777a12017-10-24 14:54:18 -0700908 _,
909 _,
910 _,
911 _,
912 test_utils::DownloadSourceMatcher(total_bytes),
913 _,
914 _,
915 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700916 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700917 _))
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700918 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700919
920 payload_state.UpdateSucceeded();
921}
922
Jay Srinivasan19409b72013-04-12 19:23:36 -0700923TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
924 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700925 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700926 PayloadState payload_state;
927
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700928 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700929
930 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700931 SetupPayloadStateWith2Urls(
932 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700933
Alex Deymo80f70ff2016-02-10 16:08:11 -0800934 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700935 payload_state.DownloadProgress(num_bytes);
936 EXPECT_EQ(num_bytes,
937 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
938 EXPECT_EQ(num_bytes,
939 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800940 EXPECT_EQ(
941 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800942 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700943 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
944
945 payload_state.UpdateRestarted();
946 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800947 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700948 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
949 EXPECT_EQ(num_bytes,
950 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
951}
952
Chris Sosabe45bef2013-04-09 18:25:12 -0700953TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700954 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700955 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700956
Alex Deymo8427b4a2014-11-05 14:00:32 -0800957 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700958 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700959 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
960
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700961 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700962
963 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800964 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700965
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700966 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700967 payload_state.UpdateResumed();
968 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800969 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700970
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700971 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700972 payload_state.UpdateResumed();
973 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800974 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700975
976 // Restart the update again to verify we set the num of reboots back to 0.
977 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800978 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700979}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700980
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200981TEST(PayloadStateTest, RollbackHappened) {
982 FakeSystemState fake_system_state;
983 PayloadState payload_state;
984
985 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
986 fake_system_state.mock_powerwash_safe_prefs();
987 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
988
989 // Verify pre-conditions are good.
990 EXPECT_FALSE(payload_state.GetRollbackHappened());
991
992 // Set to true.
993 EXPECT_CALL(*mock_powerwash_safe_prefs,
994 SetBoolean(kPrefsRollbackHappened, true));
995 payload_state.SetRollbackHappened(true);
996 EXPECT_TRUE(payload_state.GetRollbackHappened());
997
998 // Set to false.
999 EXPECT_CALL(*mock_powerwash_safe_prefs, Delete(kPrefsRollbackHappened));
1000 payload_state.SetRollbackHappened(false);
1001 EXPECT_FALSE(payload_state.GetRollbackHappened());
1002
1003 // Let's verify we can reload it correctly.
1004 EXPECT_CALL(*mock_powerwash_safe_prefs, GetBoolean(kPrefsRollbackHappened, _))
1005 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
1006 EXPECT_CALL(*mock_powerwash_safe_prefs,
1007 SetBoolean(kPrefsRollbackHappened, true));
1008 payload_state.LoadRollbackHappened();
1009 EXPECT_TRUE(payload_state.GetRollbackHappened());
1010}
1011
Chris Sosaaa18e162013-06-20 13:20:30 -07001012TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001013 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -07001014 PayloadState payload_state;
1015
Alex Deymo8427b4a2014-11-05 14:00:32 -08001016 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001017 fake_system_state.mock_powerwash_safe_prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -07001018
1019 // Mock out the os version and make sure it's blacklisted correctly.
1020 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001021 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001022 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001023 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001024
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001025 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1026
1027 // Verify pre-conditions are good.
1028 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1029
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001030 EXPECT_CALL(*mock_powerwash_safe_prefs,
1031 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosaaa18e162013-06-20 13:20:30 -07001032 payload_state.Rollback();
1033
1034 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001035
1036 // Change it up a little and verify we load it correctly.
1037 rollback_version = "2345.0.1";
1038 // Let's verify we can reload it correctly.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001039 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(kPrefsRollbackVersion, _))
1040 .WillOnce(DoAll(SetArgPointee<1>(rollback_version), Return(true)));
1041 EXPECT_CALL(*mock_powerwash_safe_prefs,
1042 SetString(kPrefsRollbackVersion, rollback_version));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001043 payload_state.LoadRollbackVersion();
1044 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001045
David Zeuthen96197df2014-04-16 12:22:39 -07001046 // Check that we report only UpdateEngine.Rollback.* metrics in
1047 // UpdateSucceeded().
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001048 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1049 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1050 .Times(1);
1051
David Zeuthenafed4a12014-04-09 15:28:44 -07001052 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001053}
1054
David Zeuthenf413fe52013-04-22 14:04:39 -07001055TEST(PayloadStateTest, DurationsAreCorrect) {
1056 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001057 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001058 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001059 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001060 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001061 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001062
1063 // Set the clock to a well-known time - 1 second on the wall-clock
1064 // and 2 seconds on the monotonic clock
1065 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1066 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1067
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001068 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001069 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001070 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001071
1072 // Check that durations are correct for a successful update where
1073 // time has advanced 7 seconds on the wall clock and 4 seconds on
1074 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001075 SetupPayloadStateWith2Urls(
1076 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001077 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1078 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1079 payload_state.UpdateSucceeded();
1080 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1081 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1082
1083 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001084 SetupPayloadStateWith2Urls(
1085 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001086 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1087 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1088
1089 // Advance time a bit (10 secs), simulate download progress and
1090 // check that durations are updated.
1091 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1092 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1093 payload_state.DownloadProgress(10);
1094 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1095 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1096
1097 // Now simulate a reboot by resetting monotonic time (to 5000) and
1098 // creating a new PayloadState object and check that we load the
1099 // durations correctly (e.g. they are the same as before).
1100 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1101 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001102 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001103 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001104 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001105 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1106 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001107
1108 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1109 // and check that the durations are increased accordingly.
1110 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1111 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1112 payload_state2.UpdateSucceeded();
1113 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001114 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1115 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001116}
1117
David Zeuthene4c58bf2013-06-18 17:26:50 -07001118TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1119 OmahaResponse response;
1120 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001121 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001122 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001123 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001124
1125 // Set the clock to a well-known time (t = 30 seconds).
Tianjie Xu90aaa102017-10-10 17:39:03 -07001126 fake_clock.SetMonotonicTime(
1127 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001128
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001129 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001130 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001131 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001132
1133 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001134 SetupPayloadStateWith2Urls(
1135 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001136 payload_state.UpdateSucceeded();
1137
1138 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001139 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001140
1141 // Now simulate a reboot and set the wallclock time to a later point
1142 // (t = 500 seconds). We do this by using a new PayloadState object
1143 // and checking that it emits the right UMA metric with the right
1144 // value.
Tianjie Xu90aaa102017-10-10 17:39:03 -07001145 fake_clock.SetMonotonicTime(
1146 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001147 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001148 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001149
1150 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001151 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1152 ReportTimeToReboot(7));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001153 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001154
1155 payload_state2.UpdateEngineStarted();
1156
1157 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001158 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001159}
1160
Alex Deymo569c4242013-07-24 12:01:01 -07001161TEST(PayloadStateTest, RestartAfterCrash) {
1162 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001163 FakeSystemState fake_system_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001164 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001165 fake_system_state.set_metrics_reporter(&mock_metrics_reporter);
Alex Deymo8427b4a2014-11-05 14:00:32 -08001166 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001167
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001168 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001169
David Zeuthen4e1d1492014-04-25 13:12:27 -07001170 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001171 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1172 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1173 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1174 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1175 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1176 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1177 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001178 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001179
Alex Deymo569c4242013-07-24 12:01:01 -07001180 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001181 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001182
1183 payload_state.UpdateEngineStarted();
1184}
1185
David Zeuthen4e1d1492014-04-25 13:12:27 -07001186TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1187 PayloadState payload_state;
1188 FakeSystemState fake_system_state;
1189
1190 // If there's no marker at startup, ensure we don't report a metric.
1191 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001192 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1193 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1194 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001195 payload_state.UpdateEngineStarted();
1196}
1197
1198TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1199 PayloadState payload_state;
1200 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001201 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001202
1203 // If we have a marker at startup, ensure it's reported and the
1204 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001205 fake_system_state.set_prefs(&fake_prefs);
1206 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001207
1208 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1209
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001210 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1211 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1212 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001213 payload_state.UpdateEngineStarted();
1214
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001215 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001216}
1217
1218TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1219 PayloadState payload_state;
1220 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001221 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001222
1223 // Make sure the marker is written and cleared during an attempt and
1224 // also that we DO NOT emit the metric (since the attempt didn't end
1225 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001226 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001227 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001228 OmahaResponse response;
1229 response.packages.resize(1);
1230 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001231
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001232 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1233 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1234 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001235
1236 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001237 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001238
1239 payload_state.UpdateRestarted();
1240
1241 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001242 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001243
1244 payload_state.UpdateSucceeded();
1245
1246 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001247 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001248}
1249
Jay Srinivasan53173b92013-05-17 17:13:01 -07001250TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1251 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001252 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001253 PayloadState payload_state;
1254
Jay Srinivasan53173b92013-05-17 17:13:01 -07001255 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001256 fake_system_state.set_device_policy(&disable_http_policy);
1257 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001258
1259 // Test with no device policy. Should default to allowing http.
1260 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1261 .WillRepeatedly(Return(false));
1262
1263 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001264 SetupPayloadStateWith2Urls(
1265 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001266
1267 // Check that we use the HTTP URL since there is no value set for allowing
1268 // http.
1269 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1270
1271 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001272 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001273 .WillRepeatedly(DoAll(SetArgPointee<0>(false), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001274
Chris Sosaf7d80042013-08-22 16:45:17 -07001275 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001276 SetupPayloadStateWith2Urls(
1277 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001278
1279 // Check that we skip the HTTP URL and use only the HTTPS url.
1280 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1281
1282 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001283 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001284 payload_state.UpdateFailed(error);
1285
1286 // Check that we still skip the HTTP URL and use only the HTTPS url.
1287 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001288 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001289
1290 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001291 SetupPayloadStateWith2Urls(
1292 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001293
1294 // Check that we still skip the HTTP URL and use only the HTTPS url.
1295 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1296
1297 // Now, pretend that the HTTP policy is turned on. We want to make sure
1298 // the new policy is honored.
1299 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001300 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001301 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001302 .WillRepeatedly(DoAll(SetArgPointee<0>(true), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001303
1304 // Now, set the same response using the same hash
1305 // so that we can test that the state is reset not because of the
1306 // hash but because of the policy change which results in candidate url
1307 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001308 SetupPayloadStateWith2Urls(
1309 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001310
1311 // Check that we use the HTTP URL now and the failure count is reset.
1312 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001313 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001314
1315 // Fake a failure and see if we're moving over to the HTTPS url and update
1316 // the URL switch count properly.
1317 payload_state.UpdateFailed(error);
1318 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001319 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1320 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001321}
1322
Alex Deymo1c656c42013-06-28 11:02:14 -07001323TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1324 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001325 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001326 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001327
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001328 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001329 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001330
1331 // Simulate a successful download and update.
1332 payload_state.DownloadComplete();
Sen Jiang8712e962018-05-08 12:12:28 -07001333 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1334 ReportSuccessfulUpdateMetrics(
1335 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001336 payload_state.UpdateSucceeded();
1337
1338 // Mock the request to a request where the delta was disabled but Omaha sends
1339 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001340 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001341 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001342 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001343
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001344 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001345 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001346
1347 payload_state.DownloadComplete();
1348
Sen Jiang8712e962018-05-08 12:12:28 -07001349 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1350 ReportSuccessfulUpdateMetrics(
1351 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001352 payload_state.UpdateSucceeded();
1353}
1354
1355TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1356 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001357 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001358 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001359
Alex Deymo1c656c42013-06-28 11:02:14 -07001360 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001361 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001362 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001363 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001364
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001365 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1366 SetupPayloadStateWith2Urls(
1367 "Hash6437", true, false, &payload_state, &response);
1368
Alex Deymo1c656c42013-06-28 11:02:14 -07001369 // Simulate a successful download and update.
1370 payload_state.DownloadComplete();
1371
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001372 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1373 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -07001374 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001375 payload_state.UpdateSucceeded();
1376}
1377
1378TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1379 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001380 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001381 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001382
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001383 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001384 SetupPayloadStateWith2Urls(
1385 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001386
Alex Deymo820cc702013-06-28 15:43:46 -07001387 // Mock the request to a request where the delta is enabled, although the
1388 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001389 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001390 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001391 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001392
1393 // Simulate a successful download and update.
1394 payload_state.DownloadComplete();
1395
Sen Jiang8712e962018-05-08 12:12:28 -07001396 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1397 ReportSuccessfulUpdateMetrics(
1398 _, _, kPayloadTypeFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001399 payload_state.UpdateSucceeded();
1400}
1401
Alex Deymo42432912013-07-12 20:21:15 -07001402TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001403 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001404 OmahaResponse response;
1405 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001406 FakePrefs fake_prefs;
1407 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001408
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001409 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001410 SetupPayloadStateWith2Urls(
1411 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001412
1413 // Simulate a successful download and update.
1414 payload_state.DownloadComplete();
1415 payload_state.UpdateSucceeded();
1416 payload_state.ExpectRebootInNewVersion("Version:12345678");
1417
1418 // Reboot into the same environment to get an UMA metric with a value of 1.
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001419 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1420 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001421 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001422 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001423
1424 // Simulate a second update and reboot into the same environment, this should
1425 // send a value of 2.
1426 payload_state.ExpectRebootInNewVersion("Version:12345678");
1427
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001428 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1429 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001430 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001431 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001432
1433 // Simulate a third failed reboot to new version, but this time for a
1434 // different payload. This should send a value of 1 this time.
1435 payload_state.ExpectRebootInNewVersion("Version:3141592");
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001436 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1437 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001438 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001439 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001440}
1441
1442TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001443 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001444 OmahaResponse response;
1445 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001446 FakePrefs fake_prefs;
1447 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001448
Alex Deymo763e7db2015-08-27 21:08:08 -07001449 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1450 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001451
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001452 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001453 SetupPayloadStateWith2Urls(
1454 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001455
1456 // Simulate a successful download and update.
1457 payload_state.DownloadComplete();
1458 payload_state.UpdateSucceeded();
1459 payload_state.ExpectRebootInNewVersion("Version:12345678");
1460
1461 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001462 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001463
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001464 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1465 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001466 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001467 payload_state.ReportFailedBootIfNeeded();
1468
Alex Deymo763e7db2015-08-27 21:08:08 -07001469 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001470 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001471 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001472 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001473}
1474
1475TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001476 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001477 OmahaResponse response;
1478 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001479 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001480
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001481 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001482 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001483 SetupPayloadStateWith2Urls(
1484 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001485
1486 // Simulate a successful download and update.
1487 payload_state.DownloadComplete();
1488 payload_state.UpdateSucceeded();
1489 payload_state.ExpectRebootInNewVersion("Version:12345678");
1490
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001491 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1492 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001493 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001494
1495 // Cancel the applied update.
1496 payload_state.ResetUpdateStatus();
1497
1498 // Simulate a reboot.
1499 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001500}
1501
1502TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001503 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001504 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001505 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001506
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001507 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001508 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001509
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001510 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1511 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001512 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001513
1514 // Simulate a reboot in this environment.
1515 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001516}
1517
David Zeuthendcba8092013-08-06 12:16:35 -07001518TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1519 OmahaResponse response;
1520 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001521 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001522 FakePrefs fake_prefs;
1523 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001524
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001525 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001526 SetupPayloadStateWith2Urls(
1527 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001528
1529 // Should allow exactly kMaxP2PAttempts...
1530 for (int n = 0; n < kMaxP2PAttempts; n++) {
1531 payload_state.P2PNewAttempt();
1532 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1533 }
1534 // ... but not more than that.
1535 payload_state.P2PNewAttempt();
1536 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001537}
1538
1539TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1540 OmahaResponse response;
1541 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001542 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001543 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001544 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001545
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001546 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001547 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001548 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001549 SetupPayloadStateWith2Urls(
1550 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001551
1552 // Set the clock to 1 second.
1553 Time epoch = Time::FromInternalValue(1000000);
1554 fake_clock.SetWallclockTime(epoch);
1555
1556 // Do an attempt - this will set the timestamp.
1557 payload_state.P2PNewAttempt();
1558
1559 // Check that the timestamp equals what we just set.
1560 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1561
1562 // Time hasn't advanced - this should work.
1563 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1564
1565 // Set clock to half the deadline - this should work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001566 fake_clock.SetWallclockTime(
1567 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
David Zeuthendcba8092013-08-06 12:16:35 -07001568 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1569
1570 // Check that the first attempt timestamp hasn't changed just
1571 // because the wall-clock time changed.
1572 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1573
1574 // Set clock to _just_ before the deadline - this should work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001575 fake_clock.SetWallclockTime(
1576 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001577 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1578
1579 // Set clock to _just_ after the deadline - this should not work.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001580 fake_clock.SetWallclockTime(
1581 epoch + TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
David Zeuthendcba8092013-08-06 12:16:35 -07001582 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001583}
1584
1585TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1586 OmahaResponse response;
1587 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001588 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001589 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001590
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001591 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001592 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001593 SetupPayloadStateWith2Urls(
1594 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001595
1596 Time null_time = Time();
1597 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1598 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001599}
1600
1601TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1602 OmahaResponse response;
1603 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001604 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001605 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001606 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001607 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001608 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001609 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001610 SetupPayloadStateWith2Urls(
1611 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001612
1613 // Set the clock to something known.
1614 Time time = Time::FromInternalValue(12345);
1615 fake_clock.SetWallclockTime(time);
1616
1617 // New p2p attempt - as a side-effect this will update the p2p state vars.
1618 payload_state.P2PNewAttempt();
1619 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1620 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1621
1622 // Now create a new PayloadState and check that it loads the state
1623 // vars correctly.
1624 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001625 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001626 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1627 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001628}
1629
1630TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1631 OmahaResponse response;
1632 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001633 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001634 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001635 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001636 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001637 fake_system_state.set_prefs(&fake_prefs);
1638
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001639 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001640 SetupPayloadStateWith2Urls(
1641 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001642
1643 // Set the clock to something known.
1644 Time time = Time::FromInternalValue(12345);
1645 fake_clock.SetWallclockTime(time);
1646
1647 // New p2p attempt - as a side-effect this will update the p2p state vars.
1648 payload_state.P2PNewAttempt();
1649 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1650 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1651
1652 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001653 SetupPayloadStateWith2Urls(
1654 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001655
1656 // ... and check that it clears the P2P state vars.
1657 Time null_time = Time();
1658 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1659 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001660}
1661
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001662TEST(PayloadStateTest, NextPayloadResetsUrlIndex) {
1663 PayloadState payload_state;
1664 FakeSystemState fake_system_state;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001665 StrictMock<MockExcluder> mock_excluder;
1666 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1667 .WillOnce(Return(&mock_excluder));
Jae Hoon Kima3210e62020-05-07 11:32:44 -07001668 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1669
1670 OmahaResponse response;
1671 response.packages.push_back(
1672 {.payload_urls = {"http://test1a", "http://test2a"},
1673 .size = 123456789,
1674 .metadata_size = 58123,
1675 .metadata_signature = "msign",
1676 .hash = "hash"});
1677 response.packages.push_back({.payload_urls = {"http://test1b"},
1678 .size = 123456789,
1679 .metadata_size = 58123,
1680 .metadata_signature = "msign",
1681 .hash = "hash"});
1682 payload_state.SetResponse(response);
1683
1684 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1a");
1685 payload_state.IncrementUrlIndex();
1686 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test2a");
1687
1688 EXPECT_TRUE(payload_state.NextPayload());
1689 EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1b");
1690}
1691
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001692TEST(PayloadStateTest, ExcludeNoopForNonExcludables) {
1693 PayloadState payload_state;
1694 FakeSystemState fake_system_state;
1695 StrictMock<MockExcluder> mock_excluder;
1696 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1697 .WillOnce(Return(&mock_excluder));
1698 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1699
1700 OmahaResponse response;
1701 response.packages.push_back(
1702 {.payload_urls = {"http://test1a", "http://test2a"},
1703 .size = 123456789,
1704 .metadata_size = 58123,
1705 .metadata_signature = "msign",
1706 .hash = "hash",
1707 .can_exclude = false});
1708 payload_state.SetResponse(response);
1709
1710 EXPECT_CALL(mock_excluder, Exclude(_)).Times(0);
1711 payload_state.ExcludeCurrentPayload();
1712}
1713
1714TEST(PayloadStateTest, ExcludeOnlyCanExcludables) {
1715 PayloadState payload_state;
1716 FakeSystemState fake_system_state;
1717 StrictMock<MockExcluder> mock_excluder;
1718 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1719 .WillOnce(Return(&mock_excluder));
1720 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1721
1722 OmahaResponse response;
1723 response.packages.push_back(
1724 {.payload_urls = {"http://test1a", "http://test2a"},
1725 .size = 123456789,
1726 .metadata_size = 58123,
1727 .metadata_signature = "msign",
1728 .hash = "hash",
1729 .can_exclude = true});
1730 payload_state.SetResponse(response);
1731
1732 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1733 .WillOnce(Return(true));
1734 payload_state.ExcludeCurrentPayload();
1735}
1736
1737TEST(PayloadStateTest, IncrementFailureExclusionTest) {
1738 PayloadState payload_state;
1739 FakeSystemState fake_system_state;
1740 StrictMock<MockExcluder> mock_excluder;
1741 EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
1742 .WillOnce(Return(&mock_excluder));
1743 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1744
1745 OmahaResponse response;
1746 // Critical package.
1747 response.packages.push_back(
1748 {.payload_urls = {"http://crit-test1a", "http://crit-test2a"},
1749 .size = 123456789,
1750 .metadata_size = 58123,
1751 .metadata_signature = "msign",
1752 .hash = "hash",
1753 .can_exclude = false});
1754 // Non-critical package.
1755 response.packages.push_back(
1756 {.payload_urls = {"http://test1a", "http://test2a"},
1757 .size = 123456789,
1758 .metadata_size = 58123,
1759 .metadata_signature = "msign",
1760 .hash = "hash",
1761 .can_exclude = true});
1762 response.max_failure_count_per_url = 2;
1763 payload_state.SetResponse(response);
1764
1765 // Critical package won't be excluded.
1766 // Increment twice as failure count allowed per URL is set to 2.
1767 payload_state.IncrementFailureCount();
1768 payload_state.IncrementFailureCount();
1769
1770 EXPECT_TRUE(payload_state.NextPayload());
1771
1772 // First increment failure should not exclude.
1773 payload_state.IncrementFailureCount();
1774
1775 // Second increment failure should exclude.
1776 EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
1777 .WillOnce(Return(true));
1778 payload_state.IncrementFailureCount();
1779}
1780
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001781} // namespace chromeos_update_engine