blob: b6717226296050d411bc803f6d476fb54b400f2e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/payload_state.h"
18
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080020#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
26#include "update_engine/common/fake_clock.h"
27#include "update_engine/common/fake_hardware.h"
28#include "update_engine/common/fake_prefs.h"
29#include "update_engine/common/mock_prefs.h"
30#include "update_engine/common/prefs.h"
31#include "update_engine/common/test_utils.h"
32#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070033#include "update_engine/fake_system_state.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080034#include "update_engine/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080035
Jay Srinivasan08262882012-12-28 19:29:43 -080036using base::Time;
37using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038using std::string;
Alex Deymo42432912013-07-12 20:21:15 -070039using testing::AnyNumber;
40using testing::AtLeast;
41using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080043using testing::Return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070045using testing::_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046
47namespace chromeos_update_engine {
48
Jay Srinivasan19409b72013-04-12 19:23:36 -070049const char* kCurrentBytesDownloadedFromHttps =
50 "current-bytes-downloaded-from-HttpsServer";
51const char* kTotalBytesDownloadedFromHttps =
52 "total-bytes-downloaded-from-HttpsServer";
53const char* kCurrentBytesDownloadedFromHttp =
54 "current-bytes-downloaded-from-HttpServer";
55const char* kTotalBytesDownloadedFromHttp =
56 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070057const char* kCurrentBytesDownloadedFromHttpPeer =
58 "current-bytes-downloaded-from-HttpPeer";
59const char* kTotalBytesDownloadedFromHttpPeer =
60 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070061
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080062static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070063 bool http_enabled,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080064 PayloadState* payload_state,
65 OmahaResponse* response) {
66 response->payload_urls.clear();
67 response->payload_urls.push_back("http://test");
68 response->payload_urls.push_back("https://test");
69 response->size = 523456789;
70 response->hash = hash;
71 response->metadata_size = 558123;
72 response->metadata_signature = "metasign";
73 response->max_failure_count_per_url = 3;
74 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080075 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070076
77 string expected_url_https_only =
78 "NumURLs = 1\n"
79 "Candidate Url0 = https://test\n";
80
81 string expected_urls_both =
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080082 "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -070083 "Candidate Url0 = http://test\n"
84 "Candidate Url1 = https://test\n";
85
86 string expected_response_sign =
87 (http_enabled ? expected_urls_both : expected_url_https_only) +
Alex Vakulenko75039d72014-03-25 12:36:28 -070088 base::StringPrintf("Payload Size = 523456789\n"
89 "Payload Sha256 Hash = %s\n"
90 "Metadata Size = 558123\n"
91 "Metadata Signature = metasign\n"
92 "Is Delta Payload = %d\n"
93 "Max Failure Count Per Url = %d\n"
94 "Disable Payload Backoff = %d\n",
95 hash.c_str(),
96 response->is_delta_payload,
97 response->max_failure_count_per_url,
98 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -080099 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800100}
101
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800102class PayloadStateTest : public ::testing::Test { };
103
104TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
105 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700106 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800107 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700108 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700109 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
110 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700111 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
112 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700113 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
114 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
115 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
116 .Times(AtLeast(1));
117 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
118 .Times(AtLeast(1));
119 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
120 .Times(AtLeast(1));
121 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
122 .Times(AtLeast(1));
123 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
124 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700125 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
126 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700127 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800128 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700129 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800130 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800131 string stored_response_sign = payload_state.GetResponseSignature();
132 string expected_response_sign = "NumURLs = 0\n"
133 "Payload Size = 0\n"
134 "Payload Sha256 Hash = \n"
135 "Metadata Size = 0\n"
136 "Metadata Signature = \n"
137 "Is Delta Payload = 0\n"
138 "Max Failure Count Per Url = 0\n"
139 "Disable Payload Backoff = 0\n";
140 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700141 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800142 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
143 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700144 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800145}
146
147TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
148 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700149 response.payload_urls.push_back("https://single.url.test");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800150 response.size = 123456789;
151 response.hash = "hash";
152 response.metadata_size = 58123;
153 response.metadata_signature = "msign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700154 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800155 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700156 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700157 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
158 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700159 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
160 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700161 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
162 .Times(AtLeast(1));
163 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
164 .Times(AtLeast(1));
165 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
166 .Times(AtLeast(1));
167 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
168 .Times(AtLeast(1));
169 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
170 .Times(AtLeast(1));
171 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
172 .Times(AtLeast(1));
173 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
174 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700175 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
176 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700177 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
178 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700180 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800181 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800182 string stored_response_sign = payload_state.GetResponseSignature();
183 string expected_response_sign = "NumURLs = 1\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700184 "Candidate Url0 = https://single.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800185 "Payload Size = 123456789\n"
186 "Payload Sha256 Hash = hash\n"
187 "Metadata Size = 58123\n"
188 "Metadata Signature = msign\n"
189 "Is Delta Payload = 0\n"
190 "Max Failure Count Per Url = 0\n"
191 "Disable Payload Backoff = 0\n";
192 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700193 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800194 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
195 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700196 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800197}
198
199TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
200 OmahaResponse response;
201 response.payload_urls.push_back("http://multiple.url.test");
202 response.payload_urls.push_back("https://multiple.url.test");
203 response.size = 523456789;
204 response.hash = "rhash";
205 response.metadata_size = 558123;
206 response.metadata_signature = "metasign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700207 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800208 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700209 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700210 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
211 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700212 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
213 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700214 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
215 .Times(AtLeast(1));
216 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
217 .Times(AtLeast(1));
218 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
219 .Times(AtLeast(1));
220 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
221 .Times(AtLeast(1));
222 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
223 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700224 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
225 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700226 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
227 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700228
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700230 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800231 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800232 string stored_response_sign = payload_state.GetResponseSignature();
233 string expected_response_sign = "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700234 "Candidate Url0 = http://multiple.url.test\n"
235 "Candidate Url1 = https://multiple.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800236 "Payload Size = 523456789\n"
237 "Payload Sha256 Hash = rhash\n"
238 "Metadata Size = 558123\n"
239 "Metadata Signature = metasign\n"
240 "Is Delta Payload = 0\n"
241 "Max Failure Count Per Url = 0\n"
242 "Disable Payload Backoff = 0\n";
243 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700244 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800245 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
246 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700247 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800248}
249
250TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
251 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700252 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800253 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800254 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800255
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700256 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800257 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700258 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
259 .Times(AtLeast(1));
260 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
261 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700262 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
263 .Times(AtLeast(1));
264 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
265 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700266 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700267
Chris Sosabe45bef2013-04-09 18:25:12 -0700268 // Reboots will be set
269 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
270
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800271 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700272 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
273 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800274
275 // Failure count should be called each times url index is set, so that's
276 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700277 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
278 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800279
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700280 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800281
282 // This does a SetResponse which causes all the states to be set to 0 for
283 // the first time.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700284 SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response);
285 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800286
287 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700288 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700290 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800291
292 // Verify that on the next error, the URL index wraps around to 0.
293 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700294 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295
296 // Verify that on the next error, it again advances to 1.
297 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700298 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700299
300 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800301 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800302}
303
304TEST(PayloadStateTest, NewResponseResetsPayloadState) {
305 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700306 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800307 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800308
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700309 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800310
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700311 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800312 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700313 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800314 .Times(AnyNumber());
315
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800316 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700317 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700318 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319
320 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700321 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700323 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800324 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325
326 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700327 SetupPayloadStateWith2Urls("Hash8225", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700328 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800329
Alex Deymob33b0f02013-08-08 21:10:02 -0700330 // Fake an error again.
331 payload_state.UpdateFailed(error);
332 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800333 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700334
Alex Deymob33b0f02013-08-08 21:10:02 -0700335 // Return a third different response.
336 SetupPayloadStateWith2Urls("Hash9999", true, &payload_state, &response);
337 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
338
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800339 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700340 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800341 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
342 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
343 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700344 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800345 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700346 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800347 EXPECT_EQ(
348 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
349 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700350 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800351}
352
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800353TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
354 OmahaResponse response;
355 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700356 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700357 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800358 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800359
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700360 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700361 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
362 .Times(AtLeast(2));
363 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
364 .Times(AtLeast(1));
365 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
366 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800367
Alex Deymo820cc702013-06-28 15:43:46 -0700368 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
369 .Times(AtLeast(2));
370 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
371 .Times(AtLeast(1));
372 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
373 .Times(AtLeast(1));
374
Jay Srinivasan19409b72013-04-12 19:23:36 -0700375 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800376
Jay Srinivasan19409b72013-04-12 19:23:36 -0700377 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
378 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800379
Jay Srinivasan19409b72013-04-12 19:23:36 -0700380 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
381 .Times(AtLeast(7));
382 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
383 .Times(AtLeast(2));
384 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
385 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800386
Jay Srinivasan19409b72013-04-12 19:23:36 -0700387 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
388 .Times(AtLeast(1));
389 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
390 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700391
Jay Srinivasan19409b72013-04-12 19:23:36 -0700392 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
393 .Times(AtLeast(1));
394 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
395 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700396 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
397 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700398 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
399 .Times(AtLeast(1));
400 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
401 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700402 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
403 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700404
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700405 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800406
Jay Srinivasan53173b92013-05-17 17:13:01 -0700407 SetupPayloadStateWith2Urls("Hash5873", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700408 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800409
410 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700411 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800412 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700413 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700414 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800415 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
416 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800417
418 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700419 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800420 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700421 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700422 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800423 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
424 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800425
426 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700427 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800428 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700429 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700430 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800431 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
432 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800433
434 // This should advance the URL index as we've reached the
435 // max failure count and reset the failure count for the new URL index.
436 // This should also wrap around the URL index and thus cause the payload
437 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700438 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800439 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700440 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700441 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800442 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
443 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800444 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800445
446 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700447 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
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("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800451 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
452 EXPECT_EQ(3U, 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 and payload attempt number due to
456 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700457 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800458 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700459 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700460 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800461 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
462 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800463 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800464
465 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700466 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700467 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800468 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700469 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700470 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800471 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
472 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800473 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800474
475 // And that failure count should be reset when we download some bytes
476 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700477 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800478 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700479 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700480 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800481 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
482 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800483 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800484
485 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700486 SetupPayloadStateWith2Urls("Hash8532", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700487 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800488
489 // Make sure the url index was reset to 0 because of the new response.
490 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700491 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700492 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800493 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
494 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800495 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800496}
497
Alex Deymo820cc702013-06-28 15:43:46 -0700498TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800499 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700500 response.is_delta_payload = false;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800501 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700502 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800503 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800504
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700505 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700506 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
507 .Times(AtLeast(1));
508 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
509 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800510
Alex Deymo820cc702013-06-28 15:43:46 -0700511 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
512 .Times(AtLeast(1));
513 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
514 .Times(AtLeast(1));
515
Jay Srinivasan19409b72013-04-12 19:23:36 -0700516 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
517 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800518
Jay Srinivasan19409b72013-04-12 19:23:36 -0700519 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
520 .Times(AtLeast(1));
521 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
522 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800523
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700524 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800525
Jay Srinivasan53173b92013-05-17 17:13:01 -0700526 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800527
528 // This should just advance the payload attempt number;
529 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700530 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800531 payload_state.DownloadComplete();
532 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700533 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
534 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800535 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
536 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700537}
538
539TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
540 OmahaResponse response;
541 response.is_delta_payload = true;
542 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700543 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800544 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700545
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700546 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700547 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
548 .Times(AtLeast(1));
549 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
550 .Times(AtLeast(1));
551
552 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
553 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
554 .Times(AtLeast(1));
555
556 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
557 .Times(1);
558
559 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
560 .Times(AtLeast(1));
561 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
562 .Times(AtLeast(1));
563
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700564 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700565
566 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
567
568 // This should just advance the payload attempt number;
569 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
570 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
571 payload_state.DownloadComplete();
572 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
573 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700574 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800575 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
576 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800577}
578
579TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
580 OmahaResponse response;
581 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700582 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800583
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700584 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700585 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800586
587 // Generate enough events to advance URL index, failure count and
588 // payload attempt number all to 1.
589 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700590 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
591 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800592 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700593 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700594 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800595 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
596 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800597
598 // Now, simulate a corrupted url index on persisted store which gets
599 // loaded when update_engine restarts. Using a different prefs object
600 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700601 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800602 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700603 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700604 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700605 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
606 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700607 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
608 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700609 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
610 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
611 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
612 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700613 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
614 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800615
616 // Note: This will be a different payload object, but the response should
617 // have the same hash as before so as to not trivially reset because the
618 // response was different. We want to specifically test that even if the
619 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700620 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700621 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800622
623 // Make sure all counters get reset to 0 because of the corrupted URL index
624 // we supplied above.
625 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700626 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700627 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800628 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
629 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800630}
Jay Srinivasan08262882012-12-28 19:29:43 -0800631
Chris Sosa20f005c2013-09-05 13:53:08 -0700632TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
633 OmahaResponse response;
634 response.is_delta_payload = false;
635 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700636 FakeSystemState fake_system_state;
637 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700638 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700639 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700640
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700641 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700642 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
643
644 // Simulate two failures (enough to cause payload backoff) and check
645 // again that we're ready to re-download without any backoff as this is
646 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700647 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
648 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700649 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
650 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
651 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
652 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
653}
654
655TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
656 OmahaResponse response;
657 response.is_delta_payload = false;
658 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 FakeSystemState fake_system_state;
660 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700661 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700662 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700663
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700664 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700665 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
666
667 // Simulate two failures (enough to cause payload backoff) and check
668 // again that we're ready to re-download without any backoff as this is
669 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700670 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
671 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700672 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
673 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
674 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
675 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700676 payload_state.SetUsingP2PForDownloading(true);
677 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700678 // Should not backoff for p2p updates.
679 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
680
Gilad Arnold74b5f552014-10-07 08:17:16 -0700681 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700682 // No actual p2p update if no url is provided.
683 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
684}
685
Jay Srinivasan08262882012-12-28 19:29:43 -0800686TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
687 OmahaResponse response;
688 response.is_delta_payload = true;
689 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700690 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800691
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700692 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700693 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800694
695 // Simulate a successful download and see that we're ready to download
696 // again without any backoff as this is a delta payload.
697 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700698 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
699 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800700 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
701
702 // Simulate two failures (enough to cause payload backoff) and check
703 // again that we're ready to re-download without any backoff as this is
704 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700705 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
706 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700707 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700708 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
709 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800710 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
711}
712
713static void CheckPayloadBackoffState(PayloadState* payload_state,
714 int expected_attempt_number,
715 TimeDelta expected_days) {
716 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700717 EXPECT_EQ(expected_attempt_number,
718 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800719 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
720 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
721 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
722 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
723 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
724 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
725 EXPECT_LT(expected_min_time.ToInternalValue(),
726 backoff_expiry_time.ToInternalValue());
727 EXPECT_GT(expected_max_time.ToInternalValue(),
728 backoff_expiry_time.ToInternalValue());
729}
730
731TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
732 OmahaResponse response;
733 response.is_delta_payload = false;
734 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700735 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800736
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700737 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700738 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800739
740 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
741 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
742 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
743 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
744 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
745 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
746 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
747 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
748 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
749 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
750}
751
752TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
753 OmahaResponse response;
754 response.disable_payload_backoff = true;
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 // Simulate a successful download and see that we are ready to download
762 // again without any backoff.
763 payload_state.DownloadComplete();
764 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700765 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800766 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
767
768 // Test again, this time by simulating two errors that would cause
769 // the payload attempt number to increment due to wrap around. And
770 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700771 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
772 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800773 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700774 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800775 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
776}
777
Jay Srinivasan19409b72013-04-12 19:23:36 -0700778TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
779 OmahaResponse response;
780 response.disable_payload_backoff = true;
781 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700782 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800783 uint64_t https_total = 0;
784 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700785
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700786 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700787 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700788 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700789
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700790 // Simulate a previous attempt with in order to set an initial non-zero value
791 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800792 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700793 http_total += prev_chunk;
794 payload_state.DownloadProgress(prev_chunk);
795
796 // Ensure that the initial values for HTTP reflect this attempt.
797 EXPECT_EQ(prev_chunk,
798 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
799 EXPECT_EQ(http_total,
800 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
801
802 // Change the response hash so as to simulate a new response which will
803 // reset the current bytes downloaded, but not the total bytes downloaded.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700804 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700805 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700806
807 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800808 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700809 http_total += first_chunk;
810 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700811 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700812 EXPECT_EQ(first_chunk,
813 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
814 EXPECT_EQ(http_total,
815 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800816 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700817 kDownloadSourceHttpsServer));
818 EXPECT_EQ(https_total,
819 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
820
821 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700822 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700823 payload_state.UpdateFailed(error);
824
Jay Srinivasan53173b92013-05-17 17:13:01 -0700825 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800826 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700827 https_total += second_chunk;
828 payload_state.DownloadProgress(second_chunk);
829 EXPECT_EQ(first_chunk,
830 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
831 EXPECT_EQ(http_total,
832 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
833 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
834 kDownloadSourceHttpsServer));
835 EXPECT_EQ(https_total,
836 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
837
838 // Simulate error to go back to http.
839 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800840 uint64_t third_chunk = 32345678;
841 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700842 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700843 payload_state.DownloadProgress(third_chunk);
844
845 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
846 EXPECT_EQ(http_chunk,
847 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700848 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700849 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
850 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
851 kDownloadSourceHttpsServer));
852 EXPECT_EQ(https_total,
853 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
854
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700855 // Simulate error (will cause URL switch), set p2p is to be used and
856 // then do 42MB worth of progress
857 payload_state.UpdateFailed(error);
858 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800859 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700860 payload_state.DownloadProgress(p2p_total);
861
862 EXPECT_EQ(p2p_total,
863 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
864
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700865 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700866 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700867 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800868 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700869 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800870 metrics::kMetricSuccessfulUpdateUrlSwitchCount,
871 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700872 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800873 metrics::kMetricSuccessfulUpdateTotalDurationMinutes,
874 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700875 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800876 metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
877 314, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700878 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800879 metrics::kMetricAttemptPayloadType, kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700880 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800881 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
882 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700883 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800884 metrics::kMetricSuccessfulUpdateAttemptCount, 1, _, _, _));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700885
886 payload_state.UpdateSucceeded();
887
888 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800889 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700890 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800891 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700892 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800893 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700894 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800895 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700896 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700897 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700898}
899
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700900TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
901 OmahaResponse response;
902 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700903 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700904
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700905 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700906 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
907
908 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800909 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700910 payload_state.DownloadProgress(num_bytes);
911
912 // Check that this was done via HTTP.
913 EXPECT_EQ(num_bytes,
914 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
915 EXPECT_EQ(num_bytes,
916 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
917
918 // Check that only HTTP is reported as a download source.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700919 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700920 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700921 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800922 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed,
923 (1 << kDownloadSourceHttpServer),
924 _, _, _));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700925
926 payload_state.UpdateSucceeded();
927}
928
Jay Srinivasan19409b72013-04-12 19:23:36 -0700929TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
930 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700931 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700932 PayloadState payload_state;
933
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700934 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700935
936 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700937 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700938
Alex Deymo80f70ff2016-02-10 16:08:11 -0800939 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700940 payload_state.DownloadProgress(num_bytes);
941 EXPECT_EQ(num_bytes,
942 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
943 EXPECT_EQ(num_bytes,
944 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800945 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700946 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800947 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700948 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
949
950 payload_state.UpdateRestarted();
951 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800952 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700953 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
954 EXPECT_EQ(num_bytes,
955 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
956}
957
Chris Sosabe45bef2013-04-09 18:25:12 -0700958TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700959 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700960 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700961
Alex Deymo8427b4a2014-11-05 14:00:32 -0800962 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700963 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700964 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
965
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700966 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700967
968 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800969 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700970
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700971 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700972 payload_state.UpdateResumed();
973 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800974 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700975
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700976 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700977 payload_state.UpdateResumed();
978 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800979 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700980
981 // Restart the update again to verify we set the num of reboots back to 0.
982 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800983 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700984}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700985
Chris Sosaaa18e162013-06-20 13:20:30 -0700986TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700987 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -0700988 PayloadState payload_state;
989
Alex Deymo8427b4a2014-11-05 14:00:32 -0800990 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700991 fake_system_state.mock_powerwash_safe_prefs();
992 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -0700993
994 // Verify pre-conditions are good.
995 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
996
997 // Mock out the os version and make sure it's blacklisted correctly.
998 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700999 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001000 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001001 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001002
1003 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1004 rollback_version));
1005 payload_state.Rollback();
1006
1007 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001008
1009 // Change it up a little and verify we load it correctly.
1010 rollback_version = "2345.0.1";
1011 // Let's verify we can reload it correctly.
1012 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1013 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1014 SetArgumentPointee<1>(rollback_version), Return(true)));
1015 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1016 rollback_version));
1017 payload_state.LoadRollbackVersion();
1018 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001019
David Zeuthen96197df2014-04-16 12:22:39 -07001020 // Check that we report only UpdateEngine.Rollback.* metrics in
1021 // UpdateSucceeded().
David Zeuthenafed4a12014-04-09 15:28:44 -07001022 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1023 .Times(0);
1024 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1025 .Times(0);
David Zeuthen96197df2014-04-16 12:22:39 -07001026 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1027 SendEnumToUMA(
1028 metrics::kMetricRollbackResult,
1029 static_cast<int>(metrics::RollbackResult::kSuccess),
1030 static_cast<int>(metrics::RollbackResult::kNumConstants)));
David Zeuthenafed4a12014-04-09 15:28:44 -07001031 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001032}
1033
David Zeuthenf413fe52013-04-22 14:04:39 -07001034TEST(PayloadStateTest, DurationsAreCorrect) {
1035 OmahaResponse response;
1036 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001037 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001038 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001039 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001040
1041 // Set the clock to a well-known time - 1 second on the wall-clock
1042 // and 2 seconds on the monotonic clock
1043 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1044 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1045
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001046 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001047 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001048 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001049
1050 // Check that durations are correct for a successful update where
1051 // time has advanced 7 seconds on the wall clock and 4 seconds on
1052 // the monotonic clock.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001053 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001054 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1055 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1056 payload_state.UpdateSucceeded();
1057 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1058 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1059
1060 // Check that durations are reset when a new response comes in.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001061 SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001062 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1063 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1064
1065 // Advance time a bit (10 secs), simulate download progress and
1066 // check that durations are updated.
1067 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1068 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1069 payload_state.DownloadProgress(10);
1070 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1071 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1072
1073 // Now simulate a reboot by resetting monotonic time (to 5000) and
1074 // creating a new PayloadState object and check that we load the
1075 // durations correctly (e.g. they are the same as before).
1076 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1077 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001078 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001079 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001080 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1081 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001082
1083 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1084 // and check that the durations are increased accordingly.
1085 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1086 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1087 payload_state2.UpdateSucceeded();
1088 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001089 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1090 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001091}
1092
David Zeuthene4c58bf2013-06-18 17:26:50 -07001093TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1094 OmahaResponse response;
1095 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001096 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001097 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001098 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001099
1100 // Set the clock to a well-known time (t = 30 seconds).
1101 fake_clock.SetWallclockTime(Time::FromInternalValue(
1102 30 * Time::kMicrosecondsPerSecond));
1103
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001104 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001105 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001106 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001107
1108 // Make the update succeed.
1109 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1110 payload_state.UpdateSucceeded();
1111
1112 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001113 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001114
1115 // Now simulate a reboot and set the wallclock time to a later point
1116 // (t = 500 seconds). We do this by using a new PayloadState object
1117 // and checking that it emits the right UMA metric with the right
1118 // value.
1119 fake_clock.SetWallclockTime(Time::FromInternalValue(
1120 500 * Time::kMicrosecondsPerSecond));
1121 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001122 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001123
1124 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001125 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001126 metrics::kMetricTimeToRebootMinutes,
1127 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001128 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001129
1130 payload_state2.UpdateEngineStarted();
1131
1132 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001133 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001134}
1135
Alex Deymo569c4242013-07-24 12:01:01 -07001136TEST(PayloadStateTest, RestartAfterCrash) {
1137 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001138 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001139 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001140
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001141 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001142
David Zeuthen4e1d1492014-04-25 13:12:27 -07001143 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001144 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1145 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1146 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1147 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1148 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1149 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1150 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001151 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001152
1153 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001154 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001155 SendToUMA(_, _, _, _, _)).Times(0);
1156
1157 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001158 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001159
1160 payload_state.UpdateEngineStarted();
1161}
1162
David Zeuthen4e1d1492014-04-25 13:12:27 -07001163TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1164 PayloadState payload_state;
1165 FakeSystemState fake_system_state;
1166
1167 // If there's no marker at startup, ensure we don't report a metric.
1168 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1169 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1170 SendEnumToUMA(
1171 metrics::kMetricAttemptResult,
1172 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1173 _)).Times(0);
1174 payload_state.UpdateEngineStarted();
1175}
1176
1177TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1178 PayloadState payload_state;
1179 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001180 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001181
1182 // If we have a marker at startup, ensure it's reported and the
1183 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001184 fake_system_state.set_prefs(&fake_prefs);
1185 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001186
1187 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1188
1189 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1190 SendEnumToUMA(
1191 metrics::kMetricAttemptResult,
1192 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1193 _)).Times(1);
1194 payload_state.UpdateEngineStarted();
1195
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001196 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001197}
1198
1199TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1200 PayloadState payload_state;
1201 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001202 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001203
1204 // Make sure the marker is written and cleared during an attempt and
1205 // also that we DO NOT emit the metric (since the attempt didn't end
1206 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001207 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001208 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1209
1210 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1211 .Times(AnyNumber());
1212 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1213 .Times(AnyNumber());
1214 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1215 SendEnumToUMA(
1216 metrics::kMetricAttemptResult,
1217 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1218 _)).Times(0);
1219
1220 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001221 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001222
1223 payload_state.UpdateRestarted();
1224
1225 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001226 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001227
1228 payload_state.UpdateSucceeded();
1229
1230 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001231 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001232}
1233
Jay Srinivasan53173b92013-05-17 17:13:01 -07001234TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1235 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001236 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001237 PayloadState payload_state;
1238
Jay Srinivasan53173b92013-05-17 17:13:01 -07001239 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001240 fake_system_state.set_device_policy(&disable_http_policy);
1241 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001242
1243 // Test with no device policy. Should default to allowing http.
1244 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1245 .WillRepeatedly(Return(false));
1246
1247 // Set the first response.
1248 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1249
1250 // Check that we use the HTTP URL since there is no value set for allowing
1251 // http.
1252 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1253
1254 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001255 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1256 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1257
Chris Sosaf7d80042013-08-22 16:45:17 -07001258 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001259 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1260
1261 // Check that we skip the HTTP URL and use only the HTTPS url.
1262 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1263
1264 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001265 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001266 payload_state.UpdateFailed(error);
1267
1268 // Check that we still skip the HTTP URL and use only the HTTPS url.
1269 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001270 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001271
1272 // Now, slightly change the response and set it again.
1273 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1274
1275 // Check that we still skip the HTTP URL and use only the HTTPS url.
1276 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1277
1278 // Now, pretend that the HTTP policy is turned on. We want to make sure
1279 // the new policy is honored.
1280 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001281 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001282 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1283 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1284
1285 // Now, set the same response using the same hash
1286 // so that we can test that the state is reset not because of the
1287 // hash but because of the policy change which results in candidate url
1288 // list change.
1289 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1290
1291 // Check that we use the HTTP URL now and the failure count is reset.
1292 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001293 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001294
1295 // Fake a failure and see if we're moving over to the HTTPS url and update
1296 // the URL switch count properly.
1297 payload_state.UpdateFailed(error);
1298 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001299 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1300 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001301}
1302
Alex Deymo1c656c42013-06-28 11:02:14 -07001303TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1304 OmahaResponse response;
1305 response.is_delta_payload = true;
1306 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001307 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001308
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001309 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001310 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1311
1312 // Simulate a successful download and update.
1313 payload_state.DownloadComplete();
1314
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001315 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001316 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001317 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001318 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001319 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001320 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1321 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001322 payload_state.UpdateSucceeded();
1323
1324 // Mock the request to a request where the delta was disabled but Omaha sends
1325 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001326 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001327 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001328 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001329
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001330 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001331 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1332
1333 payload_state.DownloadComplete();
1334
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001335 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001336 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1337 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001338 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001339 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1340 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001341 payload_state.UpdateSucceeded();
1342}
1343
1344TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1345 OmahaResponse response;
1346 response.is_delta_payload = false;
1347 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001348 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001349
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001350 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001351 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1352
1353 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001354 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001355 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001356 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001357
1358 // Simulate a successful download and update.
1359 payload_state.DownloadComplete();
1360
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001361 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001362 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001363 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001364 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1365 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001366 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001367 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1368 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001369 payload_state.UpdateSucceeded();
1370}
1371
1372TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1373 OmahaResponse response;
1374 response.is_delta_payload = false;
1375 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001376 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001377
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001378 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001379 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1380
Alex Deymo820cc702013-06-28 15:43:46 -07001381 // Mock the request to a request where the delta is enabled, although the
1382 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001383 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001384 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001385 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001386
1387 // Simulate a successful download and update.
1388 payload_state.DownloadComplete();
1389
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001390 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001391 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001392 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001393 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1394 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001395 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001396 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1397 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001398 payload_state.UpdateSucceeded();
1399}
1400
Alex Deymo42432912013-07-12 20:21:15 -07001401TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001402 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001403 OmahaResponse response;
1404 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001405 FakePrefs fake_prefs;
1406 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001407
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001408 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001409 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1410
1411 // Simulate a successful download and update.
1412 payload_state.DownloadComplete();
1413 payload_state.UpdateSucceeded();
1414 payload_state.ExpectRebootInNewVersion("Version:12345678");
1415
1416 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001417 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001418 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001419 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001420 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001421
1422 // Simulate a second update and reboot into the same environment, this should
1423 // send a value of 2.
1424 payload_state.ExpectRebootInNewVersion("Version:12345678");
1425
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001426 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001427 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001428 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001429 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001430
1431 // Simulate a third failed reboot to new version, but this time for a
1432 // different payload. This should send a value of 1 this time.
1433 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001434 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001435 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001436 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001437 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001438}
1439
1440TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001441 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001442 OmahaResponse response;
1443 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001444 FakePrefs fake_prefs;
1445 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001446
Alex Deymo763e7db2015-08-27 21:08:08 -07001447 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1448 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001449
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001450 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001451 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1452
1453 // Simulate a successful download and update.
1454 payload_state.DownloadComplete();
1455 payload_state.UpdateSucceeded();
1456 payload_state.ExpectRebootInNewVersion("Version:12345678");
1457
1458 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001459 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001460
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001461 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001462 metrics::kMetricFailedUpdateCount, _, _, _, _))
1463 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001464 payload_state.ReportFailedBootIfNeeded();
1465
Alex Deymo763e7db2015-08-27 21:08:08 -07001466 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001467 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001468 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001469 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001470}
1471
1472TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001473 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001474 OmahaResponse response;
1475 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001476 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001477
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001478 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001479 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001480 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1481
1482 // Simulate a successful download and update.
1483 payload_state.DownloadComplete();
1484 payload_state.UpdateSucceeded();
1485 payload_state.ExpectRebootInNewVersion("Version:12345678");
1486
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001487 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001488 metrics::kMetricFailedUpdateCount, _, _, _, _))
1489 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001490
1491 // Cancel the applied update.
1492 payload_state.ResetUpdateStatus();
1493
1494 // Simulate a reboot.
1495 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001496}
1497
1498TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001499 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001500 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001501 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001502
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001503 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001504 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001505
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001506 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001507 metrics::kMetricFailedUpdateCount, _, _, _, _))
1508 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001509
1510 // Simulate a reboot in this environment.
1511 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001512}
1513
David Zeuthendcba8092013-08-06 12:16:35 -07001514TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1515 OmahaResponse response;
1516 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001517 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001518 FakePrefs fake_prefs;
1519 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001520
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001521 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001522 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1523
1524 // Should allow exactly kMaxP2PAttempts...
1525 for (int n = 0; n < kMaxP2PAttempts; n++) {
1526 payload_state.P2PNewAttempt();
1527 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1528 }
1529 // ... but not more than that.
1530 payload_state.P2PNewAttempt();
1531 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001532}
1533
1534TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1535 OmahaResponse response;
1536 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001537 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001538 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001539 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001540
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001541 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001542 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001543 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001544 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1545
1546 // Set the clock to 1 second.
1547 Time epoch = Time::FromInternalValue(1000000);
1548 fake_clock.SetWallclockTime(epoch);
1549
1550 // Do an attempt - this will set the timestamp.
1551 payload_state.P2PNewAttempt();
1552
1553 // Check that the timestamp equals what we just set.
1554 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1555
1556 // Time hasn't advanced - this should work.
1557 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1558
1559 // Set clock to half the deadline - this should work.
1560 fake_clock.SetWallclockTime(epoch +
1561 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1562 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1563
1564 // Check that the first attempt timestamp hasn't changed just
1565 // because the wall-clock time changed.
1566 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1567
1568 // Set clock to _just_ before the deadline - this should work.
1569 fake_clock.SetWallclockTime(epoch +
1570 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1571 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1572
1573 // Set clock to _just_ after the deadline - this should not work.
1574 fake_clock.SetWallclockTime(epoch +
1575 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1576 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001577}
1578
1579TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1580 OmahaResponse response;
1581 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001582 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001583 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001584
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001585 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001586 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001587 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1588
1589 Time null_time = Time();
1590 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1591 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001592}
1593
1594TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1595 OmahaResponse response;
1596 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001597 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001598 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001599 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001600 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001601 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001602 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001603 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1604
1605 // Set the clock to something known.
1606 Time time = Time::FromInternalValue(12345);
1607 fake_clock.SetWallclockTime(time);
1608
1609 // New p2p attempt - as a side-effect this will update the p2p state vars.
1610 payload_state.P2PNewAttempt();
1611 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1612 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1613
1614 // Now create a new PayloadState and check that it loads the state
1615 // vars correctly.
1616 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001617 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001618 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1619 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001620}
1621
1622TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1623 OmahaResponse response;
1624 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001625 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001626 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001627 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001628 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001629 fake_system_state.set_prefs(&fake_prefs);
1630
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001631 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001632 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1633
1634 // Set the clock to something known.
1635 Time time = Time::FromInternalValue(12345);
1636 fake_clock.SetWallclockTime(time);
1637
1638 // New p2p attempt - as a side-effect this will update the p2p state vars.
1639 payload_state.P2PNewAttempt();
1640 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1641 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1642
1643 // Set a new response...
1644 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1645
1646 // ... and check that it clears the P2P state vars.
1647 Time null_time = Time();
1648 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1649 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001650}
1651
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001652} // namespace chromeos_update_engine