OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/tests/test_url_loader.h" | 5 #include "ppapi/tests/test_url_loader.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "ppapi/c/dev/ppb_testing_dev.h" | 11 #include "ppapi/c/dev/ppb_testing_dev.h" |
12 #include "ppapi/c/dev/ppb_url_util_dev.h" | |
13 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
14 #include "ppapi/c/ppb_file_io.h" | 13 #include "ppapi/c/ppb_file_io.h" |
15 #include "ppapi/c/ppb_url_loader.h" | 14 #include "ppapi/c/ppb_url_loader.h" |
16 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | 15 #include "ppapi/c/trusted/ppb_file_io_trusted.h" |
17 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 16 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
18 #include "ppapi/cpp/dev/url_util_dev.h" | 17 #include "ppapi/cpp/dev/url_util_dev.h" |
19 #include "ppapi/cpp/file_io.h" | 18 #include "ppapi/cpp/file_io.h" |
20 #include "ppapi/cpp/file_ref.h" | 19 #include "ppapi/cpp/file_ref.h" |
21 #include "ppapi/cpp/file_system.h" | 20 #include "ppapi/cpp/file_system.h" |
22 #include "ppapi/cpp/instance.h" | 21 #include "ppapi/cpp/instance.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 RUN_TEST_FORCEASYNC_AND_NOT(FailsBogusContentLength, filter); | 114 RUN_TEST_FORCEASYNC_AND_NOT(FailsBogusContentLength, filter); |
116 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile, filter); | 115 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile, filter); |
117 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedSameOriginRestriction, filter); | 116 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedSameOriginRestriction, filter); |
118 RUN_TEST_FORCEASYNC_AND_NOT(TrustedSameOriginRestriction, filter); | 117 RUN_TEST_FORCEASYNC_AND_NOT(TrustedSameOriginRestriction, filter); |
119 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCrossOriginRequest, filter); | 118 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCrossOriginRequest, filter); |
120 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCrossOriginRequest, filter); | 119 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCrossOriginRequest, filter); |
121 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedJavascriptURLRestriction, filter); | 120 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedJavascriptURLRestriction, filter); |
122 RUN_TEST_FORCEASYNC_AND_NOT(TrustedJavascriptURLRestriction, filter); | 121 RUN_TEST_FORCEASYNC_AND_NOT(TrustedJavascriptURLRestriction, filter); |
123 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedHttpRequests, filter); | 122 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedHttpRequests, filter); |
124 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHttpRequests, filter); | 123 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHttpRequests, filter); |
| 124 RUN_TEST_FORCEASYNC_AND_NOT(FollowURLRedirect, filter); |
125 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter); | 125 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter); |
126 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); | 126 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); |
127 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter); | 127 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter); |
128 } | 128 } |
129 | 129 |
130 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, | 130 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, |
131 std::string* data) { | 131 std::string* data) { |
132 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 132 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
133 char buf[256]; | 133 char buf[256]; |
134 int64_t offset = 0; | 134 int64_t offset = 0; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 246 |
247 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data); | 247 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data); |
248 if (rv != PP_OK) { | 248 if (rv != PP_OK) { |
249 message->assign("FileIO::Write"); | 249 message->assign("FileIO::Write"); |
250 return rv; | 250 return rv; |
251 } | 251 } |
252 | 252 |
253 return rv; | 253 return rv; |
254 } | 254 } |
255 | 255 |
256 std::string TestURLLoader::GetReachableCrossOriginURL() { | 256 std::string TestURLLoader::GetReachableAbsoluteURL( |
257 // Get the document URL and use it to construct a URL that will be | 257 const std::string& file_name) { |
258 // considered cross-origin by the WebKit access control code, and yet be | 258 // Request the test page and scrape the absolute URL that the server returns |
| 259 // as part of the response. Using the test page allows us to test redirected |
| 260 // and cross-origin requests since we have a mock headers file. |
| 261 pp::URLRequestInfo request(instance_); |
| 262 request.SetURL(file_name); |
| 263 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 264 |
| 265 pp::URLLoader loader(*instance_); |
| 266 int32_t rv = loader.Open(request, callback); |
| 267 if (rv == PP_OK_COMPLETIONPENDING) |
| 268 rv = callback.WaitForResult(); |
| 269 ASSERT_EQ(rv, PP_OK); |
| 270 |
| 271 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
| 272 ASSERT_FALSE(response_info.is_null()); |
| 273 ASSERT_EQ(response_info.GetStatusCode(), 200); |
| 274 return response_info.GetURL().AsString(); |
| 275 } |
| 276 |
| 277 std::string TestURLLoader::GetReachableCrossOriginURL( |
| 278 const std::string& file_name) { |
| 279 // Get an absolute URL and use it to construct a URL that will be |
| 280 // considered cross-origin by the CORS access control code, and yet be |
259 // reachable by the test server. | 281 // reachable by the test server. |
260 PP_URLComponents_Dev components; | 282 std::string url = GetReachableAbsoluteURL(file_name); |
261 pp::Var pp_document_url = pp::URLUtil_Dev::Get()->GetDocumentURL( | 283 // Replace '127.0.0.1' with 'localhost'. |
262 *instance_, &components); | 284 std::string host("127.0.0.1"); |
263 std::string document_url = pp_document_url.AsString(); | 285 size_t index = url.find(host); |
264 // Replace "127.0.0.1" with "localhost". Or vice versa. | 286 ASSERT_NE(index, std::string::npos); |
265 bool changedHost = false; | 287 url.replace(index, host.length(), "localhost"); |
266 if (document_url.find("127.0.0.1") != std::string::npos) { | 288 return url; |
267 document_url.replace(components.host.begin, | |
268 components.host.len, | |
269 "localhost"); | |
270 changedHost = true; | |
271 } else if (document_url.find("localhost") != std::string::npos) { | |
272 document_url.replace(components.host.begin, | |
273 components.host.len, | |
274 "127.0.0.1"); | |
275 changedHost = true; | |
276 } | |
277 ASSERT_TRUE(changedHost); | |
278 return document_url; | |
279 } | 289 } |
280 | 290 |
281 int32_t TestURLLoader::OpenUntrusted(const std::string& method, | 291 int32_t TestURLLoader::OpenUntrusted(const std::string& method, |
282 const std::string& header) { | 292 const std::string& header) { |
283 pp::URLRequestInfo request(instance_); | 293 pp::URLRequestInfo request(instance_); |
284 request.SetURL("/echo"); | 294 request.SetURL("/echo"); |
285 request.SetMethod(method); | 295 request.SetMethod(method); |
286 request.SetHeaders(header); | 296 request.SetHeaders(header); |
287 | 297 |
288 return OpenUntrusted(request); | 298 return OpenUntrusted(request); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; | 510 return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; |
501 #endif | 511 #endif |
502 } | 512 } |
503 } | 513 } |
504 PASS(); | 514 PASS(); |
505 } | 515 } |
506 | 516 |
507 // Untrusted, unintended cross-origin requests should fail. | 517 // Untrusted, unintended cross-origin requests should fail. |
508 std::string TestURLLoader::TestUntrustedSameOriginRestriction() { | 518 std::string TestURLLoader::TestUntrustedSameOriginRestriction() { |
509 pp::URLRequestInfo request(instance_); | 519 pp::URLRequestInfo request(instance_); |
510 std::string cross_origin_url = GetReachableCrossOriginURL(); | 520 std::string cross_origin_url = GetReachableCrossOriginURL("test_case.html"); |
511 request.SetURL(cross_origin_url); | 521 request.SetURL(cross_origin_url); |
512 | 522 |
513 int32_t rv = OpenUntrusted(request); | 523 int32_t rv = OpenUntrusted(request); |
514 if (rv != PP_ERROR_NOACCESS) | 524 if (rv != PP_ERROR_NOACCESS) |
515 return ReportError( | 525 return ReportError( |
516 "Untrusted, unintended cross-origin request restriction", rv); | 526 "Untrusted, unintended cross-origin request restriction", rv); |
517 | 527 |
518 PASS(); | 528 PASS(); |
519 } | 529 } |
520 | 530 |
521 // Trusted, unintended cross-origin requests should succeed. | 531 // Trusted, unintended cross-origin requests should succeed. |
522 std::string TestURLLoader::TestTrustedSameOriginRestriction() { | 532 std::string TestURLLoader::TestTrustedSameOriginRestriction() { |
523 pp::URLRequestInfo request(instance_); | 533 pp::URLRequestInfo request(instance_); |
524 std::string cross_origin_url = GetReachableCrossOriginURL(); | 534 std::string cross_origin_url = GetReachableCrossOriginURL("test_case.html"); |
525 request.SetURL(cross_origin_url); | 535 request.SetURL(cross_origin_url); |
526 | 536 |
527 int32_t rv = OpenTrusted(request); | 537 int32_t rv = OpenTrusted(request); |
528 if (rv != PP_OK) | 538 if (rv != PP_OK) |
529 return ReportError("Trusted cross-origin request failed", rv); | 539 return ReportError("Trusted cross-origin request failed", rv); |
530 | 540 |
531 PASS(); | 541 PASS(); |
532 } | 542 } |
533 | 543 |
534 // Untrusted, intended cross-origin requests should use CORS and succeed. | 544 // Untrusted, intended cross-origin requests should use CORS and succeed. |
535 std::string TestURLLoader::TestUntrustedCrossOriginRequest() { | 545 std::string TestURLLoader::TestUntrustedCrossOriginRequest() { |
536 pp::URLRequestInfo request(instance_); | 546 pp::URLRequestInfo request(instance_); |
537 std::string cross_origin_url = GetReachableCrossOriginURL(); | 547 std::string cross_origin_url = GetReachableCrossOriginURL("test_case.html"); |
538 request.SetURL(cross_origin_url); | 548 request.SetURL(cross_origin_url); |
539 request.SetAllowCrossOriginRequests(true); | 549 request.SetAllowCrossOriginRequests(true); |
540 | 550 |
541 int32_t rv = OpenUntrusted(request); | 551 int32_t rv = OpenUntrusted(request); |
542 if (rv != PP_OK) | 552 if (rv != PP_OK) |
543 return ReportError( | 553 return ReportError( |
544 "Untrusted, intended cross-origin request failed", rv); | 554 "Untrusted, intended cross-origin request failed", rv); |
545 | 555 |
546 PASS(); | 556 PASS(); |
547 } | 557 } |
548 | 558 |
549 // Trusted, intended cross-origin requests should use CORS and succeed. | 559 // Trusted, intended cross-origin requests should use CORS and succeed. |
550 std::string TestURLLoader::TestTrustedCrossOriginRequest() { | 560 std::string TestURLLoader::TestTrustedCrossOriginRequest() { |
551 pp::URLRequestInfo request(instance_); | 561 pp::URLRequestInfo request(instance_); |
552 std::string cross_origin_url = GetReachableCrossOriginURL(); | 562 std::string cross_origin_url = GetReachableCrossOriginURL("test_case.html"); |
553 request.SetURL(cross_origin_url); | 563 request.SetURL(cross_origin_url); |
554 request.SetAllowCrossOriginRequests(true); | 564 request.SetAllowCrossOriginRequests(true); |
555 | 565 |
556 int32_t rv = OpenTrusted(request); | 566 int32_t rv = OpenTrusted(request); |
557 if (rv != PP_OK) | 567 if (rv != PP_OK) |
558 return ReportError("Trusted cross-origin request failed", rv); | 568 return ReportError("Trusted cross-origin request failed", rv); |
559 | 569 |
560 PASS(); | 570 PASS(); |
561 } | 571 } |
562 | 572 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 | 708 |
699 int32_t rv = OpenTrusted(request); | 709 int32_t rv = OpenTrusted(request); |
700 if (rv != PP_OK) | 710 if (rv != PP_OK) |
701 return ReportError( | 711 return ReportError( |
702 "Trusted request with content-transfer-encoding failed", rv); | 712 "Trusted request with content-transfer-encoding failed", rv); |
703 } | 713 } |
704 | 714 |
705 PASS(); | 715 PASS(); |
706 } | 716 } |
707 | 717 |
| 718 // This test should cause a redirect and ensure that the loader follows it. |
| 719 std::string TestURLLoader::TestFollowURLRedirect() { |
| 720 pp::URLRequestInfo request(instance_); |
| 721 // This prefix causes the test server to return a 301 redirect. |
| 722 std::string redirect_prefix("/server-redirect?"); |
| 723 // We need an absolute path for the redirect to actually work. |
| 724 std::string redirect_url = |
| 725 GetReachableAbsoluteURL("test_url_loader_data/hello.txt"); |
| 726 request.SetURL(redirect_prefix.append(redirect_url)); |
| 727 return LoadAndCompareBody(request, "hello\n"); |
| 728 } |
| 729 |
708 // This test should cause a redirect and ensure that the loader runs | 730 // This test should cause a redirect and ensure that the loader runs |
709 // the callback, rather than following the redirect. | 731 // the callback, rather than following the redirect. |
710 std::string TestURLLoader::TestAuditURLRedirect() { | 732 std::string TestURLLoader::TestAuditURLRedirect() { |
711 pp::URLRequestInfo request(instance_); | 733 pp::URLRequestInfo request(instance_); |
712 // This path will cause the server to return a 301 redirect. | 734 // This path will cause the server to return a 301 redirect. |
713 request.SetURL("/server-redirect?www.google.com"); | 735 // This prefix causes the test server to return a 301 redirect. |
| 736 std::string redirect_prefix("/server-redirect?"); |
| 737 // We need an absolute path for the redirect to actually work. |
| 738 std::string redirect_url = |
| 739 GetReachableAbsoluteURL("test_url_loader_data/hello.txt"); |
| 740 request.SetURL(redirect_prefix.append(redirect_url)); |
714 request.SetFollowRedirects(false); | 741 request.SetFollowRedirects(false); |
715 | 742 |
716 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 743 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
717 | 744 |
718 pp::URLLoader loader(*instance_); | 745 pp::URLLoader loader(*instance_); |
719 int32_t rv = loader.Open(request, callback); | 746 int32_t rv = loader.Open(request, callback); |
720 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 747 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
721 return ReportError("URLLoader::Open force_async", rv); | 748 return ReportError("URLLoader::Open force_async", rv); |
722 if (rv == PP_OK_COMPLETIONPENDING) | 749 if (rv == PP_OK_COMPLETIONPENDING) |
723 rv = callback.WaitForResult(); | 750 rv = callback.WaitForResult(); |
724 if (rv != PP_OK) | 751 if (rv != PP_OK) |
725 return ReportError("URLLoader::Open", rv); | 752 return ReportError("URLLoader::Open", rv); |
726 | 753 |
727 // Checks that the response indicates a redirect, and that the URL | 754 // Checks that the response indicates a redirect, and that the URL |
728 // is correct. | 755 // is correct. |
729 pp::URLResponseInfo response_info(loader.GetResponseInfo()); | 756 pp::URLResponseInfo response_info(loader.GetResponseInfo()); |
730 if (response_info.is_null()) | 757 if (response_info.is_null()) |
731 return "URLLoader::GetResponseInfo returned null"; | 758 return "URLLoader::GetResponseInfo returned null"; |
732 int32_t status_code = response_info.GetStatusCode(); | 759 int32_t status_code = response_info.GetStatusCode(); |
733 if (status_code != 301) | 760 if (status_code != 301) |
734 return "Response status should be 301"; | 761 return "Response status should be 301"; |
735 if (response_info.GetRedirectURL().AsString() != "www.google.com") | 762 |
736 return "Redirect URL should be www.google.com"; | 763 // Test that the paused loader can be resumed. |
| 764 TestCompletionCallback redirect_callback(instance_->pp_instance(), |
| 765 force_async_); |
| 766 rv = loader.FollowRedirect(redirect_callback); |
| 767 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 768 return ReportError("URLLoader::FollowRedirect force_async", rv); |
| 769 if (rv == PP_OK_COMPLETIONPENDING) |
| 770 rv = redirect_callback.WaitForResult(); |
| 771 if (rv != PP_OK) |
| 772 return ReportError("URLLoader::FollowRedirect", rv); |
| 773 std::string body; |
| 774 std::string error = ReadEntireResponseBody(&loader, &body); |
| 775 if (!error.empty()) |
| 776 return error; |
| 777 |
| 778 if (body != "hello\n") |
| 779 return "URLLoader::FollowRedirect failed"; |
737 | 780 |
738 PASS(); | 781 PASS(); |
739 } | 782 } |
740 | 783 |
741 std::string TestURLLoader::TestAbortCalls() { | 784 std::string TestURLLoader::TestAbortCalls() { |
742 pp::URLRequestInfo request(instance_); | 785 pp::URLRequestInfo request(instance_); |
743 request.SetURL("test_url_loader_data/hello.txt"); | 786 request.SetURL("test_url_loader_data/hello.txt"); |
744 | 787 |
745 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 788 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
746 int32_t rv; | 789 int32_t rv; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 std::string body; | 879 std::string body; |
837 std::string error = ReadEntireResponseBody(&loader, &body); | 880 std::string error = ReadEntireResponseBody(&loader, &body); |
838 if (!error.empty()) | 881 if (!error.empty()) |
839 return error; | 882 return error; |
840 if (body != "hello\n") | 883 if (body != "hello\n") |
841 return ReportError("Couldn't read data", rv); | 884 return ReportError("Couldn't read data", rv); |
842 | 885 |
843 PASS(); | 886 PASS(); |
844 } | 887 } |
845 | 888 |
846 // TODO(viettrungluu): Add tests for FollowRedirect, | 889 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close |
847 // Get{Upload,Download}Progress, Close (including abort tests if applicable). | 890 // (including abort tests if applicable). |
| 891 |
OLD | NEW |