blob: 6366a7e6687121e4ce29c5a0855966bcea2debf5 [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) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080066 response->packages.clear();
67 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
68 .size = 523456789,
69 .metadata_size = 558123,
70 .metadata_signature = "metasign",
71 .hash = hash});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080072 response->max_failure_count_per_url = 3;
73 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080074 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070075
76 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080077 " NumURLs = 1\n"
78 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070079
80 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080081 " NumURLs = 2\n"
82 " Candidate Url0 = http://test\n"
83 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070084
Sen Jiang0affc2c2017-02-10 15:55:05 -080085 string expected_response_sign = base::StringPrintf(
86 "Payload 0:\n"
87 " Size = 523456789\n"
88 " Sha256 Hash = %s\n"
89 " Metadata Size = 558123\n"
90 " Metadata Signature = metasign\n"
91 "%s"
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 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
97 response->is_delta_payload,
98 response->max_failure_count_per_url,
99 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800100 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800101}
102
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800103class PayloadStateTest : public ::testing::Test { };
104
105TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
106 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700107 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800108 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700109 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700110 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
111 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700112 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
113 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700114 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
115 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
116 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
117 .Times(AtLeast(1));
118 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
119 .Times(AtLeast(1));
120 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
121 .Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
123 .Times(AtLeast(1));
124 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
125 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700126 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
127 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700128 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800129 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700130 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800131 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800132 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800133 string expected_response_sign =
134 "Is Delta Payload = 0\n"
135 "Max Failure Count Per Url = 0\n"
136 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800137 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700138 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800139 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
140 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700141 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800142}
143
144TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
145 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800146 response.packages.push_back({.payload_urls = {"https://single.url.test"},
147 .size = 123456789,
148 .metadata_size = 58123,
149 .metadata_signature = "msign",
150 .hash = "hash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700151 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800152 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700153 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700154 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
155 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700156 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
157 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700158 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
159 .Times(AtLeast(1));
160 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
161 .Times(AtLeast(1));
162 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
163 .Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
165 .Times(AtLeast(1));
166 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
167 .Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
169 .Times(AtLeast(1));
170 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
171 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700172 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
173 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700174 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
175 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800176 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700177 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800179 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800180 string expected_response_sign =
181 "Payload 0:\n"
182 " Size = 123456789\n"
183 " Sha256 Hash = hash\n"
184 " Metadata Size = 58123\n"
185 " Metadata Signature = msign\n"
186 " NumURLs = 1\n"
187 " Candidate Url0 = https://single.url.test\n"
188 "Is Delta Payload = 0\n"
189 "Max Failure Count Per Url = 0\n"
190 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800191 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700192 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800193 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
194 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700195 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800196}
197
198TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
199 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800200 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
201 "https://multiple.url.test"},
202 .size = 523456789,
203 .metadata_size = 558123,
204 .metadata_signature = "metasign",
205 .hash = "rhash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700206 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800207 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700208 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700209 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
210 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700211 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
212 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700213 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
214 .Times(AtLeast(1));
215 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
216 .Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
222 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
224 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700225 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
226 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700227
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700229 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800231 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800232 string expected_response_sign =
233 "Payload 0:\n"
234 " Size = 523456789\n"
235 " Sha256 Hash = rhash\n"
236 " Metadata Size = 558123\n"
237 " Metadata Signature = metasign\n"
238 " NumURLs = 2\n"
239 " Candidate Url0 = http://multiple.url.test\n"
240 " Candidate Url1 = https://multiple.url.test\n"
241 "Is Delta Payload = 0\n"
242 "Max Failure Count Per Url = 0\n"
243 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800244 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700245 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800246 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
247 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700248 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800249}
250
251TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
252 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700253 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800254 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800255 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700257 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800258 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700259 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
260 .Times(AtLeast(1));
261 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
262 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700263 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
264 .Times(AtLeast(1));
265 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
266 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700267 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700268
Chris Sosabe45bef2013-04-09 18:25:12 -0700269 // Reboots will be set
270 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
271
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800272 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700273 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
274 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800275
276 // Failure count should be called each times url index is set, so that's
277 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700278 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
279 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800280
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700281 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800282
283 // This does a SetResponse which causes all the states to be set to 0 for
284 // the first time.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700285 SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response);
286 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287
288 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700289 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700291 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292
293 // Verify that on the next error, the URL index wraps around to 0.
294 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296
297 // Verify that on the next error, it again advances to 1.
298 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700300
301 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800302 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800303}
304
305TEST(PayloadStateTest, NewResponseResetsPayloadState) {
306 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700307 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800311
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700312 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800313 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700314 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800315 .Times(AnyNumber());
316
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800317 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700318 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700319 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800320
321 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700322 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800323 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700324 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800325 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800326
327 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700328 SetupPayloadStateWith2Urls("Hash8225", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700329 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330
Alex Deymob33b0f02013-08-08 21:10:02 -0700331 // Fake an error again.
332 payload_state.UpdateFailed(error);
333 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800334 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700335
Alex Deymob33b0f02013-08-08 21:10:02 -0700336 // Return a third different response.
337 SetupPayloadStateWith2Urls("Hash9999", true, &payload_state, &response);
338 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
339
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800340 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700341 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800342 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
343 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
344 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700345 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800346 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700347 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800348 EXPECT_EQ(
349 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
350 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700351 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800352}
353
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800354TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
355 OmahaResponse response;
356 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700357 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700358 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800359 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800360
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700361 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700362 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
363 .Times(AtLeast(2));
364 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
365 .Times(AtLeast(1));
366 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
367 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800368
Alex Deymo820cc702013-06-28 15:43:46 -0700369 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
370 .Times(AtLeast(2));
371 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
372 .Times(AtLeast(1));
373 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
374 .Times(AtLeast(1));
375
Jay Srinivasan19409b72013-04-12 19:23:36 -0700376 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800377
Jay Srinivasan19409b72013-04-12 19:23:36 -0700378 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
379 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800380
Jay Srinivasan19409b72013-04-12 19:23:36 -0700381 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
382 .Times(AtLeast(7));
383 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
384 .Times(AtLeast(2));
385 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
386 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800387
Jay Srinivasan19409b72013-04-12 19:23:36 -0700388 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
389 .Times(AtLeast(1));
390 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
391 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700392
Jay Srinivasan19409b72013-04-12 19:23:36 -0700393 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
394 .Times(AtLeast(1));
395 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
396 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700397 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
398 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700399 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
400 .Times(AtLeast(1));
401 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
402 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700403 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
404 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700405
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700406 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800407
Jay Srinivasan53173b92013-05-17 17:13:01 -0700408 SetupPayloadStateWith2Urls("Hash5873", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700409 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800410
411 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700412 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800413 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700414 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700415 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800416 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
417 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800418
419 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700420 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800421 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700422 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700423 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800424 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
425 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800426
427 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700428 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800429 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700430 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700431 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800432 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
433 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800434
435 // This should advance the URL index as we've reached the
436 // max failure count and reset the failure count for the new URL index.
437 // This should also wrap around the URL index and thus cause the payload
438 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700439 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800440 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700441 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700442 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800443 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
444 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800445 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800446
447 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700448 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800449 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700450 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700451 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800452 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
453 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800454 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800455
456 // This should advance the URL index and payload attempt number due to
457 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700458 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800459 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700460 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700461 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800462 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
463 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800464 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800465
466 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700467 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700468 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800469 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700470 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700471 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800472 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
473 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800474 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800475
476 // And that failure count should be reset when we download some bytes
477 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700478 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800479 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700480 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700481 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800482 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
483 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800484 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800485
486 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700487 SetupPayloadStateWith2Urls("Hash8532", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700488 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800489
490 // Make sure the url index was reset to 0 because of the new response.
491 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700492 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700493 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800494 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
495 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800496 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800497}
498
Alex Deymo820cc702013-06-28 15:43:46 -0700499TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800500 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700501 response.is_delta_payload = false;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800502 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700503 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800504 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800505
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700506 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700507 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
508 .Times(AtLeast(1));
509 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
510 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800511
Alex Deymo820cc702013-06-28 15:43:46 -0700512 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
513 .Times(AtLeast(1));
514 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
515 .Times(AtLeast(1));
516
Jay Srinivasan19409b72013-04-12 19:23:36 -0700517 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
518 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800519
Jay Srinivasan19409b72013-04-12 19:23:36 -0700520 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
521 .Times(AtLeast(1));
522 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
523 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800524
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700525 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800526
Jay Srinivasan53173b92013-05-17 17:13:01 -0700527 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800528
529 // This should just advance the payload attempt number;
530 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700531 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800532 payload_state.DownloadComplete();
533 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700534 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
535 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800536 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
537 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700538}
539
540TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
541 OmahaResponse response;
542 response.is_delta_payload = true;
543 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700544 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800545 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700546
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700547 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700548 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
549 .Times(AtLeast(1));
550 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
551 .Times(AtLeast(1));
552
553 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
554 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
555 .Times(AtLeast(1));
556
557 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
558 .Times(1);
559
560 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
561 .Times(AtLeast(1));
562 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
563 .Times(AtLeast(1));
564
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700565 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700566
567 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
568
569 // This should just advance the payload attempt number;
570 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
571 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
572 payload_state.DownloadComplete();
573 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
574 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700575 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800576 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
577 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800578}
579
580TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
581 OmahaResponse response;
582 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700583 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800584
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700586 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800587
588 // Generate enough events to advance URL index, failure count and
589 // payload attempt number all to 1.
590 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700591 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
592 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800593 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700594 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700595 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800596 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
597 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800598
599 // Now, simulate a corrupted url index on persisted store which gets
600 // loaded when update_engine restarts. Using a different prefs object
601 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700602 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800603 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700604 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700605 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700606 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
607 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700608 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
609 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700610 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
611 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
612 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
613 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700614 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
615 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800616
617 // Note: This will be a different payload object, but the response should
618 // have the same hash as before so as to not trivially reset because the
619 // response was different. We want to specifically test that even if the
620 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700621 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700622 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800623
624 // Make sure all counters get reset to 0 because of the corrupted URL index
625 // we supplied above.
626 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700627 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700628 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800629 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
630 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800631}
Jay Srinivasan08262882012-12-28 19:29:43 -0800632
Chris Sosa20f005c2013-09-05 13:53:08 -0700633TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
634 OmahaResponse response;
635 response.is_delta_payload = false;
636 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700637 FakeSystemState fake_system_state;
638 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700639 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700640 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700641
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700642 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700643 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
644
645 // Simulate two failures (enough to cause payload backoff) and check
646 // again that we're ready to re-download without any backoff as this is
647 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700648 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
649 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700650 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
651 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
652 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
653 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
654}
655
656TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
657 OmahaResponse response;
658 response.is_delta_payload = false;
659 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700660 FakeSystemState fake_system_state;
661 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700662 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700663 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700664
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700665 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700666 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
667
668 // Simulate two failures (enough to cause payload backoff) and check
669 // again that we're ready to re-download without any backoff as this is
670 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700671 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
672 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700673 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
674 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
675 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
676 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700677 payload_state.SetUsingP2PForDownloading(true);
678 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700679 // Should not backoff for p2p updates.
680 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
681
Gilad Arnold74b5f552014-10-07 08:17:16 -0700682 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700683 // No actual p2p update if no url is provided.
684 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
685}
686
Jay Srinivasan08262882012-12-28 19:29:43 -0800687TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
688 OmahaResponse response;
689 response.is_delta_payload = true;
690 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700691 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800692
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700693 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700694 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800695
696 // Simulate a successful download and see that we're ready to download
697 // again without any backoff as this is a delta payload.
698 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700699 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
700 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800701 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
702
703 // Simulate two failures (enough to cause payload backoff) and check
704 // again that we're ready to re-download without any backoff as this is
705 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700706 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
707 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700708 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700709 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
710 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800711 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
712}
713
714static void CheckPayloadBackoffState(PayloadState* payload_state,
715 int expected_attempt_number,
716 TimeDelta expected_days) {
717 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700718 EXPECT_EQ(expected_attempt_number,
719 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800720 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
721 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
722 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
723 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
724 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
725 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
726 EXPECT_LT(expected_min_time.ToInternalValue(),
727 backoff_expiry_time.ToInternalValue());
728 EXPECT_GT(expected_max_time.ToInternalValue(),
729 backoff_expiry_time.ToInternalValue());
730}
731
732TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
733 OmahaResponse response;
734 response.is_delta_payload = false;
735 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700736 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800737
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700738 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700739 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800740
741 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
742 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
743 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
744 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
745 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
746 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
747 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
748 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
749 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
750 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
751}
752
753TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
754 OmahaResponse response;
755 response.disable_payload_backoff = true;
756 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700757 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800758
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700759 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700760 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800761
762 // Simulate a successful download and see that we are ready to download
763 // again without any backoff.
764 payload_state.DownloadComplete();
765 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700766 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800767 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
768
769 // Test again, this time by simulating two errors that would cause
770 // the payload attempt number to increment due to wrap around. And
771 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700772 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
773 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800774 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700775 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800776 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
777}
778
Jay Srinivasan19409b72013-04-12 19:23:36 -0700779TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
780 OmahaResponse response;
781 response.disable_payload_backoff = true;
782 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700783 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800784 uint64_t https_total = 0;
785 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700786
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700787 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700788 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700789 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700790
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700791 // Simulate a previous attempt with in order to set an initial non-zero value
792 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800793 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700794 http_total += prev_chunk;
795 payload_state.DownloadProgress(prev_chunk);
796
797 // Ensure that the initial values for HTTP reflect this attempt.
798 EXPECT_EQ(prev_chunk,
799 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
800 EXPECT_EQ(http_total,
801 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
802
803 // Change the response hash so as to simulate a new response which will
804 // reset the current bytes downloaded, but not the total bytes downloaded.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700805 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700806 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700807
808 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800809 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700810 http_total += first_chunk;
811 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700812 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700813 EXPECT_EQ(first_chunk,
814 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
815 EXPECT_EQ(http_total,
816 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800817 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700818 kDownloadSourceHttpsServer));
819 EXPECT_EQ(https_total,
820 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
821
822 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700823 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700824 payload_state.UpdateFailed(error);
825
Jay Srinivasan53173b92013-05-17 17:13:01 -0700826 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800827 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700828 https_total += second_chunk;
829 payload_state.DownloadProgress(second_chunk);
830 EXPECT_EQ(first_chunk,
831 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
832 EXPECT_EQ(http_total,
833 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
834 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
835 kDownloadSourceHttpsServer));
836 EXPECT_EQ(https_total,
837 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
838
839 // Simulate error to go back to http.
840 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800841 uint64_t third_chunk = 32345678;
842 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700843 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700844 payload_state.DownloadProgress(third_chunk);
845
846 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
847 EXPECT_EQ(http_chunk,
848 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700849 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700850 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
851 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
852 kDownloadSourceHttpsServer));
853 EXPECT_EQ(https_total,
854 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
855
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700856 // Simulate error (will cause URL switch), set p2p is to be used and
857 // then do 42MB worth of progress
858 payload_state.UpdateFailed(error);
859 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800860 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700861 payload_state.DownloadProgress(p2p_total);
862
863 EXPECT_EQ(p2p_total,
864 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
865
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700866 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700867 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700868 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800869 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700870 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800871 metrics::kMetricSuccessfulUpdateUrlSwitchCount,
872 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700873 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800874 metrics::kMetricSuccessfulUpdateTotalDurationMinutes,
875 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700876 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800877 metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
878 314, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700879 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800880 metrics::kMetricAttemptPayloadType, kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700881 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800882 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
883 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700884 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800885 metrics::kMetricSuccessfulUpdateAttemptCount, 1, _, _, _));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700886
887 payload_state.UpdateSucceeded();
888
889 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800890 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700891 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800892 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700893 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800894 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700895 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800896 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700897 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700898 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700899}
900
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700901TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
902 OmahaResponse response;
903 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700904 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700905
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700906 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700907 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
908
909 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800910 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700911 payload_state.DownloadProgress(num_bytes);
912
913 // Check that this was done via HTTP.
914 EXPECT_EQ(num_bytes,
915 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
916 EXPECT_EQ(num_bytes,
917 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
918
919 // Check that only HTTP is reported as a download source.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700920 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700921 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700922 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800923 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed,
924 (1 << kDownloadSourceHttpServer),
925 _, _, _));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700926
927 payload_state.UpdateSucceeded();
928}
929
Jay Srinivasan19409b72013-04-12 19:23:36 -0700930TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
931 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700932 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700933 PayloadState payload_state;
934
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700935 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700936
937 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700938 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700939
Alex Deymo80f70ff2016-02-10 16:08:11 -0800940 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700941 payload_state.DownloadProgress(num_bytes);
942 EXPECT_EQ(num_bytes,
943 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
944 EXPECT_EQ(num_bytes,
945 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800946 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700947 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800948 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700949 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
950
951 payload_state.UpdateRestarted();
952 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800953 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700954 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
955 EXPECT_EQ(num_bytes,
956 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
957}
958
Chris Sosabe45bef2013-04-09 18:25:12 -0700959TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700960 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700961 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700962
Alex Deymo8427b4a2014-11-05 14:00:32 -0800963 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700964 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700965 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
966
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700967 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700968
969 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800970 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700971
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700972 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700973 payload_state.UpdateResumed();
974 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800975 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700976
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700977 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700978 payload_state.UpdateResumed();
979 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800980 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700981
982 // Restart the update again to verify we set the num of reboots back to 0.
983 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800984 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700985}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700986
Chris Sosaaa18e162013-06-20 13:20:30 -0700987TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700988 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -0700989 PayloadState payload_state;
990
Alex Deymo8427b4a2014-11-05 14:00:32 -0800991 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700992 fake_system_state.mock_powerwash_safe_prefs();
993 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -0700994
995 // Verify pre-conditions are good.
996 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
997
998 // Mock out the os version and make sure it's blacklisted correctly.
999 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001000 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001001 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001002 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001003
1004 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1005 rollback_version));
1006 payload_state.Rollback();
1007
1008 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001009
1010 // Change it up a little and verify we load it correctly.
1011 rollback_version = "2345.0.1";
1012 // Let's verify we can reload it correctly.
1013 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1014 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1015 SetArgumentPointee<1>(rollback_version), Return(true)));
1016 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1017 rollback_version));
1018 payload_state.LoadRollbackVersion();
1019 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001020
David Zeuthen96197df2014-04-16 12:22:39 -07001021 // Check that we report only UpdateEngine.Rollback.* metrics in
1022 // UpdateSucceeded().
David Zeuthenafed4a12014-04-09 15:28:44 -07001023 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1024 .Times(0);
1025 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1026 .Times(0);
David Zeuthen96197df2014-04-16 12:22:39 -07001027 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1028 SendEnumToUMA(
1029 metrics::kMetricRollbackResult,
1030 static_cast<int>(metrics::RollbackResult::kSuccess),
1031 static_cast<int>(metrics::RollbackResult::kNumConstants)));
David Zeuthenafed4a12014-04-09 15:28:44 -07001032 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001033}
1034
David Zeuthenf413fe52013-04-22 14:04:39 -07001035TEST(PayloadStateTest, DurationsAreCorrect) {
1036 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001037 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001038 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001039 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001040 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001041 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001042
1043 // Set the clock to a well-known time - 1 second on the wall-clock
1044 // and 2 seconds on the monotonic clock
1045 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1046 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1047
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001048 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001049 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001050 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001051
1052 // Check that durations are correct for a successful update where
1053 // time has advanced 7 seconds on the wall clock and 4 seconds on
1054 // the monotonic clock.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001055 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001056 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1057 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1058 payload_state.UpdateSucceeded();
1059 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1060 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1061
1062 // Check that durations are reset when a new response comes in.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001063 SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001064 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1065 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1066
1067 // Advance time a bit (10 secs), simulate download progress and
1068 // check that durations are updated.
1069 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1070 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1071 payload_state.DownloadProgress(10);
1072 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1073 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1074
1075 // Now simulate a reboot by resetting monotonic time (to 5000) and
1076 // creating a new PayloadState object and check that we load the
1077 // durations correctly (e.g. they are the same as before).
1078 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1079 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001080 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001081 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001082 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001083 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1084 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001085
1086 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1087 // and check that the durations are increased accordingly.
1088 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1089 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1090 payload_state2.UpdateSucceeded();
1091 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001092 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1093 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001094}
1095
David Zeuthene4c58bf2013-06-18 17:26:50 -07001096TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1097 OmahaResponse response;
1098 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001099 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001100 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001101 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001102
1103 // Set the clock to a well-known time (t = 30 seconds).
1104 fake_clock.SetWallclockTime(Time::FromInternalValue(
1105 30 * Time::kMicrosecondsPerSecond));
1106
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001107 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001108 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001109 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001110
1111 // Make the update succeed.
1112 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1113 payload_state.UpdateSucceeded();
1114
1115 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001116 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001117
1118 // Now simulate a reboot and set the wallclock time to a later point
1119 // (t = 500 seconds). We do this by using a new PayloadState object
1120 // and checking that it emits the right UMA metric with the right
1121 // value.
1122 fake_clock.SetWallclockTime(Time::FromInternalValue(
1123 500 * Time::kMicrosecondsPerSecond));
1124 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001125 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001126
1127 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001128 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001129 metrics::kMetricTimeToRebootMinutes,
1130 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001131 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001132
1133 payload_state2.UpdateEngineStarted();
1134
1135 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001136 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001137}
1138
Alex Deymo569c4242013-07-24 12:01:01 -07001139TEST(PayloadStateTest, RestartAfterCrash) {
1140 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001141 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001142 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001143
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001144 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001145
David Zeuthen4e1d1492014-04-25 13:12:27 -07001146 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001147 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1148 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1149 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1150 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1151 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1152 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1153 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001154 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001155
1156 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001157 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001158 SendToUMA(_, _, _, _, _)).Times(0);
1159
1160 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001161 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001162
1163 payload_state.UpdateEngineStarted();
1164}
1165
David Zeuthen4e1d1492014-04-25 13:12:27 -07001166TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1167 PayloadState payload_state;
1168 FakeSystemState fake_system_state;
1169
1170 // If there's no marker at startup, ensure we don't report a metric.
1171 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1172 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1173 SendEnumToUMA(
1174 metrics::kMetricAttemptResult,
1175 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1176 _)).Times(0);
1177 payload_state.UpdateEngineStarted();
1178}
1179
1180TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1181 PayloadState payload_state;
1182 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001183 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001184
1185 // If we have a marker at startup, ensure it's reported and the
1186 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001187 fake_system_state.set_prefs(&fake_prefs);
1188 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001189
1190 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1191
1192 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1193 SendEnumToUMA(
1194 metrics::kMetricAttemptResult,
1195 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1196 _)).Times(1);
1197 payload_state.UpdateEngineStarted();
1198
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001199 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001200}
1201
1202TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1203 PayloadState payload_state;
1204 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001205 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001206
1207 // Make sure the marker is written and cleared during an attempt and
1208 // also that we DO NOT emit the metric (since the attempt didn't end
1209 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001210 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001211 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001212 OmahaResponse response;
1213 response.packages.resize(1);
1214 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001215
1216 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1217 .Times(AnyNumber());
1218 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1219 .Times(AnyNumber());
1220 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1221 SendEnumToUMA(
1222 metrics::kMetricAttemptResult,
1223 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1224 _)).Times(0);
1225
1226 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001227 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001228
1229 payload_state.UpdateRestarted();
1230
1231 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001232 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001233
1234 payload_state.UpdateSucceeded();
1235
1236 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001237 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001238}
1239
Jay Srinivasan53173b92013-05-17 17:13:01 -07001240TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1241 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001242 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001243 PayloadState payload_state;
1244
Jay Srinivasan53173b92013-05-17 17:13:01 -07001245 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001246 fake_system_state.set_device_policy(&disable_http_policy);
1247 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001248
1249 // Test with no device policy. Should default to allowing http.
1250 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1251 .WillRepeatedly(Return(false));
1252
1253 // Set the first response.
1254 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1255
1256 // Check that we use the HTTP URL since there is no value set for allowing
1257 // http.
1258 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1259
1260 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001261 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1262 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1263
Chris Sosaf7d80042013-08-22 16:45:17 -07001264 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001265 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1266
1267 // Check that we skip the HTTP URL and use only the HTTPS url.
1268 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1269
1270 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001271 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001272 payload_state.UpdateFailed(error);
1273
1274 // Check that we still skip the HTTP URL and use only the HTTPS url.
1275 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001276 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001277
1278 // Now, slightly change the response and set it again.
1279 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1280
1281 // Check that we still skip the HTTP URL and use only the HTTPS url.
1282 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1283
1284 // Now, pretend that the HTTP policy is turned on. We want to make sure
1285 // the new policy is honored.
1286 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001287 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001288 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1289 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1290
1291 // Now, set the same response using the same hash
1292 // so that we can test that the state is reset not because of the
1293 // hash but because of the policy change which results in candidate url
1294 // list change.
1295 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1296
1297 // Check that we use the HTTP URL now and the failure count is reset.
1298 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001299 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001300
1301 // Fake a failure and see if we're moving over to the HTTPS url and update
1302 // the URL switch count properly.
1303 payload_state.UpdateFailed(error);
1304 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001305 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1306 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001307}
1308
Alex Deymo1c656c42013-06-28 11:02:14 -07001309TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1310 OmahaResponse response;
1311 response.is_delta_payload = true;
1312 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001313 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001314
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001315 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001316 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1317
1318 // Simulate a successful download and update.
1319 payload_state.DownloadComplete();
1320
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001321 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001322 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001323 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001324 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001325 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001326 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1327 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001328 payload_state.UpdateSucceeded();
1329
1330 // Mock the request to a request where the delta was disabled but Omaha sends
1331 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001332 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001333 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001334 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001335
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001336 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001337 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1338
1339 payload_state.DownloadComplete();
1340
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001341 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001342 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1343 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001344 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001345 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1346 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001347 payload_state.UpdateSucceeded();
1348}
1349
1350TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1351 OmahaResponse response;
1352 response.is_delta_payload = false;
1353 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001354 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001355
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001356 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001357 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1358
1359 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001360 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001361 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001362 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001363
1364 // Simulate a successful download and update.
1365 payload_state.DownloadComplete();
1366
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001367 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001368 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001369 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001370 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1371 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001372 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001373 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1374 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001375 payload_state.UpdateSucceeded();
1376}
1377
1378TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1379 OmahaResponse response;
1380 response.is_delta_payload = false;
1381 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001382 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001383
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001384 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001385 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1386
Alex Deymo820cc702013-06-28 15:43:46 -07001387 // Mock the request to a request where the delta is enabled, although the
1388 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001389 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001390 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001391 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001392
1393 // Simulate a successful download and update.
1394 payload_state.DownloadComplete();
1395
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001396 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001397 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001398 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001399 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1400 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001401 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001402 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1403 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001404 payload_state.UpdateSucceeded();
1405}
1406
Alex Deymo42432912013-07-12 20:21:15 -07001407TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001408 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001409 OmahaResponse response;
1410 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001411 FakePrefs fake_prefs;
1412 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001413
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001414 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001415 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1416
1417 // Simulate a successful download and update.
1418 payload_state.DownloadComplete();
1419 payload_state.UpdateSucceeded();
1420 payload_state.ExpectRebootInNewVersion("Version:12345678");
1421
1422 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001423 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001424 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001425 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001426 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001427
1428 // Simulate a second update and reboot into the same environment, this should
1429 // send a value of 2.
1430 payload_state.ExpectRebootInNewVersion("Version:12345678");
1431
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001432 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001433 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001434 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001435 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001436
1437 // Simulate a third failed reboot to new version, but this time for a
1438 // different payload. This should send a value of 1 this time.
1439 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001440 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001441 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001442 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001443 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001444}
1445
1446TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001447 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001448 OmahaResponse response;
1449 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001450 FakePrefs fake_prefs;
1451 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001452
Alex Deymo763e7db2015-08-27 21:08:08 -07001453 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1454 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001455
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001456 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001457 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1458
1459 // Simulate a successful download and update.
1460 payload_state.DownloadComplete();
1461 payload_state.UpdateSucceeded();
1462 payload_state.ExpectRebootInNewVersion("Version:12345678");
1463
1464 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001465 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001466
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001467 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001468 metrics::kMetricFailedUpdateCount, _, _, _, _))
1469 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001470 payload_state.ReportFailedBootIfNeeded();
1471
Alex Deymo763e7db2015-08-27 21:08:08 -07001472 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001473 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001474 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001475 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001476}
1477
1478TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001479 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001480 OmahaResponse response;
1481 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001482 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001483
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001484 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001485 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001486 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1487
1488 // Simulate a successful download and update.
1489 payload_state.DownloadComplete();
1490 payload_state.UpdateSucceeded();
1491 payload_state.ExpectRebootInNewVersion("Version:12345678");
1492
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001493 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001494 metrics::kMetricFailedUpdateCount, _, _, _, _))
1495 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001496
1497 // Cancel the applied update.
1498 payload_state.ResetUpdateStatus();
1499
1500 // Simulate a reboot.
1501 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001502}
1503
1504TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001505 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001506 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001507 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001508
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001509 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001510 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001511
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001512 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001513 metrics::kMetricFailedUpdateCount, _, _, _, _))
1514 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001515
1516 // Simulate a reboot in this environment.
1517 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001518}
1519
David Zeuthendcba8092013-08-06 12:16:35 -07001520TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1521 OmahaResponse response;
1522 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001523 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001524 FakePrefs fake_prefs;
1525 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001526
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001527 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001528 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1529
1530 // Should allow exactly kMaxP2PAttempts...
1531 for (int n = 0; n < kMaxP2PAttempts; n++) {
1532 payload_state.P2PNewAttempt();
1533 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1534 }
1535 // ... but not more than that.
1536 payload_state.P2PNewAttempt();
1537 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001538}
1539
1540TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1541 OmahaResponse response;
1542 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001543 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001544 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001545 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001546
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001547 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001548 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001549 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001550 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1551
1552 // Set the clock to 1 second.
1553 Time epoch = Time::FromInternalValue(1000000);
1554 fake_clock.SetWallclockTime(epoch);
1555
1556 // Do an attempt - this will set the timestamp.
1557 payload_state.P2PNewAttempt();
1558
1559 // Check that the timestamp equals what we just set.
1560 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1561
1562 // Time hasn't advanced - this should work.
1563 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1564
1565 // Set clock to half the deadline - this should work.
1566 fake_clock.SetWallclockTime(epoch +
1567 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1568 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1569
1570 // Check that the first attempt timestamp hasn't changed just
1571 // because the wall-clock time changed.
1572 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1573
1574 // Set clock to _just_ before the deadline - this should work.
1575 fake_clock.SetWallclockTime(epoch +
1576 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1577 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1578
1579 // Set clock to _just_ after the deadline - this should not work.
1580 fake_clock.SetWallclockTime(epoch +
1581 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1582 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001583}
1584
1585TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1586 OmahaResponse response;
1587 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001588 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001589 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001590
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001591 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001592 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001593 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1594
1595 Time null_time = Time();
1596 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1597 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001598}
1599
1600TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1601 OmahaResponse response;
1602 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001603 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001604 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001605 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001606 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001607 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001608 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001609 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1610
1611 // Set the clock to something known.
1612 Time time = Time::FromInternalValue(12345);
1613 fake_clock.SetWallclockTime(time);
1614
1615 // New p2p attempt - as a side-effect this will update the p2p state vars.
1616 payload_state.P2PNewAttempt();
1617 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1618 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1619
1620 // Now create a new PayloadState and check that it loads the state
1621 // vars correctly.
1622 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001623 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001624 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1625 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001626}
1627
1628TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1629 OmahaResponse response;
1630 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001631 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001632 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001633 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001634 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001635 fake_system_state.set_prefs(&fake_prefs);
1636
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001637 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001638 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1639
1640 // Set the clock to something known.
1641 Time time = Time::FromInternalValue(12345);
1642 fake_clock.SetWallclockTime(time);
1643
1644 // New p2p attempt - as a side-effect this will update the p2p state vars.
1645 payload_state.P2PNewAttempt();
1646 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1647 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1648
1649 // Set a new response...
1650 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1651
1652 // ... and check that it clears the P2P state vars.
1653 Time null_time = Time();
1654 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1655 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001656}
1657
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001658} // namespace chromeos_update_engine