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 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST, filter); | 113 RUN_TEST_FORCEASYNC_AND_NOT(BinaryDataPOST, filter); |
114 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader, filter); | 114 RUN_TEST_FORCEASYNC_AND_NOT(CustomRequestHeader, filter); |
115 RUN_TEST_FORCEASYNC_AND_NOT(FailsBogusContentLength, filter); | 115 RUN_TEST_FORCEASYNC_AND_NOT(FailsBogusContentLength, filter); |
116 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile, filter); | 116 RUN_TEST_FORCEASYNC_AND_NOT(StreamToFile, filter); |
117 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedSameOriginRestriction, filter); | 117 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedSameOriginRestriction, filter); |
118 RUN_TEST_FORCEASYNC_AND_NOT(TrustedSameOriginRestriction, filter); | 118 RUN_TEST_FORCEASYNC_AND_NOT(TrustedSameOriginRestriction, filter); |
119 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCrossOriginRequest, filter); | 119 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCrossOriginRequest, filter); |
120 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCrossOriginRequest, filter); | 120 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCrossOriginRequest, filter); |
121 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedJavascriptURLRestriction, filter); | 121 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedJavascriptURLRestriction, filter); |
122 RUN_TEST_FORCEASYNC_AND_NOT(TrustedJavascriptURLRestriction, filter); | 122 RUN_TEST_FORCEASYNC_AND_NOT(TrustedJavascriptURLRestriction, filter); |
123 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedMethodRestriction, filter); | 123 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedHttpRequests, filter); |
124 RUN_TEST_FORCEASYNC_AND_NOT(TrustedMethodRestriction, filter); | 124 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHttpRequests, filter); |
125 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedHeaderRestriction, filter); | |
126 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHeaderRestriction, filter); | |
127 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCustomReferrer, filter); | |
128 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCustomReferrer, filter); | |
129 RUN_TEST_FORCEASYNC_AND_NOT(UntrustedCustomContentTransferEncoding, filter); | |
130 RUN_TEST_FORCEASYNC_AND_NOT(TrustedCustomContentTransferEncoding, filter); | |
131 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter); | 125 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter); |
132 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); | 126 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); |
133 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter); | 127 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter); |
134 } | 128 } |
135 | 129 |
136 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, | 130 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, |
137 std::string* data) { | 131 std::string* data) { |
138 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 132 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
139 char buf[256]; | 133 char buf[256]; |
140 int64_t offset = 0; | 134 int64_t offset = 0; |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 request.SetURL("javascript:foo = bar"); | 579 request.SetURL("javascript:foo = bar"); |
586 | 580 |
587 int32_t rv = OpenTrusted(request); | 581 int32_t rv = OpenTrusted(request); |
588 if (rv == PP_ERROR_NOACCESS) | 582 if (rv == PP_ERROR_NOACCESS) |
589 return ReportError( | 583 return ReportError( |
590 "Trusted Javascript URL request", rv); | 584 "Trusted Javascript URL request", rv); |
591 | 585 |
592 PASS(); | 586 PASS(); |
593 } | 587 } |
594 | 588 |
595 // HTTP methods are restricted only for untrusted loaders. Forbidden | 589 std::string TestURLLoader::TestUntrustedHttpRequests() { |
596 // methods are CONNECT, TRACE, and TRACK, and any string that is not a valid | 590 // HTTP methods are restricted only for untrusted loaders. Forbidden |
597 // token (containing special characters like CR, LF). | 591 // methods are CONNECT, TRACE, and TRACK, and any string that is not a |
598 // http://www.w3.org/TR/XMLHttpRequest/ | 592 // valid token (containing special characters like CR, LF). |
599 std::string TestURLLoader::TestUntrustedMethodRestriction() { | 593 // http://www.w3.org/TR/XMLHttpRequest/ |
600 ASSERT_EQ(OpenUntrusted("cOnNeCt", ""), PP_ERROR_NOACCESS); | 594 { |
601 ASSERT_EQ(OpenUntrusted("tRaCk", ""), PP_ERROR_NOACCESS); | 595 ASSERT_EQ(OpenUntrusted("cOnNeCt", ""), PP_ERROR_NOACCESS); |
602 ASSERT_EQ(OpenUntrusted("tRaCe", ""), PP_ERROR_NOACCESS); | 596 ASSERT_EQ(OpenUntrusted("tRaCk", ""), PP_ERROR_NOACCESS); |
603 ASSERT_EQ(OpenUntrusted("POST\x0d\x0ax-csrf-token:\x20test1234", ""), | 597 ASSERT_EQ(OpenUntrusted("tRaCe", ""), PP_ERROR_NOACCESS); |
604 PP_ERROR_NOACCESS); | 598 ASSERT_EQ(OpenUntrusted("POST\x0d\x0ax-csrf-token:\x20test1234", ""), |
605 PASS(); | 599 PP_ERROR_NOACCESS); |
606 } | 600 } |
| 601 // HTTP methods are restricted only for untrusted loaders. Try all headers |
| 602 // that are forbidden by http://www.w3.org/TR/XMLHttpRequest/. |
| 603 { |
| 604 ASSERT_EQ(OpenUntrusted("GET", "Accept-Charset:\n"), PP_ERROR_NOACCESS); |
| 605 ASSERT_EQ(OpenUntrusted("GET", "Accept-Encoding:\n"), PP_ERROR_NOACCESS); |
| 606 ASSERT_EQ(OpenUntrusted("GET", "Connection:\n"), PP_ERROR_NOACCESS); |
| 607 ASSERT_EQ(OpenUntrusted("GET", "Content-Length:\n"), PP_ERROR_NOACCESS); |
| 608 ASSERT_EQ(OpenUntrusted("GET", "Cookie:\n"), PP_ERROR_NOACCESS); |
| 609 ASSERT_EQ(OpenUntrusted("GET", "Cookie2:\n"), PP_ERROR_NOACCESS); |
| 610 ASSERT_EQ(OpenUntrusted( |
| 611 "GET", "Content-Transfer-Encoding:\n"), PP_ERROR_NOACCESS); |
| 612 ASSERT_EQ(OpenUntrusted("GET", "Date:\n"), PP_ERROR_NOACCESS); |
| 613 ASSERT_EQ(OpenUntrusted("GET", "Expect:\n"), PP_ERROR_NOACCESS); |
| 614 ASSERT_EQ(OpenUntrusted("GET", "Host:\n"), PP_ERROR_NOACCESS); |
| 615 ASSERT_EQ(OpenUntrusted("GET", "Keep-Alive:\n"), PP_ERROR_NOACCESS); |
| 616 ASSERT_EQ(OpenUntrusted("GET", "Referer:\n"), PP_ERROR_NOACCESS); |
| 617 ASSERT_EQ(OpenUntrusted("GET", "TE:\n"), PP_ERROR_NOACCESS); |
| 618 ASSERT_EQ(OpenUntrusted("GET", "Trailer:\n"), PP_ERROR_NOACCESS); |
| 619 ASSERT_EQ(OpenUntrusted( |
| 620 "GET", "Transfer-Encoding:\n"), PP_ERROR_NOACCESS); |
| 621 ASSERT_EQ(OpenUntrusted("GET", "Upgrade:\n"), PP_ERROR_NOACCESS); |
| 622 ASSERT_EQ(OpenUntrusted("GET", "User-Agent:\n"), PP_ERROR_NOACCESS); |
| 623 ASSERT_EQ(OpenUntrusted("GET", "Via:\n"), PP_ERROR_NOACCESS); |
| 624 ASSERT_EQ(OpenUntrusted( |
| 625 "GET", "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==:\n"), |
| 626 PP_ERROR_NOACCESS); |
| 627 ASSERT_EQ(OpenUntrusted("GET", "Sec-foo:\n"), PP_ERROR_NOACCESS); |
| 628 } |
| 629 // Untrusted requests with custom referrer should fail. |
| 630 { |
| 631 pp::URLRequestInfo request(instance_); |
| 632 request.SetCustomReferrerURL("http://www.google.com/"); |
607 | 633 |
608 // Trusted requests can use restricted methods. | 634 int32_t rv = OpenUntrusted(request); |
609 std::string TestURLLoader::TestTrustedMethodRestriction() { | 635 if (rv != PP_ERROR_NOACCESS) |
610 ASSERT_EQ(OpenTrusted("cOnNeCt", ""), PP_OK); | 636 return ReportError( |
611 ASSERT_EQ(OpenTrusted("tRaCk", ""), PP_OK); | 637 "Untrusted request with custom referrer restriction", rv); |
612 ASSERT_EQ(OpenTrusted("tRaCe", ""), PP_OK); | 638 } |
| 639 // Untrusted requests with custom transfer encodings should fail. |
| 640 { |
| 641 pp::URLRequestInfo request(instance_); |
| 642 request.SetCustomContentTransferEncoding("foo"); |
| 643 |
| 644 int32_t rv = OpenUntrusted(request); |
| 645 if (rv != PP_ERROR_NOACCESS) |
| 646 return ReportError( |
| 647 "Untrusted request with content-transfer-encoding restriction", rv); |
| 648 } |
613 | 649 |
614 PASS(); | 650 PASS(); |
615 } | 651 } |
616 | 652 |
617 // HTTP methods are restricted only for untrusted loaders. Try all headers | 653 std::string TestURLLoader::TestTrustedHttpRequests() { |
618 // that are forbidden by http://www.w3.org/TR/XMLHttpRequest/. | 654 // Trusted requests can use restricted methods. |
619 std::string TestURLLoader::TestUntrustedHeaderRestriction() { | 655 { |
620 ASSERT_EQ(OpenUntrusted("GET", "Accept-Charset:\n"), PP_ERROR_NOACCESS); | 656 ASSERT_EQ(OpenTrusted("cOnNeCt", ""), PP_OK); |
621 ASSERT_EQ(OpenUntrusted("GET", "Accept-Encoding:\n"), PP_ERROR_NOACCESS); | 657 ASSERT_EQ(OpenTrusted("tRaCk", ""), PP_OK); |
622 ASSERT_EQ(OpenUntrusted("GET", "Connection:\n"), PP_ERROR_NOACCESS); | 658 ASSERT_EQ(OpenTrusted("tRaCe", ""), PP_OK); |
623 ASSERT_EQ(OpenUntrusted("GET", "Content-Length:\n"), PP_ERROR_NOACCESS); | 659 } |
624 ASSERT_EQ(OpenUntrusted("GET", "Cookie:\n"), PP_ERROR_NOACCESS); | 660 // Trusted requests can use restricted headers. |
625 ASSERT_EQ(OpenUntrusted("GET", "Cookie2:\n"), PP_ERROR_NOACCESS); | 661 { |
626 ASSERT_EQ(OpenUntrusted( | 662 ASSERT_EQ(OpenTrusted("GET", "Accept-Charset:\n"), PP_OK); |
627 "GET", "Content-Transfer-Encoding:\n"), PP_ERROR_NOACCESS); | 663 ASSERT_EQ(OpenTrusted("GET", "Accept-Encoding:\n"), PP_OK); |
628 ASSERT_EQ(OpenUntrusted("GET", "Date:\n"), PP_ERROR_NOACCESS); | 664 ASSERT_EQ(OpenTrusted("GET", "Connection:\n"), PP_OK); |
629 ASSERT_EQ(OpenUntrusted("GET", "Expect:\n"), PP_ERROR_NOACCESS); | 665 ASSERT_EQ(OpenTrusted("GET", "Content-Length:\n"), PP_OK); |
630 ASSERT_EQ(OpenUntrusted("GET", "Host:\n"), PP_ERROR_NOACCESS); | 666 ASSERT_EQ(OpenTrusted("GET", "Cookie:\n"), PP_OK); |
631 ASSERT_EQ(OpenUntrusted("GET", "Keep-Alive:\n"), PP_ERROR_NOACCESS); | 667 ASSERT_EQ(OpenTrusted("GET", "Cookie2:\n"), PP_OK); |
632 ASSERT_EQ(OpenUntrusted("GET", "Referer:\n"), PP_ERROR_NOACCESS); | 668 ASSERT_EQ(OpenTrusted( |
633 ASSERT_EQ(OpenUntrusted("GET", "TE:\n"), PP_ERROR_NOACCESS); | 669 "GET", "Content-Transfer-Encoding:\n"), PP_OK); |
634 ASSERT_EQ(OpenUntrusted("GET", "Trailer:\n"), PP_ERROR_NOACCESS); | 670 ASSERT_EQ(OpenTrusted("GET", "Date:\n"), PP_OK); |
635 ASSERT_EQ(OpenUntrusted("GET", "Transfer-Encoding:\n"), PP_ERROR_NOACCESS); | 671 ASSERT_EQ(OpenTrusted("GET", "Expect:\n"), PP_OK); |
636 ASSERT_EQ(OpenUntrusted("GET", "Upgrade:\n"), PP_ERROR_NOACCESS); | 672 ASSERT_EQ(OpenTrusted("GET", "Host:\n"), PP_OK); |
637 ASSERT_EQ(OpenUntrusted("GET", "User-Agent:\n"), PP_ERROR_NOACCESS); | 673 ASSERT_EQ(OpenTrusted("GET", "Keep-Alive:\n"), PP_OK); |
638 ASSERT_EQ(OpenUntrusted("GET", "Via:\n"), PP_ERROR_NOACCESS); | 674 ASSERT_EQ(OpenTrusted("GET", "Referer:\n"), PP_OK); |
639 ASSERT_EQ(OpenUntrusted( | 675 ASSERT_EQ(OpenTrusted("GET", "TE:\n"), PP_OK); |
640 "GET", "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==:\n"), | 676 ASSERT_EQ(OpenTrusted("GET", "Trailer:\n"), PP_OK); |
641 PP_ERROR_NOACCESS); | 677 ASSERT_EQ(OpenTrusted("GET", "Transfer-Encoding:\n"), PP_OK); |
642 ASSERT_EQ(OpenUntrusted("GET", "Sec-foo:\n"), PP_ERROR_NOACCESS); | 678 ASSERT_EQ(OpenTrusted("GET", "Upgrade:\n"), PP_OK); |
| 679 ASSERT_EQ(OpenTrusted("GET", "User-Agent:\n"), PP_OK); |
| 680 ASSERT_EQ(OpenTrusted("GET", "Via:\n"), PP_OK); |
| 681 ASSERT_EQ(OpenTrusted( |
| 682 "GET", "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==:\n"), PP_OK); |
| 683 ASSERT_EQ(OpenTrusted("GET", "Sec-foo:\n"), PP_OK); |
| 684 } |
| 685 // Trusted requests with custom referrer should succeed. |
| 686 { |
| 687 pp::URLRequestInfo request(instance_); |
| 688 request.SetCustomReferrerURL("http://www.google.com/"); |
643 | 689 |
644 PASS(); | 690 int32_t rv = OpenTrusted(request); |
645 } | 691 if (rv != PP_OK) |
| 692 return ReportError("Trusted request with custom referrer", rv); |
| 693 } |
| 694 // Trusted requests with custom transfer encodings should succeed. |
| 695 { |
| 696 pp::URLRequestInfo request(instance_); |
| 697 request.SetCustomContentTransferEncoding("foo"); |
646 | 698 |
647 // Trusted requests can use restricted headers. | 699 int32_t rv = OpenTrusted(request); |
648 std::string TestURLLoader::TestTrustedHeaderRestriction() { | 700 if (rv != PP_OK) |
649 ASSERT_EQ(OpenTrusted("GET", "Accept-Charset:\n"), PP_OK); | 701 return ReportError( |
650 ASSERT_EQ(OpenTrusted("GET", "Accept-Encoding:\n"), PP_OK); | 702 "Trusted request with content-transfer-encoding failed", rv); |
651 ASSERT_EQ(OpenTrusted("GET", "Connection:\n"), PP_OK); | 703 } |
652 ASSERT_EQ(OpenTrusted("GET", "Content-Length:\n"), PP_OK); | |
653 ASSERT_EQ(OpenTrusted("GET", "Cookie:\n"), PP_OK); | |
654 ASSERT_EQ(OpenTrusted("GET", "Cookie2:\n"), PP_OK); | |
655 ASSERT_EQ(OpenTrusted( | |
656 "GET", "Content-Transfer-Encoding:\n"), PP_OK); | |
657 ASSERT_EQ(OpenTrusted("GET", "Date:\n"), PP_OK); | |
658 ASSERT_EQ(OpenTrusted("GET", "Expect:\n"), PP_OK); | |
659 ASSERT_EQ(OpenTrusted("GET", "Host:\n"), PP_OK); | |
660 ASSERT_EQ(OpenTrusted("GET", "Keep-Alive:\n"), PP_OK); | |
661 ASSERT_EQ(OpenTrusted("GET", "Referer:\n"), PP_OK); | |
662 ASSERT_EQ(OpenTrusted("GET", "TE:\n"), PP_OK); | |
663 ASSERT_EQ(OpenTrusted("GET", "Trailer:\n"), PP_OK); | |
664 ASSERT_EQ(OpenTrusted("GET", "Transfer-Encoding:\n"), PP_OK); | |
665 ASSERT_EQ(OpenTrusted("GET", "Upgrade:\n"), PP_OK); | |
666 ASSERT_EQ(OpenTrusted("GET", "User-Agent:\n"), PP_OK); | |
667 ASSERT_EQ(OpenTrusted("GET", "Via:\n"), PP_OK); | |
668 ASSERT_EQ(OpenTrusted( | |
669 "GET", "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==:\n"), PP_OK); | |
670 ASSERT_EQ(OpenTrusted("GET", "Sec-foo:\n"), PP_OK); | |
671 | 704 |
672 PASS(); | 705 PASS(); |
673 } | 706 } |
674 | 707 |
675 // Untrusted requests with custom referrer should fail. | |
676 std::string TestURLLoader::TestUntrustedCustomReferrer() { | |
677 pp::URLRequestInfo request(instance_); | |
678 request.SetCustomReferrerURL("http://www.google.com/"); | |
679 | |
680 int32_t rv = OpenUntrusted(request); | |
681 if (rv != PP_ERROR_NOACCESS) | |
682 return ReportError( | |
683 "Untrusted request with custom referrer restriction", rv); | |
684 | |
685 PASS(); | |
686 } | |
687 | |
688 // Trusted requests with custom referrer should succeed. | |
689 std::string TestURLLoader::TestTrustedCustomReferrer() { | |
690 pp::URLRequestInfo request(instance_); | |
691 request.SetCustomReferrerURL("http://www.google.com/"); | |
692 | |
693 int32_t rv = OpenTrusted(request); | |
694 if (rv != PP_OK) | |
695 return ReportError("Trusted request with custom referrer", rv); | |
696 | |
697 PASS(); | |
698 } | |
699 | |
700 // Untrusted requests with custom transfer encodings should fail. | |
701 std::string TestURLLoader::TestUntrustedCustomContentTransferEncoding() { | |
702 pp::URLRequestInfo request(instance_); | |
703 request.SetCustomContentTransferEncoding("foo"); | |
704 | |
705 int32_t rv = OpenUntrusted(request); | |
706 if (rv != PP_ERROR_NOACCESS) | |
707 return ReportError( | |
708 "Untrusted request with content-transfer-encoding restriction", rv); | |
709 | |
710 PASS(); | |
711 } | |
712 | |
713 // Trusted requests with custom transfer encodings should succeed. | |
714 std::string TestURLLoader::TestTrustedCustomContentTransferEncoding() { | |
715 pp::URLRequestInfo request(instance_); | |
716 request.SetCustomContentTransferEncoding("foo"); | |
717 | |
718 int32_t rv = OpenTrusted(request); | |
719 if (rv != PP_OK) | |
720 return ReportError("Trusted request with content-transfer-encoding failed", | |
721 rv); | |
722 PASS(); | |
723 } | |
724 | |
725 // This test should cause a redirect and ensure that the loader runs | 708 // This test should cause a redirect and ensure that the loader runs |
726 // the callback, rather than following the redirect. | 709 // the callback, rather than following the redirect. |
727 std::string TestURLLoader::TestAuditURLRedirect() { | 710 std::string TestURLLoader::TestAuditURLRedirect() { |
728 pp::URLRequestInfo request(instance_); | 711 pp::URLRequestInfo request(instance_); |
729 // This path will cause the server to return a 301 redirect. | 712 // This path will cause the server to return a 301 redirect. |
730 request.SetURL("/server-redirect?www.google.com"); | 713 request.SetURL("/server-redirect?www.google.com"); |
731 request.SetFollowRedirects(false); | 714 request.SetFollowRedirects(false); |
732 | 715 |
733 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 716 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
734 | 717 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 if (!error.empty()) | 838 if (!error.empty()) |
856 return error; | 839 return error; |
857 if (body != "hello\n") | 840 if (body != "hello\n") |
858 return ReportError("Couldn't read data", rv); | 841 return ReportError("Couldn't read data", rv); |
859 | 842 |
860 PASS(); | 843 PASS(); |
861 } | 844 } |
862 | 845 |
863 // TODO(viettrungluu): Add tests for FollowRedirect, | 846 // TODO(viettrungluu): Add tests for FollowRedirect, |
864 // Get{Upload,Download}Progress, Close (including abort tests if applicable). | 847 // Get{Upload,Download}Progress, Close (including abort tests if applicable). |
OLD | NEW |