blob: 52c28d0450e9f2669c54e4b4b2872c2314468026 [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
Chris Sosaaa18e162013-06-20 13:20:30 -0700988TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700989 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -0700990 PayloadState payload_state;
991
Alex Deymo8427b4a2014-11-05 14:00:32 -0800992 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700993 fake_system_state.mock_powerwash_safe_prefs();
994 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -0700995
996 // Verify pre-conditions are good.
997 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
998
999 // Mock out the os version and make sure it's blacklisted correctly.
1000 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001001 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001002 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001003 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001004
1005 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1006 rollback_version));
1007 payload_state.Rollback();
1008
1009 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001010
1011 // Change it up a little and verify we load it correctly.
1012 rollback_version = "2345.0.1";
1013 // Let's verify we can reload it correctly.
1014 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1015 kPrefsRollbackVersion, _)).WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -07001016 SetArgPointee<1>(rollback_version), Return(true)));
Chris Sosab3dcdb32013-09-04 15:22:12 -07001017 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1018 rollback_version));
1019 payload_state.LoadRollbackVersion();
1020 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001021
David Zeuthen96197df2014-04-16 12:22:39 -07001022 // Check that we report only UpdateEngine.Rollback.* metrics in
1023 // UpdateSucceeded().
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001024 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1025 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1026 .Times(1);
1027
David Zeuthenafed4a12014-04-09 15:28:44 -07001028 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001029}
1030
David Zeuthenf413fe52013-04-22 14:04:39 -07001031TEST(PayloadStateTest, DurationsAreCorrect) {
1032 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001033 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001034 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001035 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001036 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001037 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001038
1039 // Set the clock to a well-known time - 1 second on the wall-clock
1040 // and 2 seconds on the monotonic clock
1041 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1042 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1043
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001044 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001045 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001046 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001047
1048 // Check that durations are correct for a successful update where
1049 // time has advanced 7 seconds on the wall clock and 4 seconds on
1050 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001051 SetupPayloadStateWith2Urls(
1052 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001053 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1054 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1055 payload_state.UpdateSucceeded();
1056 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1057 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1058
1059 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001060 SetupPayloadStateWith2Urls(
1061 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001062 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1063 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1064
1065 // Advance time a bit (10 secs), simulate download progress and
1066 // check that durations are updated.
1067 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1068 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1069 payload_state.DownloadProgress(10);
1070 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1071 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1072
1073 // Now simulate a reboot by resetting monotonic time (to 5000) and
1074 // creating a new PayloadState object and check that we load the
1075 // durations correctly (e.g. they are the same as before).
1076 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1077 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001078 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001079 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001080 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001081 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1082 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001083
1084 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1085 // and check that the durations are increased accordingly.
1086 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1087 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1088 payload_state2.UpdateSucceeded();
1089 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001090 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1091 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001092}
1093
David Zeuthene4c58bf2013-06-18 17:26:50 -07001094TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1095 OmahaResponse response;
1096 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001097 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001098 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001099 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001100
1101 // Set the clock to a well-known time (t = 30 seconds).
Tianjie Xu90aaa102017-10-10 17:39:03 -07001102 fake_clock.SetMonotonicTime(
1103 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001104
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001105 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001106 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001107 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001108
1109 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001110 SetupPayloadStateWith2Urls(
1111 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001112 payload_state.UpdateSucceeded();
1113
1114 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001115 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001116
1117 // Now simulate a reboot and set the wallclock time to a later point
1118 // (t = 500 seconds). We do this by using a new PayloadState object
1119 // and checking that it emits the right UMA metric with the right
1120 // value.
Tianjie Xu90aaa102017-10-10 17:39:03 -07001121 fake_clock.SetMonotonicTime(
1122 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001123 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001124 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001125
1126 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001127 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1128 ReportTimeToReboot(7));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001129 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001130
1131 payload_state2.UpdateEngineStarted();
1132
1133 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001134 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001135}
1136
Alex Deymo569c4242013-07-24 12:01:01 -07001137TEST(PayloadStateTest, RestartAfterCrash) {
1138 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001139 FakeSystemState fake_system_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001140 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001141 fake_system_state.set_metrics_reporter(&mock_metrics_reporter);
Alex Deymo8427b4a2014-11-05 14:00:32 -08001142 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001143
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001144 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001145
David Zeuthen4e1d1492014-04-25 13:12:27 -07001146 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001147 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1148 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1149 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1150 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1151 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1152 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1153 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001154 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001155
Alex Deymo569c4242013-07-24 12:01:01 -07001156 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001157 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001158
1159 payload_state.UpdateEngineStarted();
1160}
1161
David Zeuthen4e1d1492014-04-25 13:12:27 -07001162TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1163 PayloadState payload_state;
1164 FakeSystemState fake_system_state;
1165
1166 // If there's no marker at startup, ensure we don't report a metric.
1167 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001168 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1169 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1170 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001171 payload_state.UpdateEngineStarted();
1172}
1173
1174TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1175 PayloadState payload_state;
1176 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001177 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001178
1179 // If we have a marker at startup, ensure it's reported and the
1180 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001181 fake_system_state.set_prefs(&fake_prefs);
1182 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001183
1184 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1185
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001186 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1187 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1188 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001189 payload_state.UpdateEngineStarted();
1190
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001191 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001192}
1193
1194TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1195 PayloadState payload_state;
1196 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001197 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001198
1199 // Make sure the marker is written and cleared during an attempt and
1200 // also that we DO NOT emit the metric (since the attempt didn't end
1201 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001202 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001203 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001204 OmahaResponse response;
1205 response.packages.resize(1);
1206 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001207
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001208 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1209 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1210 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001211
1212 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001213 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001214
1215 payload_state.UpdateRestarted();
1216
1217 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001218 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001219
1220 payload_state.UpdateSucceeded();
1221
1222 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001223 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001224}
1225
Jay Srinivasan53173b92013-05-17 17:13:01 -07001226TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1227 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001228 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001229 PayloadState payload_state;
1230
Jay Srinivasan53173b92013-05-17 17:13:01 -07001231 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001232 fake_system_state.set_device_policy(&disable_http_policy);
1233 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001234
1235 // Test with no device policy. Should default to allowing http.
1236 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1237 .WillRepeatedly(Return(false));
1238
1239 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001240 SetupPayloadStateWith2Urls(
1241 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001242
1243 // Check that we use the HTTP URL since there is no value set for allowing
1244 // http.
1245 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1246
1247 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001248 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001249 .WillRepeatedly(DoAll(SetArgPointee<0>(false), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001250
Chris Sosaf7d80042013-08-22 16:45:17 -07001251 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001252 SetupPayloadStateWith2Urls(
1253 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001254
1255 // Check that we skip the HTTP URL and use only the HTTPS url.
1256 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1257
1258 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001259 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001260 payload_state.UpdateFailed(error);
1261
1262 // Check that we still skip the HTTP URL and use only the HTTPS url.
1263 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001264 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001265
1266 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001267 SetupPayloadStateWith2Urls(
1268 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001269
1270 // Check that we still skip the HTTP URL and use only the HTTPS url.
1271 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1272
1273 // Now, pretend that the HTTP policy is turned on. We want to make sure
1274 // the new policy is honored.
1275 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001276 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001277 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
Ben Chan672c1f52017-10-23 15:41:39 -07001278 .WillRepeatedly(DoAll(SetArgPointee<0>(true), Return(true)));
Jay Srinivasan53173b92013-05-17 17:13:01 -07001279
1280 // Now, set the same response using the same hash
1281 // so that we can test that the state is reset not because of the
1282 // hash but because of the policy change which results in candidate url
1283 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001284 SetupPayloadStateWith2Urls(
1285 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001286
1287 // Check that we use the HTTP URL now and the failure count is reset.
1288 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001289 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001290
1291 // Fake a failure and see if we're moving over to the HTTPS url and update
1292 // the URL switch count properly.
1293 payload_state.UpdateFailed(error);
1294 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001295 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1296 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001297}
1298
Alex Deymo1c656c42013-06-28 11:02:14 -07001299TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1300 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001301 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001302 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001303
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001304 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001305 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001306
1307 // Simulate a successful download and update.
1308 payload_state.DownloadComplete();
Sen Jiang8712e962018-05-08 12:12:28 -07001309 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1310 ReportSuccessfulUpdateMetrics(
1311 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001312 payload_state.UpdateSucceeded();
1313
1314 // Mock the request to a request where the delta was disabled but Omaha sends
1315 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001316 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001317 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001318 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001319
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001320 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001321 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001322
1323 payload_state.DownloadComplete();
1324
Sen Jiang8712e962018-05-08 12:12:28 -07001325 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1326 ReportSuccessfulUpdateMetrics(
1327 _, _, kPayloadTypeDelta, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001328 payload_state.UpdateSucceeded();
1329}
1330
1331TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1332 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001333 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001334 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001335
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001336 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001337 SetupPayloadStateWith2Urls(
1338 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001339
1340 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001341 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001342 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001343 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001344
1345 // Simulate a successful download and update.
1346 payload_state.DownloadComplete();
1347
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001348 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1349 ReportSuccessfulUpdateMetrics(
Sen Jiang8712e962018-05-08 12:12:28 -07001350 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001351 payload_state.UpdateSucceeded();
1352}
1353
1354TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1355 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001356 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001357 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001358
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001359 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001360 SetupPayloadStateWith2Urls(
1361 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001362
Alex Deymo820cc702013-06-28 15:43:46 -07001363 // Mock the request to a request where the delta is enabled, although the
1364 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001365 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001366 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001367 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001368
1369 // Simulate a successful download and update.
1370 payload_state.DownloadComplete();
1371
Sen Jiang8712e962018-05-08 12:12:28 -07001372 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1373 ReportSuccessfulUpdateMetrics(
1374 _, _, kPayloadTypeFull, _, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001375 payload_state.UpdateSucceeded();
1376}
1377
Alex Deymo42432912013-07-12 20:21:15 -07001378TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001379 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001380 OmahaResponse response;
1381 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001382 FakePrefs fake_prefs;
1383 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001384
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001385 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001386 SetupPayloadStateWith2Urls(
1387 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001388
1389 // Simulate a successful download and update.
1390 payload_state.DownloadComplete();
1391 payload_state.UpdateSucceeded();
1392 payload_state.ExpectRebootInNewVersion("Version:12345678");
1393
1394 // Reboot into the same environment to get an UMA metric with a value of 1.
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001395 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1396 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001397 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001398 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001399
1400 // Simulate a second update and reboot into the same environment, this should
1401 // send a value of 2.
1402 payload_state.ExpectRebootInNewVersion("Version:12345678");
1403
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001404 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1405 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001406 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001407 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001408
1409 // Simulate a third failed reboot to new version, but this time for a
1410 // different payload. This should send a value of 1 this time.
1411 payload_state.ExpectRebootInNewVersion("Version:3141592");
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001412 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1413 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001414 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001415 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001416}
1417
1418TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001419 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001420 OmahaResponse response;
1421 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001422 FakePrefs fake_prefs;
1423 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001424
Alex Deymo763e7db2015-08-27 21:08:08 -07001425 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1426 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001427
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001428 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001429 SetupPayloadStateWith2Urls(
1430 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001431
1432 // Simulate a successful download and update.
1433 payload_state.DownloadComplete();
1434 payload_state.UpdateSucceeded();
1435 payload_state.ExpectRebootInNewVersion("Version:12345678");
1436
1437 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001438 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001439
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001440 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1441 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001442 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001443 payload_state.ReportFailedBootIfNeeded();
1444
Alex Deymo763e7db2015-08-27 21:08:08 -07001445 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001446 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001447 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001448 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001449}
1450
1451TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001452 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001453 OmahaResponse response;
1454 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001455 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001456
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001457 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001458 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001459 SetupPayloadStateWith2Urls(
1460 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001461
1462 // Simulate a successful download and update.
1463 payload_state.DownloadComplete();
1464 payload_state.UpdateSucceeded();
1465 payload_state.ExpectRebootInNewVersion("Version:12345678");
1466
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001467 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1468 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001469 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001470
1471 // Cancel the applied update.
1472 payload_state.ResetUpdateStatus();
1473
1474 // Simulate a reboot.
1475 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001476}
1477
1478TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001479 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001480 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001481 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001482
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001483 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001484 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001485
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001486 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1487 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001488 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001489
1490 // Simulate a reboot in this environment.
1491 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001492}
1493
David Zeuthendcba8092013-08-06 12:16:35 -07001494TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1495 OmahaResponse response;
1496 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001497 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001498 FakePrefs fake_prefs;
1499 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001500
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001501 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001502 SetupPayloadStateWith2Urls(
1503 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001504
1505 // Should allow exactly kMaxP2PAttempts...
1506 for (int n = 0; n < kMaxP2PAttempts; n++) {
1507 payload_state.P2PNewAttempt();
1508 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1509 }
1510 // ... but not more than that.
1511 payload_state.P2PNewAttempt();
1512 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001513}
1514
1515TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1516 OmahaResponse response;
1517 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001518 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001519 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001520 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001521
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001522 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001523 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001524 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001525 SetupPayloadStateWith2Urls(
1526 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001527
1528 // Set the clock to 1 second.
1529 Time epoch = Time::FromInternalValue(1000000);
1530 fake_clock.SetWallclockTime(epoch);
1531
1532 // Do an attempt - this will set the timestamp.
1533 payload_state.P2PNewAttempt();
1534
1535 // Check that the timestamp equals what we just set.
1536 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1537
1538 // Time hasn't advanced - this should work.
1539 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1540
1541 // Set clock to half the deadline - this should work.
1542 fake_clock.SetWallclockTime(epoch +
1543 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1544 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1545
1546 // Check that the first attempt timestamp hasn't changed just
1547 // because the wall-clock time changed.
1548 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1549
1550 // Set clock to _just_ before the deadline - this should work.
1551 fake_clock.SetWallclockTime(epoch +
1552 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1553 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1554
1555 // Set clock to _just_ after the deadline - this should not work.
1556 fake_clock.SetWallclockTime(epoch +
1557 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1558 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001559}
1560
1561TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1562 OmahaResponse response;
1563 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001564 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001565 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001566
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001567 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001568 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001569 SetupPayloadStateWith2Urls(
1570 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001571
1572 Time null_time = Time();
1573 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1574 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001575}
1576
1577TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1578 OmahaResponse response;
1579 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001580 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001581 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001582 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001583 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001584 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001585 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001586 SetupPayloadStateWith2Urls(
1587 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001588
1589 // Set the clock to something known.
1590 Time time = Time::FromInternalValue(12345);
1591 fake_clock.SetWallclockTime(time);
1592
1593 // New p2p attempt - as a side-effect this will update the p2p state vars.
1594 payload_state.P2PNewAttempt();
1595 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1596 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1597
1598 // Now create a new PayloadState and check that it loads the state
1599 // vars correctly.
1600 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001601 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001602 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1603 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001604}
1605
1606TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1607 OmahaResponse response;
1608 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001609 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001610 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001611 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001612 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001613 fake_system_state.set_prefs(&fake_prefs);
1614
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001615 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001616 SetupPayloadStateWith2Urls(
1617 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001618
1619 // Set the clock to something known.
1620 Time time = Time::FromInternalValue(12345);
1621 fake_clock.SetWallclockTime(time);
1622
1623 // New p2p attempt - as a side-effect this will update the p2p state vars.
1624 payload_state.P2PNewAttempt();
1625 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1626 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1627
1628 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001629 SetupPayloadStateWith2Urls(
1630 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001631
1632 // ... and check that it clears the P2P state vars.
1633 Time null_time = Time();
1634 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1635 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001636}
1637
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001638} // namespace chromeos_update_engine