Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 7 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 8 | #include <glib.h> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 9 | |
| 10 | #include "base/string_util.h" |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 12 | #include "base/time.h" |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 14 | #include "update_engine/action_pipe.h" |
| 15 | #include "update_engine/mock_http_fetcher.h" |
| 16 | #include "update_engine/omaha_hash_calculator.h" |
| 17 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 18 | #include "update_engine/omaha_request_params.h" |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 19 | #include "update_engine/prefs.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 20 | #include "update_engine/test_utils.h" |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 21 | #include "update_engine/utils.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 22 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 23 | using base::Time; |
| 24 | using base::TimeDelta; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 25 | using std::string; |
| 26 | using std::vector; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 27 | using testing::_; |
| 28 | using testing::AllOf; |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 29 | using testing::DoAll; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 30 | using testing::Ge; |
| 31 | using testing::Le; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 32 | using testing::NiceMock; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 33 | using testing::Return; |
| 34 | using testing::SetArgumentPointee; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
| 38 | class OmahaRequestActionTest : public ::testing::Test { }; |
| 39 | |
| 40 | namespace { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 41 | |
| 42 | OmahaRequestParams kDefaultTestParams( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 43 | OmahaRequestParams::kOsPlatform, |
| 44 | OmahaRequestParams::kOsVersion, |
| 45 | "service_pack", |
| 46 | "x86-generic", |
| 47 | OmahaRequestParams::kAppId, |
| 48 | "0.1.0.0", |
| 49 | "en-US", |
| 50 | "unittest", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 51 | "OEM MODEL 09235 7471", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 52 | false, // delta okay |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 53 | "http://url", |
| 54 | false, // update_disabled |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 55 | ""); // target_version_prefix); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 56 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 57 | string GetNoUpdateResponse(const string& app_id) { |
| 58 | return string( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 59 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 60 | "<daystart elapsed_seconds=\"100\"/>" |
| 61 | "<app appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 62 | "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></response>"; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 65 | string GetUpdateResponse2(const string& app_id, |
| 66 | const string& display_version, |
| 67 | const string& more_info_url, |
| 68 | const string& prompt, |
| 69 | const string& codebase, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 70 | const string& filename, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 71 | const string& hash, |
| 72 | const string& needsadmin, |
| 73 | const string& size, |
| 74 | const string& deadline, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 75 | const string& max_days_to_scatter) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 76 | string response = |
| 77 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 78 | "protocol=\"3.0\">" |
| 79 | "<daystart elapsed_seconds=\"100\"/>" |
| 80 | "<app appid=\"" + app_id + "\" status=\"ok\">" |
| 81 | "<ping status=\"ok\"/><updatecheck status=\"ok\">" |
| 82 | "<urls><url codebase=\"" + codebase + "\"/></urls>" |
| 83 | "<manifest version=\"" + display_version + "\">" |
| 84 | "<packages><package hash=\"not-used\" name=\"" + filename + "\" " |
| 85 | "size=\"" + size + "\"/></packages>" |
| 86 | "<actions><action event=\"postinstall\" " |
| 87 | "DisplayVersion=\"" + display_version + "\" " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 88 | "ChromeOSVersion=\"" + display_version + "\" " |
| 89 | "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " |
| 90 | "IsDelta=\"true\" " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 91 | "MaxDaysToScatter=\"" + max_days_to_scatter + "\" " |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 92 | "sha256=\"" + hash + "\" " |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 93 | "needsadmin=\"" + needsadmin + "\" " + |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 94 | (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) + |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 95 | "/></actions></manifest></updatecheck></app></response>"; |
| 96 | LOG(INFO) << "Response = " << response; |
| 97 | return response; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 100 | string GetUpdateResponse(const string& app_id, |
| 101 | const string& display_version, |
| 102 | const string& more_info_url, |
| 103 | const string& prompt, |
| 104 | const string& codebase, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 105 | const string& filename, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 106 | const string& hash, |
| 107 | const string& needsadmin, |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 108 | const string& size, |
| 109 | const string& deadline) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 110 | return GetUpdateResponse2(app_id, |
| 111 | display_version, |
| 112 | more_info_url, |
| 113 | prompt, |
| 114 | codebase, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 115 | filename, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 116 | hash, |
| 117 | needsadmin, |
| 118 | size, |
| 119 | deadline, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 120 | "7"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 124 | public: |
| 125 | OmahaRequestActionTestProcessorDelegate() |
| 126 | : loop_(NULL), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 127 | expected_code_(kActionCodeSuccess) {} |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 128 | virtual ~OmahaRequestActionTestProcessorDelegate() { |
| 129 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 130 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 131 | ActionExitCode code) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 132 | ASSERT_TRUE(loop_); |
| 133 | g_main_loop_quit(loop_); |
| 134 | } |
| 135 | |
| 136 | virtual void ActionCompleted(ActionProcessor* processor, |
| 137 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 138 | ActionExitCode code) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 139 | // make sure actions always succeed |
| 140 | if (action->Type() == OmahaRequestAction::StaticType()) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 141 | EXPECT_EQ(expected_code_, code); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 142 | else |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 143 | EXPECT_EQ(kActionCodeSuccess, code); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 144 | } |
| 145 | GMainLoop *loop_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 146 | ActionExitCode expected_code_; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 150 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 151 | processor->StartProcessing(); |
| 152 | return FALSE; |
| 153 | } |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 154 | } // namespace {} |
| 155 | |
| 156 | class OutputObjectCollectorAction; |
| 157 | |
| 158 | template<> |
| 159 | class ActionTraits<OutputObjectCollectorAction> { |
| 160 | public: |
| 161 | // Does not take an object for input |
| 162 | typedef OmahaResponse InputObjectType; |
| 163 | // On success, puts the output path on output |
| 164 | typedef NoneType OutputObjectType; |
| 165 | }; |
| 166 | |
| 167 | class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> { |
| 168 | public: |
| 169 | OutputObjectCollectorAction() : has_input_object_(false) {} |
| 170 | void PerformAction() { |
| 171 | // copy input object |
| 172 | has_input_object_ = HasInputObject(); |
| 173 | if (has_input_object_) |
| 174 | omaha_response_ = GetInputObject(); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 175 | processor_->ActionComplete(this, kActionCodeSuccess); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 176 | } |
| 177 | // Should never be called |
| 178 | void TerminateProcessing() { |
| 179 | CHECK(false); |
| 180 | } |
| 181 | // Debugging/logging |
| 182 | static std::string StaticType() { |
| 183 | return "OutputObjectCollectorAction"; |
| 184 | } |
| 185 | std::string Type() const { return StaticType(); } |
| 186 | bool has_input_object_; |
| 187 | OmahaResponse omaha_response_; |
| 188 | }; |
| 189 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 190 | // Returns true iff an output response was obtained from the |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 191 | // OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 192 | // used. out_response may be NULL. If |fail_http_response_code| is non-negative, |
| 193 | // the transfer will fail with that code. |ping_only| is passed through to the |
| 194 | // OmahaRequestAction constructor. out_post_data may be null; if non-null, the |
| 195 | // post-data received by the mock HttpFetcher is returned. |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 196 | bool TestUpdateCheck(PrefsInterface* prefs, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 197 | OmahaRequestParams params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 198 | const string& http_response, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 199 | int fail_http_response_code, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 200 | bool ping_only, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 201 | ActionExitCode expected_code, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 202 | OmahaResponse* out_response, |
| 203 | vector<char>* out_post_data) { |
| 204 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 205 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 206 | http_response.size(), |
| 207 | NULL); |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 208 | if (fail_http_response_code >= 0) { |
| 209 | fetcher->FailTransfer(fail_http_response_code); |
| 210 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 211 | MockSystemState mock_system_state; |
| 212 | if (prefs) |
| 213 | mock_system_state.set_prefs(prefs); |
| 214 | OmahaRequestAction action(&mock_system_state, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 215 | ¶ms, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 216 | NULL, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 217 | fetcher, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 218 | ping_only); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 219 | OmahaRequestActionTestProcessorDelegate delegate; |
| 220 | delegate.loop_ = loop; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 221 | delegate.expected_code_ = expected_code; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 222 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 223 | ActionProcessor processor; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 224 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 225 | processor.EnqueueAction(&action); |
| 226 | |
| 227 | OutputObjectCollectorAction collector_action; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 228 | BondActions(&action, &collector_action); |
| 229 | processor.EnqueueAction(&collector_action); |
| 230 | |
| 231 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 232 | g_main_loop_run(loop); |
| 233 | g_main_loop_unref(loop); |
| 234 | if (collector_action.has_input_object_ && out_response) |
| 235 | *out_response = collector_action.omaha_response_; |
| 236 | if (out_post_data) |
| 237 | *out_post_data = fetcher->post_data(); |
| 238 | return collector_action.has_input_object_; |
| 239 | } |
| 240 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 241 | // Tests Event requests -- they should always succeed. |out_post_data| |
| 242 | // may be null; if non-null, the post-data received by the mock |
| 243 | // HttpFetcher is returned. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 244 | void TestEvent(OmahaRequestParams params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 245 | OmahaEvent* event, |
| 246 | const string& http_response, |
| 247 | vector<char>* out_post_data) { |
| 248 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 249 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 250 | http_response.size(), |
| 251 | NULL); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 252 | MockSystemState mock_system_state; |
| 253 | OmahaRequestAction action(&mock_system_state, ¶ms, event, fetcher, false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 254 | OmahaRequestActionTestProcessorDelegate delegate; |
| 255 | delegate.loop_ = loop; |
| 256 | ActionProcessor processor; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 257 | processor.set_delegate(&delegate); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 258 | processor.EnqueueAction(&action); |
| 259 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 260 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 261 | g_main_loop_run(loop); |
| 262 | g_main_loop_unref(loop); |
| 263 | if (out_post_data) |
| 264 | *out_post_data = fetcher->post_data(); |
| 265 | } |
| 266 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 267 | TEST(OmahaRequestActionTest, NoUpdateTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 268 | OmahaResponse response; |
| 269 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 270 | TestUpdateCheck(NULL, // prefs |
| 271 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 272 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 273 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 274 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 275 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 276 | &response, |
| 277 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 278 | EXPECT_FALSE(response.update_exists); |
| 279 | } |
| 280 | |
| 281 | TEST(OmahaRequestActionTest, ValidUpdateTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 282 | OmahaResponse response; |
| 283 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 284 | TestUpdateCheck(NULL, // prefs |
| 285 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 286 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 287 | "1.2.3.4", // version |
| 288 | "http://more/info", |
| 289 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 290 | "http://code/base/", // dl url |
| 291 | "file.signed", // file name |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 292 | "HASH1234=", // checksum |
| 293 | "false", // needs admin |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 294 | "123", // size |
| 295 | "20101020"), // deadline |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 296 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 297 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 298 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 299 | &response, |
| 300 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 301 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 302 | EXPECT_TRUE(response.update_exists); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 303 | EXPECT_EQ("1.2.3.4", response.display_version); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 304 | EXPECT_EQ("http://code/base/file.signed", response.payload_urls[0]); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 305 | EXPECT_EQ("http://more/info", response.more_info_url); |
| 306 | EXPECT_EQ("HASH1234=", response.hash); |
| 307 | EXPECT_EQ(123, response.size); |
| 308 | EXPECT_FALSE(response.needs_admin); |
| 309 | EXPECT_TRUE(response.prompt); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 310 | EXPECT_EQ("20101020", response.deadline); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 313 | TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) { |
| 314 | OmahaResponse response; |
| 315 | OmahaRequestParams params = kDefaultTestParams; |
| 316 | params.update_disabled = true; |
| 317 | ASSERT_FALSE( |
| 318 | TestUpdateCheck(NULL, // prefs |
| 319 | params, |
| 320 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 321 | "1.2.3.4", // version |
| 322 | "http://more/info", |
| 323 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 324 | "http://code/base/", // dl url |
| 325 | "file.signed", // file name |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 326 | "HASH1234=", // checksum |
| 327 | "false", // needs admin |
| 328 | "123", // size |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 329 | ""), // deadline |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 330 | -1, |
| 331 | false, // ping_only |
| 332 | kActionCodeOmahaUpdateIgnoredPerPolicy, |
| 333 | &response, |
| 334 | NULL)); |
| 335 | EXPECT_FALSE(response.update_exists); |
| 336 | } |
| 337 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 338 | TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) { |
| 339 | OmahaResponse response; |
| 340 | OmahaRequestParams params = kDefaultTestParams; |
| 341 | params.update_disabled = true; |
| 342 | ASSERT_TRUE( |
| 343 | TestUpdateCheck(NULL, // prefs |
| 344 | params, |
| 345 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 346 | -1, |
| 347 | false, // ping_only |
| 348 | kActionCodeSuccess, |
| 349 | &response, |
| 350 | NULL)); |
| 351 | EXPECT_FALSE(response.update_exists); |
| 352 | } |
| 353 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 354 | TEST(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) { |
| 355 | OmahaResponse response; |
| 356 | OmahaRequestParams params = kDefaultTestParams; |
| 357 | params.wall_clock_based_wait_enabled = true; |
| 358 | params.update_check_count_wait_enabled = false; |
| 359 | params.waiting_period = TimeDelta::FromDays(2); |
| 360 | |
| 361 | string prefs_dir; |
| 362 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 363 | &prefs_dir)); |
| 364 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 365 | |
| 366 | Prefs prefs; |
| 367 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 368 | << "Failed to initialize preferences."; |
| 369 | |
| 370 | ASSERT_FALSE( |
| 371 | TestUpdateCheck(&prefs, // prefs |
| 372 | params, |
| 373 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 374 | "1.2.3.4", // version |
| 375 | "http://more/info", |
| 376 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 377 | "http://code/base/", // dl url |
| 378 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 379 | "HASH1234=", // checksum |
| 380 | "false", // needs admin |
| 381 | "123", // size |
| 382 | "", // deadline |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 383 | "7"), // max days to scatter |
| 384 | -1, |
| 385 | false, // ping_only |
| 386 | kActionCodeOmahaUpdateDeferredPerPolicy, |
| 387 | &response, |
| 388 | NULL)); |
| 389 | EXPECT_FALSE(response.update_exists); |
| 390 | } |
| 391 | |
| 392 | TEST(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) { |
| 393 | OmahaResponse response; |
| 394 | OmahaRequestParams params = kDefaultTestParams; |
| 395 | params.wall_clock_based_wait_enabled = false; |
| 396 | params.waiting_period = TimeDelta::FromDays(2); |
| 397 | |
| 398 | params.update_check_count_wait_enabled = true; |
| 399 | params.min_update_checks_needed = 1; |
| 400 | params.max_update_checks_allowed = 8; |
| 401 | |
| 402 | string prefs_dir; |
| 403 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 404 | &prefs_dir)); |
| 405 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 406 | |
| 407 | Prefs prefs; |
| 408 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 409 | << "Failed to initialize preferences."; |
| 410 | |
| 411 | ASSERT_TRUE( |
| 412 | TestUpdateCheck(&prefs, // prefs |
| 413 | params, |
| 414 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 415 | "1.2.3.4", // version |
| 416 | "http://more/info", |
| 417 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 418 | "http://code/base/", // dl url |
| 419 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 420 | "HASH1234=", // checksum |
| 421 | "false", // needs admin |
| 422 | "123", // size |
| 423 | "", // deadline |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 424 | "7"), // max days to scatter |
| 425 | -1, |
| 426 | false, // ping_only |
| 427 | kActionCodeSuccess, |
| 428 | &response, |
| 429 | NULL)); |
| 430 | EXPECT_TRUE(response.update_exists); |
| 431 | } |
| 432 | |
| 433 | TEST(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) { |
| 434 | OmahaResponse response; |
| 435 | OmahaRequestParams params = kDefaultTestParams; |
| 436 | params.wall_clock_based_wait_enabled = true; |
| 437 | params.waiting_period = TimeDelta::FromDays(2); |
| 438 | |
| 439 | params.update_check_count_wait_enabled = true; |
| 440 | params.min_update_checks_needed = 1; |
| 441 | params.max_update_checks_allowed = 8; |
| 442 | |
| 443 | string prefs_dir; |
| 444 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 445 | &prefs_dir)); |
| 446 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 447 | |
| 448 | Prefs prefs; |
| 449 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 450 | << "Failed to initialize preferences."; |
| 451 | |
| 452 | ASSERT_TRUE( |
| 453 | TestUpdateCheck(&prefs, // prefs |
| 454 | params, |
| 455 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 456 | "1.2.3.4", // version |
| 457 | "http://more/info", |
| 458 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 459 | "http://code/base/", // dl url |
| 460 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 461 | "HASH1234=", // checksum |
| 462 | "false", // needs admin |
| 463 | "123", // size |
| 464 | "", // deadline |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 465 | "0"), // max days to scatter |
| 466 | -1, |
| 467 | false, // ping_only |
| 468 | kActionCodeSuccess, |
| 469 | &response, |
| 470 | NULL)); |
| 471 | EXPECT_TRUE(response.update_exists); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | TEST(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) { |
| 476 | OmahaResponse response; |
| 477 | OmahaRequestParams params = kDefaultTestParams; |
| 478 | params.wall_clock_based_wait_enabled = true; |
| 479 | params.waiting_period = TimeDelta(); |
| 480 | |
| 481 | params.update_check_count_wait_enabled = true; |
| 482 | params.min_update_checks_needed = 0; |
| 483 | params.max_update_checks_allowed = 0; |
| 484 | |
| 485 | string prefs_dir; |
| 486 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 487 | &prefs_dir)); |
| 488 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 489 | |
| 490 | Prefs prefs; |
| 491 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 492 | << "Failed to initialize preferences."; |
| 493 | |
| 494 | ASSERT_TRUE(TestUpdateCheck( |
| 495 | &prefs, // prefs |
| 496 | params, |
| 497 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 498 | "1.2.3.4", // version |
| 499 | "http://more/info", |
| 500 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 501 | "http://code/base/", // dl url |
| 502 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 503 | "HASH1234=", // checksum |
| 504 | "false", // needs admin |
| 505 | "123", // size |
| 506 | "", // deadline |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 507 | "7"), // max days to scatter |
| 508 | -1, |
| 509 | false, // ping_only |
| 510 | kActionCodeSuccess, |
| 511 | &response, |
| 512 | NULL)); |
| 513 | |
| 514 | int64 count; |
| 515 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count)); |
| 516 | ASSERT_TRUE(count == 0); |
| 517 | EXPECT_TRUE(response.update_exists); |
| 518 | } |
| 519 | |
| 520 | TEST(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) { |
| 521 | OmahaResponse response; |
| 522 | OmahaRequestParams params = kDefaultTestParams; |
| 523 | params.wall_clock_based_wait_enabled = true; |
| 524 | params.waiting_period = TimeDelta(); |
| 525 | |
| 526 | params.update_check_count_wait_enabled = true; |
| 527 | params.min_update_checks_needed = 1; |
| 528 | params.max_update_checks_allowed = 8; |
| 529 | |
| 530 | string prefs_dir; |
| 531 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 532 | &prefs_dir)); |
| 533 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 534 | |
| 535 | Prefs prefs; |
| 536 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 537 | << "Failed to initialize preferences."; |
| 538 | |
| 539 | ASSERT_FALSE(TestUpdateCheck( |
| 540 | &prefs, // prefs |
| 541 | params, |
| 542 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 543 | "1.2.3.4", // version |
| 544 | "http://more/info", |
| 545 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 546 | "http://code/base/", // dl url |
| 547 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 548 | "HASH1234=", // checksum |
| 549 | "false", // needs admin |
| 550 | "123", // size |
| 551 | "", // deadline |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 552 | "7"), // max days to scatter |
| 553 | -1, |
| 554 | false, // ping_only |
| 555 | kActionCodeOmahaUpdateDeferredPerPolicy, |
| 556 | &response, |
| 557 | NULL)); |
| 558 | |
| 559 | int64 count; |
| 560 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count)); |
| 561 | ASSERT_TRUE(count > 0); |
| 562 | EXPECT_FALSE(response.update_exists); |
| 563 | } |
| 564 | |
| 565 | TEST(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) { |
| 566 | OmahaResponse response; |
| 567 | OmahaRequestParams params = kDefaultTestParams; |
| 568 | params.wall_clock_based_wait_enabled = true; |
| 569 | params.waiting_period = TimeDelta(); |
| 570 | |
| 571 | params.update_check_count_wait_enabled = true; |
| 572 | params.min_update_checks_needed = 1; |
| 573 | params.max_update_checks_allowed = 8; |
| 574 | |
| 575 | string prefs_dir; |
| 576 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 577 | &prefs_dir)); |
| 578 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 579 | |
| 580 | Prefs prefs; |
| 581 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 582 | << "Failed to initialize preferences."; |
| 583 | |
| 584 | ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, 5)); |
| 585 | |
| 586 | ASSERT_FALSE(TestUpdateCheck( |
| 587 | &prefs, // prefs |
| 588 | params, |
| 589 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 590 | "1.2.3.4", // version |
| 591 | "http://more/info", |
| 592 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 593 | "http://code/base/", // dl url |
| 594 | "file.signed", // file name |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 595 | "HASH1234=", // checksum |
| 596 | "false", // needs admin |
| 597 | "123", // size |
| 598 | "", // deadline |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 599 | "7"), // max days to scatter |
| 600 | -1, |
| 601 | false, // ping_only |
| 602 | kActionCodeOmahaUpdateDeferredPerPolicy, |
| 603 | &response, |
| 604 | NULL)); |
| 605 | |
| 606 | int64 count; |
| 607 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count)); |
| 608 | // count remains the same, as the decrementing happens in update_attempter |
| 609 | // which this test doesn't exercise. |
| 610 | ASSERT_TRUE(count == 5); |
| 611 | EXPECT_FALSE(response.update_exists); |
| 612 | } |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 613 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 614 | TEST(OmahaRequestActionTest, NoOutputPipeTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 615 | const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); |
| 616 | |
| 617 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 618 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 619 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 620 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 621 | OmahaRequestAction action(&mock_system_state, ¶ms, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 622 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 623 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 624 | NULL), |
| 625 | false); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 626 | OmahaRequestActionTestProcessorDelegate delegate; |
| 627 | delegate.loop_ = loop; |
| 628 | ActionProcessor processor; |
| 629 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 630 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 631 | |
| 632 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 633 | g_main_loop_run(loop); |
| 634 | g_main_loop_unref(loop); |
| 635 | EXPECT_FALSE(processor.IsRunning()); |
| 636 | } |
| 637 | |
| 638 | TEST(OmahaRequestActionTest, InvalidXmlTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 639 | OmahaResponse response; |
| 640 | ASSERT_FALSE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 641 | TestUpdateCheck(NULL, // prefs |
| 642 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 643 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 644 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 645 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 646 | kActionCodeOmahaRequestXMLParseError, |
| 647 | &response, |
| 648 | NULL)); |
| 649 | EXPECT_FALSE(response.update_exists); |
| 650 | } |
| 651 | |
| 652 | TEST(OmahaRequestActionTest, EmptyResponseTest) { |
| 653 | OmahaResponse response; |
| 654 | ASSERT_FALSE( |
| 655 | TestUpdateCheck(NULL, // prefs |
| 656 | kDefaultTestParams, |
| 657 | "", |
| 658 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 659 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 660 | kActionCodeOmahaRequestEmptyResponseError, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 661 | &response, |
| 662 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 663 | EXPECT_FALSE(response.update_exists); |
| 664 | } |
| 665 | |
| 666 | TEST(OmahaRequestActionTest, MissingStatusTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 667 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 668 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 669 | NULL, // prefs |
| 670 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 671 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 672 | "<daystart elapsed_seconds=\"100\"/>" |
| 673 | "<app appid=\"foo\" status=\"ok\">" |
| 674 | "<ping status=\"ok\"/>" |
| 675 | "<updatecheck/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 676 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 677 | false, // ping_only |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 678 | kActionCodeOmahaResponseInvalid, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 679 | &response, |
| 680 | NULL)); |
| 681 | EXPECT_FALSE(response.update_exists); |
| 682 | } |
| 683 | |
| 684 | TEST(OmahaRequestActionTest, InvalidStatusTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 685 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 686 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 687 | NULL, // prefs |
| 688 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 689 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 690 | "<daystart elapsed_seconds=\"100\"/>" |
| 691 | "<app appid=\"foo\" status=\"ok\">" |
| 692 | "<ping status=\"ok\"/>" |
| 693 | "<updatecheck status=\"InvalidStatusTest\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 694 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 695 | false, // ping_only |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 696 | kActionCodeOmahaResponseInvalid, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 697 | &response, |
| 698 | NULL)); |
| 699 | EXPECT_FALSE(response.update_exists); |
| 700 | } |
| 701 | |
| 702 | TEST(OmahaRequestActionTest, MissingNodesetTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 703 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 704 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 705 | NULL, // prefs |
| 706 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 707 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 708 | "<daystart elapsed_seconds=\"100\"/>" |
| 709 | "<app appid=\"foo\" status=\"ok\">" |
| 710 | "<ping status=\"ok\"/>" |
| 711 | "</app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 712 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 713 | false, // ping_only |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 714 | kActionCodeOmahaResponseInvalid, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 715 | &response, |
| 716 | NULL)); |
| 717 | EXPECT_FALSE(response.update_exists); |
| 718 | } |
| 719 | |
| 720 | TEST(OmahaRequestActionTest, MissingFieldTest) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 721 | string input_response = |
| 722 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" |
| 723 | "<daystart elapsed_seconds=\"100\"/>" |
| 724 | "<app appid=\"xyz\" status=\"ok\">" |
| 725 | "<updatecheck status=\"ok\">" |
| 726 | "<urls><url codebase=\"http://missing/field/test/\"/></urls>" |
| 727 | "<manifest version=\"1.0.0.0\">" |
| 728 | "<packages><package hash=\"not-used\" name=\"f\" " |
| 729 | "size=\"587\"/></packages>" |
| 730 | "<actions><action event=\"postinstall\" " |
| 731 | "DisplayVersion=\"10.2.3.4\" " |
| 732 | "ChromeOSVersion=\"10.2.3.4\" " |
| 733 | "Prompt=\"false\" " |
| 734 | "IsDelta=\"true\" " |
| 735 | "sha256=\"lkq34j5345\" " |
| 736 | "needsadmin=\"true\" " |
| 737 | "/></actions></manifest></updatecheck></app></response>"; |
| 738 | LOG(INFO) << "Input Response = " << input_response; |
| 739 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 740 | OmahaResponse response; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 741 | ASSERT_TRUE(TestUpdateCheck(NULL, // prefs |
| 742 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 743 | input_response, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 744 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 745 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 746 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 747 | &response, |
| 748 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 749 | EXPECT_TRUE(response.update_exists); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 750 | EXPECT_EQ("10.2.3.4", response.display_version); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 751 | EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 752 | EXPECT_EQ("", response.more_info_url); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 753 | EXPECT_EQ("lkq34j5345", response.hash); |
| 754 | EXPECT_EQ(587, response.size); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 755 | EXPECT_TRUE(response.needs_admin); |
| 756 | EXPECT_FALSE(response.prompt); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 757 | EXPECT_TRUE(response.deadline.empty()); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | namespace { |
| 761 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 762 | public: |
| 763 | void ProcessingStopped(const ActionProcessor* processor) { |
| 764 | ASSERT_TRUE(loop_); |
| 765 | g_main_loop_quit(loop_); |
| 766 | } |
| 767 | GMainLoop *loop_; |
| 768 | }; |
| 769 | |
| 770 | gboolean TerminateTransferTestStarter(gpointer data) { |
| 771 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 772 | processor->StartProcessing(); |
| 773 | CHECK(processor->IsRunning()); |
| 774 | processor->StopProcessing(); |
| 775 | return FALSE; |
| 776 | } |
| 777 | } // namespace {} |
| 778 | |
| 779 | TEST(OmahaRequestActionTest, TerminateTransferTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 780 | string http_response("doesn't matter"); |
| 781 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 782 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 783 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 784 | OmahaRequestParams params = kDefaultTestParams; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 785 | OmahaRequestAction action(&mock_system_state, ¶ms, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 786 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 787 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 788 | NULL), |
| 789 | false); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 790 | TerminateEarlyTestProcessorDelegate delegate; |
| 791 | delegate.loop_ = loop; |
| 792 | ActionProcessor processor; |
| 793 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 794 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 795 | |
| 796 | g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
| 797 | g_main_loop_run(loop); |
| 798 | g_main_loop_unref(loop); |
| 799 | } |
| 800 | |
| 801 | TEST(OmahaRequestActionTest, XmlEncodeTest) { |
| 802 | EXPECT_EQ("ab", XmlEncode("ab")); |
| 803 | EXPECT_EQ("a<b", XmlEncode("a<b")); |
| 804 | EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
| 805 | EXPECT_EQ("<&>", XmlEncode("<&>")); |
| 806 | EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>")); |
| 807 | |
| 808 | vector<char> post_data; |
| 809 | |
| 810 | // Make sure XML Encode is being called on the params |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 811 | OmahaRequestParams params(OmahaRequestParams::kOsPlatform, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 812 | OmahaRequestParams::kOsVersion, |
| 813 | "testtheservice_pack>", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 814 | "x86 generic<id", |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 815 | OmahaRequestParams::kAppId, |
| 816 | "0.1.0.0", |
| 817 | "en-US", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 818 | "unittest_track<", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 819 | "<OEM MODEL>", |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 820 | false, // delta okay |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 821 | "http://url", |
| 822 | false, // update_disabled |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 823 | ""); // target_version_prefix |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 824 | OmahaResponse response; |
| 825 | ASSERT_FALSE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 826 | TestUpdateCheck(NULL, // prefs |
| 827 | params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 828 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 829 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 830 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 831 | kActionCodeOmahaRequestXMLParseError, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 832 | &response, |
| 833 | &post_data)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 834 | // convert post_data to string |
| 835 | string post_str(&post_data[0], post_data.size()); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 836 | EXPECT_NE(post_str.find("testtheservice_pack>"), string::npos); |
| 837 | EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 838 | EXPECT_NE(post_str.find("x86 generic<id"), string::npos); |
| 839 | EXPECT_EQ(post_str.find("x86 generic<id"), string::npos); |
| 840 | EXPECT_NE(post_str.find("unittest_track&lt;"), string::npos); |
| 841 | EXPECT_EQ(post_str.find("unittest_track<"), string::npos); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 842 | EXPECT_NE(post_str.find("<OEM MODEL>"), string::npos); |
| 843 | EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | TEST(OmahaRequestActionTest, XmlDecodeTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 847 | OmahaResponse response; |
| 848 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 849 | TestUpdateCheck(NULL, // prefs |
| 850 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 851 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 852 | "1.2.3.4", // version |
| 853 | "testthe<url", // more info |
| 854 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 855 | "testthe&codebase/", // dl url |
| 856 | "file.signed", // file name |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 857 | "HASH1234=", // checksum |
| 858 | "false", // needs admin |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 859 | "123", // size |
| 860 | "<20110101"), // deadline |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 861 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 862 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 863 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 864 | &response, |
| 865 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 866 | |
| 867 | EXPECT_EQ(response.more_info_url, "testthe<url"); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 868 | EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed"); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 869 | EXPECT_EQ(response.deadline, "<20110101"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | TEST(OmahaRequestActionTest, ParseIntTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 873 | OmahaResponse response; |
| 874 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 875 | TestUpdateCheck(NULL, // prefs |
| 876 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 877 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 878 | "1.2.3.4", // version |
| 879 | "theurl", // more info |
| 880 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 881 | "thecodebase/", // dl url |
| 882 | "file.signed", // file name |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 883 | "HASH1234=", // checksum |
| 884 | "false", // needs admin |
| 885 | // overflows int32: |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 886 | "123123123123123", // size |
| 887 | "deadline"), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 888 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 889 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 890 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 891 | &response, |
| 892 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 893 | |
| 894 | EXPECT_EQ(response.size, 123123123123123ll); |
| 895 | } |
| 896 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 897 | TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { |
| 898 | vector<char> post_data; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 899 | NiceMock<PrefsMock> prefs; |
| 900 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) |
| 901 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); |
| 902 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0); |
| 903 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 904 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 905 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 906 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 907 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 908 | kActionCodeOmahaRequestXMLParseError, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 909 | NULL, // response |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 910 | &post_data)); |
| 911 | // convert post_data to string |
| 912 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 913 | EXPECT_NE(post_str.find( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 914 | " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n" |
| 915 | " <updatecheck" |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 916 | " targetversionprefix=\"\"" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 917 | "></updatecheck>\n"), |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 918 | string::npos); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 919 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), |
| 920 | string::npos); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 921 | EXPECT_EQ(post_str.find("event"), string::npos); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 924 | |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 925 | TEST(OmahaRequestActionTest, FormatTargetVersionPrefixTest) { |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 926 | vector<char> post_data; |
| 927 | NiceMock<PrefsMock> prefs; |
| 928 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 929 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); |
| 930 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0); |
| 931 | OmahaRequestParams params = kDefaultTestParams; |
| 932 | params.update_disabled = true; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 933 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 934 | params, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 935 | "invalid xml>", |
| 936 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 937 | false, // ping_only |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 938 | kActionCodeOmahaRequestXMLParseError, |
| 939 | NULL, // response |
| 940 | &post_data)); |
| 941 | // convert post_data to string |
| 942 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 943 | EXPECT_NE(post_str.find( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 944 | " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n" |
| 945 | " <updatecheck" |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 946 | " targetversionprefix=\"\"" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 947 | "></updatecheck>\n"), |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 948 | string::npos); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 949 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), |
| 950 | string::npos); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 951 | EXPECT_EQ(post_str.find("event"), string::npos); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 954 | TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) { |
| 955 | vector<char> post_data; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 956 | TestEvent(kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 957 | new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted), |
| 958 | "invalid xml>", |
| 959 | &post_data); |
| 960 | // convert post_data to string |
| 961 | string post_str(&post_data[0], post_data.size()); |
| 962 | string expected_event = StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 963 | " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n", |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 964 | OmahaEvent::kTypeUpdateDownloadStarted, |
| 965 | OmahaEvent::kResultSuccess); |
| 966 | EXPECT_NE(post_str.find(expected_event), string::npos); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 967 | EXPECT_EQ(post_str.find("ping"), string::npos); |
| 968 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) { |
| 972 | vector<char> post_data; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 973 | TestEvent(kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 974 | new OmahaEvent(OmahaEvent::kTypeDownloadComplete, |
| 975 | OmahaEvent::kResultError, |
| 976 | kActionCodeError), |
| 977 | "invalid xml>", |
| 978 | &post_data); |
| 979 | // convert post_data to string |
| 980 | string post_str(&post_data[0], post_data.size()); |
| 981 | string expected_event = StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 982 | " <event eventtype=\"%d\" eventresult=\"%d\" " |
| 983 | "errorcode=\"%d\"></event>\n", |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 984 | OmahaEvent::kTypeDownloadComplete, |
| 985 | OmahaEvent::kResultError, |
Darin Petkov | 44d98d9 | 2011-03-21 16:08:11 -0700 | [diff] [blame] | 986 | kActionCodeError); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 987 | EXPECT_NE(post_str.find(expected_event), string::npos); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 988 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | TEST(OmahaRequestActionTest, IsEventTest) { |
| 992 | string http_response("doesn't matter"); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 993 | MockSystemState mock_system_state; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 994 | OmahaRequestParams params = kDefaultTestParams; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 995 | OmahaRequestAction update_check_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 996 | &mock_system_state, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 997 | ¶ms, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 998 | NULL, |
| 999 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 1000 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1001 | NULL), |
| 1002 | false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1003 | EXPECT_FALSE(update_check_action.IsEvent()); |
| 1004 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1005 | params = kDefaultTestParams; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1006 | OmahaRequestAction event_action( |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1007 | &mock_system_state, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1008 | ¶ms, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1009 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1010 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 1011 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1012 | NULL), |
| 1013 | false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1014 | EXPECT_TRUE(event_action.IsEvent()); |
| 1015 | } |
| 1016 | |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1017 | TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) { |
| 1018 | for (int i = 0; i < 2; i++) { |
| 1019 | bool delta_okay = i == 1; |
| 1020 | const char* delta_okay_str = delta_okay ? "true" : "false"; |
| 1021 | vector<char> post_data; |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1022 | OmahaRequestParams params(OmahaRequestParams::kOsPlatform, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1023 | OmahaRequestParams::kOsVersion, |
| 1024 | "service_pack", |
| 1025 | "x86-generic", |
| 1026 | OmahaRequestParams::kAppId, |
| 1027 | "0.1.0.0", |
| 1028 | "en-US", |
| 1029 | "unittest_track", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 1030 | "OEM MODEL REV 1234", |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1031 | delta_okay, |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1032 | "http://url", |
| 1033 | false, // update_disabled |
| 1034 | ""); // target_version_prefix |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1035 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
| 1036 | params, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1037 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1038 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1039 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1040 | kActionCodeOmahaRequestXMLParseError, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 1041 | NULL, |
| 1042 | &post_data)); |
| 1043 | // convert post_data to string |
| 1044 | string post_str(&post_data[0], post_data.size()); |
| 1045 | EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)), |
| 1046 | string::npos) |
| 1047 | << "i = " << i; |
| 1048 | } |
| 1049 | } |
| 1050 | |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 1051 | TEST(OmahaRequestActionTest, OmahaEventTest) { |
| 1052 | OmahaEvent default_event; |
| 1053 | EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type); |
| 1054 | EXPECT_EQ(OmahaEvent::kResultError, default_event.result); |
| 1055 | EXPECT_EQ(kActionCodeError, default_event.error_code); |
| 1056 | |
| 1057 | OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted); |
| 1058 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type); |
| 1059 | EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result); |
| 1060 | EXPECT_EQ(kActionCodeSuccess, success_event.error_code); |
| 1061 | |
| 1062 | OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished, |
| 1063 | OmahaEvent::kResultError, |
| 1064 | kActionCodeError); |
| 1065 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type); |
| 1066 | EXPECT_EQ(OmahaEvent::kResultError, error_event.result); |
| 1067 | EXPECT_EQ(kActionCodeError, error_event.error_code); |
| 1068 | } |
| 1069 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1070 | TEST(OmahaRequestActionTest, PingTest) { |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1071 | for (int ping_only = 0; ping_only < 2; ping_only++) { |
| 1072 | NiceMock<PrefsMock> prefs; |
| 1073 | // Add a few hours to the day difference to test no rounding, etc. |
| 1074 | int64_t five_days_ago = |
| 1075 | (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue(); |
| 1076 | int64_t six_days_ago = |
| 1077 | (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue(); |
| 1078 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1079 | .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true))); |
| 1080 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1081 | .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true))); |
| 1082 | vector<char> post_data; |
| 1083 | ASSERT_TRUE( |
| 1084 | TestUpdateCheck(&prefs, |
| 1085 | kDefaultTestParams, |
| 1086 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 1087 | -1, |
| 1088 | ping_only, |
| 1089 | kActionCodeSuccess, |
| 1090 | NULL, |
| 1091 | &post_data)); |
| 1092 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1093 | 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] | 1094 | string::npos); |
| 1095 | if (ping_only) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1096 | EXPECT_EQ(post_str.find("updatecheck"), string::npos); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1097 | EXPECT_EQ(post_str.find("previousversion"), string::npos); |
| 1098 | } else { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1099 | EXPECT_NE(post_str.find("updatecheck"), string::npos); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1100 | EXPECT_NE(post_str.find("previousversion"), string::npos); |
| 1101 | } |
| 1102 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | TEST(OmahaRequestActionTest, ActivePingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1106 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1107 | int64_t three_days_ago = |
| 1108 | (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue(); |
| 1109 | int64_t now = Time::Now().ToInternalValue(); |
| 1110 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1111 | .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true))); |
| 1112 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1113 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1114 | vector<char> post_data; |
| 1115 | ASSERT_TRUE( |
| 1116 | TestUpdateCheck(&prefs, |
| 1117 | kDefaultTestParams, |
| 1118 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1119 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1120 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1121 | kActionCodeSuccess, |
| 1122 | NULL, |
| 1123 | &post_data)); |
| 1124 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1125 | EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1126 | string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | TEST(OmahaRequestActionTest, RollCallPingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1130 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1131 | int64_t four_days_ago = |
| 1132 | (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue(); |
| 1133 | int64_t now = Time::Now().ToInternalValue(); |
| 1134 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1135 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1136 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1137 | .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true))); |
| 1138 | vector<char> post_data; |
| 1139 | ASSERT_TRUE( |
| 1140 | TestUpdateCheck(&prefs, |
| 1141 | kDefaultTestParams, |
| 1142 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1143 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1144 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1145 | kActionCodeSuccess, |
| 1146 | NULL, |
| 1147 | &post_data)); |
| 1148 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1149 | EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1150 | string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | TEST(OmahaRequestActionTest, NoPingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1154 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1155 | int64_t one_hour_ago = |
| 1156 | (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue(); |
| 1157 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1158 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); |
| 1159 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1160 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); |
| 1161 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1162 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1163 | vector<char> post_data; |
| 1164 | ASSERT_TRUE( |
| 1165 | TestUpdateCheck(&prefs, |
| 1166 | kDefaultTestParams, |
| 1167 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1168 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1169 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1170 | kActionCodeSuccess, |
| 1171 | NULL, |
| 1172 | &post_data)); |
| 1173 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1174 | EXPECT_EQ(post_str.find("ping"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 1177 | TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) { |
| 1178 | // This test ensures that we ignore empty ping only requests. |
| 1179 | NiceMock<PrefsMock> prefs; |
| 1180 | int64_t now = Time::Now().ToInternalValue(); |
| 1181 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1182 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1183 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1184 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 1185 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1186 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1187 | vector<char> post_data; |
| 1188 | EXPECT_TRUE( |
| 1189 | TestUpdateCheck(&prefs, |
| 1190 | kDefaultTestParams, |
| 1191 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 1192 | -1, |
| 1193 | true, // ping_only |
| 1194 | kActionCodeSuccess, |
| 1195 | NULL, |
| 1196 | &post_data)); |
| 1197 | EXPECT_EQ(post_data.size(), 0); |
| 1198 | } |
| 1199 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1200 | TEST(OmahaRequestActionTest, BackInTimePingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1201 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1202 | int64_t future = |
| 1203 | (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue(); |
| 1204 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 1205 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); |
| 1206 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 1207 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); |
| 1208 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) |
| 1209 | .WillOnce(Return(true)); |
| 1210 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) |
| 1211 | .WillOnce(Return(true)); |
| 1212 | vector<char> post_data; |
| 1213 | ASSERT_TRUE( |
| 1214 | TestUpdateCheck(&prefs, |
| 1215 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1216 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1217 | "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1218 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1219 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1220 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1221 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1222 | kActionCodeSuccess, |
| 1223 | NULL, |
| 1224 | &post_data)); |
| 1225 | string post_str(&post_data[0], post_data.size()); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1226 | EXPECT_EQ(post_str.find("ping"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | TEST(OmahaRequestActionTest, LastPingDayUpdateTest) { |
| 1230 | // 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] | 1231 | // minus 200 seconds with a slack of 5 seconds. Therefore, the test |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1232 | // may fail if it runs for longer than 5 seconds. It shouldn't run |
| 1233 | // that long though. |
| 1234 | int64_t midnight = |
| 1235 | (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue(); |
| 1236 | int64_t midnight_slack = |
| 1237 | (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue(); |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1238 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1239 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, |
| 1240 | AllOf(Ge(midnight), Le(midnight_slack)))) |
| 1241 | .WillOnce(Return(true)); |
| 1242 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, |
| 1243 | AllOf(Ge(midnight), Le(midnight_slack)))) |
| 1244 | .WillOnce(Return(true)); |
| 1245 | ASSERT_TRUE( |
| 1246 | TestUpdateCheck(&prefs, |
| 1247 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1248 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1249 | "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1250 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1251 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1252 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1253 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1254 | kActionCodeSuccess, |
| 1255 | NULL, |
| 1256 | NULL)); |
| 1257 | } |
| 1258 | |
| 1259 | TEST(OmahaRequestActionTest, NoElapsedSecondsTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1260 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1261 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1262 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1263 | ASSERT_TRUE( |
| 1264 | TestUpdateCheck(&prefs, |
| 1265 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1266 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1267 | "protocol=\"3.0\"><daystart blah=\"200\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1268 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1269 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1270 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1271 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1272 | kActionCodeSuccess, |
| 1273 | NULL, |
| 1274 | NULL)); |
| 1275 | } |
| 1276 | |
| 1277 | TEST(OmahaRequestActionTest, BadElapsedSecondsTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 1278 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1279 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 1280 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 1281 | ASSERT_TRUE( |
| 1282 | TestUpdateCheck(&prefs, |
| 1283 | kDefaultTestParams, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1284 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " |
| 1285 | "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1286 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1287 | "<updatecheck status=\"noupdate\"/></app></response>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1288 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1289 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1290 | kActionCodeSuccess, |
| 1291 | NULL, |
| 1292 | NULL)); |
| 1293 | } |
| 1294 | |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1295 | TEST(OmahaRequestActionTest, NoUniqueIDTest) { |
| 1296 | vector<char> post_data; |
| 1297 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
| 1298 | kDefaultTestParams, |
| 1299 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1300 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1301 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1302 | kActionCodeOmahaRequestXMLParseError, |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 1303 | NULL, // response |
| 1304 | &post_data)); |
| 1305 | // convert post_data to string |
| 1306 | string post_str(&post_data[0], post_data.size()); |
| 1307 | EXPECT_EQ(post_str.find("machineid="), string::npos); |
| 1308 | EXPECT_EQ(post_str.find("userid="), string::npos); |
| 1309 | } |
| 1310 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1311 | TEST(OmahaRequestActionTest, NetworkFailureTest) { |
| 1312 | OmahaResponse response; |
| 1313 | ASSERT_FALSE( |
| 1314 | TestUpdateCheck(NULL, // prefs |
| 1315 | kDefaultTestParams, |
| 1316 | "", |
| 1317 | 501, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1318 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1319 | static_cast<ActionExitCode>( |
| 1320 | kActionCodeOmahaRequestHTTPResponseBase + 501), |
| 1321 | &response, |
| 1322 | NULL)); |
| 1323 | EXPECT_FALSE(response.update_exists); |
| 1324 | } |
| 1325 | |
| 1326 | TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) { |
| 1327 | OmahaResponse response; |
| 1328 | ASSERT_FALSE( |
| 1329 | TestUpdateCheck(NULL, // prefs |
| 1330 | kDefaultTestParams, |
| 1331 | "", |
| 1332 | 1500, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1333 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1334 | static_cast<ActionExitCode>( |
| 1335 | kActionCodeOmahaRequestHTTPResponseBase + 999), |
| 1336 | &response, |
| 1337 | NULL)); |
| 1338 | EXPECT_FALSE(response.update_exists); |
| 1339 | } |
| 1340 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1341 | TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) { |
| 1342 | OmahaResponse response; |
| 1343 | OmahaRequestParams params = kDefaultTestParams; |
| 1344 | params.wall_clock_based_wait_enabled = true; |
| 1345 | params.waiting_period = TimeDelta().FromDays(1); |
| 1346 | params.update_check_count_wait_enabled = false; |
| 1347 | |
| 1348 | string prefs_dir; |
| 1349 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 1350 | &prefs_dir)); |
| 1351 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 1352 | |
| 1353 | Prefs prefs; |
| 1354 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 1355 | << "Failed to initialize preferences."; |
| 1356 | |
| 1357 | ASSERT_FALSE(TestUpdateCheck( |
| 1358 | &prefs, // prefs |
| 1359 | params, |
| 1360 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 1361 | "1.2.3.4", // version |
| 1362 | "http://more/info", |
| 1363 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1364 | "http://code/base/", // dl url |
| 1365 | "file.signed", // file name |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1366 | "HASH1234=", // checksum |
| 1367 | "false", // needs admin |
| 1368 | "123", // size |
| 1369 | "", // deadline |
| 1370 | "7"), // max days to scatter |
| 1371 | -1, |
| 1372 | false, // ping_only |
| 1373 | kActionCodeOmahaUpdateDeferredPerPolicy, |
| 1374 | &response, |
| 1375 | NULL)); |
| 1376 | |
| 1377 | int64 timestamp = 0; |
| 1378 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); |
| 1379 | ASSERT_TRUE(timestamp > 0); |
| 1380 | EXPECT_FALSE(response.update_exists); |
| 1381 | } |
| 1382 | |
| 1383 | TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) { |
| 1384 | OmahaResponse response; |
| 1385 | OmahaRequestParams params = kDefaultTestParams; |
| 1386 | params.wall_clock_based_wait_enabled = true; |
| 1387 | params.waiting_period = TimeDelta().FromDays(1); |
| 1388 | params.update_check_count_wait_enabled = false; |
| 1389 | |
| 1390 | string prefs_dir; |
| 1391 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 1392 | &prefs_dir)); |
| 1393 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 1394 | |
| 1395 | Prefs prefs; |
| 1396 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 1397 | << "Failed to initialize preferences."; |
| 1398 | |
| 1399 | // Set the timestamp to a very old value such that it exceeds the |
| 1400 | // waiting period set above. |
| 1401 | Time t1; |
| 1402 | Time::FromString("1/1/2012", &t1); |
| 1403 | ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue())); |
| 1404 | ASSERT_TRUE(TestUpdateCheck( |
| 1405 | &prefs, // prefs |
| 1406 | params, |
| 1407 | GetUpdateResponse2(OmahaRequestParams::kAppId, |
| 1408 | "1.2.3.4", // version |
| 1409 | "http://more/info", |
| 1410 | "true", // prompt |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1411 | "http://code/base/", // dl url |
| 1412 | "file.signed", // file name |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1413 | "HASH1234=", // checksum |
| 1414 | "false", // needs admin |
| 1415 | "123", // size |
| 1416 | "", // deadline |
| 1417 | "7"), // max days to scatter |
| 1418 | -1, |
| 1419 | false, // ping_only |
| 1420 | kActionCodeSuccess, |
| 1421 | &response, |
| 1422 | NULL)); |
| 1423 | |
| 1424 | EXPECT_TRUE(response.update_exists); |
| 1425 | |
| 1426 | // Make sure the timestamp t1 is unchanged showing that it was reused. |
| 1427 | int64 timestamp = 0; |
| 1428 | ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); |
| 1429 | ASSERT_TRUE(timestamp == t1.ToInternalValue()); |
| 1430 | } |
| 1431 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1432 | } // namespace chromeos_update_engine |