Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1 | // 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 | |
| 5 | #include <glib.h> |
| 6 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 7 | #include "base/file_path.h" |
| 8 | #include "base/file_util.h" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 9 | #include "base/stringprintf.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 10 | #include "gmock/gmock.h" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 13 | #include "update_engine/constants.h" |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 14 | #include "update_engine/fake_clock.h" |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 15 | #include "update_engine/fake_hardware.h" |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 16 | #include "update_engine/mock_system_state.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 17 | #include "update_engine/omaha_request_action.h" |
| 18 | #include "update_engine/payload_state.h" |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 19 | #include "update_engine/prefs.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 20 | #include "update_engine/prefs_mock.h" |
| 21 | #include "update_engine/test_utils.h" |
| 22 | #include "update_engine/utils.h" |
| 23 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 24 | using base::Time; |
| 25 | using base::TimeDelta; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 26 | using std::string; |
| 27 | using testing::_; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 28 | using testing::AnyNumber; |
| 29 | using testing::AtLeast; |
| 30 | using testing::Mock; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 31 | using testing::NiceMock; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 32 | using testing::Return; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 33 | using testing::SetArgumentPointee; |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 37 | const char* kCurrentBytesDownloadedFromHttps = |
| 38 | "current-bytes-downloaded-from-HttpsServer"; |
| 39 | const char* kTotalBytesDownloadedFromHttps = |
| 40 | "total-bytes-downloaded-from-HttpsServer"; |
| 41 | const char* kCurrentBytesDownloadedFromHttp = |
| 42 | "current-bytes-downloaded-from-HttpServer"; |
| 43 | const char* kTotalBytesDownloadedFromHttp = |
| 44 | "total-bytes-downloaded-from-HttpServer"; |
| 45 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 46 | static void SetupPayloadStateWith2Urls(string hash, |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 47 | bool http_enabled, |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 48 | PayloadState* payload_state, |
| 49 | OmahaResponse* response) { |
| 50 | response->payload_urls.clear(); |
| 51 | response->payload_urls.push_back("http://test"); |
| 52 | response->payload_urls.push_back("https://test"); |
| 53 | response->size = 523456789; |
| 54 | response->hash = hash; |
| 55 | response->metadata_size = 558123; |
| 56 | response->metadata_signature = "metasign"; |
| 57 | response->max_failure_count_per_url = 3; |
| 58 | payload_state->SetResponse(*response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 59 | string stored_response_sign = payload_state->GetResponseSignature(); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 60 | |
| 61 | string expected_url_https_only = |
| 62 | "NumURLs = 1\n" |
| 63 | "Candidate Url0 = https://test\n"; |
| 64 | |
| 65 | string expected_urls_both = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 66 | "NumURLs = 2\n" |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 67 | "Candidate Url0 = http://test\n" |
| 68 | "Candidate Url1 = https://test\n"; |
| 69 | |
| 70 | string expected_response_sign = |
| 71 | (http_enabled ? expected_urls_both : expected_url_https_only) + |
| 72 | StringPrintf("Payload Size = 523456789\n" |
| 73 | "Payload Sha256 Hash = %s\n" |
| 74 | "Metadata Size = 558123\n" |
| 75 | "Metadata Signature = metasign\n" |
| 76 | "Is Delta Payload = %d\n" |
| 77 | "Max Failure Count Per Url = %d\n" |
| 78 | "Disable Payload Backoff = %d\n", |
| 79 | hash.c_str(), |
| 80 | response->is_delta_payload, |
| 81 | response->max_failure_count_per_url, |
| 82 | response->disable_payload_backoff); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 83 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 86 | class PayloadStateTest : public ::testing::Test { }; |
| 87 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 88 | TEST(PayloadStateTest, DidYouAddANewErrorCode) { |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 89 | if (kErrorCodeUmaReportedMax != 44) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 90 | LOG(ERROR) << "The following failure is intentional. If you added a new " |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 91 | << "ErrorCode enum value, make sure to add it to the " |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 92 | << "PayloadState::UpdateFailed method and then update this test " |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 93 | << "to the new value of kErrorCodeUmaReportedMax, which is " |
| 94 | << kErrorCodeUmaReportedMax; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 95 | EXPECT_FALSE("Please see the log line above"); |
| 96 | } |
| 97 | } |
| 98 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 99 | TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) { |
| 100 | OmahaResponse response; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 101 | MockSystemState mock_system_state; |
| 102 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 103 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 104 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 105 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 106 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 107 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 108 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1)); |
| 109 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1)); |
| 110 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 111 | .Times(AtLeast(1)); |
| 112 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _)) |
| 113 | .Times(AtLeast(1)); |
| 114 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _)) |
| 115 | .Times(AtLeast(1)); |
| 116 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 117 | .Times(AtLeast(1)); |
| 118 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 119 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 120 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 121 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 123 | payload_state.SetResponse(response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 124 | string stored_response_sign = payload_state.GetResponseSignature(); |
| 125 | string expected_response_sign = "NumURLs = 0\n" |
| 126 | "Payload Size = 0\n" |
| 127 | "Payload Sha256 Hash = \n" |
| 128 | "Metadata Size = 0\n" |
| 129 | "Metadata Signature = \n" |
| 130 | "Is Delta Payload = 0\n" |
| 131 | "Max Failure Count Per Url = 0\n" |
| 132 | "Disable Payload Backoff = 0\n"; |
| 133 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 134 | EXPECT_EQ("", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 135 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 136 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 137 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) { |
| 141 | OmahaResponse response; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 142 | response.payload_urls.push_back("https://single.url.test"); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 143 | response.size = 123456789; |
| 144 | response.hash = "hash"; |
| 145 | response.metadata_size = 58123; |
| 146 | response.metadata_signature = "msign"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 147 | MockSystemState mock_system_state; |
| 148 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 149 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 150 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 151 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 152 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 153 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 154 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)) |
| 155 | .Times(AtLeast(1)); |
| 156 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 157 | .Times(AtLeast(1)); |
| 158 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 159 | .Times(AtLeast(1)); |
| 160 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _)) |
| 161 | .Times(AtLeast(1)); |
| 162 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _)) |
| 163 | .Times(AtLeast(1)); |
| 164 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 165 | .Times(AtLeast(1)); |
| 166 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 167 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 168 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)) |
| 169 | .Times(AtLeast(1)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 170 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 171 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 172 | payload_state.SetResponse(response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 173 | string stored_response_sign = payload_state.GetResponseSignature(); |
| 174 | string expected_response_sign = "NumURLs = 1\n" |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 175 | "Candidate Url0 = https://single.url.test\n" |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 176 | "Payload Size = 123456789\n" |
| 177 | "Payload Sha256 Hash = hash\n" |
| 178 | "Metadata Size = 58123\n" |
| 179 | "Metadata Signature = msign\n" |
| 180 | "Is Delta Payload = 0\n" |
| 181 | "Max Failure Count Per Url = 0\n" |
| 182 | "Disable Payload Backoff = 0\n"; |
| 183 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 184 | EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 185 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 186 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 187 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) { |
| 191 | OmahaResponse response; |
| 192 | response.payload_urls.push_back("http://multiple.url.test"); |
| 193 | response.payload_urls.push_back("https://multiple.url.test"); |
| 194 | response.size = 523456789; |
| 195 | response.hash = "rhash"; |
| 196 | response.metadata_size = 558123; |
| 197 | response.metadata_signature = "metasign"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 198 | MockSystemState mock_system_state; |
| 199 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 200 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 201 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 202 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 203 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 204 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 205 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)) |
| 206 | .Times(AtLeast(1)); |
| 207 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 208 | .Times(AtLeast(1)); |
| 209 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 210 | .Times(AtLeast(1)); |
| 211 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 212 | .Times(AtLeast(1)); |
| 213 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 214 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 215 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)) |
| 216 | .Times(AtLeast(1)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 217 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 218 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 219 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 220 | payload_state.SetResponse(response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 221 | string stored_response_sign = payload_state.GetResponseSignature(); |
| 222 | string expected_response_sign = "NumURLs = 2\n" |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 223 | "Candidate Url0 = http://multiple.url.test\n" |
| 224 | "Candidate Url1 = https://multiple.url.test\n" |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 225 | "Payload Size = 523456789\n" |
| 226 | "Payload Sha256 Hash = rhash\n" |
| 227 | "Metadata Size = 558123\n" |
| 228 | "Metadata Signature = metasign\n" |
| 229 | "Is Delta Payload = 0\n" |
| 230 | "Max Failure Count Per Url = 0\n" |
| 231 | "Disable Payload Backoff = 0\n"; |
| 232 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 233 | EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 234 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 235 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 236 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) { |
| 240 | OmahaResponse response; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 241 | MockSystemState mock_system_state; |
| 242 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 243 | PayloadState payload_state; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 244 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 245 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 246 | // Payload attempt should start with 0 and then advance to 1. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 247 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 248 | .Times(AtLeast(1)); |
| 249 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 250 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 251 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 252 | .Times(AtLeast(1)); |
| 253 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1)) |
| 254 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 255 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2)); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 256 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 257 | // Reboots will be set |
| 258 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1)); |
| 259 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 260 | // Url index should go from 0 to 1 twice. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 261 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1)); |
| 262 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 263 | |
| 264 | // Failure count should be called each times url index is set, so that's |
| 265 | // 4 times for this test. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 266 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 267 | .Times(AtLeast(4)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 268 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 269 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 270 | |
| 271 | // This does a SetResponse which causes all the states to be set to 0 for |
| 272 | // the first time. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 273 | SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response); |
| 274 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 275 | |
| 276 | // Verify that on the first error, the URL index advances to 1. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 277 | ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 278 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 279 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 280 | |
| 281 | // Verify that on the next error, the URL index wraps around to 0. |
| 282 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 283 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 284 | |
| 285 | // Verify that on the next error, it again advances to 1. |
| 286 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 287 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 288 | |
| 289 | // Verify that we switched URLs three times |
| 290 | EXPECT_EQ(3, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST(PayloadStateTest, NewResponseResetsPayloadState) { |
| 294 | OmahaResponse response; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 295 | MockSystemState mock_system_state; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 296 | PayloadState payload_state; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 297 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 298 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 299 | |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame^] | 300 | // The first response doesn't send an abandoned event. |
| 301 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 302 | "Installer.UpdatesAbandonedEventCount", 0, _, _, _)).Times(0); |
| 303 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 304 | // Set the first response. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 305 | SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 306 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 307 | |
| 308 | // Advance the URL index to 1 by faking an error. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 309 | ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 310 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 311 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 312 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 313 | |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame^] | 314 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 315 | "Installer.UpdatesAbandonedEventCount", 1, _, _, _)); |
| 316 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 317 | // Now, slightly change the response and set it again. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 318 | SetupPayloadStateWith2Urls("Hash8225", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 319 | EXPECT_EQ(2, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 320 | |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame^] | 321 | // Fake an error again. |
| 322 | payload_state.UpdateFailed(error); |
| 323 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 324 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
| 325 | |
| 326 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 327 | "Installer.UpdatesAbandonedEventCount", 2, _, _, _)); |
| 328 | |
| 329 | // Return a third different response. |
| 330 | SetupPayloadStateWith2Urls("Hash9999", true, &payload_state, &response); |
| 331 | EXPECT_EQ(3, payload_state.GetNumResponsesSeen()); |
| 332 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 333 | // Make sure the url index was reset to 0 because of the new response. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 334 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 335 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 336 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 337 | EXPECT_EQ(0, |
| 338 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 339 | EXPECT_EQ(0, |
| 340 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 341 | EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded( |
| 342 | kDownloadSourceHttpsServer)); |
| 343 | EXPECT_EQ(0, |
| 344 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 347 | TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) { |
| 348 | OmahaResponse response; |
| 349 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 350 | MockSystemState mock_system_state; |
| 351 | int progress_bytes = 100; |
| 352 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 353 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 354 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 355 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 356 | .Times(AtLeast(2)); |
| 357 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 358 | .Times(AtLeast(1)); |
| 359 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2)) |
| 360 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 361 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 362 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 363 | .Times(AtLeast(2)); |
| 364 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1)) |
| 365 | .Times(AtLeast(1)); |
| 366 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2)) |
| 367 | .Times(AtLeast(1)); |
| 368 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 369 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4)); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 370 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 371 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4)); |
| 372 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 373 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 374 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 375 | .Times(AtLeast(7)); |
| 376 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1)) |
| 377 | .Times(AtLeast(2)); |
| 378 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2)) |
| 379 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 380 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 381 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _)) |
| 382 | .Times(AtLeast(1)); |
| 383 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _)) |
| 384 | .Times(AtLeast(1)); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 385 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 386 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 387 | .Times(AtLeast(1)); |
| 388 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 389 | .Times(AtLeast(1)); |
| 390 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes)) |
| 391 | .Times(AtLeast(1)); |
| 392 | EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes)) |
| 393 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 394 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)) |
| 395 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 396 | |
| 397 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 398 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 399 | SetupPayloadStateWith2Urls("Hash5873", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 400 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 401 | |
| 402 | // This should advance the URL index. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 403 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 404 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 405 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 406 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 407 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 408 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 409 | |
| 410 | // This should advance the failure count only. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 411 | payload_state.UpdateFailed(kErrorCodeDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 412 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 413 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 414 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 415 | EXPECT_EQ(1, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 416 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 417 | |
| 418 | // This should advance the failure count only. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 419 | payload_state.UpdateFailed(kErrorCodeDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 420 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 421 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 422 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 423 | EXPECT_EQ(2, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 424 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 425 | |
| 426 | // This should advance the URL index as we've reached the |
| 427 | // max failure count and reset the failure count for the new URL index. |
| 428 | // This should also wrap around the URL index and thus cause the payload |
| 429 | // attempt number to be incremented. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 430 | payload_state.UpdateFailed(kErrorCodeDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 431 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 432 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 433 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 434 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 435 | EXPECT_EQ(2, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 436 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 437 | |
| 438 | // This should advance the URL index. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 439 | payload_state.UpdateFailed(kErrorCodePayloadHashMismatchError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 440 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 441 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 442 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 443 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 444 | EXPECT_EQ(3, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 445 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 446 | |
| 447 | // This should advance the URL index and payload attempt number due to |
| 448 | // wrap-around of URL index. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 449 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMissingError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 450 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 451 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 452 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 453 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 454 | EXPECT_EQ(4, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 455 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 456 | |
| 457 | // This HTTP error code should only increase the failure count. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 458 | payload_state.UpdateFailed(static_cast<ErrorCode>( |
| 459 | kErrorCodeOmahaRequestHTTPResponseBase + 404)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 460 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 461 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 462 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 463 | EXPECT_EQ(1, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 464 | EXPECT_EQ(4, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 465 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 466 | |
| 467 | // And that failure count should be reset when we download some bytes |
| 468 | // afterwards. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 469 | payload_state.DownloadProgress(progress_bytes); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 470 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 471 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 472 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 473 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 474 | EXPECT_EQ(4, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 475 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 476 | |
| 477 | // Now, slightly change the response and set it again. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 478 | SetupPayloadStateWith2Urls("Hash8532", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 479 | EXPECT_EQ(2, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 480 | |
| 481 | // Make sure the url index was reset to 0 because of the new response. |
| 482 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 483 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 484 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 485 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 486 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 487 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 490 | TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 491 | OmahaResponse response; |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 492 | response.is_delta_payload = false; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 493 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 494 | MockSystemState mock_system_state; |
| 495 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 496 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 497 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 498 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 499 | .Times(AtLeast(1)); |
| 500 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 501 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 502 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 503 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 504 | .Times(AtLeast(1)); |
| 505 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1)) |
| 506 | .Times(AtLeast(1)); |
| 507 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 508 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)) |
| 509 | .Times(AtLeast(2)); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 510 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 511 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 512 | .Times(AtLeast(1)); |
| 513 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 514 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 515 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 516 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 517 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 518 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 519 | |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame] | 520 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 521 | "Installer.PayloadAttemptNumber", 1, _, _, _)); |
| 522 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 523 | "Installer.FullPayloadAttemptNumber", 1, _, _, _)); |
| 524 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 525 | // This should just advance the payload attempt number; |
| 526 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 527 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 528 | payload_state.DownloadComplete(); |
| 529 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 530 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
| 531 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
| 532 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
| 533 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
| 534 | } |
| 535 | |
| 536 | TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) { |
| 537 | OmahaResponse response; |
| 538 | response.is_delta_payload = true; |
| 539 | PayloadState payload_state; |
| 540 | MockSystemState mock_system_state; |
| 541 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
| 542 | |
| 543 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AnyNumber()); |
| 544 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 545 | .Times(AtLeast(1)); |
| 546 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 547 | .Times(AtLeast(1)); |
| 548 | |
| 549 | // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads. |
| 550 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 551 | .Times(AtLeast(1)); |
| 552 | |
| 553 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)) |
| 554 | .Times(1); |
| 555 | |
| 556 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 557 | .Times(AtLeast(1)); |
| 558 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 559 | .Times(AtLeast(1)); |
| 560 | |
| 561 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 562 | |
| 563 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 564 | |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame] | 565 | // Metrics for Full payload attempt number is not sent with Delta payloads. |
| 566 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 567 | "Installer.PayloadAttemptNumber", 1, _, _, _)); |
| 568 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 569 | "Installer.FullPayloadAttemptNumber", _, _, _, _)) |
| 570 | .Times(0); |
| 571 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 572 | // This should just advance the payload attempt number; |
| 573 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
| 574 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
| 575 | payload_state.DownloadComplete(); |
| 576 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
| 577 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 578 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 579 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 580 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) { |
| 584 | OmahaResponse response; |
| 585 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 586 | MockSystemState mock_system_state; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 587 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 588 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 589 | SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 590 | |
| 591 | // Generate enough events to advance URL index, failure count and |
| 592 | // payload attempt number all to 1. |
| 593 | payload_state.DownloadComplete(); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 594 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch); |
| 595 | payload_state.UpdateFailed(kErrorCodeDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 596 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 597 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 598 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 599 | EXPECT_EQ(1, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 600 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 601 | |
| 602 | // Now, simulate a corrupted url index on persisted store which gets |
| 603 | // loaded when update_engine restarts. Using a different prefs object |
| 604 | // so as to not bother accounting for the uninteresting calls above. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 605 | MockSystemState mock_system_state2; |
| 606 | NiceMock<PrefsMock>* prefs2 = mock_system_state2.mock_prefs(); |
| 607 | EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true)); |
| 608 | EXPECT_CALL(*prefs2, GetInt64(_,_)).Times(AtLeast(1)); |
| 609 | EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _)) |
| 610 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 611 | EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _)) |
| 612 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 613 | EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _)) |
| 614 | .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true))); |
| 615 | EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _)) |
| 616 | .Times(AtLeast(1)); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 617 | EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _)) |
| 618 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 619 | |
| 620 | // Note: This will be a different payload object, but the response should |
| 621 | // have the same hash as before so as to not trivially reset because the |
| 622 | // response was different. We want to specifically test that even if the |
| 623 | // response is same, we should reset the state if we find it corrupted. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 624 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state2)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 625 | SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 626 | |
| 627 | // Make sure all counters get reset to 0 because of the corrupted URL index |
| 628 | // we supplied above. |
| 629 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 630 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 631 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 632 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 633 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 634 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 635 | |
| 636 | TEST(PayloadStateTest, NoBackoffForDeltaPayloads) { |
| 637 | OmahaResponse response; |
| 638 | response.is_delta_payload = true; |
| 639 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 640 | MockSystemState mock_system_state; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 641 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 642 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 643 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 644 | |
| 645 | // Simulate a successful download and see that we're ready to download |
| 646 | // again without any backoff as this is a delta payload. |
| 647 | payload_state.DownloadComplete(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 648 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
| 649 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 650 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 651 | |
| 652 | // Simulate two failures (enough to cause payload backoff) and check |
| 653 | // again that we're ready to re-download without any backoff as this is |
| 654 | // a delta payload. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 655 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch); |
| 656 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 657 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 658 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
| 659 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 660 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 661 | } |
| 662 | |
| 663 | static void CheckPayloadBackoffState(PayloadState* payload_state, |
| 664 | int expected_attempt_number, |
| 665 | TimeDelta expected_days) { |
| 666 | payload_state->DownloadComplete(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 667 | EXPECT_EQ(expected_attempt_number, |
| 668 | payload_state->GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 669 | EXPECT_TRUE(payload_state->ShouldBackoffDownload()); |
| 670 | Time backoff_expiry_time = payload_state->GetBackoffExpiryTime(); |
| 671 | // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases. |
| 672 | TimeDelta max_fuzz_delta = TimeDelta::FromHours(7); |
| 673 | Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta; |
| 674 | Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta; |
| 675 | EXPECT_LT(expected_min_time.ToInternalValue(), |
| 676 | backoff_expiry_time.ToInternalValue()); |
| 677 | EXPECT_GT(expected_max_time.ToInternalValue(), |
| 678 | backoff_expiry_time.ToInternalValue()); |
| 679 | } |
| 680 | |
| 681 | TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) { |
| 682 | OmahaResponse response; |
| 683 | response.is_delta_payload = false; |
| 684 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 685 | MockSystemState mock_system_state; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 686 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 687 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 688 | SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 689 | |
| 690 | CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1)); |
| 691 | CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2)); |
| 692 | CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4)); |
| 693 | CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8)); |
| 694 | CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16)); |
| 695 | CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16)); |
| 696 | CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16)); |
| 697 | CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16)); |
| 698 | CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16)); |
| 699 | CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16)); |
| 700 | } |
| 701 | |
| 702 | TEST(PayloadStateTest, BackoffLogicCanBeDisabled) { |
| 703 | OmahaResponse response; |
| 704 | response.disable_payload_backoff = true; |
| 705 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 706 | MockSystemState mock_system_state; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 707 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 708 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 709 | SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 710 | |
| 711 | // Simulate a successful download and see that we are ready to download |
| 712 | // again without any backoff. |
| 713 | payload_state.DownloadComplete(); |
| 714 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 715 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 716 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 717 | |
| 718 | // Test again, this time by simulating two errors that would cause |
| 719 | // the payload attempt number to increment due to wrap around. And |
| 720 | // check that we are still ready to re-download without any backoff. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 721 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch); |
| 722 | payload_state.UpdateFailed(kErrorCodeDownloadMetadataSignatureMismatch); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 723 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 724 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 725 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 726 | } |
| 727 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 728 | TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) { |
| 729 | OmahaResponse response; |
| 730 | response.disable_payload_backoff = true; |
| 731 | PayloadState payload_state; |
| 732 | MockSystemState mock_system_state; |
| 733 | int https_total = 0; |
| 734 | int http_total = 0; |
| 735 | |
| 736 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 737 | SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 738 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 739 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 740 | // Simulate a previous attempt with in order to set an initial non-zero value |
| 741 | // for the total bytes downloaded for HTTP. |
| 742 | int prev_chunk = 323456789; |
| 743 | http_total += prev_chunk; |
| 744 | payload_state.DownloadProgress(prev_chunk); |
| 745 | |
| 746 | // Ensure that the initial values for HTTP reflect this attempt. |
| 747 | EXPECT_EQ(prev_chunk, |
| 748 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 749 | EXPECT_EQ(http_total, |
| 750 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 751 | |
| 752 | // Change the response hash so as to simulate a new response which will |
| 753 | // reset the current bytes downloaded, but not the total bytes downloaded. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 754 | SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 755 | EXPECT_EQ(2, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 756 | |
| 757 | // First, simulate successful download of a few bytes over HTTP. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 758 | int first_chunk = 5000000; |
| 759 | http_total += first_chunk; |
| 760 | payload_state.DownloadProgress(first_chunk); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 761 | // Test that first all progress is made on HTTP and none on HTTPS. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 762 | EXPECT_EQ(first_chunk, |
| 763 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 764 | EXPECT_EQ(http_total, |
| 765 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 766 | EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded( |
| 767 | kDownloadSourceHttpsServer)); |
| 768 | EXPECT_EQ(https_total, |
| 769 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 770 | |
| 771 | // Simulate an error that'll cause the url index to point to https. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 772 | ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 773 | payload_state.UpdateFailed(error); |
| 774 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 775 | // Test that no new progress is made on HTTP and new progress is on HTTPS. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 776 | int second_chunk = 23456789; |
| 777 | https_total += second_chunk; |
| 778 | payload_state.DownloadProgress(second_chunk); |
| 779 | EXPECT_EQ(first_chunk, |
| 780 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 781 | EXPECT_EQ(http_total, |
| 782 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 783 | EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded( |
| 784 | kDownloadSourceHttpsServer)); |
| 785 | EXPECT_EQ(https_total, |
| 786 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 787 | |
| 788 | // Simulate error to go back to http. |
| 789 | payload_state.UpdateFailed(error); |
| 790 | int third_chunk = 32345678; |
| 791 | int http_chunk = first_chunk + third_chunk; |
| 792 | http_total += third_chunk; |
| 793 | int https_chunk = second_chunk; |
| 794 | payload_state.DownloadProgress(third_chunk); |
| 795 | |
| 796 | // Test that third chunk is again back on HTTP. HTTPS remains on second chunk. |
| 797 | EXPECT_EQ(http_chunk, |
| 798 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 799 | EXPECT_EQ(http_total, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 800 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 801 | EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded( |
| 802 | kDownloadSourceHttpsServer)); |
| 803 | EXPECT_EQ(https_total, |
| 804 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 805 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 806 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _)) |
| 807 | .Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 808 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 809 | "Installer.SuccessfulMBsDownloadedFromHttpServer", |
| 810 | http_chunk / kNumBytesInOneMiB, _, _, _)); |
| 811 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 812 | "Installer.TotalMBsDownloadedFromHttpServer", |
| 813 | http_total / kNumBytesInOneMiB, _, _, _)); |
| 814 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 815 | "Installer.SuccessfulMBsDownloadedFromHttpsServer", |
| 816 | https_chunk / kNumBytesInOneMiB, _, _, _)); |
| 817 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 818 | "Installer.TotalMBsDownloadedFromHttpsServer", |
| 819 | https_total / kNumBytesInOneMiB, _, _, _)); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 820 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 821 | "Installer.UpdateURLSwitches", |
| 822 | 2, _, _, _)); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 823 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 824 | "Installer.UpdateDurationMinutes", |
| 825 | _, _, _, _)); |
| 826 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 827 | "Installer.UpdateDurationUptimeMinutes", |
| 828 | _, _, _, _)); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 829 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 830 | "Installer.DownloadSourcesUsed", 3, _, _, _)); |
| 831 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 832 | "Installer.DownloadOverheadPercentage", 542, _, _, _)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 833 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendEnumToUMA( |
| 834 | "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 835 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 836 | "Installer.AttemptsCount.Total", 1, _, _, _)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 837 | |
| 838 | payload_state.UpdateSucceeded(); |
| 839 | |
| 840 | // Make sure the metrics are reset after a successful update. |
| 841 | EXPECT_EQ(0, |
| 842 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 843 | EXPECT_EQ(0, |
| 844 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 845 | EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded( |
| 846 | kDownloadSourceHttpsServer)); |
| 847 | EXPECT_EQ(0, |
| 848 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 849 | EXPECT_EQ(0, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | TEST(PayloadStateTest, RestartingUpdateResetsMetrics) { |
| 853 | OmahaResponse response; |
| 854 | MockSystemState mock_system_state; |
| 855 | PayloadState payload_state; |
| 856 | |
| 857 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 858 | |
| 859 | // Set the first response. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 860 | SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 861 | |
| 862 | int num_bytes = 10000; |
| 863 | payload_state.DownloadProgress(num_bytes); |
| 864 | EXPECT_EQ(num_bytes, |
| 865 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 866 | EXPECT_EQ(num_bytes, |
| 867 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 868 | EXPECT_EQ(0, payload_state.GetCurrentBytesDownloaded( |
| 869 | kDownloadSourceHttpsServer)); |
| 870 | EXPECT_EQ(0, |
| 871 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 872 | |
| 873 | payload_state.UpdateRestarted(); |
| 874 | // Make sure the current bytes downloaded is reset, but not the total bytes. |
| 875 | EXPECT_EQ(0, |
| 876 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 877 | EXPECT_EQ(num_bytes, |
| 878 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 879 | } |
| 880 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 881 | TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) { |
| 882 | MockSystemState mock_system_state; |
| 883 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 884 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 885 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
| 886 | EXPECT_CALL(*prefs, SetInt64(_,_)).Times(AtLeast(0)); |
| 887 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1)); |
| 888 | |
| 889 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 890 | |
| 891 | payload_state.UpdateRestarted(); |
| 892 | EXPECT_EQ(0, payload_state.GetNumReboots()); |
| 893 | |
| 894 | EXPECT_CALL(mock_system_state, system_rebooted()).WillOnce(Return(true)); |
| 895 | payload_state.UpdateResumed(); |
| 896 | // Num reboots should be incremented because system rebooted detected. |
| 897 | EXPECT_EQ(1, payload_state.GetNumReboots()); |
| 898 | |
| 899 | EXPECT_CALL(mock_system_state, system_rebooted()).WillOnce(Return(false)); |
| 900 | payload_state.UpdateResumed(); |
| 901 | // Num reboots should now be 1 as reboot was not detected. |
| 902 | EXPECT_EQ(1, payload_state.GetNumReboots()); |
| 903 | |
| 904 | // Restart the update again to verify we set the num of reboots back to 0. |
| 905 | payload_state.UpdateRestarted(); |
| 906 | EXPECT_EQ(0, payload_state.GetNumReboots()); |
| 907 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 908 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 909 | TEST(PayloadStateTest, RollbackVersion) { |
| 910 | MockSystemState mock_system_state; |
| 911 | PayloadState payload_state; |
| 912 | |
| 913 | NiceMock<PrefsMock>* mock_powerwash_safe_prefs = |
| 914 | mock_system_state.mock_powerwash_safe_prefs(); |
| 915 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 916 | |
| 917 | // Verify pre-conditions are good. |
| 918 | EXPECT_TRUE(payload_state.GetRollbackVersion().empty()); |
| 919 | |
| 920 | // Mock out the os version and make sure it's blacklisted correctly. |
| 921 | string rollback_version = "2345.0.0"; |
| 922 | OmahaRequestParams params(&mock_system_state); |
| 923 | params.Init(rollback_version, "", false); |
| 924 | mock_system_state.set_request_params(¶ms); |
| 925 | |
| 926 | EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion, |
| 927 | rollback_version)); |
| 928 | payload_state.Rollback(); |
| 929 | |
| 930 | EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion()); |
| 931 | } |
| 932 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 933 | TEST(PayloadStateTest, DurationsAreCorrect) { |
| 934 | OmahaResponse response; |
| 935 | PayloadState payload_state; |
| 936 | MockSystemState mock_system_state; |
| 937 | FakeClock fake_clock; |
| 938 | Prefs prefs; |
| 939 | string temp_dir; |
| 940 | |
| 941 | // Set the clock to a well-known time - 1 second on the wall-clock |
| 942 | // and 2 seconds on the monotonic clock |
| 943 | fake_clock.SetWallclockTime(Time::FromInternalValue(1000000)); |
| 944 | fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000)); |
| 945 | |
| 946 | // We need persistent preferences for this test |
| 947 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateDurationTests.XXXXXX", |
| 948 | &temp_dir)); |
| 949 | prefs.Init(FilePath(temp_dir)); |
| 950 | |
| 951 | mock_system_state.set_clock(&fake_clock); |
| 952 | mock_system_state.set_prefs(&prefs); |
| 953 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 954 | |
| 955 | // Check that durations are correct for a successful update where |
| 956 | // time has advanced 7 seconds on the wall clock and 4 seconds on |
| 957 | // the monotonic clock. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 958 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 959 | fake_clock.SetWallclockTime(Time::FromInternalValue(8000000)); |
| 960 | fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000)); |
| 961 | payload_state.UpdateSucceeded(); |
| 962 | EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000); |
| 963 | EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000); |
| 964 | |
| 965 | // Check that durations are reset when a new response comes in. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 966 | SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 967 | EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0); |
| 968 | EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0); |
| 969 | |
| 970 | // Advance time a bit (10 secs), simulate download progress and |
| 971 | // check that durations are updated. |
| 972 | fake_clock.SetWallclockTime(Time::FromInternalValue(18000000)); |
| 973 | fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000)); |
| 974 | payload_state.DownloadProgress(10); |
| 975 | EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000); |
| 976 | EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000); |
| 977 | |
| 978 | // Now simulate a reboot by resetting monotonic time (to 5000) and |
| 979 | // creating a new PayloadState object and check that we load the |
| 980 | // durations correctly (e.g. they are the same as before). |
| 981 | fake_clock.SetMonotonicTime(Time::FromInternalValue(5000)); |
| 982 | PayloadState payload_state2; |
| 983 | EXPECT_TRUE(payload_state2.Initialize(&mock_system_state)); |
| 984 | EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000); |
| 985 | EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),10000000); |
| 986 | |
| 987 | // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds |
| 988 | // and check that the durations are increased accordingly. |
| 989 | fake_clock.SetWallclockTime(Time::FromInternalValue(25000000)); |
| 990 | fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000)); |
| 991 | payload_state2.UpdateSucceeded(); |
| 992 | EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000); |
| 993 | EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(),16000000); |
| 994 | |
| 995 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 996 | } |
| 997 | |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 998 | TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) { |
| 999 | OmahaResponse response; |
| 1000 | PayloadState payload_state; |
| 1001 | MockSystemState mock_system_state; |
| 1002 | FakeClock fake_clock; |
| 1003 | Prefs prefs; |
| 1004 | string temp_dir; |
| 1005 | |
| 1006 | // Set the clock to a well-known time (t = 30 seconds). |
| 1007 | fake_clock.SetWallclockTime(Time::FromInternalValue( |
| 1008 | 30 * Time::kMicrosecondsPerSecond)); |
| 1009 | |
| 1010 | // We need persistent preferences for this test |
| 1011 | EXPECT_TRUE(utils::MakeTempDirectory( |
| 1012 | "/tmp/RebootAfterSuccessfulUpdateTest.XXXXXX", &temp_dir)); |
| 1013 | prefs.Init(FilePath(temp_dir)); |
| 1014 | |
| 1015 | mock_system_state.set_clock(&fake_clock); |
| 1016 | mock_system_state.set_prefs(&prefs); |
| 1017 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1018 | |
| 1019 | // Make the update succeed. |
| 1020 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1021 | payload_state.UpdateSucceeded(); |
| 1022 | |
| 1023 | // Check that the marker was written. |
| 1024 | EXPECT_TRUE(prefs.Exists(kPrefsSystemUpdatedMarker)); |
| 1025 | |
| 1026 | // Now simulate a reboot and set the wallclock time to a later point |
| 1027 | // (t = 500 seconds). We do this by using a new PayloadState object |
| 1028 | // and checking that it emits the right UMA metric with the right |
| 1029 | // value. |
| 1030 | fake_clock.SetWallclockTime(Time::FromInternalValue( |
| 1031 | 500 * Time::kMicrosecondsPerSecond)); |
| 1032 | PayloadState payload_state2; |
| 1033 | EXPECT_TRUE(payload_state2.Initialize(&mock_system_state)); |
| 1034 | |
| 1035 | // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec |
| 1036 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1037 | "Installer.TimeToRebootMinutes", |
| 1038 | 7, _, _, _)); |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1039 | EXPECT_CALL(mock_system_state, system_rebooted()) |
| 1040 | .WillRepeatedly(Return(true)); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1041 | |
| 1042 | payload_state2.UpdateEngineStarted(); |
| 1043 | |
| 1044 | // Check that the marker was nuked. |
| 1045 | EXPECT_FALSE(prefs.Exists(kPrefsSystemUpdatedMarker)); |
| 1046 | |
| 1047 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 1048 | } |
| 1049 | |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1050 | TEST(PayloadStateTest, RestartAfterCrash) { |
| 1051 | PayloadState payload_state; |
| 1052 | MockSystemState mock_system_state; |
| 1053 | NiceMock<PrefsMock>* prefs = mock_system_state.mock_prefs(); |
| 1054 | |
| 1055 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1056 | |
| 1057 | // No prefs should be used after a crash. |
| 1058 | EXPECT_CALL(*prefs, Exists(_)).Times(0); |
| 1059 | EXPECT_CALL(*prefs, SetString(_, _)).Times(0); |
| 1060 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0); |
| 1061 | EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0); |
| 1062 | EXPECT_CALL(*prefs, GetString(_, _)).Times(0); |
| 1063 | EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0); |
| 1064 | EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0); |
| 1065 | |
| 1066 | // No metrics are reported after a crash. |
| 1067 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), |
| 1068 | SendToUMA(_, _, _, _, _)).Times(0); |
| 1069 | |
| 1070 | // Simulate an update_engine restart without a reboot. |
| 1071 | EXPECT_CALL(mock_system_state, system_rebooted()) |
| 1072 | .WillRepeatedly(Return(false)); |
| 1073 | |
| 1074 | payload_state.UpdateEngineStarted(); |
| 1075 | } |
| 1076 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1077 | TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) { |
| 1078 | OmahaResponse response; |
| 1079 | MockSystemState mock_system_state; |
| 1080 | PayloadState payload_state; |
| 1081 | |
| 1082 | // Pretend that this is an offical build so that the HTTP download policy |
| 1083 | // is honored. |
| 1084 | EXPECT_CALL(mock_system_state, IsOfficialBuild()) |
| 1085 | .WillRepeatedly(Return(true)); |
| 1086 | |
| 1087 | policy::MockDevicePolicy disable_http_policy; |
| 1088 | EXPECT_CALL(mock_system_state, device_policy()) |
| 1089 | .WillRepeatedly(Return(&disable_http_policy)); |
| 1090 | EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_)) |
| 1091 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true))); |
| 1092 | |
| 1093 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1094 | |
| 1095 | // Set the first response. |
| 1096 | SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response); |
| 1097 | |
| 1098 | // Check that we skip the HTTP URL and use only the HTTPS url. |
| 1099 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 1100 | |
| 1101 | // Advance the URL index to 1 by faking an error. |
| 1102 | ErrorCode error = kErrorCodeDownloadMetadataSignatureMismatch; |
| 1103 | payload_state.UpdateFailed(error); |
| 1104 | |
| 1105 | // Check that we still skip the HTTP URL and use only the HTTPS url. |
| 1106 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 1107 | EXPECT_EQ(0, payload_state.GetUrlSwitchCount()); |
| 1108 | |
| 1109 | // Now, slightly change the response and set it again. |
| 1110 | SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response); |
| 1111 | |
| 1112 | // Check that we still skip the HTTP URL and use only the HTTPS url. |
| 1113 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 1114 | |
| 1115 | // Now, pretend that the HTTP policy is turned on. We want to make sure |
| 1116 | // the new policy is honored. |
| 1117 | policy::MockDevicePolicy enable_http_policy; |
| 1118 | EXPECT_CALL(mock_system_state, device_policy()) |
| 1119 | .WillRepeatedly(Return(&enable_http_policy)); |
| 1120 | EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_)) |
| 1121 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true))); |
| 1122 | |
| 1123 | // Now, set the same response using the same hash |
| 1124 | // so that we can test that the state is reset not because of the |
| 1125 | // hash but because of the policy change which results in candidate url |
| 1126 | // list change. |
| 1127 | SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response); |
| 1128 | |
| 1129 | // Check that we use the HTTP URL now and the failure count is reset. |
| 1130 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
| 1131 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
| 1132 | |
| 1133 | // Fake a failure and see if we're moving over to the HTTPS url and update |
| 1134 | // the URL switch count properly. |
| 1135 | payload_state.UpdateFailed(error); |
| 1136 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 1137 | EXPECT_EQ(1, payload_state.GetUrlSwitchCount()); |
| 1138 | EXPECT_EQ(0, payload_state.GetUrlFailureCount()); |
| 1139 | } |
| 1140 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1141 | TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) { |
| 1142 | OmahaResponse response; |
| 1143 | response.is_delta_payload = true; |
| 1144 | PayloadState payload_state; |
| 1145 | MockSystemState mock_system_state; |
| 1146 | |
| 1147 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1148 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1149 | |
| 1150 | // Simulate a successful download and update. |
| 1151 | payload_state.DownloadComplete(); |
| 1152 | |
| 1153 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendEnumToUMA( |
| 1154 | "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes)); |
| 1155 | payload_state.UpdateSucceeded(); |
| 1156 | |
| 1157 | // Mock the request to a request where the delta was disabled but Omaha sends |
| 1158 | // a delta anyway and test again. |
| 1159 | OmahaRequestParams params(&mock_system_state); |
| 1160 | params.set_delta_okay(false); |
| 1161 | mock_system_state.set_request_params(¶ms); |
| 1162 | |
| 1163 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1164 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1165 | |
| 1166 | payload_state.DownloadComplete(); |
| 1167 | |
| 1168 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendEnumToUMA( |
| 1169 | "Installer.PayloadFormat", kPayloadTypeDelta, kNumPayloadTypes)); |
| 1170 | payload_state.UpdateSucceeded(); |
| 1171 | } |
| 1172 | |
| 1173 | TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) { |
| 1174 | OmahaResponse response; |
| 1175 | response.is_delta_payload = false; |
| 1176 | PayloadState payload_state; |
| 1177 | MockSystemState mock_system_state; |
| 1178 | |
| 1179 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1180 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1181 | |
| 1182 | // Mock the request to a request where the delta was disabled. |
| 1183 | OmahaRequestParams params(&mock_system_state); |
| 1184 | params.set_delta_okay(false); |
| 1185 | mock_system_state.set_request_params(¶ms); |
| 1186 | |
| 1187 | // Simulate a successful download and update. |
| 1188 | payload_state.DownloadComplete(); |
| 1189 | |
| 1190 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendEnumToUMA( |
| 1191 | "Installer.PayloadFormat", kPayloadTypeForcedFull, kNumPayloadTypes)); |
| 1192 | payload_state.UpdateSucceeded(); |
| 1193 | } |
| 1194 | |
| 1195 | TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) { |
| 1196 | OmahaResponse response; |
| 1197 | response.is_delta_payload = false; |
| 1198 | PayloadState payload_state; |
| 1199 | MockSystemState mock_system_state; |
| 1200 | |
| 1201 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1202 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1203 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 1204 | // Mock the request to a request where the delta is enabled, although the |
| 1205 | // result is full. |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1206 | OmahaRequestParams params(&mock_system_state); |
| 1207 | params.set_delta_okay(true); |
| 1208 | mock_system_state.set_request_params(¶ms); |
| 1209 | |
| 1210 | // Simulate a successful download and update. |
| 1211 | payload_state.DownloadComplete(); |
| 1212 | |
| 1213 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendEnumToUMA( |
| 1214 | "Installer.PayloadFormat", kPayloadTypeFull, kNumPayloadTypes)); |
| 1215 | payload_state.UpdateSucceeded(); |
| 1216 | } |
| 1217 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1218 | TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) { |
| 1219 | FakeHardware fake_hardware; |
| 1220 | MockSystemState mock_system_state; |
| 1221 | OmahaResponse response; |
| 1222 | PayloadState payload_state; |
| 1223 | Prefs prefs; |
| 1224 | string temp_dir; |
| 1225 | |
| 1226 | // Setup an environment with persistent prefs across simulated reboots. |
| 1227 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateReboot.XXXXXX", |
| 1228 | &temp_dir)); |
| 1229 | prefs.Init(FilePath(temp_dir)); |
| 1230 | mock_system_state.set_prefs(&prefs); |
| 1231 | |
| 1232 | fake_hardware.SetBootDevice("/dev/sda3"); |
| 1233 | fake_hardware.SetKernelDeviceOfBootDevice("/dev/sda3", "/dev/sda2"); |
| 1234 | mock_system_state.set_hardware(&fake_hardware); |
| 1235 | |
| 1236 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1237 | SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response); |
| 1238 | |
| 1239 | // Simulate a successful download and update. |
| 1240 | payload_state.DownloadComplete(); |
| 1241 | payload_state.UpdateSucceeded(); |
| 1242 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1243 | |
| 1244 | // Reboot into the same environment to get an UMA metric with a value of 1. |
| 1245 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1246 | "Installer.RebootToNewPartitionAttempt", 1, _, _, _)); |
| 1247 | payload_state.ReportFailedBootIfNeeded(); |
| 1248 | Mock::VerifyAndClearExpectations(mock_system_state.mock_metrics_lib()); |
| 1249 | |
| 1250 | // Simulate a second update and reboot into the same environment, this should |
| 1251 | // send a value of 2. |
| 1252 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1253 | |
| 1254 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1255 | "Installer.RebootToNewPartitionAttempt", 2, _, _, _)); |
| 1256 | payload_state.ReportFailedBootIfNeeded(); |
| 1257 | Mock::VerifyAndClearExpectations(mock_system_state.mock_metrics_lib()); |
| 1258 | |
| 1259 | // Simulate a third failed reboot to new version, but this time for a |
| 1260 | // different payload. This should send a value of 1 this time. |
| 1261 | payload_state.ExpectRebootInNewVersion("Version:3141592"); |
| 1262 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1263 | "Installer.RebootToNewPartitionAttempt", 1, _, _, _)); |
| 1264 | payload_state.ReportFailedBootIfNeeded(); |
| 1265 | Mock::VerifyAndClearExpectations(mock_system_state.mock_metrics_lib()); |
| 1266 | |
| 1267 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 1268 | } |
| 1269 | |
| 1270 | TEST(PayloadStateTest, RebootAfterUpdateSucceed) { |
| 1271 | FakeHardware fake_hardware; |
| 1272 | MockSystemState mock_system_state; |
| 1273 | OmahaResponse response; |
| 1274 | PayloadState payload_state; |
| 1275 | Prefs prefs; |
| 1276 | string temp_dir; |
| 1277 | |
| 1278 | // Setup an environment with persistent prefs across simulated reboots. |
| 1279 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateReboot.XXXXXX", |
| 1280 | &temp_dir)); |
| 1281 | prefs.Init(FilePath(temp_dir)); |
| 1282 | mock_system_state.set_prefs(&prefs); |
| 1283 | |
| 1284 | fake_hardware.SetKernelDeviceOfBootDevice("/dev/sda3", "/dev/sda2"); |
| 1285 | fake_hardware.SetKernelDeviceOfBootDevice("/dev/sda5", "/dev/sda4"); |
| 1286 | fake_hardware.SetBootDevice("/dev/sda3"); |
| 1287 | mock_system_state.set_hardware(&fake_hardware); |
| 1288 | |
| 1289 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1290 | SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response); |
| 1291 | |
| 1292 | // Simulate a successful download and update. |
| 1293 | payload_state.DownloadComplete(); |
| 1294 | payload_state.UpdateSucceeded(); |
| 1295 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1296 | |
| 1297 | // Change the BootDevice to a different one, no metric should be sent. |
| 1298 | fake_hardware.SetBootDevice("/dev/sda5"); |
| 1299 | |
| 1300 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1301 | "Installer.RebootToNewPartitionAttempt", _, _, _, _)) |
| 1302 | .Times(0); |
| 1303 | payload_state.ReportFailedBootIfNeeded(); |
| 1304 | |
| 1305 | // A second reboot in eiher partition should not send a metric. |
| 1306 | payload_state.ReportFailedBootIfNeeded(); |
| 1307 | fake_hardware.SetBootDevice("/dev/sda3"); |
| 1308 | payload_state.ReportFailedBootIfNeeded(); |
| 1309 | |
| 1310 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 1311 | } |
| 1312 | |
| 1313 | TEST(PayloadStateTest, RebootAfterCanceledUpdate) { |
| 1314 | MockSystemState mock_system_state; |
| 1315 | OmahaResponse response; |
| 1316 | PayloadState payload_state; |
| 1317 | Prefs prefs; |
| 1318 | string temp_dir; |
| 1319 | |
| 1320 | // Setup an environment with persistent prefs across simulated reboots. |
| 1321 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateReboot.XXXXXX", |
| 1322 | &temp_dir)); |
| 1323 | prefs.Init(FilePath(temp_dir)); |
| 1324 | mock_system_state.set_prefs(&prefs); |
| 1325 | |
| 1326 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1327 | SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response); |
| 1328 | |
| 1329 | // Simulate a successful download and update. |
| 1330 | payload_state.DownloadComplete(); |
| 1331 | payload_state.UpdateSucceeded(); |
| 1332 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1333 | |
| 1334 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1335 | "Installer.RebootToNewPartitionAttempt", _, _, _, _)) |
| 1336 | .Times(0); |
| 1337 | |
| 1338 | // Cancel the applied update. |
| 1339 | payload_state.ResetUpdateStatus(); |
| 1340 | |
| 1341 | // Simulate a reboot. |
| 1342 | payload_state.ReportFailedBootIfNeeded(); |
| 1343 | |
| 1344 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 1345 | } |
| 1346 | |
| 1347 | TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) { |
| 1348 | MockSystemState mock_system_state; |
| 1349 | PayloadState payload_state; |
| 1350 | Prefs prefs; |
| 1351 | string temp_dir; |
| 1352 | |
| 1353 | // Setup an environment with persistent but initially empty prefs. |
| 1354 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateReboot.XXXXXX", |
| 1355 | &temp_dir)); |
| 1356 | prefs.Init(FilePath(temp_dir)); |
| 1357 | mock_system_state.set_prefs(&prefs); |
| 1358 | |
| 1359 | EXPECT_TRUE(payload_state.Initialize(&mock_system_state)); |
| 1360 | |
| 1361 | EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendToUMA( |
| 1362 | "Installer.RebootToNewPartitionAttempt", _, _, _, _)) |
| 1363 | .Times(0); |
| 1364 | |
| 1365 | // Simulate a reboot in this environment. |
| 1366 | payload_state.ReportFailedBootIfNeeded(); |
| 1367 | |
| 1368 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 1369 | } |
| 1370 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1371 | } |