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" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 19 | #include "update_engine/prefs_mock.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 20 | #include "update_engine/test_utils.h" |
| 21 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 22 | using base::Time; |
| 23 | using base::TimeDelta; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 24 | using std::string; |
| 25 | using std::vector; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 26 | using testing::_; |
| 27 | using testing::AllOf; |
| 28 | using testing::Ge; |
| 29 | using testing::Le; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 30 | using testing::NiceMock; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 31 | using testing::Return; |
| 32 | using testing::SetArgumentPointee; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 33 | |
| 34 | namespace chromeos_update_engine { |
| 35 | |
| 36 | class OmahaRequestActionTest : public ::testing::Test { }; |
| 37 | |
| 38 | namespace { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 39 | const OmahaRequestParams kDefaultTestParams( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 40 | OmahaRequestParams::kOsPlatform, |
| 41 | OmahaRequestParams::kOsVersion, |
| 42 | "service_pack", |
| 43 | "x86-generic", |
| 44 | OmahaRequestParams::kAppId, |
| 45 | "0.1.0.0", |
| 46 | "en-US", |
| 47 | "unittest", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 48 | "OEM MODEL 09235 7471", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 49 | false, // delta okay |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 50 | "http://url", |
| 51 | false, // update_disabled |
| 52 | ""); // target_version_prefix |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 53 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 54 | string GetNoUpdateResponse(const string& app_id) { |
| 55 | return string( |
| 56 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 57 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 58 | "appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 59 | "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>"; |
| 60 | } |
| 61 | |
| 62 | string GetUpdateResponse(const string& app_id, |
| 63 | const string& display_version, |
| 64 | const string& more_info_url, |
| 65 | const string& prompt, |
| 66 | const string& codebase, |
| 67 | const string& hash, |
| 68 | const string& needsadmin, |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 69 | const string& size, |
| 70 | const string& deadline) { |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 71 | return string( |
| 72 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 73 | "xmlns=\"http://www.google.com/update2/response\" " |
| 74 | "protocol=\"2.0\"><app " |
| 75 | "appid=\"") + app_id + "\" status=\"ok\"><ping " |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 76 | "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" " |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 77 | "ChromeOSVersion=\"" + display_version + "\" " |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 78 | "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 79 | "IsDelta=\"true\" " |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 80 | "codebase=\"" + codebase + "\" hash=\"not-applicable\" " |
| 81 | "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" " |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 82 | "size=\"" + size + "\" deadline=\"" + deadline + |
| 83 | "\" status=\"ok\"/></app></gupdate>"; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 87 | public: |
| 88 | OmahaRequestActionTestProcessorDelegate() |
| 89 | : loop_(NULL), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 90 | expected_code_(kActionCodeSuccess) {} |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 91 | virtual ~OmahaRequestActionTestProcessorDelegate() { |
| 92 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 93 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 94 | ActionExitCode code) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(loop_); |
| 96 | g_main_loop_quit(loop_); |
| 97 | } |
| 98 | |
| 99 | virtual void ActionCompleted(ActionProcessor* processor, |
| 100 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 101 | ActionExitCode code) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 102 | // make sure actions always succeed |
| 103 | if (action->Type() == OmahaRequestAction::StaticType()) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 104 | EXPECT_EQ(expected_code_, code); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 105 | else |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 106 | EXPECT_EQ(kActionCodeSuccess, code); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 107 | } |
| 108 | GMainLoop *loop_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 109 | ActionExitCode expected_code_; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 113 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 114 | processor->StartProcessing(); |
| 115 | return FALSE; |
| 116 | } |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 117 | } // namespace {} |
| 118 | |
| 119 | class OutputObjectCollectorAction; |
| 120 | |
| 121 | template<> |
| 122 | class ActionTraits<OutputObjectCollectorAction> { |
| 123 | public: |
| 124 | // Does not take an object for input |
| 125 | typedef OmahaResponse InputObjectType; |
| 126 | // On success, puts the output path on output |
| 127 | typedef NoneType OutputObjectType; |
| 128 | }; |
| 129 | |
| 130 | class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> { |
| 131 | public: |
| 132 | OutputObjectCollectorAction() : has_input_object_(false) {} |
| 133 | void PerformAction() { |
| 134 | // copy input object |
| 135 | has_input_object_ = HasInputObject(); |
| 136 | if (has_input_object_) |
| 137 | omaha_response_ = GetInputObject(); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 138 | processor_->ActionComplete(this, kActionCodeSuccess); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 139 | } |
| 140 | // Should never be called |
| 141 | void TerminateProcessing() { |
| 142 | CHECK(false); |
| 143 | } |
| 144 | // Debugging/logging |
| 145 | static std::string StaticType() { |
| 146 | return "OutputObjectCollectorAction"; |
| 147 | } |
| 148 | std::string Type() const { return StaticType(); } |
| 149 | bool has_input_object_; |
| 150 | OmahaResponse omaha_response_; |
| 151 | }; |
| 152 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 153 | // Returns true iff an output response was obtained from the |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 154 | // OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 155 | // used. out_response may be NULL. If |fail_http_response_code| is non-negative, |
| 156 | // the transfer will fail with that code. |ping_only| is passed through to the |
| 157 | // OmahaRequestAction constructor. out_post_data may be null; if non-null, the |
| 158 | // post-data received by the mock HttpFetcher is returned. |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 159 | bool TestUpdateCheck(PrefsInterface* prefs, |
| 160 | const OmahaRequestParams& params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 161 | const string& http_response, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 162 | int fail_http_response_code, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 163 | bool ping_only, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 164 | ActionExitCode expected_code, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 165 | OmahaResponse* out_response, |
| 166 | vector<char>* out_post_data) { |
| 167 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 168 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 169 | http_response.size(), |
| 170 | NULL); |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 171 | if (fail_http_response_code >= 0) { |
| 172 | fetcher->FailTransfer(fail_http_response_code); |
| 173 | } |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 174 | NiceMock<PrefsMock> local_prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 175 | OmahaRequestAction action(prefs ? prefs : &local_prefs, |
| 176 | params, |
| 177 | NULL, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 178 | fetcher, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 179 | ping_only); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 180 | OmahaRequestActionTestProcessorDelegate delegate; |
| 181 | delegate.loop_ = loop; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 182 | delegate.expected_code_ = expected_code; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 183 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 184 | ActionProcessor processor; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 185 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 186 | processor.EnqueueAction(&action); |
| 187 | |
| 188 | OutputObjectCollectorAction collector_action; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 189 | BondActions(&action, &collector_action); |
| 190 | processor.EnqueueAction(&collector_action); |
| 191 | |
| 192 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 193 | g_main_loop_run(loop); |
| 194 | g_main_loop_unref(loop); |
| 195 | if (collector_action.has_input_object_ && out_response) |
| 196 | *out_response = collector_action.omaha_response_; |
| 197 | if (out_post_data) |
| 198 | *out_post_data = fetcher->post_data(); |
| 199 | return collector_action.has_input_object_; |
| 200 | } |
| 201 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 202 | // Tests Event requests -- they should always succeed. |out_post_data| |
| 203 | // may be null; if non-null, the post-data received by the mock |
| 204 | // HttpFetcher is returned. |
| 205 | void TestEvent(const OmahaRequestParams& params, |
| 206 | OmahaEvent* event, |
| 207 | const string& http_response, |
| 208 | vector<char>* out_post_data) { |
| 209 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 210 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 211 | http_response.size(), |
| 212 | NULL); |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 213 | NiceMock<PrefsMock> prefs; |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 214 | OmahaRequestAction action(&prefs, params, event, fetcher, false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 215 | OmahaRequestActionTestProcessorDelegate delegate; |
| 216 | delegate.loop_ = loop; |
| 217 | ActionProcessor processor; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 218 | processor.set_delegate(&delegate); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 219 | processor.EnqueueAction(&action); |
| 220 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 221 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 222 | g_main_loop_run(loop); |
| 223 | g_main_loop_unref(loop); |
| 224 | if (out_post_data) |
| 225 | *out_post_data = fetcher->post_data(); |
| 226 | } |
| 227 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 228 | TEST(OmahaRequestActionTest, NoUpdateTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 229 | OmahaResponse response; |
| 230 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 231 | TestUpdateCheck(NULL, // prefs |
| 232 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 233 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 234 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 235 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 236 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 237 | &response, |
| 238 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 239 | EXPECT_FALSE(response.update_exists); |
| 240 | } |
| 241 | |
| 242 | TEST(OmahaRequestActionTest, ValidUpdateTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 243 | OmahaResponse response; |
| 244 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 245 | TestUpdateCheck(NULL, // prefs |
| 246 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 247 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 248 | "1.2.3.4", // version |
| 249 | "http://more/info", |
| 250 | "true", // prompt |
| 251 | "http://code/base", // dl url |
| 252 | "HASH1234=", // checksum |
| 253 | "false", // needs admin |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 254 | "123", // size |
| 255 | "20101020"), // deadline |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 256 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 257 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 258 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 259 | &response, |
| 260 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 261 | EXPECT_TRUE(response.update_exists); |
| 262 | EXPECT_EQ("1.2.3.4", response.display_version); |
| 263 | EXPECT_EQ("http://code/base", response.codebase); |
| 264 | EXPECT_EQ("http://more/info", response.more_info_url); |
| 265 | EXPECT_EQ("HASH1234=", response.hash); |
| 266 | EXPECT_EQ(123, response.size); |
| 267 | EXPECT_FALSE(response.needs_admin); |
| 268 | EXPECT_TRUE(response.prompt); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 269 | EXPECT_EQ("20101020", response.deadline); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 272 | TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) { |
| 273 | OmahaResponse response; |
| 274 | OmahaRequestParams params = kDefaultTestParams; |
| 275 | params.update_disabled = true; |
| 276 | ASSERT_FALSE( |
| 277 | TestUpdateCheck(NULL, // prefs |
| 278 | params, |
| 279 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 280 | "1.2.3.4", // version |
| 281 | "http://more/info", |
| 282 | "true", // prompt |
| 283 | "http://code/base", // dl url |
| 284 | "HASH1234=", // checksum |
| 285 | "false", // needs admin |
| 286 | "123", // size |
| 287 | "20101020"), // deadline |
| 288 | -1, |
| 289 | false, // ping_only |
| 290 | kActionCodeOmahaUpdateIgnoredPerPolicy, |
| 291 | &response, |
| 292 | NULL)); |
| 293 | EXPECT_FALSE(response.update_exists); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) { |
| 298 | OmahaResponse response; |
| 299 | OmahaRequestParams params = kDefaultTestParams; |
| 300 | params.update_disabled = true; |
| 301 | ASSERT_TRUE( |
| 302 | TestUpdateCheck(NULL, // prefs |
| 303 | params, |
| 304 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 305 | -1, |
| 306 | false, // ping_only |
| 307 | kActionCodeSuccess, |
| 308 | &response, |
| 309 | NULL)); |
| 310 | EXPECT_FALSE(response.update_exists); |
| 311 | } |
| 312 | |
| 313 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 314 | TEST(OmahaRequestActionTest, NoOutputPipeTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 315 | const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); |
| 316 | |
| 317 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 318 | |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 319 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 320 | OmahaRequestAction action(&prefs, kDefaultTestParams, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 321 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 322 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 323 | NULL), |
| 324 | false); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 325 | OmahaRequestActionTestProcessorDelegate delegate; |
| 326 | delegate.loop_ = loop; |
| 327 | ActionProcessor processor; |
| 328 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 329 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 330 | |
| 331 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 332 | g_main_loop_run(loop); |
| 333 | g_main_loop_unref(loop); |
| 334 | EXPECT_FALSE(processor.IsRunning()); |
| 335 | } |
| 336 | |
| 337 | TEST(OmahaRequestActionTest, InvalidXmlTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 338 | OmahaResponse response; |
| 339 | ASSERT_FALSE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 340 | TestUpdateCheck(NULL, // prefs |
| 341 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 342 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 343 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 344 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 345 | kActionCodeOmahaRequestXMLParseError, |
| 346 | &response, |
| 347 | NULL)); |
| 348 | EXPECT_FALSE(response.update_exists); |
| 349 | } |
| 350 | |
| 351 | TEST(OmahaRequestActionTest, EmptyResponseTest) { |
| 352 | OmahaResponse response; |
| 353 | ASSERT_FALSE( |
| 354 | TestUpdateCheck(NULL, // prefs |
| 355 | kDefaultTestParams, |
| 356 | "", |
| 357 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 358 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 359 | kActionCodeOmahaRequestEmptyResponseError, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 360 | &response, |
| 361 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 362 | EXPECT_FALSE(response.update_exists); |
| 363 | } |
| 364 | |
| 365 | TEST(OmahaRequestActionTest, MissingStatusTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 366 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 367 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 368 | NULL, // prefs |
| 369 | kDefaultTestParams, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 370 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 371 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 372 | "appid=\"foo\" status=\"ok\"><ping " |
| 373 | "status=\"ok\"/><updatecheck/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 374 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 375 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 376 | kActionCodeOmahaRequestNoUpdateCheckStatus, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 377 | &response, |
| 378 | NULL)); |
| 379 | EXPECT_FALSE(response.update_exists); |
| 380 | } |
| 381 | |
| 382 | TEST(OmahaRequestActionTest, InvalidStatusTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 383 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 384 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 385 | NULL, // prefs |
| 386 | kDefaultTestParams, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 387 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 388 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 389 | "appid=\"foo\" status=\"ok\"><ping " |
| 390 | "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 391 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 392 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 393 | kActionCodeOmahaRequestBadUpdateCheckStatus, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 394 | &response, |
| 395 | NULL)); |
| 396 | EXPECT_FALSE(response.update_exists); |
| 397 | } |
| 398 | |
| 399 | TEST(OmahaRequestActionTest, MissingNodesetTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 400 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 401 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 402 | NULL, // prefs |
| 403 | kDefaultTestParams, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 404 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 405 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 406 | "appid=\"foo\" status=\"ok\"><ping " |
| 407 | "status=\"ok\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 408 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 409 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 410 | kActionCodeOmahaRequestNoUpdateCheckNode, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 411 | &response, |
| 412 | NULL)); |
| 413 | EXPECT_FALSE(response.update_exists); |
| 414 | } |
| 415 | |
| 416 | TEST(OmahaRequestActionTest, MissingFieldTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 417 | OmahaResponse response; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 418 | ASSERT_TRUE(TestUpdateCheck(NULL, // prefs |
| 419 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 420 | string("<?xml version=\"1.0\" " |
| 421 | "encoding=\"UTF-8\"?><gupdate " |
| 422 | "xmlns=\"http://www.google.com/" |
| 423 | "update2/response\" " |
| 424 | "protocol=\"2.0\"><app appid=\"") + |
| 425 | OmahaRequestParams::kAppId |
| 426 | + "\" status=\"ok\"><ping " |
| 427 | "status=\"ok\"/><updatecheck " |
| 428 | "DisplayVersion=\"1.2.3.4\" " |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 429 | "ChromeOSVersion=\"1.2.3.4\" " |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 430 | "Prompt=\"false\" " |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 431 | "IsDelta=\"true\" " |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 432 | "codebase=\"http://code/base\" hash=\"foo\" " |
| 433 | "sha256=\"HASH1234=\" needsadmin=\"true\" " |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 434 | "size=\"123\" " |
| 435 | "status=\"ok\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 436 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 437 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 438 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 439 | &response, |
| 440 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 441 | EXPECT_TRUE(response.update_exists); |
| 442 | EXPECT_EQ("1.2.3.4", response.display_version); |
| 443 | EXPECT_EQ("http://code/base", response.codebase); |
| 444 | EXPECT_EQ("", response.more_info_url); |
| 445 | EXPECT_EQ("HASH1234=", response.hash); |
| 446 | EXPECT_EQ(123, response.size); |
| 447 | EXPECT_TRUE(response.needs_admin); |
| 448 | EXPECT_FALSE(response.prompt); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 449 | EXPECT_TRUE(response.deadline.empty()); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | namespace { |
| 453 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 454 | public: |
| 455 | void ProcessingStopped(const ActionProcessor* processor) { |
| 456 | ASSERT_TRUE(loop_); |
| 457 | g_main_loop_quit(loop_); |
| 458 | } |
| 459 | GMainLoop *loop_; |
| 460 | }; |
| 461 | |
| 462 | gboolean TerminateTransferTestStarter(gpointer data) { |
| 463 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 464 | processor->StartProcessing(); |
| 465 | CHECK(processor->IsRunning()); |
| 466 | processor->StopProcessing(); |
| 467 | return FALSE; |
| 468 | } |
| 469 | } // namespace {} |
| 470 | |
| 471 | TEST(OmahaRequestActionTest, TerminateTransferTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 472 | string http_response("doesn't matter"); |
| 473 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 474 | |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 475 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 476 | OmahaRequestAction action(&prefs, kDefaultTestParams, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 477 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 478 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 479 | NULL), |
| 480 | false); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 481 | TerminateEarlyTestProcessorDelegate delegate; |
| 482 | delegate.loop_ = loop; |
| 483 | ActionProcessor processor; |
| 484 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 485 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 486 | |
| 487 | g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
| 488 | g_main_loop_run(loop); |
| 489 | g_main_loop_unref(loop); |
| 490 | } |
| 491 | |
| 492 | TEST(OmahaRequestActionTest, XmlEncodeTest) { |
| 493 | EXPECT_EQ("ab", XmlEncode("ab")); |
| 494 | EXPECT_EQ("a<b", XmlEncode("a<b")); |
| 495 | EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
| 496 | EXPECT_EQ("<&>", XmlEncode("<&>")); |
| 497 | EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>")); |
| 498 | |
| 499 | vector<char> post_data; |
| 500 | |
| 501 | // Make sure XML Encode is being called on the params |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 502 | OmahaRequestParams params(OmahaRequestParams::kOsPlatform, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 503 | OmahaRequestParams::kOsVersion, |
| 504 | "testtheservice_pack>", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 505 | "x86 generic<id", |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 506 | OmahaRequestParams::kAppId, |
| 507 | "0.1.0.0", |
| 508 | "en-US", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 509 | "unittest_track<", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 510 | "<OEM MODEL>", |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 511 | false, // delta okay |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 512 | "http://url", |
| 513 | false, // update_disabled |
| 514 | ""); // target_version_prefix |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 515 | OmahaResponse response; |
| 516 | ASSERT_FALSE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 517 | TestUpdateCheck(NULL, // prefs |
| 518 | params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 519 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 520 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 521 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 522 | kActionCodeOmahaRequestXMLParseError, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 523 | &response, |
| 524 | &post_data)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 525 | // convert post_data to string |
| 526 | string post_str(&post_data[0], post_data.size()); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 527 | EXPECT_NE(post_str.find("testtheservice_pack>"), string::npos); |
| 528 | EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 529 | EXPECT_NE(post_str.find("x86 generic<id"), string::npos); |
| 530 | EXPECT_EQ(post_str.find("x86 generic<id"), string::npos); |
| 531 | EXPECT_NE(post_str.find("unittest_track&lt;"), string::npos); |
| 532 | EXPECT_EQ(post_str.find("unittest_track<"), string::npos); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 533 | EXPECT_NE(post_str.find("<OEM MODEL>"), string::npos); |
| 534 | EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | TEST(OmahaRequestActionTest, XmlDecodeTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 538 | OmahaResponse response; |
| 539 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 540 | TestUpdateCheck(NULL, // prefs |
| 541 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 542 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 543 | "1.2.3.4", // version |
| 544 | "testthe<url", // more info |
| 545 | "true", // prompt |
| 546 | "testthe&codebase", // dl url |
| 547 | "HASH1234=", // checksum |
| 548 | "false", // needs admin |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 549 | "123", // size |
| 550 | "<20110101"), // deadline |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 551 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 552 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 553 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 554 | &response, |
| 555 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 556 | |
| 557 | EXPECT_EQ(response.more_info_url, "testthe<url"); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 558 | EXPECT_EQ(response.codebase, "testthe&codebase"); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 559 | EXPECT_EQ(response.deadline, "<20110101"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | TEST(OmahaRequestActionTest, ParseIntTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 563 | OmahaResponse response; |
| 564 | ASSERT_TRUE( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 565 | TestUpdateCheck(NULL, // prefs |
| 566 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 567 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 568 | "1.2.3.4", // version |
| 569 | "theurl", // more info |
| 570 | "true", // prompt |
| 571 | "thecodebase", // dl url |
| 572 | "HASH1234=", // checksum |
| 573 | "false", // needs admin |
| 574 | // overflows int32: |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 575 | "123123123123123", // size |
| 576 | "deadline"), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 577 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 578 | false, // ping_only |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 579 | kActionCodeSuccess, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 580 | &response, |
| 581 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 582 | |
| 583 | EXPECT_EQ(response.size, 123123123123123ll); |
| 584 | } |
| 585 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 586 | TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { |
| 587 | vector<char> post_data; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 588 | NiceMock<PrefsMock> prefs; |
| 589 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) |
| 590 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); |
| 591 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0); |
| 592 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 593 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 594 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 595 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 596 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 597 | kActionCodeOmahaRequestXMLParseError, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 598 | NULL, // response |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 599 | &post_data)); |
| 600 | // convert post_data to string |
| 601 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 602 | EXPECT_NE(post_str.find( |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 603 | " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n" |
| 604 | " <o:updatecheck" |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 605 | " targetversionprefix=\"\"" |
| 606 | "></o:updatecheck>\n"), |
| 607 | string::npos); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 608 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), |
| 609 | string::npos); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 610 | EXPECT_EQ(post_str.find("o:event"), string::npos); |
| 611 | } |
| 612 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 613 | |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 614 | TEST(OmahaRequestActionTest, FormatTargetVersionPrefixTest) { |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 615 | vector<char> post_data; |
| 616 | NiceMock<PrefsMock> prefs; |
| 617 | EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 618 | .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true))); |
| 619 | EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0); |
| 620 | OmahaRequestParams params = kDefaultTestParams; |
| 621 | params.update_disabled = true; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 622 | ASSERT_FALSE(TestUpdateCheck(&prefs, |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 623 | params, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 624 | "invalid xml>", |
| 625 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 626 | false, // ping_only |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 627 | kActionCodeOmahaRequestXMLParseError, |
| 628 | NULL, // response |
| 629 | &post_data)); |
| 630 | // convert post_data to string |
| 631 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 632 | EXPECT_NE(post_str.find( |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 633 | " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n" |
| 634 | " <o:updatecheck" |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 635 | " targetversionprefix=\"\"" |
| 636 | "></o:updatecheck>\n"), |
| 637 | string::npos); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 638 | EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), |
| 639 | string::npos); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 640 | EXPECT_EQ(post_str.find("o:event"), string::npos); |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 643 | TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) { |
| 644 | vector<char> post_data; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 645 | TestEvent(kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 646 | new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted), |
| 647 | "invalid xml>", |
| 648 | &post_data); |
| 649 | // convert post_data to string |
| 650 | string post_str(&post_data[0], post_data.size()); |
| 651 | string expected_event = StringPrintf( |
| 652 | " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n", |
| 653 | OmahaEvent::kTypeUpdateDownloadStarted, |
| 654 | OmahaEvent::kResultSuccess); |
| 655 | EXPECT_NE(post_str.find(expected_event), string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 656 | EXPECT_EQ(post_str.find("o:ping"), string::npos); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 657 | EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); |
| 658 | } |
| 659 | |
| 660 | TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) { |
| 661 | vector<char> post_data; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 662 | TestEvent(kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 663 | new OmahaEvent(OmahaEvent::kTypeDownloadComplete, |
| 664 | OmahaEvent::kResultError, |
| 665 | kActionCodeError), |
| 666 | "invalid xml>", |
| 667 | &post_data); |
| 668 | // convert post_data to string |
| 669 | string post_str(&post_data[0], post_data.size()); |
| 670 | string expected_event = StringPrintf( |
| 671 | " <o:event eventtype=\"%d\" eventresult=\"%d\" " |
| 672 | "errorcode=\"%d\"></o:event>\n", |
| 673 | OmahaEvent::kTypeDownloadComplete, |
| 674 | OmahaEvent::kResultError, |
Darin Petkov | 44d98d9 | 2011-03-21 16:08:11 -0700 | [diff] [blame] | 675 | kActionCodeError); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 676 | EXPECT_NE(post_str.find(expected_event), string::npos); |
| 677 | EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); |
| 678 | } |
| 679 | |
| 680 | TEST(OmahaRequestActionTest, IsEventTest) { |
| 681 | string http_response("doesn't matter"); |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 682 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 683 | OmahaRequestAction update_check_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 684 | &prefs, |
| 685 | kDefaultTestParams, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 686 | NULL, |
| 687 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 688 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 689 | NULL), |
| 690 | false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 691 | EXPECT_FALSE(update_check_action.IsEvent()); |
| 692 | |
| 693 | OmahaRequestAction event_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 694 | &prefs, |
| 695 | kDefaultTestParams, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 696 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 697 | new MockHttpFetcher(http_response.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 698 | http_response.size(), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 699 | NULL), |
| 700 | false); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 701 | EXPECT_TRUE(event_action.IsEvent()); |
| 702 | } |
| 703 | |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 704 | TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) { |
| 705 | for (int i = 0; i < 2; i++) { |
| 706 | bool delta_okay = i == 1; |
| 707 | const char* delta_okay_str = delta_okay ? "true" : "false"; |
| 708 | vector<char> post_data; |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 709 | OmahaRequestParams params(OmahaRequestParams::kOsPlatform, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 710 | OmahaRequestParams::kOsVersion, |
| 711 | "service_pack", |
| 712 | "x86-generic", |
| 713 | OmahaRequestParams::kAppId, |
| 714 | "0.1.0.0", |
| 715 | "en-US", |
| 716 | "unittest_track", |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 717 | "OEM MODEL REV 1234", |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 718 | delta_okay, |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 719 | "http://url", |
| 720 | false, // update_disabled |
| 721 | ""); // target_version_prefix |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 722 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
| 723 | params, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 724 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 725 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 726 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 727 | kActionCodeOmahaRequestXMLParseError, |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 728 | NULL, |
| 729 | &post_data)); |
| 730 | // convert post_data to string |
| 731 | string post_str(&post_data[0], post_data.size()); |
| 732 | EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)), |
| 733 | string::npos) |
| 734 | << "i = " << i; |
| 735 | } |
| 736 | } |
| 737 | |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 738 | TEST(OmahaRequestActionTest, OmahaEventTest) { |
| 739 | OmahaEvent default_event; |
| 740 | EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type); |
| 741 | EXPECT_EQ(OmahaEvent::kResultError, default_event.result); |
| 742 | EXPECT_EQ(kActionCodeError, default_event.error_code); |
| 743 | |
| 744 | OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted); |
| 745 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type); |
| 746 | EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result); |
| 747 | EXPECT_EQ(kActionCodeSuccess, success_event.error_code); |
| 748 | |
| 749 | OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished, |
| 750 | OmahaEvent::kResultError, |
| 751 | kActionCodeError); |
| 752 | EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type); |
| 753 | EXPECT_EQ(OmahaEvent::kResultError, error_event.result); |
| 754 | EXPECT_EQ(kActionCodeError, error_event.error_code); |
| 755 | } |
| 756 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 757 | TEST(OmahaRequestActionTest, PingTest) { |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 758 | for (int ping_only = 0; ping_only < 2; ping_only++) { |
| 759 | NiceMock<PrefsMock> prefs; |
| 760 | // Add a few hours to the day difference to test no rounding, etc. |
| 761 | int64_t five_days_ago = |
| 762 | (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue(); |
| 763 | int64_t six_days_ago = |
| 764 | (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue(); |
| 765 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 766 | .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true))); |
| 767 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 768 | .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true))); |
| 769 | vector<char> post_data; |
| 770 | ASSERT_TRUE( |
| 771 | TestUpdateCheck(&prefs, |
| 772 | kDefaultTestParams, |
| 773 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 774 | -1, |
| 775 | ping_only, |
| 776 | kActionCodeSuccess, |
| 777 | NULL, |
| 778 | &post_data)); |
| 779 | string post_str(&post_data[0], post_data.size()); |
| 780 | EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"), |
| 781 | string::npos); |
| 782 | if (ping_only) { |
| 783 | EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); |
| 784 | EXPECT_EQ(post_str.find("previousversion"), string::npos); |
| 785 | } else { |
| 786 | EXPECT_NE(post_str.find("o:updatecheck"), string::npos); |
| 787 | EXPECT_NE(post_str.find("previousversion"), string::npos); |
| 788 | } |
| 789 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | TEST(OmahaRequestActionTest, ActivePingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 793 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 794 | int64_t three_days_ago = |
| 795 | (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue(); |
| 796 | int64_t now = Time::Now().ToInternalValue(); |
| 797 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 798 | .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true))); |
| 799 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 800 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 801 | vector<char> post_data; |
| 802 | ASSERT_TRUE( |
| 803 | TestUpdateCheck(&prefs, |
| 804 | kDefaultTestParams, |
| 805 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 806 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 807 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 808 | kActionCodeSuccess, |
| 809 | NULL, |
| 810 | &post_data)); |
| 811 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 812 | EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"), |
| 813 | string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | TEST(OmahaRequestActionTest, RollCallPingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 817 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 818 | int64_t four_days_ago = |
| 819 | (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue(); |
| 820 | int64_t now = Time::Now().ToInternalValue(); |
| 821 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 822 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 823 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 824 | .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true))); |
| 825 | vector<char> post_data; |
| 826 | ASSERT_TRUE( |
| 827 | TestUpdateCheck(&prefs, |
| 828 | kDefaultTestParams, |
| 829 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 830 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 831 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 832 | kActionCodeSuccess, |
| 833 | NULL, |
| 834 | &post_data)); |
| 835 | string post_str(&post_data[0], post_data.size()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 836 | EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"), |
| 837 | string::npos); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | TEST(OmahaRequestActionTest, NoPingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 841 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 842 | int64_t one_hour_ago = |
| 843 | (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue(); |
| 844 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 845 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); |
| 846 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 847 | .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true))); |
| 848 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 849 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 850 | vector<char> post_data; |
| 851 | ASSERT_TRUE( |
| 852 | TestUpdateCheck(&prefs, |
| 853 | kDefaultTestParams, |
| 854 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 855 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 856 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 857 | kActionCodeSuccess, |
| 858 | NULL, |
| 859 | &post_data)); |
| 860 | string post_str(&post_data[0], post_data.size()); |
| 861 | EXPECT_EQ(post_str.find("o:ping"), string::npos); |
| 862 | } |
| 863 | |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 864 | TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) { |
| 865 | // This test ensures that we ignore empty ping only requests. |
| 866 | NiceMock<PrefsMock> prefs; |
| 867 | int64_t now = Time::Now().ToInternalValue(); |
| 868 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 869 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 870 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 871 | .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true))); |
| 872 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 873 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 874 | vector<char> post_data; |
| 875 | EXPECT_TRUE( |
| 876 | TestUpdateCheck(&prefs, |
| 877 | kDefaultTestParams, |
| 878 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 879 | -1, |
| 880 | true, // ping_only |
| 881 | kActionCodeSuccess, |
| 882 | NULL, |
| 883 | &post_data)); |
| 884 | EXPECT_EQ(post_data.size(), 0); |
| 885 | } |
| 886 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 887 | TEST(OmahaRequestActionTest, BackInTimePingTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 888 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 889 | int64_t future = |
| 890 | (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue(); |
| 891 | EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) |
| 892 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); |
| 893 | EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) |
| 894 | .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true))); |
| 895 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) |
| 896 | .WillOnce(Return(true)); |
| 897 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) |
| 898 | .WillOnce(Return(true)); |
| 899 | vector<char> post_data; |
| 900 | ASSERT_TRUE( |
| 901 | TestUpdateCheck(&prefs, |
| 902 | kDefaultTestParams, |
| 903 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 904 | "xmlns=\"http://www.google.com/update2/response\" " |
| 905 | "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>" |
| 906 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
| 907 | "<updatecheck status=\"noupdate\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 908 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 909 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 910 | kActionCodeSuccess, |
| 911 | NULL, |
| 912 | &post_data)); |
| 913 | string post_str(&post_data[0], post_data.size()); |
| 914 | EXPECT_EQ(post_str.find("o:ping"), string::npos); |
| 915 | } |
| 916 | |
| 917 | TEST(OmahaRequestActionTest, LastPingDayUpdateTest) { |
| 918 | // 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] | 919 | // minus 200 seconds with a slack of 5 seconds. Therefore, the test |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 920 | // may fail if it runs for longer than 5 seconds. It shouldn't run |
| 921 | // that long though. |
| 922 | int64_t midnight = |
| 923 | (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue(); |
| 924 | int64_t midnight_slack = |
| 925 | (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue(); |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 926 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 927 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, |
| 928 | AllOf(Ge(midnight), Le(midnight_slack)))) |
| 929 | .WillOnce(Return(true)); |
| 930 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, |
| 931 | AllOf(Ge(midnight), Le(midnight_slack)))) |
| 932 | .WillOnce(Return(true)); |
| 933 | ASSERT_TRUE( |
| 934 | TestUpdateCheck(&prefs, |
| 935 | kDefaultTestParams, |
| 936 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 937 | "xmlns=\"http://www.google.com/update2/response\" " |
| 938 | "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>" |
| 939 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
| 940 | "<updatecheck status=\"noupdate\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 941 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 942 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 943 | kActionCodeSuccess, |
| 944 | NULL, |
| 945 | NULL)); |
| 946 | } |
| 947 | |
| 948 | TEST(OmahaRequestActionTest, NoElapsedSecondsTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 949 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 950 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 951 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 952 | ASSERT_TRUE( |
| 953 | TestUpdateCheck(&prefs, |
| 954 | kDefaultTestParams, |
| 955 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 956 | "xmlns=\"http://www.google.com/update2/response\" " |
| 957 | "protocol=\"2.0\"><daystart blah=\"200\"/>" |
| 958 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
| 959 | "<updatecheck status=\"noupdate\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 960 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 961 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 962 | kActionCodeSuccess, |
| 963 | NULL, |
| 964 | NULL)); |
| 965 | } |
| 966 | |
| 967 | TEST(OmahaRequestActionTest, BadElapsedSecondsTest) { |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 968 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 969 | EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); |
| 970 | EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); |
| 971 | ASSERT_TRUE( |
| 972 | TestUpdateCheck(&prefs, |
| 973 | kDefaultTestParams, |
| 974 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 975 | "xmlns=\"http://www.google.com/update2/response\" " |
| 976 | "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>" |
| 977 | "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" |
| 978 | "<updatecheck status=\"noupdate\"/></app></gupdate>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 979 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 980 | false, // ping_only |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 981 | kActionCodeSuccess, |
| 982 | NULL, |
| 983 | NULL)); |
| 984 | } |
| 985 | |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 986 | TEST(OmahaRequestActionTest, NoUniqueIDTest) { |
| 987 | vector<char> post_data; |
| 988 | ASSERT_FALSE(TestUpdateCheck(NULL, // prefs |
| 989 | kDefaultTestParams, |
| 990 | "invalid xml>", |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 991 | -1, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 992 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 993 | kActionCodeOmahaRequestXMLParseError, |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 994 | NULL, // response |
| 995 | &post_data)); |
| 996 | // convert post_data to string |
| 997 | string post_str(&post_data[0], post_data.size()); |
| 998 | EXPECT_EQ(post_str.find("machineid="), string::npos); |
| 999 | EXPECT_EQ(post_str.find("userid="), string::npos); |
| 1000 | } |
| 1001 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1002 | TEST(OmahaRequestActionTest, NetworkFailureTest) { |
| 1003 | OmahaResponse response; |
| 1004 | ASSERT_FALSE( |
| 1005 | TestUpdateCheck(NULL, // prefs |
| 1006 | kDefaultTestParams, |
| 1007 | "", |
| 1008 | 501, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1009 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1010 | static_cast<ActionExitCode>( |
| 1011 | kActionCodeOmahaRequestHTTPResponseBase + 501), |
| 1012 | &response, |
| 1013 | NULL)); |
| 1014 | EXPECT_FALSE(response.update_exists); |
| 1015 | } |
| 1016 | |
| 1017 | TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) { |
| 1018 | OmahaResponse response; |
| 1019 | ASSERT_FALSE( |
| 1020 | TestUpdateCheck(NULL, // prefs |
| 1021 | kDefaultTestParams, |
| 1022 | "", |
| 1023 | 1500, |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1024 | false, // ping_only |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1025 | static_cast<ActionExitCode>( |
| 1026 | kActionCodeOmahaRequestHTTPResponseBase + 999), |
| 1027 | &response, |
| 1028 | NULL)); |
| 1029 | EXPECT_FALSE(response.update_exists); |
| 1030 | } |
| 1031 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1032 | } // namespace chromeos_update_engine |