blob: 637891b4b4ac6cef1835ad81453208b8448e3eac [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"
26#include "update_engine/common/fake_clock.h"
27#include "update_engine/common/fake_hardware.h"
28#include "update_engine/common/fake_prefs.h"
29#include "update_engine/common/mock_prefs.h"
30#include "update_engine/common/prefs.h"
31#include "update_engine/common/test_utils.h"
32#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070033#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070034#include "update_engine/metrics_reporter_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080035#include "update_engine/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036
Jay Srinivasan08262882012-12-28 19:29:43 -080037using base::Time;
38using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080039using std::string;
Alex Deymo42432912013-07-12 20:21:15 -070040using testing::AnyNumber;
41using testing::AtLeast;
42using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080043using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080044using testing::Return;
Ben Chan672c1f52017-10-23 15:41:39 -070045using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070046using testing::_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080047
48namespace chromeos_update_engine {
49
Jay Srinivasan19409b72013-04-12 19:23:36 -070050const char* kCurrentBytesDownloadedFromHttps =
51 "current-bytes-downloaded-from-HttpsServer";
52const char* kTotalBytesDownloadedFromHttps =
53 "total-bytes-downloaded-from-HttpsServer";
54const char* kCurrentBytesDownloadedFromHttp =
55 "current-bytes-downloaded-from-HttpServer";
56const char* kTotalBytesDownloadedFromHttp =
57 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070058const char* kCurrentBytesDownloadedFromHttpPeer =
59 "current-bytes-downloaded-from-HttpPeer";
60const char* kTotalBytesDownloadedFromHttpPeer =
61 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070062
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080063static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070064 bool http_enabled,
Sen Jiangcdd52062017-05-18 15:33:10 -070065 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080066 PayloadState* payload_state,
67 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080068 response->packages.clear();
69 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
70 .size = 523456789,
71 .metadata_size = 558123,
72 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070073 .hash = hash,
74 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080075 response->max_failure_count_per_url = 3;
76 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080077 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070078
79 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080080 " NumURLs = 1\n"
81 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070082
83 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080084 " NumURLs = 2\n"
85 " Candidate Url0 = http://test\n"
86 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070087
Sen Jiang0affc2c2017-02-10 15:55:05 -080088 string expected_response_sign = base::StringPrintf(
89 "Payload 0:\n"
90 " Size = 523456789\n"
91 " Sha256 Hash = %s\n"
92 " Metadata Size = 558123\n"
93 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070094 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080095 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080096 "Max Failure Count Per Url = %d\n"
97 "Disable Payload Backoff = %d\n",
98 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -070099 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800100 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800101 response->max_failure_count_per_url,
102 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800103 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800104}
105
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800106class PayloadStateTest : public ::testing::Test { };
107
108TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
109 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700110 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800111 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700112 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700113 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
114 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700115 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
116 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700117 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
118 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
119 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
120 .Times(AtLeast(1));
121 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
122 .Times(AtLeast(1));
123 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
124 .Times(AtLeast(1));
125 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
126 .Times(AtLeast(1));
127 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
128 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700129 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
130 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700131 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800132 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700133 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800134 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800135 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800136 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800137 "Max Failure Count Per Url = 0\n"
138 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800139 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700140 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800141 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
142 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700143 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800144}
145
146TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
147 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800148 response.packages.push_back({.payload_urls = {"https://single.url.test"},
149 .size = 123456789,
150 .metadata_size = 58123,
151 .metadata_signature = "msign",
152 .hash = "hash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700153 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800154 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700155 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700156 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
157 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700158 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
159 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700160 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
161 .Times(AtLeast(1));
162 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
163 .Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
165 .Times(AtLeast(1));
166 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
167 .Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
169 .Times(AtLeast(1));
170 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
171 .Times(AtLeast(1));
172 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
173 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700174 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
175 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700176 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
177 .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))
212 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700213 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
214 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700215 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
216 .Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
222 .Times(AtLeast(1));
223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
224 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700225 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
226 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700227 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
228 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700229
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700231 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800232 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800233 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800234 string expected_response_sign =
235 "Payload 0:\n"
236 " Size = 523456789\n"
237 " Sha256 Hash = rhash\n"
238 " Metadata Size = 558123\n"
239 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700240 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800241 " NumURLs = 2\n"
242 " Candidate Url0 = http://multiple.url.test\n"
243 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800244 "Max Failure Count Per Url = 0\n"
245 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800246 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700247 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800248 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
249 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700250 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800251}
252
253TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
254 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700255 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800256 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800257 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800258
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700259 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800260 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700261 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
262 .Times(AtLeast(1));
263 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
264 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700265 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
266 .Times(AtLeast(1));
267 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
268 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700269 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700270
Chris Sosabe45bef2013-04-09 18:25:12 -0700271 // Reboots will be set
272 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
273
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800274 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700275 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
276 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800277
278 // Failure count should be called each times url index is set, so that's
279 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700280 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
281 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800282
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700283 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800284
285 // This does a SetResponse which causes all the states to be set to 0 for
286 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700287 SetupPayloadStateWith2Urls(
288 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700289 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290
291 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700292 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800293 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700294 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295
296 // Verify that on the next error, the URL index wraps around to 0.
297 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700298 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800299
300 // Verify that on the next error, it again advances to 1.
301 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700302 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700303
304 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800305 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306}
307
308TEST(PayloadStateTest, NewResponseResetsPayloadState) {
309 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800311 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800312
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700313 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800314
315 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700316 SetupPayloadStateWith2Urls(
317 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700318 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319
320 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700321 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700323 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800324 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325
326 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700327 SetupPayloadStateWith2Urls(
328 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700329 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330
Alex Deymob33b0f02013-08-08 21:10:02 -0700331 // Fake an error again.
332 payload_state.UpdateFailed(error);
333 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800334 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700335
Alex Deymob33b0f02013-08-08 21:10:02 -0700336 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700337 SetupPayloadStateWith2Urls(
338 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700339 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
340
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800341 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700342 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800343 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
344 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
345 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700346 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800347 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700348 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800349 EXPECT_EQ(
350 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
351 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700352 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800353}
354
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800355TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
356 OmahaResponse response;
357 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700358 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700359 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800360 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800361
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700362 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700363 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
364 .Times(AtLeast(2));
365 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
366 .Times(AtLeast(1));
367 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
368 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800369
Alex Deymo820cc702013-06-28 15:43:46 -0700370 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
371 .Times(AtLeast(2));
372 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
373 .Times(AtLeast(1));
374 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
375 .Times(AtLeast(1));
376
Jay Srinivasan19409b72013-04-12 19:23:36 -0700377 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800378
Jay Srinivasan19409b72013-04-12 19:23:36 -0700379 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
380 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800381
Jay Srinivasan19409b72013-04-12 19:23:36 -0700382 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
383 .Times(AtLeast(7));
384 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
385 .Times(AtLeast(2));
386 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
387 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800388
Jay Srinivasan19409b72013-04-12 19:23:36 -0700389 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
390 .Times(AtLeast(1));
391 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
392 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700393
Jay Srinivasan19409b72013-04-12 19:23:36 -0700394 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
395 .Times(AtLeast(1));
396 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
397 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700398 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
399 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700400 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
401 .Times(AtLeast(1));
402 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
403 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700404 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
405 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700406
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700407 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800408
Sen Jiangcdd52062017-05-18 15:33:10 -0700409 SetupPayloadStateWith2Urls(
410 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700411 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800412
413 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700414 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800415 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700416 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700417 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800418 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
419 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800420
421 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700422 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800423 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700424 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700425 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800426 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
427 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800428
429 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700430 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800431 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700432 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700433 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800434 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
435 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800436
437 // This should advance the URL index as we've reached the
438 // max failure count and reset the failure count for the new URL index.
439 // This should also wrap around the URL index and thus cause the payload
440 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700441 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800442 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700443 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700444 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800445 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
446 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800447 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800448
449 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700450 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800451 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700452 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700453 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800454 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
455 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800456 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800457
458 // This should advance the URL index and payload attempt number due to
459 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700460 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800461 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700462 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700463 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800464 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
465 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800466 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800467
468 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700469 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700470 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800471 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700472 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700473 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800474 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
475 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800476 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800477
478 // And that failure count should be reset when we download some bytes
479 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700480 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800481 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700482 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700483 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800484 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
485 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800486 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800487
488 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700489 SetupPayloadStateWith2Urls(
490 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700491 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800492
493 // Make sure the url index was reset to 0 because of the new response.
494 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700495 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700496 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800497 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
498 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800499 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800500}
501
Alex Deymo820cc702013-06-28 15:43:46 -0700502TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800503 OmahaResponse response;
504 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700505 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800506 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800507
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700508 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700509 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
510 .Times(AtLeast(1));
511 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
512 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800513
Alex Deymo820cc702013-06-28 15:43:46 -0700514 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
515 .Times(AtLeast(1));
516 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
517 .Times(AtLeast(1));
518
Jay Srinivasan19409b72013-04-12 19:23:36 -0700519 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
520 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800521
Jay Srinivasan19409b72013-04-12 19:23:36 -0700522 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
523 .Times(AtLeast(1));
524 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
525 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800526
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700527 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800528
Sen Jiangcdd52062017-05-18 15:33:10 -0700529 SetupPayloadStateWith2Urls(
530 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800531
532 // This should just advance the payload attempt number;
533 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700534 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800535 payload_state.DownloadComplete();
536 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700537 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
538 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800539 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
540 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700541}
542
543TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
544 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700545 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700546 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800547 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700548
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700549 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700550 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
551 .Times(AtLeast(1));
552 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
553 .Times(AtLeast(1));
554
555 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
556 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
557 .Times(AtLeast(1));
558
559 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
560 .Times(1);
561
562 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
563 .Times(AtLeast(1));
564 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
565 .Times(AtLeast(1));
566
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700567 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700568
Sen Jiangcdd52062017-05-18 15:33:10 -0700569 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700570
571 // This should just advance the payload attempt number;
572 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
573 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
574 payload_state.DownloadComplete();
575 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
576 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700577 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800578 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
579 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800580}
581
582TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
583 OmahaResponse response;
584 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800586
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700587 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700588 SetupPayloadStateWith2Urls(
589 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800590
591 // Generate enough events to advance URL index, failure count and
592 // payload attempt number all to 1.
593 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700594 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
595 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800596 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700597 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700598 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800599 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
600 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800601
602 // Now, simulate a corrupted url index on persisted store which gets
603 // loaded when update_engine restarts. Using a different prefs object
604 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700605 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800606 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700607 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700608 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700609 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
610 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700611 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
612 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700613 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
Ben Chan672c1f52017-10-23 15:41:39 -0700614 .WillRepeatedly(DoAll(SetArgPointee<1>(2), Return(true)));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700615 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
616 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700617 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
618 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800619
620 // Note: This will be a different payload object, but the response should
621 // have the same hash as before so as to not trivially reset because the
622 // response was different. We want to specifically test that even if the
623 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700624 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Sen Jiangcdd52062017-05-18 15:33:10 -0700625 SetupPayloadStateWith2Urls(
626 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800627
628 // Make sure all counters get reset to 0 because of the corrupted URL index
629 // we supplied above.
630 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700631 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700632 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800633 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
634 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800635}
Jay Srinivasan08262882012-12-28 19:29:43 -0800636
Chris Sosa20f005c2013-09-05 13:53:08 -0700637TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
638 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700639 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700640 FakeSystemState fake_system_state;
641 OmahaRequestParams params(&fake_system_state);
Amin Hassanied37d682018-04-06 13:22:00 -0700642 params.Init("", "", true); // interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700643 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700644
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700646 SetupPayloadStateWith2Urls(
647 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700648
649 // Simulate two failures (enough to cause payload backoff) and check
650 // again that we're ready to re-download without any backoff as this is
651 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700652 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
653 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700654 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
655 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
656 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
657 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
658}
659
660TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
661 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700662 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700663 FakeSystemState fake_system_state;
664 OmahaRequestParams params(&fake_system_state);
Amin Hassanied37d682018-04-06 13:22:00 -0700665 params.Init("", "", false); // interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700666 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700667
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700668 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700669 SetupPayloadStateWith2Urls(
670 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700671
672 // Simulate two failures (enough to cause payload backoff) and check
673 // again that we're ready to re-download without any backoff as this is
674 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700675 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
676 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700677 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
678 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
679 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
680 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700681 payload_state.SetUsingP2PForDownloading(true);
682 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700683 // Should not backoff for p2p updates.
684 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
685
Gilad Arnold74b5f552014-10-07 08:17:16 -0700686 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700687 // No actual p2p update if no url is provided.
688 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
689}
690
Jay Srinivasan08262882012-12-28 19:29:43 -0800691TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
692 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800693 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700694 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800695
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700696 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700697 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800698
699 // Simulate a successful download and see that we're ready to download
700 // again without any backoff as this is a delta payload.
701 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700702 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
703 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800704 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
705
706 // Simulate two failures (enough to cause payload backoff) and check
707 // again that we're ready to re-download without any backoff as this is
708 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700709 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
710 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700711 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700712 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
713 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800714 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
715}
716
717static void CheckPayloadBackoffState(PayloadState* payload_state,
718 int expected_attempt_number,
719 TimeDelta expected_days) {
720 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700721 EXPECT_EQ(expected_attempt_number,
722 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800723 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
724 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
725 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
726 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
727 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
728 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
729 EXPECT_LT(expected_min_time.ToInternalValue(),
730 backoff_expiry_time.ToInternalValue());
731 EXPECT_GT(expected_max_time.ToInternalValue(),
732 backoff_expiry_time.ToInternalValue());
733}
734
735TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
736 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800737 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700738 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800739
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700740 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700741 SetupPayloadStateWith2Urls(
742 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800743
744 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
745 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
746 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
747 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
748 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
749 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
750 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
751 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
752 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
753 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
754}
755
756TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
757 OmahaResponse response;
758 response.disable_payload_backoff = true;
759 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700760 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800761
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700762 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700763 SetupPayloadStateWith2Urls(
764 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800765
766 // Simulate a successful download and see that we are ready to download
767 // again without any backoff.
768 payload_state.DownloadComplete();
769 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700770 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800771 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
772
773 // Test again, this time by simulating two errors that would cause
774 // the payload attempt number to increment due to wrap around. And
775 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700776 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
777 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800778 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700779 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800780 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
781}
782
Jay Srinivasan19409b72013-04-12 19:23:36 -0700783TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
784 OmahaResponse response;
785 response.disable_payload_backoff = true;
786 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700787 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800788 uint64_t https_total = 0;
789 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700790
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700791 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700792 SetupPayloadStateWith2Urls(
793 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700794 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700795
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700796 // Simulate a previous attempt with in order to set an initial non-zero value
797 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800798 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700799 http_total += prev_chunk;
800 payload_state.DownloadProgress(prev_chunk);
801
802 // Ensure that the initial values for HTTP reflect this attempt.
803 EXPECT_EQ(prev_chunk,
804 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
805 EXPECT_EQ(http_total,
806 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
807
808 // Change the response hash so as to simulate a new response which will
809 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700810 SetupPayloadStateWith2Urls(
811 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700812 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700813
814 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800815 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700816 http_total += first_chunk;
817 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700818 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700819 EXPECT_EQ(first_chunk,
820 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
821 EXPECT_EQ(http_total,
822 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800823 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700824 kDownloadSourceHttpsServer));
825 EXPECT_EQ(https_total,
826 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
827
828 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700829 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700830 payload_state.UpdateFailed(error);
831
Jay Srinivasan53173b92013-05-17 17:13:01 -0700832 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800833 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700834 https_total += second_chunk;
835 payload_state.DownloadProgress(second_chunk);
836 EXPECT_EQ(first_chunk,
837 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
838 EXPECT_EQ(http_total,
839 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
840 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
841 kDownloadSourceHttpsServer));
842 EXPECT_EQ(https_total,
843 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
844
845 // Simulate error to go back to http.
846 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800847 uint64_t third_chunk = 32345678;
848 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700849 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700850 payload_state.DownloadProgress(third_chunk);
851
852 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
853 EXPECT_EQ(http_chunk,
854 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700855 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700856 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
857 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
858 kDownloadSourceHttpsServer));
859 EXPECT_EQ(https_total,
860 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
861
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700862 // Simulate error (will cause URL switch), set p2p is to be used and
863 // then do 42MB worth of progress
864 payload_state.UpdateFailed(error);
865 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800866 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700867 payload_state.DownloadProgress(p2p_total);
868
869 EXPECT_EQ(p2p_total,
870 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
871
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700872 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
873 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -0700874 1, _, kPayloadTypeFull, _, _, 314, _, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700875
876 payload_state.UpdateSucceeded();
877
878 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800879 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700880 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800881 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700882 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800883 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700884 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800885 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700886 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700887 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700888}
889
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700890TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
891 OmahaResponse response;
892 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700893 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700894
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700895 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700896 SetupPayloadStateWith2Urls(
897 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700898
899 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800900 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700901 payload_state.DownloadProgress(num_bytes);
902
903 // Check that this was done via HTTP.
904 EXPECT_EQ(num_bytes,
905 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
906 EXPECT_EQ(num_bytes,
907 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
908
909 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700910 int64_t total_bytes[kNumDownloadSources] = {};
911 total_bytes[kDownloadSourceHttpServer] = num_bytes;
912
913 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
914 ReportSuccessfulUpdateMetrics(
Tianjie Xud4777a12017-10-24 14:54:18 -0700915 _,
916 _,
917 _,
918 _,
919 test_utils::DownloadSourceMatcher(total_bytes),
920 _,
921 _,
922 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700923 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700924 _))
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700925 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700926
927 payload_state.UpdateSucceeded();
928}
929
Jay Srinivasan19409b72013-04-12 19:23:36 -0700930TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
931 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700932 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700933 PayloadState payload_state;
934
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700935 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700936
937 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700938 SetupPayloadStateWith2Urls(
939 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700940
Alex Deymo80f70ff2016-02-10 16:08:11 -0800941 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700942 payload_state.DownloadProgress(num_bytes);
943 EXPECT_EQ(num_bytes,
944 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
945 EXPECT_EQ(num_bytes,
946 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800947 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700948 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800949 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700950 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
951
952 payload_state.UpdateRestarted();
953 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800954 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700955 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
956 EXPECT_EQ(num_bytes,
957 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
958}
959
Chris Sosabe45bef2013-04-09 18:25:12 -0700960TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700961 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700962 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700963
Alex Deymo8427b4a2014-11-05 14:00:32 -0800964 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700965 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700966 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
967
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700968 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700969
970 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800971 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700972
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700973 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700974 payload_state.UpdateResumed();
975 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800976 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700977
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700978 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700979 payload_state.UpdateResumed();
980 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800981 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700982
983 // Restart the update again to verify we set the num of reboots back to 0.
984 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800985 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700986}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700987
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200988TEST(PayloadStateTest, RollbackHappened) {
989 FakeSystemState fake_system_state;
990 PayloadState payload_state;
991
992 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
993 fake_system_state.mock_powerwash_safe_prefs();
994 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
995
996 // Verify pre-conditions are good.
997 EXPECT_FALSE(payload_state.GetRollbackHappened());
998
999 // Set to true.
1000 EXPECT_CALL(*mock_powerwash_safe_prefs,
1001 SetBoolean(kPrefsRollbackHappened, true));
1002 payload_state.SetRollbackHappened(true);
1003 EXPECT_TRUE(payload_state.GetRollbackHappened());
1004
1005 // Set to false.
1006 EXPECT_CALL(*mock_powerwash_safe_prefs, Delete(kPrefsRollbackHappened));
1007 payload_state.SetRollbackHappened(false);
1008 EXPECT_FALSE(payload_state.GetRollbackHappened());
1009
1010 // Let's verify we can reload it correctly.
1011 EXPECT_CALL(*mock_powerwash_safe_prefs, GetBoolean(kPrefsRollbackHappened, _))
1012 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
1013 EXPECT_CALL(*mock_powerwash_safe_prefs,
1014 SetBoolean(kPrefsRollbackHappened, true));
1015 payload_state.LoadRollbackHappened();
1016 EXPECT_TRUE(payload_state.GetRollbackHappened());
1017}
1018
Chris Sosaaa18e162013-06-20 13:20:30 -07001019TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001020 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -07001021 PayloadState payload_state;
1022
Alex Deymo8427b4a2014-11-05 14:00:32 -08001023 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001024 fake_system_state.mock_powerwash_safe_prefs();
1025 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -07001026
1027 // Verify pre-conditions are good.
1028 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1029
1030 // Mock out the os version and make sure it's blacklisted correctly.
1031 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001032 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001033 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001034 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001035
1036 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1037 rollback_version));
1038 payload_state.Rollback();
1039
1040 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001041
1042 // Change it up a little and verify we load it correctly.
1043 rollback_version = "2345.0.1";
1044 // Let's verify we can reload it correctly.
1045 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1046 kPrefsRollbackVersion, _)).WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -07001047 SetArgPointee<1>(rollback_version), Return(true)));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001048 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1049 rollback_version));
1050 payload_state.LoadRollbackVersion();
1051 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001052
David Zeuthen96197df2014-04-16 12:22:39 -07001053 // Check that we report only UpdateEngine.Rollback.* metrics in
1054 // UpdateSucceeded().
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001055 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1056 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1057 .Times(1);
1058
David Zeuthenafed4a12014-04-09 15:28:44 -07001059 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001060}
1061
David Zeuthenf413fe52013-04-22 14:04:39 -07001062TEST(PayloadStateTest, DurationsAreCorrect) {
1063 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001064 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001065 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001066 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001067 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001068 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001069
1070 // Set the clock to a well-known time - 1 second on the wall-clock
1071 // and 2 seconds on the monotonic clock
1072 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1073 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1074
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001075 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001076 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001077 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001078
1079 // Check that durations are correct for a successful update where
1080 // time has advanced 7 seconds on the wall clock and 4 seconds on
1081 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001082 SetupPayloadStateWith2Urls(
1083 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001084 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1085 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1086 payload_state.UpdateSucceeded();
1087 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1088 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1089
1090 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001091 SetupPayloadStateWith2Urls(
1092 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001093 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1094 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1095
1096 // Advance time a bit (10 secs), simulate download progress and
1097 // check that durations are updated.
1098 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1099 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1100 payload_state.DownloadProgress(10);
1101 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1102 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1103
1104 // Now simulate a reboot by resetting monotonic time (to 5000) and
1105 // creating a new PayloadState object and check that we load the
1106 // durations correctly (e.g. they are the same as before).
1107 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1108 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001109 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001110 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001111 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001112 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1113 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001114
1115 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1116 // and check that the durations are increased accordingly.
1117 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1118 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1119 payload_state2.UpdateSucceeded();
1120 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001121 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1122 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001123}
1124
David Zeuthene4c58bf2013-06-18 17:26:50 -07001125TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1126 OmahaResponse response;
1127 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001128 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001129 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001130 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001131
1132 // Set the clock to a well-known time (t = 30 seconds).
Tianjie Xu90aaa102017-10-10 17:39:03 -07001133 fake_clock.SetMonotonicTime(
1134 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001135
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001136 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001137 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001138 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001139
1140 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001141 SetupPayloadStateWith2Urls(
1142 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001143 payload_state.UpdateSucceeded();
1144
1145 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001146 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001147
1148 // Now simulate a reboot and set the wallclock time to a later point
1149 // (t = 500 seconds). We do this by using a new PayloadState object
1150 // and checking that it emits the right UMA metric with the right
1151 // value.
Tianjie Xu90aaa102017-10-10 17:39:03 -07001152 fake_clock.SetMonotonicTime(
1153 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001154 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001155 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001156
1157 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001158 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1159 ReportTimeToReboot(7));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001160 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001161
1162 payload_state2.UpdateEngineStarted();
1163
1164 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001165 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001166}
1167
Alex Deymo569c4242013-07-24 12:01:01 -07001168TEST(PayloadStateTest, RestartAfterCrash) {
1169 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001170 FakeSystemState fake_system_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001171 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001172 fake_system_state.set_metrics_reporter(&mock_metrics_reporter);
Alex Deymo8427b4a2014-11-05 14:00:32 -08001173 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001174
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001175 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001176
David Zeuthen4e1d1492014-04-25 13:12:27 -07001177 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001178 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1179 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1180 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1181 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1182 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1183 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1184 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001185 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001186
Alex Deymo569c4242013-07-24 12:01:01 -07001187 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001188 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001189
1190 payload_state.UpdateEngineStarted();
1191}
1192
David Zeuthen4e1d1492014-04-25 13:12:27 -07001193TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1194 PayloadState payload_state;
1195 FakeSystemState fake_system_state;
1196
1197 // If there's no marker at startup, ensure we don't report a metric.
1198 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001199 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1200 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1201 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001202 payload_state.UpdateEngineStarted();
1203}
1204
1205TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1206 PayloadState payload_state;
1207 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001208 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001209
1210 // If we have a marker at startup, ensure it's reported and the
1211 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001212 fake_system_state.set_prefs(&fake_prefs);
1213 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001214
1215 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1216
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001217 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1218 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1219 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001220 payload_state.UpdateEngineStarted();
1221
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001222 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001223}
1224
1225TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1226 PayloadState payload_state;
1227 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001228 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001229
1230 // Make sure the marker is written and cleared during an attempt and
1231 // also that we DO NOT emit the metric (since the attempt didn't end
1232 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001233 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001234 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001235 OmahaResponse response;
1236 response.packages.resize(1);
1237 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001238
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001239 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1240 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1241 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001242
1243 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001244 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001245
1246 payload_state.UpdateRestarted();
1247
1248 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001249 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001250
1251 payload_state.UpdateSucceeded();
1252
1253 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001254 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001255}
1256
Jay Srinivasan53173b92013-05-17 17:13:01 -07001257TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1258 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001259 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001260 PayloadState payload_state;
1261
Jay Srinivasan53173b92013-05-17 17:13:01 -07001262 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001263 fake_system_state.set_device_policy(&disable_http_policy);
1264 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001265
1266 // Test with no device policy. Should default to allowing http.
1267 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1268 .WillRepeatedly(Return(false));
1269
1270 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001271 SetupPayloadStateWith2Urls(
1272 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001273
1274 // Check that we use the HTTP URL since there is no value set for allowing
1275 // http.
1276 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1277
1278 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001279 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001280 .WillRepeatedly(DoAll(SetArgPointee<0>(false), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001281
Chris Sosaf7d80042013-08-22 16:45:17 -07001282 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001283 SetupPayloadStateWith2Urls(
1284 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001285
1286 // Check that we skip the HTTP URL and use only the HTTPS url.
1287 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1288
1289 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001290 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001291 payload_state.UpdateFailed(error);
1292
1293 // Check that we still skip the HTTP URL and use only the HTTPS url.
1294 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001295 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001296
1297 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001298 SetupPayloadStateWith2Urls(
1299 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001300
1301 // Check that we still skip the HTTP URL and use only the HTTPS url.
1302 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1303
1304 // Now, pretend that the HTTP policy is turned on. We want to make sure
1305 // the new policy is honored.
1306 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001307 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001308 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001309 .WillRepeatedly(DoAll(SetArgPointee<0>(true), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001310
1311 // Now, set the same response using the same hash
1312 // so that we can test that the state is reset not because of the
1313 // hash but because of the policy change which results in candidate url
1314 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001315 SetupPayloadStateWith2Urls(
1316 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001317
1318 // Check that we use the HTTP URL now and the failure count is reset.
1319 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001320 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001321
1322 // Fake a failure and see if we're moving over to the HTTPS url and update
1323 // the URL switch count properly.
1324 payload_state.UpdateFailed(error);
1325 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001326 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1327 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001328}
1329
Alex Deymo1c656c42013-06-28 11:02:14 -07001330TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1331 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001332 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001333 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001334
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001335 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001336 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001337
1338 // Simulate a successful download and update.
1339 payload_state.DownloadComplete();
Sen Jiang8712e962018-05-08 12:12:28 -07001340 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1341 ReportSuccessfulUpdateMetrics(
1342 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001343 payload_state.UpdateSucceeded();
1344
1345 // Mock the request to a request where the delta was disabled but Omaha sends
1346 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001347 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001348 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001349 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001350
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001351 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001352 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001353
1354 payload_state.DownloadComplete();
1355
Sen Jiang8712e962018-05-08 12:12:28 -07001356 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1357 ReportSuccessfulUpdateMetrics(
1358 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001359 payload_state.UpdateSucceeded();
1360}
1361
1362TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1363 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001364 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001365 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001366
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001367 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001368 SetupPayloadStateWith2Urls(
1369 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001370
1371 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001372 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001373 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001374 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001375
1376 // Simulate a successful download and update.
1377 payload_state.DownloadComplete();
1378
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001379 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1380 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -07001381 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001382 payload_state.UpdateSucceeded();
1383}
1384
1385TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1386 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001387 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001388 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001389
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001390 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001391 SetupPayloadStateWith2Urls(
1392 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001393
Alex Deymo820cc702013-06-28 15:43:46 -07001394 // Mock the request to a request where the delta is enabled, although the
1395 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001396 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001397 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001398 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001399
1400 // Simulate a successful download and update.
1401 payload_state.DownloadComplete();
1402
Sen Jiang8712e962018-05-08 12:12:28 -07001403 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1404 ReportSuccessfulUpdateMetrics(
1405 _, _, kPayloadTypeFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001406 payload_state.UpdateSucceeded();
1407}
1408
Alex Deymo42432912013-07-12 20:21:15 -07001409TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001410 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001411 OmahaResponse response;
1412 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001413 FakePrefs fake_prefs;
1414 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001415
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001416 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001417 SetupPayloadStateWith2Urls(
1418 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001419
1420 // Simulate a successful download and update.
1421 payload_state.DownloadComplete();
1422 payload_state.UpdateSucceeded();
1423 payload_state.ExpectRebootInNewVersion("Version:12345678");
1424
1425 // Reboot into the same environment to get an UMA metric with a value of 1.
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001426 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1427 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001428 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001429 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001430
1431 // Simulate a second update and reboot into the same environment, this should
1432 // send a value of 2.
1433 payload_state.ExpectRebootInNewVersion("Version:12345678");
1434
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001435 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1436 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001437 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001438 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001439
1440 // Simulate a third failed reboot to new version, but this time for a
1441 // different payload. This should send a value of 1 this time.
1442 payload_state.ExpectRebootInNewVersion("Version:3141592");
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001443 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1444 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001445 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001446 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001447}
1448
1449TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001450 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001451 OmahaResponse response;
1452 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001453 FakePrefs fake_prefs;
1454 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001455
Alex Deymo763e7db2015-08-27 21:08:08 -07001456 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1457 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001458
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001459 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001460 SetupPayloadStateWith2Urls(
1461 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001462
1463 // Simulate a successful download and update.
1464 payload_state.DownloadComplete();
1465 payload_state.UpdateSucceeded();
1466 payload_state.ExpectRebootInNewVersion("Version:12345678");
1467
1468 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001469 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001470
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001471 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1472 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001473 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001474 payload_state.ReportFailedBootIfNeeded();
1475
Alex Deymo763e7db2015-08-27 21:08:08 -07001476 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001477 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001478 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001479 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001480}
1481
1482TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001483 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001484 OmahaResponse response;
1485 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001486 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001487
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001488 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001489 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001490 SetupPayloadStateWith2Urls(
1491 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001492
1493 // Simulate a successful download and update.
1494 payload_state.DownloadComplete();
1495 payload_state.UpdateSucceeded();
1496 payload_state.ExpectRebootInNewVersion("Version:12345678");
1497
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001498 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1499 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001500 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001501
1502 // Cancel the applied update.
1503 payload_state.ResetUpdateStatus();
1504
1505 // Simulate a reboot.
1506 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001507}
1508
1509TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001510 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001511 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001512 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001513
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001514 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001515 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001516
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001517 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1518 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001519 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001520
1521 // Simulate a reboot in this environment.
1522 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001523}
1524
David Zeuthendcba8092013-08-06 12:16:35 -07001525TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1526 OmahaResponse response;
1527 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001528 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001529 FakePrefs fake_prefs;
1530 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001531
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001532 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001533 SetupPayloadStateWith2Urls(
1534 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001535
1536 // Should allow exactly kMaxP2PAttempts...
1537 for (int n = 0; n < kMaxP2PAttempts; n++) {
1538 payload_state.P2PNewAttempt();
1539 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1540 }
1541 // ... but not more than that.
1542 payload_state.P2PNewAttempt();
1543 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001544}
1545
1546TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1547 OmahaResponse response;
1548 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001549 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001550 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001551 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001552
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001553 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001554 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001555 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001556 SetupPayloadStateWith2Urls(
1557 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001558
1559 // Set the clock to 1 second.
1560 Time epoch = Time::FromInternalValue(1000000);
1561 fake_clock.SetWallclockTime(epoch);
1562
1563 // Do an attempt - this will set the timestamp.
1564 payload_state.P2PNewAttempt();
1565
1566 // Check that the timestamp equals what we just set.
1567 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1568
1569 // Time hasn't advanced - this should work.
1570 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1571
1572 // Set clock to half the deadline - this should work.
1573 fake_clock.SetWallclockTime(epoch +
1574 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1575 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1576
1577 // Check that the first attempt timestamp hasn't changed just
1578 // because the wall-clock time changed.
1579 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1580
1581 // Set clock to _just_ before the deadline - this should work.
1582 fake_clock.SetWallclockTime(epoch +
1583 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1584 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1585
1586 // Set clock to _just_ after the deadline - this should not work.
1587 fake_clock.SetWallclockTime(epoch +
1588 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1589 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001590}
1591
1592TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1593 OmahaResponse response;
1594 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001595 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001596 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001597
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001598 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001599 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001600 SetupPayloadStateWith2Urls(
1601 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001602
1603 Time null_time = Time();
1604 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1605 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001606}
1607
1608TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1609 OmahaResponse response;
1610 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001611 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001612 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001613 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001614 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001615 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001616 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001617 SetupPayloadStateWith2Urls(
1618 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001619
1620 // Set the clock to something known.
1621 Time time = Time::FromInternalValue(12345);
1622 fake_clock.SetWallclockTime(time);
1623
1624 // New p2p attempt - as a side-effect this will update the p2p state vars.
1625 payload_state.P2PNewAttempt();
1626 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1627 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1628
1629 // Now create a new PayloadState and check that it loads the state
1630 // vars correctly.
1631 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001632 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001633 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1634 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001635}
1636
1637TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1638 OmahaResponse response;
1639 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001640 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001641 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001642 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001643 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001644 fake_system_state.set_prefs(&fake_prefs);
1645
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001646 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001647 SetupPayloadStateWith2Urls(
1648 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001649
1650 // Set the clock to something known.
1651 Time time = Time::FromInternalValue(12345);
1652 fake_clock.SetWallclockTime(time);
1653
1654 // New p2p attempt - as a side-effect this will update the p2p state vars.
1655 payload_state.P2PNewAttempt();
1656 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1657 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1658
1659 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001660 SetupPayloadStateWith2Urls(
1661 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001662
1663 // ... and check that it clears the P2P state vars.
1664 Time null_time = Time();
1665 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1666 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001667}
1668
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001669} // namespace chromeos_update_engine