blob: a8067dca5558a2bf803ea5123d5c4685c70c6c9c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/payload_state.h"
18
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080020#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/constants.h"
26#include "update_engine/common/fake_clock.h"
27#include "update_engine/common/fake_hardware.h"
28#include "update_engine/common/fake_prefs.h"
29#include "update_engine/common/mock_prefs.h"
30#include "update_engine/common/prefs.h"
31#include "update_engine/common/test_utils.h"
32#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070033#include "update_engine/fake_system_state.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080034#include "update_engine/omaha_request_action.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080035
Jay Srinivasan08262882012-12-28 19:29:43 -080036using base::Time;
37using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038using std::string;
Alex Deymo42432912013-07-12 20:21:15 -070039using testing::AnyNumber;
40using testing::AtLeast;
41using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080043using testing::Return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070045using testing::_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046
47namespace chromeos_update_engine {
48
Jay Srinivasan19409b72013-04-12 19:23:36 -070049const char* kCurrentBytesDownloadedFromHttps =
50 "current-bytes-downloaded-from-HttpsServer";
51const char* kTotalBytesDownloadedFromHttps =
52 "total-bytes-downloaded-from-HttpsServer";
53const char* kCurrentBytesDownloadedFromHttp =
54 "current-bytes-downloaded-from-HttpServer";
55const char* kTotalBytesDownloadedFromHttp =
56 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070057const char* kCurrentBytesDownloadedFromHttpPeer =
58 "current-bytes-downloaded-from-HttpPeer";
59const char* kTotalBytesDownloadedFromHttpPeer =
60 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070061
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080062static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070063 bool http_enabled,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080064 PayloadState* payload_state,
65 OmahaResponse* response) {
66 response->payload_urls.clear();
67 response->payload_urls.push_back("http://test");
68 response->payload_urls.push_back("https://test");
69 response->size = 523456789;
70 response->hash = hash;
71 response->metadata_size = 558123;
72 response->metadata_signature = "metasign";
73 response->max_failure_count_per_url = 3;
74 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080075 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070076
77 string expected_url_https_only =
78 "NumURLs = 1\n"
79 "Candidate Url0 = https://test\n";
80
81 string expected_urls_both =
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080082 "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -070083 "Candidate Url0 = http://test\n"
84 "Candidate Url1 = https://test\n";
85
86 string expected_response_sign =
87 (http_enabled ? expected_urls_both : expected_url_https_only) +
Alex Vakulenko75039d72014-03-25 12:36:28 -070088 base::StringPrintf("Payload Size = 523456789\n"
89 "Payload Sha256 Hash = %s\n"
90 "Metadata Size = 558123\n"
91 "Metadata Signature = metasign\n"
92 "Is Delta Payload = %d\n"
93 "Max Failure Count Per Url = %d\n"
94 "Disable Payload Backoff = %d\n",
95 hash.c_str(),
96 response->is_delta_payload,
97 response->max_failure_count_per_url,
98 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -080099 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800100}
101
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800102class PayloadStateTest : public ::testing::Test { };
103
David Zeuthena99981f2013-04-29 13:42:47 -0700104TEST(PayloadStateTest, DidYouAddANewErrorCode) {
Allie Woodeb9e6d82015-04-17 13:55:30 -0700105 if (static_cast<int>(ErrorCode::kUmaReportedMax) != 48) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800106 LOG(ERROR) << "The following failure is intentional. If you added a new "
David Zeuthena99981f2013-04-29 13:42:47 -0700107 << "ErrorCode enum value, make sure to add it to the "
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800108 << "PayloadState::UpdateFailed method and then update this test "
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700109 << "to the new value of ErrorCode::kUmaReportedMax, which is "
110 << ErrorCode::kUmaReportedMax;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800111 EXPECT_FALSE("Please see the log line above");
112 }
113}
114
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800115TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
116 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700117 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800118 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700119 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700120 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
121 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700122 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
123 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700124 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
125 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
126 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
127 .Times(AtLeast(1));
128 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
129 .Times(AtLeast(1));
130 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
131 .Times(AtLeast(1));
132 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
133 .Times(AtLeast(1));
134 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
135 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700136 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
137 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700138 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700140 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800141 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 string stored_response_sign = payload_state.GetResponseSignature();
143 string expected_response_sign = "NumURLs = 0\n"
144 "Payload Size = 0\n"
145 "Payload Sha256 Hash = \n"
146 "Metadata Size = 0\n"
147 "Metadata Signature = \n"
148 "Is Delta Payload = 0\n"
149 "Max Failure Count Per Url = 0\n"
150 "Disable Payload Backoff = 0\n";
151 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700152 EXPECT_EQ("", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800153 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700154 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700155 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800156}
157
158TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
159 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700160 response.payload_urls.push_back("https://single.url.test");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800161 response.size = 123456789;
162 response.hash = "hash";
163 response.metadata_size = 58123;
164 response.metadata_signature = "msign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700165 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800166 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700167 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700168 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
169 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700170 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
171 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700172 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
173 .Times(AtLeast(1));
174 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
175 .Times(AtLeast(1));
176 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
177 .Times(AtLeast(1));
178 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
179 .Times(AtLeast(1));
180 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
181 .Times(AtLeast(1));
182 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
183 .Times(AtLeast(1));
184 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
185 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700186 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
187 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700188 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
189 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800190 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700191 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800192 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800193 string stored_response_sign = payload_state.GetResponseSignature();
194 string expected_response_sign = "NumURLs = 1\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700195 "Candidate Url0 = https://single.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800196 "Payload Size = 123456789\n"
197 "Payload Sha256 Hash = hash\n"
198 "Metadata Size = 58123\n"
199 "Metadata Signature = msign\n"
200 "Is Delta Payload = 0\n"
201 "Max Failure Count Per Url = 0\n"
202 "Disable Payload Backoff = 0\n";
203 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700204 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800205 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700206 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700207 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800208}
209
210TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
211 OmahaResponse response;
212 response.payload_urls.push_back("http://multiple.url.test");
213 response.payload_urls.push_back("https://multiple.url.test");
214 response.size = 523456789;
215 response.hash = "rhash";
216 response.metadata_size = 558123;
217 response.metadata_signature = "metasign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700218 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800219 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700220 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700221 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
222 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700223 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
224 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700225 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
226 .Times(AtLeast(1));
227 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
228 .Times(AtLeast(1));
229 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
230 .Times(AtLeast(1));
231 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
232 .Times(AtLeast(1));
233 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
234 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700235 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
236 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700237 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
238 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700239
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800240 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700241 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800242 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800243 string stored_response_sign = payload_state.GetResponseSignature();
244 string expected_response_sign = "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700245 "Candidate Url0 = http://multiple.url.test\n"
246 "Candidate Url1 = https://multiple.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800247 "Payload Size = 523456789\n"
248 "Payload Sha256 Hash = rhash\n"
249 "Metadata Size = 558123\n"
250 "Metadata Signature = metasign\n"
251 "Is Delta Payload = 0\n"
252 "Max Failure Count Per Url = 0\n"
253 "Disable Payload Backoff = 0\n";
254 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700255 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700257 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700258 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800259}
260
261TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
262 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700263 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800264 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800265 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800266
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700267 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800268 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700269 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
270 .Times(AtLeast(1));
271 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
272 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700273 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
274 .Times(AtLeast(1));
275 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
276 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700277 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700278
Chris Sosabe45bef2013-04-09 18:25:12 -0700279 // Reboots will be set
280 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
281
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800282 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700283 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
284 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800285
286 // Failure count should be called each times url index is set, so that's
287 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700288 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
289 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800290
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700291 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800292
293 // This does a SetResponse which causes all the states to be set to 0 for
294 // the first time.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response);
296 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800297
298 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700299 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700301 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800302
303 // Verify that on the next error, the URL index wraps around to 0.
304 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700305 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306
307 // Verify that on the next error, it again advances to 1.
308 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700309 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700310
311 // Verify that we switched URLs three times
312 EXPECT_EQ(3, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800313}
314
315TEST(PayloadStateTest, NewResponseResetsPayloadState) {
316 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700317 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800318 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800319
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700320 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800321
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700322 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800323 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700324 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800325 .Times(AnyNumber());
326
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800327 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700328 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700329 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330
331 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700332 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800333 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700334 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700335 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800336
337 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700338 SetupPayloadStateWith2Urls("Hash8225", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700339 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800340
Alex Deymob33b0f02013-08-08 21:10:02 -0700341 // Fake an error again.
342 payload_state.UpdateFailed(error);
343 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
344 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
345
Alex Deymob33b0f02013-08-08 21:10:02 -0700346 // Return a third different response.
347 SetupPayloadStateWith2Urls("Hash9999", true, &payload_state, &response);
348 EXPECT_EQ(3, payload_state.GetNumResponsesSeen());
349
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800350 // Make sure the url index was reset to 0 because of the new response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700351 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800352 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700353 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700354 EXPECT_EQ(0,
355 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
356 EXPECT_EQ(0,
357 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
358 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
359 kDownloadSourceHttpsServer));
360 EXPECT_EQ(0,
361 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800362}
363
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800364TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) {
365 OmahaResponse response;
366 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700367 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700368 int progress_bytes = 100;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800369 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800370
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700371 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700372 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
373 .Times(AtLeast(2));
374 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
375 .Times(AtLeast(1));
376 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2))
377 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800378
Alex Deymo820cc702013-06-28 15:43:46 -0700379 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
380 .Times(AtLeast(2));
381 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
382 .Times(AtLeast(1));
383 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2))
384 .Times(AtLeast(1));
385
Jay Srinivasan19409b72013-04-12 19:23:36 -0700386 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4));
Jay Srinivasan08262882012-12-28 19:29:43 -0800387
Jay Srinivasan19409b72013-04-12 19:23:36 -0700388 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4));
389 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800390
Jay Srinivasan19409b72013-04-12 19:23:36 -0700391 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
392 .Times(AtLeast(7));
393 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1))
394 .Times(AtLeast(2));
395 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2))
396 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800397
Jay Srinivasan19409b72013-04-12 19:23:36 -0700398 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
399 .Times(AtLeast(1));
400 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
401 .Times(AtLeast(1));
David Zeuthen9a017f22013-04-11 16:10:26 -0700402
Jay Srinivasan19409b72013-04-12 19:23:36 -0700403 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
404 .Times(AtLeast(1));
405 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
406 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700407 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
408 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700409 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes))
410 .Times(AtLeast(1));
411 EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes))
412 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700413 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
414 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700415
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700416 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800417
Jay Srinivasan53173b92013-05-17 17:13:01 -0700418 SetupPayloadStateWith2Urls("Hash5873", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700419 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800420
421 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700422 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800423 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700424 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700425 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800426 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700427 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800428
429 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700430 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800431 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700432 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700433 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800434 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700435 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800436
437 // This should advance the failure count only.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700438 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800439 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700440 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700441 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800442 EXPECT_EQ(2, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700443 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800444
445 // This should advance the URL index as we've reached the
446 // max failure count and reset the failure count for the new URL index.
447 // This should also wrap around the URL index and thus cause the payload
448 // attempt number to be incremented.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700449 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800450 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700451 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700452 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800453 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700454 EXPECT_EQ(2, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800455 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800456
457 // This should advance the URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700458 payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800459 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700460 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700461 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800462 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700463 EXPECT_EQ(3, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800464 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800465
466 // This should advance the URL index and payload attempt number due to
467 // wrap-around of URL index.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700468 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800469 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700470 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700471 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800472 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700473 EXPECT_EQ(4, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800474 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800475
476 // This HTTP error code should only increase the failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700477 payload_state.UpdateFailed(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700478 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800479 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700480 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700481 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800482 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700483 EXPECT_EQ(4, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800484 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800485
486 // And that failure count should be reset when we download some bytes
487 // afterwards.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700488 payload_state.DownloadProgress(progress_bytes);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800489 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700490 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700491 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800492 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700493 EXPECT_EQ(4, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800494 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800495
496 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700497 SetupPayloadStateWith2Urls("Hash8532", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700498 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800499
500 // Make sure the url index was reset to 0 because of the new response.
501 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700502 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700503 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800504 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700505 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan08262882012-12-28 19:29:43 -0800506 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800507}
508
Alex Deymo820cc702013-06-28 15:43:46 -0700509TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800510 OmahaResponse response;
Alex Deymo820cc702013-06-28 15:43:46 -0700511 response.is_delta_payload = false;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800512 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700513 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800514 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800515
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700516 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700517 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
518 .Times(AtLeast(1));
519 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
520 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800521
Alex Deymo820cc702013-06-28 15:43:46 -0700522 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
523 .Times(AtLeast(1));
524 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
525 .Times(AtLeast(1));
526
Jay Srinivasan19409b72013-04-12 19:23:36 -0700527 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
528 .Times(AtLeast(2));
Jay Srinivasan08262882012-12-28 19:29:43 -0800529
Jay Srinivasan19409b72013-04-12 19:23:36 -0700530 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
531 .Times(AtLeast(1));
532 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
533 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800534
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700535 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800536
Jay Srinivasan53173b92013-05-17 17:13:01 -0700537 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800538
539 // This should just advance the payload attempt number;
540 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700541 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800542 payload_state.DownloadComplete();
543 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700544 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
545 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
546 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
547 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
548}
549
550TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
551 OmahaResponse response;
552 response.is_delta_payload = true;
553 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700554 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800555 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700556
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700557 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700558 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
559 .Times(AtLeast(1));
560 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
561 .Times(AtLeast(1));
562
563 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
564 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
565 .Times(AtLeast(1));
566
567 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
568 .Times(1);
569
570 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
571 .Times(AtLeast(1));
572 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
573 .Times(AtLeast(1));
574
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700575 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700576
577 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
578
579 // This should just advance the payload attempt number;
580 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
581 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
582 payload_state.DownloadComplete();
583 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
584 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700585 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800586 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700587 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800588}
589
590TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
591 OmahaResponse response;
592 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700593 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800594
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700595 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700596 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800597
598 // Generate enough events to advance URL index, failure count and
599 // payload attempt number all to 1.
600 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700601 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
602 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800603 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700604 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700605 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800606 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700607 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800608
609 // Now, simulate a corrupted url index on persisted store which gets
610 // loaded when update_engine restarts. Using a different prefs object
611 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700612 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800613 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700614 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700615 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700616 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
617 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700618 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
619 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700620 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
621 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
622 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
623 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700624 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
625 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800626
627 // Note: This will be a different payload object, but the response should
628 // have the same hash as before so as to not trivially reset because the
629 // response was different. We want to specifically test that even if the
630 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700631 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700632 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800633
634 // Make sure all counters get reset to 0 because of the corrupted URL index
635 // we supplied above.
636 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700637 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700638 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800639 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700640 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800641}
Jay Srinivasan08262882012-12-28 19:29:43 -0800642
Chris Sosa20f005c2013-09-05 13:53:08 -0700643TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
644 OmahaResponse response;
645 response.is_delta_payload = false;
646 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700647 FakeSystemState fake_system_state;
648 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700649 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700650 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700651
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700652 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700653 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
654
655 // Simulate two failures (enough to cause payload backoff) and check
656 // again that we're ready to re-download without any backoff as this is
657 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700658 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
659 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700660 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
661 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
662 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
663 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
664}
665
666TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
667 OmahaResponse response;
668 response.is_delta_payload = false;
669 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700670 FakeSystemState fake_system_state;
671 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700672 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700673 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700674
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700675 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700676 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
677
678 // Simulate two failures (enough to cause payload backoff) and check
679 // again that we're ready to re-download without any backoff as this is
680 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700681 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
682 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700683 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
684 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
685 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
686 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700687 payload_state.SetUsingP2PForDownloading(true);
688 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700689 // Should not backoff for p2p updates.
690 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
691
Gilad Arnold74b5f552014-10-07 08:17:16 -0700692 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700693 // No actual p2p update if no url is provided.
694 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
695}
696
Jay Srinivasan08262882012-12-28 19:29:43 -0800697TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
698 OmahaResponse response;
699 response.is_delta_payload = true;
700 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700701 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800702
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700703 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700704 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800705
706 // Simulate a successful download and see that we're ready to download
707 // again without any backoff as this is a delta payload.
708 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700709 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
710 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800711 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
712
713 // Simulate two failures (enough to cause payload backoff) and check
714 // again that we're ready to re-download without any backoff as this is
715 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700716 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
717 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700718 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700719 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
720 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800721 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
722}
723
724static void CheckPayloadBackoffState(PayloadState* payload_state,
725 int expected_attempt_number,
726 TimeDelta expected_days) {
727 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700728 EXPECT_EQ(expected_attempt_number,
729 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800730 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
731 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
732 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
733 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
734 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
735 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
736 EXPECT_LT(expected_min_time.ToInternalValue(),
737 backoff_expiry_time.ToInternalValue());
738 EXPECT_GT(expected_max_time.ToInternalValue(),
739 backoff_expiry_time.ToInternalValue());
740}
741
742TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
743 OmahaResponse response;
744 response.is_delta_payload = false;
745 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700746 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800747
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700748 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700749 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800750
751 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
752 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
753 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
754 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
755 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
756 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
757 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
758 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
759 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
760 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
761}
762
763TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
764 OmahaResponse response;
765 response.disable_payload_backoff = true;
766 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700767 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800768
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700769 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700770 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800771
772 // Simulate a successful download and see that we are ready to download
773 // again without any backoff.
774 payload_state.DownloadComplete();
775 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700776 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800777 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
778
779 // Test again, this time by simulating two errors that would cause
780 // the payload attempt number to increment due to wrap around. And
781 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700782 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
783 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800784 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700785 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800786 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
787}
788
Jay Srinivasan19409b72013-04-12 19:23:36 -0700789TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
790 OmahaResponse response;
791 response.disable_payload_backoff = true;
792 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700793 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700794 int https_total = 0;
795 int http_total = 0;
796
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700797 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700798 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700799 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700800
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700801 // Simulate a previous attempt with in order to set an initial non-zero value
802 // for the total bytes downloaded for HTTP.
803 int prev_chunk = 323456789;
804 http_total += prev_chunk;
805 payload_state.DownloadProgress(prev_chunk);
806
807 // Ensure that the initial values for HTTP reflect this attempt.
808 EXPECT_EQ(prev_chunk,
809 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
810 EXPECT_EQ(http_total,
811 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
812
813 // Change the response hash so as to simulate a new response which will
814 // reset the current bytes downloaded, but not the total bytes downloaded.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700815 SetupPayloadStateWith2Urls("Hash9904", true, &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.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700819 int first_chunk = 5000000;
820 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));
827 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
828 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.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700837 int second_chunk = 23456789;
838 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);
851 int third_chunk = 32345678;
852 int http_chunk = first_chunk + third_chunk;
853 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);
870 int p2p_total = 42 * 1000 * 1000;
871 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.
900 EXPECT_EQ(0,
901 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
902 EXPECT_EQ(0,
903 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
904 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
905 kDownloadSourceHttpsServer));
906 EXPECT_EQ(0,
907 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));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700917 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
918
919 // Simulate progress in order to mark HTTP as one of the sources used.
920 int num_bytes = 42 * 1000 * 1000;
921 payload_state.DownloadProgress(num_bytes);
922
923 // Check that this was done via HTTP.
924 EXPECT_EQ(num_bytes,
925 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
926 EXPECT_EQ(num_bytes,
927 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
928
929 // Check that only HTTP is reported as a download source.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700930 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700931 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700932 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800933 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed,
934 (1 << kDownloadSourceHttpServer),
935 _, _, _));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700936
937 payload_state.UpdateSucceeded();
938}
939
Jay Srinivasan19409b72013-04-12 19:23:36 -0700940TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
941 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700942 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700943 PayloadState payload_state;
944
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700945 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700946
947 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700948 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700949
950 int num_bytes = 10000;
951 payload_state.DownloadProgress(num_bytes);
952 EXPECT_EQ(num_bytes,
953 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
954 EXPECT_EQ(num_bytes,
955 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
956 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
957 kDownloadSourceHttpsServer));
958 EXPECT_EQ(0,
959 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
960
961 payload_state.UpdateRestarted();
962 // Make sure the current bytes downloaded is reset, but not the total bytes.
963 EXPECT_EQ(0,
964 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
965 EXPECT_EQ(num_bytes,
966 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
967}
968
Chris Sosabe45bef2013-04-09 18:25:12 -0700969TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700970 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -0700971 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700972
Alex Deymo8427b4a2014-11-05 14:00:32 -0800973 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700974 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -0700975 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
976
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700977 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -0700978
979 payload_state.UpdateRestarted();
980 EXPECT_EQ(0, payload_state.GetNumReboots());
981
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700982 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -0700983 payload_state.UpdateResumed();
984 // Num reboots should be incremented because system rebooted detected.
985 EXPECT_EQ(1, payload_state.GetNumReboots());
986
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700987 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -0700988 payload_state.UpdateResumed();
989 // Num reboots should now be 1 as reboot was not detected.
990 EXPECT_EQ(1, payload_state.GetNumReboots());
991
992 // Restart the update again to verify we set the num of reboots back to 0.
993 payload_state.UpdateRestarted();
994 EXPECT_EQ(0, payload_state.GetNumReboots());
995}
Jay Srinivasan19409b72013-04-12 19:23:36 -0700996
Chris Sosaaa18e162013-06-20 13:20:30 -0700997TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700998 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -0700999 PayloadState payload_state;
1000
Alex Deymo8427b4a2014-11-05 14:00:32 -08001001 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001002 fake_system_state.mock_powerwash_safe_prefs();
1003 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -07001004
1005 // Verify pre-conditions are good.
1006 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1007
1008 // Mock out the os version and make sure it's blacklisted correctly.
1009 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001010 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001011 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001012 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001013
1014 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1015 rollback_version));
1016 payload_state.Rollback();
1017
1018 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001019
1020 // Change it up a little and verify we load it correctly.
1021 rollback_version = "2345.0.1";
1022 // Let's verify we can reload it correctly.
1023 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1024 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1025 SetArgumentPointee<1>(rollback_version), Return(true)));
1026 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1027 rollback_version));
1028 payload_state.LoadRollbackVersion();
1029 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001030
David Zeuthen96197df2014-04-16 12:22:39 -07001031 // Check that we report only UpdateEngine.Rollback.* metrics in
1032 // UpdateSucceeded().
David Zeuthenafed4a12014-04-09 15:28:44 -07001033 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1034 .Times(0);
1035 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1036 .Times(0);
David Zeuthen96197df2014-04-16 12:22:39 -07001037 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1038 SendEnumToUMA(
1039 metrics::kMetricRollbackResult,
1040 static_cast<int>(metrics::RollbackResult::kSuccess),
1041 static_cast<int>(metrics::RollbackResult::kNumConstants)));
David Zeuthenafed4a12014-04-09 15:28:44 -07001042 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001043}
1044
David Zeuthenf413fe52013-04-22 14:04:39 -07001045TEST(PayloadStateTest, DurationsAreCorrect) {
1046 OmahaResponse response;
1047 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001048 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001049 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001050 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001051
1052 // Set the clock to a well-known time - 1 second on the wall-clock
1053 // and 2 seconds on the monotonic clock
1054 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1055 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1056
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001057 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001058 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001059 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001060
1061 // Check that durations are correct for a successful update where
1062 // time has advanced 7 seconds on the wall clock and 4 seconds on
1063 // the monotonic clock.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001064 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001065 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1066 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1067 payload_state.UpdateSucceeded();
1068 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1069 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1070
1071 // Check that durations are reset when a new response comes in.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001072 SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001073 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1074 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1075
1076 // Advance time a bit (10 secs), simulate download progress and
1077 // check that durations are updated.
1078 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1079 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1080 payload_state.DownloadProgress(10);
1081 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1082 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1083
1084 // Now simulate a reboot by resetting monotonic time (to 5000) and
1085 // creating a new PayloadState object and check that we load the
1086 // durations correctly (e.g. they are the same as before).
1087 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1088 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001089 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001090 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001091 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1092 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001093
1094 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1095 // and check that the durations are increased accordingly.
1096 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1097 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1098 payload_state2.UpdateSucceeded();
1099 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001100 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1101 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001102}
1103
David Zeuthene4c58bf2013-06-18 17:26:50 -07001104TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1105 OmahaResponse response;
1106 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001107 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001108 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001109 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001110
1111 // Set the clock to a well-known time (t = 30 seconds).
1112 fake_clock.SetWallclockTime(Time::FromInternalValue(
1113 30 * Time::kMicrosecondsPerSecond));
1114
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001115 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001116 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001117 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001118
1119 // Make the update succeed.
1120 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1121 payload_state.UpdateSucceeded();
1122
1123 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001124 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001125
1126 // Now simulate a reboot and set the wallclock time to a later point
1127 // (t = 500 seconds). We do this by using a new PayloadState object
1128 // and checking that it emits the right UMA metric with the right
1129 // value.
1130 fake_clock.SetWallclockTime(Time::FromInternalValue(
1131 500 * Time::kMicrosecondsPerSecond));
1132 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001133 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001134
1135 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001136 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001137 metrics::kMetricTimeToRebootMinutes,
1138 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001139 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001140
1141 payload_state2.UpdateEngineStarted();
1142
1143 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001144 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001145}
1146
Alex Deymo569c4242013-07-24 12:01:01 -07001147TEST(PayloadStateTest, RestartAfterCrash) {
1148 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001149 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001150 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001151
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001152 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001153
David Zeuthen4e1d1492014-04-25 13:12:27 -07001154 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001155 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1156 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1157 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1158 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1159 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1160 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1161 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001162 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001163
1164 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001165 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001166 SendToUMA(_, _, _, _, _)).Times(0);
1167
1168 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001169 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001170
1171 payload_state.UpdateEngineStarted();
1172}
1173
David Zeuthen4e1d1492014-04-25 13:12:27 -07001174TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1175 PayloadState payload_state;
1176 FakeSystemState fake_system_state;
1177
1178 // If there's no marker at startup, ensure we don't report a metric.
1179 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1180 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1181 SendEnumToUMA(
1182 metrics::kMetricAttemptResult,
1183 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1184 _)).Times(0);
1185 payload_state.UpdateEngineStarted();
1186}
1187
1188TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1189 PayloadState payload_state;
1190 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001191 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001192
1193 // If we have a marker at startup, ensure it's reported and the
1194 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001195 fake_system_state.set_prefs(&fake_prefs);
1196 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001197
1198 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1199
1200 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1201 SendEnumToUMA(
1202 metrics::kMetricAttemptResult,
1203 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1204 _)).Times(1);
1205 payload_state.UpdateEngineStarted();
1206
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001207 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001208}
1209
1210TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1211 PayloadState payload_state;
1212 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001213 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001214
1215 // Make sure the marker is written and cleared during an attempt and
1216 // also that we DO NOT emit the metric (since the attempt didn't end
1217 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001218 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001219 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1220
1221 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1222 .Times(AnyNumber());
1223 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1224 .Times(AnyNumber());
1225 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1226 SendEnumToUMA(
1227 metrics::kMetricAttemptResult,
1228 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1229 _)).Times(0);
1230
1231 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001232 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001233
1234 payload_state.UpdateRestarted();
1235
1236 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001237 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001238
1239 payload_state.UpdateSucceeded();
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
Jay Srinivasan53173b92013-05-17 17:13:01 -07001245TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1246 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001247 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001248 PayloadState payload_state;
1249
Jay Srinivasan53173b92013-05-17 17:13:01 -07001250 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001251 fake_system_state.set_device_policy(&disable_http_policy);
1252 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001253
1254 // Test with no device policy. Should default to allowing http.
1255 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1256 .WillRepeatedly(Return(false));
1257
1258 // Set the first response.
1259 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1260
1261 // Check that we use the HTTP URL since there is no value set for allowing
1262 // http.
1263 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1264
1265 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001266 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1267 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1268
Chris Sosaf7d80042013-08-22 16:45:17 -07001269 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001270 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1271
1272 // Check that we skip the HTTP URL and use only the HTTPS url.
1273 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1274
1275 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001276 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001277 payload_state.UpdateFailed(error);
1278
1279 // Check that we still skip the HTTP URL and use only the HTTPS url.
1280 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1281 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
1282
1283 // Now, slightly change the response and set it again.
1284 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1285
1286 // Check that we still skip the HTTP URL and use only the HTTPS url.
1287 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1288
1289 // Now, pretend that the HTTP policy is turned on. We want to make sure
1290 // the new policy is honored.
1291 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001292 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001293 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1294 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1295
1296 // Now, set the same response using the same hash
1297 // so that we can test that the state is reset not because of the
1298 // hash but because of the policy change which results in candidate url
1299 // list change.
1300 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1301
1302 // Check that we use the HTTP URL now and the failure count is reset.
1303 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1304 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1305
1306 // Fake a failure and see if we're moving over to the HTTPS url and update
1307 // the URL switch count properly.
1308 payload_state.UpdateFailed(error);
1309 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1310 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
1311 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1312}
1313
Alex Deymo1c656c42013-06-28 11:02:14 -07001314TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1315 OmahaResponse response;
1316 response.is_delta_payload = true;
1317 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001318 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001319
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001320 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001321 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1322
1323 // Simulate a successful download and update.
1324 payload_state.DownloadComplete();
1325
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001326 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001327 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001328 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001329 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001330 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001331 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1332 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001333 payload_state.UpdateSucceeded();
1334
1335 // Mock the request to a request where the delta was disabled but Omaha sends
1336 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001337 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001338 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001339 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001340
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001341 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001342 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1343
1344 payload_state.DownloadComplete();
1345
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001346 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001347 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1348 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001349 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001350 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1351 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001352 payload_state.UpdateSucceeded();
1353}
1354
1355TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1356 OmahaResponse response;
1357 response.is_delta_payload = false;
1358 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001359 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001360
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001361 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001362 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1363
1364 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001365 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001366 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001367 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001368
1369 // Simulate a successful download and update.
1370 payload_state.DownloadComplete();
1371
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001372 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001373 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001374 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001375 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1376 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001377 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001378 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1379 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001380 payload_state.UpdateSucceeded();
1381}
1382
1383TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1384 OmahaResponse response;
1385 response.is_delta_payload = false;
1386 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001387 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001388
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001389 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001390 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1391
Alex Deymo820cc702013-06-28 15:43:46 -07001392 // Mock the request to a request where the delta is enabled, although the
1393 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001394 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001395 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001396 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001397
1398 // Simulate a successful download and update.
1399 payload_state.DownloadComplete();
1400
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001401 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001402 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001403 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001404 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1405 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001406 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001407 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1408 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001409 payload_state.UpdateSucceeded();
1410}
1411
Alex Deymo42432912013-07-12 20:21:15 -07001412TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001413 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001414 OmahaResponse response;
1415 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001416 FakePrefs fake_prefs;
1417 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001418
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001419 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001420 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1421
1422 // Simulate a successful download and update.
1423 payload_state.DownloadComplete();
1424 payload_state.UpdateSucceeded();
1425 payload_state.ExpectRebootInNewVersion("Version:12345678");
1426
1427 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001428 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001429 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001430 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001431 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001432
1433 // Simulate a second update and reboot into the same environment, this should
1434 // send a value of 2.
1435 payload_state.ExpectRebootInNewVersion("Version:12345678");
1436
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001437 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001438 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001439 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001440 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001441
1442 // Simulate a third failed reboot to new version, but this time for a
1443 // different payload. This should send a value of 1 this time.
1444 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001445 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001446 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001447 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001448 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001449}
1450
1451TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001452 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001453 OmahaResponse response;
1454 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001455 FakePrefs fake_prefs;
1456 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001457
Alex Deymo763e7db2015-08-27 21:08:08 -07001458 FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control();
1459 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001460
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001461 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001462 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1463
1464 // Simulate a successful download and update.
1465 payload_state.DownloadComplete();
1466 payload_state.UpdateSucceeded();
1467 payload_state.ExpectRebootInNewVersion("Version:12345678");
1468
1469 // Change the BootDevice to a different one, no metric should be sent.
Alex Deymo763e7db2015-08-27 21:08:08 -07001470 fake_boot_control->SetCurrentSlot(1);
Alex Deymo42432912013-07-12 20:21:15 -07001471
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001472 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001473 metrics::kMetricFailedUpdateCount, _, _, _, _))
1474 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001475 payload_state.ReportFailedBootIfNeeded();
1476
Alex Deymo763e7db2015-08-27 21:08:08 -07001477 // A second reboot in either partition should not send a metric.
Alex Deymo42432912013-07-12 20:21:15 -07001478 payload_state.ReportFailedBootIfNeeded();
Alex Deymo763e7db2015-08-27 21:08:08 -07001479 fake_boot_control->SetCurrentSlot(0);
Alex Deymo42432912013-07-12 20:21:15 -07001480 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001481}
1482
1483TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001484 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001485 OmahaResponse response;
1486 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001487 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001488
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001489 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001490 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001491 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1492
1493 // Simulate a successful download and update.
1494 payload_state.DownloadComplete();
1495 payload_state.UpdateSucceeded();
1496 payload_state.ExpectRebootInNewVersion("Version:12345678");
1497
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001498 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001499 metrics::kMetricFailedUpdateCount, _, _, _, _))
1500 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001501
1502 // Cancel the applied update.
1503 payload_state.ResetUpdateStatus();
1504
1505 // Simulate a reboot.
1506 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001507}
1508
1509TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001510 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001511 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001512 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001513
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001514 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001515 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001516
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001517 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001518 metrics::kMetricFailedUpdateCount, _, _, _, _))
1519 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001520
1521 // Simulate a reboot in this environment.
1522 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001523}
1524
David Zeuthendcba8092013-08-06 12:16:35 -07001525TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1526 OmahaResponse response;
1527 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001528 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001529 FakePrefs fake_prefs;
1530 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001531
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001532 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001533 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1534
1535 // Should allow exactly kMaxP2PAttempts...
1536 for (int n = 0; n < kMaxP2PAttempts; n++) {
1537 payload_state.P2PNewAttempt();
1538 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1539 }
1540 // ... but not more than that.
1541 payload_state.P2PNewAttempt();
1542 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001543}
1544
1545TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1546 OmahaResponse response;
1547 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001548 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001549 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001550 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001551
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001552 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001553 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001554 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001555 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1556
1557 // Set the clock to 1 second.
1558 Time epoch = Time::FromInternalValue(1000000);
1559 fake_clock.SetWallclockTime(epoch);
1560
1561 // Do an attempt - this will set the timestamp.
1562 payload_state.P2PNewAttempt();
1563
1564 // Check that the timestamp equals what we just set.
1565 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1566
1567 // Time hasn't advanced - this should work.
1568 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1569
1570 // Set clock to half the deadline - this should work.
1571 fake_clock.SetWallclockTime(epoch +
1572 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1573 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1574
1575 // Check that the first attempt timestamp hasn't changed just
1576 // because the wall-clock time changed.
1577 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1578
1579 // Set clock to _just_ before the deadline - this should work.
1580 fake_clock.SetWallclockTime(epoch +
1581 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1582 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1583
1584 // Set clock to _just_ after the deadline - this should not work.
1585 fake_clock.SetWallclockTime(epoch +
1586 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1587 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001588}
1589
1590TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1591 OmahaResponse response;
1592 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001593 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001594 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001595
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001596 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001597 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001598 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1599
1600 Time null_time = Time();
1601 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1602 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001603}
1604
1605TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1606 OmahaResponse response;
1607 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001608 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001609 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001610 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001611 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001612 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001613 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001614 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1615
1616 // Set the clock to something known.
1617 Time time = Time::FromInternalValue(12345);
1618 fake_clock.SetWallclockTime(time);
1619
1620 // New p2p attempt - as a side-effect this will update the p2p state vars.
1621 payload_state.P2PNewAttempt();
1622 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1623 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1624
1625 // Now create a new PayloadState and check that it loads the state
1626 // vars correctly.
1627 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001628 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001629 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1630 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001631}
1632
1633TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1634 OmahaResponse response;
1635 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001636 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001637 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001638 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001639 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001640 fake_system_state.set_prefs(&fake_prefs);
1641
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001642 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001643 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1644
1645 // Set the clock to something known.
1646 Time time = Time::FromInternalValue(12345);
1647 fake_clock.SetWallclockTime(time);
1648
1649 // New p2p attempt - as a side-effect this will update the p2p state vars.
1650 payload_state.P2PNewAttempt();
1651 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1652 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1653
1654 // Set a new response...
1655 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1656
1657 // ... and check that it clears the P2P state vars.
1658 Time null_time = Time();
1659 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1660 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001661}
1662
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001663} // namespace chromeos_update_engine