blob: 428a286675183064be3a0e01cb80e94021f55258 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// 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 Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
10#include "base/string_util.h"
Mike Frysinger8155d082012-04-06 15:23:18 -040011#include <base/stringprintf.h>
Darin Petkov1cbd78f2010-07-29 12:38:34 -070012#include "base/time.h"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070013#include "gtest/gtest.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070014
Darin Petkov6a5b3222010-07-13 14:55:28 -070015#include "update_engine/action_pipe.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070016#include "update_engine/constants.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070017#include "update_engine/mock_http_fetcher.h"
18#include "update_engine/omaha_hash_calculator.h"
19#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070020#include "update_engine/omaha_request_params.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070021#include "update_engine/prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070022#include "update_engine/test_utils.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070023#include "update_engine/utils.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070024
Darin Petkov1cbd78f2010-07-29 12:38:34 -070025using base::Time;
26using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070027using std::string;
28using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070029using testing::_;
30using testing::AllOf;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070031using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070032using testing::Ge;
33using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080034using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070035using testing::Return;
36using testing::SetArgumentPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070037
38namespace chromeos_update_engine {
39
Jay Srinivasanae4697c2013-03-18 17:08:08 -070040class OmahaRequestActionTest : public ::testing::Test {};
Darin Petkov6a5b3222010-07-13 14:55:28 -070041
42namespace {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070043
Jay Srinivasanae4697c2013-03-18 17:08:08 -070044MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070045OmahaRequestParams kDefaultTestParams(
Jay Srinivasanae4697c2013-03-18 17:08:08 -070046 &mock_system_state,
Darin Petkov1cbd78f2010-07-29 12:38:34 -070047 OmahaRequestParams::kOsPlatform,
48 OmahaRequestParams::kOsVersion,
49 "service_pack",
50 "x86-generic",
51 OmahaRequestParams::kAppId,
52 "0.1.0.0",
53 "en-US",
54 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070055 "OEM MODEL 09235 7471",
Chris Sosac1972482013-04-30 22:31:10 -070056 "ChromeOSFirmware.1.0",
57 "0X0A1",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058 false, // delta okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080059 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -070060 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -070061 false, // update_disabled
62 "", // target_version_prefix
63 false, // use_p2p_for_downloading
64 false); // use_p2p_for_sharing
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065
Darin Petkov6a5b3222010-07-13 14:55:28 -070066string GetNoUpdateResponse(const string& app_id) {
67 return string(
Jay Srinivasan23b92a52012-10-27 02:00:21 -070068 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
69 "<daystart elapsed_seconds=\"100\"/>"
70 "<app appid=\"") + app_id + "\" status=\"ok\"><ping "
71 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></response>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070072}
73
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070074string GetUpdateResponse2(const string& app_id,
Chris Sosa3b748432013-06-20 16:42:59 -070075 const string& version,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070076 const string& more_info_url,
77 const string& prompt,
78 const string& codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -070079 const string& filename,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070080 const string& hash,
81 const string& needsadmin,
82 const string& size,
83 const string& deadline,
David Zeuthen8f191b22013-08-06 12:27:50 -070084 const string& max_days_to_scatter,
85 bool disable_p2p_for_downloading,
86 bool disable_p2p_for_sharing) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -070087 string response =
88 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
89 "protocol=\"3.0\">"
90 "<daystart elapsed_seconds=\"100\"/>"
91 "<app appid=\"" + app_id + "\" status=\"ok\">"
92 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
93 "<urls><url codebase=\"" + codebase + "\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -070094 "<manifest version=\"" + version + "\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -070095 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
96 "size=\"" + size + "\"/></packages>"
97 "<actions><action event=\"postinstall\" "
Chris Sosa3b748432013-06-20 16:42:59 -070098 "ChromeOSVersion=\"" + version + "\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070099 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
100 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800101 "IsDeltaPayload=\"true\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700102 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700103 "sha256=\"" + hash + "\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700104 "needsadmin=\"" + needsadmin + "\" " +
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700105 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
David Zeuthen8f191b22013-08-06 12:27:50 -0700106 (disable_p2p_for_downloading ?
107 "DisableP2PForDownloading=\"true\" " : "") +
108 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700109 "/></actions></manifest></updatecheck></app></response>";
110 LOG(INFO) << "Response = " << response;
111 return response;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700112}
113
Darin Petkov6a5b3222010-07-13 14:55:28 -0700114string GetUpdateResponse(const string& app_id,
Chris Sosa3b748432013-06-20 16:42:59 -0700115 const string& version,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700116 const string& more_info_url,
117 const string& prompt,
118 const string& codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700119 const string& filename,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700120 const string& hash,
121 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -0700122 const string& size,
123 const string& deadline) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700124 return GetUpdateResponse2(app_id,
Chris Sosa3b748432013-06-20 16:42:59 -0700125 version,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700126 more_info_url,
127 prompt,
128 codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700129 filename,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700130 hash,
131 needsadmin,
132 size,
133 deadline,
David Zeuthen8f191b22013-08-06 12:27:50 -0700134 "7",
135 false, // disable_p2p_for_downloading
136 false); // disable_p2p_for sharing
Darin Petkov6a5b3222010-07-13 14:55:28 -0700137}
138
139class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
140 public:
141 OmahaRequestActionTestProcessorDelegate()
142 : loop_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -0700143 expected_code_(kErrorCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700144 virtual ~OmahaRequestActionTestProcessorDelegate() {
145 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700146 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700147 ErrorCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700148 ASSERT_TRUE(loop_);
149 g_main_loop_quit(loop_);
150 }
151
152 virtual void ActionCompleted(ActionProcessor* processor,
153 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700154 ErrorCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700155 // make sure actions always succeed
156 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700157 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700158 else
David Zeuthena99981f2013-04-29 13:42:47 -0700159 EXPECT_EQ(kErrorCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700160 }
161 GMainLoop *loop_;
David Zeuthena99981f2013-04-29 13:42:47 -0700162 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700163};
164
165gboolean StartProcessorInRunLoop(gpointer data) {
166 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
167 processor->StartProcessing();
168 return FALSE;
169}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700170} // namespace {}
171
172class OutputObjectCollectorAction;
173
174template<>
175class ActionTraits<OutputObjectCollectorAction> {
176 public:
177 // Does not take an object for input
178 typedef OmahaResponse InputObjectType;
179 // On success, puts the output path on output
180 typedef NoneType OutputObjectType;
181};
182
183class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
184 public:
185 OutputObjectCollectorAction() : has_input_object_(false) {}
186 void PerformAction() {
187 // copy input object
188 has_input_object_ = HasInputObject();
189 if (has_input_object_)
190 omaha_response_ = GetInputObject();
David Zeuthena99981f2013-04-29 13:42:47 -0700191 processor_->ActionComplete(this, kErrorCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700192 }
193 // Should never be called
194 void TerminateProcessing() {
195 CHECK(false);
196 }
197 // Debugging/logging
198 static std::string StaticType() {
199 return "OutputObjectCollectorAction";
200 }
201 std::string Type() const { return StaticType(); }
202 bool has_input_object_;
203 OmahaResponse omaha_response_;
204};
205
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700206// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700207// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
David Zeuthen8f191b22013-08-06 12:27:50 -0700208// used. |payload_state| may be NULL, in which case a local mock is used.
209// |p2p_manager| may be NULL, in which case a local mock is used.
210// out_response may be NULL. If |fail_http_response_code| is non-negative,
Darin Petkov265f2902011-05-09 15:17:40 -0700211// the transfer will fail with that code. |ping_only| is passed through to the
212// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
213// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700214bool TestUpdateCheck(PrefsInterface* prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -0700215 PayloadStateInterface *payload_state,
216 P2PManager *p2p_manager,
217 OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700218 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700219 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700220 bool ping_only,
David Zeuthena99981f2013-04-29 13:42:47 -0700221 ErrorCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700222 OmahaResponse* out_response,
223 vector<char>* out_post_data) {
224 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
225 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800226 http_response.size(),
227 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700228 if (fail_http_response_code >= 0) {
229 fetcher->FailTransfer(fail_http_response_code);
230 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800231 MockSystemState mock_system_state;
232 if (prefs)
233 mock_system_state.set_prefs(prefs);
David Zeuthen8f191b22013-08-06 12:27:50 -0700234 if (payload_state)
235 mock_system_state.set_payload_state(payload_state);
236 if (p2p_manager)
237 mock_system_state.set_p2p_manager(p2p_manager);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700238 mock_system_state.set_request_params(&params);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800239 OmahaRequestAction action(&mock_system_state,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700240 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700241 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700242 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700243 OmahaRequestActionTestProcessorDelegate delegate;
244 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700245 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700246
Darin Petkov6a5b3222010-07-13 14:55:28 -0700247 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700248 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700249 processor.EnqueueAction(&action);
250
251 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700252 BondActions(&action, &collector_action);
253 processor.EnqueueAction(&collector_action);
254
255 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
256 g_main_loop_run(loop);
257 g_main_loop_unref(loop);
258 if (collector_action.has_input_object_ && out_response)
259 *out_response = collector_action.omaha_response_;
260 if (out_post_data)
261 *out_post_data = fetcher->post_data();
262 return collector_action.has_input_object_;
263}
264
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700265// Tests Event requests -- they should always succeed. |out_post_data|
266// may be null; if non-null, the post-data received by the mock
267// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700268void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700269 OmahaEvent* event,
270 const string& http_response,
271 vector<char>* out_post_data) {
272 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
273 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800274 http_response.size(),
275 NULL);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800276 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700277 mock_system_state.set_request_params(&params);
278 OmahaRequestAction action(&mock_system_state, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700279 OmahaRequestActionTestProcessorDelegate delegate;
280 delegate.loop_ = loop;
281 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700282 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700283 processor.EnqueueAction(&action);
284
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700285 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
286 g_main_loop_run(loop);
287 g_main_loop_unref(loop);
288 if (out_post_data)
289 *out_post_data = fetcher->post_data();
290}
291
Darin Petkov6a5b3222010-07-13 14:55:28 -0700292TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700293 OmahaResponse response;
294 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700295 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700296 NULL, // payload_state
297 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700298 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700299 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700300 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700301 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700302 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700303 &response,
304 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700305 EXPECT_FALSE(response.update_exists);
306}
307
308TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700309 OmahaResponse response;
310 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700311 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700312 NULL, // payload_state
313 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700314 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700315 GetUpdateResponse(OmahaRequestParams::kAppId,
316 "1.2.3.4", // version
317 "http://more/info",
318 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700319 "http://code/base/", // dl url
320 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700321 "HASH1234=", // checksum
322 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700323 "123", // size
324 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700325 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700326 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700327 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700328 &response,
329 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700331 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700332 EXPECT_EQ("1.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800333 EXPECT_EQ("http://code/base/file.signed", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700334 EXPECT_EQ("http://more/info", response.more_info_url);
335 EXPECT_EQ("HASH1234=", response.hash);
336 EXPECT_EQ(123, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700337 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700338 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700339}
340
Jay Srinivasan0a708742012-03-20 11:26:12 -0700341TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) {
342 OmahaResponse response;
343 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700344 params.set_update_disabled(true);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700345 ASSERT_FALSE(
346 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700347 NULL, // payload_state
348 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -0700349 params,
350 GetUpdateResponse(OmahaRequestParams::kAppId,
351 "1.2.3.4", // version
352 "http://more/info",
353 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700354 "http://code/base/", // dl url
355 "file.signed", // file name
Jay Srinivasan0a708742012-03-20 11:26:12 -0700356 "HASH1234=", // checksum
357 "false", // needs admin
358 "123", // size
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700359 ""), // deadline
Jay Srinivasan0a708742012-03-20 11:26:12 -0700360 -1,
361 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700362 kErrorCodeOmahaUpdateIgnoredPerPolicy,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700363 &response,
364 NULL));
365 EXPECT_FALSE(response.update_exists);
366}
367
Jay Srinivasan0a708742012-03-20 11:26:12 -0700368TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) {
369 OmahaResponse response;
370 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700371 params.set_update_disabled(true);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700372 ASSERT_TRUE(
373 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700374 NULL, // payload_state
375 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -0700376 params,
377 GetNoUpdateResponse(OmahaRequestParams::kAppId),
378 -1,
379 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700380 kErrorCodeSuccess,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700381 &response,
382 NULL));
383 EXPECT_FALSE(response.update_exists);
384}
385
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700386TEST(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
387 OmahaResponse response;
388 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700389 params.set_wall_clock_based_wait_enabled(true);
390 params.set_update_check_count_wait_enabled(false);
391 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700392
393 string prefs_dir;
394 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
395 &prefs_dir));
396 ScopedDirRemover temp_dir_remover(prefs_dir);
397
398 Prefs prefs;
399 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
400 << "Failed to initialize preferences.";
401
402 ASSERT_FALSE(
403 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700404 NULL, // payload_state
405 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700406 params,
407 GetUpdateResponse2(OmahaRequestParams::kAppId,
408 "1.2.3.4", // version
409 "http://more/info",
410 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700411 "http://code/base/", // dl url
412 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700413 "HASH1234=", // checksum
414 "false", // needs admin
415 "123", // size
416 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700417 "7", // max days to scatter
418 false, // disable_p2p_for_downloading
419 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700420 -1,
421 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700422 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700423 &response,
424 NULL));
425 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700426
427 // Verify if we are interactive check we don't defer.
428 params.set_interactive(true);
429 ASSERT_TRUE(
430 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700431 NULL, // payload_state
432 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700433 params,
434 GetUpdateResponse2(OmahaRequestParams::kAppId,
435 "1.2.3.4", // version
436 "http://more/info",
437 "true", // prompt
438 "http://code/base/", // dl url
439 "file.signed", // file name
440 "HASH1234=", // checksum
441 "false", // needs admin
442 "123", // size
443 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700444 "7", // max days to scatter
445 false, // disable_p2p_for_downloading
446 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700447 -1,
448 false, // ping_only
449 kErrorCodeSuccess,
450 &response,
451 NULL));
452 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700453}
454
455TEST(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
456 OmahaResponse response;
457 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700458 params.set_wall_clock_based_wait_enabled(false);
459 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700460
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700461 params.set_update_check_count_wait_enabled(true);
462 params.set_min_update_checks_needed(1);
463 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700464
465 string prefs_dir;
466 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
467 &prefs_dir));
468 ScopedDirRemover temp_dir_remover(prefs_dir);
469
470 Prefs prefs;
471 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
472 << "Failed to initialize preferences.";
473
474 ASSERT_TRUE(
475 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700476 NULL, // payload_state
477 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700478 params,
479 GetUpdateResponse2(OmahaRequestParams::kAppId,
480 "1.2.3.4", // version
481 "http://more/info",
482 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700483 "http://code/base/", // dl url
484 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700485 "HASH1234=", // checksum
486 "false", // needs admin
487 "123", // size
488 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700489 "7", // max days to scatter
490 false, // disable_p2p_for_downloading
491 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700492 -1,
493 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700494 kErrorCodeSuccess,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700495 &response,
496 NULL));
497 EXPECT_TRUE(response.update_exists);
498}
499
500TEST(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
501 OmahaResponse response;
502 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700503 params.set_wall_clock_based_wait_enabled(true);
504 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700505
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700506 params.set_update_check_count_wait_enabled(true);
507 params.set_min_update_checks_needed(1);
508 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700509
510 string prefs_dir;
511 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
512 &prefs_dir));
513 ScopedDirRemover temp_dir_remover(prefs_dir);
514
515 Prefs prefs;
516 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
517 << "Failed to initialize preferences.";
518
519 ASSERT_TRUE(
520 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700521 NULL, // payload_state
522 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700523 params,
524 GetUpdateResponse2(OmahaRequestParams::kAppId,
525 "1.2.3.4", // version
526 "http://more/info",
527 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700528 "http://code/base/", // dl url
529 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700530 "HASH1234=", // checksum
531 "false", // needs admin
532 "123", // size
533 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700534 "0", // max days to scatter
535 false, // disable_p2p_for_downloading
536 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700537 -1,
538 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700539 kErrorCodeSuccess,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700540 &response,
541 NULL));
542 EXPECT_TRUE(response.update_exists);
543}
544
545
546TEST(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
547 OmahaResponse response;
548 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700549 params.set_wall_clock_based_wait_enabled(true);
550 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700551
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700552 params.set_update_check_count_wait_enabled(true);
553 params.set_min_update_checks_needed(0);
554 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700555
556 string prefs_dir;
557 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
558 &prefs_dir));
559 ScopedDirRemover temp_dir_remover(prefs_dir);
560
561 Prefs prefs;
562 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
563 << "Failed to initialize preferences.";
564
565 ASSERT_TRUE(TestUpdateCheck(
566 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700567 NULL, // payload_state
568 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700569 params,
570 GetUpdateResponse2(OmahaRequestParams::kAppId,
571 "1.2.3.4", // version
572 "http://more/info",
573 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700574 "http://code/base/", // dl url
575 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700576 "HASH1234=", // checksum
577 "false", // needs admin
578 "123", // size
579 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700580 "7", // max days to scatter
581 false, // disable_p2p_for_downloading
582 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700583 -1,
584 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700585 kErrorCodeSuccess,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700586 &response,
587 NULL));
588
589 int64 count;
590 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
591 ASSERT_TRUE(count == 0);
592 EXPECT_TRUE(response.update_exists);
593}
594
595TEST(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
596 OmahaResponse response;
597 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700598 params.set_wall_clock_based_wait_enabled(true);
599 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700600
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700601 params.set_update_check_count_wait_enabled(true);
602 params.set_min_update_checks_needed(1);
603 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700604
605 string prefs_dir;
606 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
607 &prefs_dir));
608 ScopedDirRemover temp_dir_remover(prefs_dir);
609
610 Prefs prefs;
611 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
612 << "Failed to initialize preferences.";
613
614 ASSERT_FALSE(TestUpdateCheck(
615 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700616 NULL, // payload_state
617 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700618 params,
619 GetUpdateResponse2(OmahaRequestParams::kAppId,
620 "1.2.3.4", // version
621 "http://more/info",
622 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700623 "http://code/base/", // dl url
624 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700625 "HASH1234=", // checksum
626 "false", // needs admin
627 "123", // size
628 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700629 "7", // max days to scatter
630 false, // disable_p2p_for_downloading
631 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700632 -1,
633 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700634 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700635 &response,
636 NULL));
637
638 int64 count;
639 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
640 ASSERT_TRUE(count > 0);
641 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700642
643 // Verify if we are interactive check we don't defer.
644 params.set_interactive(true);
645 ASSERT_TRUE(
646 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700647 NULL, // payload_state
648 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700649 params,
650 GetUpdateResponse2(OmahaRequestParams::kAppId,
651 "1.2.3.4", // version
652 "http://more/info",
653 "true", // prompt
654 "http://code/base/", // dl url
655 "file.signed", // file name
656 "HASH1234=", // checksum
657 "false", // needs admin
658 "123", // size
659 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700660 "7", // max days to scatter
661 false, // disable_p2p_for_downloading
662 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700663 -1,
664 false, // ping_only
665 kErrorCodeSuccess,
666 &response,
667 NULL));
668 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700669}
670
671TEST(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
672 OmahaResponse response;
673 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700674 params.set_wall_clock_based_wait_enabled(true);
675 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700676
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700677 params.set_update_check_count_wait_enabled(true);
678 params.set_min_update_checks_needed(1);
679 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700680
681 string prefs_dir;
682 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
683 &prefs_dir));
684 ScopedDirRemover temp_dir_remover(prefs_dir);
685
686 Prefs prefs;
687 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
688 << "Failed to initialize preferences.";
689
690 ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, 5));
691
692 ASSERT_FALSE(TestUpdateCheck(
693 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700694 NULL, // payload_state
695 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700696 params,
697 GetUpdateResponse2(OmahaRequestParams::kAppId,
698 "1.2.3.4", // version
699 "http://more/info",
700 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700701 "http://code/base/", // dl url
702 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700703 "HASH1234=", // checksum
704 "false", // needs admin
705 "123", // size
706 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700707 "7", // max days to scatter
708 false, // disable_p2p_for_downloading
709 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700710 -1,
711 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700712 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700713 &response,
714 NULL));
715
716 int64 count;
717 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
718 // count remains the same, as the decrementing happens in update_attempter
719 // which this test doesn't exercise.
720 ASSERT_TRUE(count == 5);
721 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700722
723 // Verify if we are interactive check we don't defer.
724 params.set_interactive(true);
725 ASSERT_TRUE(
726 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700727 NULL, // payload_state
728 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700729 params,
730 GetUpdateResponse2(OmahaRequestParams::kAppId,
731 "1.2.3.4", // version
732 "http://more/info",
733 "true", // prompt
734 "http://code/base/", // dl url
735 "file.signed", // file name
736 "HASH1234=", // checksum
737 "false", // needs admin
738 "123", // size
739 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700740 "7", // max days to scatter
741 false, // disable_p2p_for_downloading
742 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700743 -1,
744 false, // ping_only
745 kErrorCodeSuccess,
746 &response,
747 NULL));
748 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700749}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700750
Darin Petkov6a5b3222010-07-13 14:55:28 -0700751TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700752 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
753
754 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
755
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800756 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700757 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700758 mock_system_state.set_request_params(&params);
759 OmahaRequestAction action(&mock_system_state, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700760 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800761 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700762 NULL),
763 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700764 OmahaRequestActionTestProcessorDelegate delegate;
765 delegate.loop_ = loop;
766 ActionProcessor processor;
767 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700768 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700769
770 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
771 g_main_loop_run(loop);
772 g_main_loop_unref(loop);
773 EXPECT_FALSE(processor.IsRunning());
774}
775
776TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700777 OmahaResponse response;
778 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700779 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700780 NULL, // payload_state
781 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700782 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700783 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700784 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700785 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700786 kErrorCodeOmahaRequestXMLParseError,
Darin Petkovedc522e2010-11-05 09:35:17 -0700787 &response,
788 NULL));
789 EXPECT_FALSE(response.update_exists);
790}
791
792TEST(OmahaRequestActionTest, EmptyResponseTest) {
793 OmahaResponse response;
794 ASSERT_FALSE(
795 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700796 NULL, // payload_state
797 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -0700798 kDefaultTestParams,
799 "",
800 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700801 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700802 kErrorCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700803 &response,
804 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700805 EXPECT_FALSE(response.update_exists);
806}
807
808TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700809 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700810 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700811 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700812 NULL, // payload_state
813 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700814 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700815 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
816 "<daystart elapsed_seconds=\"100\"/>"
817 "<app appid=\"foo\" status=\"ok\">"
818 "<ping status=\"ok\"/>"
819 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700820 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700821 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700822 kErrorCodeOmahaResponseInvalid,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700823 &response,
824 NULL));
825 EXPECT_FALSE(response.update_exists);
826}
827
828TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700829 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700830 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700831 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700832 NULL, // payload_state
833 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700834 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700835 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
836 "<daystart elapsed_seconds=\"100\"/>"
837 "<app appid=\"foo\" status=\"ok\">"
838 "<ping status=\"ok\"/>"
839 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700840 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700841 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700842 kErrorCodeOmahaResponseInvalid,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700843 &response,
844 NULL));
845 EXPECT_FALSE(response.update_exists);
846}
847
848TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700849 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700850 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700851 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700852 NULL, // payload_state
853 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700854 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700855 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
856 "<daystart elapsed_seconds=\"100\"/>"
857 "<app appid=\"foo\" status=\"ok\">"
858 "<ping status=\"ok\"/>"
859 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700860 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700861 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700862 kErrorCodeOmahaResponseInvalid,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700863 &response,
864 NULL));
865 EXPECT_FALSE(response.update_exists);
866}
867
868TEST(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700869 string input_response =
870 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
871 "<daystart elapsed_seconds=\"100\"/>"
872 "<app appid=\"xyz\" status=\"ok\">"
873 "<updatecheck status=\"ok\">"
874 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700875 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700876 "<packages><package hash=\"not-used\" name=\"f\" "
877 "size=\"587\"/></packages>"
878 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700879 "ChromeOSVersion=\"10.2.3.4\" "
880 "Prompt=\"false\" "
881 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800882 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700883 "sha256=\"lkq34j5345\" "
884 "needsadmin=\"true\" "
885 "/></actions></manifest></updatecheck></app></response>";
886 LOG(INFO) << "Input Response = " << input_response;
887
Darin Petkov6a5b3222010-07-13 14:55:28 -0700888 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700889 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700890 NULL, // payload_state
891 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700892 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700893 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700894 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700895 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700896 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700897 &response,
898 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700899 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700900 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800901 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700902 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700903 EXPECT_EQ("lkq34j5345", response.hash);
904 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700905 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700906 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700907}
908
909namespace {
910class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
911 public:
912 void ProcessingStopped(const ActionProcessor* processor) {
913 ASSERT_TRUE(loop_);
914 g_main_loop_quit(loop_);
915 }
916 GMainLoop *loop_;
917};
918
919gboolean TerminateTransferTestStarter(gpointer data) {
920 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
921 processor->StartProcessing();
922 CHECK(processor->IsRunning());
923 processor->StopProcessing();
924 return FALSE;
925}
926} // namespace {}
927
928TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700929 string http_response("doesn't matter");
930 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
931
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800932 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700933 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700934 mock_system_state.set_request_params(&params);
935 OmahaRequestAction action(&mock_system_state, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700936 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800937 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700938 NULL),
939 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700940 TerminateEarlyTestProcessorDelegate delegate;
941 delegate.loop_ = loop;
942 ActionProcessor processor;
943 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700944 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700945
946 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
947 g_main_loop_run(loop);
948 g_main_loop_unref(loop);
949}
950
951TEST(OmahaRequestActionTest, XmlEncodeTest) {
952 EXPECT_EQ("ab", XmlEncode("ab"));
953 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
954 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
955 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
956 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
957
958 vector<char> post_data;
959
960 // Make sure XML Encode is being called on the params
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700961 MockSystemState mock_system_state;
962 OmahaRequestParams params(&mock_system_state,
963 OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700964 OmahaRequestParams::kOsVersion,
965 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700966 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700967 OmahaRequestParams::kAppId,
968 "0.1.0.0",
969 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700970 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700971 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -0700972 "ChromeOSFirmware.1.0",
973 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700974 false, // delta okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800975 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -0700976 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -0700977 false, // update_disabled
978 "", // target_version_prefix
979 false, // use_p2p_for_downloading
980 false); // use_p2p_for_sharing
Darin Petkov6a5b3222010-07-13 14:55:28 -0700981 OmahaResponse response;
982 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700983 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700984 NULL, // payload_state
985 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700986 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700987 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700988 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700989 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700990 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700991 &response,
992 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700993 // convert post_data to string
994 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700995 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
996 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700997 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
998 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
999 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
1000 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001001 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
1002 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001003}
1004
1005TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001006 OmahaResponse response;
1007 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001008 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001009 NULL, // payload_state
1010 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001011 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001012 GetUpdateResponse(OmahaRequestParams::kAppId,
1013 "1.2.3.4", // version
1014 "testthe&lt;url", // more info
1015 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001016 "testthe&amp;codebase/", // dl url
1017 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001018 "HASH1234=", // checksum
1019 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -07001020 "123", // size
1021 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -07001022 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001023 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001024 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001025 &response,
1026 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001027
1028 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001029 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001030 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001031}
1032
1033TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001034 OmahaResponse response;
1035 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001036 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001037 NULL, // payload_state
1038 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001039 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001040 GetUpdateResponse(OmahaRequestParams::kAppId,
1041 "1.2.3.4", // version
1042 "theurl", // more info
1043 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001044 "thecodebase/", // dl url
1045 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001046 "HASH1234=", // checksum
1047 "false", // needs admin
1048 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -07001049 "123123123123123", // size
1050 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -07001051 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001052 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001053 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001054 &response,
1055 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001056
1057 EXPECT_EQ(response.size, 123123123123123ll);
1058}
1059
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001060TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
1061 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -08001062 NiceMock<PrefsMock> prefs;
1063 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1064 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001065 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Darin Petkov95508da2011-01-05 12:42:29 -08001066 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001067 NULL, // payload_state
1068 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001069 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001070 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001071 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001072 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001073 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001074 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001075 &post_data));
1076 // convert post_data to string
1077 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -07001078 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001079 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001080 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001081 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001082 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1083 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001084 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1085 string::npos);
1086 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1087 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001088}
1089
Jay Srinivasan0a708742012-03-20 11:26:12 -07001090
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001091TEST(OmahaRequestActionTest, FormatUpdateDisabledOutputTest) {
Darin Petkov95508da2011-01-05 12:42:29 -08001092 vector<char> post_data;
1093 NiceMock<PrefsMock> prefs;
1094 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Jay Srinivasan0a708742012-03-20 11:26:12 -07001095 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001096 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Jay Srinivasan0a708742012-03-20 11:26:12 -07001097 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001098 params.set_update_disabled(true);
Darin Petkov95508da2011-01-05 12:42:29 -08001099 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001100 NULL, // payload_state
1101 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -07001102 params,
Darin Petkov95508da2011-01-05 12:42:29 -08001103 "invalid xml>",
1104 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001105 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001106 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov95508da2011-01-05 12:42:29 -08001107 NULL, // response
1108 &post_data));
1109 // convert post_data to string
1110 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -07001111 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001112 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001113 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001114 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -08001115 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1116 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001117 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1118 string::npos);
1119 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1120 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -08001121}
1122
Darin Petkove17f86b2010-07-20 09:12:01 -07001123TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
1124 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001125 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -07001126 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1127 "invalid xml>",
1128 &post_data);
1129 // convert post_data to string
1130 string post_str(&post_data[0], post_data.size());
1131 string expected_event = StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001132 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001133 OmahaEvent::kTypeUpdateDownloadStarted,
1134 OmahaEvent::kResultSuccess);
1135 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001136 EXPECT_EQ(post_str.find("ping"), string::npos);
1137 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001138}
1139
1140TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
1141 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001142 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -07001143 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1144 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001145 kErrorCodeError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001146 "invalid xml>",
1147 &post_data);
1148 // convert post_data to string
1149 string post_str(&post_data[0], post_data.size());
1150 string expected_event = StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001151 " <event eventtype=\"%d\" eventresult=\"%d\" "
1152 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001153 OmahaEvent::kTypeDownloadComplete,
1154 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001155 kErrorCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001156 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001157 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001158}
1159
1160TEST(OmahaRequestActionTest, IsEventTest) {
1161 string http_response("doesn't matter");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001162 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001163 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001164 mock_system_state.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001165 OmahaRequestAction update_check_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001166 &mock_system_state,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001167 NULL,
1168 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001169 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001170 NULL),
1171 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001172 EXPECT_FALSE(update_check_action.IsEvent());
1173
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001174 params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001175 mock_system_state.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001176 OmahaRequestAction event_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001177 &mock_system_state,
Darin Petkove17f86b2010-07-20 09:12:01 -07001178 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001179 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001180 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001181 NULL),
1182 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001183 EXPECT_TRUE(event_action.IsEvent());
1184}
1185
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001186TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
1187 for (int i = 0; i < 2; i++) {
1188 bool delta_okay = i == 1;
1189 const char* delta_okay_str = delta_okay ? "true" : "false";
1190 vector<char> post_data;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001191 MockSystemState mock_system_state;
1192 OmahaRequestParams params(&mock_system_state,
1193 OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001194 OmahaRequestParams::kOsVersion,
1195 "service_pack",
1196 "x86-generic",
1197 OmahaRequestParams::kAppId,
1198 "0.1.0.0",
1199 "en-US",
1200 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001201 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001202 "ChromeOSFirmware.1.0",
1203 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001204 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001205 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001206 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001207 false, // update_disabled
1208 "", // target_version_prefix
1209 false, // use_p2p_for_downloading
1210 false); // use_p2p_for_sharing
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001211 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001212 NULL, // payload_state
1213 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001214 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001215 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001216 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001217 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001218 kErrorCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001219 NULL,
1220 &post_data));
1221 // convert post_data to string
1222 string post_str(&post_data[0], post_data.size());
1223 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1224 string::npos)
1225 << "i = " << i;
1226 }
1227}
1228
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001229TEST(OmahaRequestActionTest, FormatInteractiveOutputTest) {
1230 for (int i = 0; i < 2; i++) {
1231 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001232 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001233 vector<char> post_data;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001234 MockSystemState mock_system_state;
1235 OmahaRequestParams params(&mock_system_state,
1236 OmahaRequestParams::kOsPlatform,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001237 OmahaRequestParams::kOsVersion,
1238 "service_pack",
1239 "x86-generic",
1240 OmahaRequestParams::kAppId,
1241 "0.1.0.0",
1242 "en-US",
1243 "unittest_track",
1244 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001245 "ChromeOSFirmware.1.0",
1246 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001247 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001248 interactive,
1249 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001250 false, // update_disabled
1251 "", // target_version_prefix
1252 false, // use_p2p_for_downloading
1253 false); // use_p2p_for_sharing
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001254 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001255 NULL, // payload_state
1256 NULL, // p2p_manager
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001257 params,
1258 "invalid xml>",
1259 -1,
1260 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001261 kErrorCodeOmahaRequestXMLParseError,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001262 NULL,
1263 &post_data));
1264 // convert post_data to string
1265 string post_str(&post_data[0], post_data.size());
Gilad Arnold8a659d82013-01-24 11:26:00 -08001266 EXPECT_NE(post_str.find(StringPrintf("installsource=\"%s\"",
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001267 interactive_str)),
1268 string::npos)
1269 << "i = " << i;
1270 }
1271}
1272
Darin Petkove17f86b2010-07-20 09:12:01 -07001273TEST(OmahaRequestActionTest, OmahaEventTest) {
1274 OmahaEvent default_event;
1275 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1276 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001277 EXPECT_EQ(kErrorCodeError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001278
1279 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1280 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1281 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001282 EXPECT_EQ(kErrorCodeSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001283
1284 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1285 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001286 kErrorCodeError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001287 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1288 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001289 EXPECT_EQ(kErrorCodeError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001290}
1291
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001292TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -07001293 for (int ping_only = 0; ping_only < 2; ping_only++) {
1294 NiceMock<PrefsMock> prefs;
1295 // Add a few hours to the day difference to test no rounding, etc.
1296 int64_t five_days_ago =
1297 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1298 int64_t six_days_ago =
1299 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1300 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1301 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1302 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1303 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
1304 vector<char> post_data;
1305 ASSERT_TRUE(
1306 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001307 NULL, // payload_state
1308 NULL, // p2p_manager
Darin Petkov265f2902011-05-09 15:17:40 -07001309 kDefaultTestParams,
1310 GetNoUpdateResponse(OmahaRequestParams::kAppId),
1311 -1,
1312 ping_only,
David Zeuthena99981f2013-04-29 13:42:47 -07001313 kErrorCodeSuccess,
Darin Petkov265f2902011-05-09 15:17:40 -07001314 NULL,
1315 &post_data));
1316 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001317 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
Darin Petkov265f2902011-05-09 15:17:40 -07001318 string::npos);
1319 if (ping_only) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001320 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001321 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1322 } else {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001323 EXPECT_NE(post_str.find("updatecheck"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001324 EXPECT_NE(post_str.find("previousversion"), string::npos);
1325 }
1326 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001327}
1328
1329TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001330 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001331 int64_t three_days_ago =
1332 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1333 int64_t now = Time::Now().ToInternalValue();
1334 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1335 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1336 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1337 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1338 vector<char> post_data;
1339 ASSERT_TRUE(
1340 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001341 NULL, // payload_state
1342 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001343 kDefaultTestParams,
1344 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001345 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001346 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001347 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001348 NULL,
1349 &post_data));
1350 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001351 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001352 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001353}
1354
1355TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001356 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001357 int64_t four_days_ago =
1358 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1359 int64_t now = Time::Now().ToInternalValue();
1360 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1361 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1362 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1363 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
1364 vector<char> post_data;
1365 ASSERT_TRUE(
1366 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001367 NULL, // payload_state
1368 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001369 kDefaultTestParams,
1370 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001371 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001372 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001373 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001374 NULL,
1375 &post_data));
1376 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001377 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001378 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001379}
1380
1381TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001382 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001383 int64_t one_hour_ago =
1384 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
1385 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1386 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1387 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1388 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1389 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1390 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1391 vector<char> post_data;
1392 ASSERT_TRUE(
1393 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001394 NULL, // payload_state
1395 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001396 kDefaultTestParams,
1397 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001398 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001399 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001400 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001401 NULL,
1402 &post_data));
1403 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001404 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001405}
1406
Thieu Leb44e9e82011-06-06 14:34:04 -07001407TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
1408 // This test ensures that we ignore empty ping only requests.
1409 NiceMock<PrefsMock> prefs;
1410 int64_t now = Time::Now().ToInternalValue();
1411 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1412 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1413 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1414 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1415 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1416 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1417 vector<char> post_data;
1418 EXPECT_TRUE(
1419 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001420 NULL, // payload_state
1421 NULL, // p2p_manager
Thieu Leb44e9e82011-06-06 14:34:04 -07001422 kDefaultTestParams,
1423 GetNoUpdateResponse(OmahaRequestParams::kAppId),
1424 -1,
1425 true, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001426 kErrorCodeSuccess,
Thieu Leb44e9e82011-06-06 14:34:04 -07001427 NULL,
1428 &post_data));
1429 EXPECT_EQ(post_data.size(), 0);
1430}
1431
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001432TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001433 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001434 int64_t future =
1435 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
1436 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1437 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1438 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1439 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1440 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1441 .WillOnce(Return(true));
1442 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1443 .WillOnce(Return(true));
1444 vector<char> post_data;
1445 ASSERT_TRUE(
1446 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001447 NULL, // payload_state
1448 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001449 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001450 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1451 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001452 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001453 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001454 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001455 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001456 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001457 NULL,
1458 &post_data));
1459 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001460 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001461}
1462
1463TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
1464 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001465 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001466 // may fail if it runs for longer than 5 seconds. It shouldn't run
1467 // that long though.
1468 int64_t midnight =
1469 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1470 int64_t midnight_slack =
1471 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -08001472 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001473 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1474 AllOf(Ge(midnight), Le(midnight_slack))))
1475 .WillOnce(Return(true));
1476 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1477 AllOf(Ge(midnight), Le(midnight_slack))))
1478 .WillOnce(Return(true));
1479 ASSERT_TRUE(
1480 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001481 NULL, // payload_state
1482 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001483 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001484 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1485 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001486 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001487 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001488 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001489 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001490 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001491 NULL,
1492 NULL));
1493}
1494
1495TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001496 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001497 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1498 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1499 ASSERT_TRUE(
1500 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001501 NULL, // payload_state
1502 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001503 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001504 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1505 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001506 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001507 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001508 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001509 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001510 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001511 NULL,
1512 NULL));
1513}
1514
1515TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001516 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001517 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1518 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1519 ASSERT_TRUE(
1520 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001521 NULL, // payload_state
1522 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001523 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001524 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1525 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001526 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001527 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001528 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001529 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001530 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001531 NULL,
1532 NULL));
1533}
1534
Darin Petkov84c763c2010-07-29 16:27:58 -07001535TEST(OmahaRequestActionTest, NoUniqueIDTest) {
1536 vector<char> post_data;
1537 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001538 NULL, // payload_state
1539 NULL, // p2p_manager
Darin Petkov84c763c2010-07-29 16:27:58 -07001540 kDefaultTestParams,
1541 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001542 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001543 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001544 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -07001545 NULL, // response
1546 &post_data));
1547 // convert post_data to string
1548 string post_str(&post_data[0], post_data.size());
1549 EXPECT_EQ(post_str.find("machineid="), string::npos);
1550 EXPECT_EQ(post_str.find("userid="), string::npos);
1551}
1552
Darin Petkovedc522e2010-11-05 09:35:17 -07001553TEST(OmahaRequestActionTest, NetworkFailureTest) {
1554 OmahaResponse response;
1555 ASSERT_FALSE(
1556 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001557 NULL, // payload_state
1558 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -07001559 kDefaultTestParams,
1560 "",
1561 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001562 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001563 static_cast<ErrorCode>(
1564 kErrorCodeOmahaRequestHTTPResponseBase + 501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001565 &response,
1566 NULL));
1567 EXPECT_FALSE(response.update_exists);
1568}
1569
1570TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
1571 OmahaResponse response;
1572 ASSERT_FALSE(
1573 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001574 NULL, // payload_state
1575 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -07001576 kDefaultTestParams,
1577 "",
1578 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001579 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001580 static_cast<ErrorCode>(
1581 kErrorCodeOmahaRequestHTTPResponseBase + 999),
Darin Petkovedc522e2010-11-05 09:35:17 -07001582 &response,
1583 NULL));
1584 EXPECT_FALSE(response.update_exists);
1585}
1586
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001587TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
1588 OmahaResponse response;
1589 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001590 params.set_wall_clock_based_wait_enabled(true);
1591 params.set_waiting_period(TimeDelta().FromDays(1));
1592 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001593
1594 string prefs_dir;
1595 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
1596 &prefs_dir));
1597 ScopedDirRemover temp_dir_remover(prefs_dir);
1598
1599 Prefs prefs;
1600 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
1601 << "Failed to initialize preferences.";
1602
1603 ASSERT_FALSE(TestUpdateCheck(
1604 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001605 NULL, // payload_state
1606 NULL, // p2p_manager
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001607 params,
1608 GetUpdateResponse2(OmahaRequestParams::kAppId,
1609 "1.2.3.4", // version
1610 "http://more/info",
1611 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001612 "http://code/base/", // dl url
1613 "file.signed", // file name
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001614 "HASH1234=", // checksum
1615 "false", // needs admin
1616 "123", // size
1617 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001618 "7", // max days to scatter
1619 false, // disable_p2p_for_downloading
1620 false), // disable_p2p_for sharing
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001621 -1,
1622 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001623 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001624 &response,
1625 NULL));
1626
1627 int64 timestamp = 0;
1628 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
1629 ASSERT_TRUE(timestamp > 0);
1630 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001631
1632 // Verify if we are interactive check we don't defer.
1633 params.set_interactive(true);
1634 ASSERT_TRUE(
1635 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001636 NULL, // payload_state
1637 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -07001638 params,
1639 GetUpdateResponse2(OmahaRequestParams::kAppId,
1640 "1.2.3.4", // version
1641 "http://more/info",
1642 "true", // prompt
1643 "http://code/base/", // dl url
1644 "file.signed", // file name
1645 "HASH1234=", // checksum
1646 "false", // needs admin
1647 "123", // size
1648 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001649 "7", // max days to scatter
1650 false, // disable_p2p_for_downloading
1651 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -07001652 -1,
1653 false, // ping_only
1654 kErrorCodeSuccess,
1655 &response,
1656 NULL));
1657 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001658}
1659
1660TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
1661 OmahaResponse response;
1662 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001663 params.set_wall_clock_based_wait_enabled(true);
1664 params.set_waiting_period(TimeDelta().FromDays(1));
1665 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001666
1667 string prefs_dir;
1668 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
1669 &prefs_dir));
1670 ScopedDirRemover temp_dir_remover(prefs_dir);
1671
1672 Prefs prefs;
1673 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
1674 << "Failed to initialize preferences.";
1675
1676 // Set the timestamp to a very old value such that it exceeds the
1677 // waiting period set above.
1678 Time t1;
1679 Time::FromString("1/1/2012", &t1);
1680 ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
1681 ASSERT_TRUE(TestUpdateCheck(
1682 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001683 NULL, // payload_state
1684 NULL, // p2p_manager
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001685 params,
1686 GetUpdateResponse2(OmahaRequestParams::kAppId,
1687 "1.2.3.4", // version
1688 "http://more/info",
1689 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001690 "http://code/base/", // dl url
1691 "file.signed", // file name
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001692 "HASH1234=", // checksum
1693 "false", // needs admin
1694 "123", // size
1695 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001696 "7", // max days to scatter
1697 false, // disable_p2p_for_downloading
1698 false), // disable_p2p_for sharing
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001699 -1,
1700 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001701 kErrorCodeSuccess,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001702 &response,
1703 NULL));
1704
1705 EXPECT_TRUE(response.update_exists);
1706
1707 // Make sure the timestamp t1 is unchanged showing that it was reused.
1708 int64 timestamp = 0;
1709 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
1710 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1711}
1712
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001713TEST(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001714 // Create a uniquely named test directory.
1715 string test_dir;
1716 ASSERT_TRUE(utils::MakeTempDirectory(
1717 "omaha_request_action-test-XXXXXX", &test_dir));
1718
1719 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1720 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001721 kStatefulPartition + "/etc"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001722 vector<char> post_data;
1723 NiceMock<PrefsMock> prefs;
1724 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001725 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001726 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1727 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1728 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1729 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001730 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001731 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1732 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1733 OmahaRequestParams params = kDefaultTestParams;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001734 params.set_root(string("./") + test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001735 params.SetLockDown(false);
1736 params.Init("1.2.3.4", "", 0);
1737 EXPECT_EQ("canary-channel", params.current_channel());
1738 EXPECT_EQ("stable-channel", params.target_channel());
1739 EXPECT_TRUE(params.to_more_stable_channel());
1740 EXPECT_TRUE(params.is_powerwash_allowed());
1741 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001742 NULL, // payload_state
1743 NULL, // p2p_manager
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001744 params,
1745 "invalid xml>",
1746 -1,
1747 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001748 kErrorCodeOmahaRequestXMLParseError,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001749 NULL, // response
1750 &post_data));
1751 // convert post_data to string
1752 string post_str(&post_data[0], post_data.size());
1753 EXPECT_NE(string::npos, post_str.find(
1754 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1755 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1756 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001757
1758 ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001759}
1760
1761TEST(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001762 // Create a uniquely named test directory.
1763 string test_dir;
1764 ASSERT_TRUE(utils::MakeTempDirectory(
1765 "omaha_request_action-test-XXXXXX", &test_dir));
1766
1767 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1768 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001769 kStatefulPartition + "/etc"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001770 vector<char> post_data;
1771 NiceMock<PrefsMock> prefs;
1772 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001773 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001774 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1775 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1776 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1777 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001778 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001779 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1780 OmahaRequestParams params = kDefaultTestParams;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001781 params.set_root(string("./") + test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001782 params.SetLockDown(false);
1783 params.Init("5.6.7.8", "", 0);
1784 EXPECT_EQ("stable-channel", params.current_channel());
1785 EXPECT_EQ("canary-channel", params.target_channel());
1786 EXPECT_FALSE(params.to_more_stable_channel());
1787 EXPECT_FALSE(params.is_powerwash_allowed());
1788 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001789 NULL, // payload_state
1790 NULL, // p2p_manager
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001791 params,
1792 "invalid xml>",
1793 -1,
1794 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001795 kErrorCodeOmahaRequestXMLParseError,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001796 NULL, // response
1797 &post_data));
1798 // convert post_data to string
1799 string post_str(&post_data[0], post_data.size());
1800 EXPECT_NE(string::npos, post_str.find(
1801 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1802 "version=\"5.6.7.8\" "
1803 "track=\"canary-channel\" from_track=\"stable-channel\""));
1804 EXPECT_EQ(string::npos, post_str.find( "from_version"));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001805
1806 ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001807}
1808
David Zeuthen8f191b22013-08-06 12:27:50 -07001809void P2PTest(bool initial_allow_p2p_for_downloading,
1810 bool initial_allow_p2p_for_sharing,
1811 bool omaha_disable_p2p_for_downloading,
1812 bool omaha_disable_p2p_for_sharing,
1813 bool payload_state_allow_p2p_attempt,
1814 bool expect_p2p_client_lookup,
1815 const string& p2p_client_result_url,
1816 bool expected_allow_p2p_for_downloading,
1817 bool expected_allow_p2p_for_sharing,
1818 const string& expected_p2p_url) {
1819 OmahaResponse response;
1820 OmahaRequestParams request_params = kDefaultTestParams;
1821 request_params.set_use_p2p_for_downloading(initial_allow_p2p_for_downloading);
1822 request_params.set_use_p2p_for_sharing(initial_allow_p2p_for_sharing);
1823
1824 MockPayloadState mock_payload_state;
1825 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1826 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
1827 MockP2PManager mock_p2p_manager;
1828 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1829
1830 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, _, _))
1831 .Times(expect_p2p_client_lookup ? 1 : 0);
1832
1833 ASSERT_TRUE(
1834 TestUpdateCheck(NULL, // prefs
1835 &mock_payload_state,
1836 &mock_p2p_manager,
1837 request_params,
1838 GetUpdateResponse2(OmahaRequestParams::kAppId,
1839 "1.2.3.4", // version
1840 "http://more/info",
1841 "true", // prompt
1842 "http://code/base/", // dl url
1843 "file.signed", // file name
1844 "HASH1234=", // checksum
1845 "false", // needs admin
1846 "123", // size
1847 "", // deadline
1848 "7", // max days to scatter
1849 omaha_disable_p2p_for_downloading,
1850 omaha_disable_p2p_for_sharing),
1851 -1,
1852 false, // ping_only
1853 kErrorCodeSuccess,
1854 &response,
1855 NULL));
1856 EXPECT_TRUE(response.update_exists);
1857
1858 EXPECT_EQ(response.disable_p2p_for_downloading,
1859 omaha_disable_p2p_for_downloading);
1860 EXPECT_EQ(response.disable_p2p_for_sharing,
1861 omaha_disable_p2p_for_sharing);
1862
1863 EXPECT_EQ(request_params.use_p2p_for_downloading(),
1864 expected_allow_p2p_for_downloading);
1865
1866 EXPECT_EQ(request_params.use_p2p_for_sharing(),
1867 expected_allow_p2p_for_sharing);
1868
1869 EXPECT_EQ(request_params.p2p_url(), expected_p2p_url);
1870}
1871
1872TEST(OmahaRequestActionTest, P2PWithPeer) {
1873 P2PTest(true, // initial_allow_p2p_for_downloading
1874 true, // initial_allow_p2p_for_sharing
1875 false, // omaha_disable_p2p_for_downloading
1876 false, // omaha_disable_p2p_for_sharing
1877 true, // payload_state_allow_p2p_attempt
1878 true, // expect_p2p_client_lookup
1879 "http://1.3.5.7/p2p", // p2p_client_result_url
1880 true, // expected_allow_p2p_for_downloading
1881 true, // expected_allow_p2p_for_sharing
1882 "http://1.3.5.7/p2p"); // expected_p2p_url
1883}
1884
1885TEST(OmahaRequestActionTest, P2PWithoutPeer) {
1886 P2PTest(true, // initial_allow_p2p_for_downloading
1887 true, // initial_allow_p2p_for_sharing
1888 false, // omaha_disable_p2p_for_downloading
1889 false, // omaha_disable_p2p_for_sharing
1890 true, // payload_state_allow_p2p_attempt
1891 true, // expect_p2p_client_lookup
1892 "", // p2p_client_result_url
1893 false, // expected_allow_p2p_for_downloading
1894 true, // expected_allow_p2p_for_sharing
1895 ""); // expected_p2p_url
1896}
1897
1898TEST(OmahaRequestActionTest, P2PDownloadNotAllowed) {
1899 P2PTest(false, // initial_allow_p2p_for_downloading
1900 true, // initial_allow_p2p_for_sharing
1901 false, // omaha_disable_p2p_for_downloading
1902 false, // omaha_disable_p2p_for_sharing
1903 true, // payload_state_allow_p2p_attempt
1904 false, // expect_p2p_client_lookup
1905 "unset", // p2p_client_result_url
1906 false, // expected_allow_p2p_for_downloading
1907 true, // expected_allow_p2p_for_sharing
1908 ""); // expected_p2p_url
1909}
1910
1911TEST(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
1912 P2PTest(true, // initial_allow_p2p_for_downloading
1913 true, // initial_allow_p2p_for_sharing
1914 true, // omaha_disable_p2p_for_downloading
1915 false, // omaha_disable_p2p_for_sharing
1916 true, // payload_state_allow_p2p_attempt
1917 false, // expect_p2p_client_lookup
1918 "unset", // p2p_client_result_url
1919 false, // expected_allow_p2p_for_downloading
1920 true, // expected_allow_p2p_for_sharing
1921 ""); // expected_p2p_url
1922}
1923
1924TEST(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
1925 P2PTest(true, // initial_allow_p2p_for_downloading
1926 true, // initial_allow_p2p_for_sharing
1927 false, // omaha_disable_p2p_for_downloading
1928 true, // omaha_disable_p2p_for_sharing
1929 true, // payload_state_allow_p2p_attempt
1930 true, // expect_p2p_client_lookup
1931 "http://1.3.5.7/p2p", // p2p_client_result_url
1932 true, // expected_allow_p2p_for_downloading
1933 false, // expected_allow_p2p_for_sharing
1934 "http://1.3.5.7/p2p"); // expected_p2p_url
1935}
1936
1937TEST(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
1938 P2PTest(true, // initial_allow_p2p_for_downloading
1939 true, // initial_allow_p2p_for_sharing
1940 true, // omaha_disable_p2p_for_downloading
1941 true, // omaha_disable_p2p_for_sharing
1942 true, // payload_state_allow_p2p_attempt
1943 false, // expect_p2p_client_lookup
1944 "unset", // p2p_client_result_url
1945 false, // expected_allow_p2p_for_downloading
1946 false, // expected_allow_p2p_for_sharing
1947 ""); // expected_p2p_url
1948}
1949
Darin Petkov6a5b3222010-07-13 14:55:28 -07001950} // namespace chromeos_update_engine