blob: c4e66ac6e22661906aedaf87e8455a1f7020b2a5 [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
Alex Deymo2c0db7b2014-11-04 12:23:39 -08005#include "update_engine/payload_state.h"
6
Alex Vakulenko75039d72014-03-25 12:36:28 -07007#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -08008#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080010#include <gmock/gmock.h>
11#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080012
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"
David Zeuthen4e1d1492014-04-25 13:12:27 -070016#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070017#include "update_engine/fake_system_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080018#include "update_engine/mock_prefs.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019#include "update_engine/omaha_request_action.h"
David Zeuthenf413fe52013-04-22 14:04:39 -070020#include "update_engine/prefs.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021#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;
Alex Deymo42432912013-07-12 20:21:15 -070027using testing::AnyNumber;
28using testing::AtLeast;
29using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080030using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080031using testing::Return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070033using testing::_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080034
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) {
Allie Woodeb9e6d82015-04-17 13:55:30 -070093 if (static_cast<int>(ErrorCode::kUmaReportedMax) != 48) {
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 "
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070097 << "to the new value of ErrorCode::kUmaReportedMax, which is "
98 << ErrorCode::kUmaReportedMax;
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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800106 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800154 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700155 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700156 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
157 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700158 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
159 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700160 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
161 .Times(AtLeast(1));
162 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
163 .Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
165 .Times(AtLeast(1));
166 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
167 .Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
169 .Times(AtLeast(1));
170 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
171 .Times(AtLeast(1));
172 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
173 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700174 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
175 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700176 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
177 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700179 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800181 string stored_response_sign = payload_state.GetResponseSignature();
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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800207 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800252 NiceMock<MockPrefs>* 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
Alex Vakulenkod2779df2014-06-16 13:19:00 -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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700287 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700324 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800367 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800368
Alex Vakulenkod2779df2014-06-16 13:19:00 -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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700420 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700428 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700436 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700447 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700456 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700466 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
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>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700476 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800512 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800513
Alex Vakulenkod2779df2014-06-16 13:19:00 -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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800558 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700559
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700560 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700561 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();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700611 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
612 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
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;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800623 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700624 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700625 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700626 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);
Alex Vakulenkod2779df2014-06-16 13:19:00 -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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700668 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
669 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700670 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);
Alex Vakulenkod2779df2014-06-16 13:19:00 -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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700691 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
692 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700693 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.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700697 payload_state.SetUsingP2PForDownloading(true);
698 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700699 // Should not backoff for p2p updates.
700 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
701
Gilad Arnold74b5f552014-10-07 08:17:16 -0700702 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700703 // 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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700726 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
727 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700792 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
793 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
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.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700843 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
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
Alex Deymo8427b4a2014-11-05 14:00:32 -08001026 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001027 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -07001028 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
Alex Deymo8427b4a2014-11-05 14:00:32 -08001054 NiceMock<MockPrefs>* 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
David Zeuthen96197df2014-04-16 12:22:39 -07001084 // Check that we report only UpdateEngine.Rollback.* metrics in
1085 // UpdateSucceeded().
David Zeuthenafed4a12014-04-09 15:28:44 -07001086 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1087 .Times(0);
1088 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1089 .Times(0);
David Zeuthen96197df2014-04-16 12:22:39 -07001090 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1091 SendEnumToUMA(
1092 metrics::kMetricRollbackResult,
1093 static_cast<int>(metrics::RollbackResult::kSuccess),
1094 static_cast<int>(metrics::RollbackResult::kNumConstants)));
David Zeuthenafed4a12014-04-09 15:28:44 -07001095 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001096}
1097
David Zeuthenf413fe52013-04-22 14:04:39 -07001098TEST(PayloadStateTest, DurationsAreCorrect) {
1099 OmahaResponse response;
1100 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001101 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001102 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001103 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001104
1105 // Set the clock to a well-known time - 1 second on the wall-clock
1106 // and 2 seconds on the monotonic clock
1107 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1108 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1109
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001110 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001111 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001112 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);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001144 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1145 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001146
1147 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1148 // and check that the durations are increased accordingly.
1149 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1150 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1151 payload_state2.UpdateSucceeded();
1152 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001153 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1154 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001155}
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;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001162 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001163
1164 // Set the clock to a well-known time (t = 30 seconds).
1165 fake_clock.SetWallclockTime(Time::FromInternalValue(
1166 30 * Time::kMicrosecondsPerSecond));
1167
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001168 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001169 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001170 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001171
1172 // Make the update succeed.
1173 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1174 payload_state.UpdateSucceeded();
1175
1176 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001177 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001178
1179 // Now simulate a reboot and set the wallclock time to a later point
1180 // (t = 500 seconds). We do this by using a new PayloadState object
1181 // and checking that it emits the right UMA metric with the right
1182 // value.
1183 fake_clock.SetWallclockTime(Time::FromInternalValue(
1184 500 * Time::kMicrosecondsPerSecond));
1185 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001186 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001187
1188 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001189 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthene4c58bf2013-06-18 17:26:50 -07001190 "Installer.TimeToRebootMinutes",
1191 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001192 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001193 metrics::kMetricTimeToRebootMinutes,
1194 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001195 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001196
1197 payload_state2.UpdateEngineStarted();
1198
1199 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001200 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001201}
1202
Alex Deymo569c4242013-07-24 12:01:01 -07001203TEST(PayloadStateTest, RestartAfterCrash) {
1204 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001205 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001206 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001207
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001208 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001209
David Zeuthen4e1d1492014-04-25 13:12:27 -07001210 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001211 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1212 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1213 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1214 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1215 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1216 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1217 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001218 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001219
1220 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001221 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001222 SendToUMA(_, _, _, _, _)).Times(0);
1223
1224 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001225 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001226
1227 payload_state.UpdateEngineStarted();
1228}
1229
David Zeuthen4e1d1492014-04-25 13:12:27 -07001230TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1231 PayloadState payload_state;
1232 FakeSystemState fake_system_state;
1233
1234 // If there's no marker at startup, ensure we don't report a metric.
1235 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1236 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1237 SendEnumToUMA(
1238 metrics::kMetricAttemptResult,
1239 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1240 _)).Times(0);
1241 payload_state.UpdateEngineStarted();
1242}
1243
1244TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1245 PayloadState payload_state;
1246 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001247 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001248
1249 // If we have a marker at startup, ensure it's reported and the
1250 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001251 fake_system_state.set_prefs(&fake_prefs);
1252 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001253
1254 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1255
1256 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1257 SendEnumToUMA(
1258 metrics::kMetricAttemptResult,
1259 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1260 _)).Times(1);
1261 payload_state.UpdateEngineStarted();
1262
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001263 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001264}
1265
1266TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1267 PayloadState payload_state;
1268 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001269 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001270
1271 // Make sure the marker is written and cleared during an attempt and
1272 // also that we DO NOT emit the metric (since the attempt didn't end
1273 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001274 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001275 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1276
1277 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1278 .Times(AnyNumber());
1279 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1280 .Times(AnyNumber());
1281 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1282 SendEnumToUMA(
1283 metrics::kMetricAttemptResult,
1284 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1285 _)).Times(0);
1286
1287 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001288 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001289
1290 payload_state.UpdateRestarted();
1291
1292 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001293 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001294
1295 payload_state.UpdateSucceeded();
1296
1297 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001298 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001299}
1300
Jay Srinivasan53173b92013-05-17 17:13:01 -07001301TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1302 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001303 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001304 PayloadState payload_state;
1305
Jay Srinivasan53173b92013-05-17 17:13:01 -07001306 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001307 fake_system_state.set_device_policy(&disable_http_policy);
1308 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001309
1310 // Test with no device policy. Should default to allowing http.
1311 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1312 .WillRepeatedly(Return(false));
1313
1314 // Set the first response.
1315 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1316
1317 // Check that we use the HTTP URL since there is no value set for allowing
1318 // http.
1319 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1320
1321 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001322 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1323 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1324
Chris Sosaf7d80042013-08-22 16:45:17 -07001325 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001326 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1327
1328 // Check that we skip the HTTP URL and use only the HTTPS url.
1329 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1330
1331 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001332 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001333 payload_state.UpdateFailed(error);
1334
1335 // Check that we still skip the HTTP URL and use only the HTTPS url.
1336 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1337 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
1338
1339 // Now, slightly change the response and set it again.
1340 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1341
1342 // Check that we still skip the HTTP URL and use only the HTTPS url.
1343 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1344
1345 // Now, pretend that the HTTP policy is turned on. We want to make sure
1346 // the new policy is honored.
1347 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001348 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001349 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1350 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1351
1352 // Now, set the same response using the same hash
1353 // so that we can test that the state is reset not because of the
1354 // hash but because of the policy change which results in candidate url
1355 // list change.
1356 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1357
1358 // Check that we use the HTTP URL now and the failure count is reset.
1359 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1360 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1361
1362 // Fake a failure and see if we're moving over to the HTTPS url and update
1363 // the URL switch count properly.
1364 payload_state.UpdateFailed(error);
1365 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1366 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
1367 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1368}
1369
Alex Deymo1c656c42013-06-28 11:02:14 -07001370TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1371 OmahaResponse response;
1372 response.is_delta_payload = true;
1373 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001374 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001375
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001376 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001377 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1378
1379 // Simulate a successful download and update.
1380 payload_state.DownloadComplete();
1381
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001382 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001383 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001384 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001385 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001386 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001387 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001388 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001389 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1390 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001391 payload_state.UpdateSucceeded();
1392
1393 // Mock the request to a request where the delta was disabled but Omaha sends
1394 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001395 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001396 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001397 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001398
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001399 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001400 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1401
1402 payload_state.DownloadComplete();
1403
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001404 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001405 "Installer.PayloadFormat", kPayloadTypeDelta, 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::kMetricAttemptPayloadType, kPayloadTypeDelta,
1408 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001409 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001410 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1411 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001412 payload_state.UpdateSucceeded();
1413}
1414
1415TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1416 OmahaResponse response;
1417 response.is_delta_payload = false;
1418 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001419 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001420
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001421 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001422 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1423
1424 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001425 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001426 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001427 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001428
1429 // Simulate a successful download and update.
1430 payload_state.DownloadComplete();
1431
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001432 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001433 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001434 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001435 "Installer.PayloadFormat", kPayloadTypeForcedFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001436 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001437 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1438 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001439 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001440 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1441 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001442 payload_state.UpdateSucceeded();
1443}
1444
1445TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1446 OmahaResponse response;
1447 response.is_delta_payload = false;
1448 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001449 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001450
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001451 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001452 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1453
Alex Deymo820cc702013-06-28 15:43:46 -07001454 // Mock the request to a request where the delta is enabled, although the
1455 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001456 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001457 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001458 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001459
1460 // Simulate a successful download and update.
1461 payload_state.DownloadComplete();
1462
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001463 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001464 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001465 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001466 "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001467 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001468 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1469 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001470 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001471 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1472 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001473 payload_state.UpdateSucceeded();
1474}
1475
Alex Deymo42432912013-07-12 20:21:15 -07001476TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001477 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001478 OmahaResponse response;
1479 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001480 FakePrefs fake_prefs;
1481 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001482
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001483 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001484 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001485
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001486 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001487 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1488
1489 // Simulate a successful download and update.
1490 payload_state.DownloadComplete();
1491 payload_state.UpdateSucceeded();
1492 payload_state.ExpectRebootInNewVersion("Version:12345678");
1493
1494 // Reboot into the same environment to get an UMA metric with a value of 1.
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", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001497 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001498 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001499 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001500 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001501
1502 // Simulate a second update and reboot into the same environment, this should
1503 // send a value of 2.
1504 payload_state.ExpectRebootInNewVersion("Version:12345678");
1505
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001506 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001507 "Installer.RebootToNewPartitionAttempt", 2, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001508 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001509 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001510 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001511 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001512
1513 // Simulate a third failed reboot to new version, but this time for a
1514 // different payload. This should send a value of 1 this time.
1515 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001516 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001517 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001518 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001519 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001520 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001521 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001522}
1523
1524TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001525 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001526 OmahaResponse response;
1527 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001528 FakePrefs fake_prefs;
1529 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001530
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001531 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001532 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001533
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001534 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001535 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1536
1537 // Simulate a successful download and update.
1538 payload_state.DownloadComplete();
1539 payload_state.UpdateSucceeded();
1540 payload_state.ExpectRebootInNewVersion("Version:12345678");
1541
1542 // Change the BootDevice to a different one, no metric should be sent.
Don Garrett6646b442013-11-13 15:29:11 -08001543 fake_hardware->SetBootDevice("/dev/sda5");
Alex Deymo42432912013-07-12 20:21:15 -07001544
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001545 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001546 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1547 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001548 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001549 metrics::kMetricFailedUpdateCount, _, _, _, _))
1550 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001551 payload_state.ReportFailedBootIfNeeded();
1552
1553 // A second reboot in eiher partition should not send a metric.
1554 payload_state.ReportFailedBootIfNeeded();
Don Garrett6646b442013-11-13 15:29:11 -08001555 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001556 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001557}
1558
1559TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001560 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001561 OmahaResponse response;
1562 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001563 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001564
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001565 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001566 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001567 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1568
1569 // Simulate a successful download and update.
1570 payload_state.DownloadComplete();
1571 payload_state.UpdateSucceeded();
1572 payload_state.ExpectRebootInNewVersion("Version:12345678");
1573
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001574 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001575 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1576 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001577 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001578 metrics::kMetricFailedUpdateCount, _, _, _, _))
1579 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001580
1581 // Cancel the applied update.
1582 payload_state.ResetUpdateStatus();
1583
1584 // Simulate a reboot.
1585 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001586}
1587
1588TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001589 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001590 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001591 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001592
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001593 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001594 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001595
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001596 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001597 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1598 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001599 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001600 metrics::kMetricFailedUpdateCount, _, _, _, _))
1601 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001602
1603 // Simulate a reboot in this environment.
1604 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001605}
1606
David Zeuthendcba8092013-08-06 12:16:35 -07001607TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1608 OmahaResponse response;
1609 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001610 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001611 FakePrefs fake_prefs;
1612 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001613
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001614 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001615 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1616
1617 // Should allow exactly kMaxP2PAttempts...
1618 for (int n = 0; n < kMaxP2PAttempts; n++) {
1619 payload_state.P2PNewAttempt();
1620 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1621 }
1622 // ... but not more than that.
1623 payload_state.P2PNewAttempt();
1624 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001625}
1626
1627TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1628 OmahaResponse response;
1629 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001630 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001631 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001632 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001633
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001634 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001635 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001636 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001637 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1638
1639 // Set the clock to 1 second.
1640 Time epoch = Time::FromInternalValue(1000000);
1641 fake_clock.SetWallclockTime(epoch);
1642
1643 // Do an attempt - this will set the timestamp.
1644 payload_state.P2PNewAttempt();
1645
1646 // Check that the timestamp equals what we just set.
1647 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1648
1649 // Time hasn't advanced - this should work.
1650 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1651
1652 // Set clock to half the deadline - this should work.
1653 fake_clock.SetWallclockTime(epoch +
1654 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1655 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1656
1657 // Check that the first attempt timestamp hasn't changed just
1658 // because the wall-clock time changed.
1659 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1660
1661 // Set clock to _just_ before the deadline - this should work.
1662 fake_clock.SetWallclockTime(epoch +
1663 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1664 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1665
1666 // Set clock to _just_ after the deadline - this should not work.
1667 fake_clock.SetWallclockTime(epoch +
1668 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1669 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001670}
1671
1672TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1673 OmahaResponse response;
1674 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001675 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001676 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001677
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001678 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001679 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001680 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1681
1682 Time null_time = Time();
1683 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1684 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001685}
1686
1687TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1688 OmahaResponse response;
1689 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001690 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001691 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001692 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001693 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001694 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001695 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001696 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1697
1698 // Set the clock to something known.
1699 Time time = Time::FromInternalValue(12345);
1700 fake_clock.SetWallclockTime(time);
1701
1702 // New p2p attempt - as a side-effect this will update the p2p state vars.
1703 payload_state.P2PNewAttempt();
1704 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1705 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1706
1707 // Now create a new PayloadState and check that it loads the state
1708 // vars correctly.
1709 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001710 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001711 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1712 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001713}
1714
1715TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1716 OmahaResponse response;
1717 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001718 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001719 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001720 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001721 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001722 fake_system_state.set_prefs(&fake_prefs);
1723
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001724 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001725 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1726
1727 // Set the clock to something known.
1728 Time time = Time::FromInternalValue(12345);
1729 fake_clock.SetWallclockTime(time);
1730
1731 // New p2p attempt - as a side-effect this will update the p2p state vars.
1732 payload_state.P2PNewAttempt();
1733 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1734 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1735
1736 // Set a new response...
1737 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1738
1739 // ... and check that it clears the P2P state vars.
1740 Time null_time = Time();
1741 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1742 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001743}
1744
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001745} // namespace chromeos_update_engine