blob: 45461800dfba433ad977648b62484bc546f63a53 [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,
Sen Jiangcdd52062017-05-18 15:33:10 -070064 bool is_delta_payload,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080065 PayloadState* payload_state,
66 OmahaResponse* response) {
Sen Jiang0affc2c2017-02-10 15:55:05 -080067 response->packages.clear();
68 response->packages.push_back({.payload_urls = {"http://test", "https://test"},
69 .size = 523456789,
70 .metadata_size = 558123,
71 .metadata_signature = "metasign",
Sen Jiangcdd52062017-05-18 15:33:10 -070072 .hash = hash,
73 .is_delta = is_delta_payload});
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080074 response->max_failure_count_per_url = 3;
75 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080076 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070077
78 string expected_url_https_only =
Sen Jiang0affc2c2017-02-10 15:55:05 -080079 " NumURLs = 1\n"
80 " Candidate Url0 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070081
82 string expected_urls_both =
Sen Jiang0affc2c2017-02-10 15:55:05 -080083 " NumURLs = 2\n"
84 " Candidate Url0 = http://test\n"
85 " Candidate Url1 = https://test\n";
Jay Srinivasan53173b92013-05-17 17:13:01 -070086
Sen Jiang0affc2c2017-02-10 15:55:05 -080087 string expected_response_sign = base::StringPrintf(
88 "Payload 0:\n"
89 " Size = 523456789\n"
90 " Sha256 Hash = %s\n"
91 " Metadata Size = 558123\n"
92 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -070093 " Is Delta = %d\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -080094 "%s"
Sen Jiang0affc2c2017-02-10 15:55:05 -080095 "Max Failure Count Per Url = %d\n"
96 "Disable Payload Backoff = %d\n",
97 hash.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -070098 response->packages[0].is_delta,
Sen Jiang0affc2c2017-02-10 15:55:05 -080099 (http_enabled ? expected_urls_both : expected_url_https_only).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800100 response->max_failure_count_per_url,
101 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800102 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800103}
104
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800105class PayloadStateTest : public ::testing::Test { };
106
107TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
108 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700109 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800110 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700111 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700112 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
113 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700114 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
115 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700116 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
117 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
118 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
119 .Times(AtLeast(1));
120 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
121 .Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
123 .Times(AtLeast(1));
124 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
125 .Times(AtLeast(1));
126 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
127 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700128 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
129 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700130 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800131 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700132 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800133 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800134 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800135 string expected_response_sign =
Sen Jiang0affc2c2017-02-10 15:55:05 -0800136 "Max Failure Count Per Url = 0\n"
137 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800138 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700139 EXPECT_EQ("", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800140 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
141 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700142 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800143}
144
145TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
146 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800147 response.packages.push_back({.payload_urls = {"https://single.url.test"},
148 .size = 123456789,
149 .metadata_size = 58123,
150 .metadata_signature = "msign",
151 .hash = "hash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700152 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800153 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700154 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700155 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
156 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700157 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
158 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700159 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
160 .Times(AtLeast(1));
161 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
162 .Times(AtLeast(1));
163 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
164 .Times(AtLeast(1));
165 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
166 .Times(AtLeast(1));
167 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
168 .Times(AtLeast(1));
169 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
170 .Times(AtLeast(1));
171 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
172 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700173 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
174 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700175 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
176 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700178 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800180 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800181 string expected_response_sign =
182 "Payload 0:\n"
183 " Size = 123456789\n"
184 " Sha256 Hash = hash\n"
185 " Metadata Size = 58123\n"
186 " Metadata Signature = msign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700187 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800188 " NumURLs = 1\n"
189 " Candidate Url0 = https://single.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800190 "Max Failure Count Per Url = 0\n"
191 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800192 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700193 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800194 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
195 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700196 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800197}
198
199TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
200 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800201 response.packages.push_back({.payload_urls = {"http://multiple.url.test",
202 "https://multiple.url.test"},
203 .size = 523456789,
204 .metadata_size = 558123,
205 .metadata_signature = "metasign",
206 .hash = "rhash"});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700207 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800208 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700209 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700210 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
211 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700212 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
213 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700214 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
215 .Times(AtLeast(1));
216 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
217 .Times(AtLeast(1));
218 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
219 .Times(AtLeast(1));
220 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
221 .Times(AtLeast(1));
222 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
223 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700224 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
225 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700226 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
227 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700228
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700230 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800231 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800232 string stored_response_sign = payload_state.GetResponseSignature();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800233 string expected_response_sign =
234 "Payload 0:\n"
235 " Size = 523456789\n"
236 " Sha256 Hash = rhash\n"
237 " Metadata Size = 558123\n"
238 " Metadata Signature = metasign\n"
Sen Jiangcdd52062017-05-18 15:33:10 -0700239 " Is Delta = 0\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800240 " NumURLs = 2\n"
241 " Candidate Url0 = http://multiple.url.test\n"
242 " Candidate Url1 = https://multiple.url.test\n"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800243 "Max Failure Count Per Url = 0\n"
244 "Disable Payload Backoff = 0\n";
Jay Srinivasan08262882012-12-28 19:29:43 -0800245 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700246 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800247 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
248 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700249 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250}
251
252TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
253 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700254 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800255 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800256 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800257
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700258 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800259 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700260 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
261 .Times(AtLeast(1));
262 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
263 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700264 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
265 .Times(AtLeast(1));
266 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
267 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700268 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700269
Chris Sosabe45bef2013-04-09 18:25:12 -0700270 // Reboots will be set
271 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
272
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800273 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700274 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
275 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800276
277 // Failure count should be called each times url index is set, so that's
278 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700279 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
280 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800281
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700282 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800283
284 // This does a SetResponse which causes all the states to be set to 0 for
285 // the first time.
Sen Jiangcdd52062017-05-18 15:33:10 -0700286 SetupPayloadStateWith2Urls(
287 "Hash1235", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700288 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289
290 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700291 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700293 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294
295 // Verify that on the next error, the URL index wraps around to 0.
296 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700297 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800298
299 // Verify that on the next error, it again advances to 1.
300 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700301 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700302
303 // Verify that we switched URLs three times
Alex Deymo80f70ff2016-02-10 16:08:11 -0800304 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800305}
306
307TEST(PayloadStateTest, NewResponseResetsPayloadState) {
308 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700309 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800310 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800311
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700312 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800313
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700314 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800315 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700316 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800317 .Times(AnyNumber());
318
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800319 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700320 SetupPayloadStateWith2Urls(
321 "Hash5823", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700322 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800323
324 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700325 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800326 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700327 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800328 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800329
330 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700331 SetupPayloadStateWith2Urls(
332 "Hash8225", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700333 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800334
Alex Deymob33b0f02013-08-08 21:10:02 -0700335 // Fake an error again.
336 payload_state.UpdateFailed(error);
337 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800338 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Alex Deymob33b0f02013-08-08 21:10:02 -0700339
Alex Deymob33b0f02013-08-08 21:10:02 -0700340 // Return a third different response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700341 SetupPayloadStateWith2Urls(
342 "Hash9999", true, false, &payload_state, &response);
Alex Deymob33b0f02013-08-08 21:10:02 -0700343 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
344
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800345 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700346 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800347 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
348 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
349 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700350 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800351 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700352 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800353 EXPECT_EQ(
354 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer));
355 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700356 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800357}
358
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800359TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
360 OmahaResponse response;
361 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700362 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700363 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800364 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800365
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700366 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700367 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
368 .Times(AtLeast(2));
369 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
370 .Times(AtLeast(1));
371 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
372 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800373
Alex Deymo820cc702013-06-28 15:43:46 -0700374 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
375 .Times(AtLeast(2));
376 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
377 .Times(AtLeast(1));
378 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
379 .Times(AtLeast(1));
380
Jay Srinivasan19409b72013-04-12 19:23:36 -0700381 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800382
Jay Srinivasan19409b72013-04-12 19:23:36 -0700383 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
384 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800385
Jay Srinivasan19409b72013-04-12 19:23:36 -0700386 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
387 .Times(AtLeast(7));
388 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
389 .Times(AtLeast(2));
390 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
391 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800392
Jay Srinivasan19409b72013-04-12 19:23:36 -0700393 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
394 .Times(AtLeast(1));
395 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
396 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700397
Jay Srinivasan19409b72013-04-12 19:23:36 -0700398 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
399 .Times(AtLeast(1));
400 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
401 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700402 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
403 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700404 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
405 .Times(AtLeast(1));
406 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
407 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700408 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
409 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700410
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700411 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800412
Sen Jiangcdd52062017-05-18 15:33:10 -0700413 SetupPayloadStateWith2Urls(
414 "Hash5873", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700415 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800416
417 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700418 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800419 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700420 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700421 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800422 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
423 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800424
425 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700426 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800427 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700428 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700429 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800430 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
431 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800432
433 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700434 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800435 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700436 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700437 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800438 EXPECT_EQ(2U, payload_state.GetUrlFailureCount());
439 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800440
441 // This should advance the URL index as we've reached the
442 // max failure count and reset the failure count for the new URL index.
443 // This should also wrap around the URL index and thus cause the payload
444 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700445 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800446 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700447 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700448 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800449 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
450 EXPECT_EQ(2U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800451 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800452
453 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700454 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800455 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700456 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700457 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800458 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
459 EXPECT_EQ(3U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800460 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800461
462 // This should advance the URL index and payload attempt number due to
463 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700464 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800465 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700466 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700467 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800468 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
469 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800470 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800471
472 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700473 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700474 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800475 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700476 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700477 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800478 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
479 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800480 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800481
482 // And that failure count should be reset when we download some bytes
483 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700484 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800485 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700486 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700487 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800488 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
489 EXPECT_EQ(4U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800490 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800491
492 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -0700493 SetupPayloadStateWith2Urls(
494 "Hash8532", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700495 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800496
497 // Make sure the url index was reset to 0 because of the new response.
498 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700499 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700500 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800501 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
502 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800503 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800504}
505
Alex Deymo820cc702013-06-28 15:43:46 -0700506TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800507 OmahaResponse response;
508 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700509 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800510 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800511
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700512 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700513 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
514 .Times(AtLeast(1));
515 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
516 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800517
Alex Deymo820cc702013-06-28 15:43:46 -0700518 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
519 .Times(AtLeast(1));
520 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
521 .Times(AtLeast(1));
522
Jay Srinivasan19409b72013-04-12 19:23:36 -0700523 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
524 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800525
Jay Srinivasan19409b72013-04-12 19:23:36 -0700526 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
527 .Times(AtLeast(1));
528 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
529 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800530
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700531 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800532
Sen Jiangcdd52062017-05-18 15:33:10 -0700533 SetupPayloadStateWith2Urls(
534 "Hash8593", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800535
536 // This should just advance the payload attempt number;
537 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700538 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800539 payload_state.DownloadComplete();
540 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700541 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
542 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800543 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
544 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Alex Deymo820cc702013-06-28 15:43:46 -0700545}
546
547TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
548 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700549 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700550 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800551 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700552
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700553 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700554 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
555 .Times(AtLeast(1));
556 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
557 .Times(AtLeast(1));
558
559 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
560 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
561 .Times(AtLeast(1));
562
563 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
564 .Times(1);
565
566 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
567 .Times(AtLeast(1));
568 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
569 .Times(AtLeast(1));
570
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700571 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700572
Sen Jiangcdd52062017-05-18 15:33:10 -0700573 SetupPayloadStateWith2Urls("Hash8593", true, true, &payload_state, &response);
Alex Deymo820cc702013-06-28 15:43:46 -0700574
575 // This should just advance the payload attempt number;
576 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
577 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
578 payload_state.DownloadComplete();
579 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
580 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700581 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800582 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
583 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800584}
585
586TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
587 OmahaResponse response;
588 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700589 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800590
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700591 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700592 SetupPayloadStateWith2Urls(
593 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800594
595 // Generate enough events to advance URL index, failure count and
596 // payload attempt number all to 1.
597 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700598 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
599 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800600 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700601 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700602 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800603 EXPECT_EQ(1U, payload_state.GetUrlFailureCount());
604 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800605
606 // Now, simulate a corrupted url index on persisted store which gets
607 // loaded when update_engine restarts. Using a different prefs object
608 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700609 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800610 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700611 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700612 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700613 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
614 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700615 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
616 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700617 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
618 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
619 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
620 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700621 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
622 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800623
624 // Note: This will be a different payload object, but the response should
625 // have the same hash as before so as to not trivially reset because the
626 // response was different. We want to specifically test that even if the
627 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700628 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Sen Jiangcdd52062017-05-18 15:33:10 -0700629 SetupPayloadStateWith2Urls(
630 "Hash4427", true, false, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800631
632 // Make sure all counters get reset to 0 because of the corrupted URL index
633 // we supplied above.
634 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700635 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700636 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800637 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
638 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800639}
Jay Srinivasan08262882012-12-28 19:29:43 -0800640
Chris Sosa20f005c2013-09-05 13:53:08 -0700641TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
642 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700643 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700644 FakeSystemState fake_system_state;
645 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700646 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700647 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700648
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700650 SetupPayloadStateWith2Urls(
651 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700652
653 // Simulate two failures (enough to cause payload backoff) and check
654 // again that we're ready to re-download without any backoff as this is
655 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700656 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
657 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700658 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
659 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
660 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
661 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
662}
663
664TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
665 OmahaResponse response;
Chris Sosa20f005c2013-09-05 13:53:08 -0700666 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700667 FakeSystemState fake_system_state;
668 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700669 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700670 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700671
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700672 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700673 SetupPayloadStateWith2Urls(
674 "Hash6437", true, false, &payload_state, &response);
Chris Sosa20f005c2013-09-05 13:53:08 -0700675
676 // Simulate two failures (enough to cause payload backoff) and check
677 // again that we're ready to re-download without any backoff as this is
678 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700679 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
680 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700681 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
682 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
683 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
684 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700685 payload_state.SetUsingP2PForDownloading(true);
686 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700687 // Should not backoff for p2p updates.
688 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
689
Gilad Arnold74b5f552014-10-07 08:17:16 -0700690 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700691 // No actual p2p update if no url is provided.
692 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
693}
694
Jay Srinivasan08262882012-12-28 19:29:43 -0800695TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
696 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800697 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700698 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800699
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700700 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700701 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800702
703 // Simulate a successful download and see that we're ready to download
704 // again without any backoff as this is a delta payload.
705 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700706 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
707 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800708 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
709
710 // Simulate two failures (enough to cause payload backoff) and check
711 // again that we're ready to re-download without any backoff as this is
712 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700713 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
714 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700715 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700716 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
717 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800718 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
719}
720
721static void CheckPayloadBackoffState(PayloadState* payload_state,
722 int expected_attempt_number,
723 TimeDelta expected_days) {
724 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700725 EXPECT_EQ(expected_attempt_number,
726 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800727 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
728 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
729 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
730 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
731 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
732 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
733 EXPECT_LT(expected_min_time.ToInternalValue(),
734 backoff_expiry_time.ToInternalValue());
735 EXPECT_GT(expected_max_time.ToInternalValue(),
736 backoff_expiry_time.ToInternalValue());
737}
738
739TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
740 OmahaResponse response;
Jay Srinivasan08262882012-12-28 19:29:43 -0800741 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700742 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800743
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700744 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700745 SetupPayloadStateWith2Urls(
746 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800747
748 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
749 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
750 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
751 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
752 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
753 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
754 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
755 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
756 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
757 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
758}
759
760TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
761 OmahaResponse response;
762 response.disable_payload_backoff = true;
763 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700764 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800765
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700766 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700767 SetupPayloadStateWith2Urls(
768 "Hash8939", true, false, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800769
770 // Simulate a successful download and see that we are ready to download
771 // again without any backoff.
772 payload_state.DownloadComplete();
773 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700774 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800775 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
776
777 // Test again, this time by simulating two errors that would cause
778 // the payload attempt number to increment due to wrap around. And
779 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700780 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
781 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800782 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700783 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800784 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
785}
786
Jay Srinivasan19409b72013-04-12 19:23:36 -0700787TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
788 OmahaResponse response;
789 response.disable_payload_backoff = true;
790 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700791 FakeSystemState fake_system_state;
Alex Deymo80f70ff2016-02-10 16:08:11 -0800792 uint64_t https_total = 0;
793 uint64_t http_total = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700794
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700795 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700796 SetupPayloadStateWith2Urls(
797 "Hash3286", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700798 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700799
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700800 // Simulate a previous attempt with in order to set an initial non-zero value
801 // for the total bytes downloaded for HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800802 uint64_t prev_chunk = 323456789;
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700803 http_total += prev_chunk;
804 payload_state.DownloadProgress(prev_chunk);
805
806 // Ensure that the initial values for HTTP reflect this attempt.
807 EXPECT_EQ(prev_chunk,
808 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
809 EXPECT_EQ(http_total,
810 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
811
812 // Change the response hash so as to simulate a new response which will
813 // reset the current bytes downloaded, but not the total bytes downloaded.
Sen Jiangcdd52062017-05-18 15:33:10 -0700814 SetupPayloadStateWith2Urls(
815 "Hash9904", true, false, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700816 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700817
818 // First, simulate successful download of a few bytes over HTTP.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800819 uint64_t first_chunk = 5000000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700820 http_total += first_chunk;
821 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700822 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700823 EXPECT_EQ(first_chunk,
824 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
825 EXPECT_EQ(http_total,
826 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800827 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700828 kDownloadSourceHttpsServer));
829 EXPECT_EQ(https_total,
830 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
831
832 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700833 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700834 payload_state.UpdateFailed(error);
835
Jay Srinivasan53173b92013-05-17 17:13:01 -0700836 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800837 uint64_t second_chunk = 23456789;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700838 https_total += second_chunk;
839 payload_state.DownloadProgress(second_chunk);
840 EXPECT_EQ(first_chunk,
841 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
842 EXPECT_EQ(http_total,
843 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
844 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
845 kDownloadSourceHttpsServer));
846 EXPECT_EQ(https_total,
847 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
848
849 // Simulate error to go back to http.
850 payload_state.UpdateFailed(error);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800851 uint64_t third_chunk = 32345678;
852 uint64_t http_chunk = first_chunk + third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700853 http_total += third_chunk;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700854 payload_state.DownloadProgress(third_chunk);
855
856 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
857 EXPECT_EQ(http_chunk,
858 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700859 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700860 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
861 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
862 kDownloadSourceHttpsServer));
863 EXPECT_EQ(https_total,
864 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
865
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700866 // Simulate error (will cause URL switch), set p2p is to be used and
867 // then do 42MB worth of progress
868 payload_state.UpdateFailed(error);
869 payload_state.SetUsingP2PForDownloading(true);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800870 uint64_t p2p_total = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700871 payload_state.DownloadProgress(p2p_total);
872
873 EXPECT_EQ(p2p_total,
874 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
875
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700876 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700877 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700878 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800879 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700880 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800881 metrics::kMetricSuccessfulUpdateUrlSwitchCount,
882 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700883 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800884 metrics::kMetricSuccessfulUpdateTotalDurationMinutes,
885 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700886 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800887 metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
888 314, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700889 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800890 metrics::kMetricAttemptPayloadType, kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700891 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800892 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
893 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700894 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800895 metrics::kMetricSuccessfulUpdateAttemptCount, 1, _, _, _));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700896
897 payload_state.UpdateSucceeded();
898
899 // Make sure the metrics are reset after a successful update.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800900 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700901 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800902 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700903 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800904 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700905 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800906 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700907 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700908 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700909}
910
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700911TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
912 OmahaResponse response;
913 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700914 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700915
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700916 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -0700917 SetupPayloadStateWith2Urls(
918 "Hash3286", true, false, &payload_state, &response);
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700919
920 // Simulate progress in order to mark HTTP as one of the sources used.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800921 uint64_t num_bytes = 42 * 1000 * 1000;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700922 payload_state.DownloadProgress(num_bytes);
923
924 // Check that this was done via HTTP.
925 EXPECT_EQ(num_bytes,
926 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
927 EXPECT_EQ(num_bytes,
928 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
929
930 // Check that only HTTP is reported as a download source.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700931 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700932 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700933 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800934 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed,
935 (1 << kDownloadSourceHttpServer),
936 _, _, _));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700937
938 payload_state.UpdateSucceeded();
939}
940
Jay Srinivasan19409b72013-04-12 19:23:36 -0700941TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
942 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700943 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700944 PayloadState payload_state;
945
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700946 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700947
948 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -0700949 SetupPayloadStateWith2Urls(
950 "Hash5823", true, false, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700951
Alex Deymo80f70ff2016-02-10 16:08:11 -0800952 uint64_t num_bytes = 10000;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700953 payload_state.DownloadProgress(num_bytes);
954 EXPECT_EQ(num_bytes,
955 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
956 EXPECT_EQ(num_bytes,
957 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800958 EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700959 kDownloadSourceHttpsServer));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800960 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700961 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
962
963 payload_state.UpdateRestarted();
964 // Make sure the current bytes downloaded is reset, but not the total bytes.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800965 EXPECT_EQ(0U,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700966 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
967 EXPECT_EQ(num_bytes,
968 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
969}
970
Chris Sosabe45bef2013-04-09 18:25:12 -0700971TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700972 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700973 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700974
Alex Deymo8427b4a2014-11-05 14:00:32 -0800975 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700976 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700977 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
978
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700979 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700980
981 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800982 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700983
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700984 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700985 payload_state.UpdateResumed();
986 // Num reboots should be incremented because system rebooted detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800987 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700988
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700989 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700990 payload_state.UpdateResumed();
991 // Num reboots should now be 1 as reboot was not detected.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800992 EXPECT_EQ(1U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700993
994 // Restart the update again to verify we set the num of reboots back to 0.
995 payload_state.UpdateRestarted();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800996 EXPECT_EQ(0U, payload_state.GetNumReboots());
Chris Sosabe45bef2013-04-09 18:25:12 -0700997}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700998
Chris Sosaaa18e162013-06-20 13:20:30 -0700999TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001000 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -07001001 PayloadState payload_state;
1002
Alex Deymo8427b4a2014-11-05 14:00:32 -08001003 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001004 fake_system_state.mock_powerwash_safe_prefs();
1005 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -07001006
1007 // Verify pre-conditions are good.
1008 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1009
1010 // Mock out the os version and make sure it's blacklisted correctly.
1011 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001012 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001013 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001014 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001015
1016 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1017 rollback_version));
1018 payload_state.Rollback();
1019
1020 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001021
1022 // Change it up a little and verify we load it correctly.
1023 rollback_version = "2345.0.1";
1024 // Let's verify we can reload it correctly.
1025 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1026 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1027 SetArgumentPointee<1>(rollback_version), Return(true)));
1028 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1029 rollback_version));
1030 payload_state.LoadRollbackVersion();
1031 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001032
David Zeuthen96197df2014-04-16 12:22:39 -07001033 // Check that we report only UpdateEngine.Rollback.* metrics in
1034 // UpdateSucceeded().
David Zeuthenafed4a12014-04-09 15:28:44 -07001035 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1036 .Times(0);
1037 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1038 .Times(0);
David Zeuthen96197df2014-04-16 12:22:39 -07001039 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1040 SendEnumToUMA(
1041 metrics::kMetricRollbackResult,
1042 static_cast<int>(metrics::RollbackResult::kSuccess),
1043 static_cast<int>(metrics::RollbackResult::kNumConstants)));
David Zeuthenafed4a12014-04-09 15:28:44 -07001044 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001045}
1046
David Zeuthenf413fe52013-04-22 14:04:39 -07001047TEST(PayloadStateTest, DurationsAreCorrect) {
1048 OmahaResponse response;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001049 response.packages.resize(1);
David Zeuthenf413fe52013-04-22 14:04:39 -07001050 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001051 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001052 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001053 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001054
1055 // Set the clock to a well-known time - 1 second on the wall-clock
1056 // and 2 seconds on the monotonic clock
1057 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1058 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1059
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001060 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001061 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001062 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001063
1064 // Check that durations are correct for a successful update where
1065 // time has advanced 7 seconds on the wall clock and 4 seconds on
1066 // the monotonic clock.
Sen Jiangcdd52062017-05-18 15:33:10 -07001067 SetupPayloadStateWith2Urls(
1068 "Hash8593", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001069 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1070 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1071 payload_state.UpdateSucceeded();
1072 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1073 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1074
1075 // Check that durations are reset when a new response comes in.
Sen Jiangcdd52062017-05-18 15:33:10 -07001076 SetupPayloadStateWith2Urls(
1077 "Hash8594", true, false, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001078 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1079 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1080
1081 // Advance time a bit (10 secs), simulate download progress and
1082 // check that durations are updated.
1083 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1084 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1085 payload_state.DownloadProgress(10);
1086 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1087 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1088
1089 // Now simulate a reboot by resetting monotonic time (to 5000) and
1090 // creating a new PayloadState object and check that we load the
1091 // durations correctly (e.g. they are the same as before).
1092 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1093 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001094 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001095 payload_state2.SetResponse(response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001096 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001097 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1098 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001099
1100 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1101 // and check that the durations are increased accordingly.
1102 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1103 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1104 payload_state2.UpdateSucceeded();
1105 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001106 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1107 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001108}
1109
David Zeuthene4c58bf2013-06-18 17:26:50 -07001110TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1111 OmahaResponse response;
1112 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001113 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001114 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001115 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001116
1117 // Set the clock to a well-known time (t = 30 seconds).
1118 fake_clock.SetWallclockTime(Time::FromInternalValue(
1119 30 * Time::kMicrosecondsPerSecond));
1120
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001121 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001122 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001123 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001124
1125 // Make the update succeed.
Sen Jiangcdd52062017-05-18 15:33:10 -07001126 SetupPayloadStateWith2Urls(
1127 "Hash8593", true, false, &payload_state, &response);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001128 payload_state.UpdateSucceeded();
1129
1130 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001131 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001132
1133 // Now simulate a reboot and set the wallclock time to a later point
1134 // (t = 500 seconds). We do this by using a new PayloadState object
1135 // and checking that it emits the right UMA metric with the right
1136 // value.
1137 fake_clock.SetWallclockTime(Time::FromInternalValue(
1138 500 * Time::kMicrosecondsPerSecond));
1139 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001140 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001141
1142 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001143 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001144 metrics::kMetricTimeToRebootMinutes,
1145 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001146 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001147
1148 payload_state2.UpdateEngineStarted();
1149
1150 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001151 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001152}
1153
Alex Deymo569c4242013-07-24 12:01:01 -07001154TEST(PayloadStateTest, RestartAfterCrash) {
1155 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001156 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001157 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001158
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001159 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001160
David Zeuthen4e1d1492014-04-25 13:12:27 -07001161 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001162 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1163 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1164 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1165 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1166 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1167 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1168 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001169 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001170
1171 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001172 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001173 SendToUMA(_, _, _, _, _)).Times(0);
1174
1175 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001176 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001177
1178 payload_state.UpdateEngineStarted();
1179}
1180
David Zeuthen4e1d1492014-04-25 13:12:27 -07001181TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1182 PayloadState payload_state;
1183 FakeSystemState fake_system_state;
1184
1185 // If there's no marker at startup, ensure we don't report a metric.
1186 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1187 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1188 SendEnumToUMA(
1189 metrics::kMetricAttemptResult,
1190 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1191 _)).Times(0);
1192 payload_state.UpdateEngineStarted();
1193}
1194
1195TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1196 PayloadState payload_state;
1197 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001198 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001199
1200 // If we have a marker at startup, ensure it's reported and the
1201 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001202 fake_system_state.set_prefs(&fake_prefs);
1203 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001204
1205 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1206
1207 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1208 SendEnumToUMA(
1209 metrics::kMetricAttemptResult,
1210 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1211 _)).Times(1);
1212 payload_state.UpdateEngineStarted();
1213
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001214 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001215}
1216
1217TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1218 PayloadState payload_state;
1219 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001220 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001221
1222 // Make sure the marker is written and cleared during an attempt and
1223 // also that we DO NOT emit the metric (since the attempt didn't end
1224 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001225 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001226 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiang0affc2c2017-02-10 15:55:05 -08001227 OmahaResponse response;
1228 response.packages.resize(1);
1229 payload_state.SetResponse(response);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001230
1231 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1232 .Times(AnyNumber());
1233 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1234 .Times(AnyNumber());
1235 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1236 SendEnumToUMA(
1237 metrics::kMetricAttemptResult,
1238 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1239 _)).Times(0);
1240
1241 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001242 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001243
1244 payload_state.UpdateRestarted();
1245
1246 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001247 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001248
1249 payload_state.UpdateSucceeded();
1250
1251 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001252 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001253}
1254
Jay Srinivasan53173b92013-05-17 17:13:01 -07001255TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1256 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001257 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001258 PayloadState payload_state;
1259
Jay Srinivasan53173b92013-05-17 17:13:01 -07001260 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001261 fake_system_state.set_device_policy(&disable_http_policy);
1262 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001263
1264 // Test with no device policy. Should default to allowing http.
1265 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1266 .WillRepeatedly(Return(false));
1267
1268 // Set the first response.
Sen Jiangcdd52062017-05-18 15:33:10 -07001269 SetupPayloadStateWith2Urls(
1270 "Hash8433", true, false, &payload_state, &response);
Chris Sosaf7d80042013-08-22 16:45:17 -07001271
1272 // Check that we use the HTTP URL since there is no value set for allowing
1273 // http.
1274 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1275
1276 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001277 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1278 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1279
Chris Sosaf7d80042013-08-22 16:45:17 -07001280 // Reset state and set again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001281 SetupPayloadStateWith2Urls(
1282 "Hash8433", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001283
1284 // Check that we skip the HTTP URL and use only the HTTPS url.
1285 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1286
1287 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001288 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001289 payload_state.UpdateFailed(error);
1290
1291 // Check that we still skip the HTTP URL and use only the HTTPS url.
1292 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001293 EXPECT_EQ(0U, payload_state.GetUrlSwitchCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001294
1295 // Now, slightly change the response and set it again.
Sen Jiangcdd52062017-05-18 15:33:10 -07001296 SetupPayloadStateWith2Urls(
1297 "Hash2399", false, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001298
1299 // Check that we still skip the HTTP URL and use only the HTTPS url.
1300 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1301
1302 // Now, pretend that the HTTP policy is turned on. We want to make sure
1303 // the new policy is honored.
1304 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001305 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001306 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1307 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1308
1309 // Now, set the same response using the same hash
1310 // so that we can test that the state is reset not because of the
1311 // hash but because of the policy change which results in candidate url
1312 // list change.
Sen Jiangcdd52062017-05-18 15:33:10 -07001313 SetupPayloadStateWith2Urls(
1314 "Hash2399", true, false, &payload_state, &response);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001315
1316 // Check that we use the HTTP URL now and the failure count is reset.
1317 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001318 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001319
1320 // Fake a failure and see if we're moving over to the HTTPS url and update
1321 // the URL switch count properly.
1322 payload_state.UpdateFailed(error);
1323 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Alex Deymo80f70ff2016-02-10 16:08:11 -08001324 EXPECT_EQ(1U, payload_state.GetUrlSwitchCount());
1325 EXPECT_EQ(0U, payload_state.GetUrlFailureCount());
Jay Srinivasan53173b92013-05-17 17:13:01 -07001326}
1327
Alex Deymo1c656c42013-06-28 11:02:14 -07001328TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1329 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001330 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001331 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001332
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001333 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001334 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001335
1336 // Simulate a successful download and update.
1337 payload_state.DownloadComplete();
1338
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001339 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001340 .Times(AnyNumber());
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, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001343 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001344 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1345 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001346 payload_state.UpdateSucceeded();
1347
1348 // Mock the request to a request where the delta was disabled but Omaha sends
1349 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001350 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001351 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001352 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001353
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001354 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001355 SetupPayloadStateWith2Urls("Hash6437", true, true, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001356
1357 payload_state.DownloadComplete();
1358
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001359 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001360 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1361 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001362 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001363 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1364 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001365 payload_state.UpdateSucceeded();
1366}
1367
1368TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1369 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001370 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001371 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001372
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001373 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001374 SetupPayloadStateWith2Urls(
1375 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001376
1377 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001378 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001379 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001380 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001381
1382 // Simulate a successful download and update.
1383 payload_state.DownloadComplete();
1384
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001385 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001386 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001387 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001388 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1389 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001390 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001391 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1392 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001393 payload_state.UpdateSucceeded();
1394}
1395
1396TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1397 OmahaResponse response;
Alex Deymo1c656c42013-06-28 11:02:14 -07001398 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001399 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001400
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001401 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001402 SetupPayloadStateWith2Urls(
1403 "Hash6437", true, false, &payload_state, &response);
Alex Deymo1c656c42013-06-28 11:02:14 -07001404
Alex Deymo820cc702013-06-28 15:43:46 -07001405 // Mock the request to a request where the delta is enabled, although the
1406 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001407 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001408 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001409 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001410
1411 // Simulate a successful download and update.
1412 payload_state.DownloadComplete();
1413
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001414 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001415 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001416 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001417 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1418 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001419 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001420 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1421 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001422 payload_state.UpdateSucceeded();
1423}
1424
Alex Deymo42432912013-07-12 20:21:15 -07001425TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001426 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001427 OmahaResponse response;
1428 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001429 FakePrefs fake_prefs;
1430 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001431
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001432 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001433 SetupPayloadStateWith2Urls(
1434 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001435
1436 // Simulate a successful download and update.
1437 payload_state.DownloadComplete();
1438 payload_state.UpdateSucceeded();
1439 payload_state.ExpectRebootInNewVersion("Version:12345678");
1440
1441 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001442 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001443 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001444 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001445 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001446
1447 // Simulate a second update and reboot into the same environment, this should
1448 // send a value of 2.
1449 payload_state.ExpectRebootInNewVersion("Version:12345678");
1450
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001451 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001452 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001453 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001454 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001455
1456 // Simulate a third failed reboot to new version, but this time for a
1457 // different payload. This should send a value of 1 this time.
1458 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001459 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001460 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001461 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001462 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001463}
1464
1465TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001466 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001467 OmahaResponse response;
1468 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001469 FakePrefs fake_prefs;
1470 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001471
Alex Deymo763e7db2015-08-27 21:08:08 -07001472 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1473 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001474
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001475 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001476 SetupPayloadStateWith2Urls(
1477 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001478
1479 // Simulate a successful download and update.
1480 payload_state.DownloadComplete();
1481 payload_state.UpdateSucceeded();
1482 payload_state.ExpectRebootInNewVersion("Version:12345678");
1483
1484 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001485 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001486
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001487 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001488 metrics::kMetricFailedUpdateCount, _, _, _, _))
1489 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001490 payload_state.ReportFailedBootIfNeeded();
1491
Alex Deymo763e7db2015-08-27 21:08:08 -07001492 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001493 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001494 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001495 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001496}
1497
1498TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001499 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001500 OmahaResponse response;
1501 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001502 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001503
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001504 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001505 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001506 SetupPayloadStateWith2Urls(
1507 "Hash3141", true, false, &payload_state, &response);
Alex Deymo42432912013-07-12 20:21:15 -07001508
1509 // Simulate a successful download and update.
1510 payload_state.DownloadComplete();
1511 payload_state.UpdateSucceeded();
1512 payload_state.ExpectRebootInNewVersion("Version:12345678");
1513
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001514 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001515 metrics::kMetricFailedUpdateCount, _, _, _, _))
1516 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001517
1518 // Cancel the applied update.
1519 payload_state.ResetUpdateStatus();
1520
1521 // Simulate a reboot.
1522 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001523}
1524
1525TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001526 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001527 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001528 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001529
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001530 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001531 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001532
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001533 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001534 metrics::kMetricFailedUpdateCount, _, _, _, _))
1535 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001536
1537 // Simulate a reboot in this environment.
1538 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001539}
1540
David Zeuthendcba8092013-08-06 12:16:35 -07001541TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1542 OmahaResponse response;
1543 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001544 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001545 FakePrefs fake_prefs;
1546 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001547
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001548 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001549 SetupPayloadStateWith2Urls(
1550 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001551
1552 // Should allow exactly kMaxP2PAttempts...
1553 for (int n = 0; n < kMaxP2PAttempts; n++) {
1554 payload_state.P2PNewAttempt();
1555 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1556 }
1557 // ... but not more than that.
1558 payload_state.P2PNewAttempt();
1559 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001560}
1561
1562TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1563 OmahaResponse response;
1564 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001565 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001566 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001567 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001568
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001569 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001570 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001571 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001572 SetupPayloadStateWith2Urls(
1573 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001574
1575 // Set the clock to 1 second.
1576 Time epoch = Time::FromInternalValue(1000000);
1577 fake_clock.SetWallclockTime(epoch);
1578
1579 // Do an attempt - this will set the timestamp.
1580 payload_state.P2PNewAttempt();
1581
1582 // Check that the timestamp equals what we just set.
1583 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1584
1585 // Time hasn't advanced - this should work.
1586 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1587
1588 // Set clock to half the deadline - this should work.
1589 fake_clock.SetWallclockTime(epoch +
1590 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1591 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1592
1593 // Check that the first attempt timestamp hasn't changed just
1594 // because the wall-clock time changed.
1595 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1596
1597 // Set clock to _just_ before the deadline - this should work.
1598 fake_clock.SetWallclockTime(epoch +
1599 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1600 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1601
1602 // Set clock to _just_ after the deadline - this should not work.
1603 fake_clock.SetWallclockTime(epoch +
1604 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1605 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001606}
1607
1608TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1609 OmahaResponse response;
1610 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001611 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001612 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001613
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001614 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001615 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001616 SetupPayloadStateWith2Urls(
1617 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001618
1619 Time null_time = Time();
1620 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1621 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001622}
1623
1624TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1625 OmahaResponse response;
1626 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001627 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001628 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001629 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001630 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001631 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001632 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001633 SetupPayloadStateWith2Urls(
1634 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001635
1636 // Set the clock to something known.
1637 Time time = Time::FromInternalValue(12345);
1638 fake_clock.SetWallclockTime(time);
1639
1640 // New p2p attempt - as a side-effect this will update the p2p state vars.
1641 payload_state.P2PNewAttempt();
1642 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1643 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1644
1645 // Now create a new PayloadState and check that it loads the state
1646 // vars correctly.
1647 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001648 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001649 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1650 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001651}
1652
1653TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1654 OmahaResponse response;
1655 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001656 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001657 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001658 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001659 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001660 fake_system_state.set_prefs(&fake_prefs);
1661
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001662 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Sen Jiangcdd52062017-05-18 15:33:10 -07001663 SetupPayloadStateWith2Urls(
1664 "Hash8593", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001665
1666 // Set the clock to something known.
1667 Time time = Time::FromInternalValue(12345);
1668 fake_clock.SetWallclockTime(time);
1669
1670 // New p2p attempt - as a side-effect this will update the p2p state vars.
1671 payload_state.P2PNewAttempt();
1672 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1673 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1674
1675 // Set a new response...
Sen Jiangcdd52062017-05-18 15:33:10 -07001676 SetupPayloadStateWith2Urls(
1677 "Hash9904", true, false, &payload_state, &response);
David Zeuthendcba8092013-08-06 12:16:35 -07001678
1679 // ... and check that it clears the P2P state vars.
1680 Time null_time = Time();
1681 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1682 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001683}
1684
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001685} // namespace chromeos_update_engine