blob: c47e38947abcf254989508cfa20a08e167024009 [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;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080045using testing::SetArgumentPointee;
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
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700106// Compare the value of native array for download source parameter.
107MATCHER_P(DownloadSourceMatcher, source_array, "") {
108 return memcmp(source_array, arg, kNumDownloadSources) == 0;
109}
110
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800111class PayloadStateTest : public ::testing::Test { };
112
113TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
114 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700115 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800116 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700117 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700118 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
119 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700120 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
121 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700122 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
123 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
124 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
125 .Times(AtLeast(1));
126 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
127 .Times(AtLeast(1));
128 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
129 .Times(AtLeast(1));
130 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
131 .Times(AtLeast(1));
132 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
133 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700134 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
135 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700136 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800137 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700138 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800140 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800141 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800142 "Max Failure Count Per Url = 0\n"
143 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800144 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700145 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800146 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
147 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700148 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800149}
150
151TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
152 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800153 response.packages.push_back({.payload_urls = {"https://single.url.test"},
154 .size = 123456789,
155 .metadata_size = 58123,
156 .metadata_signature = "msign",
157 .hash = "hash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700158 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800159 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700160 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700161 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
162 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700163 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
164 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700165 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
166 .Times(AtLeast(1));
167 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
168 .Times(AtLeast(1));
169 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
170 .Times(AtLeast(1));
171 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
172 .Times(AtLeast(1));
173 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
174 .Times(AtLeast(1));
175 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
176 .Times(AtLeast(1));
177 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
178 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700179 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
180 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700181 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
182 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800183 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700184 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800185 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800186 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800187 string expected_response_sign =
188 "Payload 0:\n"
189 " Size = 123456789\n"
190 " Sha256 Hash = hash\n"
191 " Metadata Size = 58123\n"
192 " Metadata Signature = msign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700193 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800194 " NumURLs = 1\n"
195 " Candidate Url0 = https://single.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800196 "Max Failure Count Per Url = 0\n"
197 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800198 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700199 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800200 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
201 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700202 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800203}
204
205TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
206 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800207 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
208 "https://multiple.url.test"},
209 .size = 523456789,
210 .metadata_size = 558123,
211 .metadata_signature = "metasign",
212 .hash = "rhash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700213 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800214 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700215 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700216 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
217 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700218 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
219 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700220 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
221 .Times(AtLeast(1));
222 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
223 .Times(AtLeast(1));
224 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
225 .Times(AtLeast(1));
226 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
227 .Times(AtLeast(1));
228 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
229 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700230 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
231 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700232 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
233 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700234
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800235 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700236 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800237 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800238 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800239 string expected_response_sign =
240 "Payload 0:\n"
241 " Size = 523456789\n"
242 " Sha256 Hash = rhash\n"
243 " Metadata Size = 558123\n"
244 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700245 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800246 " NumURLs = 2\n"
247 " Candidate Url0 = http://multiple.url.test\n"
248 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800249 "Max Failure Count Per Url = 0\n"
250 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800251 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700252 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800253 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
254 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700255 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800256}
257
258TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
259 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700260 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800261 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800262 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800263
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700264 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800265 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700266 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
267 .Times(AtLeast(1));
268 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
269 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700270 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
271 .Times(AtLeast(1));
272 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
273 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700274 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700275
Chris Sosabe45bef2013-04-09 18:25:12 -0700276 // Reboots will be set
277 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
278
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800279 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700280 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
281 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800282
283 // Failure count should be called each times url index is set, so that's
284 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700285 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
286 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800287
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700288 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800289
290 // This does a SetResponse which causes all the states to be set to 0 for
291 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700292 SetupPayloadStateWith2Urls(
293 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700294 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295
296 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700297 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800298 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300
301 // Verify that on the next error, the URL index wraps around to 0.
302 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700303 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800304
305 // Verify that on the next error, it again advances to 1.
306 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700307 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700308
309 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800310 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800311}
312
313TEST(PayloadStateTest, NewResponseResetsPayloadState) {
314 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700315 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800317
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700318 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800319
320 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700321 SetupPayloadStateWith2Urls(
322 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700323 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800324
325 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700326 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700328 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800329 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330
331 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700332 SetupPayloadStateWith2Urls(
333 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700334 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800335
Alex Deymob33b0f02013-08-08 21:10:02 -0700336 // Fake an error again.
337 payload_state.UpdateFailed(error);
338 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800339 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700340
Alex Deymob33b0f02013-08-08 21:10:02 -0700341 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700342 SetupPayloadStateWith2Urls(
343 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700344 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
345
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800346 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700347 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800348 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
349 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
350 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700351 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800352 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700353 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800354 EXPECT_EQ(
355 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
356 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700357 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800358}
359
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800360TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
361 OmahaResponse response;
362 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700363 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700364 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800365 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800366
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700367 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700368 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
369 .Times(AtLeast(2));
370 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
371 .Times(AtLeast(1));
372 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
373 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800374
Alex Deymo820cc702013-06-28 15:43:46 -0700375 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
376 .Times(AtLeast(2));
377 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
378 .Times(AtLeast(1));
379 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
380 .Times(AtLeast(1));
381
Jay Srinivasan19409b72013-04-12 19:23:36 -0700382 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800383
Jay Srinivasan19409b72013-04-12 19:23:36 -0700384 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
385 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800386
Jay Srinivasan19409b72013-04-12 19:23:36 -0700387 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
388 .Times(AtLeast(7));
389 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
390 .Times(AtLeast(2));
391 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
392 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800393
Jay Srinivasan19409b72013-04-12 19:23:36 -0700394 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
395 .Times(AtLeast(1));
396 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
397 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700398
Jay Srinivasan19409b72013-04-12 19:23:36 -0700399 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
400 .Times(AtLeast(1));
401 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
402 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700403 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
404 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700405 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
406 .Times(AtLeast(1));
407 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
408 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700409 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
410 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700411
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700412 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800413
Sen Jiangcdd52062017-05-18 15:33:10 -0700414 SetupPayloadStateWith2Urls(
415 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700416 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800417
418 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700419 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800420 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700421 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700422 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800423 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
424 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800425
426 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700427 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800428 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700429 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700430 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800431 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
432 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800433
434 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700435 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800436 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700437 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700438 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800439 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
440 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800441
442 // This should advance the URL index as we've reached the
443 // max failure count and reset the failure count for the new URL index.
444 // This should also wrap around the URL index and thus cause the payload
445 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700446 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800447 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700448 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700449 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800450 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
451 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800452 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800453
454 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700455 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800456 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700457 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700458 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800459 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
460 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800461 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800462
463 // This should advance the URL index and payload attempt number due to
464 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700465 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800466 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700467 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700468 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800469 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
470 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800471 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800472
473 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700474 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700475 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800476 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700477 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700478 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800479 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
480 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800481 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800482
483 // And that failure count should be reset when we download some bytes
484 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700485 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800486 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700487 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700488 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800489 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
490 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800491 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800492
493 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700494 SetupPayloadStateWith2Urls(
495 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700496 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800497
498 // Make sure the url index was reset to 0 because of the new response.
499 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700500 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700501 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800502 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
503 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800504 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800505}
506
Alex Deymo820cc702013-06-28 15:43:46 -0700507TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800508 OmahaResponse response;
509 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700510 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800511 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800512
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700513 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700514 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
515 .Times(AtLeast(1));
516 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
517 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800518
Alex Deymo820cc702013-06-28 15:43:46 -0700519 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
520 .Times(AtLeast(1));
521 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
522 .Times(AtLeast(1));
523
Jay Srinivasan19409b72013-04-12 19:23:36 -0700524 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
525 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800526
Jay Srinivasan19409b72013-04-12 19:23:36 -0700527 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
528 .Times(AtLeast(1));
529 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
530 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800531
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700532 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800533
Sen Jiangcdd52062017-05-18 15:33:10 -0700534 SetupPayloadStateWith2Urls(
535 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800536
537 // This should just advance the payload attempt number;
538 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700539 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800540 payload_state.DownloadComplete();
541 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700542 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
543 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800544 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
545 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700546}
547
548TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
549 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700550 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700551 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800552 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700553
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700554 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700555 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
556 .Times(AtLeast(1));
557 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
558 .Times(AtLeast(1));
559
560 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
561 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
562 .Times(AtLeast(1));
563
564 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
565 .Times(1);
566
567 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
568 .Times(AtLeast(1));
569 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
570 .Times(AtLeast(1));
571
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700572 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700573
Sen Jiangcdd52062017-05-18 15:33:10 -0700574 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700575
576 // This should just advance the payload attempt number;
577 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
578 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
579 payload_state.DownloadComplete();
580 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
581 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700582 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800583 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
584 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800585}
586
587TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
588 OmahaResponse response;
589 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700590 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800591
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700592 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700593 SetupPayloadStateWith2Urls(
594 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800595
596 // Generate enough events to advance URL index, failure count and
597 // payload attempt number all to 1.
598 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700599 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
600 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800601 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700602 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700603 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800604 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
605 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800606
607 // Now, simulate a corrupted url index on persisted store which gets
608 // loaded when update_engine restarts. Using a different prefs object
609 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700610 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800611 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700612 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700613 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700614 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
615 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700616 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
617 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700618 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
619 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
620 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
621 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700622 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
623 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800624
625 // Note: This will be a different payload object, but the response should
626 // have the same hash as before so as to not trivially reset because the
627 // response was different. We want to specifically test that even if the
628 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700629 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Sen Jiangcdd52062017-05-18 15:33:10 -0700630 SetupPayloadStateWith2Urls(
631 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800632
633 // Make sure all counters get reset to 0 because of the corrupted URL index
634 // we supplied above.
635 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700636 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700637 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800638 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
639 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800640}
Jay Srinivasan08262882012-12-28 19:29:43 -0800641
Chris Sosa20f005c2013-09-05 13:53:08 -0700642TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
643 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700644 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 FakeSystemState fake_system_state;
646 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700647 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700648 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700649
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700650 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700651 SetupPayloadStateWith2Urls(
652 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700653
654 // Simulate two failures (enough to cause payload backoff) and check
655 // again that we're ready to re-download without any backoff as this is
656 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700657 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
658 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700659 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
660 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
661 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
662 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
663}
664
665TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
666 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700667 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700668 FakeSystemState fake_system_state;
669 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700670 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700671 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700672
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700673 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700674 SetupPayloadStateWith2Urls(
675 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700676
677 // Simulate two failures (enough to cause payload backoff) and check
678 // again that we're ready to re-download without any backoff as this is
679 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700680 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
681 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700682 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
683 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
684 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
685 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700686 payload_state.SetUsingP2PForDownloading(true);
687 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700688 // Should not backoff for p2p updates.
689 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
690
Gilad Arnold74b5f552014-10-07 08:17:16 -0700691 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700692 // No actual p2p update if no url is provided.
693 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
694}
695
Jay Srinivasan08262882012-12-28 19:29:43 -0800696TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
697 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800698 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700699 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800700
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700701 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700702 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800703
704 // Simulate a successful download and see that we're ready to download
705 // again without any backoff as this is a delta payload.
706 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700707 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
708 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800709 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
710
711 // Simulate two failures (enough to cause payload backoff) and check
712 // again that we're ready to re-download without any backoff as this is
713 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700714 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
715 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700716 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700717 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
718 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800719 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
720}
721
722static void CheckPayloadBackoffState(PayloadState* payload_state,
723 int expected_attempt_number,
724 TimeDelta expected_days) {
725 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700726 EXPECT_EQ(expected_attempt_number,
727 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800728 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
729 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
730 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
731 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
732 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
733 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
734 EXPECT_LT(expected_min_time.ToInternalValue(),
735 backoff_expiry_time.ToInternalValue());
736 EXPECT_GT(expected_max_time.ToInternalValue(),
737 backoff_expiry_time.ToInternalValue());
738}
739
740TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
741 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800742 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700743 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800744
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700745 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700746 SetupPayloadStateWith2Urls(
747 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800748
749 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
750 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
751 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
752 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
753 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
754 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
755 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
756 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
757 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
758 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
759}
760
761TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
762 OmahaResponse response;
763 response.disable_payload_backoff = true;
764 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700765 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800766
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700767 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700768 SetupPayloadStateWith2Urls(
769 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800770
771 // Simulate a successful download and see that we are ready to download
772 // again without any backoff.
773 payload_state.DownloadComplete();
774 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700775 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800776 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
777
778 // Test again, this time by simulating two errors that would cause
779 // the payload attempt number to increment due to wrap around. And
780 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700781 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
782 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800783 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700784 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800785 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
786}
787
Jay Srinivasan19409b72013-04-12 19:23:36 -0700788TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
789 OmahaResponse response;
790 response.disable_payload_backoff = true;
791 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700792 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800793 uint64_t https_total = 0;
794 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700795
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700796 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700797 SetupPayloadStateWith2Urls(
798 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700799 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700800
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700801 // Simulate a previous attempt with in order to set an initial non-zero value
802 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800803 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700804 http_total += prev_chunk;
805 payload_state.DownloadProgress(prev_chunk);
806
807 // Ensure that the initial values for HTTP reflect this attempt.
808 EXPECT_EQ(prev_chunk,
809 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
810 EXPECT_EQ(http_total,
811 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
812
813 // Change the response hash so as to simulate a new response which will
814 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700815 SetupPayloadStateWith2Urls(
816 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700817 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700818
819 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800820 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700821 http_total += first_chunk;
822 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700823 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700824 EXPECT_EQ(first_chunk,
825 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
826 EXPECT_EQ(http_total,
827 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800828 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700829 kDownloadSourceHttpsServer));
830 EXPECT_EQ(https_total,
831 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
832
833 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700834 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700835 payload_state.UpdateFailed(error);
836
Jay Srinivasan53173b92013-05-17 17:13:01 -0700837 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800838 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700839 https_total += second_chunk;
840 payload_state.DownloadProgress(second_chunk);
841 EXPECT_EQ(first_chunk,
842 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
843 EXPECT_EQ(http_total,
844 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
845 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
846 kDownloadSourceHttpsServer));
847 EXPECT_EQ(https_total,
848 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
849
850 // Simulate error to go back to http.
851 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800852 uint64_t third_chunk = 32345678;
853 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700854 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700855 payload_state.DownloadProgress(third_chunk);
856
857 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
858 EXPECT_EQ(http_chunk,
859 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700860 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700861 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
862 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
863 kDownloadSourceHttpsServer));
864 EXPECT_EQ(https_total,
865 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
866
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700867 // Simulate error (will cause URL switch), set p2p is to be used and
868 // then do 42MB worth of progress
869 payload_state.UpdateFailed(error);
870 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800871 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700872 payload_state.DownloadProgress(p2p_total);
873
874 EXPECT_EQ(p2p_total,
875 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
876
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700877 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
878 ReportSuccessfulUpdateMetrics(
879 1, _, kPayloadTypeFull, _, _, 314, _, _, 3));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700880
881 payload_state.UpdateSucceeded();
882
883 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800884 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700885 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800886 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700887 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800888 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700889 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800890 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700891 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700892 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700893}
894
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700895TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
896 OmahaResponse response;
897 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700898 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700899
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700900 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700901 SetupPayloadStateWith2Urls(
902 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700903
904 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800905 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700906 payload_state.DownloadProgress(num_bytes);
907
908 // Check that this was done via HTTP.
909 EXPECT_EQ(num_bytes,
910 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
911 EXPECT_EQ(num_bytes,
912 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
913
914 // Check that only HTTP is reported as a download source.
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700915 int64_t total_bytes[kNumDownloadSources] = {};
916 total_bytes[kDownloadSourceHttpServer] = num_bytes;
917
918 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
919 ReportSuccessfulUpdateMetrics(
920 _, _, _, _, DownloadSourceMatcher(total_bytes), _, _, _, _))
921 .Times(1);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700922
923 payload_state.UpdateSucceeded();
924}
925
Jay Srinivasan19409b72013-04-12 19:23:36 -0700926TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
927 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700928 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700929 PayloadState payload_state;
930
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700931 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700932
933 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700934 SetupPayloadStateWith2Urls(
935 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700936
Alex Deymo80f70ff2016-02-10 16:08:11 -0800937 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700938 payload_state.DownloadProgress(num_bytes);
939 EXPECT_EQ(num_bytes,
940 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
941 EXPECT_EQ(num_bytes,
942 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800943 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700944 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800945 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700946 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
947
948 payload_state.UpdateRestarted();
949 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800950 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700951 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
952 EXPECT_EQ(num_bytes,
953 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
954}
955
Chris Sosabe45bef2013-04-09 18:25:12 -0700956TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700957 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700958 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700959
Alex Deymo8427b4a2014-11-05 14:00:32 -0800960 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700961 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700962 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
963
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700964 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700965
966 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800967 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700968
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700969 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700970 payload_state.UpdateResumed();
971 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800972 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700973
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700974 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700975 payload_state.UpdateResumed();
976 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800977 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700978
979 // Restart the update again to verify we set the num of reboots back to 0.
980 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800981 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700982}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700983
Chris Sosaaa18e162013-06-20 13:20:30 -0700984TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700985 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -0700986 PayloadState payload_state;
987
Alex Deymo8427b4a2014-11-05 14:00:32 -0800988 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700989 fake_system_state.mock_powerwash_safe_prefs();
990 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -0700991
992 // Verify pre-conditions are good.
993 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
994
995 // Mock out the os version and make sure it's blacklisted correctly.
996 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700997 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -0700998 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700999 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001000
1001 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1002 rollback_version));
1003 payload_state.Rollback();
1004
1005 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001006
1007 // Change it up a little and verify we load it correctly.
1008 rollback_version = "2345.0.1";
1009 // Let's verify we can reload it correctly.
1010 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1011 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1012 SetArgumentPointee<1>(rollback_version), Return(true)));
1013 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1014 rollback_version));
1015 payload_state.LoadRollbackVersion();
1016 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001017
David Zeuthen96197df2014-04-16 12:22:39 -07001018 // Check that we report only UpdateEngine.Rollback.* metrics in
1019 // UpdateSucceeded().
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001020 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1021 ReportRollbackMetrics(metrics::RollbackResult::kSuccess))
1022 .Times(1);
1023
David Zeuthenafed4a12014-04-09 15:28:44 -07001024 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001025}
1026
David Zeuthenf413fe52013-04-22 14:04:39 -07001027TEST(PayloadStateTest, DurationsAreCorrect) {
1028 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001029 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001030 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001031 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001032 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001033 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001034
1035 // Set the clock to a well-known time - 1 second on the wall-clock
1036 // and 2 seconds on the monotonic clock
1037 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1038 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1039
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001040 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001041 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001042 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001043
1044 // Check that durations are correct for a successful update where
1045 // time has advanced 7 seconds on the wall clock and 4 seconds on
1046 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001047 SetupPayloadStateWith2Urls(
1048 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001049 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1050 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1051 payload_state.UpdateSucceeded();
1052 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1053 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1054
1055 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001056 SetupPayloadStateWith2Urls(
1057 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001058 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1059 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1060
1061 // Advance time a bit (10 secs), simulate download progress and
1062 // check that durations are updated.
1063 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1064 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1065 payload_state.DownloadProgress(10);
1066 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1067 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1068
1069 // Now simulate a reboot by resetting monotonic time (to 5000) and
1070 // creating a new PayloadState object and check that we load the
1071 // durations correctly (e.g. they are the same as before).
1072 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1073 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001074 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001075 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001076 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001077 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1078 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001079
1080 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1081 // and check that the durations are increased accordingly.
1082 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1083 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1084 payload_state2.UpdateSucceeded();
1085 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001086 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1087 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001088}
1089
David Zeuthene4c58bf2013-06-18 17:26:50 -07001090TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1091 OmahaResponse response;
1092 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001093 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001094 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001095 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001096
1097 // Set the clock to a well-known time (t = 30 seconds).
Tianjie Xu90aaa102017-10-10 17:39:03 -07001098 fake_clock.SetMonotonicTime(
1099 Time::FromInternalValue(30 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001100
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001101 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001102 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001103 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001104
1105 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001106 SetupPayloadStateWith2Urls(
1107 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001108 payload_state.UpdateSucceeded();
1109
1110 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001111 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001112
1113 // Now simulate a reboot and set the wallclock time to a later point
1114 // (t = 500 seconds). We do this by using a new PayloadState object
1115 // and checking that it emits the right UMA metric with the right
1116 // value.
Tianjie Xu90aaa102017-10-10 17:39:03 -07001117 fake_clock.SetMonotonicTime(
1118 Time::FromInternalValue(500 * Time::kMicrosecondsPerSecond));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001119 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001120 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001121
1122 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001123 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1124 ReportTimeToReboot(7));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001125 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001126
1127 payload_state2.UpdateEngineStarted();
1128
1129 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001130 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001131}
1132
Alex Deymo569c4242013-07-24 12:01:01 -07001133TEST(PayloadStateTest, RestartAfterCrash) {
1134 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001135 FakeSystemState fake_system_state;
Tianjie Xu1f93d092017-10-09 12:13:29 -07001136 testing::StrictMock<MockMetricsReporter> mock_metrics_reporter;
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001137 fake_system_state.set_metrics_reporter(&mock_metrics_reporter);
Alex Deymo8427b4a2014-11-05 14:00:32 -08001138 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001139
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001140 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001141
David Zeuthen4e1d1492014-04-25 13:12:27 -07001142 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001143 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1144 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1145 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1146 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1147 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1148 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1149 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001150 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001151
Alex Deymo569c4242013-07-24 12:01:01 -07001152 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001153 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001154
1155 payload_state.UpdateEngineStarted();
1156}
1157
David Zeuthen4e1d1492014-04-25 13:12:27 -07001158TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1159 PayloadState payload_state;
1160 FakeSystemState fake_system_state;
1161
1162 // If there's no marker at startup, ensure we don't report a metric.
1163 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001164 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1165 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1166 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001167 payload_state.UpdateEngineStarted();
1168}
1169
1170TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1171 PayloadState payload_state;
1172 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001173 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001174
1175 // If we have a marker at startup, ensure it's reported and the
1176 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001177 fake_system_state.set_prefs(&fake_prefs);
1178 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001179
1180 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1181
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001182 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1183 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1184 .Times(1);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001185 payload_state.UpdateEngineStarted();
1186
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001187 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001188}
1189
1190TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1191 PayloadState payload_state;
1192 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001193 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001194
1195 // Make sure the marker is written and cleared during an attempt and
1196 // also that we DO NOT emit the metric (since the attempt didn't end
1197 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001198 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001199 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001200 OmahaResponse response;
1201 response.packages.resize(1);
1202 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001203
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001204 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1205 ReportAbnormallyTerminatedUpdateAttemptMetrics())
1206 .Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001207
1208 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001209 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001210
1211 payload_state.UpdateRestarted();
1212
1213 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001214 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001215
1216 payload_state.UpdateSucceeded();
1217
1218 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001219 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001220}
1221
Jay Srinivasan53173b92013-05-17 17:13:01 -07001222TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1223 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001224 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001225 PayloadState payload_state;
1226
Jay Srinivasan53173b92013-05-17 17:13:01 -07001227 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001228 fake_system_state.set_device_policy(&disable_http_policy);
1229 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001230
1231 // Test with no device policy. Should default to allowing http.
1232 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1233 .WillRepeatedly(Return(false));
1234
1235 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001236 SetupPayloadStateWith2Urls(
1237 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001238
1239 // Check that we use the HTTP URL since there is no value set for allowing
1240 // http.
1241 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1242
1243 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001244 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1245 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1246
Chris Sosaf7d80042013-08-22 16:45:17 -07001247 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001248 SetupPayloadStateWith2Urls(
1249 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001250
1251 // Check that we skip the HTTP URL and use only the HTTPS url.
1252 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1253
1254 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001255 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001256 payload_state.UpdateFailed(error);
1257
1258 // Check that we still skip the HTTP URL and use only the HTTPS url.
1259 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001260 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001261
1262 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001263 SetupPayloadStateWith2Urls(
1264 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001265
1266 // Check that we still skip the HTTP URL and use only the HTTPS url.
1267 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1268
1269 // Now, pretend that the HTTP policy is turned on. We want to make sure
1270 // the new policy is honored.
1271 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001272 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001273 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1274 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1275
1276 // Now, set the same response using the same hash
1277 // so that we can test that the state is reset not because of the
1278 // hash but because of the policy change which results in candidate url
1279 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001280 SetupPayloadStateWith2Urls(
1281 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001282
1283 // Check that we use the HTTP URL now and the failure count is reset.
1284 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001285 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001286
1287 // Fake a failure and see if we're moving over to the HTTPS url and update
1288 // the URL switch count properly.
1289 payload_state.UpdateFailed(error);
1290 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001291 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1292 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001293}
1294
Alex Deymo1c656c42013-06-28 11:02:14 -07001295TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1296 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001297 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001298 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001299
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001300 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001301 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001302
1303 // Simulate a successful download and update.
1304 payload_state.DownloadComplete();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001305 EXPECT_CALL(
1306 *fake_system_state.mock_metrics_reporter(),
1307 ReportSuccessfulUpdateMetrics(_, _, kPayloadTypeDelta, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001308 payload_state.UpdateSucceeded();
1309
1310 // Mock the request to a request where the delta was disabled but Omaha sends
1311 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001312 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001313 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001314 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001315
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001316 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001317 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001318
1319 payload_state.DownloadComplete();
1320
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001321 EXPECT_CALL(
1322 *fake_system_state.mock_metrics_reporter(),
1323 ReportSuccessfulUpdateMetrics(_, _, kPayloadTypeDelta, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001324 payload_state.UpdateSucceeded();
1325}
1326
1327TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1328 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001329 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001330 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001331
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001332 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001333 SetupPayloadStateWith2Urls(
1334 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001335
1336 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001337 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001338 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001339 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001340
1341 // Simulate a successful download and update.
1342 payload_state.DownloadComplete();
1343
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001344 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1345 ReportSuccessfulUpdateMetrics(
1346 _, _, kPayloadTypeForcedFull, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001347 payload_state.UpdateSucceeded();
1348}
1349
1350TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1351 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001352 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001353 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001354
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001355 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001356 SetupPayloadStateWith2Urls(
1357 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001358
Alex Deymo820cc702013-06-28 15:43:46 -07001359 // Mock the request to a request where the delta is enabled, although the
1360 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001361 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001362 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001363 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001364
1365 // Simulate a successful download and update.
1366 payload_state.DownloadComplete();
1367
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001368 EXPECT_CALL(
1369 *fake_system_state.mock_metrics_reporter(),
1370 ReportSuccessfulUpdateMetrics(_, _, kPayloadTypeFull, _, _, _, _, _, _));
Alex Deymo1c656c42013-06-28 11:02:14 -07001371 payload_state.UpdateSucceeded();
1372}
1373
Alex Deymo42432912013-07-12 20:21:15 -07001374TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001375 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001376 OmahaResponse response;
1377 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001378 FakePrefs fake_prefs;
1379 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001380
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001381 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001382 SetupPayloadStateWith2Urls(
1383 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001384
1385 // Simulate a successful download and update.
1386 payload_state.DownloadComplete();
1387 payload_state.UpdateSucceeded();
1388 payload_state.ExpectRebootInNewVersion("Version:12345678");
1389
1390 // Reboot into the same environment to get an UMA metric with a value of 1.
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001391 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1392 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001393 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001394 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001395
1396 // Simulate a second update and reboot into the same environment, this should
1397 // send a value of 2.
1398 payload_state.ExpectRebootInNewVersion("Version:12345678");
1399
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001400 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1401 ReportFailedUpdateCount(2));
Alex Deymo42432912013-07-12 20:21:15 -07001402 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001403 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001404
1405 // Simulate a third failed reboot to new version, but this time for a
1406 // different payload. This should send a value of 1 this time.
1407 payload_state.ExpectRebootInNewVersion("Version:3141592");
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001408 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1409 ReportFailedUpdateCount(1));
Alex Deymo42432912013-07-12 20:21:15 -07001410 payload_state.ReportFailedBootIfNeeded();
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001411 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_reporter());
Alex Deymo42432912013-07-12 20:21:15 -07001412}
1413
1414TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001415 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001416 OmahaResponse response;
1417 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001418 FakePrefs fake_prefs;
1419 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001420
Alex Deymo763e7db2015-08-27 21:08:08 -07001421 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1422 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001423
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001424 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001425 SetupPayloadStateWith2Urls(
1426 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001427
1428 // Simulate a successful download and update.
1429 payload_state.DownloadComplete();
1430 payload_state.UpdateSucceeded();
1431 payload_state.ExpectRebootInNewVersion("Version:12345678");
1432
1433 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001434 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001435
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001436 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1437 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001438 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001439 payload_state.ReportFailedBootIfNeeded();
1440
Alex Deymo763e7db2015-08-27 21:08:08 -07001441 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001442 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001443 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001444 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001445}
1446
1447TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001448 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001449 OmahaResponse response;
1450 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001451 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001452
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001453 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001454 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001455 SetupPayloadStateWith2Urls(
1456 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001457
1458 // Simulate a successful download and update.
1459 payload_state.DownloadComplete();
1460 payload_state.UpdateSucceeded();
1461 payload_state.ExpectRebootInNewVersion("Version:12345678");
1462
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001463 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1464 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001465 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001466
1467 // Cancel the applied update.
1468 payload_state.ResetUpdateStatus();
1469
1470 // Simulate a reboot.
1471 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001472}
1473
1474TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001475 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001476 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001477 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001478
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001479 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001480 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001481
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001482 EXPECT_CALL(*fake_system_state.mock_metrics_reporter(),
1483 ReportFailedUpdateCount(_))
David Zeuthen33bae492014-02-25 16:16:18 -08001484 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001485
1486 // Simulate a reboot in this environment.
1487 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001488}
1489
David Zeuthendcba8092013-08-06 12:16:35 -07001490TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1491 OmahaResponse response;
1492 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001493 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001494 FakePrefs fake_prefs;
1495 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001496
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001497 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001498 SetupPayloadStateWith2Urls(
1499 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001500
1501 // Should allow exactly kMaxP2PAttempts...
1502 for (int n = 0; n < kMaxP2PAttempts; n++) {
1503 payload_state.P2PNewAttempt();
1504 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1505 }
1506 // ... but not more than that.
1507 payload_state.P2PNewAttempt();
1508 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001509}
1510
1511TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1512 OmahaResponse response;
1513 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001514 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001515 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001516 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001517
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001518 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001519 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001520 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001521 SetupPayloadStateWith2Urls(
1522 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001523
1524 // Set the clock to 1 second.
1525 Time epoch = Time::FromInternalValue(1000000);
1526 fake_clock.SetWallclockTime(epoch);
1527
1528 // Do an attempt - this will set the timestamp.
1529 payload_state.P2PNewAttempt();
1530
1531 // Check that the timestamp equals what we just set.
1532 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1533
1534 // Time hasn't advanced - this should work.
1535 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1536
1537 // Set clock to half the deadline - this should work.
1538 fake_clock.SetWallclockTime(epoch +
1539 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1540 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1541
1542 // Check that the first attempt timestamp hasn't changed just
1543 // because the wall-clock time changed.
1544 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1545
1546 // Set clock to _just_ before the deadline - this should work.
1547 fake_clock.SetWallclockTime(epoch +
1548 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1549 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1550
1551 // Set clock to _just_ after the deadline - this should not work.
1552 fake_clock.SetWallclockTime(epoch +
1553 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1554 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001555}
1556
1557TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1558 OmahaResponse response;
1559 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001560 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001561 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001562
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001563 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001564 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001565 SetupPayloadStateWith2Urls(
1566 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001567
1568 Time null_time = Time();
1569 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1570 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001571}
1572
1573TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1574 OmahaResponse response;
1575 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001576 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001577 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001578 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001579 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001580 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001581 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001582 SetupPayloadStateWith2Urls(
1583 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001584
1585 // Set the clock to something known.
1586 Time time = Time::FromInternalValue(12345);
1587 fake_clock.SetWallclockTime(time);
1588
1589 // New p2p attempt - as a side-effect this will update the p2p state vars.
1590 payload_state.P2PNewAttempt();
1591 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1592 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1593
1594 // Now create a new PayloadState and check that it loads the state
1595 // vars correctly.
1596 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001597 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001598 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1599 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001600}
1601
1602TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1603 OmahaResponse response;
1604 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001605 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001606 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001607 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001608 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001609 fake_system_state.set_prefs(&fake_prefs);
1610
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001611 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001612 SetupPayloadStateWith2Urls(
1613 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001614
1615 // Set the clock to something known.
1616 Time time = Time::FromInternalValue(12345);
1617 fake_clock.SetWallclockTime(time);
1618
1619 // New p2p attempt - as a side-effect this will update the p2p state vars.
1620 payload_state.P2PNewAttempt();
1621 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1622 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1623
1624 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001625 SetupPayloadStateWith2Urls(
1626 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001627
1628 // ... and check that it clears the P2P state vars.
1629 Time null_time = Time();
1630 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1631 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001632}
1633
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001634} // namespace chromeos_update_engine