blob: 20d321cbbdb6b2dfd2bdb949fdaf22cb334f5279 [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
Alex Vakulenko75039d72014-03-25 12:36:28 -07007#include <base/files/file_path.h>
Chris Sosabe45bef2013-04-09 18:25:12 -07008#include "base/file_util.h"
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/strings/stringprintf.h>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080010#include "gmock/gmock.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080011#include "gtest/gtest.h"
12
Jay Srinivasand29695d2013-04-08 15:08:05 -070013#include "update_engine/constants.h"
David Zeuthenf413fe52013-04-22 14:04:39 -070014#include "update_engine/fake_clock.h"
Alex Deymo42432912013-07-12 20:21:15 -070015#include "update_engine/fake_hardware.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070016#include "update_engine/fake_system_state.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080017#include "update_engine/omaha_request_action.h"
18#include "update_engine/payload_state.h"
David Zeuthenf413fe52013-04-22 14:04:39 -070019#include "update_engine/prefs.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080020#include "update_engine/prefs_mock.h"
21#include "update_engine/test_utils.h"
22#include "update_engine/utils.h"
23
Jay Srinivasan08262882012-12-28 19:29:43 -080024using base::Time;
25using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026using std::string;
27using testing::_;
Alex Deymo42432912013-07-12 20:21:15 -070028using testing::AnyNumber;
29using testing::AtLeast;
30using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080031using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080032using testing::Return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080033using testing::SetArgumentPointee;
34
35namespace chromeos_update_engine {
36
Jay Srinivasan19409b72013-04-12 19:23:36 -070037const char* kCurrentBytesDownloadedFromHttps =
38 "current-bytes-downloaded-from-HttpsServer";
39const char* kTotalBytesDownloadedFromHttps =
40 "total-bytes-downloaded-from-HttpsServer";
41const char* kCurrentBytesDownloadedFromHttp =
42 "current-bytes-downloaded-from-HttpServer";
43const char* kTotalBytesDownloadedFromHttp =
44 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070045const char* kCurrentBytesDownloadedFromHttpPeer =
46 "current-bytes-downloaded-from-HttpPeer";
47const char* kTotalBytesDownloadedFromHttpPeer =
48 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070049
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080050static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070051 bool http_enabled,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080052 PayloadState* payload_state,
53 OmahaResponse* response) {
54 response->payload_urls.clear();
55 response->payload_urls.push_back("http://test");
56 response->payload_urls.push_back("https://test");
57 response->size = 523456789;
58 response->hash = hash;
59 response->metadata_size = 558123;
60 response->metadata_signature = "metasign";
61 response->max_failure_count_per_url = 3;
62 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080063 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070064
65 string expected_url_https_only =
66 "NumURLs = 1\n"
67 "Candidate Url0 = https://test\n";
68
69 string expected_urls_both =
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080070 "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -070071 "Candidate Url0 = http://test\n"
72 "Candidate Url1 = https://test\n";
73
74 string expected_response_sign =
75 (http_enabled ? expected_urls_both : expected_url_https_only) +
Alex Vakulenko75039d72014-03-25 12:36:28 -070076 base::StringPrintf("Payload Size = 523456789\n"
77 "Payload Sha256 Hash = %s\n"
78 "Metadata Size = 558123\n"
79 "Metadata Signature = metasign\n"
80 "Is Delta Payload = %d\n"
81 "Max Failure Count Per Url = %d\n"
82 "Disable Payload Backoff = %d\n",
83 hash.c_str(),
84 response->is_delta_payload,
85 response->max_failure_count_per_url,
86 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -080087 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080088}
89
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080090class PayloadStateTest : public ::testing::Test { };
91
David Zeuthena99981f2013-04-29 13:42:47 -070092TEST(PayloadStateTest, DidYouAddANewErrorCode) {
Don Garrett4d039442013-10-28 18:40:06 -070093 if (kErrorCodeUmaReportedMax != 46) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080094 LOG(ERROR) << "The following failure is intentional. If you added a new "
David Zeuthena99981f2013-04-29 13:42:47 -070095 << "ErrorCode enum value, make sure to add it to the "
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080096 << "PayloadState::UpdateFailed method and then update this test "
David Zeuthena99981f2013-04-29 13:42:47 -070097 << "to the new value of kErrorCodeUmaReportedMax, which is "
98 << kErrorCodeUmaReportedMax;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080099 EXPECT_FALSE("Please see the log line above");
100 }
101}
102
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800103TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
104 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700105 FakeSystemState fake_system_state;
106 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700107 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700108 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
109 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700110 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
111 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700112 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
113 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
114 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
115 .Times(AtLeast(1));
116 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
117 .Times(AtLeast(1));
118 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
119 .Times(AtLeast(1));
120 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
121 .Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
123 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700124 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
125 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700126 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800127 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700128 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800129 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800130 string stored_response_sign = payload_state.GetResponseSignature();
131 string expected_response_sign = "NumURLs = 0\n"
132 "Payload Size = 0\n"
133 "Payload Sha256 Hash = \n"
134 "Metadata Size = 0\n"
135 "Metadata Signature = \n"
136 "Is Delta Payload = 0\n"
137 "Max Failure Count Per Url = 0\n"
138 "Disable Payload Backoff = 0\n";
139 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700140 EXPECT_EQ("", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800141 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700142 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700143 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800144}
145
146TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
147 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700148 response.payload_urls.push_back("https://single.url.test");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800149 response.size = 123456789;
150 response.hash = "hash";
151 response.metadata_size = 58123;
152 response.metadata_signature = "msign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700153 FakeSystemState fake_system_state;
154 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700155 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700156 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
157 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700158 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
159 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700160 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
161 .Times(AtLeast(1));
162 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
163 .Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
165 .Times(AtLeast(1));
166 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
167 .Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
169 .Times(AtLeast(1));
170 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
171 .Times(AtLeast(1));
172 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
173 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700174 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
175 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700176 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
177 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700179 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800181 string stored_response_sign = payload_state.GetResponseSignature();
182 string expected_response_sign = "NumURLs = 1\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700183 "Candidate Url0 = https://single.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800184 "Payload Size = 123456789\n"
185 "Payload Sha256 Hash = hash\n"
186 "Metadata Size = 58123\n"
187 "Metadata Signature = msign\n"
188 "Is Delta Payload = 0\n"
189 "Max Failure Count Per Url = 0\n"
190 "Disable Payload Backoff = 0\n";
191 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700192 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800193 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700194 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700195 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800196}
197
198TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
199 OmahaResponse response;
200 response.payload_urls.push_back("http://multiple.url.test");
201 response.payload_urls.push_back("https://multiple.url.test");
202 response.size = 523456789;
203 response.hash = "rhash";
204 response.metadata_size = 558123;
205 response.metadata_signature = "metasign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700206 FakeSystemState fake_system_state;
207 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700208 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700209 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
210 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700211 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
212 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700213 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
214 .Times(AtLeast(1));
215 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
216 .Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
222 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
224 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700225 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
226 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700227
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700229 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800231 string stored_response_sign = payload_state.GetResponseSignature();
232 string expected_response_sign = "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700233 "Candidate Url0 = http://multiple.url.test\n"
234 "Candidate Url1 = https://multiple.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800235 "Payload Size = 523456789\n"
236 "Payload Sha256 Hash = rhash\n"
237 "Metadata Size = 558123\n"
238 "Metadata Signature = metasign\n"
239 "Is Delta Payload = 0\n"
240 "Max Failure Count Per Url = 0\n"
241 "Disable Payload Backoff = 0\n";
242 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700243 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800244 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700245 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700246 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800247}
248
249TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
250 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700251 FakeSystemState fake_system_state;
252 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800254
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700255 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700257 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
258 .Times(AtLeast(1));
259 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
260 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700261 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
262 .Times(AtLeast(1));
263 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
264 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700265 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700266
Chris Sosabe45bef2013-04-09 18:25:12 -0700267 // Reboots will be set
268 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
269
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800270 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700271 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
272 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800273
274 // Failure count should be called each times url index is set, so that's
275 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700276 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
277 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800278
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700279 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800280
281 // This does a SetResponse which causes all the states to be set to 0 for
282 // the first time.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700283 SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response);
284 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800285
286 // Verify that on the first error, the URL index advances to 1.
David Zeuthena99981f2013-04-29 13:42:47 -0700287 ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800288 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700289 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290
291 // Verify that on the next error, the URL index wraps around to 0.
292 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700293 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294
295 // Verify that on the next error, it again advances to 1.
296 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700297 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700298
299 // Verify that we switched URLs three times
300 EXPECT_EQ(3, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800301}
302
303TEST(PayloadStateTest, NewResponseResetsPayloadState) {
304 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700305 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800307
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700308 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800311 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700312 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800313 .Times(AnyNumber());
314
Alex Deymob33b0f02013-08-08 21:10:02 -0700315 // The first response doesn't send an abandoned event.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700316 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymob33b0f02013-08-08 21:10:02 -0700317 "Installer.UpdatesAbandonedEventCount", 0, _, _, _)).Times(0);
318
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800319 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700320 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700321 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322
323 // Advance the URL index to 1 by faking an error.
David Zeuthena99981f2013-04-29 13:42:47 -0700324 ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700326 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700327 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800328
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700329 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymob33b0f02013-08-08 21:10:02 -0700330 "Installer.UpdatesAbandonedEventCount", 1, _, _, _));
331
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800332 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700333 SetupPayloadStateWith2Urls("Hash8225", true, &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());
339 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
340
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700341 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymob33b0f02013-08-08 21:10:02 -0700342 "Installer.UpdatesAbandonedEventCount", 2, _, _, _));
343
344 // Return a third different response.
345 SetupPayloadStateWith2Urls("Hash9999", true, &payload_state, &response);
346 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
347
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800348 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700349 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800350 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700351 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700352 EXPECT_EQ(0,
353 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
354 EXPECT_EQ(0,
355 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
356 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
357 kDownloadSourceHttpsServer));
358 EXPECT_EQ(0,
359 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800360}
361
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800362TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
363 OmahaResponse response;
364 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700365 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700366 int progress_bytes = 100;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700367 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800368
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700369 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700370 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
371 .Times(AtLeast(2));
372 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
373 .Times(AtLeast(1));
374 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
375 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800376
Alex Deymo820cc702013-06-28 15:43:46 -0700377 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
378 .Times(AtLeast(2));
379 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
380 .Times(AtLeast(1));
381 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
382 .Times(AtLeast(1));
383
Jay Srinivasan19409b72013-04-12 19:23:36 -0700384 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800385
Jay Srinivasan19409b72013-04-12 19:23:36 -0700386 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
387 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800388
Jay Srinivasan19409b72013-04-12 19:23:36 -0700389 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
390 .Times(AtLeast(7));
391 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
392 .Times(AtLeast(2));
393 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
394 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800395
Jay Srinivasan19409b72013-04-12 19:23:36 -0700396 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
397 .Times(AtLeast(1));
398 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
399 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700400
Jay Srinivasan19409b72013-04-12 19:23:36 -0700401 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
402 .Times(AtLeast(1));
403 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
404 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700405 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
406 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700407 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
408 .Times(AtLeast(1));
409 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
410 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700411 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
412 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700413
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700414 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800415
Jay Srinivasan53173b92013-05-17 17:13:01 -0700416 SetupPayloadStateWith2Urls("Hash5873", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700417 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800418
419 // This should advance the URL index.
David Zeuthena99981f2013-04-29 13:42:47 -0700420 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800421 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700422 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700423 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800424 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700425 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800426
427 // This should advance the failure count only.
David Zeuthena99981f2013-04-29 13:42:47 -0700428 payload_state.UpdateFailed(kErrorCodeDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800429 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700430 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700431 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800432 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700433 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800434
435 // This should advance the failure count only.
David Zeuthena99981f2013-04-29 13:42:47 -0700436 payload_state.UpdateFailed(kErrorCodeDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800437 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700438 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700439 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800440 EXPECT_EQ(2, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700441 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800442
443 // This should advance the URL index as we've reached the
444 // max failure count and reset the failure count for the new URL index.
445 // This should also wrap around the URL index and thus cause the payload
446 // attempt number to be incremented.
David Zeuthena99981f2013-04-29 13:42:47 -0700447 payload_state.UpdateFailed(kErrorCodeDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800448 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700449 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700450 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800451 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700452 EXPECT_EQ(2, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800453 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800454
455 // This should advance the URL index.
David Zeuthena99981f2013-04-29 13:42:47 -0700456 payload_state.UpdateFailed(kErrorCodePayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800457 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700458 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700459 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800460 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700461 EXPECT_EQ(3, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800462 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800463
464 // This should advance the URL index and payload attempt number due to
465 // wrap-around of URL index.
David Zeuthena99981f2013-04-29 13:42:47 -0700466 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800467 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700468 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700469 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800470 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700471 EXPECT_EQ(4, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800472 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800473
474 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700475 payload_state.UpdateFailed(static_cast<ErrorCode>(
476 kErrorCodeOmahaRequestHTTPResponseBase + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800477 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700478 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700479 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800480 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700481 EXPECT_EQ(4, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800482 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800483
484 // And that failure count should be reset when we download some bytes
485 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700486 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800487 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700488 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700489 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800490 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700491 EXPECT_EQ(4, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800492 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800493
494 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700495 SetupPayloadStateWith2Urls("Hash8532", true, &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());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800502 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700503 EXPECT_EQ(0, 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;
Alex Deymo820cc702013-06-28 15:43:46 -0700509 response.is_delta_payload = false;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800510 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700511 FakeSystemState fake_system_state;
512 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800513
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700514 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700515 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
516 .Times(AtLeast(1));
517 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
518 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800519
Alex Deymo820cc702013-06-28 15:43:46 -0700520 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
521 .Times(AtLeast(1));
522 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
523 .Times(AtLeast(1));
524
Jay Srinivasan19409b72013-04-12 19:23:36 -0700525 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
526 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800527
Jay Srinivasan19409b72013-04-12 19:23:36 -0700528 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
529 .Times(AtLeast(1));
530 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
531 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800532
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700533 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800534
Jay Srinivasan53173b92013-05-17 17:13:01 -0700535 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800536
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700537 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700538 "Installer.PayloadAttemptNumber", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700539 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700540 "Installer.FullPayloadAttemptNumber", 1, _, _, _));
541
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800542 // This should just advance the payload attempt number;
543 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700544 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800545 payload_state.DownloadComplete();
546 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700547 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
548 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
549 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
550 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
551}
552
553TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
554 OmahaResponse response;
555 response.is_delta_payload = true;
556 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700557 FakeSystemState fake_system_state;
558 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700559
560 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber());
561 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
562 .Times(AtLeast(1));
563 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
564 .Times(AtLeast(1));
565
566 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
567 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
568 .Times(AtLeast(1));
569
570 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
571 .Times(1);
572
573 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
574 .Times(AtLeast(1));
575 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
576 .Times(AtLeast(1));
577
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700578 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700579
580 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
581
Alex Deymo29b51d92013-07-09 15:26:24 -0700582 // Metrics for Full payload attempt number is not sent with Delta payloads.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700583 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700584 "Installer.PayloadAttemptNumber", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700586 "Installer.FullPayloadAttemptNumber", _, _, _, _))
587 .Times(0);
588
Alex Deymo820cc702013-06-28 15:43:46 -0700589 // This should just advance the payload attempt number;
590 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
591 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
592 payload_state.DownloadComplete();
593 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
594 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700595 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800596 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700597 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800598}
599
600TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
601 OmahaResponse response;
602 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700603 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800604
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700605 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700606 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800607
608 // Generate enough events to advance URL index, failure count and
609 // payload attempt number all to 1.
610 payload_state.DownloadComplete();
David Zeuthena99981f2013-04-29 13:42:47 -0700611 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
612 payload_state.UpdateFailed(kErrorCodeDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800613 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700614 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700615 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800616 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700617 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800618
619 // Now, simulate a corrupted url index on persisted store which gets
620 // loaded when update_engine restarts. Using a different prefs object
621 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700622 FakeSystemState fake_system_state2;
623 NiceMock<PrefsMock>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700624 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
625 EXPECT_CALL(*prefs2, GetInt64(_,_)).Times(AtLeast(1));
626 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
627 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700628 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
629 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700630 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
631 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
632 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
633 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700634 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
635 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800636
637 // Note: This will be a different payload object, but the response should
638 // have the same hash as before so as to not trivially reset because the
639 // response was different. We want to specifically test that even if the
640 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700641 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700642 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800643
644 // Make sure all counters get reset to 0 because of the corrupted URL index
645 // we supplied above.
646 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700647 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700648 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800649 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700650 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800651}
Jay Srinivasan08262882012-12-28 19:29:43 -0800652
Chris Sosa20f005c2013-09-05 13:53:08 -0700653TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
654 OmahaResponse response;
655 response.is_delta_payload = false;
656 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700657 FakeSystemState fake_system_state;
658 OmahaRequestParams params(&fake_system_state);
Chris Sosa20f005c2013-09-05 13:53:08 -0700659 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700660 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700661
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700662 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700663 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
664
665 // Simulate two failures (enough to cause payload backoff) and check
666 // again that we're ready to re-download without any backoff as this is
667 // an interactive check.
668 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
669 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
670 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
671 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
672 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
673 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
674}
675
676TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
677 OmahaResponse response;
678 response.is_delta_payload = false;
679 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700680 FakeSystemState fake_system_state;
681 OmahaRequestParams params(&fake_system_state);
Chris Sosa20f005c2013-09-05 13:53:08 -0700682 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700683 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700684
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700685 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700686 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
687
688 // Simulate two failures (enough to cause payload backoff) and check
689 // again that we're ready to re-download without any backoff as this is
690 // an interactive check.
691 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
692 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
693 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
694 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
695 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
696 // Set p2p url.
697 params.set_use_p2p_for_downloading(true);
698 params.set_p2p_url("http://mypeer:52909/path/to/file");
699 // Should not backoff for p2p updates.
700 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
701
702 params.set_p2p_url("");
703 // No actual p2p update if no url is provided.
704 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
705}
706
Jay Srinivasan08262882012-12-28 19:29:43 -0800707TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
708 OmahaResponse response;
709 response.is_delta_payload = true;
710 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700711 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800712
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700713 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700714 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800715
716 // Simulate a successful download and see that we're ready to download
717 // again without any backoff as this is a delta payload.
718 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700719 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
720 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800721 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
722
723 // Simulate two failures (enough to cause payload backoff) and check
724 // again that we're ready to re-download without any backoff as this is
725 // a delta payload.
David Zeuthena99981f2013-04-29 13:42:47 -0700726 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
727 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700728 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700729 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
730 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800731 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
732}
733
734static void CheckPayloadBackoffState(PayloadState* payload_state,
735 int expected_attempt_number,
736 TimeDelta expected_days) {
737 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700738 EXPECT_EQ(expected_attempt_number,
739 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800740 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
741 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
742 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
743 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
744 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
745 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
746 EXPECT_LT(expected_min_time.ToInternalValue(),
747 backoff_expiry_time.ToInternalValue());
748 EXPECT_GT(expected_max_time.ToInternalValue(),
749 backoff_expiry_time.ToInternalValue());
750}
751
752TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
753 OmahaResponse response;
754 response.is_delta_payload = false;
755 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700756 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800757
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700758 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700759 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800760
761 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
762 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
763 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
764 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
765 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
766 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
767 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
768 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
769 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
770 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
771}
772
773TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
774 OmahaResponse response;
775 response.disable_payload_backoff = true;
776 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700777 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800778
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700779 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700780 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800781
782 // Simulate a successful download and see that we are ready to download
783 // again without any backoff.
784 payload_state.DownloadComplete();
785 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700786 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800787 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
788
789 // Test again, this time by simulating two errors that would cause
790 // the payload attempt number to increment due to wrap around. And
791 // check that we are still ready to re-download without any backoff.
David Zeuthena99981f2013-04-29 13:42:47 -0700792 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
793 payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800794 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700795 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800796 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
797}
798
Jay Srinivasan19409b72013-04-12 19:23:36 -0700799TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
800 OmahaResponse response;
801 response.disable_payload_backoff = true;
802 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700803 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700804 int https_total = 0;
805 int http_total = 0;
806
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700807 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700808 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700809 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700810
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700811 // Simulate a previous attempt with in order to set an initial non-zero value
812 // for the total bytes downloaded for HTTP.
813 int prev_chunk = 323456789;
814 http_total += prev_chunk;
815 payload_state.DownloadProgress(prev_chunk);
816
817 // Ensure that the initial values for HTTP reflect this attempt.
818 EXPECT_EQ(prev_chunk,
819 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
820 EXPECT_EQ(http_total,
821 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
822
823 // Change the response hash so as to simulate a new response which will
824 // reset the current bytes downloaded, but not the total bytes downloaded.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700825 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700826 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700827
828 // First, simulate successful download of a few bytes over HTTP.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700829 int first_chunk = 5000000;
830 http_total += first_chunk;
831 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700832 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700833 EXPECT_EQ(first_chunk,
834 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
835 EXPECT_EQ(http_total,
836 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
837 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
838 kDownloadSourceHttpsServer));
839 EXPECT_EQ(https_total,
840 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
841
842 // Simulate an error that'll cause the url index to point to https.
David Zeuthena99981f2013-04-29 13:42:47 -0700843 ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700844 payload_state.UpdateFailed(error);
845
Jay Srinivasan53173b92013-05-17 17:13:01 -0700846 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700847 int second_chunk = 23456789;
848 https_total += second_chunk;
849 payload_state.DownloadProgress(second_chunk);
850 EXPECT_EQ(first_chunk,
851 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
852 EXPECT_EQ(http_total,
853 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
854 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
855 kDownloadSourceHttpsServer));
856 EXPECT_EQ(https_total,
857 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
858
859 // Simulate error to go back to http.
860 payload_state.UpdateFailed(error);
861 int third_chunk = 32345678;
862 int http_chunk = first_chunk + third_chunk;
863 http_total += third_chunk;
864 int https_chunk = second_chunk;
865 payload_state.DownloadProgress(third_chunk);
866
867 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
868 EXPECT_EQ(http_chunk,
869 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700870 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700871 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
872 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
873 kDownloadSourceHttpsServer));
874 EXPECT_EQ(https_total,
875 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
876
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700877 // Simulate error (will cause URL switch), set p2p is to be used and
878 // then do 42MB worth of progress
879 payload_state.UpdateFailed(error);
880 payload_state.SetUsingP2PForDownloading(true);
881 int p2p_total = 42 * 1000 * 1000;
882 payload_state.DownloadProgress(p2p_total);
883
884 EXPECT_EQ(p2p_total,
885 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
886
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700887 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700888 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700889 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800890 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700891 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700892 "Installer.SuccessfulMBsDownloadedFromHttpServer",
893 http_chunk / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700894 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700895 "Installer.TotalMBsDownloadedFromHttpServer",
896 http_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700897 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700898 "Installer.SuccessfulMBsDownloadedFromHttpsServer",
899 https_chunk / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700900 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700901 "Installer.TotalMBsDownloadedFromHttpsServer",
902 https_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700903 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700904 "Installer.SuccessfulMBsDownloadedFromHttpPeer",
905 p2p_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700906 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700907 "Installer.TotalMBsDownloadedFromHttpPeer",
908 p2p_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700909 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthencc6f9962013-04-18 11:57:24 -0700910 "Installer.UpdateURLSwitches",
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700911 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700912 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800913 metrics::kMetricSuccessfulUpdateUrlSwitchCount,
914 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700915 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen674c3182013-04-18 14:05:20 -0700916 "Installer.UpdateDurationMinutes",
917 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700918 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800919 metrics::kMetricSuccessfulUpdateTotalDurationMinutes,
920 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700921 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen674c3182013-04-18 14:05:20 -0700922 "Installer.UpdateDurationUptimeMinutes",
923 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700924 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700925 "Installer.DownloadSourcesUsed",
926 (1 << kDownloadSourceHttpsServer) | (1 << kDownloadSourceHttpServer) |
927 (1 << kDownloadSourceHttpPeer),
928 _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700929 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700930 "Installer.DownloadOverheadPercentage", 318, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700931 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800932 metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
933 314, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700934 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -0700935 "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700936 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800937 metrics::kMetricAttemptPayloadType, kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700938 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800939 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
940 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700941 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo820cc702013-06-28 15:43:46 -0700942 "Installer.AttemptsCount.Total", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700943 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800944 metrics::kMetricSuccessfulUpdateAttemptCount, 1, _, _, _));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700945
946 payload_state.UpdateSucceeded();
947
948 // Make sure the metrics are reset after a successful update.
949 EXPECT_EQ(0,
950 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
951 EXPECT_EQ(0,
952 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
953 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
954 kDownloadSourceHttpsServer));
955 EXPECT_EQ(0,
956 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700957 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700958}
959
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700960TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
961 OmahaResponse response;
962 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700963 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700964
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700965 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700966 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
967
968 // Simulate progress in order to mark HTTP as one of the sources used.
969 int num_bytes = 42 * 1000 * 1000;
970 payload_state.DownloadProgress(num_bytes);
971
972 // Check that this was done via HTTP.
973 EXPECT_EQ(num_bytes,
974 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
975 EXPECT_EQ(num_bytes,
976 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
977
978 // Check that only HTTP is reported as a download source.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700979 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700980 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700981 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700982 "Installer.DownloadSourcesUsed",
983 (1 << kDownloadSourceHttpServer),
984 _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700985 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800986 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed,
987 (1 << kDownloadSourceHttpServer),
988 _, _, _));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700989
990 payload_state.UpdateSucceeded();
991}
992
Jay Srinivasan19409b72013-04-12 19:23:36 -0700993TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
994 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700995 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700996 PayloadState payload_state;
997
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700998 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700999
1000 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001001 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001002
1003 int num_bytes = 10000;
1004 payload_state.DownloadProgress(num_bytes);
1005 EXPECT_EQ(num_bytes,
1006 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
1007 EXPECT_EQ(num_bytes,
1008 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
1009 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
1010 kDownloadSourceHttpsServer));
1011 EXPECT_EQ(0,
1012 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
1013
1014 payload_state.UpdateRestarted();
1015 // Make sure the current bytes downloaded is reset, but not the total bytes.
1016 EXPECT_EQ(0,
1017 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
1018 EXPECT_EQ(num_bytes,
1019 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
1020}
1021
Chris Sosabe45bef2013-04-09 18:25:12 -07001022TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001023 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -07001024 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -07001025
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001026 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Chris Sosabe45bef2013-04-09 18:25:12 -07001027 EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AtLeast(0));
1028 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
1029
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001030 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -07001031
1032 payload_state.UpdateRestarted();
1033 EXPECT_EQ(0, payload_state.GetNumReboots());
1034
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001035 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -07001036 payload_state.UpdateResumed();
1037 // Num reboots should be incremented because system rebooted detected.
1038 EXPECT_EQ(1, payload_state.GetNumReboots());
1039
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001040 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -07001041 payload_state.UpdateResumed();
1042 // Num reboots should now be 1 as reboot was not detected.
1043 EXPECT_EQ(1, payload_state.GetNumReboots());
1044
1045 // Restart the update again to verify we set the num of reboots back to 0.
1046 payload_state.UpdateRestarted();
1047 EXPECT_EQ(0, payload_state.GetNumReboots());
1048}
Jay Srinivasan19409b72013-04-12 19:23:36 -07001049
Chris Sosaaa18e162013-06-20 13:20:30 -07001050TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001051 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -07001052 PayloadState payload_state;
1053
1054 NiceMock<PrefsMock>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001055 fake_system_state.mock_powerwash_safe_prefs();
1056 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -07001057
1058 // Verify pre-conditions are good.
1059 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1060
1061 // Mock out the os version and make sure it's blacklisted correctly.
1062 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001063 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001064 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001065 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001066
1067 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1068 rollback_version));
1069 payload_state.Rollback();
1070
1071 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001072
1073 // Change it up a little and verify we load it correctly.
1074 rollback_version = "2345.0.1";
1075 // Let's verify we can reload it correctly.
1076 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1077 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1078 SetArgumentPointee<1>(rollback_version), Return(true)));
1079 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1080 rollback_version));
1081 payload_state.LoadRollbackVersion();
1082 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosaaa18e162013-06-20 13:20:30 -07001083}
1084
David Zeuthenf413fe52013-04-22 14:04:39 -07001085TEST(PayloadStateTest, DurationsAreCorrect) {
1086 OmahaResponse response;
1087 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001088 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001089 FakeClock fake_clock;
1090 Prefs prefs;
1091 string temp_dir;
1092
1093 // Set the clock to a well-known time - 1 second on the wall-clock
1094 // and 2 seconds on the monotonic clock
1095 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1096 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1097
1098 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001099 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateDurationTests.XXXXXX",
David Zeuthenf413fe52013-04-22 14:04:39 -07001100 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001101 prefs.Init(base::FilePath(temp_dir));
David Zeuthenf413fe52013-04-22 14:04:39 -07001102
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001103 fake_system_state.set_clock(&fake_clock);
1104 fake_system_state.set_prefs(&prefs);
1105 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001106
1107 // Check that durations are correct for a successful update where
1108 // time has advanced 7 seconds on the wall clock and 4 seconds on
1109 // the monotonic clock.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001110 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001111 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1112 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1113 payload_state.UpdateSucceeded();
1114 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1115 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1116
1117 // Check that durations are reset when a new response comes in.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001118 SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001119 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1120 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1121
1122 // Advance time a bit (10 secs), simulate download progress and
1123 // check that durations are updated.
1124 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1125 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1126 payload_state.DownloadProgress(10);
1127 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1128 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1129
1130 // Now simulate a reboot by resetting monotonic time (to 5000) and
1131 // creating a new PayloadState object and check that we load the
1132 // durations correctly (e.g. they are the same as before).
1133 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1134 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001135 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001136 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
1137 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),10000000);
1138
1139 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1140 // and check that the durations are increased accordingly.
1141 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1142 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1143 payload_state2.UpdateSucceeded();
1144 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
1145 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),16000000);
1146
1147 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1148}
1149
David Zeuthene4c58bf2013-06-18 17:26:50 -07001150TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1151 OmahaResponse response;
1152 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001153 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001154 FakeClock fake_clock;
1155 Prefs prefs;
1156 string temp_dir;
1157
1158 // Set the clock to a well-known time (t = 30 seconds).
1159 fake_clock.SetWallclockTime(Time::FromInternalValue(
1160 30 * Time::kMicrosecondsPerSecond));
1161
1162 // We need persistent preferences for this test
1163 EXPECT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -08001164 "RebootAfterSuccessfulUpdateTest.XXXXXX", &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001165 prefs.Init(base::FilePath(temp_dir));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001166
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001167 fake_system_state.set_clock(&fake_clock);
1168 fake_system_state.set_prefs(&prefs);
1169 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001170
1171 // Make the update succeed.
1172 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1173 payload_state.UpdateSucceeded();
1174
1175 // Check that the marker was written.
1176 EXPECT_TRUE(prefs.Exists(kPrefsSystemUpdatedMarker));
1177
1178 // Now simulate a reboot and set the wallclock time to a later point
1179 // (t = 500 seconds). We do this by using a new PayloadState object
1180 // and checking that it emits the right UMA metric with the right
1181 // value.
1182 fake_clock.SetWallclockTime(Time::FromInternalValue(
1183 500 * Time::kMicrosecondsPerSecond));
1184 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001185 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001186
1187 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001188 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthene4c58bf2013-06-18 17:26:50 -07001189 "Installer.TimeToRebootMinutes",
1190 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001191 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001192 metrics::kMetricTimeToRebootMinutes,
1193 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001194 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001195
1196 payload_state2.UpdateEngineStarted();
1197
1198 // Check that the marker was nuked.
1199 EXPECT_FALSE(prefs.Exists(kPrefsSystemUpdatedMarker));
1200
1201 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1202}
1203
Alex Deymo569c4242013-07-24 12:01:01 -07001204TEST(PayloadStateTest, RestartAfterCrash) {
1205 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001206 FakeSystemState fake_system_state;
1207 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001208
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001209 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001210
1211 // No prefs should be used after a crash.
1212 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1213 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1214 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1215 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1216 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1217 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1218 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
1219
1220 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001221 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001222 SendToUMA(_, _, _, _, _)).Times(0);
1223
1224 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001225 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001226
1227 payload_state.UpdateEngineStarted();
1228}
1229
Jay Srinivasan53173b92013-05-17 17:13:01 -07001230TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1231 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001232 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001233 PayloadState payload_state;
1234
Jay Srinivasan53173b92013-05-17 17:13:01 -07001235 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001236 fake_system_state.set_device_policy(&disable_http_policy);
1237 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001238
1239 // Test with no device policy. Should default to allowing http.
1240 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1241 .WillRepeatedly(Return(false));
1242
1243 // Set the first response.
1244 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1245
1246 // Check that we use the HTTP URL since there is no value set for allowing
1247 // http.
1248 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1249
1250 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001251 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1252 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1253
Chris Sosaf7d80042013-08-22 16:45:17 -07001254 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001255 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1256
1257 // Check that we skip the HTTP URL and use only the HTTPS url.
1258 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1259
1260 // Advance the URL index to 1 by faking an error.
1261 ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch;
1262 payload_state.UpdateFailed(error);
1263
1264 // Check that we still skip the HTTP URL and use only the HTTPS url.
1265 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1266 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
1267
1268 // Now, slightly change the response and set it again.
1269 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1270
1271 // Check that we still skip the HTTP URL and use only the HTTPS url.
1272 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1273
1274 // Now, pretend that the HTTP policy is turned on. We want to make sure
1275 // the new policy is honored.
1276 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001277 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001278 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1279 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1280
1281 // Now, set the same response using the same hash
1282 // so that we can test that the state is reset not because of the
1283 // hash but because of the policy change which results in candidate url
1284 // list change.
1285 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1286
1287 // Check that we use the HTTP URL now and the failure count is reset.
1288 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1289 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1290
1291 // Fake a failure and see if we're moving over to the HTTPS url and update
1292 // the URL switch count properly.
1293 payload_state.UpdateFailed(error);
1294 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1295 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
1296 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1297}
1298
Alex Deymo1c656c42013-06-28 11:02:14 -07001299TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1300 OmahaResponse response;
1301 response.is_delta_payload = true;
1302 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001303 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001304
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001305 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001306 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1307
1308 // Simulate a successful download and update.
1309 payload_state.DownloadComplete();
1310
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001311 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001312 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001313 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001314 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001315 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001316 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001317 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001318 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1319 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001320 payload_state.UpdateSucceeded();
1321
1322 // Mock the request to a request where the delta was disabled but Omaha sends
1323 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001324 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001325 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001326 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001327
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001328 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001329 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1330
1331 payload_state.DownloadComplete();
1332
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001333 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001334 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001335 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001336 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1337 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001338 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001339 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1340 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001341 payload_state.UpdateSucceeded();
1342}
1343
1344TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1345 OmahaResponse response;
1346 response.is_delta_payload = false;
1347 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001348 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001349
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001350 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001351 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1352
1353 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001354 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001355 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001356 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001357
1358 // Simulate a successful download and update.
1359 payload_state.DownloadComplete();
1360
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001361 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001362 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001363 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001364 "Installer.PayloadFormat", kPayloadTypeForcedFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001365 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001366 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1367 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001368 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001369 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1370 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001371 payload_state.UpdateSucceeded();
1372}
1373
1374TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1375 OmahaResponse response;
1376 response.is_delta_payload = false;
1377 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001378 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001379
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001380 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001381 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1382
Alex Deymo820cc702013-06-28 15:43:46 -07001383 // Mock the request to a request where the delta is enabled, although the
1384 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001385 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001386 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001387 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001388
1389 // Simulate a successful download and update.
1390 payload_state.DownloadComplete();
1391
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001392 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001393 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001394 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001395 "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001396 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001397 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1398 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001399 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001400 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1401 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001402 payload_state.UpdateSucceeded();
1403}
1404
Alex Deymo42432912013-07-12 20:21:15 -07001405TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001406 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001407 OmahaResponse response;
1408 PayloadState payload_state;
1409 Prefs prefs;
1410 string temp_dir;
1411
1412 // Setup an environment with persistent prefs across simulated reboots.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001413 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001414 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001415 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001416 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001417
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001418 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001419 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001420
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001421 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001422 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1423
1424 // Simulate a successful download and update.
1425 payload_state.DownloadComplete();
1426 payload_state.UpdateSucceeded();
1427 payload_state.ExpectRebootInNewVersion("Version:12345678");
1428
1429 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001430 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001431 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001432 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001433 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001434 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001435 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001436
1437 // Simulate a second update and reboot into the same environment, this should
1438 // send a value of 2.
1439 payload_state.ExpectRebootInNewVersion("Version:12345678");
1440
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001441 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001442 "Installer.RebootToNewPartitionAttempt", 2, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001443 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001444 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001445 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001446 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001447
1448 // Simulate a third failed reboot to new version, but this time for a
1449 // different payload. This should send a value of 1 this time.
1450 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001451 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001452 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001453 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001454 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001455 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001456 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001457
1458 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1459}
1460
1461TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001462 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001463 OmahaResponse response;
1464 PayloadState payload_state;
1465 Prefs prefs;
1466 string temp_dir;
1467
1468 // Setup an environment with persistent prefs across simulated reboots.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001469 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001470 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001471 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001472 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001473
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001474 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001475 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001476
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001477 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001478 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1479
1480 // Simulate a successful download and update.
1481 payload_state.DownloadComplete();
1482 payload_state.UpdateSucceeded();
1483 payload_state.ExpectRebootInNewVersion("Version:12345678");
1484
1485 // Change the BootDevice to a different one, no metric should be sent.
Don Garrett6646b442013-11-13 15:29:11 -08001486 fake_hardware->SetBootDevice("/dev/sda5");
Alex Deymo42432912013-07-12 20:21:15 -07001487
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001488 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001489 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1490 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001491 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001492 metrics::kMetricFailedUpdateCount, _, _, _, _))
1493 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001494 payload_state.ReportFailedBootIfNeeded();
1495
1496 // A second reboot in eiher partition should not send a metric.
1497 payload_state.ReportFailedBootIfNeeded();
Don Garrett6646b442013-11-13 15:29:11 -08001498 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001499 payload_state.ReportFailedBootIfNeeded();
1500
1501 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1502}
1503
1504TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001505 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001506 OmahaResponse response;
1507 PayloadState payload_state;
1508 Prefs prefs;
1509 string temp_dir;
1510
1511 // Setup an environment with persistent prefs across simulated reboots.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001512 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001513 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001514 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001515 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001516
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001517 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001518 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1519
1520 // Simulate a successful download and update.
1521 payload_state.DownloadComplete();
1522 payload_state.UpdateSucceeded();
1523 payload_state.ExpectRebootInNewVersion("Version:12345678");
1524
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001525 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001526 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1527 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001528 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001529 metrics::kMetricFailedUpdateCount, _, _, _, _))
1530 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001531
1532 // Cancel the applied update.
1533 payload_state.ResetUpdateStatus();
1534
1535 // Simulate a reboot.
1536 payload_state.ReportFailedBootIfNeeded();
1537
1538 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1539}
1540
1541TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001542 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001543 PayloadState payload_state;
1544 Prefs prefs;
1545 string temp_dir;
1546
1547 // Setup an environment with persistent but initially empty prefs.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001548 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001549 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001550 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001551 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001552
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001553 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001554
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001555 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001556 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1557 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001558 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001559 metrics::kMetricFailedUpdateCount, _, _, _, _))
1560 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001561
1562 // Simulate a reboot in this environment.
1563 payload_state.ReportFailedBootIfNeeded();
1564
1565 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1566}
1567
David Zeuthendcba8092013-08-06 12:16:35 -07001568TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1569 OmahaResponse response;
1570 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001571 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001572 Prefs prefs;
1573 string temp_dir;
1574
1575 // We need persistent preferences for this test.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001576 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001577 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001578 prefs.Init(base::FilePath(temp_dir));
David Zeuthendcba8092013-08-06 12:16:35 -07001579
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001580 fake_system_state.set_prefs(&prefs);
1581 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001582 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1583
1584 // Should allow exactly kMaxP2PAttempts...
1585 for (int n = 0; n < kMaxP2PAttempts; n++) {
1586 payload_state.P2PNewAttempt();
1587 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1588 }
1589 // ... but not more than that.
1590 payload_state.P2PNewAttempt();
1591 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
1592
1593 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1594}
1595
1596TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1597 OmahaResponse response;
1598 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001599 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001600 FakeClock fake_clock;
1601 Prefs prefs;
1602 string temp_dir;
1603
1604 // We need persistent preferences for this test.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001605 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001606 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001607 prefs.Init(base::FilePath(temp_dir));
David Zeuthendcba8092013-08-06 12:16:35 -07001608
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001609 fake_system_state.set_clock(&fake_clock);
1610 fake_system_state.set_prefs(&prefs);
1611 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001612 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1613
1614 // Set the clock to 1 second.
1615 Time epoch = Time::FromInternalValue(1000000);
1616 fake_clock.SetWallclockTime(epoch);
1617
1618 // Do an attempt - this will set the timestamp.
1619 payload_state.P2PNewAttempt();
1620
1621 // Check that the timestamp equals what we just set.
1622 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1623
1624 // Time hasn't advanced - this should work.
1625 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1626
1627 // Set clock to half the deadline - this should work.
1628 fake_clock.SetWallclockTime(epoch +
1629 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1630 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1631
1632 // Check that the first attempt timestamp hasn't changed just
1633 // because the wall-clock time changed.
1634 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1635
1636 // Set clock to _just_ before the deadline - this should work.
1637 fake_clock.SetWallclockTime(epoch +
1638 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1639 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1640
1641 // Set clock to _just_ after the deadline - this should not work.
1642 fake_clock.SetWallclockTime(epoch +
1643 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1644 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
1645
1646 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1647}
1648
1649TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1650 OmahaResponse response;
1651 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001652 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001653 Prefs prefs;
1654 string temp_dir;
1655
Gilad Arnolda6742b32014-01-11 00:18:34 -08001656 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001657 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001658 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001659 fake_system_state.set_prefs(&prefs);
1660 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001661 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1662
1663 Time null_time = Time();
1664 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1665 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1666
1667 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1668}
1669
1670TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1671 OmahaResponse response;
1672 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001673 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001674 FakeClock fake_clock;
1675 Prefs prefs;
1676 string temp_dir;
1677
Gilad Arnolda6742b32014-01-11 00:18:34 -08001678 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001679 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001680 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001681 fake_system_state.set_clock(&fake_clock);
1682 fake_system_state.set_prefs(&prefs);
1683 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001684 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1685
1686 // Set the clock to something known.
1687 Time time = Time::FromInternalValue(12345);
1688 fake_clock.SetWallclockTime(time);
1689
1690 // New p2p attempt - as a side-effect this will update the p2p state vars.
1691 payload_state.P2PNewAttempt();
1692 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1693 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1694
1695 // Now create a new PayloadState and check that it loads the state
1696 // vars correctly.
1697 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001698 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001699 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1700 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
1701
1702 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1703}
1704
1705TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1706 OmahaResponse response;
1707 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001708 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001709 FakeClock fake_clock;
1710 Prefs prefs;
1711 string temp_dir;
1712
Gilad Arnolda6742b32014-01-11 00:18:34 -08001713 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001714 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001715 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001716 fake_system_state.set_clock(&fake_clock);
1717 fake_system_state.set_prefs(&prefs);
1718 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001719 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1720
1721 // Set the clock to something known.
1722 Time time = Time::FromInternalValue(12345);
1723 fake_clock.SetWallclockTime(time);
1724
1725 // New p2p attempt - as a side-effect this will update the p2p state vars.
1726 payload_state.P2PNewAttempt();
1727 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1728 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1729
1730 // Set a new response...
1731 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1732
1733 // ... and check that it clears the P2P state vars.
1734 Time null_time = Time();
1735 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1736 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1737
1738 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1739}
1740
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001741}