blob: c449d4a2826375e09a46bfa93614a0b5e27a2205 [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo2c0db7b2014-11-04 12:23:39 -08005#include "update_engine/payload_state.h"
6
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08007#include <glib.h>
8
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080010#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070011#include <base/strings/stringprintf.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080012#include <gmock/gmock.h>
13#include <gtest/gtest.h>
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080014
Jay Srinivasand29695d2013-04-08 15:08:05 -070015#include "update_engine/constants.h"
David Zeuthenf413fe52013-04-22 14:04:39 -070016#include "update_engine/fake_clock.h"
Alex Deymo42432912013-07-12 20:21:15 -070017#include "update_engine/fake_hardware.h"
David Zeuthen4e1d1492014-04-25 13:12:27 -070018#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070019#include "update_engine/fake_system_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080020#include "update_engine/mock_prefs.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021#include "update_engine/omaha_request_action.h"
David Zeuthenf413fe52013-04-22 14:04:39 -070022#include "update_engine/prefs.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023#include "update_engine/test_utils.h"
24#include "update_engine/utils.h"
25
Jay Srinivasan08262882012-12-28 19:29:43 -080026using base::Time;
27using base::TimeDelta;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080028using std::string;
Alex Deymo42432912013-07-12 20:21:15 -070029using testing::AnyNumber;
30using testing::AtLeast;
31using testing::Mock;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032using testing::NiceMock;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080033using testing::Return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080034using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070035using testing::_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036
37namespace chromeos_update_engine {
38
Jay Srinivasan19409b72013-04-12 19:23:36 -070039const char* kCurrentBytesDownloadedFromHttps =
40 "current-bytes-downloaded-from-HttpsServer";
41const char* kTotalBytesDownloadedFromHttps =
42 "total-bytes-downloaded-from-HttpsServer";
43const char* kCurrentBytesDownloadedFromHttp =
44 "current-bytes-downloaded-from-HttpServer";
45const char* kTotalBytesDownloadedFromHttp =
46 "total-bytes-downloaded-from-HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -070047const char* kCurrentBytesDownloadedFromHttpPeer =
48 "current-bytes-downloaded-from-HttpPeer";
49const char* kTotalBytesDownloadedFromHttpPeer =
50 "total-bytes-downloaded-from-HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -070051
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080052static void SetupPayloadStateWith2Urls(string hash,
Jay Srinivasan53173b92013-05-17 17:13:01 -070053 bool http_enabled,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080054 PayloadState* payload_state,
55 OmahaResponse* response) {
56 response->payload_urls.clear();
57 response->payload_urls.push_back("http://test");
58 response->payload_urls.push_back("https://test");
59 response->size = 523456789;
60 response->hash = hash;
61 response->metadata_size = 558123;
62 response->metadata_signature = "metasign";
63 response->max_failure_count_per_url = 3;
64 payload_state->SetResponse(*response);
Jay Srinivasan08262882012-12-28 19:29:43 -080065 string stored_response_sign = payload_state->GetResponseSignature();
Jay Srinivasan53173b92013-05-17 17:13:01 -070066
67 string expected_url_https_only =
68 "NumURLs = 1\n"
69 "Candidate Url0 = https://test\n";
70
71 string expected_urls_both =
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080072 "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -070073 "Candidate Url0 = http://test\n"
74 "Candidate Url1 = https://test\n";
75
76 string expected_response_sign =
77 (http_enabled ? expected_urls_both : expected_url_https_only) +
Alex Vakulenko75039d72014-03-25 12:36:28 -070078 base::StringPrintf("Payload Size = 523456789\n"
79 "Payload Sha256 Hash = %s\n"
80 "Metadata Size = 558123\n"
81 "Metadata Signature = metasign\n"
82 "Is Delta Payload = %d\n"
83 "Max Failure Count Per Url = %d\n"
84 "Disable Payload Backoff = %d\n",
85 hash.c_str(),
86 response->is_delta_payload,
87 response->max_failure_count_per_url,
88 response->disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -080089 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080090}
91
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092class PayloadStateTest : public ::testing::Test { };
93
David Zeuthena99981f2013-04-29 13:42:47 -070094TEST(PayloadStateTest, DidYouAddANewErrorCode) {
David Zeuthenf3e28012014-08-26 18:23:52 -040095 if (static_cast<int>(ErrorCode::kUmaReportedMax) != 47) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080096 LOG(ERROR) << "The following failure is intentional. If you added a new "
David Zeuthena99981f2013-04-29 13:42:47 -070097 << "ErrorCode enum value, make sure to add it to the "
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080098 << "PayloadState::UpdateFailed method and then update this test "
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070099 << "to the new value of ErrorCode::kUmaReportedMax, which is "
100 << ErrorCode::kUmaReportedMax;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800101 EXPECT_FALSE("Please see the log line above");
102 }
103}
104
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800105TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) {
106 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700107 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800108 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700109 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700110 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
111 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700112 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
113 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700114 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1));
115 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
116 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
117 .Times(AtLeast(1));
118 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
119 .Times(AtLeast(1));
120 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
121 .Times(AtLeast(1));
122 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
123 .Times(AtLeast(1));
124 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
125 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700126 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
127 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700128 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800129 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700130 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800131 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800132 string stored_response_sign = payload_state.GetResponseSignature();
133 string expected_response_sign = "NumURLs = 0\n"
134 "Payload Size = 0\n"
135 "Payload Sha256 Hash = \n"
136 "Metadata Size = 0\n"
137 "Metadata Signature = \n"
138 "Is Delta Payload = 0\n"
139 "Max Failure Count Per Url = 0\n"
140 "Disable Payload Backoff = 0\n";
141 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700142 EXPECT_EQ("", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800143 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700144 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700145 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800146}
147
148TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) {
149 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700150 response.payload_urls.push_back("https://single.url.test");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800151 response.size = 123456789;
152 response.hash = "hash";
153 response.metadata_size = 58123;
154 response.metadata_signature = "msign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700155 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800156 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700157 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700158 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
159 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700160 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
161 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700162 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
163 .Times(AtLeast(1));
164 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
165 .Times(AtLeast(1));
166 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
167 .Times(AtLeast(1));
168 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _))
169 .Times(AtLeast(1));
170 EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _))
171 .Times(AtLeast(1));
172 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
173 .Times(AtLeast(1));
174 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
175 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700176 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
177 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700178 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
179 .Times(AtLeast(1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700181 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800182 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800183 string stored_response_sign = payload_state.GetResponseSignature();
184 string expected_response_sign = "NumURLs = 1\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700185 "Candidate Url0 = https://single.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800186 "Payload Size = 123456789\n"
187 "Payload Sha256 Hash = hash\n"
188 "Metadata Size = 58123\n"
189 "Metadata Signature = msign\n"
190 "Is Delta Payload = 0\n"
191 "Max Failure Count Per Url = 0\n"
192 "Disable Payload Backoff = 0\n";
193 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700194 EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800195 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700196 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700197 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198}
199
200TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) {
201 OmahaResponse response;
202 response.payload_urls.push_back("http://multiple.url.test");
203 response.payload_urls.push_back("https://multiple.url.test");
204 response.size = 523456789;
205 response.hash = "rhash";
206 response.metadata_size = 558123;
207 response.metadata_signature = "metasign";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700208 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800209 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700210 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700211 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
212 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700213 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
214 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700215 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0))
216 .Times(AtLeast(1));
217 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
218 .Times(AtLeast(1));
219 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
220 .Times(AtLeast(1));
221 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0))
222 .Times(AtLeast(1));
223 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0))
224 .Times(AtLeast(1));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700225 EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0))
226 .Times(AtLeast(1));
Chris Sosabe45bef2013-04-09 18:25:12 -0700227 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0))
228 .Times(AtLeast(1));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700229
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700231 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800232 payload_state.SetResponse(response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800233 string stored_response_sign = payload_state.GetResponseSignature();
234 string expected_response_sign = "NumURLs = 2\n"
Jay Srinivasan53173b92013-05-17 17:13:01 -0700235 "Candidate Url0 = http://multiple.url.test\n"
236 "Candidate Url1 = https://multiple.url.test\n"
Jay Srinivasan08262882012-12-28 19:29:43 -0800237 "Payload Size = 523456789\n"
238 "Payload Sha256 Hash = rhash\n"
239 "Metadata Size = 558123\n"
240 "Metadata Signature = metasign\n"
241 "Is Delta Payload = 0\n"
242 "Max Failure Count Per Url = 0\n"
243 "Disable Payload Backoff = 0\n";
244 EXPECT_EQ(expected_response_sign, stored_response_sign);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700245 EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800246 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700247 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
David Zeuthena573d6f2013-06-14 16:13:36 -0700248 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800249}
250
251TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) {
252 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700253 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800254 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800255 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700257 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800258 // Payload attempt should start with 0 and then advance to 1.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700259 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
260 .Times(AtLeast(1));
261 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
262 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700263 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
264 .Times(AtLeast(1));
265 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1))
266 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700267 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2));
David Zeuthen9a017f22013-04-11 16:10:26 -0700268
Chris Sosabe45bef2013-04-09 18:25:12 -0700269 // Reboots will be set
270 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1));
271
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800272 // Url index should go from 0 to 1 twice.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700273 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1));
274 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800275
276 // Failure count should be called each times url index is set, so that's
277 // 4 times for this test.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700278 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
279 .Times(AtLeast(4));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800280
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700281 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800282
283 // This does a SetResponse which causes all the states to be set to 0 for
284 // the first time.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700285 SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response);
286 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287
288 // Verify that on the first error, the URL index advances to 1.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700289 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700291 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292
293 // Verify that on the next error, the URL index wraps around to 0.
294 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296
297 // Verify that on the next error, it again advances to 1.
298 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700300
301 // Verify that we switched URLs three times
302 EXPECT_EQ(3, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800303}
304
305TEST(PayloadStateTest, NewResponseResetsPayloadState) {
306 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700307 FakeSystemState fake_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 PayloadState payload_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800311
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700312 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800313 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700314 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800315 .Times(AnyNumber());
316
Alex Deymob33b0f02013-08-08 21:10:02 -0700317 // The first response doesn't send an abandoned event.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700318 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymob33b0f02013-08-08 21:10:02 -0700319 "Installer.UpdatesAbandonedEventCount", 0, _, _, _)).Times(0);
320
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800321 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700322 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700323 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800324
325 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700326 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327 payload_state.UpdateFailed(error);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700328 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
David Zeuthencc6f9962013-04-18 11:57:24 -0700329 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700331 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymob33b0f02013-08-08 21:10:02 -0700332 "Installer.UpdatesAbandonedEventCount", 1, _, _, _));
333
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800334 // Now, slightly change the response and set it again.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700335 SetupPayloadStateWith2Urls("Hash8225", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700336 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800337
Alex Deymob33b0f02013-08-08 21:10:02 -0700338 // Fake an error again.
339 payload_state.UpdateFailed(error);
340 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
341 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
342
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700343 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymob33b0f02013-08-08 21:10:02 -0700344 "Installer.UpdatesAbandonedEventCount", 2, _, _, _));
345
346 // 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
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700539 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700540 "Installer.PayloadAttemptNumber", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700541 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700542 "Installer.FullPayloadAttemptNumber", 1, _, _, _));
543
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800544 // This should just advance the payload attempt number;
545 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700546 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800547 payload_state.DownloadComplete();
548 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700549 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
550 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
551 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
552 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
553}
554
555TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) {
556 OmahaResponse response;
557 response.is_delta_payload = true;
558 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700559 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800560 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo820cc702013-06-28 15:43:46 -0700561
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700562 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700563 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0))
564 .Times(AtLeast(1));
565 EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1))
566 .Times(AtLeast(1));
567
568 // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads.
569 EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0))
570 .Times(AtLeast(1));
571
572 EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _))
573 .Times(1);
574
575 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0))
576 .Times(AtLeast(1));
577 EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0))
578 .Times(AtLeast(1));
579
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700580 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo820cc702013-06-28 15:43:46 -0700581
582 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
583
Alex Deymo29b51d92013-07-09 15:26:24 -0700584 // Metrics for Full payload attempt number is not sent with Delta payloads.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700586 "Installer.PayloadAttemptNumber", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700587 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo29b51d92013-07-09 15:26:24 -0700588 "Installer.FullPayloadAttemptNumber", _, _, _, _))
589 .Times(0);
590
Alex Deymo820cc702013-06-28 15:43:46 -0700591 // This should just advance the payload attempt number;
592 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
593 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
594 payload_state.DownloadComplete();
595 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
596 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700597 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800598 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700599 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800600}
601
602TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) {
603 OmahaResponse response;
604 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700605 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800606
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700607 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700608 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800609
610 // Generate enough events to advance URL index, failure count and
611 // payload attempt number all to 1.
612 payload_state.DownloadComplete();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700613 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
614 payload_state.UpdateFailed(ErrorCode::kDownloadTransferError);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800615 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700616 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700617 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800618 EXPECT_EQ(1, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700619 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800620
621 // Now, simulate a corrupted url index on persisted store which gets
622 // loaded when update_engine restarts. Using a different prefs object
623 // so as to not bother accounting for the uninteresting calls above.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700624 FakeSystemState fake_system_state2;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800625 NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700626 EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700627 EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700628 EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _))
629 .Times(AtLeast(1));
Alex Deymo820cc702013-06-28 15:43:46 -0700630 EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _))
631 .Times(AtLeast(1));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700632 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _))
633 .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true)));
634 EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _))
635 .Times(AtLeast(1));
David Zeuthencc6f9962013-04-18 11:57:24 -0700636 EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _))
637 .Times(AtLeast(1));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800638
639 // Note: This will be a different payload object, but the response should
640 // have the same hash as before so as to not trivially reset because the
641 // response was different. We want to specifically test that even if the
642 // response is same, we should reset the state if we find it corrupted.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700643 EXPECT_TRUE(payload_state.Initialize(&fake_system_state2));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700644 SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800645
646 // Make sure all counters get reset to 0 because of the corrupted URL index
647 // we supplied above.
648 EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700649 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan53173b92013-05-17 17:13:01 -0700650 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800651 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
David Zeuthencc6f9962013-04-18 11:57:24 -0700652 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800653}
Jay Srinivasan08262882012-12-28 19:29:43 -0800654
Chris Sosa20f005c2013-09-05 13:53:08 -0700655TEST(PayloadStateTest, NoBackoffInteractiveChecks) {
656 OmahaResponse response;
657 response.is_delta_payload = false;
658 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 FakeSystemState fake_system_state;
660 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700661 params.Init("", "", true); // is_interactive = True.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700662 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700663
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700664 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700665 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
666
667 // Simulate two failures (enough to cause payload backoff) and check
668 // again that we're ready to re-download without any backoff as this is
669 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700670 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
671 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700672 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
673 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
674 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
675 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
676}
677
678TEST(PayloadStateTest, NoBackoffForP2PUpdates) {
679 OmahaResponse response;
680 response.is_delta_payload = false;
681 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700682 FakeSystemState fake_system_state;
683 OmahaRequestParams params(&fake_system_state);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700684 params.Init("", "", false); // is_interactive = False.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700685 fake_system_state.set_request_params(&params);
Chris Sosa20f005c2013-09-05 13:53:08 -0700686
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700687 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosa20f005c2013-09-05 13:53:08 -0700688 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
689
690 // Simulate two failures (enough to cause payload backoff) and check
691 // again that we're ready to re-download without any backoff as this is
692 // an interactive check.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700693 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
694 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Chris Sosa20f005c2013-09-05 13:53:08 -0700695 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
696 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
697 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
698 // Set p2p url.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700699 payload_state.SetUsingP2PForDownloading(true);
700 payload_state.SetP2PUrl("http://mypeer:52909/path/to/file");
Chris Sosa20f005c2013-09-05 13:53:08 -0700701 // Should not backoff for p2p updates.
702 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
703
Gilad Arnold74b5f552014-10-07 08:17:16 -0700704 payload_state.SetP2PUrl("");
Chris Sosa20f005c2013-09-05 13:53:08 -0700705 // No actual p2p update if no url is provided.
706 EXPECT_TRUE(payload_state.ShouldBackoffDownload());
707}
708
Jay Srinivasan08262882012-12-28 19:29:43 -0800709TEST(PayloadStateTest, NoBackoffForDeltaPayloads) {
710 OmahaResponse response;
711 response.is_delta_payload = true;
712 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700713 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800714
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700715 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700716 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800717
718 // Simulate a successful download and see that we're ready to download
719 // again without any backoff as this is a delta payload.
720 payload_state.DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700721 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
722 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800723 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
724
725 // Simulate two failures (enough to cause payload backoff) and check
726 // again that we're ready to re-download without any backoff as this is
727 // a delta payload.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700728 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
729 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700730 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
Alex Deymo820cc702013-06-28 15:43:46 -0700731 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
732 EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800733 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
734}
735
736static void CheckPayloadBackoffState(PayloadState* payload_state,
737 int expected_attempt_number,
738 TimeDelta expected_days) {
739 payload_state->DownloadComplete();
Alex Deymo820cc702013-06-28 15:43:46 -0700740 EXPECT_EQ(expected_attempt_number,
741 payload_state->GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800742 EXPECT_TRUE(payload_state->ShouldBackoffDownload());
743 Time backoff_expiry_time = payload_state->GetBackoffExpiryTime();
744 // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases.
745 TimeDelta max_fuzz_delta = TimeDelta::FromHours(7);
746 Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta;
747 Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta;
748 EXPECT_LT(expected_min_time.ToInternalValue(),
749 backoff_expiry_time.ToInternalValue());
750 EXPECT_GT(expected_max_time.ToInternalValue(),
751 backoff_expiry_time.ToInternalValue());
752}
753
754TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) {
755 OmahaResponse response;
756 response.is_delta_payload = false;
757 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700758 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800759
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700760 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700761 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800762
763 CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1));
764 CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2));
765 CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4));
766 CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8));
767 CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16));
768 CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16));
769 CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16));
770 CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16));
771 CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16));
772 CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16));
773}
774
775TEST(PayloadStateTest, BackoffLogicCanBeDisabled) {
776 OmahaResponse response;
777 response.disable_payload_backoff = true;
778 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700779 FakeSystemState fake_system_state;
Jay Srinivasan08262882012-12-28 19:29:43 -0800780
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700781 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700782 SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response);
Jay Srinivasan08262882012-12-28 19:29:43 -0800783
784 // Simulate a successful download and see that we are ready to download
785 // again without any backoff.
786 payload_state.DownloadComplete();
787 EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700788 EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800789 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
790
791 // Test again, this time by simulating two errors that would cause
792 // the payload attempt number to increment due to wrap around. And
793 // check that we are still ready to re-download without any backoff.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700794 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
795 payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch);
Jay Srinivasan08262882012-12-28 19:29:43 -0800796 EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber());
Alex Deymo820cc702013-06-28 15:43:46 -0700797 EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber());
Jay Srinivasan08262882012-12-28 19:29:43 -0800798 EXPECT_FALSE(payload_state.ShouldBackoffDownload());
799}
800
Jay Srinivasan19409b72013-04-12 19:23:36 -0700801TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) {
802 OmahaResponse response;
803 response.disable_payload_backoff = true;
804 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700805 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700806 int https_total = 0;
807 int http_total = 0;
808
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700809 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan53173b92013-05-17 17:13:01 -0700810 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700811 EXPECT_EQ(1, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700812
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700813 // Simulate a previous attempt with in order to set an initial non-zero value
814 // for the total bytes downloaded for HTTP.
815 int prev_chunk = 323456789;
816 http_total += prev_chunk;
817 payload_state.DownloadProgress(prev_chunk);
818
819 // Ensure that the initial values for HTTP reflect this attempt.
820 EXPECT_EQ(prev_chunk,
821 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
822 EXPECT_EQ(http_total,
823 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
824
825 // Change the response hash so as to simulate a new response which will
826 // reset the current bytes downloaded, but not the total bytes downloaded.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700827 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
David Zeuthena573d6f2013-06-14 16:13:36 -0700828 EXPECT_EQ(2, payload_state.GetNumResponsesSeen());
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700829
830 // First, simulate successful download of a few bytes over HTTP.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700831 int first_chunk = 5000000;
832 http_total += first_chunk;
833 payload_state.DownloadProgress(first_chunk);
Jay Srinivasan53173b92013-05-17 17:13:01 -0700834 // Test that first all progress is made on HTTP and none on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700835 EXPECT_EQ(first_chunk,
836 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
837 EXPECT_EQ(http_total,
838 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
839 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
840 kDownloadSourceHttpsServer));
841 EXPECT_EQ(https_total,
842 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
843
844 // Simulate an error that'll cause the url index to point to https.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700845 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700846 payload_state.UpdateFailed(error);
847
Jay Srinivasan53173b92013-05-17 17:13:01 -0700848 // Test that no new progress is made on HTTP and new progress is on HTTPS.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700849 int second_chunk = 23456789;
850 https_total += second_chunk;
851 payload_state.DownloadProgress(second_chunk);
852 EXPECT_EQ(first_chunk,
853 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
854 EXPECT_EQ(http_total,
855 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
856 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
857 kDownloadSourceHttpsServer));
858 EXPECT_EQ(https_total,
859 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
860
861 // Simulate error to go back to http.
862 payload_state.UpdateFailed(error);
863 int third_chunk = 32345678;
864 int http_chunk = first_chunk + third_chunk;
865 http_total += third_chunk;
866 int https_chunk = second_chunk;
867 payload_state.DownloadProgress(third_chunk);
868
869 // Test that third chunk is again back on HTTP. HTTPS remains on second chunk.
870 EXPECT_EQ(http_chunk,
871 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700872 EXPECT_EQ(http_total,
Jay Srinivasan19409b72013-04-12 19:23:36 -0700873 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
874 EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded(
875 kDownloadSourceHttpsServer));
876 EXPECT_EQ(https_total,
877 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
878
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700879 // Simulate error (will cause URL switch), set p2p is to be used and
880 // then do 42MB worth of progress
881 payload_state.UpdateFailed(error);
882 payload_state.SetUsingP2PForDownloading(true);
883 int p2p_total = 42 * 1000 * 1000;
884 payload_state.DownloadProgress(p2p_total);
885
886 EXPECT_EQ(p2p_total,
887 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer));
888
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700889 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700890 .Times(AnyNumber());
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 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700893 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700894 "Installer.SuccessfulMBsDownloadedFromHttpServer",
895 http_chunk / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700896 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700897 "Installer.TotalMBsDownloadedFromHttpServer",
898 http_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700899 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700900 "Installer.SuccessfulMBsDownloadedFromHttpsServer",
901 https_chunk / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700902 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Jay Srinivasan19409b72013-04-12 19:23:36 -0700903 "Installer.TotalMBsDownloadedFromHttpsServer",
904 https_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700905 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700906 "Installer.SuccessfulMBsDownloadedFromHttpPeer",
907 p2p_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700908 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700909 "Installer.TotalMBsDownloadedFromHttpPeer",
910 p2p_total / kNumBytesInOneMiB, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700911 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthencc6f9962013-04-18 11:57:24 -0700912 "Installer.UpdateURLSwitches",
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700913 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700914 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800915 metrics::kMetricSuccessfulUpdateUrlSwitchCount,
916 3, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700917 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen674c3182013-04-18 14:05:20 -0700918 "Installer.UpdateDurationMinutes",
919 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700920 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800921 metrics::kMetricSuccessfulUpdateTotalDurationMinutes,
922 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700923 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen674c3182013-04-18 14:05:20 -0700924 "Installer.UpdateDurationUptimeMinutes",
925 _, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700926 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700927 "Installer.DownloadSourcesUsed",
928 (1 << kDownloadSourceHttpsServer) | (1 << kDownloadSourceHttpServer) |
929 (1 << kDownloadSourceHttpPeer),
930 _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700931 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700932 "Installer.DownloadOverheadPercentage", 318, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700933 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800934 metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
935 314, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700936 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -0700937 "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700938 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800939 metrics::kMetricAttemptPayloadType, kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700940 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800941 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
942 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700943 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo820cc702013-06-28 15:43:46 -0700944 "Installer.AttemptsCount.Total", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700945 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800946 metrics::kMetricSuccessfulUpdateAttemptCount, 1, _, _, _));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700947
948 payload_state.UpdateSucceeded();
949
950 // Make sure the metrics are reset after a successful update.
951 EXPECT_EQ(0,
952 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
953 EXPECT_EQ(0,
954 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
955 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
956 kDownloadSourceHttpsServer));
957 EXPECT_EQ(0,
958 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
David Zeuthena573d6f2013-06-14 16:13:36 -0700959 EXPECT_EQ(0, payload_state.GetNumResponsesSeen());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700960}
961
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700962TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) {
963 OmahaResponse response;
964 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700965 FakeSystemState fake_system_state;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700966
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700967 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700968 SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response);
969
970 // Simulate progress in order to mark HTTP as one of the sources used.
971 int num_bytes = 42 * 1000 * 1000;
972 payload_state.DownloadProgress(num_bytes);
973
974 // Check that this was done via HTTP.
975 EXPECT_EQ(num_bytes,
976 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
977 EXPECT_EQ(num_bytes,
978 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
979
980 // Check that only HTTP is reported as a download source.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700981 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700982 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700983 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700984 "Installer.DownloadSourcesUsed",
985 (1 << kDownloadSourceHttpServer),
986 _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700987 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -0800988 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed,
989 (1 << kDownloadSourceHttpServer),
990 _, _, _));
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700991
992 payload_state.UpdateSucceeded();
993}
994
Jay Srinivasan19409b72013-04-12 19:23:36 -0700995TEST(PayloadStateTest, RestartingUpdateResetsMetrics) {
996 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700997 FakeSystemState fake_system_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700998 PayloadState payload_state;
999
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001000 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Jay Srinivasan19409b72013-04-12 19:23:36 -07001001
1002 // Set the first response.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001003 SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001004
1005 int num_bytes = 10000;
1006 payload_state.DownloadProgress(num_bytes);
1007 EXPECT_EQ(num_bytes,
1008 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
1009 EXPECT_EQ(num_bytes,
1010 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
1011 EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded(
1012 kDownloadSourceHttpsServer));
1013 EXPECT_EQ(0,
1014 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer));
1015
1016 payload_state.UpdateRestarted();
1017 // Make sure the current bytes downloaded is reset, but not the total bytes.
1018 EXPECT_EQ(0,
1019 payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer));
1020 EXPECT_EQ(num_bytes,
1021 payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer));
1022}
1023
Chris Sosabe45bef2013-04-09 18:25:12 -07001024TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001025 FakeSystemState fake_system_state;
Chris Sosabe45bef2013-04-09 18:25:12 -07001026 PayloadState payload_state;
Jay Srinivasan19409b72013-04-12 19:23:36 -07001027
Alex Deymo8427b4a2014-11-05 14:00:32 -08001028 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001029 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0));
Chris Sosabe45bef2013-04-09 18:25:12 -07001030 EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1));
1031
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001032 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosabe45bef2013-04-09 18:25:12 -07001033
1034 payload_state.UpdateRestarted();
1035 EXPECT_EQ(0, payload_state.GetNumReboots());
1036
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001037 fake_system_state.set_system_rebooted(true);
Chris Sosabe45bef2013-04-09 18:25:12 -07001038 payload_state.UpdateResumed();
1039 // Num reboots should be incremented because system rebooted detected.
1040 EXPECT_EQ(1, payload_state.GetNumReboots());
1041
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001042 fake_system_state.set_system_rebooted(false);
Chris Sosabe45bef2013-04-09 18:25:12 -07001043 payload_state.UpdateResumed();
1044 // Num reboots should now be 1 as reboot was not detected.
1045 EXPECT_EQ(1, payload_state.GetNumReboots());
1046
1047 // Restart the update again to verify we set the num of reboots back to 0.
1048 payload_state.UpdateRestarted();
1049 EXPECT_EQ(0, payload_state.GetNumReboots());
1050}
Jay Srinivasan19409b72013-04-12 19:23:36 -07001051
Chris Sosaaa18e162013-06-20 13:20:30 -07001052TEST(PayloadStateTest, RollbackVersion) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001053 FakeSystemState fake_system_state;
Chris Sosaaa18e162013-06-20 13:20:30 -07001054 PayloadState payload_state;
1055
Alex Deymo8427b4a2014-11-05 14:00:32 -08001056 NiceMock<MockPrefs>* mock_powerwash_safe_prefs =
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001057 fake_system_state.mock_powerwash_safe_prefs();
1058 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaaa18e162013-06-20 13:20:30 -07001059
1060 // Verify pre-conditions are good.
1061 EXPECT_TRUE(payload_state.GetRollbackVersion().empty());
1062
1063 // Mock out the os version and make sure it's blacklisted correctly.
1064 string rollback_version = "2345.0.0";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001065 OmahaRequestParams params(&fake_system_state);
Chris Sosaaa18e162013-06-20 13:20:30 -07001066 params.Init(rollback_version, "", false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001067 fake_system_state.set_request_params(&params);
Chris Sosaaa18e162013-06-20 13:20:30 -07001068
1069 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1070 rollback_version));
1071 payload_state.Rollback();
1072
1073 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
Chris Sosab3dcdb32013-09-04 15:22:12 -07001074
1075 // Change it up a little and verify we load it correctly.
1076 rollback_version = "2345.0.1";
1077 // Let's verify we can reload it correctly.
1078 EXPECT_CALL(*mock_powerwash_safe_prefs, GetString(
1079 kPrefsRollbackVersion, _)).WillOnce(DoAll(
1080 SetArgumentPointee<1>(rollback_version), Return(true)));
1081 EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion,
1082 rollback_version));
1083 payload_state.LoadRollbackVersion();
1084 EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion());
David Zeuthenafed4a12014-04-09 15:28:44 -07001085
David Zeuthen96197df2014-04-16 12:22:39 -07001086 // Check that we report only UpdateEngine.Rollback.* metrics in
1087 // UpdateSucceeded().
David Zeuthenafed4a12014-04-09 15:28:44 -07001088 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1089 .Times(0);
1090 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1091 .Times(0);
David Zeuthen96197df2014-04-16 12:22:39 -07001092 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1093 SendEnumToUMA(
1094 metrics::kMetricRollbackResult,
1095 static_cast<int>(metrics::RollbackResult::kSuccess),
1096 static_cast<int>(metrics::RollbackResult::kNumConstants)));
David Zeuthenafed4a12014-04-09 15:28:44 -07001097 payload_state.UpdateSucceeded();
Chris Sosaaa18e162013-06-20 13:20:30 -07001098}
1099
David Zeuthenf413fe52013-04-22 14:04:39 -07001100TEST(PayloadStateTest, DurationsAreCorrect) {
1101 OmahaResponse response;
1102 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001103 FakeSystemState fake_system_state;
David Zeuthenf413fe52013-04-22 14:04:39 -07001104 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001105 FakePrefs fake_prefs;
David Zeuthenf413fe52013-04-22 14:04:39 -07001106
1107 // Set the clock to a well-known time - 1 second on the wall-clock
1108 // and 2 seconds on the monotonic clock
1109 fake_clock.SetWallclockTime(Time::FromInternalValue(1000000));
1110 fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000));
1111
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001112 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001113 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001114 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001115
1116 // Check that durations are correct for a successful update where
1117 // time has advanced 7 seconds on the wall clock and 4 seconds on
1118 // the monotonic clock.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001119 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001120 fake_clock.SetWallclockTime(Time::FromInternalValue(8000000));
1121 fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000));
1122 payload_state.UpdateSucceeded();
1123 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000);
1124 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000);
1125
1126 // Check that durations are reset when a new response comes in.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001127 SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response);
David Zeuthenf413fe52013-04-22 14:04:39 -07001128 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0);
1129 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0);
1130
1131 // Advance time a bit (10 secs), simulate download progress and
1132 // check that durations are updated.
1133 fake_clock.SetWallclockTime(Time::FromInternalValue(18000000));
1134 fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000));
1135 payload_state.DownloadProgress(10);
1136 EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000);
1137 EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000);
1138
1139 // Now simulate a reboot by resetting monotonic time (to 5000) and
1140 // creating a new PayloadState object and check that we load the
1141 // durations correctly (e.g. they are the same as before).
1142 fake_clock.SetMonotonicTime(Time::FromInternalValue(5000));
1143 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001144 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthenf413fe52013-04-22 14:04:39 -07001145 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001146 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1147 10000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001148
1149 // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds
1150 // and check that the durations are increased accordingly.
1151 fake_clock.SetWallclockTime(Time::FromInternalValue(25000000));
1152 fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000));
1153 payload_state2.UpdateSucceeded();
1154 EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001155 EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),
1156 16000000);
David Zeuthenf413fe52013-04-22 14:04:39 -07001157}
1158
David Zeuthene4c58bf2013-06-18 17:26:50 -07001159TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) {
1160 OmahaResponse response;
1161 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001162 FakeSystemState fake_system_state;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001163 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001164 FakePrefs fake_prefs;
David Zeuthene4c58bf2013-06-18 17:26:50 -07001165
1166 // Set the clock to a well-known time (t = 30 seconds).
1167 fake_clock.SetWallclockTime(Time::FromInternalValue(
1168 30 * Time::kMicrosecondsPerSecond));
1169
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001170 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001171 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001172 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001173
1174 // Make the update succeed.
1175 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1176 payload_state.UpdateSucceeded();
1177
1178 // Check that the marker was written.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001179 EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001180
1181 // Now simulate a reboot and set the wallclock time to a later point
1182 // (t = 500 seconds). We do this by using a new PayloadState object
1183 // and checking that it emits the right UMA metric with the right
1184 // value.
1185 fake_clock.SetWallclockTime(Time::FromInternalValue(
1186 500 * Time::kMicrosecondsPerSecond));
1187 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001188 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001189
1190 // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001191 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthene4c58bf2013-06-18 17:26:50 -07001192 "Installer.TimeToRebootMinutes",
1193 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001194 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001195 metrics::kMetricTimeToRebootMinutes,
1196 7, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001197 fake_system_state.set_system_rebooted(true);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001198
1199 payload_state2.UpdateEngineStarted();
1200
1201 // Check that the marker was nuked.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001202 EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker));
David Zeuthene4c58bf2013-06-18 17:26:50 -07001203}
1204
Alex Deymo569c4242013-07-24 12:01:01 -07001205TEST(PayloadStateTest, RestartAfterCrash) {
1206 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001207 FakeSystemState fake_system_state;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001208 NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs();
Alex Deymo569c4242013-07-24 12:01:01 -07001209
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001210 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo569c4242013-07-24 12:01:01 -07001211
David Zeuthen4e1d1492014-04-25 13:12:27 -07001212 // Only the |kPrefsAttemptInProgress| state variable should be read.
Alex Deymo569c4242013-07-24 12:01:01 -07001213 EXPECT_CALL(*prefs, Exists(_)).Times(0);
1214 EXPECT_CALL(*prefs, SetString(_, _)).Times(0);
1215 EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0);
1216 EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0);
1217 EXPECT_CALL(*prefs, GetString(_, _)).Times(0);
1218 EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0);
1219 EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001220 EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _));
Alex Deymo569c4242013-07-24 12:01:01 -07001221
1222 // No metrics are reported after a crash.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001223 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
Alex Deymo569c4242013-07-24 12:01:01 -07001224 SendToUMA(_, _, _, _, _)).Times(0);
1225
1226 // Simulate an update_engine restart without a reboot.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001227 fake_system_state.set_system_rebooted(false);
Alex Deymo569c4242013-07-24 12:01:01 -07001228
1229 payload_state.UpdateEngineStarted();
1230}
1231
David Zeuthen4e1d1492014-04-25 13:12:27 -07001232TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) {
1233 PayloadState payload_state;
1234 FakeSystemState fake_system_state;
1235
1236 // If there's no marker at startup, ensure we don't report a metric.
1237 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1238 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1239 SendEnumToUMA(
1240 metrics::kMetricAttemptResult,
1241 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1242 _)).Times(0);
1243 payload_state.UpdateEngineStarted();
1244}
1245
1246TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) {
1247 PayloadState payload_state;
1248 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001249 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001250
1251 // If we have a marker at startup, ensure it's reported and the
1252 // marker is then cleared.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001253 fake_system_state.set_prefs(&fake_prefs);
1254 fake_prefs.SetBoolean(kPrefsAttemptInProgress, true);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001255
1256 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1257
1258 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1259 SendEnumToUMA(
1260 metrics::kMetricAttemptResult,
1261 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1262 _)).Times(1);
1263 payload_state.UpdateEngineStarted();
1264
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001265 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001266}
1267
1268TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) {
1269 PayloadState payload_state;
1270 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001271 FakePrefs fake_prefs;
David Zeuthen4e1d1492014-04-25 13:12:27 -07001272
1273 // Make sure the marker is written and cleared during an attempt and
1274 // also that we DO NOT emit the metric (since the attempt didn't end
1275 // abnormally).
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001276 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen4e1d1492014-04-25 13:12:27 -07001277 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
1278
1279 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _))
1280 .Times(AnyNumber());
1281 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
1282 .Times(AnyNumber());
1283 EXPECT_CALL(*fake_system_state.mock_metrics_lib(),
1284 SendEnumToUMA(
1285 metrics::kMetricAttemptResult,
1286 static_cast<int>(metrics::AttemptResult::kAbnormalTermination),
1287 _)).Times(0);
1288
1289 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001290 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001291
1292 payload_state.UpdateRestarted();
1293
1294 // Attempt not in progress, should be set.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001295 EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001296
1297 payload_state.UpdateSucceeded();
1298
1299 // Attempt not in progress, should be clear.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001300 EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress));
David Zeuthen4e1d1492014-04-25 13:12:27 -07001301}
1302
Jay Srinivasan53173b92013-05-17 17:13:01 -07001303TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) {
1304 OmahaResponse response;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001305 FakeSystemState fake_system_state;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001306 PayloadState payload_state;
1307
Jay Srinivasan53173b92013-05-17 17:13:01 -07001308 policy::MockDevicePolicy disable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001309 fake_system_state.set_device_policy(&disable_http_policy);
1310 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Chris Sosaf7d80042013-08-22 16:45:17 -07001311
1312 // Test with no device policy. Should default to allowing http.
1313 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1314 .WillRepeatedly(Return(false));
1315
1316 // Set the first response.
1317 SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response);
1318
1319 // Check that we use the HTTP URL since there is no value set for allowing
1320 // http.
1321 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1322
1323 // Test with device policy not allowing http updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001324 EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_))
1325 .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true)));
1326
Chris Sosaf7d80042013-08-22 16:45:17 -07001327 // Reset state and set again.
Jay Srinivasan53173b92013-05-17 17:13:01 -07001328 SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response);
1329
1330 // Check that we skip the HTTP URL and use only the HTTPS url.
1331 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1332
1333 // Advance the URL index to 1 by faking an error.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001334 ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001335 payload_state.UpdateFailed(error);
1336
1337 // Check that we still skip the HTTP URL and use only the HTTPS url.
1338 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1339 EXPECT_EQ(0, payload_state.GetUrlSwitchCount());
1340
1341 // Now, slightly change the response and set it again.
1342 SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response);
1343
1344 // Check that we still skip the HTTP URL and use only the HTTPS url.
1345 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1346
1347 // Now, pretend that the HTTP policy is turned on. We want to make sure
1348 // the new policy is honored.
1349 policy::MockDevicePolicy enable_http_policy;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001350 fake_system_state.set_device_policy(&enable_http_policy);
Jay Srinivasan53173b92013-05-17 17:13:01 -07001351 EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_))
1352 .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true)));
1353
1354 // Now, set the same response using the same hash
1355 // so that we can test that the state is reset not because of the
1356 // hash but because of the policy change which results in candidate url
1357 // list change.
1358 SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response);
1359
1360 // Check that we use the HTTP URL now and the failure count is reset.
1361 EXPECT_EQ("http://test", payload_state.GetCurrentUrl());
1362 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1363
1364 // Fake a failure and see if we're moving over to the HTTPS url and update
1365 // the URL switch count properly.
1366 payload_state.UpdateFailed(error);
1367 EXPECT_EQ("https://test", payload_state.GetCurrentUrl());
1368 EXPECT_EQ(1, payload_state.GetUrlSwitchCount());
1369 EXPECT_EQ(0, payload_state.GetUrlFailureCount());
1370}
1371
Alex Deymo1c656c42013-06-28 11:02:14 -07001372TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) {
1373 OmahaResponse response;
1374 response.is_delta_payload = true;
1375 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001376 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001377
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001378 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001379 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1380
1381 // Simulate a successful download and update.
1382 payload_state.DownloadComplete();
1383
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001384 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001385 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001386 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001387 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001388 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001389 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001390 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001391 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1392 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001393 payload_state.UpdateSucceeded();
1394
1395 // Mock the request to a request where the delta was disabled but Omaha sends
1396 // a delta anyway and test again.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001397 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001398 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001399 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001400
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001401 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001402 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1403
1404 payload_state.DownloadComplete();
1405
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001406 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001407 "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001408 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001409 metrics::kMetricAttemptPayloadType, kPayloadTypeDelta,
1410 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001411 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001412 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta,
1413 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001414 payload_state.UpdateSucceeded();
1415}
1416
1417TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) {
1418 OmahaResponse response;
1419 response.is_delta_payload = false;
1420 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001421 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001422
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001423 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001424 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1425
1426 // Mock the request to a request where the delta was disabled.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001427 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001428 params.set_delta_okay(false);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001429 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001430
1431 // Simulate a successful download and update.
1432 payload_state.DownloadComplete();
1433
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001434 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001435 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001436 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001437 "Installer.PayloadFormat", kPayloadTypeForcedFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001438 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001439 metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull,
1440 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001441 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001442 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull,
1443 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001444 payload_state.UpdateSucceeded();
1445}
1446
1447TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) {
1448 OmahaResponse response;
1449 response.is_delta_payload = false;
1450 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001451 FakeSystemState fake_system_state;
Alex Deymo1c656c42013-06-28 11:02:14 -07001452
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001453 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo1c656c42013-06-28 11:02:14 -07001454 SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response);
1455
Alex Deymo820cc702013-06-28 15:43:46 -07001456 // Mock the request to a request where the delta is enabled, although the
1457 // result is full.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001458 OmahaRequestParams params(&fake_system_state);
Alex Deymo1c656c42013-06-28 11:02:14 -07001459 params.set_delta_okay(true);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001460 fake_system_state.set_request_params(&params);
Alex Deymo1c656c42013-06-28 11:02:14 -07001461
1462 // Simulate a successful download and update.
1463 payload_state.DownloadComplete();
1464
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001465 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -08001466 .Times(AnyNumber());
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001467 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
Alex Deymo1c656c42013-06-28 11:02:14 -07001468 "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001469 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001470 metrics::kMetricAttemptPayloadType, kPayloadTypeFull,
1471 kNumPayloadTypes));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001472 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001473 metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull,
1474 kNumPayloadTypes));
Alex Deymo1c656c42013-06-28 11:02:14 -07001475 payload_state.UpdateSucceeded();
1476}
1477
Alex Deymo42432912013-07-12 20:21:15 -07001478TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001479 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001480 OmahaResponse response;
1481 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001482 FakePrefs fake_prefs;
1483 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001484
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001485 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001486 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001487
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001488 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001489 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1490
1491 // Simulate a successful download and update.
1492 payload_state.DownloadComplete();
1493 payload_state.UpdateSucceeded();
1494 payload_state.ExpectRebootInNewVersion("Version:12345678");
1495
1496 // Reboot into the same environment to get an UMA metric with a value of 1.
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001497 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001498 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001499 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001500 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001501 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001502 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001503
1504 // Simulate a second update and reboot into the same environment, this should
1505 // send a value of 2.
1506 payload_state.ExpectRebootInNewVersion("Version:12345678");
1507
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001508 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001509 "Installer.RebootToNewPartitionAttempt", 2, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001510 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001511 metrics::kMetricFailedUpdateCount, 2, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001512 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001513 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001514
1515 // Simulate a third failed reboot to new version, but this time for a
1516 // different payload. This should send a value of 1 this time.
1517 payload_state.ExpectRebootInNewVersion("Version:3141592");
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001518 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001519 "Installer.RebootToNewPartitionAttempt", 1, _, _, _));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001520 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001521 metrics::kMetricFailedUpdateCount, 1, _, _, _));
Alex Deymo42432912013-07-12 20:21:15 -07001522 payload_state.ReportFailedBootIfNeeded();
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001523 Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib());
Alex Deymo42432912013-07-12 20:21:15 -07001524}
1525
1526TEST(PayloadStateTest, RebootAfterUpdateSucceed) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001527 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001528 OmahaResponse response;
1529 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001530 FakePrefs fake_prefs;
1531 fake_system_state.set_prefs(&fake_prefs);
Alex Deymo42432912013-07-12 20:21:15 -07001532
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001533 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
Don Garrett6646b442013-11-13 15:29:11 -08001534 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001535
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001536 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001537 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1538
1539 // Simulate a successful download and update.
1540 payload_state.DownloadComplete();
1541 payload_state.UpdateSucceeded();
1542 payload_state.ExpectRebootInNewVersion("Version:12345678");
1543
1544 // Change the BootDevice to a different one, no metric should be sent.
Don Garrett6646b442013-11-13 15:29:11 -08001545 fake_hardware->SetBootDevice("/dev/sda5");
Alex Deymo42432912013-07-12 20:21:15 -07001546
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001547 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001548 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1549 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001550 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001551 metrics::kMetricFailedUpdateCount, _, _, _, _))
1552 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001553 payload_state.ReportFailedBootIfNeeded();
1554
1555 // A second reboot in eiher partition should not send a metric.
1556 payload_state.ReportFailedBootIfNeeded();
Don Garrett6646b442013-11-13 15:29:11 -08001557 fake_hardware->SetBootDevice("/dev/sda3");
Alex Deymo42432912013-07-12 20:21:15 -07001558 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001559}
1560
1561TEST(PayloadStateTest, RebootAfterCanceledUpdate) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001562 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001563 OmahaResponse response;
1564 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001565 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001566
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001567 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001568 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001569 SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response);
1570
1571 // Simulate a successful download and update.
1572 payload_state.DownloadComplete();
1573 payload_state.UpdateSucceeded();
1574 payload_state.ExpectRebootInNewVersion("Version:12345678");
1575
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001576 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001577 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1578 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001579 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001580 metrics::kMetricFailedUpdateCount, _, _, _, _))
1581 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001582
1583 // Cancel the applied update.
1584 payload_state.ResetUpdateStatus();
1585
1586 // Simulate a reboot.
1587 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001588}
1589
1590TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001591 FakeSystemState fake_system_state;
Alex Deymo42432912013-07-12 20:21:15 -07001592 PayloadState payload_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001593 FakePrefs fake_prefs;
Alex Deymo42432912013-07-12 20:21:15 -07001594
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001595 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001596 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
Alex Deymo42432912013-07-12 20:21:15 -07001597
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001598 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
Alex Deymo42432912013-07-12 20:21:15 -07001599 "Installer.RebootToNewPartitionAttempt", _, _, _, _))
1600 .Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001601 EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(
David Zeuthen33bae492014-02-25 16:16:18 -08001602 metrics::kMetricFailedUpdateCount, _, _, _, _))
1603 .Times(0);
Alex Deymo42432912013-07-12 20:21:15 -07001604
1605 // Simulate a reboot in this environment.
1606 payload_state.ReportFailedBootIfNeeded();
Alex Deymo42432912013-07-12 20:21:15 -07001607}
1608
David Zeuthendcba8092013-08-06 12:16:35 -07001609TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) {
1610 OmahaResponse response;
1611 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001612 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001613 FakePrefs fake_prefs;
1614 fake_system_state.set_prefs(&fake_prefs);
David Zeuthendcba8092013-08-06 12:16:35 -07001615
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001616 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001617 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1618
1619 // Should allow exactly kMaxP2PAttempts...
1620 for (int n = 0; n < kMaxP2PAttempts; n++) {
1621 payload_state.P2PNewAttempt();
1622 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1623 }
1624 // ... but not more than that.
1625 payload_state.P2PNewAttempt();
1626 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001627}
1628
1629TEST(PayloadStateTest, DisallowP2PAfterDeadline) {
1630 OmahaResponse response;
1631 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001632 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001633 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001634 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001635
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001636 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001637 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001638 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001639 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1640
1641 // Set the clock to 1 second.
1642 Time epoch = Time::FromInternalValue(1000000);
1643 fake_clock.SetWallclockTime(epoch);
1644
1645 // Do an attempt - this will set the timestamp.
1646 payload_state.P2PNewAttempt();
1647
1648 // Check that the timestamp equals what we just set.
1649 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1650
1651 // Time hasn't advanced - this should work.
1652 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1653
1654 // Set clock to half the deadline - this should work.
1655 fake_clock.SetWallclockTime(epoch +
1656 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2);
1657 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1658
1659 // Check that the first attempt timestamp hasn't changed just
1660 // because the wall-clock time changed.
1661 EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp());
1662
1663 // Set clock to _just_ before the deadline - this should work.
1664 fake_clock.SetWallclockTime(epoch +
1665 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1));
1666 EXPECT_TRUE(payload_state.P2PAttemptAllowed());
1667
1668 // Set clock to _just_ after the deadline - this should not work.
1669 fake_clock.SetWallclockTime(epoch +
1670 TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1));
1671 EXPECT_FALSE(payload_state.P2PAttemptAllowed());
David Zeuthendcba8092013-08-06 12:16:35 -07001672}
1673
1674TEST(PayloadStateTest, P2PStateVarsInitialValue) {
1675 OmahaResponse response;
1676 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001677 FakeSystemState fake_system_state;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001678 FakePrefs fake_prefs;
David Zeuthendcba8092013-08-06 12:16:35 -07001679
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001680 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001681 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001682 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1683
1684 Time null_time = Time();
1685 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
1686 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
David Zeuthendcba8092013-08-06 12:16:35 -07001687}
1688
1689TEST(PayloadStateTest, P2PStateVarsArePersisted) {
1690 OmahaResponse response;
1691 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001692 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001693 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001694 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001695 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001696 fake_system_state.set_prefs(&fake_prefs);
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001697 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001698 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1699
1700 // Set the clock to something known.
1701 Time time = Time::FromInternalValue(12345);
1702 fake_clock.SetWallclockTime(time);
1703
1704 // New p2p attempt - as a side-effect this will update the p2p state vars.
1705 payload_state.P2PNewAttempt();
1706 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1707 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1708
1709 // Now create a new PayloadState and check that it loads the state
1710 // vars correctly.
1711 PayloadState payload_state2;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001712 EXPECT_TRUE(payload_state2.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001713 EXPECT_EQ(1, payload_state2.GetP2PNumAttempts());
1714 EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001715}
1716
1717TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) {
1718 OmahaResponse response;
1719 PayloadState payload_state;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001720 FakeSystemState fake_system_state;
David Zeuthendcba8092013-08-06 12:16:35 -07001721 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001722 FakePrefs fake_prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001723 fake_system_state.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001724 fake_system_state.set_prefs(&fake_prefs);
1725
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001726 EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
David Zeuthendcba8092013-08-06 12:16:35 -07001727 SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response);
1728
1729 // Set the clock to something known.
1730 Time time = Time::FromInternalValue(12345);
1731 fake_clock.SetWallclockTime(time);
1732
1733 // New p2p attempt - as a side-effect this will update the p2p state vars.
1734 payload_state.P2PNewAttempt();
1735 EXPECT_EQ(1, payload_state.GetP2PNumAttempts());
1736 EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp());
1737
1738 // Set a new response...
1739 SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response);
1740
1741 // ... and check that it clears the P2P state vars.
1742 Time null_time = Time();
1743 EXPECT_EQ(0, payload_state.GetP2PNumAttempts());
1744 EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
David Zeuthendcba8092013-08-06 12:16:35 -07001745}
1746
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001747} // namespace chromeos_update_engine