Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <glib.h> |
| 6 | |
| 7 | #include "gtest/gtest.h" |
| 8 | #include "gmock/gmock.h" |
| 9 | #include "update_engine/omaha_request_action.h" |
| 10 | #include "update_engine/payload_state.h" |
| 11 | #include "update_engine/prefs_mock.h" |
| 12 | #include "update_engine/test_utils.h" |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::string; |
| 16 | using testing::_; |
| 17 | using testing::NiceMock; |
| 18 | using testing::SetArgumentPointee; |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | class PayloadStateTest : public ::testing::Test { }; |
| 23 | |
| 24 | TEST(PayloadStateTest, SetResponseWorksWithEmptyResponse) { |
| 25 | OmahaResponse response; |
| 26 | NiceMock<PrefsMock> prefs; |
| 27 | EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)); |
| 28 | PayloadState payload_state; |
| 29 | EXPECT_TRUE(payload_state.Initialize(&prefs)); |
| 30 | payload_state.SetResponse(response); |
| 31 | string stored_response = payload_state.GetResponse(); |
| 32 | string expected_response = "NumURLs = 0\n" |
| 33 | "Payload Size = 0\n" |
| 34 | "Payload Sha256 Hash = \n" |
| 35 | "Metadata Size = 0\n" |
| 36 | "Metadata Signature = \n"; |
| 37 | EXPECT_EQ(expected_response, stored_response); |
| 38 | EXPECT_EQ(0, payload_state.GetUrlIndex()); |
| 39 | } |
| 40 | |
| 41 | TEST(PayloadStateTest, SetResponseWorksWithSingleUrl) { |
| 42 | OmahaResponse response; |
| 43 | response.payload_urls.push_back("http://single.url.test"); |
| 44 | response.size = 123456789; |
| 45 | response.hash = "hash"; |
| 46 | response.metadata_size = 58123; |
| 47 | response.metadata_signature = "msign"; |
| 48 | NiceMock<PrefsMock> prefs; |
| 49 | EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)); |
| 50 | PayloadState payload_state; |
| 51 | EXPECT_TRUE(payload_state.Initialize(&prefs)); |
| 52 | payload_state.SetResponse(response); |
| 53 | string stored_response = payload_state.GetResponse(); |
| 54 | string expected_response = "NumURLs = 1\n" |
| 55 | "Url0 = http://single.url.test\n" |
| 56 | "Payload Size = 123456789\n" |
| 57 | "Payload Sha256 Hash = hash\n" |
| 58 | "Metadata Size = 58123\n" |
| 59 | "Metadata Signature = msign\n"; |
| 60 | EXPECT_EQ(expected_response, stored_response); |
| 61 | EXPECT_EQ(0, payload_state.GetUrlIndex()); |
| 62 | } |
| 63 | |
| 64 | TEST(PayloadStateTest, SetResponseWorksWithMultipleUrls) { |
| 65 | OmahaResponse response; |
| 66 | response.payload_urls.push_back("http://multiple.url.test"); |
| 67 | response.payload_urls.push_back("https://multiple.url.test"); |
| 68 | response.size = 523456789; |
| 69 | response.hash = "rhash"; |
| 70 | response.metadata_size = 558123; |
| 71 | response.metadata_signature = "metasign"; |
| 72 | NiceMock<PrefsMock> prefs; |
| 73 | EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)); |
| 74 | PayloadState payload_state; |
| 75 | EXPECT_TRUE(payload_state.Initialize(&prefs)); |
| 76 | payload_state.SetResponse(response); |
| 77 | string stored_response = payload_state.GetResponse(); |
| 78 | string expected_response = "NumURLs = 2\n" |
| 79 | "Url0 = http://multiple.url.test\n" |
| 80 | "Url1 = https://multiple.url.test\n" |
| 81 | "Payload Size = 523456789\n" |
| 82 | "Payload Sha256 Hash = rhash\n" |
| 83 | "Metadata Size = 558123\n" |
| 84 | "Metadata Signature = metasign\n"; |
| 85 | EXPECT_EQ(expected_response, stored_response); |
| 86 | EXPECT_EQ(0, payload_state.GetUrlIndex()); |
| 87 | } |
| 88 | |
| 89 | TEST(PayloadStateTest, CanAdvanceUrlIndexCorrectly) { |
| 90 | OmahaResponse response; |
| 91 | // For a variation, don't set the metadata signature, as it's optional. |
| 92 | response.payload_urls.push_back("http://set.url.index"); |
| 93 | response.payload_urls.push_back("https://set.url.index"); |
| 94 | response.size = 523456789; |
| 95 | response.hash = "rhash"; |
| 96 | |
| 97 | NiceMock<PrefsMock> prefs; |
| 98 | // Should be called 4 times, one for each UpdateFailed call plus one |
| 99 | // for SetResponse. |
| 100 | EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, _)).Times(4); |
| 101 | PayloadState payload_state; |
| 102 | EXPECT_TRUE(payload_state.Initialize(&prefs)); |
| 103 | payload_state.SetResponse(response); |
| 104 | string stored_response = payload_state.GetResponse(); |
| 105 | string expected_response = "NumURLs = 2\n" |
| 106 | "Url0 = http://set.url.index\n" |
| 107 | "Url1 = https://set.url.index\n" |
| 108 | "Payload Size = 523456789\n" |
| 109 | "Payload Sha256 Hash = rhash\n" |
| 110 | "Metadata Size = 0\n" |
| 111 | "Metadata Signature = \n"; |
| 112 | EXPECT_EQ(expected_response, stored_response); |
| 113 | EXPECT_EQ(0, payload_state.GetUrlIndex()); |
| 114 | |
| 115 | // Verify that on the first error, the URL index advances to 1. |
| 116 | ActionExitCode error = kActionCodeDownloadMetadataSignatureMismatch; |
| 117 | payload_state.UpdateFailed(error); |
| 118 | EXPECT_EQ(1, payload_state.GetUrlIndex()); |
| 119 | |
| 120 | // Verify that on the next error, the URL index wraps around to 0. |
| 121 | payload_state.UpdateFailed(error); |
| 122 | EXPECT_EQ(0, payload_state.GetUrlIndex()); |
| 123 | |
| 124 | // Verify that on the next error, it again advances to 1. |
| 125 | payload_state.UpdateFailed(error); |
| 126 | EXPECT_EQ(1, payload_state.GetUrlIndex()); |
| 127 | } |
| 128 | |
| 129 | TEST(PayloadStateTest, NewResponseResetsPayloadState) { |
| 130 | OmahaResponse response; |
| 131 | response.payload_urls.push_back("http://reset.url.index"); |
| 132 | response.payload_urls.push_back("https://reset.url.index"); |
| 133 | response.size = 523456789; |
| 134 | response.hash = "rhash"; |
| 135 | response.metadata_size = 558123; |
| 136 | response.metadata_signature = "metasign"; |
| 137 | NiceMock<PrefsMock> prefs; |
| 138 | EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 0)).Times(2); |
| 139 | EXPECT_CALL(prefs, SetInt64(kPrefsCurrentUrlIndex, 1)).Times(1); |
| 140 | PayloadState payload_state; |
| 141 | EXPECT_TRUE(payload_state.Initialize(&prefs)); |
| 142 | payload_state.SetResponse(response); |
| 143 | string stored_response = payload_state.GetResponse(); |
| 144 | string expected_response = "NumURLs = 2\n" |
| 145 | "Url0 = http://reset.url.index\n" |
| 146 | "Url1 = https://reset.url.index\n" |
| 147 | "Payload Size = 523456789\n" |
| 148 | "Payload Sha256 Hash = rhash\n" |
| 149 | "Metadata Size = 558123\n" |
| 150 | "Metadata Signature = metasign\n"; |
| 151 | EXPECT_EQ(expected_response, stored_response); |
| 152 | |
| 153 | // Advance the URL index to 1 by faking an error. |
| 154 | ActionExitCode error = kActionCodeDownloadMetadataSignatureMismatch; |
| 155 | payload_state.UpdateFailed(error); |
| 156 | EXPECT_EQ(1, payload_state.GetUrlIndex()); |
| 157 | |
| 158 | // Now, slightly change the response and set it again. |
| 159 | response.hash = "newhash"; |
| 160 | payload_state.SetResponse(response); |
| 161 | stored_response = payload_state.GetResponse(); |
| 162 | expected_response = "NumURLs = 2\n" |
| 163 | "Url0 = http://reset.url.index\n" |
| 164 | "Url1 = https://reset.url.index\n" |
| 165 | "Payload Size = 523456789\n" |
| 166 | "Payload Sha256 Hash = newhash\n" |
| 167 | "Metadata Size = 558123\n" |
| 168 | "Metadata Signature = metasign\n"; |
| 169 | // Verify the new response. |
| 170 | EXPECT_EQ(expected_response, stored_response); |
| 171 | |
| 172 | // Make sure the url index was reset to 0 because of the new response. |
| 173 | EXPECT_EQ(0, payload_state.GetUrlIndex()); |
| 174 | } |
| 175 | |
| 176 | } |