blob: b1e6c5832405e6a83fd2fe91cdd851d2dcb79fce [file] [log] [blame]
[email protected]80744782012-05-04 01:47:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include <string>
6#include <vector>
7
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/scoped_ptr.h"
[email protected]2602087e2009-08-24 23:12:169#include "base/message_loop.h"
[email protected]4003d7142009-01-12 12:56:2010#include "base/process.h"
[email protected]a68114f72009-11-30 23:32:4911#include "base/process_util.h"
[email protected]940895b2011-08-20 00:50:0512#include "content/common/request_extra_data.h"
[email protected]620161e2011-03-07 18:05:2613#include "content/common/resource_dispatcher.h"
[email protected]94dc971d2011-03-05 19:08:3214#include "content/common/resource_messages.h"
[email protected]2336ffe2011-11-24 01:23:3415#include "content/public/common/resource_response.h"
[email protected]939856a2010-08-24 20:29:0216#include "net/base/upload_data.h"
[email protected]7a4de7a62010-08-17 18:38:2417#include "net/http/http_response_headers.h"
initial.commit09911bf2008-07-26 23:55:2918#include "testing/gtest/include/gtest/gtest.h"
[email protected]f430b5712009-08-21 21:46:3119#include "webkit/appcache/appcache_interfaces.h"
initial.commit09911bf2008-07-26 23:55:2920
21using webkit_glue::ResourceLoaderBridge;
[email protected]b4b3a912010-10-08 19:05:3722using webkit_glue::ResourceResponseInfo;
initial.commit09911bf2008-07-26 23:55:2923
[email protected]be7b41e82012-07-04 09:46:5124namespace content {
25
initial.commit09911bf2008-07-26 23:55:2926static const char test_page_url[] = "http://www.google.com/";
27static const char test_page_headers[] =
28 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
29static const char test_page_mime_type[] = "text/html";
30static const char test_page_charset[] = "";
31static const char test_page_contents[] =
32 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
[email protected]b5ab3982010-02-16 23:58:2733static const uint32 test_page_contents_len = arraysize(test_page_contents) - 1;
initial.commit09911bf2008-07-26 23:55:2934
[email protected]b8901082010-11-12 01:14:2835static const char kShmemSegmentName[] = "DeferredResourceLoaderTest";
36
initial.commit09911bf2008-07-26 23:55:2937// Listens for request response data and stores it so that it can be compared
38// to the reference data.
39class TestRequestCallback : public ResourceLoaderBridge::Peer {
40 public:
41 TestRequestCallback() : complete_(false) {
42 }
43
[email protected]04f6f982012-08-03 01:02:1544 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE {
[email protected]bb551622010-07-22 20:52:4945 }
46
[email protected]6568a9e32009-07-30 18:01:3947 virtual bool OnReceivedRedirect(
48 const GURL& new_url,
[email protected]b4b3a912010-10-08 19:05:3749 const ResourceResponseInfo& info,
[email protected]041b0bbb2009-11-18 02:27:3450 bool* has_new_first_party_for_cookies,
[email protected]04f6f982012-08-03 01:02:1551 GURL* new_first_party_for_cookies) OVERRIDE {
[email protected]041b0bbb2009-11-18 02:27:3452 *has_new_first_party_for_cookies = false;
[email protected]6568a9e32009-07-30 18:01:3953 return true;
initial.commit09911bf2008-07-26 23:55:2954 }
55
[email protected]04f6f982012-08-03 01:02:1556 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE {
initial.commit09911bf2008-07-26 23:55:2957 }
58
[email protected]04f6f982012-08-03 01:02:1559 virtual void OnDownloadedData(int len) OVERRIDE {
[email protected]bb551622010-07-22 20:52:4960 }
61
[email protected]8bd0de72011-04-08 18:52:1062 virtual void OnReceivedData(const char* data,
63 int data_length,
[email protected]04f6f982012-08-03 01:02:1564 int encoded_data_length) OVERRIDE {
initial.commit09911bf2008-07-26 23:55:2965 EXPECT_FALSE(complete_);
[email protected]8bd0de72011-04-08 18:52:1066 data_.append(data, data_length);
[email protected]dfd682172011-04-13 19:57:2567 total_encoded_data_length_ += encoded_data_length;
initial.commit09911bf2008-07-26 23:55:2968 }
69
[email protected]04f6f982012-08-03 01:02:1570 virtual void OnCompletedRequest(
71 const net::URLRequestStatus& status,
72 const std::string& security_info,
73 const base::TimeTicks& completion_time) OVERRIDE {
initial.commit09911bf2008-07-26 23:55:2974 EXPECT_FALSE(complete_);
75 complete_ = true;
76 }
77
[email protected]8bd0de72011-04-08 18:52:1078 bool complete() const {
79 return complete_;
80 }
initial.commit09911bf2008-07-26 23:55:2981 const std::string& data() const {
82 return data_;
83 }
[email protected]dfd682172011-04-13 19:57:2584 int total_encoded_data_length() const {
85 return total_encoded_data_length_;
initial.commit09911bf2008-07-26 23:55:2986 }
87
88 private:
89 bool complete_;
90 std::string data_;
[email protected]dfd682172011-04-13 19:57:2591 int total_encoded_data_length_;
initial.commit09911bf2008-07-26 23:55:2992};
93
94
95// Sets up the message sender override for the unit test
[email protected]d84effeb2012-06-25 17:03:1096class ResourceDispatcherTest : public testing::Test, public IPC::Sender {
initial.commit09911bf2008-07-26 23:55:2997 public:
[email protected]d84effeb2012-06-25 17:03:1098 // Emulates IPC send operations (IPC::Sender) by adding
initial.commit09911bf2008-07-26 23:55:2999 // pending messages to the queue.
100 virtual bool Send(IPC::Message* msg) {
101 message_queue_.push_back(IPC::Message(*msg));
102 delete msg;
103 return true;
104 }
105
106 // Emulates the browser process and processes the pending IPC messages,
107 // returning the hardcoded file contents.
108 void ProcessMessages() {
109 while (!message_queue_.empty()) {
initial.commit09911bf2008-07-26 23:55:29110 int request_id;
[email protected]94dc971d2011-03-05 19:08:32111 ResourceHostMsg_Request request;
112 ASSERT_TRUE(ResourceHostMsg_RequestResource::Read(
[email protected]eb9989092009-03-12 21:42:52113 &message_queue_[0], &request_id, &request));
initial.commit09911bf2008-07-26 23:55:29114
115 // check values
116 EXPECT_EQ(test_page_url, request.url.spec());
117
118 // received response message
[email protected]be7b41e82012-07-04 09:46:51119 ResourceResponseHead response;
initial.commit09911bf2008-07-26 23:55:29120 std::string raw_headers(test_page_headers);
121 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
122 response.headers = new net::HttpResponseHeaders(raw_headers);
123 response.mime_type = test_page_mime_type;
124 response.charset = test_page_charset;
initial.commit09911bf2008-07-26 23:55:29125 dispatcher_->OnReceivedResponse(request_id, response);
126
127 // received data message with the test contents
[email protected]176aa482008-11-14 03:25:15128 base::SharedMemory shared_mem;
[email protected]54e3dfa22010-10-27 18:16:06129 EXPECT_TRUE(shared_mem.CreateAndMapAnonymous(test_page_contents_len));
initial.commit09911bf2008-07-26 23:55:29130 char* put_data_here = static_cast<char*>(shared_mem.memory());
131 memcpy(put_data_here, test_page_contents, test_page_contents_len);
[email protected]176aa482008-11-14 03:25:15132 base::SharedMemoryHandle dup_handle;
[email protected]4003d7142009-01-12 12:56:20133 EXPECT_TRUE(shared_mem.GiveToProcess(
134 base::Process::Current().handle(), &dup_handle));
[email protected]eb9989092009-03-12 21:42:52135 dispatcher_->OnReceivedData(
[email protected]8bd0de72011-04-08 18:52:10136 message_queue_[0],
137 request_id,
138 dup_handle,
139 test_page_contents_len,
140 test_page_contents_len);
initial.commit09911bf2008-07-26 23:55:29141
142 message_queue_.erase(message_queue_.begin());
143
144 // read the ack message.
[email protected]c2fe31542009-05-20 18:24:14145 Tuple1<int> request_ack;
[email protected]94dc971d2011-03-05 19:08:32146 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
[email protected]eb9989092009-03-12 21:42:52147 &message_queue_[0], &request_ack));
initial.commit09911bf2008-07-26 23:55:29148
[email protected]c2fe31542009-05-20 18:24:14149 ASSERT_EQ(request_ack.a, request_id);
initial.commit09911bf2008-07-26 23:55:29150
151 message_queue_.erase(message_queue_.begin());
152 }
153 }
154
155 protected:
initial.commit09911bf2008-07-26 23:55:29156 // testing::Test
[email protected]04f6f982012-08-03 01:02:15157 virtual void SetUp() OVERRIDE {
[email protected]eb9989092009-03-12 21:42:52158 dispatcher_.reset(new ResourceDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29159 }
[email protected]04f6f982012-08-03 01:02:15160 virtual void TearDown() OVERRIDE {
[email protected]eb9989092009-03-12 21:42:52161 dispatcher_.reset();
initial.commit09911bf2008-07-26 23:55:29162 }
163
[email protected]2602087e2009-08-24 23:12:16164 ResourceLoaderBridge* CreateBridge() {
[email protected]46b0d4a2009-12-19 00:46:33165 webkit_glue::ResourceLoaderBridge::RequestInfo request_info;
166 request_info.method = "GET";
167 request_info.url = GURL(test_page_url);
168 request_info.first_party_for_cookies = GURL(test_page_url);
169 request_info.referrer = GURL();
[email protected]46b0d4a2009-12-19 00:46:33170 request_info.headers = std::string();
171 request_info.load_flags = 0;
172 request_info.requestor_pid = 0;
173 request_info.request_type = ResourceType::SUB_RESOURCE;
[email protected]52bbcd932010-01-06 18:56:12174 request_info.appcache_host_id = appcache::kNoHostId;
175 request_info.routing_id = 0;
[email protected]537fbe02011-11-24 00:58:06176 RequestExtraData extra_data(WebKit::WebReferrerPolicyDefault,
[email protected]e372f3d2012-07-17 19:16:44177 WebKit::WebString(),
[email protected]80744782012-05-04 01:47:00178 true, 0, false, -1, true,
[email protected]be7b41e82012-07-04 09:46:51179 PAGE_TRANSITION_LINK, -1, -1);
[email protected]940895b2011-08-20 00:50:05180 request_info.extra_data = &extra_data;
[email protected]46b0d4a2009-12-19 00:46:33181
[email protected]50285ff2011-03-11 23:10:56182 return dispatcher_->CreateBridge(request_info);
[email protected]2602087e2009-08-24 23:12:16183 }
184
initial.commit09911bf2008-07-26 23:55:29185 std::vector<IPC::Message> message_queue_;
[email protected]eb9989092009-03-12 21:42:52186 static scoped_ptr<ResourceDispatcher> dispatcher_;
initial.commit09911bf2008-07-26 23:55:29187};
188
189/*static*/
[email protected]eb9989092009-03-12 21:42:52190scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_;
initial.commit09911bf2008-07-26 23:55:29191
192// Does a simple request and tests that the correct data is received.
193TEST_F(ResourceDispatcherTest, RoundTrip) {
194 TestRequestCallback callback;
[email protected]2602087e2009-08-24 23:12:16195 ResourceLoaderBridge* bridge = CreateBridge();
initial.commit09911bf2008-07-26 23:55:29196
197 bridge->Start(&callback);
198
199 ProcessMessages();
200
201 // FIXME(brettw) when the request complete messages are actually handledo
202 // and dispatched, uncomment this.
203 //EXPECT_TRUE(callback.complete());
204 //EXPECT_STREQ(test_page_contents, callback.data().c_str());
[email protected]dfd682172011-04-13 19:57:25205 //EXPECT_EQ(test_page_contents_len, callback.total_encoded_data_length());
initial.commit09911bf2008-07-26 23:55:29206
207 delete bridge;
208}
209
210// Tests that the request IDs are straight when there are multiple requests.
211TEST_F(ResourceDispatcherTest, MultipleRequests) {
212 // FIXME
213}
214
215// Tests that the cancel method prevents other messages from being received
216TEST_F(ResourceDispatcherTest, Cancel) {
217 // FIXME
218}
219
220TEST_F(ResourceDispatcherTest, Cookies) {
221 // FIXME
222}
223
224TEST_F(ResourceDispatcherTest, SerializedPostData) {
225 // FIXME
226}
[email protected]2602087e2009-08-24 23:12:16227
228// This class provides functionality to validate whether the ResourceDispatcher
229// object honors the deferred loading contract correctly, i.e. if deferred
230// loading is enabled it should queue up any responses received. If deferred
231// loading is enabled/disabled in the context of a dispatched message, other
232// queued messages should not be dispatched until deferred load is turned off.
233class DeferredResourceLoadingTest : public ResourceDispatcherTest,
234 public ResourceLoaderBridge::Peer {
235 public:
236 DeferredResourceLoadingTest()
237 : defer_loading_(false) {
238 }
239
[email protected]04f6f982012-08-03 01:02:15240 virtual bool Send(IPC::Message* msg) OVERRIDE {
[email protected]2602087e2009-08-24 23:12:16241 delete msg;
242 return true;
243 }
244
245 void InitMessages() {
246 set_defer_loading(true);
247
[email protected]be7b41e82012-07-04 09:46:51248 ResourceResponseHead response_head;
[email protected]f90bf0d92011-01-13 02:12:44249 response_head.status.set_status(net::URLRequestStatus::SUCCESS);
[email protected]2602087e2009-08-24 23:12:16250
[email protected]04f6f982012-08-03 01:02:15251 dispatcher_->OnMessageReceived(
252 ResourceMsg_ReceivedResponse(0, 0, response_head));
[email protected]2602087e2009-08-24 23:12:16253
[email protected]a68114f72009-11-30 23:32:49254 // Duplicate the shared memory handle so both the test and the callee can
255 // close their copy.
256 base::SharedMemoryHandle duplicated_handle;
257 EXPECT_TRUE(shared_handle_.ShareToProcess(base::GetCurrentProcessHandle(),
258 &duplicated_handle));
259
[email protected]04f6f982012-08-03 01:02:15260 dispatcher_->OnMessageReceived(
261 ResourceMsg_DataReceived(0, 0, duplicated_handle, 100, 100));
[email protected]2602087e2009-08-24 23:12:16262
263 set_defer_loading(false);
264 }
265
266 // ResourceLoaderBridge::Peer methods.
[email protected]04f6f982012-08-03 01:02:15267 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE {
[email protected]2602087e2009-08-24 23:12:16268 }
269
270 virtual bool OnReceivedRedirect(
271 const GURL& new_url,
[email protected]b4b3a912010-10-08 19:05:37272 const ResourceResponseInfo& info,
[email protected]041b0bbb2009-11-18 02:27:34273 bool* has_new_first_party_for_cookies,
[email protected]04f6f982012-08-03 01:02:15274 GURL* new_first_party_for_cookies) OVERRIDE {
[email protected]041b0bbb2009-11-18 02:27:34275 *has_new_first_party_for_cookies = false;
[email protected]2602087e2009-08-24 23:12:16276 return true;
277 }
278
[email protected]04f6f982012-08-03 01:02:15279 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE {
[email protected]bb551622010-07-22 20:52:49280 EXPECT_EQ(defer_loading_, false);
281 set_defer_loading(true);
282 }
283
[email protected]04f6f982012-08-03 01:02:15284 virtual void OnDownloadedData(int len) OVERRIDE {
[email protected]bb551622010-07-22 20:52:49285 }
286
[email protected]8bd0de72011-04-08 18:52:10287 virtual void OnReceivedData(const char* data,
288 int data_length,
[email protected]04f6f982012-08-03 01:02:15289 int encoded_data_length) OVERRIDE {
[email protected]2602087e2009-08-24 23:12:16290 EXPECT_EQ(defer_loading_, false);
291 set_defer_loading(false);
292 }
293
[email protected]04f6f982012-08-03 01:02:15294 virtual void OnCompletedRequest(
295 const net::URLRequestStatus& status,
296 const std::string& security_info,
297 const base::TimeTicks& completion_time) OVERRIDE {
[email protected]2602087e2009-08-24 23:12:16298 }
299
[email protected]2602087e2009-08-24 23:12:16300 protected:
[email protected]04f6f982012-08-03 01:02:15301 virtual void SetUp() OVERRIDE {
[email protected]2602087e2009-08-24 23:12:16302 ResourceDispatcherTest::SetUp();
[email protected]b8901082010-11-12 01:14:28303 shared_handle_.Delete(kShmemSegmentName);
[email protected]515f2492011-01-14 10:36:28304 EXPECT_TRUE(shared_handle_.CreateNamed(kShmemSegmentName, false, 100));
[email protected]2602087e2009-08-24 23:12:16305 }
306
[email protected]04f6f982012-08-03 01:02:15307 virtual void TearDown() OVERRIDE {
[email protected]2602087e2009-08-24 23:12:16308 shared_handle_.Close();
[email protected]b8901082010-11-12 01:14:28309 EXPECT_TRUE(shared_handle_.Delete(kShmemSegmentName));
[email protected]2602087e2009-08-24 23:12:16310 ResourceDispatcherTest::TearDown();
311 }
312
313 private:
314 void set_defer_loading(bool defer) {
315 defer_loading_ = defer;
316 dispatcher_->SetDefersLoading(0, defer);
317 }
318
319 bool defer_loading() const {
320 return defer_loading_;
321 }
322
323 bool defer_loading_;
324 base::SharedMemory shared_handle_;
325};
326
327TEST_F(DeferredResourceLoadingTest, DeferredLoadTest) {
328 MessageLoop message_loop(MessageLoop::TYPE_IO);
329
330 ResourceLoaderBridge* bridge = CreateBridge();
331
332 bridge->Start(this);
333 InitMessages();
334
335 // Dispatch deferred messages.
336 message_loop.RunAllPending();
337 delete bridge;
338}
[email protected]be7b41e82012-07-04 09:46:51339
[email protected]04f6f982012-08-03 01:02:15340class TimeConversionTest : public ResourceDispatcherTest,
341 public ResourceLoaderBridge::Peer {
342 public:
343 virtual bool Send(IPC::Message* msg) OVERRIDE {
344 delete msg;
345 return true;
346 }
347
348 void PerformTest(const ResourceResponseHead& response_head) {
349 scoped_ptr<ResourceLoaderBridge> bridge(CreateBridge());
350 bridge->Start(this);
351
352 dispatcher_->OnMessageReceived(
353 ResourceMsg_ReceivedResponse(0, 0, response_head));
354 }
355
356 // ResourceLoaderBridge::Peer methods.
357 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE {
358 }
359
360 virtual bool OnReceivedRedirect(
361 const GURL& new_url,
362 const ResourceResponseInfo& info,
363 bool* has_new_first_party_for_cookies,
364 GURL* new_first_party_for_cookies) {
365 return true;
366 }
367
368 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE {
369 response_info_ = info;
370 }
371
372 virtual void OnDownloadedData(int len) OVERRIDE {
373 }
374
375 virtual void OnReceivedData(const char* data,
376 int data_length,
377 int encoded_data_length) OVERRIDE {
378 }
379
380 virtual void OnCompletedRequest(
381 const net::URLRequestStatus& status,
382 const std::string& security_info,
383 const base::TimeTicks& completion_time) OVERRIDE {
384 }
385
386 const ResourceResponseInfo& response_info() const { return response_info_; }
387
388 private:
389 ResourceResponseInfo response_info_;
390};
391
392// TODO(simonjam): Enable this when 10829031 lands.
393TEST_F(TimeConversionTest, DISABLED_ProperlyInitialized) {
394 ResourceResponseHead response_head;
395 response_head.status.set_status(net::URLRequestStatus::SUCCESS);
396 response_head.request_start = base::TimeTicks::FromInternalValue(5);
397 response_head.response_start = base::TimeTicks::FromInternalValue(15);
398 response_head.load_timing.base_time = base::Time::Now();
399 response_head.load_timing.base_ticks = base::TimeTicks::FromInternalValue(10);
400 response_head.load_timing.dns_start = -1;
401 response_head.load_timing.connect_start = 3;
402
403 PerformTest(response_head);
404
405 EXPECT_LT(0, response_info().load_timing.base_ticks.ToInternalValue());
406 EXPECT_EQ(-1, response_info().load_timing.dns_start);
407 EXPECT_LE(0, response_info().load_timing.connect_start);
408}
409
410TEST_F(TimeConversionTest, PartiallyInitialized) {
411 ResourceResponseHead response_head;
412 response_head.status.set_status(net::URLRequestStatus::SUCCESS);
413 response_head.request_start = base::TimeTicks::FromInternalValue(5);
414 response_head.response_start = base::TimeTicks::FromInternalValue(15);
415
416 PerformTest(response_head);
417
418 EXPECT_EQ(0, response_info().load_timing.base_ticks.ToInternalValue());
419 EXPECT_EQ(-1, response_info().load_timing.dns_start);
420}
421
422TEST_F(TimeConversionTest, NotInitialized) {
423 ResourceResponseHead response_head;
424 response_head.status.set_status(net::URLRequestStatus::SUCCESS);
425
426 PerformTest(response_head);
427
428 EXPECT_EQ(0, response_info().load_timing.base_ticks.ToInternalValue());
429 EXPECT_EQ(-1, response_info().load_timing.dns_start);
430}
431
[email protected]be7b41e82012-07-04 09:46:51432} // namespace content