OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #endif | 10 #endif |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 #include "net/http/http_util.h" | 71 #include "net/http/http_util.h" |
72 #include "net/log/net_log.h" | 72 #include "net/log/net_log.h" |
73 #include "net/log/test_net_log.h" | 73 #include "net/log/test_net_log.h" |
74 #include "net/log/test_net_log_entry.h" | 74 #include "net/log/test_net_log_entry.h" |
75 #include "net/log/test_net_log_util.h" | 75 #include "net/log/test_net_log_util.h" |
76 #include "net/proxy/proxy_service.h" | 76 #include "net/proxy/proxy_service.h" |
77 #include "net/socket/ssl_client_socket.h" | 77 #include "net/socket/ssl_client_socket.h" |
78 #include "net/ssl/ssl_cipher_suite_names.h" | 78 #include "net/ssl/ssl_cipher_suite_names.h" |
79 #include "net/ssl/ssl_connection_status_flags.h" | 79 #include "net/ssl/ssl_connection_status_flags.h" |
80 #include "net/test/cert_test_util.h" | 80 #include "net/test/cert_test_util.h" |
| 81 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 82 #include "net/test/embedded_test_server/http_request.h" |
| 83 #include "net/test/embedded_test_server/http_response.h" |
81 #include "net/test/spawned_test_server/spawned_test_server.h" | 84 #include "net/test/spawned_test_server/spawned_test_server.h" |
82 #include "net/test/url_request/url_request_failed_job.h" | 85 #include "net/test/url_request/url_request_failed_job.h" |
83 #include "net/url_request/data_protocol_handler.h" | 86 #include "net/url_request/data_protocol_handler.h" |
84 #include "net/url_request/static_http_user_agent_settings.h" | 87 #include "net/url_request/static_http_user_agent_settings.h" |
85 #include "net/url_request/url_request.h" | 88 #include "net/url_request/url_request.h" |
86 #include "net/url_request/url_request_filter.h" | 89 #include "net/url_request/url_request_filter.h" |
87 #include "net/url_request/url_request_http_job.h" | 90 #include "net/url_request/url_request_http_job.h" |
88 #include "net/url_request/url_request_intercepting_job_factory.h" | 91 #include "net/url_request/url_request_intercepting_job_factory.h" |
89 #include "net/url_request/url_request_interceptor.h" | 92 #include "net/url_request/url_request_interceptor.h" |
90 #include "net/url_request/url_request_job_factory_impl.h" | 93 #include "net/url_request/url_request_job_factory_impl.h" |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 } | 658 } |
656 | 659 |
657 const GURL& latest_report_uri() { return latest_report_uri_; } | 660 const GURL& latest_report_uri() { return latest_report_uri_; } |
658 const std::string& latest_report() { return latest_report_; } | 661 const std::string& latest_report() { return latest_report_; } |
659 | 662 |
660 private: | 663 private: |
661 GURL latest_report_uri_; | 664 GURL latest_report_uri_; |
662 std::string latest_report_; | 665 std::string latest_report_; |
663 }; | 666 }; |
664 | 667 |
| 668 class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 669 public: |
| 670 bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
| 671 }; |
| 672 |
665 } // namespace | 673 } // namespace |
666 | 674 |
667 // Inherit PlatformTest since we require the autorelease pool on Mac OS X. | 675 // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
668 class URLRequestTest : public PlatformTest { | 676 class URLRequestTest : public PlatformTest { |
669 public: | 677 public: |
670 URLRequestTest() : default_context_(true) { | 678 URLRequestTest() : default_context_(true) { |
671 default_context_.set_network_delegate(&default_network_delegate_); | 679 default_context_.set_network_delegate(&default_network_delegate_); |
672 default_context_.set_net_log(&net_log_); | 680 default_context_.set_net_log(&net_log_); |
673 job_factory_impl_ = new URLRequestJobFactoryImpl(); | 681 job_factory_impl_ = new URLRequestJobFactoryImpl(); |
674 job_factory_.reset(job_factory_impl_); | 682 job_factory_.reset(job_factory_impl_); |
(...skipping 1520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2195 explicit LocalHttpTestServer(const base::FilePath& document_root) | 2203 explicit LocalHttpTestServer(const base::FilePath& document_root) |
2196 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, | 2204 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
2197 SpawnedTestServer::kLocalhost, | 2205 SpawnedTestServer::kLocalhost, |
2198 document_root) {} | 2206 document_root) {} |
2199 LocalHttpTestServer() | 2207 LocalHttpTestServer() |
2200 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, | 2208 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
2201 SpawnedTestServer::kLocalhost, | 2209 SpawnedTestServer::kLocalhost, |
2202 base::FilePath()) {} | 2210 base::FilePath()) {} |
2203 }; | 2211 }; |
2204 | 2212 |
| 2213 scoped_ptr<net::test_server::HttpResponse> HandleSetCookieRequest( |
| 2214 const test_server::HttpRequest& request) { |
| 2215 scoped_ptr<test_server::BasicHttpResponse> http_response( |
| 2216 new test_server::BasicHttpResponse()); |
| 2217 if (request.relative_url.find("/set-cookie?") != 0) { |
| 2218 http_response->set_code(net::HTTP_NOT_FOUND); |
| 2219 http_response->set_content("hello"); |
| 2220 return http_response.Pass(); |
| 2221 } |
| 2222 http_response->set_code(net::HTTP_OK); |
| 2223 http_response->set_content("hello"); |
| 2224 http_response->set_content_type("text/plain"); |
| 2225 http_response->AddCustomHeader( |
| 2226 "Set-Cookie", |
| 2227 request.relative_url.substr(request.relative_url.find("?") + 1)); |
| 2228 return http_response.Pass(); |
| 2229 } |
| 2230 |
2205 } // namespace | 2231 } // namespace |
2206 | 2232 |
2207 TEST_F(URLRequestTest, DelayedCookieCallback) { | 2233 TEST_F(URLRequestTest, DelayedCookieCallback) { |
2208 LocalHttpTestServer test_server; | 2234 LocalHttpTestServer test_server; |
2209 ASSERT_TRUE(test_server.Start()); | 2235 ASSERT_TRUE(test_server.Start()); |
2210 | 2236 |
2211 TestURLRequestContext context; | 2237 TestURLRequestContext context; |
2212 scoped_refptr<DelayedCookieMonster> delayed_cm = | 2238 scoped_refptr<DelayedCookieMonster> delayed_cm = |
2213 new DelayedCookieMonster(); | 2239 new DelayedCookieMonster(); |
2214 scoped_refptr<CookieStore> cookie_store = delayed_cm; | 2240 scoped_refptr<CookieStore> cookie_store = delayed_cm; |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2715 req->Start(); | 2741 req->Start(); |
2716 base::RunLoop().Run(); | 2742 base::RunLoop().Run(); |
2717 | 2743 |
2718 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != | 2744 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
2719 std::string::npos); | 2745 std::string::npos); |
2720 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); | 2746 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
2721 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); | 2747 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
2722 } | 2748 } |
2723 } | 2749 } |
2724 | 2750 |
| 2751 // Tests that $Secure- cookies can't be set on non-secure origins. |
| 2752 TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
| 2753 test_server::EmbeddedTestServer test_server; |
| 2754 test_server.RegisterRequestHandler(base::Bind(&HandleSetCookieRequest)); |
| 2755 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
| 2756 SpawnedTestServer test_server_https( |
| 2757 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
| 2758 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2759 ASSERT_TRUE(test_server_https.Start()); |
| 2760 |
| 2761 TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2762 TestURLRequestContext context(true); |
| 2763 context.set_network_delegate(&network_delegate); |
| 2764 context.Init(); |
| 2765 |
| 2766 // Try to set a Secure $Secure- cookie, with experimental features |
| 2767 // enabled. |
| 2768 { |
| 2769 TestDelegate d; |
| 2770 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2771 test_server.GetURL("/set-cookie?$Secure-nonsecure-origin=1;Secure"), |
| 2772 DEFAULT_PRIORITY, &d)); |
| 2773 req->Start(); |
| 2774 base::RunLoop().Run(); |
| 2775 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2776 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2777 } |
| 2778 |
| 2779 // Verify that the cookie is not set. |
| 2780 { |
| 2781 TestDelegate d; |
| 2782 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2783 test_server_https.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2784 req->Start(); |
| 2785 base::RunLoop().Run(); |
| 2786 |
| 2787 EXPECT_TRUE(d.data_received().find("$Secure-nonsecure-origin=1") == |
| 2788 std::string::npos); |
| 2789 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2790 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2791 } |
| 2792 } |
| 2793 |
| 2794 TEST_F(URLRequestTest, SecureCookiePrefixNonexperimental) { |
| 2795 SpawnedTestServer test_server( |
| 2796 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
| 2797 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2798 ASSERT_TRUE(test_server.Start()); |
| 2799 |
| 2800 TestNetworkDelegate network_delegate; |
| 2801 TestURLRequestContext context(true); |
| 2802 context.set_network_delegate(&network_delegate); |
| 2803 context.Init(); |
| 2804 |
| 2805 // Without experimental features, there should be no restrictions on |
| 2806 // $Secure- cookies. |
| 2807 |
| 2808 // Set a non-Secure cookie with the $Secure- prefix. |
| 2809 { |
| 2810 TestDelegate d; |
| 2811 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2812 test_server.GetURL("set-cookie?$Secure-nonsecure-not-experimental=1"), |
| 2813 DEFAULT_PRIORITY, &d)); |
| 2814 req->Start(); |
| 2815 base::RunLoop().Run(); |
| 2816 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2817 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2818 } |
| 2819 |
| 2820 // Set a Secure cookie with the $Secure- prefix. |
| 2821 { |
| 2822 TestDelegate d; |
| 2823 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2824 test_server.GetURL( |
| 2825 "set-cookie?$Secure-secure-not-experimental=1;Secure"), |
| 2826 DEFAULT_PRIORITY, &d)); |
| 2827 req->Start(); |
| 2828 base::RunLoop().Run(); |
| 2829 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2830 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2831 } |
| 2832 |
| 2833 // Verify that the cookies are set. Neither should have any |
| 2834 // restrictions because the experimental flag is off. |
| 2835 { |
| 2836 TestDelegate d; |
| 2837 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2838 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2839 req->Start(); |
| 2840 base::RunLoop().Run(); |
| 2841 |
| 2842 EXPECT_TRUE(d.data_received().find("$Secure-secure-not-experimental=1") != |
| 2843 std::string::npos); |
| 2844 EXPECT_TRUE( |
| 2845 d.data_received().find("$Secure-nonsecure-not-experimental=1") != |
| 2846 std::string::npos); |
| 2847 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2848 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2849 } |
| 2850 } |
| 2851 |
| 2852 TEST_F(URLRequestTest, SecureCookiePrefixExperimentalNonsecure) { |
| 2853 SpawnedTestServer test_server( |
| 2854 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
| 2855 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2856 ASSERT_TRUE(test_server.Start()); |
| 2857 |
| 2858 TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2859 TestURLRequestContext context(true); |
| 2860 context.set_network_delegate(&network_delegate); |
| 2861 context.Init(); |
| 2862 |
| 2863 // Try to set a non-Secure $Secure- cookie, with experimental features |
| 2864 // enabled. |
| 2865 { |
| 2866 TestDelegate d; |
| 2867 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2868 test_server.GetURL("set-cookie?$Secure-foo=1"), DEFAULT_PRIORITY, &d)); |
| 2869 req->Start(); |
| 2870 base::RunLoop().Run(); |
| 2871 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2872 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2873 } |
| 2874 |
| 2875 // Verify that the cookie is not set. |
| 2876 { |
| 2877 TestDelegate d; |
| 2878 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2879 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2880 req->Start(); |
| 2881 base::RunLoop().Run(); |
| 2882 |
| 2883 EXPECT_TRUE(d.data_received().find("$Secure-foo=1") == std::string::npos); |
| 2884 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2885 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2886 } |
| 2887 } |
| 2888 |
| 2889 TEST_F(URLRequestTest, SecureCookiePrefixExperimentalSecure) { |
| 2890 SpawnedTestServer test_server( |
| 2891 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
| 2892 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2893 ASSERT_TRUE(test_server.Start()); |
| 2894 |
| 2895 TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2896 TestURLRequestContext context(true); |
| 2897 context.set_network_delegate(&network_delegate); |
| 2898 context.Init(); |
| 2899 |
| 2900 // Try to set a Secure $Secure- cookie, with experimental features |
| 2901 // enabled. |
| 2902 { |
| 2903 TestDelegate d; |
| 2904 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2905 test_server.GetURL("set-cookie?$Secure-bar=1;Secure"), DEFAULT_PRIORITY, |
| 2906 &d)); |
| 2907 req->Start(); |
| 2908 base::RunLoop().Run(); |
| 2909 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2910 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2911 } |
| 2912 |
| 2913 // Verify that the cookie is set. |
| 2914 { |
| 2915 TestDelegate d; |
| 2916 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2917 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2918 req->Start(); |
| 2919 base::RunLoop().Run(); |
| 2920 |
| 2921 EXPECT_TRUE(d.data_received().find("$Secure-bar=1") != std::string::npos); |
| 2922 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2923 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2924 } |
| 2925 } |
| 2926 |
2725 // Tests that a request is cancelled while entering suspend mode. Uses mocks | 2927 // Tests that a request is cancelled while entering suspend mode. Uses mocks |
2726 // rather than a spawned test server because the connection used to talk to | 2928 // rather than a spawned test server because the connection used to talk to |
2727 // the test server is affected by entering suspend mode on Android. | 2929 // the test server is affected by entering suspend mode on Android. |
2728 TEST_F(URLRequestTest, CancelOnSuspend) { | 2930 TEST_F(URLRequestTest, CancelOnSuspend) { |
2729 TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); | 2931 TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
2730 base::PowerMonitor power_monitor(make_scoped_ptr(power_monitor_source)); | 2932 base::PowerMonitor power_monitor(make_scoped_ptr(power_monitor_source)); |
2731 | 2933 |
2732 URLRequestFailedJob::AddUrlHandler(); | 2934 URLRequestFailedJob::AddUrlHandler(); |
2733 | 2935 |
2734 TestDelegate d; | 2936 TestDelegate d; |
(...skipping 6779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9514 | 9716 |
9515 req->Start(); | 9717 req->Start(); |
9516 req->Cancel(); | 9718 req->Cancel(); |
9517 job->DetachRequest(); | 9719 job->DetachRequest(); |
9518 base::RunLoop().RunUntilIdle(); | 9720 base::RunLoop().RunUntilIdle(); |
9519 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | 9721 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
9520 EXPECT_EQ(0, d.received_redirect_count()); | 9722 EXPECT_EQ(0, d.received_redirect_count()); |
9521 } | 9723 } |
9522 | 9724 |
9523 } // namespace net | 9725 } // namespace net |
OLD | NEW |