| 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 | // | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 17 | #include "update_engine/omaha_request_action.h" | 
|  | 18 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 21 | #include <string> | 
|  | 22 | #include <vector> | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 23 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 24 | #include <base/bind.h> | 
| Alex Deymo | 110e030 | 2015-10-19 20:35:21 -0700 | [diff] [blame] | 25 | #include <base/files/file_util.h> | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 26 | #include <base/strings/string_number_conversions.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 27 | #include <base/strings/string_util.h> | 
|  | 28 | #include <base/strings/stringprintf.h> | 
|  | 29 | #include <base/time/time.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 30 | #include <brillo/bind_lambda.h> | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 31 | #include <brillo/make_unique_ptr.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 32 | #include <brillo/message_loops/fake_message_loop.h> | 
|  | 33 | #include <brillo/message_loops/message_loop.h> | 
|  | 34 | #include <brillo/message_loops/message_loop_utils.h> | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 35 | #include <gtest/gtest.h> | 
| Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 36 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | #include "update_engine/common/action_pipe.h" | 
|  | 38 | #include "update_engine/common/constants.h" | 
|  | 39 | #include "update_engine/common/fake_prefs.h" | 
|  | 40 | #include "update_engine/common/hash_calculator.h" | 
|  | 41 | #include "update_engine/common/mock_http_fetcher.h" | 
|  | 42 | #include "update_engine/common/platform_constants.h" | 
|  | 43 | #include "update_engine/common/prefs.h" | 
|  | 44 | #include "update_engine/common/test_utils.h" | 
|  | 45 | #include "update_engine/common/utils.h" | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 46 | #include "update_engine/fake_system_state.h" | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 47 | #include "update_engine/metrics.h" | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 48 | #include "update_engine/mock_connection_manager.h" | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 49 | #include "update_engine/mock_payload_state.h" | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 50 | #include "update_engine/omaha_request_params.h" | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 51 |  | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 52 | using base::Time; | 
|  | 53 | using base::TimeDelta; | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 54 | using chromeos_update_engine::test_utils::System; | 
|  | 55 | using chromeos_update_engine::test_utils::WriteFileString; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 56 | using std::string; | 
|  | 57 | using std::vector; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 58 | using testing::AllOf; | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 59 | using testing::AnyNumber; | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 60 | using testing::DoAll; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 61 | using testing::Ge; | 
|  | 62 | using testing::Le; | 
| Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 63 | using testing::NiceMock; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 64 | using testing::Return; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 65 | using testing::ReturnPointee; | 
|  | 66 | using testing::SaveArg; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 67 | using testing::SetArgumentPointee; | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 68 | using testing::_; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 69 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 70 | namespace { | 
|  | 71 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 72 | const char kTestAppId[] = "test-app-id"; | 
|  | 73 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 74 | // This is a helper struct to allow unit tests build an update response with the | 
|  | 75 | // values they care about. | 
|  | 76 | struct FakeUpdateResponse { | 
|  | 77 | string GetNoUpdateResponse() const { | 
|  | 78 | string entity_str; | 
|  | 79 | if (include_entity) | 
|  | 80 | entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>"; | 
| Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 81 | return | 
|  | 82 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | 
|  | 83 | entity_str + "<response protocol=\"3.0\">" | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 84 | "<daystart elapsed_seconds=\"100\"/>" | 
| Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 85 | "<app appid=\"" + app_id + "\" " + | 
|  | 86 | (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" + | 
|  | 87 | cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") + | 
|  | 88 | " status=\"ok\">" | 
|  | 89 | "<ping status=\"ok\"/>" | 
|  | 90 | "<updatecheck status=\"noupdate\"/></app></response>"; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | string GetUpdateResponse() const { | 
|  | 94 | return | 
|  | 95 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " | 
|  | 96 | "protocol=\"3.0\">" | 
|  | 97 | "<daystart elapsed_seconds=\"100\"" + | 
|  | 98 | (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\"")) | 
|  | 99 | + "/>" | 
|  | 100 | "<app appid=\"" + app_id + "\" " + | 
|  | 101 | (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" + | 
|  | 102 | cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") + | 
|  | 103 | " status=\"ok\">" | 
|  | 104 | "<ping status=\"ok\"/><updatecheck status=\"ok\">" | 
|  | 105 | "<urls><url codebase=\"" + codebase + "\"/></urls>" | 
|  | 106 | "<manifest version=\"" + version + "\">" | 
|  | 107 | "<packages><package hash=\"not-used\" name=\"" + filename +  "\" " | 
|  | 108 | "size=\"" + base::Int64ToString(size) + "\"/></packages>" | 
|  | 109 | "<actions><action event=\"postinstall\" " | 
|  | 110 | "ChromeOSVersion=\"" + version + "\" " | 
|  | 111 | "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " | 
|  | 112 | "IsDelta=\"true\" " | 
|  | 113 | "IsDeltaPayload=\"true\" " | 
|  | 114 | "MaxDaysToScatter=\"" + max_days_to_scatter + "\" " | 
|  | 115 | "sha256=\"" + hash + "\" " | 
|  | 116 | "needsadmin=\"" + needsadmin + "\" " + | 
|  | 117 | (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) + | 
|  | 118 | (disable_p2p_for_downloading ? | 
|  | 119 | "DisableP2PForDownloading=\"true\" " : "") + | 
|  | 120 | (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") + | 
|  | 121 | "/></actions></manifest></updatecheck></app></response>"; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | // Return the payload URL, which is split in two fields in the XML response. | 
|  | 125 | string GetPayloadUrl() { | 
|  | 126 | return codebase + filename; | 
|  | 127 | } | 
|  | 128 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 129 | string app_id = kTestAppId; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 130 | string version = "1.2.3.4"; | 
|  | 131 | string more_info_url = "http://more/info"; | 
|  | 132 | string prompt = "true"; | 
|  | 133 | string codebase = "http://code/base/"; | 
|  | 134 | string filename = "file.signed"; | 
|  | 135 | string hash = "HASH1234="; | 
|  | 136 | string needsadmin = "false"; | 
|  | 137 | int64_t size = 123; | 
|  | 138 | string deadline = ""; | 
|  | 139 | string max_days_to_scatter = "7"; | 
|  | 140 | string elapsed_days = "42"; | 
|  | 141 |  | 
|  | 142 | // P2P setting defaults to allowed. | 
|  | 143 | bool disable_p2p_for_downloading = false; | 
|  | 144 | bool disable_p2p_for_sharing = false; | 
|  | 145 |  | 
|  | 146 | // Omaha cohorts settings. | 
|  | 147 | bool include_cohorts = false; | 
|  | 148 | string cohort = ""; | 
|  | 149 | string cohorthint = ""; | 
|  | 150 | string cohortname = ""; | 
|  | 151 |  | 
|  | 152 | // Whether to include the CrOS <!ENTITY> in the XML response. | 
|  | 153 | bool include_entity = false; | 
|  | 154 | }; | 
|  | 155 |  | 
|  | 156 | }  // namespace | 
|  | 157 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 158 | namespace chromeos_update_engine { | 
|  | 159 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 160 | class OmahaRequestActionTest : public ::testing::Test { | 
|  | 161 | protected: | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 162 | void SetUp() override { | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 163 | fake_system_state_.set_request_params(&request_params_); | 
|  | 164 | fake_system_state_.set_prefs(&fake_prefs_); | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | // Returns true iff an output response was obtained from the | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 168 | // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 169 | // is used. |payload_state| may be null, in which case a local mock is used. | 
|  | 170 | // |p2p_manager| may be null, in which case a local mock is used. | 
|  | 171 | // |connection_manager| may be null, in which case a local mock is used. | 
|  | 172 | // out_response may be null. If |fail_http_response_code| is non-negative, | 
|  | 173 | // the transfer will fail with that code. |ping_only| is passed through to the | 
|  | 174 | // OmahaRequestAction constructor. out_post_data may be null; if non-null, the | 
|  | 175 | // post-data received by the mock HttpFetcher is returned. | 
|  | 176 | // | 
|  | 177 | // The |expected_check_result|, |expected_check_reaction| and | 
|  | 178 | // |expected_error_code| parameters are for checking expectations | 
|  | 179 | // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError} | 
|  | 180 | // UMA statistics. Use the appropriate ::kUnset value to specify that | 
|  | 181 | // the given metric should not be reported. | 
|  | 182 | bool TestUpdateCheck(OmahaRequestParams* request_params, | 
|  | 183 | const string& http_response, | 
|  | 184 | int fail_http_response_code, | 
|  | 185 | bool ping_only, | 
|  | 186 | ErrorCode expected_code, | 
|  | 187 | metrics::CheckResult expected_check_result, | 
|  | 188 | metrics::CheckReaction expected_check_reaction, | 
|  | 189 | metrics::DownloadErrorCode expected_download_error_code, | 
|  | 190 | OmahaResponse* out_response, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 191 | brillo::Blob* out_post_data); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 192 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 193 | // Runs and checks a ping test. |ping_only| indicates whether it should send | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 194 | // only a ping or also an updatecheck. | 
|  | 195 | void PingTest(bool ping_only); | 
|  | 196 |  | 
|  | 197 | // InstallDate test helper function. | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 198 | bool InstallDateParseHelper(const string &elapsed_days, | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 199 | OmahaResponse *response); | 
|  | 200 |  | 
|  | 201 | // P2P test helper function. | 
|  | 202 | void P2PTest( | 
|  | 203 | bool initial_allow_p2p_for_downloading, | 
|  | 204 | bool initial_allow_p2p_for_sharing, | 
|  | 205 | bool omaha_disable_p2p_for_downloading, | 
|  | 206 | bool omaha_disable_p2p_for_sharing, | 
|  | 207 | bool payload_state_allow_p2p_attempt, | 
|  | 208 | bool expect_p2p_client_lookup, | 
|  | 209 | const string& p2p_client_result_url, | 
|  | 210 | bool expected_allow_p2p_for_downloading, | 
|  | 211 | bool expected_allow_p2p_for_sharing, | 
|  | 212 | const string& expected_p2p_url); | 
|  | 213 |  | 
|  | 214 | FakeSystemState fake_system_state_; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 215 | FakeUpdateResponse fake_update_response_; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | // By default, all tests use these objects unless they replace them in the | 
|  | 218 | // fake_system_state_. | 
|  | 219 | OmahaRequestParams request_params_ = OmahaRequestParams{ | 
|  | 220 | &fake_system_state_, | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 221 | constants::kOmahaPlatformName, | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 222 | OmahaRequestParams::kOsVersion, | 
|  | 223 | "service_pack", | 
|  | 224 | "x86-generic", | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 225 | kTestAppId, | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 226 | "0.1.0.0", | 
|  | 227 | "en-US", | 
|  | 228 | "unittest", | 
|  | 229 | "OEM MODEL 09235 7471", | 
|  | 230 | "ChromeOSFirmware.1.0", | 
|  | 231 | "0X0A1", | 
|  | 232 | false,   // delta okay | 
|  | 233 | false,   // interactive | 
|  | 234 | "http://url", | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 235 | ""};     // target_version_prefix | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 236 |  | 
|  | 237 | FakePrefs fake_prefs_; | 
|  | 238 | }; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 239 |  | 
|  | 240 | namespace { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 241 | class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { | 
|  | 242 | public: | 
|  | 243 | OmahaRequestActionTestProcessorDelegate() | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 244 | : expected_code_(ErrorCode::kSuccess) {} | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 245 | ~OmahaRequestActionTestProcessorDelegate() override { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 246 | } | 
| Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 247 | void ProcessingDone(const ActionProcessor* processor, | 
|  | 248 | ErrorCode code) override { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 249 | brillo::MessageLoop::current()->BreakLoop(); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
| Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 252 | void ActionCompleted(ActionProcessor* processor, | 
|  | 253 | AbstractAction* action, | 
|  | 254 | ErrorCode code) override { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 255 | // make sure actions always succeed | 
|  | 256 | if (action->Type() == OmahaRequestAction::StaticType()) | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 257 | EXPECT_EQ(expected_code_, code); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 258 | else | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 259 | EXPECT_EQ(ErrorCode::kSuccess, code); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 260 | } | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 261 | ErrorCode expected_code_; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 262 | }; | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 263 | }  // namespace | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 264 |  | 
|  | 265 | class OutputObjectCollectorAction; | 
|  | 266 |  | 
|  | 267 | template<> | 
|  | 268 | class ActionTraits<OutputObjectCollectorAction> { | 
|  | 269 | public: | 
|  | 270 | // Does not take an object for input | 
|  | 271 | typedef OmahaResponse InputObjectType; | 
|  | 272 | // On success, puts the output path on output | 
|  | 273 | typedef NoneType OutputObjectType; | 
|  | 274 | }; | 
|  | 275 |  | 
|  | 276 | class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> { | 
|  | 277 | public: | 
|  | 278 | OutputObjectCollectorAction() : has_input_object_(false) {} | 
|  | 279 | void PerformAction() { | 
|  | 280 | // copy input object | 
|  | 281 | has_input_object_ = HasInputObject(); | 
|  | 282 | if (has_input_object_) | 
|  | 283 | omaha_response_ = GetInputObject(); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 284 | processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 285 | } | 
|  | 286 | // Should never be called | 
|  | 287 | void TerminateProcessing() { | 
|  | 288 | CHECK(false); | 
|  | 289 | } | 
|  | 290 | // Debugging/logging | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 291 | static string StaticType() { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 292 | return "OutputObjectCollectorAction"; | 
|  | 293 | } | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 294 | string Type() const { return StaticType(); } | 
| Alex Vakulenko | 0057daa | 2016-01-23 16:22:50 -0800 | [diff] [blame] | 295 | using InputObjectType = | 
|  | 296 | ActionTraits<OutputObjectCollectorAction>::InputObjectType; | 
|  | 297 | using OutputObjectType = | 
|  | 298 | ActionTraits<OutputObjectCollectorAction>::OutputObjectType; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 299 | bool has_input_object_; | 
|  | 300 | OmahaResponse omaha_response_; | 
|  | 301 | }; | 
|  | 302 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 303 | bool OmahaRequestActionTest::TestUpdateCheck( | 
|  | 304 | OmahaRequestParams* request_params, | 
|  | 305 | const string& http_response, | 
|  | 306 | int fail_http_response_code, | 
|  | 307 | bool ping_only, | 
|  | 308 | ErrorCode expected_code, | 
|  | 309 | metrics::CheckResult expected_check_result, | 
|  | 310 | metrics::CheckReaction expected_check_reaction, | 
|  | 311 | metrics::DownloadErrorCode expected_download_error_code, | 
|  | 312 | OmahaResponse* out_response, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 313 | brillo::Blob* out_post_data) { | 
|  | 314 | brillo::FakeMessageLoop loop(nullptr); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 315 | loop.SetAsCurrent(); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 316 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), | 
| Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 317 | http_response.size(), | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 318 | nullptr); | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 319 | if (fail_http_response_code >= 0) { | 
|  | 320 | fetcher->FailTransfer(fail_http_response_code); | 
|  | 321 | } | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 322 | if (request_params) | 
|  | 323 | fake_system_state_.set_request_params(request_params); | 
|  | 324 | OmahaRequestAction action(&fake_system_state_, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 325 | nullptr, | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 326 | brillo::make_unique_ptr(fetcher), | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 327 | ping_only); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 328 | OmahaRequestActionTestProcessorDelegate delegate; | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 329 | delegate.expected_code_ = expected_code; | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 330 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 331 | ActionProcessor processor; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 332 | processor.set_delegate(&delegate); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 333 | processor.EnqueueAction(&action); | 
|  | 334 |  | 
|  | 335 | OutputObjectCollectorAction collector_action; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 336 | BondActions(&action, &collector_action); | 
|  | 337 | processor.EnqueueAction(&collector_action); | 
|  | 338 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 339 | EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _)) | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 340 | .Times(AnyNumber()); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 341 | EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 342 | SendEnumToUMA(metrics::kMetricCheckResult, | 
|  | 343 | static_cast<int>(expected_check_result), | 
|  | 344 | static_cast<int>(metrics::CheckResult::kNumConstants) - 1)) | 
|  | 345 | .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 346 | EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 347 | SendEnumToUMA(metrics::kMetricCheckReaction, | 
|  | 348 | static_cast<int>(expected_check_reaction), | 
|  | 349 | static_cast<int>(metrics::CheckReaction::kNumConstants) - 1)) | 
|  | 350 | .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 351 | EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), | 
| David Zeuthen | c0dd021 | 2014-04-04 14:49:49 -0700 | [diff] [blame] | 352 | SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode, | 
|  | 353 | static_cast<int>(expected_download_error_code))) | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 354 | .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset | 
|  | 355 | ? 0 : 1); | 
|  | 356 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 357 | loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); })); | 
|  | 358 | LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks(); | 
|  | 359 | loop.Run(); | 
|  | 360 | LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks(); | 
|  | 361 | EXPECT_FALSE(loop.PendingTasks()); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 362 | if (collector_action.has_input_object_ && out_response) | 
|  | 363 | *out_response = collector_action.omaha_response_; | 
|  | 364 | if (out_post_data) | 
|  | 365 | *out_post_data = fetcher->post_data(); | 
|  | 366 | return collector_action.has_input_object_; | 
|  | 367 | } | 
|  | 368 |  | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 369 | // Tests Event requests -- they should always succeed. |out_post_data| | 
|  | 370 | // may be null; if non-null, the post-data received by the mock | 
|  | 371 | // HttpFetcher is returned. | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 372 | void TestEvent(OmahaRequestParams params, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 373 | OmahaEvent* event, | 
|  | 374 | const string& http_response, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 375 | brillo::Blob* out_post_data) { | 
|  | 376 | brillo::FakeMessageLoop loop(nullptr); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 377 | loop.SetAsCurrent(); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 378 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), | 
| Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 379 | http_response.size(), | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 380 | nullptr); | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 381 | FakeSystemState fake_system_state; | 
|  | 382 | fake_system_state.set_request_params(¶ms); | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 383 | OmahaRequestAction action(&fake_system_state, | 
|  | 384 | event, | 
|  | 385 | brillo::make_unique_ptr(fetcher), | 
|  | 386 | false); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 387 | OmahaRequestActionTestProcessorDelegate delegate; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 388 | ActionProcessor processor; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 389 | processor.set_delegate(&delegate); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 390 | processor.EnqueueAction(&action); | 
|  | 391 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 392 | loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); })); | 
|  | 393 | loop.Run(); | 
|  | 394 |  | 
|  | 395 | // This test should schedule a callback to notify the crash reporter if | 
|  | 396 | // the passed event is an error. | 
|  | 397 | EXPECT_EQ(event->result == OmahaEvent::kResultError, loop.PendingTasks()); | 
|  | 398 |  | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 399 | if (out_post_data) | 
|  | 400 | *out_post_data = fetcher->post_data(); | 
|  | 401 | } | 
|  | 402 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 403 | TEST_F(OmahaRequestActionTest, RejectEntities) { | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 404 | OmahaResponse response; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 405 | fake_update_response_.include_entity = true; | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 406 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 407 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 408 | fake_update_response_.GetNoUpdateResponse(), | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 409 | -1, | 
|  | 410 | false,  // ping_only | 
|  | 411 | ErrorCode::kOmahaRequestXMLHasEntityDecl, | 
|  | 412 | metrics::CheckResult::kParsingError, | 
|  | 413 | metrics::CheckReaction::kUnset, | 
|  | 414 | metrics::DownloadErrorCode::kUnset, | 
|  | 415 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 416 | nullptr)); | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 417 | EXPECT_FALSE(response.update_exists); | 
|  | 418 | } | 
|  | 419 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 420 | TEST_F(OmahaRequestActionTest, NoUpdateTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 421 | OmahaResponse response; | 
|  | 422 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 423 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 424 | fake_update_response_.GetNoUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 425 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 426 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 427 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 428 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 429 | metrics::CheckReaction::kUnset, | 
|  | 430 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 431 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 432 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 433 | EXPECT_FALSE(response.update_exists); | 
|  | 434 | } | 
|  | 435 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 436 | // Test that all the values in the response are parsed in a normal update | 
|  | 437 | // response. | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 438 | TEST_F(OmahaRequestActionTest, ValidUpdateTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 439 | OmahaResponse response; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 440 | fake_update_response_.deadline = "20101020"; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 441 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 442 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 443 | fake_update_response_.GetUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 444 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 445 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 446 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 447 | metrics::CheckResult::kUpdateAvailable, | 
|  | 448 | metrics::CheckReaction::kUpdating, | 
|  | 449 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 450 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 451 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 452 | EXPECT_TRUE(response.update_exists); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 453 | EXPECT_TRUE(response.update_exists); | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 454 | EXPECT_EQ(fake_update_response_.version, response.version); | 
|  | 455 | EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]); | 
|  | 456 | EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url); | 
|  | 457 | EXPECT_EQ(fake_update_response_.hash, response.hash); | 
|  | 458 | EXPECT_EQ(fake_update_response_.size, response.size); | 
|  | 459 | EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt); | 
|  | 460 | EXPECT_EQ(fake_update_response_.deadline, response.deadline); | 
|  | 461 | // Omaha cohort attribets are not set in the response, so they should not be | 
|  | 462 | // persisted. | 
|  | 463 | EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort)); | 
|  | 464 | EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint)); | 
|  | 465 | EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 468 | TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 469 | OmahaResponse response; | 
|  | 470 | // Set up a connection manager that doesn't allow a valid update over | 
|  | 471 | // the current ethernet connection. | 
| Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 472 | MockConnectionManager mock_cm; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 473 | fake_system_state_.set_connection_manager(&mock_cm); | 
|  | 474 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 475 | EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) | 
|  | 476 | .WillRepeatedly( | 
|  | 477 | DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet), | 
|  | 478 | SetArgumentPointee<1>(NetworkTethering::kUnknown), | 
|  | 479 | Return(true))); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 480 | EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _)) | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 481 | .WillRepeatedly(Return(false)); | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 482 |  | 
|  | 483 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 484 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 485 | fake_update_response_.GetUpdateResponse(), | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 486 | -1, | 
|  | 487 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 488 | ErrorCode::kOmahaUpdateIgnoredPerPolicy, | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 489 | metrics::CheckResult::kUpdateAvailable, | 
|  | 490 | metrics::CheckReaction::kIgnored, | 
|  | 491 | metrics::DownloadErrorCode::kUnset, | 
|  | 492 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 493 | nullptr)); | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 494 | EXPECT_FALSE(response.update_exists); | 
|  | 495 | } | 
|  | 496 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 497 | TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 498 | string rollback_version = "1234.0.0"; | 
|  | 499 | OmahaResponse response; | 
|  | 500 |  | 
|  | 501 | MockPayloadState mock_payload_state; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 502 | fake_system_state_.set_payload_state(&mock_payload_state); | 
|  | 503 |  | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 504 | EXPECT_CALL(mock_payload_state, GetRollbackVersion()) | 
|  | 505 | .WillRepeatedly(Return(rollback_version)); | 
|  | 506 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 507 | fake_update_response_.version = rollback_version; | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 508 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 509 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 510 | fake_update_response_.GetUpdateResponse(), | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 511 | -1, | 
|  | 512 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 513 | ErrorCode::kOmahaUpdateIgnoredPerPolicy, | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 514 | metrics::CheckResult::kUpdateAvailable, | 
|  | 515 | metrics::CheckReaction::kIgnored, | 
|  | 516 | metrics::DownloadErrorCode::kUnset, | 
|  | 517 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 518 | nullptr)); | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 519 | EXPECT_FALSE(response.update_exists); | 
|  | 520 | } | 
|  | 521 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 522 | TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 523 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 524 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 525 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 526 | params.set_update_check_count_wait_enabled(false); | 
|  | 527 | params.set_waiting_period(TimeDelta::FromDays(2)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 528 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 529 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 530 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 531 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 532 | -1, | 
|  | 533 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 534 | ErrorCode::kOmahaUpdateDeferredPerPolicy, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 535 | metrics::CheckResult::kUpdateAvailable, | 
|  | 536 | metrics::CheckReaction::kDeferring, | 
|  | 537 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 538 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 539 | nullptr)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 540 | EXPECT_FALSE(response.update_exists); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 541 |  | 
|  | 542 | // Verify if we are interactive check we don't defer. | 
|  | 543 | params.set_interactive(true); | 
|  | 544 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 545 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 546 | fake_update_response_.GetUpdateResponse(), | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 547 | -1, | 
|  | 548 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 549 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 550 | metrics::CheckResult::kUpdateAvailable, | 
|  | 551 | metrics::CheckReaction::kUpdating, | 
|  | 552 | metrics::DownloadErrorCode::kUnset, | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 553 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 554 | nullptr)); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 555 | EXPECT_TRUE(response.update_exists); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 556 | } | 
|  | 557 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 558 | TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 559 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 560 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 561 | params.set_wall_clock_based_wait_enabled(false); | 
|  | 562 | params.set_waiting_period(TimeDelta::FromDays(2)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 563 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 564 | params.set_update_check_count_wait_enabled(true); | 
|  | 565 | params.set_min_update_checks_needed(1); | 
|  | 566 | params.set_max_update_checks_allowed(8); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 567 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 568 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 569 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 570 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 571 | -1, | 
|  | 572 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 573 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 574 | metrics::CheckResult::kUpdateAvailable, | 
|  | 575 | metrics::CheckReaction::kUpdating, | 
|  | 576 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 577 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 578 | nullptr)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 579 | EXPECT_TRUE(response.update_exists); | 
|  | 580 | } | 
|  | 581 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 582 | TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 583 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 584 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 585 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 586 | params.set_waiting_period(TimeDelta::FromDays(2)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 587 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 588 | params.set_update_check_count_wait_enabled(true); | 
|  | 589 | params.set_min_update_checks_needed(1); | 
|  | 590 | params.set_max_update_checks_allowed(8); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 591 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 592 | fake_update_response_.max_days_to_scatter = "0"; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 593 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 594 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 595 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 596 | -1, | 
|  | 597 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 598 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 599 | metrics::CheckResult::kUpdateAvailable, | 
|  | 600 | metrics::CheckReaction::kUpdating, | 
|  | 601 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 602 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 603 | nullptr)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 604 | EXPECT_TRUE(response.update_exists); | 
|  | 605 | } | 
|  | 606 |  | 
|  | 607 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 608 | TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 609 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 610 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 611 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 612 | params.set_waiting_period(TimeDelta()); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 613 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 614 | params.set_update_check_count_wait_enabled(true); | 
|  | 615 | params.set_min_update_checks_needed(0); | 
|  | 616 | params.set_max_update_checks_allowed(0); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 617 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 618 | ASSERT_TRUE(TestUpdateCheck( | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 619 | ¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 620 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 621 | -1, | 
|  | 622 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 623 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 624 | metrics::CheckResult::kUpdateAvailable, | 
|  | 625 | metrics::CheckReaction::kUpdating, | 
|  | 626 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 627 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 628 | nullptr)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 629 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 630 | int64_t count; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 631 | ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count)); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 632 | ASSERT_EQ(count, 0); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 633 | EXPECT_TRUE(response.update_exists); | 
|  | 634 | } | 
|  | 635 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 636 | TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 637 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 638 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 639 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 640 | params.set_waiting_period(TimeDelta()); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 641 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 642 | params.set_update_check_count_wait_enabled(true); | 
|  | 643 | params.set_min_update_checks_needed(1); | 
|  | 644 | params.set_max_update_checks_allowed(8); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 645 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 646 | ASSERT_FALSE(TestUpdateCheck( | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 647 | ¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 648 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 649 | -1, | 
|  | 650 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 651 | ErrorCode::kOmahaUpdateDeferredPerPolicy, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 652 | metrics::CheckResult::kUpdateAvailable, | 
|  | 653 | metrics::CheckReaction::kDeferring, | 
|  | 654 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 655 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 656 | nullptr)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 657 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 658 | int64_t count; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 659 | ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count)); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 660 | ASSERT_GT(count, 0); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 661 | EXPECT_FALSE(response.update_exists); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 662 |  | 
|  | 663 | // Verify if we are interactive check we don't defer. | 
|  | 664 | params.set_interactive(true); | 
|  | 665 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 666 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 667 | fake_update_response_.GetUpdateResponse(), | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 668 | -1, | 
|  | 669 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 670 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 671 | metrics::CheckResult::kUpdateAvailable, | 
|  | 672 | metrics::CheckReaction::kUpdating, | 
|  | 673 | metrics::DownloadErrorCode::kUnset, | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 674 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 675 | nullptr)); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 676 | EXPECT_TRUE(response.update_exists); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 677 | } | 
|  | 678 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 679 | TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 680 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 681 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 682 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 683 | params.set_waiting_period(TimeDelta()); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 684 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 685 | params.set_update_check_count_wait_enabled(true); | 
|  | 686 | params.set_min_update_checks_needed(1); | 
|  | 687 | params.set_max_update_checks_allowed(8); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 688 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 689 | ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 690 |  | 
|  | 691 | ASSERT_FALSE(TestUpdateCheck( | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 692 | ¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 693 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 694 | -1, | 
|  | 695 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 696 | ErrorCode::kOmahaUpdateDeferredPerPolicy, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 697 | metrics::CheckResult::kUpdateAvailable, | 
|  | 698 | metrics::CheckReaction::kDeferring, | 
|  | 699 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 700 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 701 | nullptr)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 702 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 703 | int64_t count; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 704 | ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count)); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 705 | // count remains the same, as the decrementing happens in update_attempter | 
|  | 706 | // which this test doesn't exercise. | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 707 | ASSERT_EQ(count, 5); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 708 | EXPECT_FALSE(response.update_exists); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 709 |  | 
|  | 710 | // Verify if we are interactive check we don't defer. | 
|  | 711 | params.set_interactive(true); | 
|  | 712 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 713 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 714 | fake_update_response_.GetUpdateResponse(), | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 715 | -1, | 
|  | 716 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 717 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 718 | metrics::CheckResult::kUpdateAvailable, | 
|  | 719 | metrics::CheckReaction::kUpdating, | 
|  | 720 | metrics::DownloadErrorCode::kUnset, | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 721 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 722 | nullptr)); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 723 | EXPECT_TRUE(response.update_exists); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 724 | } | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 725 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 726 | TEST_F(OmahaRequestActionTest, CohortsArePersisted) { | 
|  | 727 | OmahaResponse response; | 
|  | 728 | OmahaRequestParams params = request_params_; | 
|  | 729 | fake_update_response_.include_cohorts = true; | 
|  | 730 | fake_update_response_.cohort = "s/154454/8479665"; | 
|  | 731 | fake_update_response_.cohorthint = "please-put-me-on-beta"; | 
|  | 732 | fake_update_response_.cohortname = "stable"; | 
|  | 733 |  | 
|  | 734 | ASSERT_TRUE(TestUpdateCheck(¶ms, | 
|  | 735 | fake_update_response_.GetUpdateResponse(), | 
|  | 736 | -1, | 
|  | 737 | false,  // ping_only | 
|  | 738 | ErrorCode::kSuccess, | 
|  | 739 | metrics::CheckResult::kUpdateAvailable, | 
|  | 740 | metrics::CheckReaction::kUpdating, | 
|  | 741 | metrics::DownloadErrorCode::kUnset, | 
|  | 742 | &response, | 
|  | 743 | nullptr)); | 
|  | 744 |  | 
|  | 745 | string value; | 
|  | 746 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); | 
|  | 747 | EXPECT_EQ(fake_update_response_.cohort, value); | 
|  | 748 |  | 
|  | 749 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); | 
|  | 750 | EXPECT_EQ(fake_update_response_.cohorthint, value); | 
|  | 751 |  | 
|  | 752 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); | 
|  | 753 | EXPECT_EQ(fake_update_response_.cohortname, value); | 
|  | 754 | } | 
|  | 755 |  | 
|  | 756 | TEST_F(OmahaRequestActionTest, CohortsAreUpdated) { | 
|  | 757 | OmahaResponse response; | 
|  | 758 | OmahaRequestParams params = request_params_; | 
|  | 759 | EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value")); | 
|  | 760 | EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint")); | 
|  | 761 | EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name")); | 
|  | 762 | fake_update_response_.include_cohorts = true; | 
|  | 763 | fake_update_response_.cohort = "s/154454/8479665"; | 
|  | 764 | fake_update_response_.cohorthint = "please-put-me-on-beta"; | 
|  | 765 | fake_update_response_.cohortname = ""; | 
|  | 766 |  | 
|  | 767 | ASSERT_TRUE(TestUpdateCheck(¶ms, | 
|  | 768 | fake_update_response_.GetUpdateResponse(), | 
|  | 769 | -1, | 
|  | 770 | false,  // ping_only | 
|  | 771 | ErrorCode::kSuccess, | 
|  | 772 | metrics::CheckResult::kUpdateAvailable, | 
|  | 773 | metrics::CheckReaction::kUpdating, | 
|  | 774 | metrics::DownloadErrorCode::kUnset, | 
|  | 775 | &response, | 
|  | 776 | nullptr)); | 
|  | 777 |  | 
|  | 778 | string value; | 
|  | 779 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); | 
|  | 780 | EXPECT_EQ(fake_update_response_.cohort, value); | 
|  | 781 |  | 
|  | 782 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); | 
|  | 783 | EXPECT_EQ(fake_update_response_.cohorthint, value); | 
|  | 784 |  | 
|  | 785 | EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) { | 
|  | 789 | OmahaResponse response; | 
|  | 790 | OmahaRequestParams params = request_params_; | 
|  | 791 | EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value")); | 
|  | 792 |  | 
|  | 793 | ASSERT_TRUE(TestUpdateCheck(¶ms, | 
|  | 794 | fake_update_response_.GetUpdateResponse(), | 
|  | 795 | -1, | 
|  | 796 | false,  // ping_only | 
|  | 797 | ErrorCode::kSuccess, | 
|  | 798 | metrics::CheckResult::kUpdateAvailable, | 
|  | 799 | metrics::CheckReaction::kUpdating, | 
|  | 800 | metrics::DownloadErrorCode::kUnset, | 
|  | 801 | &response, | 
|  | 802 | nullptr)); | 
|  | 803 |  | 
|  | 804 | string value; | 
|  | 805 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); | 
|  | 806 | EXPECT_EQ("old_value", value); | 
|  | 807 |  | 
|  | 808 | EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); | 
|  | 809 | EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); | 
|  | 810 | } | 
|  | 811 |  | 
| Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 812 | TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) { | 
|  | 813 | OmahaResponse response; | 
|  | 814 | OmahaRequestParams params = request_params_; | 
|  | 815 | fake_update_response_.include_cohorts = true; | 
|  | 816 | fake_update_response_.cohort = "s/154454/8479665"; | 
|  | 817 | fake_update_response_.cohorthint = "please-put-me-on-beta"; | 
|  | 818 | fake_update_response_.cohortname = "stable"; | 
|  | 819 |  | 
|  | 820 | ASSERT_TRUE(TestUpdateCheck(¶ms, | 
|  | 821 | fake_update_response_.GetNoUpdateResponse(), | 
|  | 822 | -1, | 
|  | 823 | false,  // ping_only | 
|  | 824 | ErrorCode::kSuccess, | 
|  | 825 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 826 | metrics::CheckReaction::kUnset, | 
|  | 827 | metrics::DownloadErrorCode::kUnset, | 
|  | 828 | &response, | 
|  | 829 | nullptr)); | 
|  | 830 |  | 
|  | 831 | string value; | 
|  | 832 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); | 
|  | 833 | EXPECT_EQ(fake_update_response_.cohort, value); | 
|  | 834 |  | 
|  | 835 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); | 
|  | 836 | EXPECT_EQ(fake_update_response_.cohorthint, value); | 
|  | 837 |  | 
|  | 838 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); | 
|  | 839 | EXPECT_EQ(fake_update_response_.cohortname, value); | 
|  | 840 | } | 
|  | 841 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 842 | TEST_F(OmahaRequestActionTest, NoOutputPipeTest) { | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 843 | const string http_response(fake_update_response_.GetNoUpdateResponse()); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 844 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 845 | brillo::FakeMessageLoop loop(nullptr); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 846 | loop.SetAsCurrent(); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 847 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 848 | OmahaRequestParams params = request_params_; | 
|  | 849 | fake_system_state_.set_request_params(¶ms); | 
|  | 850 | OmahaRequestAction action(&fake_system_state_, nullptr, | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 851 | brillo::make_unique_ptr( | 
|  | 852 | new MockHttpFetcher(http_response.data(), | 
|  | 853 | http_response.size(), | 
|  | 854 | nullptr)), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 855 | false); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 856 | OmahaRequestActionTestProcessorDelegate delegate; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 857 | ActionProcessor processor; | 
|  | 858 | processor.set_delegate(&delegate); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 859 | processor.EnqueueAction(&action); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 860 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 861 | loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); })); | 
|  | 862 | loop.Run(); | 
|  | 863 | EXPECT_FALSE(loop.PendingTasks()); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 864 | EXPECT_FALSE(processor.IsRunning()); | 
|  | 865 | } | 
|  | 866 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 867 | TEST_F(OmahaRequestActionTest, InvalidXmlTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 868 | OmahaResponse response; | 
|  | 869 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 870 | TestUpdateCheck(nullptr,  // request_params | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 871 | "invalid xml>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 872 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 873 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 874 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 875 | metrics::CheckResult::kParsingError, | 
|  | 876 | metrics::CheckReaction::kUnset, | 
|  | 877 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 878 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 879 | nullptr)); | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 880 | EXPECT_FALSE(response.update_exists); | 
|  | 881 | } | 
|  | 882 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 883 | TEST_F(OmahaRequestActionTest, EmptyResponseTest) { | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 884 | OmahaResponse response; | 
|  | 885 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 886 | TestUpdateCheck(nullptr,  // request_params | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 887 | "", | 
|  | 888 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 889 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 890 | ErrorCode::kOmahaRequestEmptyResponseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 891 | metrics::CheckResult::kParsingError, | 
|  | 892 | metrics::CheckReaction::kUnset, | 
|  | 893 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 894 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 895 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 896 | EXPECT_FALSE(response.update_exists); | 
|  | 897 | } | 
|  | 898 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 899 | TEST_F(OmahaRequestActionTest, MissingStatusTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 900 | OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 901 | ASSERT_FALSE(TestUpdateCheck( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 902 | nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 903 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" | 
|  | 904 | "<daystart elapsed_seconds=\"100\"/>" | 
|  | 905 | "<app appid=\"foo\" status=\"ok\">" | 
|  | 906 | "<ping status=\"ok\"/>" | 
|  | 907 | "<updatecheck/></app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 908 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 909 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 910 | ErrorCode::kOmahaResponseInvalid, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 911 | metrics::CheckResult::kParsingError, | 
|  | 912 | metrics::CheckReaction::kUnset, | 
|  | 913 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 914 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 915 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 916 | EXPECT_FALSE(response.update_exists); | 
|  | 917 | } | 
|  | 918 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 919 | TEST_F(OmahaRequestActionTest, InvalidStatusTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 920 | OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 921 | ASSERT_FALSE(TestUpdateCheck( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 922 | nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 923 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" | 
|  | 924 | "<daystart elapsed_seconds=\"100\"/>" | 
|  | 925 | "<app appid=\"foo\" status=\"ok\">" | 
|  | 926 | "<ping status=\"ok\"/>" | 
|  | 927 | "<updatecheck status=\"InvalidStatusTest\"/></app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 928 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 929 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 930 | ErrorCode::kOmahaResponseInvalid, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 931 | metrics::CheckResult::kParsingError, | 
|  | 932 | metrics::CheckReaction::kUnset, | 
|  | 933 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 934 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 935 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 936 | EXPECT_FALSE(response.update_exists); | 
|  | 937 | } | 
|  | 938 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 939 | TEST_F(OmahaRequestActionTest, MissingNodesetTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 940 | OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 941 | ASSERT_FALSE(TestUpdateCheck( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 942 | nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 943 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" | 
|  | 944 | "<daystart elapsed_seconds=\"100\"/>" | 
|  | 945 | "<app appid=\"foo\" status=\"ok\">" | 
|  | 946 | "<ping status=\"ok\"/>" | 
|  | 947 | "</app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 948 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 949 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 950 | ErrorCode::kOmahaResponseInvalid, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 951 | metrics::CheckResult::kParsingError, | 
|  | 952 | metrics::CheckReaction::kUnset, | 
|  | 953 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 954 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 955 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 956 | EXPECT_FALSE(response.update_exists); | 
|  | 957 | } | 
|  | 958 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 959 | TEST_F(OmahaRequestActionTest, MissingFieldTest) { | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 960 | string input_response = | 
|  | 961 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" | 
|  | 962 | "<daystart elapsed_seconds=\"100\"/>" | 
|  | 963 | "<app appid=\"xyz\" status=\"ok\">" | 
|  | 964 | "<updatecheck status=\"ok\">" | 
|  | 965 | "<urls><url codebase=\"http://missing/field/test/\"/></urls>" | 
| Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 966 | "<manifest version=\"10.2.3.4\">" | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 967 | "<packages><package hash=\"not-used\" name=\"f\" " | 
|  | 968 | "size=\"587\"/></packages>" | 
|  | 969 | "<actions><action event=\"postinstall\" " | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 970 | "ChromeOSVersion=\"10.2.3.4\" " | 
|  | 971 | "Prompt=\"false\" " | 
|  | 972 | "IsDelta=\"true\" " | 
| Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 973 | "IsDeltaPayload=\"false\" " | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 974 | "sha256=\"lkq34j5345\" " | 
|  | 975 | "needsadmin=\"true\" " | 
|  | 976 | "/></actions></manifest></updatecheck></app></response>"; | 
|  | 977 | LOG(INFO) << "Input Response = " << input_response; | 
|  | 978 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 979 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 980 | ASSERT_TRUE(TestUpdateCheck(nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 981 | input_response, | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 982 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 983 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 984 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 985 | metrics::CheckResult::kUpdateAvailable, | 
|  | 986 | metrics::CheckReaction::kUpdating, | 
|  | 987 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 988 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 989 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 990 | EXPECT_TRUE(response.update_exists); | 
| Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 991 | EXPECT_EQ("10.2.3.4", response.version); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 992 | EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 993 | EXPECT_EQ("", response.more_info_url); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 994 | EXPECT_EQ("lkq34j5345", response.hash); | 
|  | 995 | EXPECT_EQ(587, response.size); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 996 | EXPECT_FALSE(response.prompt); | 
| Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 997 | EXPECT_TRUE(response.deadline.empty()); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 998 | } | 
|  | 999 |  | 
|  | 1000 | namespace { | 
|  | 1001 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { | 
|  | 1002 | public: | 
|  | 1003 | void ProcessingStopped(const ActionProcessor* processor) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1004 | brillo::MessageLoop::current()->BreakLoop(); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1005 | } | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1006 | }; | 
|  | 1007 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1008 | void TerminateTransferTestStarter(ActionProcessor* processor) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1009 | processor->StartProcessing(); | 
|  | 1010 | CHECK(processor->IsRunning()); | 
|  | 1011 | processor->StopProcessing(); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1012 | } | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1013 | }  // namespace | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1014 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1015 | TEST_F(OmahaRequestActionTest, TerminateTransferTest) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1016 | brillo::FakeMessageLoop loop(nullptr); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1017 | loop.SetAsCurrent(); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1018 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1019 | string http_response("doesn't matter"); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1020 | OmahaRequestAction action(&fake_system_state_, nullptr, | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1021 | brillo::make_unique_ptr( | 
|  | 1022 | new MockHttpFetcher(http_response.data(), | 
|  | 1023 | http_response.size(), | 
|  | 1024 | nullptr)), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1025 | false); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1026 | TerminateEarlyTestProcessorDelegate delegate; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1027 | ActionProcessor processor; | 
|  | 1028 | processor.set_delegate(&delegate); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1029 | processor.EnqueueAction(&action); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1030 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1031 | loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor)); | 
|  | 1032 | loop.Run(); | 
|  | 1033 | EXPECT_FALSE(loop.PendingTasks()); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1034 | } | 
|  | 1035 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1036 | TEST_F(OmahaRequestActionTest, XmlEncodeTest) { | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 1037 | string output; | 
|  | 1038 | EXPECT_TRUE(XmlEncode("ab", &output)); | 
|  | 1039 | EXPECT_EQ("ab", output); | 
|  | 1040 | EXPECT_TRUE(XmlEncode("a<b", &output)); | 
|  | 1041 | EXPECT_EQ("a<b", output); | 
| Alex Deymo | cc45785 | 2015-06-18 18:35:50 -0700 | [diff] [blame] | 1042 | EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output)); | 
|  | 1043 | EXPECT_EQ("<&>"'\\", output); | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 1044 | EXPECT_TRUE(XmlEncode("<&>", &output)); | 
|  | 1045 | EXPECT_EQ("&lt;&amp;&gt;", output); | 
| Alex Deymo | cc45785 | 2015-06-18 18:35:50 -0700 | [diff] [blame] | 1046 | // Check that unterminated UTF-8 strings are handled properly. | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 1047 | EXPECT_FALSE(XmlEncode("\xc2", &output)); | 
|  | 1048 | // Fail with invalid ASCII-7 chars. | 
|  | 1049 | EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output)); | 
|  | 1050 | } | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1051 |  | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 1052 | TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) { | 
|  | 1053 | EXPECT_EQ("<&>", XmlEncodeWithDefault("<&>", "something else")); | 
|  | 1054 | EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>")); | 
|  | 1055 | } | 
|  | 1056 |  | 
|  | 1057 | TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1058 | brillo::Blob post_data; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1059 |  | 
|  | 1060 | // Make sure XML Encode is being called on the params | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1061 | OmahaRequestParams params(&fake_system_state_, | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 1062 | constants::kOmahaPlatformName, | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1063 | OmahaRequestParams::kOsVersion, | 
|  | 1064 | "testtheservice_pack>", | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1065 | "x86 generic<id", | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 1066 | kTestAppId, | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1067 | "0.1.0.0", | 
|  | 1068 | "en-US", | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1069 | "unittest_track<", | 
| Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1070 | "<OEM MODEL>", | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1071 | "ChromeOSFirmware.1.0", | 
|  | 1072 | "EC100", | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1073 | false,   // delta okay | 
|  | 1074 | false,   // interactive | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1075 | "http://url", | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1076 | "");     // target_version_prefix | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1077 | fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring"); | 
|  | 1078 | fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\"); | 
|  | 1079 | fake_prefs_.SetString(kPrefsOmahaCohortName, | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 1080 | base::JoinString( | 
|  | 1081 | vector<string>(100, "My spoon is too big."), " ")); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1082 | OmahaResponse response; | 
|  | 1083 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1084 | TestUpdateCheck(¶ms, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1085 | "invalid xml>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1086 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1087 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1088 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1089 | metrics::CheckResult::kParsingError, | 
|  | 1090 | metrics::CheckReaction::kUnset, | 
|  | 1091 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1092 | &response, | 
|  | 1093 | &post_data)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1094 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1095 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1096 | EXPECT_NE(string::npos, post_str.find("testtheservice_pack>")); | 
|  | 1097 | EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>")); | 
|  | 1098 | EXPECT_NE(string::npos, post_str.find("x86 generic<id")); | 
|  | 1099 | EXPECT_EQ(string::npos, post_str.find("x86 generic<id")); | 
|  | 1100 | EXPECT_NE(string::npos, post_str.find("unittest_track&lt;")); | 
|  | 1101 | EXPECT_EQ(string::npos, post_str.find("unittest_track<")); | 
|  | 1102 | EXPECT_NE(string::npos, post_str.find("<OEM MODEL>")); | 
|  | 1103 | EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>")); | 
|  | 1104 | EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\"")); | 
|  | 1105 | EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\"")); | 
|  | 1106 | EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&string\\\"")); | 
|  | 1107 | // Values from Prefs that are too big are removed from the XML instead of | 
|  | 1108 | // encoded. | 
|  | 1109 | EXPECT_EQ(string::npos, post_str.find("cohortname=")); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1110 | } | 
|  | 1111 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1112 | TEST_F(OmahaRequestActionTest, XmlDecodeTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1113 | OmahaResponse response; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1114 | fake_update_response_.deadline = "<20110101"; | 
|  | 1115 | fake_update_response_.more_info_url = "testthe<url"; | 
|  | 1116 | fake_update_response_.codebase = "testthe&codebase/"; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1117 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1118 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1119 | fake_update_response_.GetUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1120 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1121 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1122 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1123 | metrics::CheckResult::kUpdateAvailable, | 
|  | 1124 | metrics::CheckReaction::kUpdating, | 
|  | 1125 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1126 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1127 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1128 |  | 
|  | 1129 | EXPECT_EQ(response.more_info_url, "testthe<url"); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1130 | EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed"); | 
| Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 1131 | EXPECT_EQ(response.deadline, "<20110101"); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1132 | } | 
|  | 1133 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1134 | TEST_F(OmahaRequestActionTest, ParseIntTest) { | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1135 | OmahaResponse response; | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1136 | // overflows int32_t: | 
|  | 1137 | fake_update_response_.size = 123123123123123ll; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1138 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1139 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1140 | fake_update_response_.GetUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1141 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1142 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1143 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1144 | metrics::CheckResult::kUpdateAvailable, | 
|  | 1145 | metrics::CheckReaction::kUpdating, | 
|  | 1146 | metrics::DownloadErrorCode::kUnset, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1147 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1148 | nullptr)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1149 |  | 
|  | 1150 | EXPECT_EQ(response.size, 123123123123123ll); | 
|  | 1151 | } | 
|  | 1152 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1153 | TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1154 | brillo::Blob post_data; | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1155 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1156 | fake_system_state_.set_prefs(&prefs); | 
|  | 1157 |  | 
| Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1158 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) | 
|  | 1159 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); | 
| Alex Deymo | efb9d83 | 2015-11-02 18:39:02 -0800 | [diff] [blame] | 1160 | // An existing but empty previous version means that we didn't reboot to a new | 
|  | 1161 | // update, therefore, no need to update the previous version. | 
|  | 1162 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1163 | ASSERT_FALSE(TestUpdateCheck(nullptr,  // request_params | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1164 | "invalid xml>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1165 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1166 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1167 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1168 | metrics::CheckResult::kParsingError, | 
|  | 1169 | metrics::CheckReaction::kUnset, | 
|  | 1170 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1171 | nullptr,  // response | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1172 | &post_data)); | 
|  | 1173 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1174 | string post_str(post_data.begin(), post_data.end()); | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1175 | EXPECT_NE(post_str.find( | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1176 | "        <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n" | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1177 | "        <updatecheck targetversionprefix=\"\"></updatecheck>\n"), | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1178 | string::npos); | 
| Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1179 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), | 
|  | 1180 | string::npos); | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1181 | EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), | 
|  | 1182 | string::npos); | 
|  | 1183 | EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), | 
|  | 1184 | string::npos); | 
| Alex Deymo | efb9d83 | 2015-11-02 18:39:02 -0800 | [diff] [blame] | 1185 | // No <event> tag should be sent if we didn't reboot to an update. | 
|  | 1186 | EXPECT_EQ(post_str.find("<event"), string::npos); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1187 | } | 
|  | 1188 |  | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1189 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1190 | TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1191 | brillo::Blob post_data; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1192 | TestEvent(request_params_, | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1193 | new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted), | 
|  | 1194 | "invalid xml>", | 
|  | 1195 | &post_data); | 
|  | 1196 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1197 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1198 | string expected_event = base::StringPrintf( | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1199 | "        <event eventtype=\"%d\" eventresult=\"%d\"></event>\n", | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1200 | OmahaEvent::kTypeUpdateDownloadStarted, | 
|  | 1201 | OmahaEvent::kResultSuccess); | 
|  | 1202 | EXPECT_NE(post_str.find(expected_event), string::npos); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1203 | EXPECT_EQ(post_str.find("ping"), string::npos); | 
|  | 1204 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1205 | } | 
|  | 1206 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1207 | TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1208 | brillo::Blob post_data; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1209 | TestEvent(request_params_, | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1210 | new OmahaEvent(OmahaEvent::kTypeDownloadComplete, | 
|  | 1211 | OmahaEvent::kResultError, | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1212 | ErrorCode::kError), | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1213 | "invalid xml>", | 
|  | 1214 | &post_data); | 
|  | 1215 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1216 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1217 | string expected_event = base::StringPrintf( | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1218 | "        <event eventtype=\"%d\" eventresult=\"%d\" " | 
|  | 1219 | "errorcode=\"%d\"></event>\n", | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1220 | OmahaEvent::kTypeDownloadComplete, | 
|  | 1221 | OmahaEvent::kResultError, | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1222 | static_cast<int>(ErrorCode::kError)); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1223 | EXPECT_NE(post_str.find(expected_event), string::npos); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1224 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1225 | } | 
|  | 1226 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1227 | TEST_F(OmahaRequestActionTest, IsEventTest) { | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1228 | string http_response("doesn't matter"); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1229 | // Create a copy of the OmahaRequestParams to reuse it later. | 
|  | 1230 | OmahaRequestParams params = request_params_; | 
|  | 1231 | fake_system_state_.set_request_params(¶ms); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1232 | OmahaRequestAction update_check_action( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1233 | &fake_system_state_, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1234 | nullptr, | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1235 | brillo::make_unique_ptr( | 
|  | 1236 | new MockHttpFetcher(http_response.data(), | 
|  | 1237 | http_response.size(), | 
|  | 1238 | nullptr)), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1239 | false); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1240 | EXPECT_FALSE(update_check_action.IsEvent()); | 
|  | 1241 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1242 | params = request_params_; | 
|  | 1243 | fake_system_state_.set_request_params(¶ms); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1244 | OmahaRequestAction event_action( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1245 | &fake_system_state_, | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1246 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1247 | brillo::make_unique_ptr( | 
|  | 1248 | new MockHttpFetcher(http_response.data(), | 
|  | 1249 | http_response.size(), | 
|  | 1250 | nullptr)), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1251 | false); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1252 | EXPECT_TRUE(event_action.IsEvent()); | 
|  | 1253 | } | 
|  | 1254 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1255 | TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) { | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1256 | for (int i = 0; i < 2; i++) { | 
|  | 1257 | bool delta_okay = i == 1; | 
|  | 1258 | const char* delta_okay_str = delta_okay ? "true" : "false"; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1259 | brillo::Blob post_data; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1260 | OmahaRequestParams params(&fake_system_state_, | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 1261 | constants::kOmahaPlatformName, | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1262 | OmahaRequestParams::kOsVersion, | 
|  | 1263 | "service_pack", | 
|  | 1264 | "x86-generic", | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 1265 | kTestAppId, | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1266 | "0.1.0.0", | 
|  | 1267 | "en-US", | 
|  | 1268 | "unittest_track", | 
| Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1269 | "OEM MODEL REV 1234", | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1270 | "ChromeOSFirmware.1.0", | 
|  | 1271 | "EC100", | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1272 | delta_okay, | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1273 | false,  // interactive | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1274 | "http://url", | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1275 | "");    // target_version_prefix | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1276 | ASSERT_FALSE(TestUpdateCheck(¶ms, | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1277 | "invalid xml>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1278 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1279 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1280 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1281 | metrics::CheckResult::kParsingError, | 
|  | 1282 | metrics::CheckReaction::kUnset, | 
|  | 1283 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1284 | nullptr, | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1285 | &post_data)); | 
|  | 1286 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1287 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1288 | EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"", | 
|  | 1289 | delta_okay_str)), | 
| Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1290 | string::npos) | 
|  | 1291 | << "i = " << i; | 
|  | 1292 | } | 
|  | 1293 | } | 
|  | 1294 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1295 | TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) { | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1296 | for (int i = 0; i < 2; i++) { | 
|  | 1297 | bool interactive = i == 1; | 
| Gilad Arnold | 8a659d8 | 2013-01-24 11:26:00 -0800 | [diff] [blame] | 1298 | const char* interactive_str = interactive ? "ondemandupdate" : "scheduler"; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1299 | brillo::Blob post_data; | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 1300 | FakeSystemState fake_system_state; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1301 | OmahaRequestParams params(&fake_system_state_, | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 1302 | constants::kOmahaPlatformName, | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1303 | OmahaRequestParams::kOsVersion, | 
|  | 1304 | "service_pack", | 
|  | 1305 | "x86-generic", | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 1306 | kTestAppId, | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1307 | "0.1.0.0", | 
|  | 1308 | "en-US", | 
|  | 1309 | "unittest_track", | 
|  | 1310 | "OEM MODEL REV 1234", | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1311 | "ChromeOSFirmware.1.0", | 
|  | 1312 | "EC100", | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1313 | true,   // delta_okay | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1314 | interactive, | 
|  | 1315 | "http://url", | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1316 | "");    // target_version_prefix | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1317 | ASSERT_FALSE(TestUpdateCheck(¶ms, | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1318 | "invalid xml>", | 
|  | 1319 | -1, | 
|  | 1320 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1321 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1322 | metrics::CheckResult::kParsingError, | 
|  | 1323 | metrics::CheckReaction::kUnset, | 
|  | 1324 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1325 | nullptr, | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1326 | &post_data)); | 
|  | 1327 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1328 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1329 | EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"", | 
|  | 1330 | interactive_str)), | 
| Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1331 | string::npos) | 
|  | 1332 | << "i = " << i; | 
|  | 1333 | } | 
|  | 1334 | } | 
|  | 1335 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1336 | TEST_F(OmahaRequestActionTest, OmahaEventTest) { | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1337 | OmahaEvent default_event; | 
|  | 1338 | EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type); | 
|  | 1339 | EXPECT_EQ(OmahaEvent::kResultError, default_event.result); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1340 | EXPECT_EQ(ErrorCode::kError, default_event.error_code); | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1341 |  | 
|  | 1342 | OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted); | 
|  | 1343 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type); | 
|  | 1344 | EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1345 | EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code); | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1346 |  | 
|  | 1347 | OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished, | 
|  | 1348 | OmahaEvent::kResultError, | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1349 | ErrorCode::kError); | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1350 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type); | 
|  | 1351 | EXPECT_EQ(OmahaEvent::kResultError, error_event.result); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1352 | EXPECT_EQ(ErrorCode::kError, error_event.error_code); | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1353 | } | 
|  | 1354 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1355 | void OmahaRequestActionTest::PingTest(bool ping_only) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1356 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1357 | fake_system_state_.set_prefs(&prefs); | 
|  | 1358 | EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) | 
|  | 1359 | .Times(AnyNumber()); | 
|  | 1360 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
|  | 1361 | // Add a few hours to the day difference to test no rounding, etc. | 
|  | 1362 | int64_t five_days_ago = | 
|  | 1363 | (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue(); | 
|  | 1364 | int64_t six_days_ago = | 
|  | 1365 | (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue(); | 
|  | 1366 | EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) | 
|  | 1367 | .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true))); | 
|  | 1368 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1369 | .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true))); | 
|  | 1370 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1371 | .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true))); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1372 | brillo::Blob post_data; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1373 | ASSERT_TRUE( | 
|  | 1374 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1375 | fake_update_response_.GetNoUpdateResponse(), | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1376 | -1, | 
|  | 1377 | ping_only, | 
|  | 1378 | ErrorCode::kSuccess, | 
|  | 1379 | metrics::CheckResult::kUnset, | 
|  | 1380 | metrics::CheckReaction::kUnset, | 
|  | 1381 | metrics::DownloadErrorCode::kUnset, | 
|  | 1382 | nullptr, | 
|  | 1383 | &post_data)); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1384 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1385 | EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"), | 
|  | 1386 | string::npos); | 
|  | 1387 | if (ping_only) { | 
|  | 1388 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); | 
|  | 1389 | EXPECT_EQ(post_str.find("previousversion"), string::npos); | 
|  | 1390 | } else { | 
|  | 1391 | EXPECT_NE(post_str.find("updatecheck"), string::npos); | 
|  | 1392 | EXPECT_NE(post_str.find("previousversion"), string::npos); | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1393 | } | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1394 | } | 
|  | 1395 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1396 | TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) { | 
|  | 1397 | PingTest(true  /* ping_only */); | 
|  | 1398 | } | 
|  | 1399 |  | 
|  | 1400 | TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) { | 
|  | 1401 | PingTest(false  /* ping_only */); | 
|  | 1402 | } | 
|  | 1403 |  | 
|  | 1404 | TEST_F(OmahaRequestActionTest, ActivePingTest) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1405 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1406 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1407 | EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) | 
|  | 1408 | .Times(AnyNumber()); | 
|  | 1409 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1410 | int64_t three_days_ago = | 
|  | 1411 | (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue(); | 
|  | 1412 | int64_t now = Time::Now().ToInternalValue(); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1413 | EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) | 
|  | 1414 | .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true))); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1415 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1416 | .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true))); | 
|  | 1417 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1418 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1419 | brillo::Blob post_data; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1420 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1421 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1422 | fake_update_response_.GetNoUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1423 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1424 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1425 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1426 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1427 | metrics::CheckReaction::kUnset, | 
|  | 1428 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1429 | nullptr, | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1430 | &post_data)); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1431 | string post_str(post_data.begin(), post_data.end()); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1432 | EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1433 | string::npos); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1434 | } | 
|  | 1435 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1436 | TEST_F(OmahaRequestActionTest, RollCallPingTest) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1437 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1438 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1439 | EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) | 
|  | 1440 | .Times(AnyNumber()); | 
|  | 1441 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1442 | int64_t four_days_ago = | 
|  | 1443 | (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue(); | 
|  | 1444 | int64_t now = Time::Now().ToInternalValue(); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1445 | EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) | 
|  | 1446 | .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true))); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1447 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1448 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); | 
|  | 1449 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1450 | .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true))); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1451 | brillo::Blob post_data; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1452 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1453 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1454 | fake_update_response_.GetNoUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1455 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1456 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1457 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1458 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1459 | metrics::CheckReaction::kUnset, | 
|  | 1460 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1461 | nullptr, | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1462 | &post_data)); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1463 | string post_str(post_data.begin(), post_data.end()); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1464 | EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1465 | string::npos); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1466 | } | 
|  | 1467 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1468 | TEST_F(OmahaRequestActionTest, NoPingTest) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1469 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1470 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1471 | EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) | 
|  | 1472 | .Times(AnyNumber()); | 
|  | 1473 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1474 | int64_t one_hour_ago = | 
|  | 1475 | (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue(); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1476 | EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) | 
|  | 1477 | .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true))); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1478 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1479 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); | 
|  | 1480 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1481 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1482 | // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't | 
|  | 1483 | // send a ping. | 
|  | 1484 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1485 | .WillOnce(Return(true)); | 
|  | 1486 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1487 | .WillOnce(Return(true)); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1488 | brillo::Blob post_data; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1489 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1490 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1491 | fake_update_response_.GetNoUpdateResponse(), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1492 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1493 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1494 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1495 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1496 | metrics::CheckReaction::kUnset, | 
|  | 1497 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1498 | nullptr, | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1499 | &post_data)); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1500 | string post_str(post_data.begin(), post_data.end()); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1501 | EXPECT_EQ(post_str.find("ping"), string::npos); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1502 | } | 
|  | 1503 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1504 | TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) { | 
| Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1505 | // This test ensures that we ignore empty ping only requests. | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1506 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1507 | fake_system_state_.set_prefs(&prefs); | 
| Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1508 | int64_t now = Time::Now().ToInternalValue(); | 
|  | 1509 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1510 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); | 
|  | 1511 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1512 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); | 
|  | 1513 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); | 
|  | 1514 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1515 | brillo::Blob post_data; | 
| Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1516 | EXPECT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1517 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1518 | fake_update_response_.GetNoUpdateResponse(), | 
| Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1519 | -1, | 
|  | 1520 | true,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1521 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1522 | metrics::CheckResult::kUnset, | 
|  | 1523 | metrics::CheckReaction::kUnset, | 
|  | 1524 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1525 | nullptr, | 
| Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1526 | &post_data)); | 
|  | 1527 | EXPECT_EQ(post_data.size(), 0); | 
|  | 1528 | } | 
|  | 1529 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1530 | TEST_F(OmahaRequestActionTest, BackInTimePingTest) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1531 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1532 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1533 | EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) | 
|  | 1534 | .Times(AnyNumber()); | 
|  | 1535 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1536 | int64_t future = | 
|  | 1537 | (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue(); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1538 | EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) | 
|  | 1539 | .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true))); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1540 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1541 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); | 
|  | 1542 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1543 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); | 
|  | 1544 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) | 
|  | 1545 | .WillOnce(Return(true)); | 
|  | 1546 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) | 
|  | 1547 | .WillOnce(Return(true)); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1548 | brillo::Blob post_data; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1549 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1550 | TestUpdateCheck(nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1551 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " | 
|  | 1552 | "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>" | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1553 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1554 | "<updatecheck status=\"noupdate\"/></app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1555 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1556 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1557 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1558 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1559 | metrics::CheckReaction::kUnset, | 
|  | 1560 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1561 | nullptr, | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1562 | &post_data)); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1563 | string post_str(post_data.begin(), post_data.end()); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1564 | EXPECT_EQ(post_str.find("ping"), string::npos); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1565 | } | 
|  | 1566 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1567 | TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) { | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1568 | // This test checks that the action updates the last ping day to now | 
| Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1569 | // minus 200 seconds with a slack of 5 seconds. Therefore, the test | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1570 | // may fail if it runs for longer than 5 seconds. It shouldn't run | 
|  | 1571 | // that long though. | 
|  | 1572 | int64_t midnight = | 
|  | 1573 | (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue(); | 
|  | 1574 | int64_t midnight_slack = | 
|  | 1575 | (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue(); | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1576 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1577 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1578 | EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber()); | 
|  | 1579 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1580 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, | 
|  | 1581 | AllOf(Ge(midnight), Le(midnight_slack)))) | 
|  | 1582 | .WillOnce(Return(true)); | 
|  | 1583 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, | 
|  | 1584 | AllOf(Ge(midnight), Le(midnight_slack)))) | 
|  | 1585 | .WillOnce(Return(true)); | 
|  | 1586 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1587 | TestUpdateCheck(nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1588 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " | 
|  | 1589 | "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>" | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1590 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1591 | "<updatecheck status=\"noupdate\"/></app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1592 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1593 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1594 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1595 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1596 | metrics::CheckReaction::kUnset, | 
|  | 1597 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1598 | nullptr, | 
|  | 1599 | nullptr)); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1600 | } | 
|  | 1601 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1602 | TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1603 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1604 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1605 | EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber()); | 
|  | 1606 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1607 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); | 
|  | 1608 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); | 
|  | 1609 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1610 | TestUpdateCheck(nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1611 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " | 
|  | 1612 | "protocol=\"3.0\"><daystart blah=\"200\"/>" | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1613 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1614 | "<updatecheck status=\"noupdate\"/></app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1615 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1616 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1617 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1618 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1619 | metrics::CheckReaction::kUnset, | 
|  | 1620 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1621 | nullptr, | 
|  | 1622 | nullptr)); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1623 | } | 
|  | 1624 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1625 | TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) { | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1626 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1627 | fake_system_state_.set_prefs(&prefs); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1628 | EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber()); | 
|  | 1629 | EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1630 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); | 
|  | 1631 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); | 
|  | 1632 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1633 | TestUpdateCheck(nullptr,  // request_params | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1634 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " | 
|  | 1635 | "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>" | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1636 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1637 | "<updatecheck status=\"noupdate\"/></app></response>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1638 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1639 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1640 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1641 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1642 | metrics::CheckReaction::kUnset, | 
|  | 1643 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1644 | nullptr, | 
|  | 1645 | nullptr)); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1646 | } | 
|  | 1647 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1648 | TEST_F(OmahaRequestActionTest, NoUniqueIDTest) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1649 | brillo::Blob post_data; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1650 | ASSERT_FALSE(TestUpdateCheck(nullptr,  // request_params | 
| Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1651 | "invalid xml>", | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1652 | -1, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1653 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1654 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1655 | metrics::CheckResult::kParsingError, | 
|  | 1656 | metrics::CheckReaction::kUnset, | 
|  | 1657 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1658 | nullptr,  // response | 
| Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1659 | &post_data)); | 
|  | 1660 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1661 | string post_str(post_data.begin(), post_data.end()); | 
| Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1662 | EXPECT_EQ(post_str.find("machineid="), string::npos); | 
|  | 1663 | EXPECT_EQ(post_str.find("userid="), string::npos); | 
|  | 1664 | } | 
|  | 1665 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1666 | TEST_F(OmahaRequestActionTest, NetworkFailureTest) { | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1667 | OmahaResponse response; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1668 | const int http_error_code = | 
|  | 1669 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501; | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1670 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1671 | TestUpdateCheck(nullptr,  // request_params | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1672 | "", | 
|  | 1673 | 501, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1674 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1675 | static_cast<ErrorCode>(http_error_code), | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1676 | metrics::CheckResult::kDownloadError, | 
|  | 1677 | metrics::CheckReaction::kUnset, | 
|  | 1678 | static_cast<metrics::DownloadErrorCode>(501), | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1679 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1680 | nullptr)); | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1681 | EXPECT_FALSE(response.update_exists); | 
|  | 1682 | } | 
|  | 1683 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1684 | TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) { | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1685 | OmahaResponse response; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1686 | const int http_error_code = | 
|  | 1687 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999; | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1688 | ASSERT_FALSE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1689 | TestUpdateCheck(nullptr,  // request_params | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1690 | "", | 
|  | 1691 | 1500, | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1692 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1693 | static_cast<ErrorCode>(http_error_code), | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1694 | metrics::CheckResult::kDownloadError, | 
|  | 1695 | metrics::CheckReaction::kUnset, | 
|  | 1696 | metrics::DownloadErrorCode::kHttpStatusOther, | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1697 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1698 | nullptr)); | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1699 | EXPECT_FALSE(response.update_exists); | 
|  | 1700 | } | 
|  | 1701 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1702 | TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1703 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1704 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1705 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 1706 | params.set_waiting_period(TimeDelta().FromDays(1)); | 
|  | 1707 | params.set_update_check_count_wait_enabled(false); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1708 |  | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1709 | ASSERT_FALSE(TestUpdateCheck( | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1710 | ¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1711 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1712 | -1, | 
|  | 1713 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1714 | ErrorCode::kOmahaUpdateDeferredPerPolicy, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1715 | metrics::CheckResult::kUpdateAvailable, | 
|  | 1716 | metrics::CheckReaction::kDeferring, | 
|  | 1717 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1718 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1719 | nullptr)); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1720 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1721 | int64_t timestamp = 0; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1722 | ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1723 | ASSERT_GT(timestamp, 0); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1724 | EXPECT_FALSE(response.update_exists); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1725 |  | 
|  | 1726 | // Verify if we are interactive check we don't defer. | 
|  | 1727 | params.set_interactive(true); | 
|  | 1728 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1729 | TestUpdateCheck(¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1730 | fake_update_response_.GetUpdateResponse(), | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1731 | -1, | 
|  | 1732 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1733 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1734 | metrics::CheckResult::kUpdateAvailable, | 
|  | 1735 | metrics::CheckReaction::kUpdating, | 
|  | 1736 | metrics::DownloadErrorCode::kUnset, | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1737 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1738 | nullptr)); | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1739 | EXPECT_TRUE(response.update_exists); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1740 | } | 
|  | 1741 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1742 | TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1743 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1744 | OmahaRequestParams params = request_params_; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1745 | params.set_wall_clock_based_wait_enabled(true); | 
|  | 1746 | params.set_waiting_period(TimeDelta().FromDays(1)); | 
|  | 1747 | params.set_update_check_count_wait_enabled(false); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1748 |  | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1749 | // Set the timestamp to a very old value such that it exceeds the | 
|  | 1750 | // waiting period set above. | 
|  | 1751 | Time t1; | 
|  | 1752 | Time::FromString("1/1/2012", &t1); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1753 | ASSERT_TRUE(fake_prefs_.SetInt64( | 
|  | 1754 | kPrefsUpdateFirstSeenAt, t1.ToInternalValue())); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1755 | ASSERT_TRUE(TestUpdateCheck( | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1756 | ¶ms, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1757 | fake_update_response_.GetUpdateResponse(), | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1758 | -1, | 
|  | 1759 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1760 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1761 | metrics::CheckResult::kUpdateAvailable, | 
|  | 1762 | metrics::CheckReaction::kUpdating, | 
|  | 1763 | metrics::DownloadErrorCode::kUnset, | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1764 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1765 | nullptr)); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1766 |  | 
|  | 1767 | EXPECT_TRUE(response.update_exists); | 
|  | 1768 |  | 
|  | 1769 | // Make sure the timestamp t1 is unchanged showing that it was reused. | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1770 | int64_t timestamp = 0; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1771 | ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1772 | ASSERT_TRUE(timestamp == t1.ToInternalValue()); | 
|  | 1773 | } | 
|  | 1774 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1775 | TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) { | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1776 | // Create a uniquely named test directory. | 
|  | 1777 | string test_dir; | 
|  | 1778 | ASSERT_TRUE(utils::MakeTempDirectory( | 
|  | 1779 | "omaha_request_action-test-XXXXXX", &test_dir)); | 
|  | 1780 |  | 
|  | 1781 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc")); | 
|  | 1782 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1783 | kStatefulPartition + "/etc")); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1784 | brillo::Blob post_data; | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1785 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1786 | fake_system_state_.set_prefs(&prefs); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1787 | ASSERT_TRUE(WriteFileString( | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1788 | test_dir + "/etc/lsb-release", | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1789 | "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n" | 
|  | 1790 | "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n" | 
|  | 1791 | "CHROMEOS_RELEASE_TRACK=canary-channel\n")); | 
|  | 1792 | ASSERT_TRUE(WriteFileString( | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1793 | test_dir + kStatefulPartition + "/etc/lsb-release", | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1794 | "CHROMEOS_IS_POWERWASH_ALLOWED=true\n" | 
|  | 1795 | "CHROMEOS_RELEASE_TRACK=stable-channel\n")); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1796 | OmahaRequestParams params = request_params_; | 
| Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 1797 | params.set_root(test_dir); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1798 | params.Init("1.2.3.4", "", 0); | 
|  | 1799 | EXPECT_EQ("canary-channel", params.current_channel()); | 
|  | 1800 | EXPECT_EQ("stable-channel", params.target_channel()); | 
|  | 1801 | EXPECT_TRUE(params.to_more_stable_channel()); | 
|  | 1802 | EXPECT_TRUE(params.is_powerwash_allowed()); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1803 | ASSERT_FALSE(TestUpdateCheck(¶ms, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1804 | "invalid xml>", | 
|  | 1805 | -1, | 
|  | 1806 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1807 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1808 | metrics::CheckResult::kParsingError, | 
|  | 1809 | metrics::CheckReaction::kUnset, | 
|  | 1810 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1811 | nullptr,  // response | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1812 | &post_data)); | 
|  | 1813 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1814 | string post_str(post_data.begin(), post_data.end()); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1815 | EXPECT_NE(string::npos, post_str.find( | 
|  | 1816 | "appid=\"{22222222-2222-2222-2222-222222222222}\" " | 
|  | 1817 | "version=\"0.0.0.0\" from_version=\"1.2.3.4\" " | 
|  | 1818 | "track=\"stable-channel\" from_track=\"canary-channel\" ")); | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1819 |  | 
| Alex Deymo | 110e030 | 2015-10-19 20:35:21 -0700 | [diff] [blame] | 1820 | ASSERT_TRUE(base::DeleteFile(base::FilePath(test_dir), true)); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1821 | } | 
|  | 1822 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1823 | TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) { | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1824 | // Create a uniquely named test directory. | 
|  | 1825 | string test_dir; | 
|  | 1826 | ASSERT_TRUE(utils::MakeTempDirectory( | 
|  | 1827 | "omaha_request_action-test-XXXXXX", &test_dir)); | 
|  | 1828 |  | 
|  | 1829 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc")); | 
|  | 1830 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1831 | kStatefulPartition + "/etc")); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1832 | brillo::Blob post_data; | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 1833 | NiceMock<MockPrefs> prefs; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1834 | fake_system_state_.set_prefs(&prefs); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1835 | ASSERT_TRUE(WriteFileString( | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1836 | test_dir + "/etc/lsb-release", | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1837 | "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n" | 
|  | 1838 | "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n" | 
|  | 1839 | "CHROMEOS_RELEASE_TRACK=stable-channel\n")); | 
|  | 1840 | ASSERT_TRUE(WriteFileString( | 
| Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1841 | test_dir + kStatefulPartition + "/etc/lsb-release", | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1842 | "CHROMEOS_RELEASE_TRACK=canary-channel\n")); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1843 | OmahaRequestParams params = request_params_; | 
| Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame] | 1844 | params.set_root(test_dir); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1845 | params.Init("5.6.7.8", "", 0); | 
|  | 1846 | EXPECT_EQ("stable-channel", params.current_channel()); | 
|  | 1847 | EXPECT_EQ("canary-channel", params.target_channel()); | 
|  | 1848 | EXPECT_FALSE(params.to_more_stable_channel()); | 
|  | 1849 | EXPECT_FALSE(params.is_powerwash_allowed()); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1850 | ASSERT_FALSE(TestUpdateCheck(¶ms, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1851 | "invalid xml>", | 
|  | 1852 | -1, | 
|  | 1853 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1854 | ErrorCode::kOmahaRequestXMLParseError, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1855 | metrics::CheckResult::kParsingError, | 
|  | 1856 | metrics::CheckReaction::kUnset, | 
|  | 1857 | metrics::DownloadErrorCode::kUnset, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1858 | nullptr,  // response | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1859 | &post_data)); | 
|  | 1860 | // convert post_data to string | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1861 | string post_str(post_data.begin(), post_data.end()); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1862 | EXPECT_NE(string::npos, post_str.find( | 
|  | 1863 | "appid=\"{11111111-1111-1111-1111-111111111111}\" " | 
|  | 1864 | "version=\"5.6.7.8\" " | 
|  | 1865 | "track=\"canary-channel\" from_track=\"stable-channel\"")); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1866 | EXPECT_EQ(string::npos, post_str.find("from_version")); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1867 | } | 
|  | 1868 |  | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1869 | // Checks that the initial ping with a=-1 r=-1 is not send when the device | 
|  | 1870 | // was powerwashed. | 
|  | 1871 | TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) { | 
|  | 1872 | fake_prefs_.SetString(kPrefsPreviousVersion, ""); | 
|  | 1873 |  | 
|  | 1874 | // Flag that the device was powerwashed in the past. | 
|  | 1875 | fake_system_state_.fake_hardware()->SetPowerwashCount(1); | 
|  | 1876 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1877 | brillo::Blob post_data; | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1878 | ASSERT_TRUE( | 
|  | 1879 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1880 | fake_update_response_.GetNoUpdateResponse(), | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1881 | -1, | 
|  | 1882 | false,  // ping_only | 
|  | 1883 | ErrorCode::kSuccess, | 
|  | 1884 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1885 | metrics::CheckReaction::kUnset, | 
|  | 1886 | metrics::DownloadErrorCode::kUnset, | 
|  | 1887 | nullptr, | 
|  | 1888 | &post_data)); | 
|  | 1889 | // We shouldn't send a ping in this case since powerwash > 0. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1890 | string post_str(post_data.begin(), post_data.end()); | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1891 | EXPECT_EQ(string::npos, post_str.find("<ping")); | 
|  | 1892 | } | 
|  | 1893 |  | 
| Alex Deymo | 9fded1e | 2015-11-05 12:31:19 -0800 | [diff] [blame] | 1894 | // Checks that the event 54 is sent on a reboot to a new update. | 
|  | 1895 | TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) { | 
|  | 1896 | // Flag that the device was updated in a previous boot. | 
|  | 1897 | fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4"); | 
|  | 1898 |  | 
|  | 1899 | brillo::Blob post_data; | 
|  | 1900 | ASSERT_TRUE( | 
|  | 1901 | TestUpdateCheck(nullptr,  // request_params | 
|  | 1902 | fake_update_response_.GetNoUpdateResponse(), | 
|  | 1903 | -1, | 
|  | 1904 | false,  // ping_only | 
|  | 1905 | ErrorCode::kSuccess, | 
|  | 1906 | metrics::CheckResult::kNoUpdateAvailable, | 
|  | 1907 | metrics::CheckReaction::kUnset, | 
|  | 1908 | metrics::DownloadErrorCode::kUnset, | 
|  | 1909 | nullptr, | 
|  | 1910 | &post_data)); | 
|  | 1911 | string post_str(post_data.begin(), post_data.end()); | 
|  | 1912 |  | 
|  | 1913 | // An event 54 is included and has the right version. | 
|  | 1914 | EXPECT_NE(string::npos, | 
|  | 1915 | post_str.find(base::StringPrintf( | 
|  | 1916 | "<event eventtype=\"%d\"", | 
|  | 1917 | OmahaEvent::kTypeRebootedAfterUpdate))); | 
|  | 1918 | EXPECT_NE(string::npos, | 
|  | 1919 | post_str.find("previousversion=\"1.2.3.4\"></event>")); | 
|  | 1920 |  | 
|  | 1921 | // The previous version flag should have been removed. | 
|  | 1922 | EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion)); | 
|  | 1923 | string prev_version; | 
|  | 1924 | EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version)); | 
|  | 1925 | EXPECT_TRUE(prev_version.empty()); | 
|  | 1926 | } | 
|  | 1927 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1928 | void OmahaRequestActionTest::P2PTest( | 
|  | 1929 | bool initial_allow_p2p_for_downloading, | 
|  | 1930 | bool initial_allow_p2p_for_sharing, | 
|  | 1931 | bool omaha_disable_p2p_for_downloading, | 
|  | 1932 | bool omaha_disable_p2p_for_sharing, | 
|  | 1933 | bool payload_state_allow_p2p_attempt, | 
|  | 1934 | bool expect_p2p_client_lookup, | 
|  | 1935 | const string& p2p_client_result_url, | 
|  | 1936 | bool expected_allow_p2p_for_downloading, | 
|  | 1937 | bool expected_allow_p2p_for_sharing, | 
|  | 1938 | const string& expected_p2p_url) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1939 | OmahaResponse response; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1940 | OmahaRequestParams request_params = request_params_; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1941 | bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading; | 
|  | 1942 | bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing; | 
|  | 1943 | string actual_p2p_url; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1944 |  | 
|  | 1945 | MockPayloadState mock_payload_state; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1946 | fake_system_state_.set_payload_state(&mock_payload_state); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1947 | EXPECT_CALL(mock_payload_state, P2PAttemptAllowed()) | 
|  | 1948 | .WillRepeatedly(Return(payload_state_allow_p2p_attempt)); | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1949 | EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading()) | 
|  | 1950 | .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading)); | 
|  | 1951 | EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing()) | 
|  | 1952 | .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing)); | 
|  | 1953 | EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_)) | 
|  | 1954 | .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading)); | 
|  | 1955 | EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_)) | 
|  | 1956 | .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing)); | 
|  | 1957 | EXPECT_CALL(mock_payload_state, SetP2PUrl(_)) | 
|  | 1958 | .WillRepeatedly(SaveArg<0>(&actual_p2p_url)); | 
|  | 1959 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1960 | MockP2PManager mock_p2p_manager; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1961 | fake_system_state_.set_p2p_manager(&mock_p2p_manager); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1962 | mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url); | 
|  | 1963 |  | 
| David Zeuthen | 4cc5ed2 | 2014-01-15 12:35:03 -0800 | [diff] [blame] | 1964 | TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds); | 
|  | 1965 | EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _)) | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1966 | .Times(expect_p2p_client_lookup ? 1 : 0); | 
|  | 1967 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1968 | fake_update_response_.disable_p2p_for_downloading = | 
|  | 1969 | omaha_disable_p2p_for_downloading; | 
|  | 1970 | fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1971 | ASSERT_TRUE( | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1972 | TestUpdateCheck(&request_params, | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1973 | fake_update_response_.GetUpdateResponse(), | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1974 | -1, | 
|  | 1975 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1976 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1977 | metrics::CheckResult::kUpdateAvailable, | 
|  | 1978 | metrics::CheckReaction::kUpdating, | 
|  | 1979 | metrics::DownloadErrorCode::kUnset, | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1980 | &response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1981 | nullptr)); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1982 | EXPECT_TRUE(response.update_exists); | 
|  | 1983 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1984 | EXPECT_EQ(omaha_disable_p2p_for_downloading, | 
|  | 1985 | response.disable_p2p_for_downloading); | 
|  | 1986 | EXPECT_EQ(omaha_disable_p2p_for_sharing, | 
|  | 1987 | response.disable_p2p_for_sharing); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1988 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1989 | EXPECT_EQ(expected_allow_p2p_for_downloading, | 
|  | 1990 | actual_allow_p2p_for_downloading); | 
|  | 1991 | EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing); | 
|  | 1992 | EXPECT_EQ(expected_p2p_url, actual_p2p_url); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1993 | } | 
|  | 1994 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1995 | TEST_F(OmahaRequestActionTest, P2PWithPeer) { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1996 | P2PTest(true,                   // initial_allow_p2p_for_downloading | 
|  | 1997 | true,                   // initial_allow_p2p_for_sharing | 
|  | 1998 | false,                  // omaha_disable_p2p_for_downloading | 
|  | 1999 | false,                  // omaha_disable_p2p_for_sharing | 
|  | 2000 | true,                   // payload_state_allow_p2p_attempt | 
|  | 2001 | true,                   // expect_p2p_client_lookup | 
|  | 2002 | "http://1.3.5.7/p2p",   // p2p_client_result_url | 
|  | 2003 | true,                   // expected_allow_p2p_for_downloading | 
|  | 2004 | true,                   // expected_allow_p2p_for_sharing | 
|  | 2005 | "http://1.3.5.7/p2p");  // expected_p2p_url | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 2006 | } | 
|  | 2007 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2008 | TEST_F(OmahaRequestActionTest, P2PWithoutPeer) { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 2009 | P2PTest(true,                   // initial_allow_p2p_for_downloading | 
|  | 2010 | true,                   // initial_allow_p2p_for_sharing | 
|  | 2011 | false,                  // omaha_disable_p2p_for_downloading | 
|  | 2012 | false,                  // omaha_disable_p2p_for_sharing | 
|  | 2013 | true,                   // payload_state_allow_p2p_attempt | 
|  | 2014 | true,                   // expect_p2p_client_lookup | 
|  | 2015 | "",                     // p2p_client_result_url | 
|  | 2016 | false,                  // expected_allow_p2p_for_downloading | 
|  | 2017 | true,                   // expected_allow_p2p_for_sharing | 
|  | 2018 | "");                    // expected_p2p_url | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 2019 | } | 
|  | 2020 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2021 | TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 2022 | P2PTest(false,                  // initial_allow_p2p_for_downloading | 
|  | 2023 | true,                   // initial_allow_p2p_for_sharing | 
|  | 2024 | false,                  // omaha_disable_p2p_for_downloading | 
|  | 2025 | false,                  // omaha_disable_p2p_for_sharing | 
|  | 2026 | true,                   // payload_state_allow_p2p_attempt | 
|  | 2027 | false,                  // expect_p2p_client_lookup | 
|  | 2028 | "unset",                // p2p_client_result_url | 
|  | 2029 | false,                  // expected_allow_p2p_for_downloading | 
|  | 2030 | true,                   // expected_allow_p2p_for_sharing | 
|  | 2031 | "");                    // expected_p2p_url | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 2032 | } | 
|  | 2033 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2034 | TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 2035 | P2PTest(true,                   // initial_allow_p2p_for_downloading | 
|  | 2036 | true,                   // initial_allow_p2p_for_sharing | 
|  | 2037 | true,                   // omaha_disable_p2p_for_downloading | 
|  | 2038 | false,                  // omaha_disable_p2p_for_sharing | 
|  | 2039 | true,                   // payload_state_allow_p2p_attempt | 
|  | 2040 | false,                  // expect_p2p_client_lookup | 
|  | 2041 | "unset",                // p2p_client_result_url | 
|  | 2042 | false,                  // expected_allow_p2p_for_downloading | 
|  | 2043 | true,                   // expected_allow_p2p_for_sharing | 
|  | 2044 | "");                    // expected_p2p_url | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 2045 | } | 
|  | 2046 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2047 | TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 2048 | P2PTest(true,                   // initial_allow_p2p_for_downloading | 
|  | 2049 | true,                   // initial_allow_p2p_for_sharing | 
|  | 2050 | false,                  // omaha_disable_p2p_for_downloading | 
|  | 2051 | true,                   // omaha_disable_p2p_for_sharing | 
|  | 2052 | true,                   // payload_state_allow_p2p_attempt | 
|  | 2053 | true,                   // expect_p2p_client_lookup | 
|  | 2054 | "http://1.3.5.7/p2p",   // p2p_client_result_url | 
|  | 2055 | true,                   // expected_allow_p2p_for_downloading | 
|  | 2056 | false,                  // expected_allow_p2p_for_sharing | 
|  | 2057 | "http://1.3.5.7/p2p");  // expected_p2p_url | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 2058 | } | 
|  | 2059 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2060 | TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 2061 | P2PTest(true,                   // initial_allow_p2p_for_downloading | 
|  | 2062 | true,                   // initial_allow_p2p_for_sharing | 
|  | 2063 | true,                   // omaha_disable_p2p_for_downloading | 
|  | 2064 | true,                   // omaha_disable_p2p_for_sharing | 
|  | 2065 | true,                   // payload_state_allow_p2p_attempt | 
|  | 2066 | false,                  // expect_p2p_client_lookup | 
|  | 2067 | "unset",                // p2p_client_result_url | 
|  | 2068 | false,                  // expected_allow_p2p_for_downloading | 
|  | 2069 | false,                  // expected_allow_p2p_for_sharing | 
|  | 2070 | "");                    // expected_p2p_url | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 2071 | } | 
|  | 2072 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 2073 | bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days, | 
|  | 2074 | OmahaResponse *response) { | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 2075 | fake_update_response_.elapsed_days = elapsed_days; | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2076 | return | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2077 | TestUpdateCheck(nullptr,  // request_params | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 2078 | fake_update_response_.GetUpdateResponse(), | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2079 | -1, | 
|  | 2080 | false,  // ping_only | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 2081 | ErrorCode::kSuccess, | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 2082 | metrics::CheckResult::kUpdateAvailable, | 
|  | 2083 | metrics::CheckReaction::kUpdating, | 
|  | 2084 | metrics::DownloadErrorCode::kUnset, | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2085 | response, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 2086 | nullptr); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2087 | } | 
|  | 2088 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2089 | TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) { | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2090 | OmahaResponse response; | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2091 |  | 
|  | 2092 | // Check that we parse elapsed_days in the Omaha Response correctly. | 
|  | 2093 | // and that the kPrefsInstallDateDays value is written to. | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2094 | EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays)); | 
|  | 2095 | EXPECT_TRUE(InstallDateParseHelper("42", &response)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2096 | EXPECT_TRUE(response.update_exists); | 
|  | 2097 | EXPECT_EQ(42, response.install_date_days); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2098 | EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2099 | int64_t prefs_days; | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2100 | EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2101 | EXPECT_EQ(prefs_days, 42); | 
|  | 2102 |  | 
|  | 2103 | // If there already is a value set, we shouldn't do anything. | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2104 | EXPECT_TRUE(InstallDateParseHelper("7", &response)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2105 | EXPECT_TRUE(response.update_exists); | 
|  | 2106 | EXPECT_EQ(7, response.install_date_days); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2107 | EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2108 | EXPECT_EQ(prefs_days, 42); | 
|  | 2109 |  | 
|  | 2110 | // Note that elapsed_days is not necessarily divisible by 7 so check | 
|  | 2111 | // that we round down correctly when populating kPrefsInstallDateDays. | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2112 | EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays)); | 
|  | 2113 | EXPECT_TRUE(InstallDateParseHelper("23", &response)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2114 | EXPECT_TRUE(response.update_exists); | 
|  | 2115 | EXPECT_EQ(23, response.install_date_days); | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2116 | EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2117 | EXPECT_EQ(prefs_days, 21); | 
|  | 2118 |  | 
|  | 2119 | // Check that we correctly handle elapsed_days not being included in | 
|  | 2120 | // the Omaha Response. | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2121 | EXPECT_TRUE(InstallDateParseHelper("", &response)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2122 | EXPECT_TRUE(response.update_exists); | 
|  | 2123 | EXPECT_EQ(-1, response.install_date_days); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2124 | } | 
|  | 2125 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2126 | // If there is no prefs and OOBE is not complete, we should not | 
|  | 2127 | // report anything to Omaha. | 
|  | 2128 | TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) { | 
|  | 2129 | EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1); | 
|  | 2130 | EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays)); | 
|  | 2131 | } | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2132 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2133 | // If OOBE is complete and happened on a valid date (e.g. after Jan | 
|  | 2134 | // 1 2007 0:00 PST), that date should be used and written to | 
|  | 2135 | // prefs. However, first try with an invalid date and check we do | 
|  | 2136 | // nothing. | 
|  | 2137 | TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) { | 
|  | 2138 | Time oobe_date = Time::FromTimeT(42);  // Dec 31, 1969 16:00:42 PST. | 
|  | 2139 | fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date); | 
|  | 2140 | EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1); | 
|  | 2141 | EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays)); | 
|  | 2142 | } | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2143 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2144 | // Then check with a valid date. The date Jan 20, 2007 0:00 PST | 
|  | 2145 | // should yield an InstallDate of 14. | 
|  | 2146 | TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) { | 
|  | 2147 | Time oobe_date = Time::FromTimeT(1169280000);  // Jan 20, 2007 0:00 PST. | 
|  | 2148 | fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date); | 
|  | 2149 | EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14); | 
|  | 2150 | EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2151 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2152 | int64_t prefs_days; | 
|  | 2153 | EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); | 
|  | 2154 | EXPECT_EQ(prefs_days, 14); | 
|  | 2155 | } | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2156 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2157 | // Now that we have a valid date in prefs, check that we keep using | 
|  | 2158 | // that even if OOBE date reports something else. The date Jan 30, | 
|  | 2159 | // 2007 0:00 PST should yield an InstallDate of 28... but since | 
|  | 2160 | // there's a prefs file, we should still get 14. | 
|  | 2161 | TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) { | 
|  | 2162 | // Set a valid date in the prefs first. | 
|  | 2163 | EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14)); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2164 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2165 | Time oobe_date = Time::FromTimeT(1170144000);  // Jan 30, 2007 0:00 PST. | 
|  | 2166 | fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date); | 
|  | 2167 | EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2168 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2169 | int64_t prefs_days; | 
|  | 2170 | EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); | 
|  | 2171 | EXPECT_EQ(prefs_days, 14); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2172 |  | 
| Alex Deymo | e1e3afe | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 2173 | // If we delete the prefs file, we should get 28 days. | 
|  | 2174 | EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays)); | 
|  | 2175 | EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28); | 
|  | 2176 | EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); | 
|  | 2177 | EXPECT_EQ(prefs_days, 28); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 2178 | } | 
|  | 2179 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 2180 | }  // namespace chromeos_update_engine |