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