Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 7 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 8 | #include <glib.h> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 9 | |
| 10 | #include "base/string_util.h" |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 12 | #include "base/time.h" |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 14 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 15 | #include "update_engine/action_pipe.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 16 | #include "update_engine/constants.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 17 | #include "update_engine/mock_http_fetcher.h" |
| 18 | #include "update_engine/omaha_hash_calculator.h" |
| 19 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 20 | #include "update_engine/omaha_request_params.h" |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 21 | #include "update_engine/prefs.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 22 | #include "update_engine/test_utils.h" |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 23 | #include "update_engine/utils.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 24 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 25 | using base::Time; |
| 26 | using base::TimeDelta; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 27 | using std::string; |
| 28 | using std::vector; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 29 | using testing::_; |
| 30 | using testing::AllOf; |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 31 | using testing::DoAll; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 32 | using testing::Ge; |
| 33 | using testing::Le; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 34 | using testing::NiceMock; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 35 | using testing::Return; |
| 36 | using testing::SetArgumentPointee; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 40 | class OmahaRequestActionTest : public ::testing::Test {}; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 41 | |
| 42 | namespace { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 43 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 44 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 45 | OmahaRequestParams kDefaultTestParams( |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 46 | &mock_system_state, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 47 | OmahaRequestParams::kOsPlatform, |
| 48 | OmahaRequestParams::kOsVersion, |
| 49 | "service_pack", |
| 50 | "x86-generic", |
| 51 | OmahaRequestParams::kAppId, |
| 52 | "0.1.0.0", |
| 53 | "en-US", |
| 54 | "unittest", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 55 | "OEM MODEL 09235 7471", |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 56 | "ChromeOSFirmware.1.0", |
| 57 | "0X0A1", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 58 | false, // delta okay |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 59 | false, // interactive |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 60 | "http://url", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 61 | false, // update_disabled |
| 62 | "", // target_version_prefix |
| 63 | false, // use_p2p_for_downloading |
| 64 | false); // use_p2p_for_sharing |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 65 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 66 | string GetNoUpdateResponse(const string& app_id) { |
| 67 | return string( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 68 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 69 | "<daystart elapsed_seconds=\"100\"/>" |
| 70 | "<app appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 71 | "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></response>"; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 74 | string GetUpdateResponse2(const string& app_id, |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 75 | const string& version, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 76 | const string& more_info_url, |
| 77 | const string& prompt, |
| 78 | const string& codebase, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 79 | const string& filename, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 80 | const string& hash, |
| 81 | const string& needsadmin, |
| 82 | const string& size, |
| 83 | const string& deadline, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 84 | const string& max_days_to_scatter, |
| 85 | bool disable_p2p_for_downloading, |
| 86 | bool disable_p2p_for_sharing) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 87 | string response = |
| 88 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 89 | "protocol=\"3.0\">" |
| 90 | "<daystart elapsed_seconds=\"100\"/>" |
| 91 | "<app appid=\"" + app_id + "\" status=\"ok\">" |
| 92 | "<ping status=\"ok\"/><updatecheck status=\"ok\">" |
| 93 | "<urls><url codebase=\"" + codebase + "\"/></urls>" |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 94 | "<manifest version=\"" + version + "\">" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 95 | "<packages><package hash=\"not-used\" name=\"" + filename + "\" " |
| 96 | "size=\"" + size + "\"/></packages>" |
| 97 | "<actions><action event=\"postinstall\" " |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 98 | "ChromeOSVersion=\"" + version + "\" " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 99 | "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " |
| 100 | "IsDelta=\"true\" " |
Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 101 | "IsDeltaPayload=\"true\" " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 102 | "MaxDaysToScatter=\"" + max_days_to_scatter + "\" " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 103 | "sha256=\"" + hash + "\" " |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 104 | "needsadmin=\"" + needsadmin + "\" " + |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 105 | (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) + |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 106 | (disable_p2p_for_downloading ? |
| 107 | "DisableP2PForDownloading=\"true\" " : "") + |
| 108 | (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") + |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 109 | "/></actions></manifest></updatecheck></app></response>"; |
| 110 | LOG(INFO) << "Response = " << response; |
| 111 | return response; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 114 | string GetUpdateResponse(const string& app_id, |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 115 | const string& version, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 116 | const string& more_info_url, |
| 117 | const string& prompt, |
| 118 | const string& codebase, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 119 | const string& filename, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 120 | const string& hash, |
| 121 | const string& needsadmin, |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 122 | const string& size, |
| 123 | const string& deadline) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 124 | return GetUpdateResponse2(app_id, |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 125 | version, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 126 | more_info_url, |
| 127 | prompt, |
| 128 | codebase, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 129 | filename, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 130 | hash, |
| 131 | needsadmin, |
| 132 | size, |
| 133 | deadline, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 134 | "7", |
| 135 | false, // disable_p2p_for_downloading |
| 136 | false); // disable_p2p_for sharing |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 140 | public: |
| 141 | OmahaRequestActionTestProcessorDelegate() |
| 142 | : loop_(NULL), |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 143 | expected_code_(kErrorCodeSuccess) {} |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 144 | virtual ~OmahaRequestActionTestProcessorDelegate() { |
| 145 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 146 | virtual void ProcessingDone(const ActionProcessor* processor, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 147 | ErrorCode code) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 148 | ASSERT_TRUE(loop_); |
| 149 | g_main_loop_quit(loop_); |
| 150 | } |
| 151 | |
| 152 | virtual void ActionCompleted(ActionProcessor* processor, |
| 153 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 154 | ErrorCode code) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 155 | // make sure actions always succeed |
| 156 | if (action->Type() == OmahaRequestAction::StaticType()) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 157 | EXPECT_EQ(expected_code_, code); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 158 | else |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 159 | EXPECT_EQ(kErrorCodeSuccess, code); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 160 | } |
| 161 | GMainLoop *loop_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 162 | ErrorCode expected_code_; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 166 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 167 | processor->StartProcessing(); |
| 168 | return FALSE; |
| 169 | } |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 170 | } // namespace {} |
| 171 | |
| 172 | class OutputObjectCollectorAction; |
| 173 | |
| 174 | template<> |
| 175 | class ActionTraits<OutputObjectCollectorAction> { |
| 176 | public: |
| 177 | // Does not take an object for input |
| 178 | typedef OmahaResponse InputObjectType; |
| 179 | // On success, puts the output path on output |
| 180 | typedef NoneType OutputObjectType; |
| 181 | }; |
| 182 | |
| 183 | class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> { |
| 184 | public: |
| 185 | OutputObjectCollectorAction() : has_input_object_(false) {} |
| 186 | void PerformAction() { |
| 187 | // copy input object |
| 188 | has_input_object_ = HasInputObject(); |
| 189 | if (has_input_object_) |
| 190 | omaha_response_ = GetInputObject(); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 191 | processor_->ActionComplete(this, kErrorCodeSuccess); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 192 | } |
| 193 | // Should never be called |
| 194 | void TerminateProcessing() { |
| 195 | CHECK(false); |
| 196 | } |
| 197 | // Debugging/logging |
| 198 | static std::string StaticType() { |
| 199 | return "OutputObjectCollectorAction"; |
| 200 | } |
| 201 | std::string Type() const { return StaticType(); } |
| 202 | bool has_input_object_; |
| 203 | OmahaResponse omaha_response_; |
| 204 | }; |
| 205 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 206 | // Returns true iff an output response was obtained from the |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 207 | // OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 208 | // used. |payload_state| may be NULL, in which case a local mock is used. |
| 209 | // |p2p_manager| may be NULL, in which case a local mock is used. |
| 210 | // out_response may be NULL. If |fail_http_response_code| is non-negative, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 211 | // the transfer will fail with that code. |ping_only| is passed through to the |
| 212 | // OmahaRequestAction constructor. out_post_data may be null; if non-null, the |
| 213 | // post-data received by the mock HttpFetcher is returned. |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 214 | bool TestUpdateCheck(PrefsInterface* prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 215 | PayloadStateInterface *payload_state, |
| 216 | P2PManager *p2p_manager, |
| 217 | OmahaRequestParams& params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 218 | const string& http_response, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 219 | int fail_http_response_code, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 220 | bool ping_only, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 221 | ErrorCode expected_code, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 222 | OmahaResponse* out_response, |
| 223 | vector<char>* out_post_data) { |
| 224 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 225 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 226 | http_response.size(), |
| 227 | NULL); |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 228 | if (fail_http_response_code >= 0) { |
| 229 | fetcher->FailTransfer(fail_http_response_code); |
| 230 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 231 | MockSystemState mock_system_state; |
| 232 | if (prefs) |
| 233 | mock_system_state.set_prefs(prefs); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 234 | if (payload_state) |
| 235 | mock_system_state.set_payload_state(payload_state); |
| 236 | if (p2p_manager) |
| 237 | mock_system_state.set_p2p_manager(p2p_manager); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 238 | mock_system_state.set_request_params(¶ms); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 239 | OmahaRequestAction action(&mock_system_state, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 240 | NULL, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 241 | fetcher, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 242 | ping_only); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 243 | OmahaRequestActionTestProcessorDelegate delegate; |
| 244 | delegate.loop_ = loop; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 245 | delegate.expected_code_ = expected_code; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 246 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 247 | ActionProcessor processor; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 248 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 249 | processor.EnqueueAction(&action); |
| 250 | |
| 251 | OutputObjectCollectorAction collector_action; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 252 | BondActions(&action, &collector_action); |
| 253 | processor.EnqueueAction(&collector_action); |
| 254 | |
| 255 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 256 | g_main_loop_run(loop); |
| 257 | g_main_loop_unref(loop); |
| 258 | if (collector_action.has_input_object_ && out_response) |
| 259 | *out_response = collector_action.omaha_response_; |
| 260 | if (out_post_data) |
| 261 | *out_post_data = fetcher->post_data(); |
| 262 | return collector_action.has_input_object_; |
| 263 | } |
| 264 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 265 | // Tests Event requests -- they should always succeed. |out_post_data| |
| 266 | // may be null; if non-null, the post-data received by the mock |
| 267 | // HttpFetcher is returned. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 268 | void TestEvent(OmahaRequestParams params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 269 | OmahaEvent* event, |
| 270 | const string& http_response, |
| 271 | vector<char>* out_post_data) { |
| 272 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 273 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 274 | http_response.size(), |
| 275 | NULL); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 276 | MockSystemState mock_system_state; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 277 | mock_system_state.set_request_params(¶ms); |
| 278 | OmahaRequestAction action(&mock_system_state, event, fetcher, false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 279 | OmahaRequestActionTestProcessorDelegate delegate; |
| 280 | delegate.loop_ = loop; |
| 281 | ActionProcessor processor; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 282 | processor.set_delegate(&delegate); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 283 | processor.EnqueueAction(&action); |
| 284 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 285 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 286 | g_main_loop_run(loop); |
| 287 | g_main_loop_unref(loop); |
| 288 | if (out_post_data) |
| 289 | *out_post_data = fetcher->post_data(); |
| 290 | } |
| 291 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 292 | TEST(OmahaRequestActionTest, NoUpdateTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 293 | OmahaResponse response; |
| 294 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 295 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 296 | NULL, // payload_state |
| 297 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 298 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 299 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 300 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 301 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 302 | kErrorCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 303 | &response, |
| 304 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 305 | EXPECT_FALSE(response.update_exists); |
| 306 | } |
| 307 | |
| 308 | TEST(OmahaRequestActionTest, ValidUpdateTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 309 | OmahaResponse response; |
| 310 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 311 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 312 | NULL, // payload_state |
| 313 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 314 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 315 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 316 | "1.2.3.4", // version |
| 317 | "http://more/info", |
| 318 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 319 | "http://code/base/", // dl url |
| 320 | "file.signed", // file name |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 321 | "HASH1234=", // checksum |
| 322 | "false", // needs admin |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 323 | "123", // size |
| 324 | "20101020"), // deadline |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 325 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 326 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 327 | kErrorCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 328 | &response, |
| 329 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 330 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 331 | EXPECT_TRUE(response.update_exists); |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 332 | EXPECT_EQ("1.2.3.4", response.version); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 333 | EXPECT_EQ("http://code/base/file.signed", response.payload_urls[0]); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 334 | EXPECT_EQ("http://more/info", response.more_info_url); |
| 335 | EXPECT_EQ("HASH1234=", response.hash); |
| 336 | EXPECT_EQ(123, response.size); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 337 | EXPECT_TRUE(response.prompt); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 338 | EXPECT_EQ("20101020", response.deadline); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 341 | TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) { |
| 342 | OmahaResponse response; |
| 343 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 344 | params.set_update_disabled(true); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 345 | ASSERT_FALSE( |
| 346 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 347 | NULL, // payload_state |
| 348 | NULL, // p2p_manager |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 349 | params, |
| 350 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 351 | "1.2.3.4", // version |
| 352 | "http://more/info", |
| 353 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 354 | "http://code/base/", // dl url |
| 355 | "file.signed", // file name |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 356 | "HASH1234=", // checksum |
| 357 | "false", // needs admin |
| 358 | "123", // size |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 359 | ""), // deadline |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 360 | -1, |
| 361 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 362 | kErrorCodeOmahaUpdateIgnoredPerPolicy, |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 363 | &response, |
| 364 | NULL)); |
| 365 | EXPECT_FALSE(response.update_exists); |
| 366 | } |
| 367 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 368 | TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) { |
| 369 | OmahaResponse response; |
| 370 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 371 | params.set_update_disabled(true); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 372 | ASSERT_TRUE( |
| 373 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 374 | NULL, // payload_state |
| 375 | NULL, // p2p_manager |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 376 | params, |
| 377 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 378 | -1, |
| 379 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 380 | kErrorCodeSuccess, |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 381 | &response, |
| 382 | NULL)); |
| 383 | EXPECT_FALSE(response.update_exists); |
| 384 | } |
| 385 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 386 | TEST(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) { |
| 387 | OmahaResponse response; |
| 388 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 389 | params.set_wall_clock_based_wait_enabled(true); |
| 390 | params.set_update_check_count_wait_enabled(false); |
| 391 | params.set_waiting_period(TimeDelta::FromDays(2)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 392 | |
| 393 | string prefs_dir; |
| 394 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 395 | &prefs_dir)); |
| 396 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 397 | |
| 398 | Prefs prefs; |
| 399 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 400 | << "Failed to initialize preferences."; |
| 401 | |
| 402 | ASSERT_FALSE( |
| 403 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 404 | NULL, // payload_state |
| 405 | NULL, // p2p_manager |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 406 | params, |
| 407 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 408 | "1.2.3.4", // version |
| 409 | "http://more/info", |
| 410 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 411 | "http://code/base/", // dl url |
| 412 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 413 | "HASH1234=", // checksum |
| 414 | "false", // needs admin |
| 415 | "123", // size |
| 416 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 417 | "7", // max days to scatter |
| 418 | false, // disable_p2p_for_downloading |
| 419 | false), // disable_p2p_for sharing |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 420 | -1, |
| 421 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 422 | kErrorCodeOmahaUpdateDeferredPerPolicy, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 423 | &response, |
| 424 | NULL)); |
| 425 | EXPECT_FALSE(response.update_exists); |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 426 | |
| 427 | // Verify if we are interactive check we don't defer. |
| 428 | params.set_interactive(true); |
| 429 | ASSERT_TRUE( |
| 430 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 431 | NULL, // payload_state |
| 432 | NULL, // p2p_manager |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 433 | params, |
| 434 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 435 | "1.2.3.4", // version |
| 436 | "http://more/info", |
| 437 | "true", // prompt |
| 438 | "http://code/base/", // dl url |
| 439 | "file.signed", // file name |
| 440 | "HASH1234=", // checksum |
| 441 | "false", // needs admin |
| 442 | "123", // size |
| 443 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 444 | "7", // max days to scatter |
| 445 | false, // disable_p2p_for_downloading |
| 446 | false), // disable_p2p_for sharing |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 447 | -1, |
| 448 | false, // ping_only |
| 449 | kErrorCodeSuccess, |
| 450 | &response, |
| 451 | NULL)); |
| 452 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | TEST(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) { |
| 456 | OmahaResponse response; |
| 457 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 458 | params.set_wall_clock_based_wait_enabled(false); |
| 459 | params.set_waiting_period(TimeDelta::FromDays(2)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 460 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 461 | params.set_update_check_count_wait_enabled(true); |
| 462 | params.set_min_update_checks_needed(1); |
| 463 | params.set_max_update_checks_allowed(8); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 464 | |
| 465 | string prefs_dir; |
| 466 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 467 | &prefs_dir)); |
| 468 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 469 | |
| 470 | Prefs prefs; |
| 471 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 472 | << "Failed to initialize preferences."; |
| 473 | |
| 474 | ASSERT_TRUE( |
| 475 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 476 | NULL, // payload_state |
| 477 | NULL, // p2p_manager |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 478 | params, |
| 479 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 480 | "1.2.3.4", // version |
| 481 | "http://more/info", |
| 482 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 483 | "http://code/base/", // dl url |
| 484 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 485 | "HASH1234=", // checksum |
| 486 | "false", // needs admin |
| 487 | "123", // size |
| 488 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 489 | "7", // max days to scatter |
| 490 | false, // disable_p2p_for_downloading |
| 491 | false), // disable_p2p_for sharing |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 492 | -1, |
| 493 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 494 | kErrorCodeSuccess, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 495 | &response, |
| 496 | NULL)); |
| 497 | EXPECT_TRUE(response.update_exists); |
| 498 | } |
| 499 | |
| 500 | TEST(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) { |
| 501 | OmahaResponse response; |
| 502 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 503 | params.set_wall_clock_based_wait_enabled(true); |
| 504 | params.set_waiting_period(TimeDelta::FromDays(2)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 505 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 506 | params.set_update_check_count_wait_enabled(true); |
| 507 | params.set_min_update_checks_needed(1); |
| 508 | params.set_max_update_checks_allowed(8); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 509 | |
| 510 | string prefs_dir; |
| 511 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 512 | &prefs_dir)); |
| 513 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 514 | |
| 515 | Prefs prefs; |
| 516 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 517 | << "Failed to initialize preferences."; |
| 518 | |
| 519 | ASSERT_TRUE( |
| 520 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 521 | NULL, // payload_state |
| 522 | NULL, // p2p_manager |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 523 | params, |
| 524 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 525 | "1.2.3.4", // version |
| 526 | "http://more/info", |
| 527 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 528 | "http://code/base/", // dl url |
| 529 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 530 | "HASH1234=", // checksum |
| 531 | "false", // needs admin |
| 532 | "123", // size |
| 533 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 534 | "0", // max days to scatter |
| 535 | false, // disable_p2p_for_downloading |
| 536 | false), // disable_p2p_for sharing |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 537 | -1, |
| 538 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 539 | kErrorCodeSuccess, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 540 | &response, |
| 541 | NULL)); |
| 542 | EXPECT_TRUE(response.update_exists); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | TEST(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) { |
| 547 | OmahaResponse response; |
| 548 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 549 | params.set_wall_clock_based_wait_enabled(true); |
| 550 | params.set_waiting_period(TimeDelta()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 551 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 552 | params.set_update_check_count_wait_enabled(true); |
| 553 | params.set_min_update_checks_needed(0); |
| 554 | params.set_max_update_checks_allowed(0); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 555 | |
| 556 | string prefs_dir; |
| 557 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 558 | &prefs_dir)); |
| 559 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 560 | |
| 561 | Prefs prefs; |
| 562 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 563 | << "Failed to initialize preferences."; |
| 564 | |
| 565 | ASSERT_TRUE(TestUpdateCheck( |
| 566 | &prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 567 | NULL, // payload_state |
| 568 | NULL, // p2p_manager |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 569 | params, |
| 570 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 571 | "1.2.3.4", // version |
| 572 | "http://more/info", |
| 573 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 574 | "http://code/base/", // dl url |
| 575 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 576 | "HASH1234=", // checksum |
| 577 | "false", // needs admin |
| 578 | "123", // size |
| 579 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 580 | "7", // max days to scatter |
| 581 | false, // disable_p2p_for_downloading |
| 582 | false), // disable_p2p_for sharing |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 583 | -1, |
| 584 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 585 | kErrorCodeSuccess, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 586 | &response, |
| 587 | NULL)); |
| 588 | |
| 589 | int64 count; |
| 590 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count)); |
| 591 | ASSERT_TRUE(count == 0); |
| 592 | EXPECT_TRUE(response.update_exists); |
| 593 | } |
| 594 | |
| 595 | TEST(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) { |
| 596 | OmahaResponse response; |
| 597 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 598 | params.set_wall_clock_based_wait_enabled(true); |
| 599 | params.set_waiting_period(TimeDelta()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 600 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 601 | params.set_update_check_count_wait_enabled(true); |
| 602 | params.set_min_update_checks_needed(1); |
| 603 | params.set_max_update_checks_allowed(8); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 604 | |
| 605 | string prefs_dir; |
| 606 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 607 | &prefs_dir)); |
| 608 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 609 | |
| 610 | Prefs prefs; |
| 611 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 612 | << "Failed to initialize preferences."; |
| 613 | |
| 614 | ASSERT_FALSE(TestUpdateCheck( |
| 615 | &prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 616 | NULL, // payload_state |
| 617 | NULL, // p2p_manager |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 618 | params, |
| 619 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 620 | "1.2.3.4", // version |
| 621 | "http://more/info", |
| 622 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 623 | "http://code/base/", // dl url |
| 624 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 625 | "HASH1234=", // checksum |
| 626 | "false", // needs admin |
| 627 | "123", // size |
| 628 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 629 | "7", // max days to scatter |
| 630 | false, // disable_p2p_for_downloading |
| 631 | false), // disable_p2p_for sharing |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 632 | -1, |
| 633 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 634 | kErrorCodeOmahaUpdateDeferredPerPolicy, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 635 | &response, |
| 636 | NULL)); |
| 637 | |
| 638 | int64 count; |
| 639 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count)); |
| 640 | ASSERT_TRUE(count > 0); |
| 641 | EXPECT_FALSE(response.update_exists); |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 642 | |
| 643 | // Verify if we are interactive check we don't defer. |
| 644 | params.set_interactive(true); |
| 645 | ASSERT_TRUE( |
| 646 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 647 | NULL, // payload_state |
| 648 | NULL, // p2p_manager |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 649 | params, |
| 650 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 651 | "1.2.3.4", // version |
| 652 | "http://more/info", |
| 653 | "true", // prompt |
| 654 | "http://code/base/", // dl url |
| 655 | "file.signed", // file name |
| 656 | "HASH1234=", // checksum |
| 657 | "false", // needs admin |
| 658 | "123", // size |
| 659 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 660 | "7", // max days to scatter |
| 661 | false, // disable_p2p_for_downloading |
| 662 | false), // disable_p2p_for sharing |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 663 | -1, |
| 664 | false, // ping_only |
| 665 | kErrorCodeSuccess, |
| 666 | &response, |
| 667 | NULL)); |
| 668 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | TEST(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) { |
| 672 | OmahaResponse response; |
| 673 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 674 | params.set_wall_clock_based_wait_enabled(true); |
| 675 | params.set_waiting_period(TimeDelta()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 676 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 677 | params.set_update_check_count_wait_enabled(true); |
| 678 | params.set_min_update_checks_needed(1); |
| 679 | params.set_max_update_checks_allowed(8); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 680 | |
| 681 | string prefs_dir; |
| 682 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 683 | &prefs_dir)); |
| 684 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 685 | |
| 686 | Prefs prefs; |
| 687 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 688 | << "Failed to initialize preferences."; |
| 689 | |
| 690 | ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, 5)); |
| 691 | |
| 692 | ASSERT_FALSE(TestUpdateCheck( |
| 693 | &prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 694 | NULL, // payload_state |
| 695 | NULL, // p2p_manager |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 696 | params, |
| 697 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 698 | "1.2.3.4", // version |
| 699 | "http://more/info", |
| 700 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 701 | "http://code/base/", // dl url |
| 702 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 703 | "HASH1234=", // checksum |
| 704 | "false", // needs admin |
| 705 | "123", // size |
| 706 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 707 | "7", // max days to scatter |
| 708 | false, // disable_p2p_for_downloading |
| 709 | false), // disable_p2p_for sharing |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 710 | -1, |
| 711 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 712 | kErrorCodeOmahaUpdateDeferredPerPolicy, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 713 | &response, |
| 714 | NULL)); |
| 715 | |
| 716 | int64 count; |
| 717 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count)); |
| 718 | // count remains the same, as the decrementing happens in update_attempter |
| 719 | // which this test doesn't exercise. |
| 720 | ASSERT_TRUE(count == 5); |
| 721 | EXPECT_FALSE(response.update_exists); |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 722 | |
| 723 | // Verify if we are interactive check we don't defer. |
| 724 | params.set_interactive(true); |
| 725 | ASSERT_TRUE( |
| 726 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 727 | NULL, // payload_state |
| 728 | NULL, // p2p_manager |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 729 | params, |
| 730 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 731 | "1.2.3.4", // version |
| 732 | "http://more/info", |
| 733 | "true", // prompt |
| 734 | "http://code/base/", // dl url |
| 735 | "file.signed", // file name |
| 736 | "HASH1234=", // checksum |
| 737 | "false", // needs admin |
| 738 | "123", // size |
| 739 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 740 | "7", // max days to scatter |
| 741 | false, // disable_p2p_for_downloading |
| 742 | false), // disable_p2p_for sharing |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 743 | -1, |
| 744 | false, // ping_only |
| 745 | kErrorCodeSuccess, |
| 746 | &response, |
| 747 | NULL)); |
| 748 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 749 | } |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 750 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 751 | TEST(OmahaRequestActionTest, NoOutputPipeTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 752 | const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); |
| 753 | |
| 754 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 755 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 756 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 757 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 758 | mock_system_state.set_request_params(¶ms); |
| 759 | OmahaRequestAction action(&mock_system_state, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 760 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 761 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 762 | NULL), |
| 763 | false); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 764 | OmahaRequestActionTestProcessorDelegate delegate; |
| 765 | delegate.loop_ = loop; |
| 766 | ActionProcessor processor; |
| 767 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 768 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 769 | |
| 770 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 771 | g_main_loop_run(loop); |
| 772 | g_main_loop_unref(loop); |
| 773 | EXPECT_FALSE(processor.IsRunning()); |
| 774 | } |
| 775 | |
| 776 | TEST(OmahaRequestActionTest, InvalidXmlTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 777 | OmahaResponse response; |
| 778 | ASSERT_FALSE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 779 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 780 | NULL, // payload_state |
| 781 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 782 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 783 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 784 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 785 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 786 | kErrorCodeOmahaRequestXMLParseError, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 787 | &response, |
| 788 | NULL)); |
| 789 | EXPECT_FALSE(response.update_exists); |
| 790 | } |
| 791 | |
| 792 | TEST(OmahaRequestActionTest, EmptyResponseTest) { |
| 793 | OmahaResponse response; |
| 794 | ASSERT_FALSE( |
| 795 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 796 | NULL, // payload_state |
| 797 | NULL, // p2p_manager |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 798 | kDefaultTestParams, |
| 799 | "", |
| 800 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 801 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 802 | kErrorCodeOmahaRequestEmptyResponseError, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 803 | &response, |
| 804 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 805 | EXPECT_FALSE(response.update_exists); |
| 806 | } |
| 807 | |
| 808 | TEST(OmahaRequestActionTest, MissingStatusTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 809 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 810 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 811 | NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 812 | NULL, // payload_state |
| 813 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 814 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 815 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 816 | "<daystart elapsed_seconds=\"100\"/>" |
| 817 | "<app appid=\"foo\" status=\"ok\">" |
| 818 | "<ping status=\"ok\"/>" |
| 819 | "<updatecheck/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 820 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 821 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 822 | kErrorCodeOmahaResponseInvalid, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 823 | &response, |
| 824 | NULL)); |
| 825 | EXPECT_FALSE(response.update_exists); |
| 826 | } |
| 827 | |
| 828 | TEST(OmahaRequestActionTest, InvalidStatusTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 829 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 830 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 831 | NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 832 | NULL, // payload_state |
| 833 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 834 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 835 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 836 | "<daystart elapsed_seconds=\"100\"/>" |
| 837 | "<app appid=\"foo\" status=\"ok\">" |
| 838 | "<ping status=\"ok\"/>" |
| 839 | "<updatecheck status=\"InvalidStatusTest\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 840 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 841 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 842 | kErrorCodeOmahaResponseInvalid, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 843 | &response, |
| 844 | NULL)); |
| 845 | EXPECT_FALSE(response.update_exists); |
| 846 | } |
| 847 | |
| 848 | TEST(OmahaRequestActionTest, MissingNodesetTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 849 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 850 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 851 | NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 852 | NULL, // payload_state |
| 853 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 854 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 855 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 856 | "<daystart elapsed_seconds=\"100\"/>" |
| 857 | "<app appid=\"foo\" status=\"ok\">" |
| 858 | "<ping status=\"ok\"/>" |
| 859 | "</app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 860 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 861 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 862 | kErrorCodeOmahaResponseInvalid, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 863 | &response, |
| 864 | NULL)); |
| 865 | EXPECT_FALSE(response.update_exists); |
| 866 | } |
| 867 | |
| 868 | TEST(OmahaRequestActionTest, MissingFieldTest) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 869 | string input_response = |
| 870 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 871 | "<daystart elapsed_seconds=\"100\"/>" |
| 872 | "<app appid=\"xyz\" status=\"ok\">" |
| 873 | "<updatecheck status=\"ok\">" |
| 874 | "<urls><url codebase=\"http://missing/field/test/\"/></urls>" |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 875 | "<manifest version=\"10.2.3.4\">" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 876 | "<packages><package hash=\"not-used\" name=\"f\" " |
| 877 | "size=\"587\"/></packages>" |
| 878 | "<actions><action event=\"postinstall\" " |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 879 | "ChromeOSVersion=\"10.2.3.4\" " |
| 880 | "Prompt=\"false\" " |
| 881 | "IsDelta=\"true\" " |
Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 882 | "IsDeltaPayload=\"false\" " |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 883 | "sha256=\"lkq34j5345\" " |
| 884 | "needsadmin=\"true\" " |
| 885 | "/></actions></manifest></updatecheck></app></response>"; |
| 886 | LOG(INFO) << "Input Response = " << input_response; |
| 887 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 888 | OmahaResponse response; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 889 | ASSERT_TRUE(TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 890 | NULL, // payload_state |
| 891 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 892 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 893 | input_response, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 894 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 895 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 896 | kErrorCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 897 | &response, |
| 898 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 899 | EXPECT_TRUE(response.update_exists); |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 900 | EXPECT_EQ("10.2.3.4", response.version); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 901 | EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 902 | EXPECT_EQ("", response.more_info_url); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 903 | EXPECT_EQ("lkq34j5345", response.hash); |
| 904 | EXPECT_EQ(587, response.size); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 905 | EXPECT_FALSE(response.prompt); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 906 | EXPECT_TRUE(response.deadline.empty()); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | namespace { |
| 910 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 911 | public: |
| 912 | void ProcessingStopped(const ActionProcessor* processor) { |
| 913 | ASSERT_TRUE(loop_); |
| 914 | g_main_loop_quit(loop_); |
| 915 | } |
| 916 | GMainLoop *loop_; |
| 917 | }; |
| 918 | |
| 919 | gboolean TerminateTransferTestStarter(gpointer data) { |
| 920 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 921 | processor->StartProcessing(); |
| 922 | CHECK(processor->IsRunning()); |
| 923 | processor->StopProcessing(); |
| 924 | return FALSE; |
| 925 | } |
| 926 | } // namespace {} |
| 927 | |
| 928 | TEST(OmahaRequestActionTest, TerminateTransferTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 929 | string http_response("doesn't matter"); |
| 930 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 931 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 932 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 933 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 934 | mock_system_state.set_request_params(¶ms); |
| 935 | OmahaRequestAction action(&mock_system_state, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 936 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 937 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 938 | NULL), |
| 939 | false); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 940 | TerminateEarlyTestProcessorDelegate delegate; |
| 941 | delegate.loop_ = loop; |
| 942 | ActionProcessor processor; |
| 943 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 944 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 945 | |
| 946 | g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
| 947 | g_main_loop_run(loop); |
| 948 | g_main_loop_unref(loop); |
| 949 | } |
| 950 | |
| 951 | TEST(OmahaRequestActionTest, XmlEncodeTest) { |
| 952 | EXPECT_EQ("ab", XmlEncode("ab")); |
| 953 | EXPECT_EQ("a<b", XmlEncode("a<b")); |
| 954 | EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
| 955 | EXPECT_EQ("<&>", XmlEncode("<&>")); |
| 956 | EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>")); |
| 957 | |
| 958 | vector<char> post_data; |
| 959 | |
| 960 | // Make sure XML Encode is being called on the params |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 961 | MockSystemState mock_system_state; |
| 962 | OmahaRequestParams params(&mock_system_state, |
| 963 | OmahaRequestParams::kOsPlatform, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 964 | OmahaRequestParams::kOsVersion, |
| 965 | "testtheservice_pack>", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 966 | "x86 generic<id", |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 967 | OmahaRequestParams::kAppId, |
| 968 | "0.1.0.0", |
| 969 | "en-US", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 970 | "unittest_track<", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 971 | "<OEM MODEL>", |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 972 | "ChromeOSFirmware.1.0", |
| 973 | "EC100", |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 974 | false, // delta okay |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 975 | false, // interactive |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 976 | "http://url", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 977 | false, // update_disabled |
| 978 | "", // target_version_prefix |
| 979 | false, // use_p2p_for_downloading |
| 980 | false); // use_p2p_for_sharing |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 981 | OmahaResponse response; |
| 982 | ASSERT_FALSE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 983 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 984 | NULL, // payload_state |
| 985 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 986 | params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 987 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 988 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 989 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 990 | kErrorCodeOmahaRequestXMLParseError, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 991 | &response, |
| 992 | &post_data)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 993 | // convert post_data to string |
| 994 | string post_str(&post_data[0], post_data.size()); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 995 | EXPECT_NE(post_str.find("testtheservice_pack>"), string::npos); |
| 996 | EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 997 | EXPECT_NE(post_str.find("x86 generic<id"), string::npos); |
| 998 | EXPECT_EQ(post_str.find("x86 generic<id"), string::npos); |
| 999 | EXPECT_NE(post_str.find("unittest_track&lt;"), string::npos); |
| 1000 | EXPECT_EQ(post_str.find("unittest_track<"), string::npos); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1001 | EXPECT_NE(post_str.find("<OEM MODEL>"), string::npos); |
| 1002 | EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | TEST(OmahaRequestActionTest, XmlDecodeTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1006 | OmahaResponse response; |
| 1007 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1008 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1009 | NULL, // payload_state |
| 1010 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1011 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1012 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 1013 | "1.2.3.4", // version |
| 1014 | "testthe<url", // more info |
| 1015 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1016 | "testthe&codebase/", // dl url |
| 1017 | "file.signed", // file name |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1018 | "HASH1234=", // checksum |
| 1019 | "false", // needs admin |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 1020 | "123", // size |
| 1021 | "<20110101"), // deadline |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1022 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1023 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1024 | kErrorCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1025 | &response, |
| 1026 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1027 | |
| 1028 | EXPECT_EQ(response.more_info_url, "testthe<url"); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1029 | EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed"); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 1030 | EXPECT_EQ(response.deadline, "<20110101"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | TEST(OmahaRequestActionTest, ParseIntTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1034 | OmahaResponse response; |
| 1035 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1036 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1037 | NULL, // payload_state |
| 1038 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1039 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1040 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 1041 | "1.2.3.4", // version |
| 1042 | "theurl", // more info |
| 1043 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1044 | "thecodebase/", // dl url |
| 1045 | "file.signed", // file name |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1046 | "HASH1234=", // checksum |
| 1047 | "false", // needs admin |
| 1048 | // overflows int32: |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 1049 | "123123123123123", // size |
| 1050 | "deadline"), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1051 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1052 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1053 | kErrorCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1054 | &response, |
| 1055 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1056 | |
| 1057 | EXPECT_EQ(response.size, 123123123123123ll); |
| 1058 | } |
| 1059 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1060 | TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { |
| 1061 | vector<char> post_data; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1062 | NiceMock<PrefsMock> prefs; |
| 1063 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) |
| 1064 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1065 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1066 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1067 | NULL, // payload_state |
| 1068 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1069 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1070 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1071 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1072 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1073 | kErrorCodeOmahaRequestXMLParseError, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1074 | NULL, // response |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1075 | &post_data)); |
| 1076 | // convert post_data to string |
| 1077 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1078 | EXPECT_NE(post_str.find( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1079 | " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1080 | " <updatecheck targetversionprefix=\"\"></updatecheck>\n"), |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1081 | string::npos); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1082 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), |
| 1083 | string::npos); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1084 | EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), |
| 1085 | string::npos); |
| 1086 | EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), |
| 1087 | string::npos); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1090 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1091 | TEST(OmahaRequestActionTest, FormatUpdateDisabledOutputTest) { |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1092 | vector<char> post_data; |
| 1093 | NiceMock<PrefsMock> prefs; |
| 1094 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1095 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1096 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1097 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1098 | params.set_update_disabled(true); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1099 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1100 | NULL, // payload_state |
| 1101 | NULL, // p2p_manager |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1102 | params, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1103 | "invalid xml>", |
| 1104 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1105 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1106 | kErrorCodeOmahaRequestXMLParseError, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1107 | NULL, // response |
| 1108 | &post_data)); |
| 1109 | // convert post_data to string |
| 1110 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1111 | EXPECT_NE(post_str.find( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1112 | " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1113 | " <updatecheck targetversionprefix=\"\"></updatecheck>\n"), |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1114 | string::npos); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1115 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), |
| 1116 | string::npos); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1117 | EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), |
| 1118 | string::npos); |
| 1119 | EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), |
| 1120 | string::npos); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1123 | TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) { |
| 1124 | vector<char> post_data; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1125 | TestEvent(kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1126 | new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted), |
| 1127 | "invalid xml>", |
| 1128 | &post_data); |
| 1129 | // convert post_data to string |
| 1130 | string post_str(&post_data[0], post_data.size()); |
| 1131 | string expected_event = StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1132 | " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n", |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1133 | OmahaEvent::kTypeUpdateDownloadStarted, |
| 1134 | OmahaEvent::kResultSuccess); |
| 1135 | EXPECT_NE(post_str.find(expected_event), string::npos); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1136 | EXPECT_EQ(post_str.find("ping"), string::npos); |
| 1137 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) { |
| 1141 | vector<char> post_data; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1142 | TestEvent(kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1143 | new OmahaEvent(OmahaEvent::kTypeDownloadComplete, |
| 1144 | OmahaEvent::kResultError, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1145 | kErrorCodeError), |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1146 | "invalid xml>", |
| 1147 | &post_data); |
| 1148 | // convert post_data to string |
| 1149 | string post_str(&post_data[0], post_data.size()); |
| 1150 | string expected_event = StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1151 | " <event eventtype=\"%d\" eventresult=\"%d\" " |
| 1152 | "errorcode=\"%d\"></event>\n", |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1153 | OmahaEvent::kTypeDownloadComplete, |
| 1154 | OmahaEvent::kResultError, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1155 | kErrorCodeError); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1156 | EXPECT_NE(post_str.find(expected_event), string::npos); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1157 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | TEST(OmahaRequestActionTest, IsEventTest) { |
| 1161 | string http_response("doesn't matter"); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1162 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1163 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1164 | mock_system_state.set_request_params(¶ms); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1165 | OmahaRequestAction update_check_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1166 | &mock_system_state, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1167 | NULL, |
| 1168 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 1169 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1170 | NULL), |
| 1171 | false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1172 | EXPECT_FALSE(update_check_action.IsEvent()); |
| 1173 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1174 | params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1175 | mock_system_state.set_request_params(¶ms); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1176 | OmahaRequestAction event_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1177 | &mock_system_state, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1178 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1179 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 1180 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1181 | NULL), |
| 1182 | false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1183 | EXPECT_TRUE(event_action.IsEvent()); |
| 1184 | } |
| 1185 | |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1186 | TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) { |
| 1187 | for (int i = 0; i < 2; i++) { |
| 1188 | bool delta_okay = i == 1; |
| 1189 | const char* delta_okay_str = delta_okay ? "true" : "false"; |
| 1190 | vector<char> post_data; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1191 | MockSystemState mock_system_state; |
| 1192 | OmahaRequestParams params(&mock_system_state, |
| 1193 | OmahaRequestParams::kOsPlatform, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1194 | OmahaRequestParams::kOsVersion, |
| 1195 | "service_pack", |
| 1196 | "x86-generic", |
| 1197 | OmahaRequestParams::kAppId, |
| 1198 | "0.1.0.0", |
| 1199 | "en-US", |
| 1200 | "unittest_track", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1201 | "OEM MODEL REV 1234", |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1202 | "ChromeOSFirmware.1.0", |
| 1203 | "EC100", |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1204 | delta_okay, |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1205 | false, // interactive |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1206 | "http://url", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1207 | false, // update_disabled |
| 1208 | "", // target_version_prefix |
| 1209 | false, // use_p2p_for_downloading |
| 1210 | false); // use_p2p_for_sharing |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1211 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1212 | NULL, // payload_state |
| 1213 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1214 | params, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1215 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1216 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1217 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1218 | kErrorCodeOmahaRequestXMLParseError, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1219 | NULL, |
| 1220 | &post_data)); |
| 1221 | // convert post_data to string |
| 1222 | string post_str(&post_data[0], post_data.size()); |
| 1223 | EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)), |
| 1224 | string::npos) |
| 1225 | << "i = " << i; |
| 1226 | } |
| 1227 | } |
| 1228 | |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1229 | TEST(OmahaRequestActionTest, FormatInteractiveOutputTest) { |
| 1230 | for (int i = 0; i < 2; i++) { |
| 1231 | bool interactive = i == 1; |
Gilad Arnold | 8a659d8 | 2013-01-24 11:26:00 -0800 | [diff] [blame] | 1232 | const char* interactive_str = interactive ? "ondemandupdate" : "scheduler"; |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1233 | vector<char> post_data; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1234 | MockSystemState mock_system_state; |
| 1235 | OmahaRequestParams params(&mock_system_state, |
| 1236 | OmahaRequestParams::kOsPlatform, |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1237 | OmahaRequestParams::kOsVersion, |
| 1238 | "service_pack", |
| 1239 | "x86-generic", |
| 1240 | OmahaRequestParams::kAppId, |
| 1241 | "0.1.0.0", |
| 1242 | "en-US", |
| 1243 | "unittest_track", |
| 1244 | "OEM MODEL REV 1234", |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 1245 | "ChromeOSFirmware.1.0", |
| 1246 | "EC100", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1247 | true, // delta_okay |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1248 | interactive, |
| 1249 | "http://url", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1250 | false, // update_disabled |
| 1251 | "", // target_version_prefix |
| 1252 | false, // use_p2p_for_downloading |
| 1253 | false); // use_p2p_for_sharing |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1254 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1255 | NULL, // payload_state |
| 1256 | NULL, // p2p_manager |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1257 | params, |
| 1258 | "invalid xml>", |
| 1259 | -1, |
| 1260 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1261 | kErrorCodeOmahaRequestXMLParseError, |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1262 | NULL, |
| 1263 | &post_data)); |
| 1264 | // convert post_data to string |
| 1265 | string post_str(&post_data[0], post_data.size()); |
Gilad Arnold | 8a659d8 | 2013-01-24 11:26:00 -0800 | [diff] [blame] | 1266 | EXPECT_NE(post_str.find(StringPrintf("installsource=\"%s\"", |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 1267 | interactive_str)), |
| 1268 | string::npos) |
| 1269 | << "i = " << i; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1273 | TEST(OmahaRequestActionTest, OmahaEventTest) { |
| 1274 | OmahaEvent default_event; |
| 1275 | EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type); |
| 1276 | EXPECT_EQ(OmahaEvent::kResultError, default_event.result); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1277 | EXPECT_EQ(kErrorCodeError, default_event.error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1278 | |
| 1279 | OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted); |
| 1280 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type); |
| 1281 | EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1282 | EXPECT_EQ(kErrorCodeSuccess, success_event.error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1283 | |
| 1284 | OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished, |
| 1285 | OmahaEvent::kResultError, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1286 | kErrorCodeError); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1287 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type); |
| 1288 | EXPECT_EQ(OmahaEvent::kResultError, error_event.result); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1289 | EXPECT_EQ(kErrorCodeError, error_event.error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1292 | TEST(OmahaRequestActionTest, PingTest) { |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1293 | for (int ping_only = 0; ping_only < 2; ping_only++) { |
| 1294 | NiceMock<PrefsMock> prefs; |
| 1295 | // Add a few hours to the day difference to test no rounding, etc. |
| 1296 | int64_t five_days_ago = |
| 1297 | (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue(); |
| 1298 | int64_t six_days_ago = |
| 1299 | (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue(); |
| 1300 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1301 | .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true))); |
| 1302 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1303 | .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true))); |
| 1304 | vector<char> post_data; |
| 1305 | ASSERT_TRUE( |
| 1306 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1307 | NULL, // payload_state |
| 1308 | NULL, // p2p_manager |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1309 | kDefaultTestParams, |
| 1310 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 1311 | -1, |
| 1312 | ping_only, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1313 | kErrorCodeSuccess, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1314 | NULL, |
| 1315 | &post_data)); |
| 1316 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1317 | EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"), |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1318 | string::npos); |
| 1319 | if (ping_only) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1320 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1321 | EXPECT_EQ(post_str.find("previousversion"), string::npos); |
| 1322 | } else { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1323 | EXPECT_NE(post_str.find("updatecheck"), string::npos); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1324 | EXPECT_NE(post_str.find("previousversion"), string::npos); |
| 1325 | } |
| 1326 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | TEST(OmahaRequestActionTest, ActivePingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1330 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1331 | int64_t three_days_ago = |
| 1332 | (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue(); |
| 1333 | int64_t now = Time::Now().ToInternalValue(); |
| 1334 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1335 | .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true))); |
| 1336 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1337 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1338 | vector<char> post_data; |
| 1339 | ASSERT_TRUE( |
| 1340 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1341 | NULL, // payload_state |
| 1342 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1343 | kDefaultTestParams, |
| 1344 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1345 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1346 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1347 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1348 | NULL, |
| 1349 | &post_data)); |
| 1350 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1351 | EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1352 | string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | TEST(OmahaRequestActionTest, RollCallPingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1356 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1357 | int64_t four_days_ago = |
| 1358 | (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue(); |
| 1359 | int64_t now = Time::Now().ToInternalValue(); |
| 1360 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1361 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1362 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1363 | .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true))); |
| 1364 | vector<char> post_data; |
| 1365 | ASSERT_TRUE( |
| 1366 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1367 | NULL, // payload_state |
| 1368 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1369 | kDefaultTestParams, |
| 1370 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1371 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1372 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1373 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1374 | NULL, |
| 1375 | &post_data)); |
| 1376 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1377 | EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1378 | string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | TEST(OmahaRequestActionTest, NoPingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1382 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1383 | int64_t one_hour_ago = |
| 1384 | (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue(); |
| 1385 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1386 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); |
| 1387 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1388 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); |
| 1389 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1390 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1391 | vector<char> post_data; |
| 1392 | ASSERT_TRUE( |
| 1393 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1394 | NULL, // payload_state |
| 1395 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1396 | kDefaultTestParams, |
| 1397 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1398 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1399 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1400 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1401 | NULL, |
| 1402 | &post_data)); |
| 1403 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1404 | EXPECT_EQ(post_str.find("ping"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1407 | TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) { |
| 1408 | // This test ensures that we ignore empty ping only requests. |
| 1409 | NiceMock<PrefsMock> prefs; |
| 1410 | int64_t now = Time::Now().ToInternalValue(); |
| 1411 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1412 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1413 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1414 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1415 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1416 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1417 | vector<char> post_data; |
| 1418 | EXPECT_TRUE( |
| 1419 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1420 | NULL, // payload_state |
| 1421 | NULL, // p2p_manager |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1422 | kDefaultTestParams, |
| 1423 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 1424 | -1, |
| 1425 | true, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1426 | kErrorCodeSuccess, |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1427 | NULL, |
| 1428 | &post_data)); |
| 1429 | EXPECT_EQ(post_data.size(), 0); |
| 1430 | } |
| 1431 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1432 | TEST(OmahaRequestActionTest, BackInTimePingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1433 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1434 | int64_t future = |
| 1435 | (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue(); |
| 1436 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1437 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); |
| 1438 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1439 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); |
| 1440 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) |
| 1441 | .WillOnce(Return(true)); |
| 1442 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) |
| 1443 | .WillOnce(Return(true)); |
| 1444 | vector<char> post_data; |
| 1445 | ASSERT_TRUE( |
| 1446 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1447 | NULL, // payload_state |
| 1448 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1449 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1450 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1451 | "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1452 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1453 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1454 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1455 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1456 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1457 | NULL, |
| 1458 | &post_data)); |
| 1459 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1460 | EXPECT_EQ(post_str.find("ping"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | TEST(OmahaRequestActionTest, LastPingDayUpdateTest) { |
| 1464 | // 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] | 1465 | // minus 200 seconds with a slack of 5 seconds. Therefore, the test |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1466 | // may fail if it runs for longer than 5 seconds. It shouldn't run |
| 1467 | // that long though. |
| 1468 | int64_t midnight = |
| 1469 | (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue(); |
| 1470 | int64_t midnight_slack = |
| 1471 | (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue(); |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1472 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1473 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, |
| 1474 | AllOf(Ge(midnight), Le(midnight_slack)))) |
| 1475 | .WillOnce(Return(true)); |
| 1476 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, |
| 1477 | AllOf(Ge(midnight), Le(midnight_slack)))) |
| 1478 | .WillOnce(Return(true)); |
| 1479 | ASSERT_TRUE( |
| 1480 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1481 | NULL, // payload_state |
| 1482 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1483 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1484 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1485 | "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1486 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1487 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1488 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1489 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1490 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1491 | NULL, |
| 1492 | NULL)); |
| 1493 | } |
| 1494 | |
| 1495 | TEST(OmahaRequestActionTest, NoElapsedSecondsTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1496 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1497 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1498 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1499 | ASSERT_TRUE( |
| 1500 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1501 | NULL, // payload_state |
| 1502 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1503 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1504 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1505 | "protocol=\"3.0\"><daystart blah=\"200\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1506 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1507 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1508 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1509 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1510 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1511 | NULL, |
| 1512 | NULL)); |
| 1513 | } |
| 1514 | |
| 1515 | TEST(OmahaRequestActionTest, BadElapsedSecondsTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1516 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1517 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1518 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1519 | ASSERT_TRUE( |
| 1520 | TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1521 | NULL, // payload_state |
| 1522 | NULL, // p2p_manager |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1523 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1524 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1525 | "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1526 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1527 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1528 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1529 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1530 | kErrorCodeSuccess, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1531 | NULL, |
| 1532 | NULL)); |
| 1533 | } |
| 1534 | |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1535 | TEST(OmahaRequestActionTest, NoUniqueIDTest) { |
| 1536 | vector<char> post_data; |
| 1537 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1538 | NULL, // payload_state |
| 1539 | NULL, // p2p_manager |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1540 | kDefaultTestParams, |
| 1541 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1542 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1543 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1544 | kErrorCodeOmahaRequestXMLParseError, |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1545 | NULL, // response |
| 1546 | &post_data)); |
| 1547 | // convert post_data to string |
| 1548 | string post_str(&post_data[0], post_data.size()); |
| 1549 | EXPECT_EQ(post_str.find("machineid="), string::npos); |
| 1550 | EXPECT_EQ(post_str.find("userid="), string::npos); |
| 1551 | } |
| 1552 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1553 | TEST(OmahaRequestActionTest, NetworkFailureTest) { |
| 1554 | OmahaResponse response; |
| 1555 | ASSERT_FALSE( |
| 1556 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1557 | NULL, // payload_state |
| 1558 | NULL, // p2p_manager |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1559 | kDefaultTestParams, |
| 1560 | "", |
| 1561 | 501, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1562 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1563 | static_cast<ErrorCode>( |
| 1564 | kErrorCodeOmahaRequestHTTPResponseBase + 501), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1565 | &response, |
| 1566 | NULL)); |
| 1567 | EXPECT_FALSE(response.update_exists); |
| 1568 | } |
| 1569 | |
| 1570 | TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) { |
| 1571 | OmahaResponse response; |
| 1572 | ASSERT_FALSE( |
| 1573 | TestUpdateCheck(NULL, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1574 | NULL, // payload_state |
| 1575 | NULL, // p2p_manager |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1576 | kDefaultTestParams, |
| 1577 | "", |
| 1578 | 1500, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1579 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1580 | static_cast<ErrorCode>( |
| 1581 | kErrorCodeOmahaRequestHTTPResponseBase + 999), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1582 | &response, |
| 1583 | NULL)); |
| 1584 | EXPECT_FALSE(response.update_exists); |
| 1585 | } |
| 1586 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1587 | TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) { |
| 1588 | OmahaResponse response; |
| 1589 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1590 | params.set_wall_clock_based_wait_enabled(true); |
| 1591 | params.set_waiting_period(TimeDelta().FromDays(1)); |
| 1592 | params.set_update_check_count_wait_enabled(false); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1593 | |
| 1594 | string prefs_dir; |
| 1595 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 1596 | &prefs_dir)); |
| 1597 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 1598 | |
| 1599 | Prefs prefs; |
| 1600 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 1601 | << "Failed to initialize preferences."; |
| 1602 | |
| 1603 | ASSERT_FALSE(TestUpdateCheck( |
| 1604 | &prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1605 | NULL, // payload_state |
| 1606 | NULL, // p2p_manager |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1607 | params, |
| 1608 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 1609 | "1.2.3.4", // version |
| 1610 | "http://more/info", |
| 1611 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1612 | "http://code/base/", // dl url |
| 1613 | "file.signed", // file name |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1614 | "HASH1234=", // checksum |
| 1615 | "false", // needs admin |
| 1616 | "123", // size |
| 1617 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1618 | "7", // max days to scatter |
| 1619 | false, // disable_p2p_for_downloading |
| 1620 | false), // disable_p2p_for sharing |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1621 | -1, |
| 1622 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1623 | kErrorCodeOmahaUpdateDeferredPerPolicy, |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1624 | &response, |
| 1625 | NULL)); |
| 1626 | |
| 1627 | int64 timestamp = 0; |
| 1628 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); |
| 1629 | ASSERT_TRUE(timestamp > 0); |
| 1630 | EXPECT_FALSE(response.update_exists); |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1631 | |
| 1632 | // Verify if we are interactive check we don't defer. |
| 1633 | params.set_interactive(true); |
| 1634 | ASSERT_TRUE( |
| 1635 | TestUpdateCheck(&prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1636 | NULL, // payload_state |
| 1637 | NULL, // p2p_manager |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1638 | params, |
| 1639 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 1640 | "1.2.3.4", // version |
| 1641 | "http://more/info", |
| 1642 | "true", // prompt |
| 1643 | "http://code/base/", // dl url |
| 1644 | "file.signed", // file name |
| 1645 | "HASH1234=", // checksum |
| 1646 | "false", // needs admin |
| 1647 | "123", // size |
| 1648 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1649 | "7", // max days to scatter |
| 1650 | false, // disable_p2p_for_downloading |
| 1651 | false), // disable_p2p_for sharing |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1652 | -1, |
| 1653 | false, // ping_only |
| 1654 | kErrorCodeSuccess, |
| 1655 | &response, |
| 1656 | NULL)); |
| 1657 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) { |
| 1661 | OmahaResponse response; |
| 1662 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1663 | params.set_wall_clock_based_wait_enabled(true); |
| 1664 | params.set_waiting_period(TimeDelta().FromDays(1)); |
| 1665 | params.set_update_check_count_wait_enabled(false); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1666 | |
| 1667 | string prefs_dir; |
| 1668 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 1669 | &prefs_dir)); |
| 1670 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 1671 | |
| 1672 | Prefs prefs; |
| 1673 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 1674 | << "Failed to initialize preferences."; |
| 1675 | |
| 1676 | // Set the timestamp to a very old value such that it exceeds the |
| 1677 | // waiting period set above. |
| 1678 | Time t1; |
| 1679 | Time::FromString("1/1/2012", &t1); |
| 1680 | ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue())); |
| 1681 | ASSERT_TRUE(TestUpdateCheck( |
| 1682 | &prefs, // prefs |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1683 | NULL, // payload_state |
| 1684 | NULL, // p2p_manager |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1685 | params, |
| 1686 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 1687 | "1.2.3.4", // version |
| 1688 | "http://more/info", |
| 1689 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1690 | "http://code/base/", // dl url |
| 1691 | "file.signed", // file name |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1692 | "HASH1234=", // checksum |
| 1693 | "false", // needs admin |
| 1694 | "123", // size |
| 1695 | "", // deadline |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1696 | "7", // max days to scatter |
| 1697 | false, // disable_p2p_for_downloading |
| 1698 | false), // disable_p2p_for sharing |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1699 | -1, |
| 1700 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1701 | kErrorCodeSuccess, |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1702 | &response, |
| 1703 | NULL)); |
| 1704 | |
| 1705 | EXPECT_TRUE(response.update_exists); |
| 1706 | |
| 1707 | // Make sure the timestamp t1 is unchanged showing that it was reused. |
| 1708 | int64 timestamp = 0; |
| 1709 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); |
| 1710 | ASSERT_TRUE(timestamp == t1.ToInternalValue()); |
| 1711 | } |
| 1712 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1713 | TEST(OmahaRequestActionTest, TestChangingToMoreStableChannel) { |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1714 | // Create a uniquely named test directory. |
| 1715 | string test_dir; |
| 1716 | ASSERT_TRUE(utils::MakeTempDirectory( |
| 1717 | "omaha_request_action-test-XXXXXX", &test_dir)); |
| 1718 | |
| 1719 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc")); |
| 1720 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1721 | kStatefulPartition + "/etc")); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1722 | vector<char> post_data; |
| 1723 | NiceMock<PrefsMock> prefs; |
| 1724 | ASSERT_TRUE(WriteFileString( |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1725 | test_dir + "/etc/lsb-release", |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1726 | "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n" |
| 1727 | "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n" |
| 1728 | "CHROMEOS_RELEASE_TRACK=canary-channel\n")); |
| 1729 | ASSERT_TRUE(WriteFileString( |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1730 | test_dir + kStatefulPartition + "/etc/lsb-release", |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1731 | "CHROMEOS_IS_POWERWASH_ALLOWED=true\n" |
| 1732 | "CHROMEOS_RELEASE_TRACK=stable-channel\n")); |
| 1733 | OmahaRequestParams params = kDefaultTestParams; |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame^] | 1734 | params.set_root(test_dir); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1735 | params.SetLockDown(false); |
| 1736 | params.Init("1.2.3.4", "", 0); |
| 1737 | EXPECT_EQ("canary-channel", params.current_channel()); |
| 1738 | EXPECT_EQ("stable-channel", params.target_channel()); |
| 1739 | EXPECT_TRUE(params.to_more_stable_channel()); |
| 1740 | EXPECT_TRUE(params.is_powerwash_allowed()); |
| 1741 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1742 | NULL, // payload_state |
| 1743 | NULL, // p2p_manager |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1744 | params, |
| 1745 | "invalid xml>", |
| 1746 | -1, |
| 1747 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1748 | kErrorCodeOmahaRequestXMLParseError, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1749 | NULL, // response |
| 1750 | &post_data)); |
| 1751 | // convert post_data to string |
| 1752 | string post_str(&post_data[0], post_data.size()); |
| 1753 | EXPECT_NE(string::npos, post_str.find( |
| 1754 | "appid=\"{22222222-2222-2222-2222-222222222222}\" " |
| 1755 | "version=\"0.0.0.0\" from_version=\"1.2.3.4\" " |
| 1756 | "track=\"stable-channel\" from_track=\"canary-channel\" ")); |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1757 | |
| 1758 | ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | TEST(OmahaRequestActionTest, TestChangingToLessStableChannel) { |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1762 | // Create a uniquely named test directory. |
| 1763 | string test_dir; |
| 1764 | ASSERT_TRUE(utils::MakeTempDirectory( |
| 1765 | "omaha_request_action-test-XXXXXX", &test_dir)); |
| 1766 | |
| 1767 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc")); |
| 1768 | ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1769 | kStatefulPartition + "/etc")); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1770 | vector<char> post_data; |
| 1771 | NiceMock<PrefsMock> prefs; |
| 1772 | ASSERT_TRUE(WriteFileString( |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1773 | test_dir + "/etc/lsb-release", |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1774 | "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n" |
| 1775 | "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n" |
| 1776 | "CHROMEOS_RELEASE_TRACK=stable-channel\n")); |
| 1777 | ASSERT_TRUE(WriteFileString( |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1778 | test_dir + kStatefulPartition + "/etc/lsb-release", |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1779 | "CHROMEOS_RELEASE_TRACK=canary-channel\n")); |
| 1780 | OmahaRequestParams params = kDefaultTestParams; |
Gilad Arnold | d04f8e2 | 2014-01-09 13:13:40 -0800 | [diff] [blame^] | 1781 | params.set_root(test_dir); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1782 | params.SetLockDown(false); |
| 1783 | params.Init("5.6.7.8", "", 0); |
| 1784 | EXPECT_EQ("stable-channel", params.current_channel()); |
| 1785 | EXPECT_EQ("canary-channel", params.target_channel()); |
| 1786 | EXPECT_FALSE(params.to_more_stable_channel()); |
| 1787 | EXPECT_FALSE(params.is_powerwash_allowed()); |
| 1788 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1789 | NULL, // payload_state |
| 1790 | NULL, // p2p_manager |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1791 | params, |
| 1792 | "invalid xml>", |
| 1793 | -1, |
| 1794 | false, // ping_only |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1795 | kErrorCodeOmahaRequestXMLParseError, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1796 | NULL, // response |
| 1797 | &post_data)); |
| 1798 | // convert post_data to string |
| 1799 | string post_str(&post_data[0], post_data.size()); |
| 1800 | EXPECT_NE(string::npos, post_str.find( |
| 1801 | "appid=\"{11111111-1111-1111-1111-111111111111}\" " |
| 1802 | "version=\"5.6.7.8\" " |
| 1803 | "track=\"canary-channel\" from_track=\"stable-channel\"")); |
| 1804 | EXPECT_EQ(string::npos, post_str.find( "from_version")); |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 1805 | |
| 1806 | ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1809 | void P2PTest(bool initial_allow_p2p_for_downloading, |
| 1810 | bool initial_allow_p2p_for_sharing, |
| 1811 | bool omaha_disable_p2p_for_downloading, |
| 1812 | bool omaha_disable_p2p_for_sharing, |
| 1813 | bool payload_state_allow_p2p_attempt, |
| 1814 | bool expect_p2p_client_lookup, |
| 1815 | const string& p2p_client_result_url, |
| 1816 | bool expected_allow_p2p_for_downloading, |
| 1817 | bool expected_allow_p2p_for_sharing, |
| 1818 | const string& expected_p2p_url) { |
| 1819 | OmahaResponse response; |
| 1820 | OmahaRequestParams request_params = kDefaultTestParams; |
| 1821 | request_params.set_use_p2p_for_downloading(initial_allow_p2p_for_downloading); |
| 1822 | request_params.set_use_p2p_for_sharing(initial_allow_p2p_for_sharing); |
| 1823 | |
| 1824 | MockPayloadState mock_payload_state; |
| 1825 | EXPECT_CALL(mock_payload_state, P2PAttemptAllowed()) |
| 1826 | .WillRepeatedly(Return(payload_state_allow_p2p_attempt)); |
| 1827 | MockP2PManager mock_p2p_manager; |
| 1828 | mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url); |
| 1829 | |
| 1830 | EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, _, _)) |
| 1831 | .Times(expect_p2p_client_lookup ? 1 : 0); |
| 1832 | |
| 1833 | ASSERT_TRUE( |
| 1834 | TestUpdateCheck(NULL, // prefs |
| 1835 | &mock_payload_state, |
| 1836 | &mock_p2p_manager, |
| 1837 | request_params, |
| 1838 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 1839 | "1.2.3.4", // version |
| 1840 | "http://more/info", |
| 1841 | "true", // prompt |
| 1842 | "http://code/base/", // dl url |
| 1843 | "file.signed", // file name |
| 1844 | "HASH1234=", // checksum |
| 1845 | "false", // needs admin |
| 1846 | "123", // size |
| 1847 | "", // deadline |
| 1848 | "7", // max days to scatter |
| 1849 | omaha_disable_p2p_for_downloading, |
| 1850 | omaha_disable_p2p_for_sharing), |
| 1851 | -1, |
| 1852 | false, // ping_only |
| 1853 | kErrorCodeSuccess, |
| 1854 | &response, |
| 1855 | NULL)); |
| 1856 | EXPECT_TRUE(response.update_exists); |
| 1857 | |
| 1858 | EXPECT_EQ(response.disable_p2p_for_downloading, |
| 1859 | omaha_disable_p2p_for_downloading); |
| 1860 | EXPECT_EQ(response.disable_p2p_for_sharing, |
| 1861 | omaha_disable_p2p_for_sharing); |
| 1862 | |
| 1863 | EXPECT_EQ(request_params.use_p2p_for_downloading(), |
| 1864 | expected_allow_p2p_for_downloading); |
| 1865 | |
| 1866 | EXPECT_EQ(request_params.use_p2p_for_sharing(), |
| 1867 | expected_allow_p2p_for_sharing); |
| 1868 | |
| 1869 | EXPECT_EQ(request_params.p2p_url(), expected_p2p_url); |
| 1870 | } |
| 1871 | |
| 1872 | TEST(OmahaRequestActionTest, P2PWithPeer) { |
| 1873 | P2PTest(true, // initial_allow_p2p_for_downloading |
| 1874 | true, // initial_allow_p2p_for_sharing |
| 1875 | false, // omaha_disable_p2p_for_downloading |
| 1876 | false, // omaha_disable_p2p_for_sharing |
| 1877 | true, // payload_state_allow_p2p_attempt |
| 1878 | true, // expect_p2p_client_lookup |
| 1879 | "http://1.3.5.7/p2p", // p2p_client_result_url |
| 1880 | true, // expected_allow_p2p_for_downloading |
| 1881 | true, // expected_allow_p2p_for_sharing |
| 1882 | "http://1.3.5.7/p2p"); // expected_p2p_url |
| 1883 | } |
| 1884 | |
| 1885 | TEST(OmahaRequestActionTest, P2PWithoutPeer) { |
| 1886 | P2PTest(true, // initial_allow_p2p_for_downloading |
| 1887 | true, // initial_allow_p2p_for_sharing |
| 1888 | false, // omaha_disable_p2p_for_downloading |
| 1889 | false, // omaha_disable_p2p_for_sharing |
| 1890 | true, // payload_state_allow_p2p_attempt |
| 1891 | true, // expect_p2p_client_lookup |
| 1892 | "", // p2p_client_result_url |
| 1893 | false, // expected_allow_p2p_for_downloading |
| 1894 | true, // expected_allow_p2p_for_sharing |
| 1895 | ""); // expected_p2p_url |
| 1896 | } |
| 1897 | |
| 1898 | TEST(OmahaRequestActionTest, P2PDownloadNotAllowed) { |
| 1899 | P2PTest(false, // initial_allow_p2p_for_downloading |
| 1900 | true, // initial_allow_p2p_for_sharing |
| 1901 | false, // omaha_disable_p2p_for_downloading |
| 1902 | false, // omaha_disable_p2p_for_sharing |
| 1903 | true, // payload_state_allow_p2p_attempt |
| 1904 | false, // expect_p2p_client_lookup |
| 1905 | "unset", // p2p_client_result_url |
| 1906 | false, // expected_allow_p2p_for_downloading |
| 1907 | true, // expected_allow_p2p_for_sharing |
| 1908 | ""); // expected_p2p_url |
| 1909 | } |
| 1910 | |
| 1911 | TEST(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) { |
| 1912 | P2PTest(true, // initial_allow_p2p_for_downloading |
| 1913 | true, // initial_allow_p2p_for_sharing |
| 1914 | true, // omaha_disable_p2p_for_downloading |
| 1915 | false, // omaha_disable_p2p_for_sharing |
| 1916 | true, // payload_state_allow_p2p_attempt |
| 1917 | false, // expect_p2p_client_lookup |
| 1918 | "unset", // p2p_client_result_url |
| 1919 | false, // expected_allow_p2p_for_downloading |
| 1920 | true, // expected_allow_p2p_for_sharing |
| 1921 | ""); // expected_p2p_url |
| 1922 | } |
| 1923 | |
| 1924 | TEST(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) { |
| 1925 | P2PTest(true, // initial_allow_p2p_for_downloading |
| 1926 | true, // initial_allow_p2p_for_sharing |
| 1927 | false, // omaha_disable_p2p_for_downloading |
| 1928 | true, // omaha_disable_p2p_for_sharing |
| 1929 | true, // payload_state_allow_p2p_attempt |
| 1930 | true, // expect_p2p_client_lookup |
| 1931 | "http://1.3.5.7/p2p", // p2p_client_result_url |
| 1932 | true, // expected_allow_p2p_for_downloading |
| 1933 | false, // expected_allow_p2p_for_sharing |
| 1934 | "http://1.3.5.7/p2p"); // expected_p2p_url |
| 1935 | } |
| 1936 | |
| 1937 | TEST(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) { |
| 1938 | P2PTest(true, // initial_allow_p2p_for_downloading |
| 1939 | true, // initial_allow_p2p_for_sharing |
| 1940 | true, // omaha_disable_p2p_for_downloading |
| 1941 | true, // omaha_disable_p2p_for_sharing |
| 1942 | true, // payload_state_allow_p2p_attempt |
| 1943 | false, // expect_p2p_client_lookup |
| 1944 | "unset", // p2p_client_result_url |
| 1945 | false, // expected_allow_p2p_for_downloading |
| 1946 | false, // expected_allow_p2p_for_sharing |
| 1947 | ""); // expected_p2p_url |
| 1948 | } |
| 1949 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1950 | } // namespace chromeos_update_engine |