blob: 11eb4afcf98dc9ca5716d01f72a713ce8b8d51ce [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());
David Zeuthenafed4a12014-04-09 15:28:44 -07001083
1084 // Check that we don't report any metrics in UpdateSucceeded().
1085 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1086 .Times(0);
1087 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1088 .Times(0);
1089 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001090}
1091
David Zeuthenf413fe52013-04-22 14:04:39 -07001092TEST(PayloadStateTest, DurationsAreCorrect) {
1093 OmahaResponse response;
1094 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001095 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001096 FakeClock fake_clock;
1097 Prefs prefs;
1098 string temp_dir;
1099
1100 // Set the clock to a well-known time - 1 second on the wall-clock
1101 // and 2 seconds on the monotonic clock
1102 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1103 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1104
1105 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001106 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateDurationTests.XXXXXX",
David Zeuthenf413fe52013-04-22 14:04:39 -07001107 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001108 prefs.Init(base::FilePath(temp_dir));
David Zeuthenf413fe52013-04-22 14:04:39 -07001109
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001110 fake_system_state.set_clock(&fake_clock);
1111 fake_system_state.set_prefs(&prefs);
1112 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001113
1114 // Check that durations are correct for a successful update where
1115 // time has advanced 7 seconds on the wall clock and 4 seconds on
1116 // the monotonic clock.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001117 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001118 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1119 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1120 payload_state.UpdateSucceeded();
1121 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1122 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1123
1124 // Check that durations are reset when a new response comes in.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001125 SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001126 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1127 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1128
1129 // Advance time a bit (10 secs), simulate download progress and
1130 // check that durations are updated.
1131 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1132 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1133 payload_state.DownloadProgress(10);
1134 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1135 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1136
1137 // Now simulate a reboot by resetting monotonic time (to 5000) and
1138 // creating a new PayloadState object and check that we load the
1139 // durations correctly (e.g. they are the same as before).
1140 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1141 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001142 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001143 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
1144 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),10000000);
1145
1146 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1147 // and check that the durations are increased accordingly.
1148 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1149 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1150 payload_state2.UpdateSucceeded();
1151 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
1152 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),16000000);
1153
1154 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1155}
1156
David Zeuthene4c58bf2013-06-18 17:26:50 -07001157TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1158 OmahaResponse response;
1159 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001160 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001161 FakeClock fake_clock;
1162 Prefs prefs;
1163 string temp_dir;
1164
1165 // Set the clock to a well-known time (t = 30 seconds).
1166 fake_clock.SetWallclockTime(Time::FromInternalValue(
1167 30 * Time::kMicrosecondsPerSecond));
1168
1169 // We need persistent preferences for this test
1170 EXPECT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -08001171 "RebootAfterSuccessfulUpdateTest.XXXXXX", &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001172 prefs.Init(base::FilePath(temp_dir));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001173
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001174 fake_system_state.set_clock(&fake_clock);
1175 fake_system_state.set_prefs(&prefs);
1176 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001177
1178 // Make the update succeed.
1179 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1180 payload_state.UpdateSucceeded();
1181
1182 // Check that the marker was written.
1183 EXPECT_TRUE(prefs.Exists(kPrefsSystemUpdatedMarker));
1184
1185 // Now simulate a reboot and set the wallclock time to a later point
1186 // (t = 500 seconds). We do this by using a new PayloadState object
1187 // and checking that it emits the right UMA metric with the right
1188 // value.
1189 fake_clock.SetWallclockTime(Time::FromInternalValue(
1190 500 * Time::kMicrosecondsPerSecond));
1191 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001192 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001193
1194 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001195 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthene4c58bf2013-06-18 17:26:50 -07001196 "Installer.TimeToRebootMinutes",
1197 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001198 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001199 metrics::kMetricTimeToRebootMinutes,
1200 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001201 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001202
1203 payload_state2.UpdateEngineStarted();
1204
1205 // Check that the marker was nuked.
1206 EXPECT_FALSE(prefs.Exists(kPrefsSystemUpdatedMarker));
1207
1208 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1209}
1210
Alex Deymo569c4242013-07-24 12:01:01 -07001211TEST(PayloadStateTest, RestartAfterCrash) {
1212 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001213 FakeSystemState fake_system_state;
1214 NiceMock<PrefsMock>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001215
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001216 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001217
1218 // No prefs should be used after a crash.
1219 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1220 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1221 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1222 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1223 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1224 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1225 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
1226
1227 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001228 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001229 SendToUMA(_, _, _, _, _)).Times(0);
1230
1231 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001232 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001233
1234 payload_state.UpdateEngineStarted();
1235}
1236
Jay Srinivasan53173b92013-05-17 17:13:01 -07001237TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1238 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001239 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001240 PayloadState payload_state;
1241
Jay Srinivasan53173b92013-05-17 17:13:01 -07001242 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001243 fake_system_state.set_device_policy(&disable_http_policy);
1244 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001245
1246 // Test with no device policy. Should default to allowing http.
1247 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1248 .WillRepeatedly(Return(false));
1249
1250 // Set the first response.
1251 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1252
1253 // Check that we use the HTTP URL since there is no value set for allowing
1254 // http.
1255 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1256
1257 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001258 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1259 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1260
Chris Sosaf7d80042013-08-22 16:45:17 -07001261 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001262 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1263
1264 // Check that we skip the HTTP URL and use only the HTTPS url.
1265 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1266
1267 // Advance the URL index to 1 by faking an error.
1268 ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch;
1269 payload_state.UpdateFailed(error);
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 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
1274
1275 // Now, slightly change the response and set it again.
1276 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1277
1278 // Check that we still skip the HTTP URL and use only the HTTPS url.
1279 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1280
1281 // Now, pretend that the HTTP policy is turned on. We want to make sure
1282 // the new policy is honored.
1283 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001284 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001285 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1286 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1287
1288 // Now, set the same response using the same hash
1289 // so that we can test that the state is reset not because of the
1290 // hash but because of the policy change which results in candidate url
1291 // list change.
1292 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1293
1294 // Check that we use the HTTP URL now and the failure count is reset.
1295 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1296 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1297
1298 // Fake a failure and see if we're moving over to the HTTPS url and update
1299 // the URL switch count properly.
1300 payload_state.UpdateFailed(error);
1301 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1302 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
1303 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1304}
1305
Alex Deymo1c656c42013-06-28 11:02:14 -07001306TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1307 OmahaResponse response;
1308 response.is_delta_payload = true;
1309 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001310 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001311
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001312 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001313 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1314
1315 // Simulate a successful download and update.
1316 payload_state.DownloadComplete();
1317
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001318 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001319 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001320 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001321 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001322 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001323 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001324 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001325 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1326 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001327 payload_state.UpdateSucceeded();
1328
1329 // Mock the request to a request where the delta was disabled but Omaha sends
1330 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001331 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001332 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001333 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001334
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001335 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001336 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1337
1338 payload_state.DownloadComplete();
1339
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001340 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001341 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001342 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001343 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1344 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001345 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001346 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1347 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001348 payload_state.UpdateSucceeded();
1349}
1350
1351TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1352 OmahaResponse response;
1353 response.is_delta_payload = false;
1354 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001355 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001356
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001357 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001358 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1359
1360 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001361 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001362 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001363 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001364
1365 // Simulate a successful download and update.
1366 payload_state.DownloadComplete();
1367
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 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001370 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001371 "Installer.PayloadFormat", kPayloadTypeForcedFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001372 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001373 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1374 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001375 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001376 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1377 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001378 payload_state.UpdateSucceeded();
1379}
1380
1381TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1382 OmahaResponse response;
1383 response.is_delta_payload = false;
1384 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001385 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001386
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001387 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001388 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1389
Alex Deymo820cc702013-06-28 15:43:46 -07001390 // Mock the request to a request where the delta is enabled, although the
1391 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001392 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001393 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001394 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001395
1396 // Simulate a successful download and update.
1397 payload_state.DownloadComplete();
1398
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 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001401 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001402 "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001403 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001404 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1405 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001406 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001407 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1408 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001409 payload_state.UpdateSucceeded();
1410}
1411
Alex Deymo42432912013-07-12 20:21:15 -07001412TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001413 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001414 OmahaResponse response;
1415 PayloadState payload_state;
1416 Prefs prefs;
1417 string temp_dir;
1418
1419 // Setup an environment with persistent prefs across simulated reboots.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001420 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001421 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001422 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001423 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001424
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001425 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001426 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001427
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001428 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001429 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1430
1431 // Simulate a successful download and update.
1432 payload_state.DownloadComplete();
1433 payload_state.UpdateSucceeded();
1434 payload_state.ExpectRebootInNewVersion("Version:12345678");
1435
1436 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001437 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001438 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001439 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001440 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001441 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001442 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001443
1444 // Simulate a second update and reboot into the same environment, this should
1445 // send a value of 2.
1446 payload_state.ExpectRebootInNewVersion("Version:12345678");
1447
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001448 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001449 "Installer.RebootToNewPartitionAttempt", 2, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001450 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001451 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001452 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001453 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001454
1455 // Simulate a third failed reboot to new version, but this time for a
1456 // different payload. This should send a value of 1 this time.
1457 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001458 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001459 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001460 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001461 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001462 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001463 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001464
1465 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1466}
1467
1468TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001469 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001470 OmahaResponse response;
1471 PayloadState payload_state;
1472 Prefs prefs;
1473 string temp_dir;
1474
1475 // Setup an environment with persistent prefs across simulated reboots.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001476 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001477 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001478 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001479 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001480
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001481 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001482 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001483
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001484 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001485 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1486
1487 // Simulate a successful download and update.
1488 payload_state.DownloadComplete();
1489 payload_state.UpdateSucceeded();
1490 payload_state.ExpectRebootInNewVersion("Version:12345678");
1491
1492 // Change the BootDevice to a different one, no metric should be sent.
Don Garrett6646b442013-11-13 15:29:11 -08001493 fake_hardware->SetBootDevice("/dev/sda5");
Alex Deymo42432912013-07-12 20:21:15 -07001494
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001495 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001496 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1497 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001498 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001499 metrics::kMetricFailedUpdateCount, _, _, _, _))
1500 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001501 payload_state.ReportFailedBootIfNeeded();
1502
1503 // A second reboot in eiher partition should not send a metric.
1504 payload_state.ReportFailedBootIfNeeded();
Don Garrett6646b442013-11-13 15:29:11 -08001505 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001506 payload_state.ReportFailedBootIfNeeded();
1507
1508 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1509}
1510
1511TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001512 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001513 OmahaResponse response;
1514 PayloadState payload_state;
1515 Prefs prefs;
1516 string temp_dir;
1517
1518 // Setup an environment with persistent prefs across simulated reboots.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001519 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001520 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001521 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001522 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001523
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001524 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001525 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1526
1527 // Simulate a successful download and update.
1528 payload_state.DownloadComplete();
1529 payload_state.UpdateSucceeded();
1530 payload_state.ExpectRebootInNewVersion("Version:12345678");
1531
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001532 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001533 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1534 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001535 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001536 metrics::kMetricFailedUpdateCount, _, _, _, _))
1537 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001538
1539 // Cancel the applied update.
1540 payload_state.ResetUpdateStatus();
1541
1542 // Simulate a reboot.
1543 payload_state.ReportFailedBootIfNeeded();
1544
1545 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1546}
1547
1548TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001549 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001550 PayloadState payload_state;
1551 Prefs prefs;
1552 string temp_dir;
1553
1554 // Setup an environment with persistent but initially empty prefs.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001555 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateReboot.XXXXXX",
Alex Deymo42432912013-07-12 20:21:15 -07001556 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001557 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001558 fake_system_state.set_prefs(&prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001559
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001560 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001561
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001562 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001563 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1564 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001565 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001566 metrics::kMetricFailedUpdateCount, _, _, _, _))
1567 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001568
1569 // Simulate a reboot in this environment.
1570 payload_state.ReportFailedBootIfNeeded();
1571
1572 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1573}
1574
David Zeuthendcba8092013-08-06 12:16:35 -07001575TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1576 OmahaResponse response;
1577 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001578 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001579 Prefs prefs;
1580 string temp_dir;
1581
1582 // We need persistent preferences for this test.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001583 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001584 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001585 prefs.Init(base::FilePath(temp_dir));
David Zeuthendcba8092013-08-06 12:16:35 -07001586
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001587 fake_system_state.set_prefs(&prefs);
1588 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001589 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1590
1591 // Should allow exactly kMaxP2PAttempts...
1592 for (int n = 0; n < kMaxP2PAttempts; n++) {
1593 payload_state.P2PNewAttempt();
1594 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1595 }
1596 // ... but not more than that.
1597 payload_state.P2PNewAttempt();
1598 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
1599
1600 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1601}
1602
1603TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1604 OmahaResponse response;
1605 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001606 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001607 FakeClock fake_clock;
1608 Prefs prefs;
1609 string temp_dir;
1610
1611 // We need persistent preferences for this test.
Gilad Arnolda6742b32014-01-11 00:18:34 -08001612 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001613 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001614 prefs.Init(base::FilePath(temp_dir));
David Zeuthendcba8092013-08-06 12:16:35 -07001615
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001616 fake_system_state.set_clock(&fake_clock);
1617 fake_system_state.set_prefs(&prefs);
1618 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001619 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1620
1621 // Set the clock to 1 second.
1622 Time epoch = Time::FromInternalValue(1000000);
1623 fake_clock.SetWallclockTime(epoch);
1624
1625 // Do an attempt - this will set the timestamp.
1626 payload_state.P2PNewAttempt();
1627
1628 // Check that the timestamp equals what we just set.
1629 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1630
1631 // Time hasn't advanced - this should work.
1632 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1633
1634 // Set clock to half the deadline - this should work.
1635 fake_clock.SetWallclockTime(epoch +
1636 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1637 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1638
1639 // Check that the first attempt timestamp hasn't changed just
1640 // because the wall-clock time changed.
1641 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1642
1643 // Set clock to _just_ before the deadline - this should work.
1644 fake_clock.SetWallclockTime(epoch +
1645 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1646 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1647
1648 // Set clock to _just_ after the deadline - this should not work.
1649 fake_clock.SetWallclockTime(epoch +
1650 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1651 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
1652
1653 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1654}
1655
1656TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1657 OmahaResponse response;
1658 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001659 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001660 Prefs prefs;
1661 string temp_dir;
1662
Gilad Arnolda6742b32014-01-11 00:18:34 -08001663 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001664 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001665 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001666 fake_system_state.set_prefs(&prefs);
1667 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001668 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1669
1670 Time null_time = Time();
1671 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1672 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1673
1674 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1675}
1676
1677TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1678 OmahaResponse response;
1679 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001680 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001681 FakeClock fake_clock;
1682 Prefs prefs;
1683 string temp_dir;
1684
Gilad Arnolda6742b32014-01-11 00:18:34 -08001685 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001686 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001687 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001688 fake_system_state.set_clock(&fake_clock);
1689 fake_system_state.set_prefs(&prefs);
1690 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001691 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1692
1693 // Set the clock to something known.
1694 Time time = Time::FromInternalValue(12345);
1695 fake_clock.SetWallclockTime(time);
1696
1697 // New p2p attempt - as a side-effect this will update the p2p state vars.
1698 payload_state.P2PNewAttempt();
1699 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1700 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1701
1702 // Now create a new PayloadState and check that it loads the state
1703 // vars correctly.
1704 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001705 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001706 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1707 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
1708
1709 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1710}
1711
1712TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1713 OmahaResponse response;
1714 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001715 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001716 FakeClock fake_clock;
1717 Prefs prefs;
1718 string temp_dir;
1719
Gilad Arnolda6742b32014-01-11 00:18:34 -08001720 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthendcba8092013-08-06 12:16:35 -07001721 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001722 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001723 fake_system_state.set_clock(&fake_clock);
1724 fake_system_state.set_prefs(&prefs);
1725 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001726 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1727
1728 // Set the clock to something known.
1729 Time time = Time::FromInternalValue(12345);
1730 fake_clock.SetWallclockTime(time);
1731
1732 // New p2p attempt - as a side-effect this will update the p2p state vars.
1733 payload_state.P2PNewAttempt();
1734 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1735 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1736
1737 // Set a new response...
1738 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1739
1740 // ... and check that it clears the P2P state vars.
1741 Time null_time = Time();
1742 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1743 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1744
1745 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1746}
1747
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001748}