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