blob: 97bebd72fc4beaa36ea87fba7419c081fbdd3090 [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <glib.h>
6
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08007#include "base/stringprintf.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08008#include "gmock/gmock.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08009#include "gtest/gtest.h"
10
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080011#include "update_engine/omaha_request_action.h"
12#include "update_engine/payload_state.h"
13#include "update_engine/prefs_mock.h"
14#include "update_engine/test_utils.h"
15#include "update_engine/utils.h"
16
Jay Srinivasan08262882012-12-28 19:29:43 -080017using base::Time;
18using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019using std::string;
20using testing::_;
21using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080022using testing::Return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023using testing::SetArgumentPointee;
24
25namespace chromeos_update_engine {
26
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080027static void SetupPayloadStateWith2Urls(string hash,
28 PayloadState* payload_state,
29 OmahaResponse* response) {
30 response->payload_urls.clear();
31 response->payload_urls.push_back("http://test");
32 response->payload_urls.push_back("https://test");
33 response->size = 523456789;
34 response->hash = hash;
35 response->metadata_size = 558123;
36 response->metadata_signature = "metasign";
37 response->max_failure_count_per_url = 3;
38 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080039 string stored_response_sign = payload_state->GetResponseSignature();
40 string expected_response_sign = StringPrintf(
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080041 "NumURLs = 2\n"
42 "Url0 = http://test\n"
43 "Url1 = https://test\n"
44 "Payload Size = 523456789\n"
45 "Payload Sha256 Hash = %s\n"
46 "Metadata Size = 558123\n"
Jay Srinivasan08262882012-12-28 19:29:43 -080047 "Metadata Signature = metasign\n"
48 "Is Delta Payload = %d\n"
49 "Max Failure Count Per Url = %d\n"
50 "Disable Payload Backoff = %d\n",
51 hash.c_str(),
52 response->is_delta_payload,
53 response->max_failure_count_per_url,
54 response->disable_payload_backoff);
55 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080056}
57
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080058class PayloadStateTest : public ::testing::Test { };
59
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080060TEST(PayloadStateTest, DidYouAddANewActionExitCode) {
Jay Srinivasan08262882012-12-28 19:29:43 -080061 if (kActionCodeUmaReportedMax != 41) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080062 LOG(ERROR) << "The following failure is intentional. If you added a new "
63 << "ActionExitCode enum value, make sure to add it to the "
64 << "PayloadState::UpdateFailed method and then update this test "
65 << "to the new value of kActionCodeUmaReportedMax, which is "
66 << kActionCodeUmaReportedMax;
67 EXPECT_FALSE("Please see the log line above");
68 }
69}
70
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080071TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
72 OmahaResponse response;
73 NiceMock<PrefsMock> prefs;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080074 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 0));
Jay Srinivasan08262882012-12-28 19:29:43 -080075 EXPECT_CALL(prefs, SetInt64(kPrefsBackoffExpiryTime, 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080076 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080077 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078 PayloadState payload_state;
79 EXPECT_TRUE(payload_state.Initialize(&prefs));
80 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -080081 string stored_response_sign = payload_state.GetResponseSignature();
82 string expected_response_sign = "NumURLs = 0\n"
83 "Payload Size = 0\n"
84 "Payload Sha256 Hash = \n"
85 "Metadata Size = 0\n"
86 "Metadata Signature = \n"
87 "Is Delta Payload = 0\n"
88 "Max Failure Count Per Url = 0\n"
89 "Disable Payload Backoff = 0\n";
90 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080091 EXPECT_EQ(0, payload_state.GetUrlIndex());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080092 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080093}
94
95TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
96 OmahaResponse response;
97 response.payload_urls.push_back("http://single.url.test");
98 response.size = 123456789;
99 response.hash = "hash";
100 response.metadata_size = 58123;
101 response.metadata_signature = "msign";
102 NiceMock<PrefsMock> prefs;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800103 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 0));
Jay Srinivasan08262882012-12-28 19:29:43 -0800104 EXPECT_CALL(prefs, SetInt64(kPrefsBackoffExpiryTime, 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800105 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800106 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800107 PayloadState payload_state;
108 EXPECT_TRUE(payload_state.Initialize(&prefs));
109 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800110 string stored_response_sign = payload_state.GetResponseSignature();
111 string expected_response_sign = "NumURLs = 1\n"
112 "Url0 = http://single.url.test\n"
113 "Payload Size = 123456789\n"
114 "Payload Sha256 Hash = hash\n"
115 "Metadata Size = 58123\n"
116 "Metadata Signature = msign\n"
117 "Is Delta Payload = 0\n"
118 "Max Failure Count Per Url = 0\n"
119 "Disable Payload Backoff = 0\n";
120 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800121 EXPECT_EQ(0, payload_state.GetUrlIndex());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800122 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800123}
124
125TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
126 OmahaResponse response;
127 response.payload_urls.push_back("http://multiple.url.test");
128 response.payload_urls.push_back("https://multiple.url.test");
129 response.size = 523456789;
130 response.hash = "rhash";
131 response.metadata_size = 558123;
132 response.metadata_signature = "metasign";
133 NiceMock<PrefsMock> prefs;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800134 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 0));
Jay Srinivasan08262882012-12-28 19:29:43 -0800135 EXPECT_CALL(prefs, SetInt64(kPrefsBackoffExpiryTime, 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800136 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800137 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800138 PayloadState payload_state;
139 EXPECT_TRUE(payload_state.Initialize(&prefs));
140 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800141 string stored_response_sign = payload_state.GetResponseSignature();
142 string expected_response_sign = "NumURLs = 2\n"
143 "Url0 = http://multiple.url.test\n"
144 "Url1 = https://multiple.url.test\n"
145 "Payload Size = 523456789\n"
146 "Payload Sha256 Hash = rhash\n"
147 "Metadata Size = 558123\n"
148 "Metadata Signature = metasign\n"
149 "Is Delta Payload = 0\n"
150 "Max Failure Count Per Url = 0\n"
151 "Disable Payload Backoff = 0\n";
152 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800153 EXPECT_EQ(0, payload_state.GetUrlIndex());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800154 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800155}
156
157TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
158 OmahaResponse response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800159 NiceMock<PrefsMock> prefs;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800160 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800161
162 // Payload attempt should start with 0 and then advance to 1.
163 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)).Times(1);
164 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)).Times(1);
Jay Srinivasan08262882012-12-28 19:29:43 -0800165 EXPECT_CALL(prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(2);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800166
167 // Url index should go from 0 to 1 twice.
168 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(2);
169 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(2);
170
171 // Failure count should be called each times url index is set, so that's
172 // 4 times for this test.
173 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)).Times(4);
174
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800175 EXPECT_TRUE(payload_state.Initialize(&prefs));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800176
177 // This does a SetResponse which causes all the states to be set to 0 for
178 // the first time.
179 SetupPayloadStateWith2Urls("Hash1235", &payload_state, &response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 EXPECT_EQ(0, payload_state.GetUrlIndex());
181
182 // Verify that on the first error, the URL index advances to 1.
183 ActionExitCode error = kActionCodeDownloadMetadataSignatureMismatch;
184 payload_state.UpdateFailed(error);
185 EXPECT_EQ(1, payload_state.GetUrlIndex());
186
187 // Verify that on the next error, the URL index wraps around to 0.
188 payload_state.UpdateFailed(error);
189 EXPECT_EQ(0, payload_state.GetUrlIndex());
190
191 // Verify that on the next error, it again advances to 1.
192 payload_state.UpdateFailed(error);
193 EXPECT_EQ(1, payload_state.GetUrlIndex());
194}
195
196TEST(PayloadStateTest, NewResponseResetsPayloadState) {
197 OmahaResponse response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198 NiceMock<PrefsMock> prefs;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800199 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800200
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800201 EXPECT_TRUE(payload_state.Initialize(&prefs));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800202
203 // Set the first response.
204 SetupPayloadStateWith2Urls("Hash5823", &payload_state, &response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800205
206 // Advance the URL index to 1 by faking an error.
207 ActionExitCode error = kActionCodeDownloadMetadataSignatureMismatch;
208 payload_state.UpdateFailed(error);
209 EXPECT_EQ(1, payload_state.GetUrlIndex());
210
211 // Now, slightly change the response and set it again.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800212 SetupPayloadStateWith2Urls("Hash8225", &payload_state, &response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800213
214 // Make sure the url index was reset to 0 because of the new response.
215 EXPECT_EQ(0, payload_state.GetUrlIndex());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800216 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800217}
218
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800219TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
220 OmahaResponse response;
221 PayloadState payload_state;
222 NiceMock<PrefsMock> prefs;
223
224 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)).Times(2);
225 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)).Times(1);
226 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 2)).Times(1);
227
Jay Srinivasan08262882012-12-28 19:29:43 -0800228 EXPECT_CALL(prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(4);
229
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800230 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(4);
231 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(2);
232
233 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)).Times(7);
234 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1)).Times(2);
235 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2)).Times(1);
236
237 EXPECT_TRUE(payload_state.Initialize(&prefs));
238
239 SetupPayloadStateWith2Urls("Hash5873", &payload_state, &response);
240
241 // This should advance the URL index.
242 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMismatch);
243 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
244 EXPECT_EQ(1, payload_state.GetUrlIndex());
245 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
246
247 // This should advance the failure count only.
248 payload_state.UpdateFailed(kActionCodeDownloadTransferError);
249 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
250 EXPECT_EQ(1, payload_state.GetUrlIndex());
251 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
252
253 // This should advance the failure count only.
254 payload_state.UpdateFailed(kActionCodeDownloadTransferError);
255 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
256 EXPECT_EQ(1, payload_state.GetUrlIndex());
257 EXPECT_EQ(2, payload_state.GetUrlFailureCount());
258
259 // This should advance the URL index as we've reached the
260 // max failure count and reset the failure count for the new URL index.
261 // This should also wrap around the URL index and thus cause the payload
262 // attempt number to be incremented.
263 payload_state.UpdateFailed(kActionCodeDownloadTransferError);
264 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
265 EXPECT_EQ(0, payload_state.GetUrlIndex());
266 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800267 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800268
269 // This should advance the URL index.
270 payload_state.UpdateFailed(kActionCodePayloadHashMismatchError);
271 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
272 EXPECT_EQ(1, payload_state.GetUrlIndex());
273 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800274 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800275
276 // This should advance the URL index and payload attempt number due to
277 // wrap-around of URL index.
278 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMissingError);
279 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
280 EXPECT_EQ(0, payload_state.GetUrlIndex());
281 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800282 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800283
284 // This HTTP error code should only increase the failure count.
285 payload_state.UpdateFailed(static_cast<ActionExitCode>(
286 kActionCodeOmahaRequestHTTPResponseBase + 404));
287 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
288 EXPECT_EQ(0, payload_state.GetUrlIndex());
289 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800290 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800291
292 // And that failure count should be reset when we download some bytes
293 // afterwards.
294 payload_state.DownloadProgress(100);
295 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
296 EXPECT_EQ(0, payload_state.GetUrlIndex());
297 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800298 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800299
300 // Now, slightly change the response and set it again.
301 SetupPayloadStateWith2Urls("Hash8532", &payload_state, &response);
302
303 // Make sure the url index was reset to 0 because of the new response.
304 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
305 EXPECT_EQ(0, payload_state.GetUrlIndex());
306 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800307 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800308}
309
310TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDownload) {
311 OmahaResponse response;
312 PayloadState payload_state;
313 NiceMock<PrefsMock> prefs;
314
315 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)).Times(1);
316 EXPECT_CALL(prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)).Times(1);
317
Jay Srinivasan08262882012-12-28 19:29:43 -0800318 EXPECT_CALL(prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(2);
319
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800320 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(1);
321 EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)).Times(1);
322
323 EXPECT_TRUE(payload_state.Initialize(&prefs));
324
325 SetupPayloadStateWith2Urls("Hash8593", &payload_state, &response);
326
327 // This should just advance the payload attempt number;
328 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
329 payload_state.DownloadComplete();
330 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
331 EXPECT_EQ(0, payload_state.GetUrlIndex());
332 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
333}
334
335TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
336 OmahaResponse response;
337 PayloadState payload_state;
338 NiceMock<PrefsMock> prefs;
339
340 EXPECT_TRUE(payload_state.Initialize(&prefs));
341 SetupPayloadStateWith2Urls("Hash4427", &payload_state, &response);
342
343 // Generate enough events to advance URL index, failure count and
344 // payload attempt number all to 1.
345 payload_state.DownloadComplete();
346 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMismatch);
347 payload_state.UpdateFailed(kActionCodeDownloadTransferError);
348 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
349 EXPECT_EQ(1, payload_state.GetUrlIndex());
350 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
351
352 // Now, simulate a corrupted url index on persisted store which gets
353 // loaded when update_engine restarts. Using a different prefs object
354 // so as to not bother accounting for the uninteresting calls above.
355 NiceMock<PrefsMock> prefs2;
356 EXPECT_CALL(prefs2, Exists(_)).WillRepeatedly(Return(true));
357 EXPECT_CALL(prefs2, GetInt64(kPrefsPayloadAttemptNumber, _));
Jay Srinivasan08262882012-12-28 19:29:43 -0800358 EXPECT_CALL(prefs2, GetInt64(kPrefsBackoffExpiryTime, _));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800359 EXPECT_CALL(prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
360 .WillOnce(DoAll(SetArgumentPointee<1>(2), Return(true)));
361 EXPECT_CALL(prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _));
362
363 // Note: This will be a different payload object, but the response should
364 // have the same hash as before so as to not trivially reset because the
365 // response was different. We want to specifically test that even if the
366 // response is same, we should reset the state if we find it corrupted.
367 EXPECT_TRUE(payload_state.Initialize(&prefs2));
368 SetupPayloadStateWith2Urls("Hash4427", &payload_state, &response);
369
370 // Make sure all counters get reset to 0 because of the corrupted URL index
371 // we supplied above.
372 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
373 EXPECT_EQ(0, payload_state.GetUrlIndex());
374 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
375}
Jay Srinivasan08262882012-12-28 19:29:43 -0800376
377TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
378 OmahaResponse response;
379 response.is_delta_payload = true;
380 PayloadState payload_state;
381 NiceMock<PrefsMock> prefs;
382
383 EXPECT_TRUE(payload_state.Initialize(&prefs));
384 SetupPayloadStateWith2Urls("Hash6437", &payload_state, &response);
385
386 // Simulate a successful download and see that we're ready to download
387 // again without any backoff as this is a delta payload.
388 payload_state.DownloadComplete();
389 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
390 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
391
392 // Simulate two failures (enough to cause payload backoff) and check
393 // again that we're ready to re-download without any backoff as this is
394 // a delta payload.
395 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMismatch);
396 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMismatch);
397 EXPECT_EQ(0, payload_state.GetUrlIndex());
398 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
399 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
400}
401
402static void CheckPayloadBackoffState(PayloadState* payload_state,
403 int expected_attempt_number,
404 TimeDelta expected_days) {
405 payload_state->DownloadComplete();
406 EXPECT_EQ(expected_attempt_number, payload_state->GetPayloadAttemptNumber());
407 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
408 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
409 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
410 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
411 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
412 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
413 EXPECT_LT(expected_min_time.ToInternalValue(),
414 backoff_expiry_time.ToInternalValue());
415 EXPECT_GT(expected_max_time.ToInternalValue(),
416 backoff_expiry_time.ToInternalValue());
417}
418
419TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
420 OmahaResponse response;
421 response.is_delta_payload = false;
422 PayloadState payload_state;
423 NiceMock<PrefsMock> prefs;
424
425 EXPECT_TRUE(payload_state.Initialize(&prefs));
426 SetupPayloadStateWith2Urls("Hash8939", &payload_state, &response);
427
428 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
429 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
430 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
431 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
432 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
433 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
434 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
435 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
436 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
437 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
438}
439
440TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
441 OmahaResponse response;
442 response.disable_payload_backoff = true;
443 PayloadState payload_state;
444 NiceMock<PrefsMock> prefs;
445
446 EXPECT_TRUE(payload_state.Initialize(&prefs));
447 SetupPayloadStateWith2Urls("Hash8939", &payload_state, &response);
448
449 // Simulate a successful download and see that we are ready to download
450 // again without any backoff.
451 payload_state.DownloadComplete();
452 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
453 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
454
455 // Test again, this time by simulating two errors that would cause
456 // the payload attempt number to increment due to wrap around. And
457 // check that we are still ready to re-download without any backoff.
458 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMismatch);
459 payload_state.UpdateFailed(kActionCodeDownloadMetadataSignatureMismatch);
460 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
461 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
462}
463
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800464}