Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_state.h" |
| 18 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 19 | #include <base/files/file_path.h> |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 20 | #include <base/files/file_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 21 | #include <base/strings/stringprintf.h> |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/constants.h" |
| 26 | #include "update_engine/common/fake_clock.h" |
| 27 | #include "update_engine/common/fake_hardware.h" |
| 28 | #include "update_engine/common/fake_prefs.h" |
| 29 | #include "update_engine/common/mock_prefs.h" |
| 30 | #include "update_engine/common/prefs.h" |
| 31 | #include "update_engine/common/test_utils.h" |
| 32 | #include "update_engine/common/utils.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 33 | #include "update_engine/fake_system_state.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 34 | #include "update_engine/omaha_request_action.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 35 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 36 | using base::Time; |
| 37 | using base::TimeDelta; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 38 | using std::string; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 39 | using testing::AnyNumber; |
| 40 | using testing::AtLeast; |
| 41 | using testing::Mock; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 42 | using testing::NiceMock; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 43 | using testing::Return; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 44 | using testing::SetArgumentPointee; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 45 | using testing::_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 49 | const char* kCurrentBytesDownloadedFromHttps = |
| 50 | "current-bytes-downloaded-from-HttpsServer"; |
| 51 | const char* kTotalBytesDownloadedFromHttps = |
| 52 | "total-bytes-downloaded-from-HttpsServer"; |
| 53 | const char* kCurrentBytesDownloadedFromHttp = |
| 54 | "current-bytes-downloaded-from-HttpServer"; |
| 55 | const char* kTotalBytesDownloadedFromHttp = |
| 56 | "total-bytes-downloaded-from-HttpServer"; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 57 | const char* kCurrentBytesDownloadedFromHttpPeer = |
| 58 | "current-bytes-downloaded-from-HttpPeer"; |
| 59 | const char* kTotalBytesDownloadedFromHttpPeer = |
| 60 | "total-bytes-downloaded-from-HttpPeer"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 61 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 62 | static void SetupPayloadStateWith2Urls(string hash, |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 63 | bool http_enabled, |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 64 | PayloadState* payload_state, |
| 65 | OmahaResponse* response) { |
| 66 | response->payload_urls.clear(); |
| 67 | response->payload_urls.push_back("http://test"); |
| 68 | response->payload_urls.push_back("https://test"); |
| 69 | response->size = 523456789; |
| 70 | response->hash = hash; |
| 71 | response->metadata_size = 558123; |
| 72 | response->metadata_signature = "metasign"; |
| 73 | response->max_failure_count_per_url = 3; |
| 74 | payload_state->SetResponse(*response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 75 | string stored_response_sign = payload_state->GetResponseSignature(); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 76 | |
| 77 | string expected_url_https_only = |
| 78 | "NumURLs = 1\n" |
| 79 | "Candidate Url0 = https://test\n"; |
| 80 | |
| 81 | string expected_urls_both = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 82 | "NumURLs = 2\n" |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 83 | "Candidate Url0 = http://test\n" |
| 84 | "Candidate Url1 = https://test\n"; |
| 85 | |
| 86 | string expected_response_sign = |
| 87 | (http_enabled ? expected_urls_both : expected_url_https_only) + |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 88 | base::StringPrintf("Payload Size = 523456789\n" |
| 89 | "Payload Sha256 Hash = %s\n" |
| 90 | "Metadata Size = 558123\n" |
| 91 | "Metadata Signature = metasign\n" |
| 92 | "Is Delta Payload = %d\n" |
| 93 | "Max Failure Count Per Url = %d\n" |
| 94 | "Disable Payload Backoff = %d\n", |
| 95 | hash.c_str(), |
| 96 | response->is_delta_payload, |
| 97 | response->max_failure_count_per_url, |
| 98 | response->disable_payload_backoff); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 99 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 102 | class PayloadStateTest : public ::testing::Test { }; |
| 103 | |
| 104 | TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) { |
| 105 | OmahaResponse response; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 106 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 107 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 108 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 109 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 110 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 111 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 112 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 113 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)).Times(AtLeast(1)); |
| 114 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1)); |
| 115 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 116 | .Times(AtLeast(1)); |
| 117 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _)) |
| 118 | .Times(AtLeast(1)); |
| 119 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _)) |
| 120 | .Times(AtLeast(1)); |
| 121 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 122 | .Times(AtLeast(1)); |
| 123 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 124 | .Times(AtLeast(1)); |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 125 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0)) |
| 126 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 127 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)).Times(AtLeast(1)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 128 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 129 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 130 | payload_state.SetResponse(response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 131 | string stored_response_sign = payload_state.GetResponseSignature(); |
| 132 | string expected_response_sign = "NumURLs = 0\n" |
| 133 | "Payload Size = 0\n" |
| 134 | "Payload Sha256 Hash = \n" |
| 135 | "Metadata Size = 0\n" |
| 136 | "Metadata Signature = \n" |
| 137 | "Is Delta Payload = 0\n" |
| 138 | "Max Failure Count Per Url = 0\n" |
| 139 | "Disable Payload Backoff = 0\n"; |
| 140 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 141 | EXPECT_EQ("", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 142 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 143 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 144 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) { |
| 148 | OmahaResponse response; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 149 | response.payload_urls.push_back("https://single.url.test"); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 150 | response.size = 123456789; |
| 151 | response.hash = "hash"; |
| 152 | response.metadata_size = 58123; |
| 153 | response.metadata_signature = "msign"; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 154 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 155 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 156 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 157 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 158 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 159 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 160 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 161 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)) |
| 162 | .Times(AtLeast(1)); |
| 163 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 164 | .Times(AtLeast(1)); |
| 165 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 166 | .Times(AtLeast(1)); |
| 167 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _)) |
| 168 | .Times(AtLeast(1)); |
| 169 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _)) |
| 170 | .Times(AtLeast(1)); |
| 171 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 172 | .Times(AtLeast(1)); |
| 173 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 174 | .Times(AtLeast(1)); |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 175 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0)) |
| 176 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 177 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)) |
| 178 | .Times(AtLeast(1)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 179 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 180 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 181 | payload_state.SetResponse(response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 182 | string stored_response_sign = payload_state.GetResponseSignature(); |
| 183 | string expected_response_sign = "NumURLs = 1\n" |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 184 | "Candidate Url0 = https://single.url.test\n" |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 185 | "Payload Size = 123456789\n" |
| 186 | "Payload Sha256 Hash = hash\n" |
| 187 | "Metadata Size = 58123\n" |
| 188 | "Metadata Signature = msign\n" |
| 189 | "Is Delta Payload = 0\n" |
| 190 | "Max Failure Count Per Url = 0\n" |
| 191 | "Disable Payload Backoff = 0\n"; |
| 192 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 193 | EXPECT_EQ("https://single.url.test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 194 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 195 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 196 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) { |
| 200 | OmahaResponse response; |
| 201 | response.payload_urls.push_back("http://multiple.url.test"); |
| 202 | response.payload_urls.push_back("https://multiple.url.test"); |
| 203 | response.size = 523456789; |
| 204 | response.hash = "rhash"; |
| 205 | response.metadata_size = 558123; |
| 206 | response.metadata_signature = "metasign"; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 207 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 208 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 209 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 210 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 211 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 212 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 213 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 214 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, 0)) |
| 215 | .Times(AtLeast(1)); |
| 216 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 217 | .Times(AtLeast(1)); |
| 218 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 219 | .Times(AtLeast(1)); |
| 220 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 221 | .Times(AtLeast(1)); |
| 222 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 223 | .Times(AtLeast(1)); |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 224 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0)) |
| 225 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 226 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)) |
| 227 | .Times(AtLeast(1)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 228 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 229 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 230 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 231 | payload_state.SetResponse(response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 232 | string stored_response_sign = payload_state.GetResponseSignature(); |
| 233 | string expected_response_sign = "NumURLs = 2\n" |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 234 | "Candidate Url0 = http://multiple.url.test\n" |
| 235 | "Candidate Url1 = https://multiple.url.test\n" |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 236 | "Payload Size = 523456789\n" |
| 237 | "Payload Sha256 Hash = rhash\n" |
| 238 | "Metadata Size = 558123\n" |
| 239 | "Metadata Signature = metasign\n" |
| 240 | "Is Delta Payload = 0\n" |
| 241 | "Max Failure Count Per Url = 0\n" |
| 242 | "Disable Payload Backoff = 0\n"; |
| 243 | EXPECT_EQ(expected_response_sign, stored_response_sign); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 244 | EXPECT_EQ("http://multiple.url.test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 245 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 246 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 247 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) { |
| 251 | OmahaResponse response; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 252 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 253 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 254 | PayloadState payload_state; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 255 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 256 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 257 | // Payload attempt should start with 0 and then advance to 1. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 258 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 259 | .Times(AtLeast(1)); |
| 260 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 261 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 262 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 263 | .Times(AtLeast(1)); |
| 264 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1)) |
| 265 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 266 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(2)); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 267 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 268 | // Reboots will be set |
| 269 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, _)).Times(AtLeast(1)); |
| 270 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 271 | // Url index should go from 0 to 1 twice. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 272 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(1)); |
| 273 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 274 | |
| 275 | // Failure count should be called each times url index is set, so that's |
| 276 | // 4 times for this test. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 277 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 278 | .Times(AtLeast(4)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 279 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 280 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 281 | |
| 282 | // This does a SetResponse which causes all the states to be set to 0 for |
| 283 | // the first time. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 284 | SetupPayloadStateWith2Urls("Hash1235", true, &payload_state, &response); |
| 285 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 286 | |
| 287 | // Verify that on the first error, the URL index advances to 1. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 288 | ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 289 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 290 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 291 | |
| 292 | // Verify that on the next error, the URL index wraps around to 0. |
| 293 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 294 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 295 | |
| 296 | // Verify that on the next error, it again advances to 1. |
| 297 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 298 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 299 | |
| 300 | // Verify that we switched URLs three times |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 301 | EXPECT_EQ(3U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | TEST(PayloadStateTest, NewResponseResetsPayloadState) { |
| 305 | OmahaResponse response; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 306 | FakeSystemState fake_system_state; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 307 | PayloadState payload_state; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 308 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 309 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 310 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 311 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _)) |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 312 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 313 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 314 | .Times(AnyNumber()); |
| 315 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 316 | // Set the first response. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 317 | SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 318 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 319 | |
| 320 | // Advance the URL index to 1 by faking an error. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 321 | ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 322 | payload_state.UpdateFailed(error); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 323 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 324 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 325 | |
| 326 | // Now, slightly change the response and set it again. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 327 | SetupPayloadStateWith2Urls("Hash8225", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 328 | EXPECT_EQ(2, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 329 | |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame] | 330 | // Fake an error again. |
| 331 | payload_state.UpdateFailed(error); |
| 332 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 333 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame] | 334 | |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame] | 335 | // Return a third different response. |
| 336 | SetupPayloadStateWith2Urls("Hash9999", true, &payload_state, &response); |
| 337 | EXPECT_EQ(3, payload_state.GetNumResponsesSeen()); |
| 338 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 339 | // 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] | 340 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 341 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 342 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
| 343 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 344 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 345 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 346 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 347 | EXPECT_EQ( |
| 348 | 0U, payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpsServer)); |
| 349 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 350 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 353 | TEST(PayloadStateTest, AllCountersGetUpdatedProperlyOnErrorCodesAndEvents) { |
| 354 | OmahaResponse response; |
| 355 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 356 | FakeSystemState fake_system_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 357 | int progress_bytes = 100; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 358 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 359 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 360 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 361 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 362 | .Times(AtLeast(2)); |
| 363 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 364 | .Times(AtLeast(1)); |
| 365 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 2)) |
| 366 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 367 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 368 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 369 | .Times(AtLeast(2)); |
| 370 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1)) |
| 371 | .Times(AtLeast(1)); |
| 372 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 2)) |
| 373 | .Times(AtLeast(1)); |
| 374 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 375 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)).Times(AtLeast(4)); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 376 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 377 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(AtLeast(4)); |
| 378 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(AtLeast(2)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 379 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 380 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 381 | .Times(AtLeast(7)); |
| 382 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 1)) |
| 383 | .Times(AtLeast(2)); |
| 384 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 2)) |
| 385 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 386 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 387 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateTimestampStart, _)) |
| 388 | .Times(AtLeast(1)); |
| 389 | EXPECT_CALL(*prefs, SetInt64(kPrefsUpdateDurationUptime, _)) |
| 390 | .Times(AtLeast(1)); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 391 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 392 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttps, 0)) |
| 393 | .Times(AtLeast(1)); |
| 394 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, 0)) |
| 395 | .Times(AtLeast(1)); |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 396 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttpPeer, 0)) |
| 397 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 398 | EXPECT_CALL(*prefs, SetInt64(kCurrentBytesDownloadedFromHttp, progress_bytes)) |
| 399 | .Times(AtLeast(1)); |
| 400 | EXPECT_CALL(*prefs, SetInt64(kTotalBytesDownloadedFromHttp, progress_bytes)) |
| 401 | .Times(AtLeast(1)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 402 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 0)) |
| 403 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 404 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 405 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 406 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 407 | SetupPayloadStateWith2Urls("Hash5873", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 408 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 409 | |
| 410 | // This should advance the URL index. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 411 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
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()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 415 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 416 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 417 | |
| 418 | // This should advance the failure count only. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 419 | payload_state.UpdateFailed(ErrorCode::kDownloadTransferError); |
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()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 423 | EXPECT_EQ(1U, payload_state.GetUrlFailureCount()); |
| 424 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 425 | |
| 426 | // This should advance the failure count only. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 427 | payload_state.UpdateFailed(ErrorCode::kDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 428 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 429 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 430 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 431 | EXPECT_EQ(2U, payload_state.GetUrlFailureCount()); |
| 432 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 433 | |
| 434 | // This should advance the URL index as we've reached the |
| 435 | // max failure count and reset the failure count for the new URL index. |
| 436 | // This should also wrap around the URL index and thus cause the payload |
| 437 | // attempt number to be incremented. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 438 | payload_state.UpdateFailed(ErrorCode::kDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 439 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 440 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 441 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 442 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 443 | EXPECT_EQ(2U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 444 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 445 | |
| 446 | // This should advance the URL index. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 447 | payload_state.UpdateFailed(ErrorCode::kPayloadHashMismatchError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 448 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 449 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 450 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 451 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 452 | EXPECT_EQ(3U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 453 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 454 | |
| 455 | // This should advance the URL index and payload attempt number due to |
| 456 | // wrap-around of URL index. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 457 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMissingError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 458 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 459 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 460 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 461 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 462 | EXPECT_EQ(4U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 463 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 464 | |
| 465 | // This HTTP error code should only increase the failure count. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 466 | payload_state.UpdateFailed(static_cast<ErrorCode>( |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 467 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 404)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 468 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 469 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 470 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 471 | EXPECT_EQ(1U, payload_state.GetUrlFailureCount()); |
| 472 | EXPECT_EQ(4U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 473 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 474 | |
| 475 | // And that failure count should be reset when we download some bytes |
| 476 | // afterwards. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 477 | payload_state.DownloadProgress(progress_bytes); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 478 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 479 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 480 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 481 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 482 | EXPECT_EQ(4U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 483 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 484 | |
| 485 | // Now, slightly change the response and set it again. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 486 | SetupPayloadStateWith2Urls("Hash8532", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 487 | EXPECT_EQ(2, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 488 | |
| 489 | // Make sure the url index was reset to 0 because of the new response. |
| 490 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 491 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 492 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 493 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 494 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 495 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 498 | TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulFullDownload) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 499 | OmahaResponse response; |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 500 | response.is_delta_payload = false; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 501 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 502 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 503 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 504 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 505 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 506 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 507 | .Times(AtLeast(1)); |
| 508 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 509 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 510 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 511 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 512 | .Times(AtLeast(1)); |
| 513 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 1)) |
| 514 | .Times(AtLeast(1)); |
| 515 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 516 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)) |
| 517 | .Times(AtLeast(2)); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 518 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 519 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 520 | .Times(AtLeast(1)); |
| 521 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 522 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 523 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 524 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 525 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 526 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 527 | |
| 528 | // This should just advance the payload attempt number; |
| 529 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 530 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 531 | payload_state.DownloadComplete(); |
| 532 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 533 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
| 534 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 535 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 536 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | TEST(PayloadStateTest, PayloadAttemptNumberIncreasesOnSuccessfulDeltaDownload) { |
| 540 | OmahaResponse response; |
| 541 | response.is_delta_payload = true; |
| 542 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 543 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 544 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 545 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 546 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AnyNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 547 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 0)) |
| 548 | .Times(AtLeast(1)); |
| 549 | EXPECT_CALL(*prefs, SetInt64(kPrefsPayloadAttemptNumber, 1)) |
| 550 | .Times(AtLeast(1)); |
| 551 | |
| 552 | // kPrefsFullPayloadAttemptNumber is not incremented for delta payloads. |
| 553 | EXPECT_CALL(*prefs, SetInt64(kPrefsFullPayloadAttemptNumber, 0)) |
| 554 | .Times(AtLeast(1)); |
| 555 | |
| 556 | EXPECT_CALL(*prefs, SetInt64(kPrefsBackoffExpiryTime, _)) |
| 557 | .Times(1); |
| 558 | |
| 559 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlIndex, 0)) |
| 560 | .Times(AtLeast(1)); |
| 561 | EXPECT_CALL(*prefs, SetInt64(kPrefsCurrentUrlFailureCount, 0)) |
| 562 | .Times(AtLeast(1)); |
| 563 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 564 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 565 | |
| 566 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 567 | |
| 568 | // This should just advance the payload attempt number; |
| 569 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
| 570 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
| 571 | payload_state.DownloadComplete(); |
| 572 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
| 573 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 574 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 575 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 576 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | TEST(PayloadStateTest, SetResponseResetsInvalidUrlIndex) { |
| 580 | OmahaResponse response; |
| 581 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 582 | FakeSystemState fake_system_state; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 583 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 584 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 585 | SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 586 | |
| 587 | // Generate enough events to advance URL index, failure count and |
| 588 | // payload attempt number all to 1. |
| 589 | payload_state.DownloadComplete(); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 590 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
| 591 | payload_state.UpdateFailed(ErrorCode::kDownloadTransferError); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 592 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 593 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 594 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 595 | EXPECT_EQ(1U, payload_state.GetUrlFailureCount()); |
| 596 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 597 | |
| 598 | // Now, simulate a corrupted url index on persisted store which gets |
| 599 | // loaded when update_engine restarts. Using a different prefs object |
| 600 | // so as to not bother accounting for the uninteresting calls above. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 601 | FakeSystemState fake_system_state2; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 602 | NiceMock<MockPrefs>* prefs2 = fake_system_state2.mock_prefs(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 603 | EXPECT_CALL(*prefs2, Exists(_)).WillRepeatedly(Return(true)); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 604 | EXPECT_CALL(*prefs2, GetInt64(_, _)).Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 605 | EXPECT_CALL(*prefs2, GetInt64(kPrefsPayloadAttemptNumber, _)) |
| 606 | .Times(AtLeast(1)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 607 | EXPECT_CALL(*prefs2, GetInt64(kPrefsFullPayloadAttemptNumber, _)) |
| 608 | .Times(AtLeast(1)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 609 | EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlIndex, _)) |
| 610 | .WillRepeatedly(DoAll(SetArgumentPointee<1>(2), Return(true))); |
| 611 | EXPECT_CALL(*prefs2, GetInt64(kPrefsCurrentUrlFailureCount, _)) |
| 612 | .Times(AtLeast(1)); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 613 | EXPECT_CALL(*prefs2, GetInt64(kPrefsUrlSwitchCount, _)) |
| 614 | .Times(AtLeast(1)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 615 | |
| 616 | // Note: This will be a different payload object, but the response should |
| 617 | // have the same hash as before so as to not trivially reset because the |
| 618 | // response was different. We want to specifically test that even if the |
| 619 | // response is same, we should reset the state if we find it corrupted. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 620 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state2)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 621 | SetupPayloadStateWith2Urls("Hash4427", true, &payload_state, &response); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 622 | |
| 623 | // Make sure all counters get reset to 0 because of the corrupted URL index |
| 624 | // we supplied above. |
| 625 | EXPECT_EQ(0, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 626 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 627 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 628 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
| 629 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 630 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 631 | |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 632 | TEST(PayloadStateTest, NoBackoffInteractiveChecks) { |
| 633 | OmahaResponse response; |
| 634 | response.is_delta_payload = false; |
| 635 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 636 | FakeSystemState fake_system_state; |
| 637 | OmahaRequestParams params(&fake_system_state); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 638 | params.Init("", "", true); // is_interactive = True. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 639 | fake_system_state.set_request_params(¶ms); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 640 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 641 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 642 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 643 | |
| 644 | // Simulate two failures (enough to cause payload backoff) and check |
| 645 | // again that we're ready to re-download without any backoff as this is |
| 646 | // an interactive check. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 647 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
| 648 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 649 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
| 650 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
| 651 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
| 652 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 653 | } |
| 654 | |
| 655 | TEST(PayloadStateTest, NoBackoffForP2PUpdates) { |
| 656 | OmahaResponse response; |
| 657 | response.is_delta_payload = false; |
| 658 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 659 | FakeSystemState fake_system_state; |
| 660 | OmahaRequestParams params(&fake_system_state); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 661 | params.Init("", "", false); // is_interactive = False. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 662 | fake_system_state.set_request_params(¶ms); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 663 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 664 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 665 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 666 | |
| 667 | // Simulate two failures (enough to cause payload backoff) and check |
| 668 | // again that we're ready to re-download without any backoff as this is |
| 669 | // an interactive check. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 670 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
| 671 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 672 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
| 673 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
| 674 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
| 675 | // Set p2p url. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 676 | payload_state.SetUsingP2PForDownloading(true); |
| 677 | payload_state.SetP2PUrl("http://mypeer:52909/path/to/file"); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 678 | // Should not backoff for p2p updates. |
| 679 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 680 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 681 | payload_state.SetP2PUrl(""); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 682 | // No actual p2p update if no url is provided. |
| 683 | EXPECT_TRUE(payload_state.ShouldBackoffDownload()); |
| 684 | } |
| 685 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 686 | TEST(PayloadStateTest, NoBackoffForDeltaPayloads) { |
| 687 | OmahaResponse response; |
| 688 | response.is_delta_payload = true; |
| 689 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 690 | FakeSystemState fake_system_state; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 691 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 692 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 693 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 694 | |
| 695 | // Simulate a successful download and see that we're ready to download |
| 696 | // again without any backoff as this is a delta payload. |
| 697 | payload_state.DownloadComplete(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 698 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
| 699 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 700 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 701 | |
| 702 | // Simulate two failures (enough to cause payload backoff) and check |
| 703 | // again that we're ready to re-download without any backoff as this is |
| 704 | // a delta payload. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 705 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
| 706 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 707 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 708 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
| 709 | EXPECT_EQ(0, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 710 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 711 | } |
| 712 | |
| 713 | static void CheckPayloadBackoffState(PayloadState* payload_state, |
| 714 | int expected_attempt_number, |
| 715 | TimeDelta expected_days) { |
| 716 | payload_state->DownloadComplete(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 717 | EXPECT_EQ(expected_attempt_number, |
| 718 | payload_state->GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 719 | EXPECT_TRUE(payload_state->ShouldBackoffDownload()); |
| 720 | Time backoff_expiry_time = payload_state->GetBackoffExpiryTime(); |
| 721 | // Add 1 hour extra to the 6 hour fuzz check to tolerate edge cases. |
| 722 | TimeDelta max_fuzz_delta = TimeDelta::FromHours(7); |
| 723 | Time expected_min_time = Time::Now() + expected_days - max_fuzz_delta; |
| 724 | Time expected_max_time = Time::Now() + expected_days + max_fuzz_delta; |
| 725 | EXPECT_LT(expected_min_time.ToInternalValue(), |
| 726 | backoff_expiry_time.ToInternalValue()); |
| 727 | EXPECT_GT(expected_max_time.ToInternalValue(), |
| 728 | backoff_expiry_time.ToInternalValue()); |
| 729 | } |
| 730 | |
| 731 | TEST(PayloadStateTest, BackoffPeriodsAreInCorrectRange) { |
| 732 | OmahaResponse response; |
| 733 | response.is_delta_payload = false; |
| 734 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 735 | FakeSystemState fake_system_state; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 736 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 737 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 738 | SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 739 | |
| 740 | CheckPayloadBackoffState(&payload_state, 1, TimeDelta::FromDays(1)); |
| 741 | CheckPayloadBackoffState(&payload_state, 2, TimeDelta::FromDays(2)); |
| 742 | CheckPayloadBackoffState(&payload_state, 3, TimeDelta::FromDays(4)); |
| 743 | CheckPayloadBackoffState(&payload_state, 4, TimeDelta::FromDays(8)); |
| 744 | CheckPayloadBackoffState(&payload_state, 5, TimeDelta::FromDays(16)); |
| 745 | CheckPayloadBackoffState(&payload_state, 6, TimeDelta::FromDays(16)); |
| 746 | CheckPayloadBackoffState(&payload_state, 7, TimeDelta::FromDays(16)); |
| 747 | CheckPayloadBackoffState(&payload_state, 8, TimeDelta::FromDays(16)); |
| 748 | CheckPayloadBackoffState(&payload_state, 9, TimeDelta::FromDays(16)); |
| 749 | CheckPayloadBackoffState(&payload_state, 10, TimeDelta::FromDays(16)); |
| 750 | } |
| 751 | |
| 752 | TEST(PayloadStateTest, BackoffLogicCanBeDisabled) { |
| 753 | OmahaResponse response; |
| 754 | response.disable_payload_backoff = true; |
| 755 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 756 | FakeSystemState fake_system_state; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 757 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 758 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 759 | SetupPayloadStateWith2Urls("Hash8939", true, &payload_state, &response); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 760 | |
| 761 | // Simulate a successful download and see that we are ready to download |
| 762 | // again without any backoff. |
| 763 | payload_state.DownloadComplete(); |
| 764 | EXPECT_EQ(1, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 765 | EXPECT_EQ(1, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 766 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 767 | |
| 768 | // Test again, this time by simulating two errors that would cause |
| 769 | // the payload attempt number to increment due to wrap around. And |
| 770 | // check that we are still ready to re-download without any backoff. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 771 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
| 772 | payload_state.UpdateFailed(ErrorCode::kDownloadMetadataSignatureMismatch); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 773 | EXPECT_EQ(2, payload_state.GetPayloadAttemptNumber()); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 774 | EXPECT_EQ(2, payload_state.GetFullPayloadAttemptNumber()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 775 | EXPECT_FALSE(payload_state.ShouldBackoffDownload()); |
| 776 | } |
| 777 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 778 | TEST(PayloadStateTest, BytesDownloadedMetricsGetAddedToCorrectSources) { |
| 779 | OmahaResponse response; |
| 780 | response.disable_payload_backoff = true; |
| 781 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 782 | FakeSystemState fake_system_state; |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 783 | uint64_t https_total = 0; |
| 784 | uint64_t http_total = 0; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 785 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 786 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 787 | SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 788 | EXPECT_EQ(1, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 789 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 790 | // Simulate a previous attempt with in order to set an initial non-zero value |
| 791 | // for the total bytes downloaded for HTTP. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 792 | uint64_t prev_chunk = 323456789; |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 793 | http_total += prev_chunk; |
| 794 | payload_state.DownloadProgress(prev_chunk); |
| 795 | |
| 796 | // Ensure that the initial values for HTTP reflect this attempt. |
| 797 | EXPECT_EQ(prev_chunk, |
| 798 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 799 | EXPECT_EQ(http_total, |
| 800 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 801 | |
| 802 | // Change the response hash so as to simulate a new response which will |
| 803 | // reset the current bytes downloaded, but not the total bytes downloaded. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 804 | SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 805 | EXPECT_EQ(2, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 806 | |
| 807 | // First, simulate successful download of a few bytes over HTTP. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 808 | uint64_t first_chunk = 5000000; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 809 | http_total += first_chunk; |
| 810 | payload_state.DownloadProgress(first_chunk); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 811 | // 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] | 812 | EXPECT_EQ(first_chunk, |
| 813 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 814 | EXPECT_EQ(http_total, |
| 815 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 816 | EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded( |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 817 | kDownloadSourceHttpsServer)); |
| 818 | EXPECT_EQ(https_total, |
| 819 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 820 | |
| 821 | // Simulate an error that'll cause the url index to point to https. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 822 | ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 823 | payload_state.UpdateFailed(error); |
| 824 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 825 | // Test that no new progress is made on HTTP and new progress is on HTTPS. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 826 | uint64_t second_chunk = 23456789; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 827 | https_total += second_chunk; |
| 828 | payload_state.DownloadProgress(second_chunk); |
| 829 | EXPECT_EQ(first_chunk, |
| 830 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 831 | EXPECT_EQ(http_total, |
| 832 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 833 | EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded( |
| 834 | kDownloadSourceHttpsServer)); |
| 835 | EXPECT_EQ(https_total, |
| 836 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 837 | |
| 838 | // Simulate error to go back to http. |
| 839 | payload_state.UpdateFailed(error); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 840 | uint64_t third_chunk = 32345678; |
| 841 | uint64_t http_chunk = first_chunk + third_chunk; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 842 | http_total += third_chunk; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 843 | payload_state.DownloadProgress(third_chunk); |
| 844 | |
| 845 | // Test that third chunk is again back on HTTP. HTTPS remains on second chunk. |
| 846 | EXPECT_EQ(http_chunk, |
| 847 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 848 | EXPECT_EQ(http_total, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 849 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 850 | EXPECT_EQ(second_chunk, payload_state.GetCurrentBytesDownloaded( |
| 851 | kDownloadSourceHttpsServer)); |
| 852 | EXPECT_EQ(https_total, |
| 853 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 854 | |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 855 | // Simulate error (will cause URL switch), set p2p is to be used and |
| 856 | // then do 42MB worth of progress |
| 857 | payload_state.UpdateFailed(error); |
| 858 | payload_state.SetUsingP2PForDownloading(true); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 859 | uint64_t p2p_total = 42 * 1000 * 1000; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 860 | payload_state.DownloadProgress(p2p_total); |
| 861 | |
| 862 | EXPECT_EQ(p2p_total, |
| 863 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpPeer)); |
| 864 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 865 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _)) |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 866 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 867 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 868 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 869 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 870 | metrics::kMetricSuccessfulUpdateUrlSwitchCount, |
| 871 | 3, _, _, _)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 872 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 873 | metrics::kMetricSuccessfulUpdateTotalDurationMinutes, |
| 874 | _, _, _, _)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 875 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 876 | metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage, |
| 877 | 314, _, _, _)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 878 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 879 | metrics::kMetricAttemptPayloadType, kPayloadTypeFull, kNumPayloadTypes)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 880 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 881 | metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull, |
| 882 | kNumPayloadTypes)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 883 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 884 | metrics::kMetricSuccessfulUpdateAttemptCount, 1, _, _, _)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 885 | |
| 886 | payload_state.UpdateSucceeded(); |
| 887 | |
| 888 | // Make sure the metrics are reset after a successful update. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 889 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 890 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 891 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 892 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 893 | EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded( |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 894 | kDownloadSourceHttpsServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 895 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 896 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 897 | EXPECT_EQ(0, payload_state.GetNumResponsesSeen()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 900 | TEST(PayloadStateTest, DownloadSourcesUsedIsCorrect) { |
| 901 | OmahaResponse response; |
| 902 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 903 | FakeSystemState fake_system_state; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 904 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 905 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 906 | SetupPayloadStateWith2Urls("Hash3286", true, &payload_state, &response); |
| 907 | |
| 908 | // Simulate progress in order to mark HTTP as one of the sources used. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 909 | uint64_t num_bytes = 42 * 1000 * 1000; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 910 | payload_state.DownloadProgress(num_bytes); |
| 911 | |
| 912 | // Check that this was done via HTTP. |
| 913 | EXPECT_EQ(num_bytes, |
| 914 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 915 | EXPECT_EQ(num_bytes, |
| 916 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 917 | |
| 918 | // Check that only HTTP is reported as a download source. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 919 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _)) |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 920 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 921 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 922 | metrics::kMetricSuccessfulUpdateDownloadSourcesUsed, |
| 923 | (1 << kDownloadSourceHttpServer), |
| 924 | _, _, _)); |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 925 | |
| 926 | payload_state.UpdateSucceeded(); |
| 927 | } |
| 928 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 929 | TEST(PayloadStateTest, RestartingUpdateResetsMetrics) { |
| 930 | OmahaResponse response; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 931 | FakeSystemState fake_system_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 932 | PayloadState payload_state; |
| 933 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 934 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 935 | |
| 936 | // Set the first response. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 937 | SetupPayloadStateWith2Urls("Hash5823", true, &payload_state, &response); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 938 | |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 939 | uint64_t num_bytes = 10000; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 940 | payload_state.DownloadProgress(num_bytes); |
| 941 | EXPECT_EQ(num_bytes, |
| 942 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 943 | EXPECT_EQ(num_bytes, |
| 944 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 945 | EXPECT_EQ(0U, payload_state.GetCurrentBytesDownloaded( |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 946 | kDownloadSourceHttpsServer)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 947 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 948 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpsServer)); |
| 949 | |
| 950 | payload_state.UpdateRestarted(); |
| 951 | // Make sure the current bytes downloaded is reset, but not the total bytes. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 952 | EXPECT_EQ(0U, |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 953 | payload_state.GetCurrentBytesDownloaded(kDownloadSourceHttpServer)); |
| 954 | EXPECT_EQ(num_bytes, |
| 955 | payload_state.GetTotalBytesDownloaded(kDownloadSourceHttpServer)); |
| 956 | } |
| 957 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 958 | TEST(PayloadStateTest, NumRebootsIncrementsCorrectly) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 959 | FakeSystemState fake_system_state; |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 960 | PayloadState payload_state; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 961 | |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 962 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 963 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(AtLeast(0)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 964 | EXPECT_CALL(*prefs, SetInt64(kPrefsNumReboots, 1)).Times(AtLeast(1)); |
| 965 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 966 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 967 | |
| 968 | payload_state.UpdateRestarted(); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 969 | EXPECT_EQ(0U, payload_state.GetNumReboots()); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 970 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 971 | fake_system_state.set_system_rebooted(true); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 972 | payload_state.UpdateResumed(); |
| 973 | // Num reboots should be incremented because system rebooted detected. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 974 | EXPECT_EQ(1U, payload_state.GetNumReboots()); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 975 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 976 | fake_system_state.set_system_rebooted(false); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 977 | payload_state.UpdateResumed(); |
| 978 | // Num reboots should now be 1 as reboot was not detected. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 979 | EXPECT_EQ(1U, payload_state.GetNumReboots()); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 980 | |
| 981 | // Restart the update again to verify we set the num of reboots back to 0. |
| 982 | payload_state.UpdateRestarted(); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 983 | EXPECT_EQ(0U, payload_state.GetNumReboots()); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 984 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 985 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 986 | TEST(PayloadStateTest, RollbackVersion) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 987 | FakeSystemState fake_system_state; |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 988 | PayloadState payload_state; |
| 989 | |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 990 | NiceMock<MockPrefs>* mock_powerwash_safe_prefs = |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 991 | fake_system_state.mock_powerwash_safe_prefs(); |
| 992 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 993 | |
| 994 | // Verify pre-conditions are good. |
| 995 | EXPECT_TRUE(payload_state.GetRollbackVersion().empty()); |
| 996 | |
| 997 | // Mock out the os version and make sure it's blacklisted correctly. |
| 998 | string rollback_version = "2345.0.0"; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 999 | OmahaRequestParams params(&fake_system_state); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1000 | params.Init(rollback_version, "", false); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1001 | fake_system_state.set_request_params(¶ms); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1002 | |
| 1003 | EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion, |
| 1004 | rollback_version)); |
| 1005 | payload_state.Rollback(); |
| 1006 | |
| 1007 | EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion()); |
Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 1008 | |
| 1009 | // Change it up a little and verify we load it correctly. |
| 1010 | rollback_version = "2345.0.1"; |
| 1011 | // Let's verify we can reload it correctly. |
| 1012 | EXPECT_CALL(*mock_powerwash_safe_prefs, GetString( |
| 1013 | kPrefsRollbackVersion, _)).WillOnce(DoAll( |
| 1014 | SetArgumentPointee<1>(rollback_version), Return(true))); |
| 1015 | EXPECT_CALL(*mock_powerwash_safe_prefs, SetString(kPrefsRollbackVersion, |
| 1016 | rollback_version)); |
| 1017 | payload_state.LoadRollbackVersion(); |
| 1018 | EXPECT_EQ(rollback_version, payload_state.GetRollbackVersion()); |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 1019 | |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 1020 | // Check that we report only UpdateEngine.Rollback.* metrics in |
| 1021 | // UpdateSucceeded(). |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 1022 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _)) |
| 1023 | .Times(0); |
| 1024 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
| 1025 | .Times(0); |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 1026 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), |
| 1027 | SendEnumToUMA( |
| 1028 | metrics::kMetricRollbackResult, |
| 1029 | static_cast<int>(metrics::RollbackResult::kSuccess), |
| 1030 | static_cast<int>(metrics::RollbackResult::kNumConstants))); |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 1031 | payload_state.UpdateSucceeded(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1034 | TEST(PayloadStateTest, DurationsAreCorrect) { |
| 1035 | OmahaResponse response; |
| 1036 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1037 | FakeSystemState fake_system_state; |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1038 | FakeClock fake_clock; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1039 | FakePrefs fake_prefs; |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1040 | |
| 1041 | // Set the clock to a well-known time - 1 second on the wall-clock |
| 1042 | // and 2 seconds on the monotonic clock |
| 1043 | fake_clock.SetWallclockTime(Time::FromInternalValue(1000000)); |
| 1044 | fake_clock.SetMonotonicTime(Time::FromInternalValue(2000000)); |
| 1045 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1046 | fake_system_state.set_clock(&fake_clock); |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1047 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1048 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1049 | |
| 1050 | // Check that durations are correct for a successful update where |
| 1051 | // time has advanced 7 seconds on the wall clock and 4 seconds on |
| 1052 | // the monotonic clock. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1053 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1054 | fake_clock.SetWallclockTime(Time::FromInternalValue(8000000)); |
| 1055 | fake_clock.SetMonotonicTime(Time::FromInternalValue(6000000)); |
| 1056 | payload_state.UpdateSucceeded(); |
| 1057 | EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 7000000); |
| 1058 | EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 4000000); |
| 1059 | |
| 1060 | // Check that durations are reset when a new response comes in. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1061 | SetupPayloadStateWith2Urls("Hash8594", true, &payload_state, &response); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1062 | EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 0); |
| 1063 | EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 0); |
| 1064 | |
| 1065 | // Advance time a bit (10 secs), simulate download progress and |
| 1066 | // check that durations are updated. |
| 1067 | fake_clock.SetWallclockTime(Time::FromInternalValue(18000000)); |
| 1068 | fake_clock.SetMonotonicTime(Time::FromInternalValue(16000000)); |
| 1069 | payload_state.DownloadProgress(10); |
| 1070 | EXPECT_EQ(payload_state.GetUpdateDuration().InMicroseconds(), 10000000); |
| 1071 | EXPECT_EQ(payload_state.GetUpdateDurationUptime().InMicroseconds(), 10000000); |
| 1072 | |
| 1073 | // Now simulate a reboot by resetting monotonic time (to 5000) and |
| 1074 | // creating a new PayloadState object and check that we load the |
| 1075 | // durations correctly (e.g. they are the same as before). |
| 1076 | fake_clock.SetMonotonicTime(Time::FromInternalValue(5000)); |
| 1077 | PayloadState payload_state2; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1078 | EXPECT_TRUE(payload_state2.Initialize(&fake_system_state)); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1079 | EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 10000000); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1080 | EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(), |
| 1081 | 10000000); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1082 | |
| 1083 | // Advance wall-clock by 7 seconds and monotonic clock by 6 seconds |
| 1084 | // and check that the durations are increased accordingly. |
| 1085 | fake_clock.SetWallclockTime(Time::FromInternalValue(25000000)); |
| 1086 | fake_clock.SetMonotonicTime(Time::FromInternalValue(6005000)); |
| 1087 | payload_state2.UpdateSucceeded(); |
| 1088 | EXPECT_EQ(payload_state2.GetUpdateDuration().InMicroseconds(), 17000000); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1089 | EXPECT_EQ(payload_state2.GetUpdateDurationUptime().InMicroseconds(), |
| 1090 | 16000000); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1093 | TEST(PayloadStateTest, RebootAfterSuccessfulUpdateTest) { |
| 1094 | OmahaResponse response; |
| 1095 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1096 | FakeSystemState fake_system_state; |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1097 | FakeClock fake_clock; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1098 | FakePrefs fake_prefs; |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1099 | |
| 1100 | // Set the clock to a well-known time (t = 30 seconds). |
| 1101 | fake_clock.SetWallclockTime(Time::FromInternalValue( |
| 1102 | 30 * Time::kMicrosecondsPerSecond)); |
| 1103 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1104 | fake_system_state.set_clock(&fake_clock); |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1105 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1106 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1107 | |
| 1108 | // Make the update succeed. |
| 1109 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1110 | payload_state.UpdateSucceeded(); |
| 1111 | |
| 1112 | // Check that the marker was written. |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1113 | EXPECT_TRUE(fake_prefs.Exists(kPrefsSystemUpdatedMarker)); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1114 | |
| 1115 | // Now simulate a reboot and set the wallclock time to a later point |
| 1116 | // (t = 500 seconds). We do this by using a new PayloadState object |
| 1117 | // and checking that it emits the right UMA metric with the right |
| 1118 | // value. |
| 1119 | fake_clock.SetWallclockTime(Time::FromInternalValue( |
| 1120 | 500 * Time::kMicrosecondsPerSecond)); |
| 1121 | PayloadState payload_state2; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1122 | EXPECT_TRUE(payload_state2.Initialize(&fake_system_state)); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1123 | |
| 1124 | // Expect 500 - 30 seconds = 470 seconds ~= 7 min 50 sec |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1125 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1126 | metrics::kMetricTimeToRebootMinutes, |
| 1127 | 7, _, _, _)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1128 | fake_system_state.set_system_rebooted(true); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1129 | |
| 1130 | payload_state2.UpdateEngineStarted(); |
| 1131 | |
| 1132 | // Check that the marker was nuked. |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1133 | EXPECT_FALSE(fake_prefs.Exists(kPrefsSystemUpdatedMarker)); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1136 | TEST(PayloadStateTest, RestartAfterCrash) { |
| 1137 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1138 | FakeSystemState fake_system_state; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1139 | NiceMock<MockPrefs>* prefs = fake_system_state.mock_prefs(); |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1140 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1141 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1142 | |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1143 | // Only the |kPrefsAttemptInProgress| state variable should be read. |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1144 | EXPECT_CALL(*prefs, Exists(_)).Times(0); |
| 1145 | EXPECT_CALL(*prefs, SetString(_, _)).Times(0); |
| 1146 | EXPECT_CALL(*prefs, SetInt64(_, _)).Times(0); |
| 1147 | EXPECT_CALL(*prefs, SetBoolean(_, _)).Times(0); |
| 1148 | EXPECT_CALL(*prefs, GetString(_, _)).Times(0); |
| 1149 | EXPECT_CALL(*prefs, GetInt64(_, _)).Times(0); |
| 1150 | EXPECT_CALL(*prefs, GetBoolean(_, _)).Times(0); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1151 | EXPECT_CALL(*prefs, GetBoolean(kPrefsAttemptInProgress, _)); |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1152 | |
| 1153 | // No metrics are reported after a crash. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1154 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1155 | SendToUMA(_, _, _, _, _)).Times(0); |
| 1156 | |
| 1157 | // Simulate an update_engine restart without a reboot. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1158 | fake_system_state.set_system_rebooted(false); |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1159 | |
| 1160 | payload_state.UpdateEngineStarted(); |
| 1161 | } |
| 1162 | |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1163 | TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsNoReporting) { |
| 1164 | PayloadState payload_state; |
| 1165 | FakeSystemState fake_system_state; |
| 1166 | |
| 1167 | // If there's no marker at startup, ensure we don't report a metric. |
| 1168 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
| 1169 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), |
| 1170 | SendEnumToUMA( |
| 1171 | metrics::kMetricAttemptResult, |
| 1172 | static_cast<int>(metrics::AttemptResult::kAbnormalTermination), |
| 1173 | _)).Times(0); |
| 1174 | payload_state.UpdateEngineStarted(); |
| 1175 | } |
| 1176 | |
| 1177 | TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsReported) { |
| 1178 | PayloadState payload_state; |
| 1179 | FakeSystemState fake_system_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1180 | FakePrefs fake_prefs; |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1181 | |
| 1182 | // If we have a marker at startup, ensure it's reported and the |
| 1183 | // marker is then cleared. |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1184 | fake_system_state.set_prefs(&fake_prefs); |
| 1185 | fake_prefs.SetBoolean(kPrefsAttemptInProgress, true); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1186 | |
| 1187 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
| 1188 | |
| 1189 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), |
| 1190 | SendEnumToUMA( |
| 1191 | metrics::kMetricAttemptResult, |
| 1192 | static_cast<int>(metrics::AttemptResult::kAbnormalTermination), |
| 1193 | _)).Times(1); |
| 1194 | payload_state.UpdateEngineStarted(); |
| 1195 | |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1196 | EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress)); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | TEST(PayloadStateTest, AbnormalTerminationAttemptMetricsClearedOnSucceess) { |
| 1200 | PayloadState payload_state; |
| 1201 | FakeSystemState fake_system_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1202 | FakePrefs fake_prefs; |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1203 | |
| 1204 | // Make sure the marker is written and cleared during an attempt and |
| 1205 | // also that we DO NOT emit the metric (since the attempt didn't end |
| 1206 | // abnormally). |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1207 | fake_system_state.set_prefs(&fake_prefs); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1208 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
| 1209 | |
| 1210 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA(_, _, _, _, _)) |
| 1211 | .Times(AnyNumber()); |
| 1212 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
| 1213 | .Times(AnyNumber()); |
| 1214 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), |
| 1215 | SendEnumToUMA( |
| 1216 | metrics::kMetricAttemptResult, |
| 1217 | static_cast<int>(metrics::AttemptResult::kAbnormalTermination), |
| 1218 | _)).Times(0); |
| 1219 | |
| 1220 | // Attempt not in progress, should be clear. |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1221 | EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress)); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1222 | |
| 1223 | payload_state.UpdateRestarted(); |
| 1224 | |
| 1225 | // Attempt not in progress, should be set. |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1226 | EXPECT_TRUE(fake_prefs.Exists(kPrefsAttemptInProgress)); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1227 | |
| 1228 | payload_state.UpdateSucceeded(); |
| 1229 | |
| 1230 | // Attempt not in progress, should be clear. |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1231 | EXPECT_FALSE(fake_prefs.Exists(kPrefsAttemptInProgress)); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1234 | TEST(PayloadStateTest, CandidateUrlsComputedCorrectly) { |
| 1235 | OmahaResponse response; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1236 | FakeSystemState fake_system_state; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1237 | PayloadState payload_state; |
| 1238 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1239 | policy::MockDevicePolicy disable_http_policy; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1240 | fake_system_state.set_device_policy(&disable_http_policy); |
| 1241 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Chris Sosa | f7d8004 | 2013-08-22 16:45:17 -0700 | [diff] [blame] | 1242 | |
| 1243 | // Test with no device policy. Should default to allowing http. |
| 1244 | EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_)) |
| 1245 | .WillRepeatedly(Return(false)); |
| 1246 | |
| 1247 | // Set the first response. |
| 1248 | SetupPayloadStateWith2Urls("Hash8433", true, &payload_state, &response); |
| 1249 | |
| 1250 | // Check that we use the HTTP URL since there is no value set for allowing |
| 1251 | // http. |
| 1252 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
| 1253 | |
| 1254 | // Test with device policy not allowing http updates. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1255 | EXPECT_CALL(disable_http_policy, GetHttpDownloadsEnabled(_)) |
| 1256 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(false), Return(true))); |
| 1257 | |
Chris Sosa | f7d8004 | 2013-08-22 16:45:17 -0700 | [diff] [blame] | 1258 | // Reset state and set again. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1259 | SetupPayloadStateWith2Urls("Hash8433", false, &payload_state, &response); |
| 1260 | |
| 1261 | // Check that we skip the HTTP URL and use only the HTTPS url. |
| 1262 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 1263 | |
| 1264 | // Advance the URL index to 1 by faking an error. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1265 | ErrorCode error = ErrorCode::kDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1266 | payload_state.UpdateFailed(error); |
| 1267 | |
| 1268 | // Check that we still skip the HTTP URL and use only the HTTPS url. |
| 1269 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 1270 | EXPECT_EQ(0U, payload_state.GetUrlSwitchCount()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1271 | |
| 1272 | // Now, slightly change the response and set it again. |
| 1273 | SetupPayloadStateWith2Urls("Hash2399", false, &payload_state, &response); |
| 1274 | |
| 1275 | // Check that we still skip the HTTP URL and use only the HTTPS url. |
| 1276 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
| 1277 | |
| 1278 | // Now, pretend that the HTTP policy is turned on. We want to make sure |
| 1279 | // the new policy is honored. |
| 1280 | policy::MockDevicePolicy enable_http_policy; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1281 | fake_system_state.set_device_policy(&enable_http_policy); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1282 | EXPECT_CALL(enable_http_policy, GetHttpDownloadsEnabled(_)) |
| 1283 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(true), Return(true))); |
| 1284 | |
| 1285 | // Now, set the same response using the same hash |
| 1286 | // so that we can test that the state is reset not because of the |
| 1287 | // hash but because of the policy change which results in candidate url |
| 1288 | // list change. |
| 1289 | SetupPayloadStateWith2Urls("Hash2399", true, &payload_state, &response); |
| 1290 | |
| 1291 | // Check that we use the HTTP URL now and the failure count is reset. |
| 1292 | EXPECT_EQ("http://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 1293 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1294 | |
| 1295 | // Fake a failure and see if we're moving over to the HTTPS url and update |
| 1296 | // the URL switch count properly. |
| 1297 | payload_state.UpdateFailed(error); |
| 1298 | EXPECT_EQ("https://test", payload_state.GetCurrentUrl()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 1299 | EXPECT_EQ(1U, payload_state.GetUrlSwitchCount()); |
| 1300 | EXPECT_EQ(0U, payload_state.GetUrlFailureCount()); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1303 | TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsDelta) { |
| 1304 | OmahaResponse response; |
| 1305 | response.is_delta_payload = true; |
| 1306 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1307 | FakeSystemState fake_system_state; |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1308 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1309 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1310 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1311 | |
| 1312 | // Simulate a successful download and update. |
| 1313 | payload_state.DownloadComplete(); |
| 1314 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1315 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1316 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1317 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1318 | metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, kNumPayloadTypes)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1319 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1320 | metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta, |
| 1321 | kNumPayloadTypes)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1322 | payload_state.UpdateSucceeded(); |
| 1323 | |
| 1324 | // Mock the request to a request where the delta was disabled but Omaha sends |
| 1325 | // a delta anyway and test again. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1326 | OmahaRequestParams params(&fake_system_state); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1327 | params.set_delta_okay(false); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1328 | fake_system_state.set_request_params(¶ms); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1329 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1330 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1331 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1332 | |
| 1333 | payload_state.DownloadComplete(); |
| 1334 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1335 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1336 | metrics::kMetricAttemptPayloadType, kPayloadTypeDelta, |
| 1337 | kNumPayloadTypes)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1338 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1339 | metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeDelta, |
| 1340 | kNumPayloadTypes)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1341 | payload_state.UpdateSucceeded(); |
| 1342 | } |
| 1343 | |
| 1344 | TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsForcedFull) { |
| 1345 | OmahaResponse response; |
| 1346 | response.is_delta_payload = false; |
| 1347 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1348 | FakeSystemState fake_system_state; |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1349 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1350 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1351 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1352 | |
| 1353 | // Mock the request to a request where the delta was disabled. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1354 | OmahaRequestParams params(&fake_system_state); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1355 | params.set_delta_okay(false); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1356 | fake_system_state.set_request_params(¶ms); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1357 | |
| 1358 | // Simulate a successful download and update. |
| 1359 | payload_state.DownloadComplete(); |
| 1360 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1361 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1362 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1363 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1364 | metrics::kMetricAttemptPayloadType, kPayloadTypeForcedFull, |
| 1365 | kNumPayloadTypes)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1366 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1367 | metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeForcedFull, |
| 1368 | kNumPayloadTypes)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1369 | payload_state.UpdateSucceeded(); |
| 1370 | } |
| 1371 | |
| 1372 | TEST(PayloadStateTest, PayloadTypeMetricWhenTypeIsFull) { |
| 1373 | OmahaResponse response; |
| 1374 | response.is_delta_payload = false; |
| 1375 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1376 | FakeSystemState fake_system_state; |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1377 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1378 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1379 | SetupPayloadStateWith2Urls("Hash6437", true, &payload_state, &response); |
| 1380 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 1381 | // Mock the request to a request where the delta is enabled, although the |
| 1382 | // result is full. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1383 | OmahaRequestParams params(&fake_system_state); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1384 | params.set_delta_okay(true); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1385 | fake_system_state.set_request_params(¶ms); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1386 | |
| 1387 | // Simulate a successful download and update. |
| 1388 | payload_state.DownloadComplete(); |
| 1389 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1390 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _)) |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1391 | .Times(AnyNumber()); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1392 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1393 | metrics::kMetricAttemptPayloadType, kPayloadTypeFull, |
| 1394 | kNumPayloadTypes)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1395 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendEnumToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1396 | metrics::kMetricSuccessfulUpdatePayloadType, kPayloadTypeFull, |
| 1397 | kNumPayloadTypes)); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 1398 | payload_state.UpdateSucceeded(); |
| 1399 | } |
| 1400 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1401 | TEST(PayloadStateTest, RebootAfterUpdateFailedMetric) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1402 | FakeSystemState fake_system_state; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1403 | OmahaResponse response; |
| 1404 | PayloadState payload_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1405 | FakePrefs fake_prefs; |
| 1406 | fake_system_state.set_prefs(&fake_prefs); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1407 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1408 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1409 | SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response); |
| 1410 | |
| 1411 | // Simulate a successful download and update. |
| 1412 | payload_state.DownloadComplete(); |
| 1413 | payload_state.UpdateSucceeded(); |
| 1414 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1415 | |
| 1416 | // Reboot into the same environment to get an UMA metric with a value of 1. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1417 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1418 | metrics::kMetricFailedUpdateCount, 1, _, _, _)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1419 | payload_state.ReportFailedBootIfNeeded(); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1420 | Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib()); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1421 | |
| 1422 | // Simulate a second update and reboot into the same environment, this should |
| 1423 | // send a value of 2. |
| 1424 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1425 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1426 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1427 | metrics::kMetricFailedUpdateCount, 2, _, _, _)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1428 | payload_state.ReportFailedBootIfNeeded(); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1429 | Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib()); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1430 | |
| 1431 | // Simulate a third failed reboot to new version, but this time for a |
| 1432 | // different payload. This should send a value of 1 this time. |
| 1433 | payload_state.ExpectRebootInNewVersion("Version:3141592"); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1434 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1435 | metrics::kMetricFailedUpdateCount, 1, _, _, _)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1436 | payload_state.ReportFailedBootIfNeeded(); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1437 | Mock::VerifyAndClearExpectations(fake_system_state.mock_metrics_lib()); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | TEST(PayloadStateTest, RebootAfterUpdateSucceed) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1441 | FakeSystemState fake_system_state; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1442 | OmahaResponse response; |
| 1443 | PayloadState payload_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1444 | FakePrefs fake_prefs; |
| 1445 | fake_system_state.set_prefs(&fake_prefs); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1446 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1447 | FakeBootControl* fake_boot_control = fake_system_state.fake_boot_control(); |
| 1448 | fake_boot_control->SetCurrentSlot(0); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1449 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1450 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1451 | SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response); |
| 1452 | |
| 1453 | // Simulate a successful download and update. |
| 1454 | payload_state.DownloadComplete(); |
| 1455 | payload_state.UpdateSucceeded(); |
| 1456 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1457 | |
| 1458 | // Change the BootDevice to a different one, no metric should be sent. |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1459 | fake_boot_control->SetCurrentSlot(1); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1460 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1461 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1462 | metrics::kMetricFailedUpdateCount, _, _, _, _)) |
| 1463 | .Times(0); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1464 | payload_state.ReportFailedBootIfNeeded(); |
| 1465 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1466 | // A second reboot in either partition should not send a metric. |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1467 | payload_state.ReportFailedBootIfNeeded(); |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1468 | fake_boot_control->SetCurrentSlot(0); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1469 | payload_state.ReportFailedBootIfNeeded(); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | TEST(PayloadStateTest, RebootAfterCanceledUpdate) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1473 | FakeSystemState fake_system_state; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1474 | OmahaResponse response; |
| 1475 | PayloadState payload_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1476 | FakePrefs fake_prefs; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1477 | |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1478 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1479 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1480 | SetupPayloadStateWith2Urls("Hash3141", true, &payload_state, &response); |
| 1481 | |
| 1482 | // Simulate a successful download and update. |
| 1483 | payload_state.DownloadComplete(); |
| 1484 | payload_state.UpdateSucceeded(); |
| 1485 | payload_state.ExpectRebootInNewVersion("Version:12345678"); |
| 1486 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1487 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1488 | metrics::kMetricFailedUpdateCount, _, _, _, _)) |
| 1489 | .Times(0); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1490 | |
| 1491 | // Cancel the applied update. |
| 1492 | payload_state.ResetUpdateStatus(); |
| 1493 | |
| 1494 | // Simulate a reboot. |
| 1495 | payload_state.ReportFailedBootIfNeeded(); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1499 | FakeSystemState fake_system_state; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1500 | PayloadState payload_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1501 | FakePrefs fake_prefs; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1502 | |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1503 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1504 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1505 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1506 | EXPECT_CALL(*fake_system_state.mock_metrics_lib(), SendToUMA( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1507 | metrics::kMetricFailedUpdateCount, _, _, _, _)) |
| 1508 | .Times(0); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1509 | |
| 1510 | // Simulate a reboot in this environment. |
| 1511 | payload_state.ReportFailedBootIfNeeded(); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1514 | TEST(PayloadStateTest, DisallowP2PAfterTooManyAttempts) { |
| 1515 | OmahaResponse response; |
| 1516 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1517 | FakeSystemState fake_system_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1518 | FakePrefs fake_prefs; |
| 1519 | fake_system_state.set_prefs(&fake_prefs); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1520 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1521 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1522 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1523 | |
| 1524 | // Should allow exactly kMaxP2PAttempts... |
| 1525 | for (int n = 0; n < kMaxP2PAttempts; n++) { |
| 1526 | payload_state.P2PNewAttempt(); |
| 1527 | EXPECT_TRUE(payload_state.P2PAttemptAllowed()); |
| 1528 | } |
| 1529 | // ... but not more than that. |
| 1530 | payload_state.P2PNewAttempt(); |
| 1531 | EXPECT_FALSE(payload_state.P2PAttemptAllowed()); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | TEST(PayloadStateTest, DisallowP2PAfterDeadline) { |
| 1535 | OmahaResponse response; |
| 1536 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1537 | FakeSystemState fake_system_state; |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1538 | FakeClock fake_clock; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1539 | FakePrefs fake_prefs; |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1540 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1541 | fake_system_state.set_clock(&fake_clock); |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1542 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1543 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1544 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1545 | |
| 1546 | // Set the clock to 1 second. |
| 1547 | Time epoch = Time::FromInternalValue(1000000); |
| 1548 | fake_clock.SetWallclockTime(epoch); |
| 1549 | |
| 1550 | // Do an attempt - this will set the timestamp. |
| 1551 | payload_state.P2PNewAttempt(); |
| 1552 | |
| 1553 | // Check that the timestamp equals what we just set. |
| 1554 | EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp()); |
| 1555 | |
| 1556 | // Time hasn't advanced - this should work. |
| 1557 | EXPECT_TRUE(payload_state.P2PAttemptAllowed()); |
| 1558 | |
| 1559 | // Set clock to half the deadline - this should work. |
| 1560 | fake_clock.SetWallclockTime(epoch + |
| 1561 | TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds) / 2); |
| 1562 | EXPECT_TRUE(payload_state.P2PAttemptAllowed()); |
| 1563 | |
| 1564 | // Check that the first attempt timestamp hasn't changed just |
| 1565 | // because the wall-clock time changed. |
| 1566 | EXPECT_EQ(epoch, payload_state.GetP2PFirstAttemptTimestamp()); |
| 1567 | |
| 1568 | // Set clock to _just_ before the deadline - this should work. |
| 1569 | fake_clock.SetWallclockTime(epoch + |
| 1570 | TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds - 1)); |
| 1571 | EXPECT_TRUE(payload_state.P2PAttemptAllowed()); |
| 1572 | |
| 1573 | // Set clock to _just_ after the deadline - this should not work. |
| 1574 | fake_clock.SetWallclockTime(epoch + |
| 1575 | TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds + 1)); |
| 1576 | EXPECT_FALSE(payload_state.P2PAttemptAllowed()); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | TEST(PayloadStateTest, P2PStateVarsInitialValue) { |
| 1580 | OmahaResponse response; |
| 1581 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1582 | FakeSystemState fake_system_state; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1583 | FakePrefs fake_prefs; |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1584 | |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1585 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1586 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1587 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1588 | |
| 1589 | Time null_time = Time(); |
| 1590 | EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp()); |
| 1591 | EXPECT_EQ(0, payload_state.GetP2PNumAttempts()); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | TEST(PayloadStateTest, P2PStateVarsArePersisted) { |
| 1595 | OmahaResponse response; |
| 1596 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1597 | FakeSystemState fake_system_state; |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1598 | FakeClock fake_clock; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1599 | FakePrefs fake_prefs; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1600 | fake_system_state.set_clock(&fake_clock); |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1601 | fake_system_state.set_prefs(&fake_prefs); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1602 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1603 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1604 | |
| 1605 | // Set the clock to something known. |
| 1606 | Time time = Time::FromInternalValue(12345); |
| 1607 | fake_clock.SetWallclockTime(time); |
| 1608 | |
| 1609 | // New p2p attempt - as a side-effect this will update the p2p state vars. |
| 1610 | payload_state.P2PNewAttempt(); |
| 1611 | EXPECT_EQ(1, payload_state.GetP2PNumAttempts()); |
| 1612 | EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp()); |
| 1613 | |
| 1614 | // Now create a new PayloadState and check that it loads the state |
| 1615 | // vars correctly. |
| 1616 | PayloadState payload_state2; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1617 | EXPECT_TRUE(payload_state2.Initialize(&fake_system_state)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1618 | EXPECT_EQ(1, payload_state2.GetP2PNumAttempts()); |
| 1619 | EXPECT_EQ(time, payload_state2.GetP2PFirstAttemptTimestamp()); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | TEST(PayloadStateTest, P2PStateVarsAreClearedOnNewResponse) { |
| 1623 | OmahaResponse response; |
| 1624 | PayloadState payload_state; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1625 | FakeSystemState fake_system_state; |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1626 | FakeClock fake_clock; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1627 | FakePrefs fake_prefs; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1628 | fake_system_state.set_clock(&fake_clock); |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 1629 | fake_system_state.set_prefs(&fake_prefs); |
| 1630 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1631 | EXPECT_TRUE(payload_state.Initialize(&fake_system_state)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1632 | SetupPayloadStateWith2Urls("Hash8593", true, &payload_state, &response); |
| 1633 | |
| 1634 | // Set the clock to something known. |
| 1635 | Time time = Time::FromInternalValue(12345); |
| 1636 | fake_clock.SetWallclockTime(time); |
| 1637 | |
| 1638 | // New p2p attempt - as a side-effect this will update the p2p state vars. |
| 1639 | payload_state.P2PNewAttempt(); |
| 1640 | EXPECT_EQ(1, payload_state.GetP2PNumAttempts()); |
| 1641 | EXPECT_EQ(time, payload_state.GetP2PFirstAttemptTimestamp()); |
| 1642 | |
| 1643 | // Set a new response... |
| 1644 | SetupPayloadStateWith2Urls("Hash9904", true, &payload_state, &response); |
| 1645 | |
| 1646 | // ... and check that it clears the P2P state vars. |
| 1647 | Time null_time = Time(); |
| 1648 | EXPECT_EQ(0, payload_state.GetP2PNumAttempts()); |
| 1649 | EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp()); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1652 | } // namespace chromeos_update_engine |