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 "net/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
6 | 6 |
7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 3706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3717 | 3717 |
3718 std::string response_data; | 3718 std::string response_data; |
3719 rv = ReadTransaction(trans.get(), &response_data); | 3719 rv = ReadTransaction(trans.get(), &response_data); |
3720 EXPECT_EQ(OK, rv); | 3720 EXPECT_EQ(OK, rv); |
3721 EXPECT_EQ(kExpectedResponseData[i], response_data); | 3721 EXPECT_EQ(kExpectedResponseData[i], response_data); |
3722 } | 3722 } |
3723 } | 3723 } |
3724 | 3724 |
3725 // Test the request-challenge-retry sequence for basic auth when there is | 3725 // Test the request-challenge-retry sequence for basic auth when there is |
3726 // an identity in the URL. The request should be sent as normal, but when | 3726 // an identity in the URL. The request should be sent as normal, but when |
3727 // it fails the identity from the URL is no longer used. | 3727 // it fails the identity from the URL is used to answer the challenge. |
3728 TEST_F(HttpNetworkTransactionSpdy2Test, IgnoreAuthIdentityInURL) { | 3728 TEST_F(HttpNetworkTransactionSpdy2Test, AuthIdentityInURL) { |
3729 HttpRequestInfo request; | 3729 HttpRequestInfo request; |
3730 request.method = "GET"; | 3730 request.method = "GET"; |
3731 request.url = GURL("http://foo:b@r@www.google.com/"); | 3731 request.url = GURL("http://foo:b@r@www.google.com/"); |
3732 request.load_flags = LOAD_NORMAL; | 3732 request.load_flags = LOAD_NORMAL; |
3733 | 3733 |
3734 SessionDependencies session_deps; | 3734 SessionDependencies session_deps; |
3735 scoped_ptr<HttpTransaction> trans( | 3735 scoped_ptr<HttpTransaction> trans( |
3736 new HttpNetworkTransaction(CreateSession(&session_deps))); | 3736 new HttpNetworkTransaction(CreateSession(&session_deps))); |
3737 | 3737 |
3738 // The password contains an escaped character -- for this test to pass it | 3738 // The password contains an escaped character -- for this test to pass it |
3739 // will need to be unescaped by HttpNetworkTransaction. | 3739 // will need to be unescaped by HttpNetworkTransaction. |
3740 EXPECT_EQ("b%40r", request.url.password()); | 3740 EXPECT_EQ("b%40r", request.url.password()); |
3741 | 3741 |
3742 MockWrite data_writes1[] = { | 3742 MockWrite data_writes1[] = { |
3743 MockWrite("GET / HTTP/1.1\r\n" | 3743 MockWrite("GET / HTTP/1.1\r\n" |
3744 "Host: www.google.com\r\n" | 3744 "Host: www.google.com\r\n" |
3745 "Connection: keep-alive\r\n\r\n"), | 3745 "Connection: keep-alive\r\n\r\n"), |
3746 }; | 3746 }; |
3747 | 3747 |
3748 MockRead data_reads1[] = { | 3748 MockRead data_reads1[] = { |
3749 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | 3749 MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
3750 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | 3750 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
3751 MockRead("Content-Length: 10\r\n\r\n"), | 3751 MockRead("Content-Length: 10\r\n\r\n"), |
3752 MockRead(SYNCHRONOUS, ERR_FAILED), | 3752 MockRead(SYNCHRONOUS, ERR_FAILED), |
3753 }; | 3753 }; |
3754 | 3754 |
| 3755 // After the challenge above, the transaction will be restarted using the |
| 3756 // identity from the url (foo, b@r) to answer the challenge. |
| 3757 MockWrite data_writes2[] = { |
| 3758 MockWrite("GET / HTTP/1.1\r\n" |
| 3759 "Host: www.google.com\r\n" |
| 3760 "Connection: keep-alive\r\n" |
| 3761 "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
| 3762 }; |
| 3763 |
| 3764 MockRead data_reads2[] = { |
| 3765 MockRead("HTTP/1.0 200 OK\r\n"), |
| 3766 MockRead("Content-Length: 100\r\n\r\n"), |
| 3767 MockRead(SYNCHRONOUS, OK), |
| 3768 }; |
| 3769 |
3755 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | 3770 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
3756 data_writes1, arraysize(data_writes1)); | 3771 data_writes1, arraysize(data_writes1)); |
| 3772 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3773 data_writes2, arraysize(data_writes2)); |
3757 session_deps.socket_factory.AddSocketDataProvider(&data1); | 3774 session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3775 session_deps.socket_factory.AddSocketDataProvider(&data2); |
3758 | 3776 |
3759 TestCompletionCallback callback1; | 3777 TestCompletionCallback callback1; |
3760 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | 3778 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
3761 EXPECT_EQ(ERR_IO_PENDING, rv); | 3779 EXPECT_EQ(ERR_IO_PENDING, rv); |
3762 rv = callback1.WaitForResult(); | 3780 rv = callback1.WaitForResult(); |
3763 EXPECT_EQ(OK, rv); | 3781 EXPECT_EQ(OK, rv); |
| 3782 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3783 |
| 3784 TestCompletionCallback callback2; |
| 3785 rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); |
| 3786 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3787 rv = callback2.WaitForResult(); |
| 3788 EXPECT_EQ(OK, rv); |
3764 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | 3789 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
3765 | 3790 |
| 3791 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3792 ASSERT_TRUE(response != NULL); |
| 3793 |
| 3794 // There is no challenge info, since the identity in URL worked. |
| 3795 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3796 |
| 3797 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3798 |
| 3799 // Empty the current queue. |
| 3800 MessageLoop::current()->RunAllPending(); |
| 3801 } |
| 3802 |
| 3803 // Test the request-challenge-retry sequence for basic auth when there is an |
| 3804 // incorrect identity in the URL. The identity from the URL should be used only |
| 3805 // once. |
| 3806 TEST_F(HttpNetworkTransactionSpdy2Test, WrongAuthIdentityInURL) { |
| 3807 HttpRequestInfo request; |
| 3808 request.method = "GET"; |
| 3809 // Note: the URL has a username:password in it. The password "baz" is |
| 3810 // wrong (should be "bar"). |
| 3811 request.url = GURL("http://foo:baz@www.google.com/"); |
| 3812 |
| 3813 request.load_flags = LOAD_NORMAL; |
| 3814 |
| 3815 SessionDependencies session_deps; |
| 3816 scoped_ptr<HttpTransaction> trans( |
| 3817 new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3818 |
| 3819 MockWrite data_writes1[] = { |
| 3820 MockWrite("GET / HTTP/1.1\r\n" |
| 3821 "Host: www.google.com\r\n" |
| 3822 "Connection: keep-alive\r\n\r\n"), |
| 3823 }; |
| 3824 |
| 3825 MockRead data_reads1[] = { |
| 3826 MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3827 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3828 MockRead("Content-Length: 10\r\n\r\n"), |
| 3829 MockRead(SYNCHRONOUS, ERR_FAILED), |
| 3830 }; |
| 3831 |
| 3832 // After the challenge above, the transaction will be restarted using the |
| 3833 // identity from the url (foo, baz) to answer the challenge. |
| 3834 MockWrite data_writes2[] = { |
| 3835 MockWrite("GET / HTTP/1.1\r\n" |
| 3836 "Host: www.google.com\r\n" |
| 3837 "Connection: keep-alive\r\n" |
| 3838 "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3839 }; |
| 3840 |
| 3841 MockRead data_reads2[] = { |
| 3842 MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3843 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3844 MockRead("Content-Length: 10\r\n\r\n"), |
| 3845 MockRead(SYNCHRONOUS, ERR_FAILED), |
| 3846 }; |
| 3847 |
| 3848 // After the challenge above, the transaction will be restarted using the |
| 3849 // identity supplied by the user (foo, bar) to answer the challenge. |
| 3850 MockWrite data_writes3[] = { |
| 3851 MockWrite("GET / HTTP/1.1\r\n" |
| 3852 "Host: www.google.com\r\n" |
| 3853 "Connection: keep-alive\r\n" |
| 3854 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3855 }; |
| 3856 |
| 3857 MockRead data_reads3[] = { |
| 3858 MockRead("HTTP/1.0 200 OK\r\n"), |
| 3859 MockRead("Content-Length: 100\r\n\r\n"), |
| 3860 MockRead(SYNCHRONOUS, OK), |
| 3861 }; |
| 3862 |
| 3863 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3864 data_writes1, arraysize(data_writes1)); |
| 3865 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3866 data_writes2, arraysize(data_writes2)); |
| 3867 StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3868 data_writes3, arraysize(data_writes3)); |
| 3869 session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3870 session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3871 session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 3872 |
| 3873 TestCompletionCallback callback1; |
| 3874 |
| 3875 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
| 3876 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3877 |
| 3878 rv = callback1.WaitForResult(); |
| 3879 EXPECT_EQ(OK, rv); |
| 3880 |
| 3881 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3882 TestCompletionCallback callback2; |
| 3883 rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); |
| 3884 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3885 rv = callback2.WaitForResult(); |
| 3886 EXPECT_EQ(OK, rv); |
| 3887 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3888 |
| 3889 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3890 ASSERT_TRUE(response != NULL); |
| 3891 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
| 3892 |
| 3893 TestCompletionCallback callback3; |
| 3894 rv = trans->RestartWithAuth( |
| 3895 AuthCredentials(kFoo, kBar), callback3.callback()); |
| 3896 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3897 rv = callback3.WaitForResult(); |
| 3898 EXPECT_EQ(OK, rv); |
| 3899 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3900 |
| 3901 response = trans->GetResponseInfo(); |
| 3902 ASSERT_TRUE(response != NULL); |
| 3903 |
| 3904 // There is no challenge info, since the identity worked. |
| 3905 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3906 |
| 3907 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3908 |
3766 // Empty the current queue. | 3909 // Empty the current queue. |
3767 MessageLoop::current()->RunAllPending(); | 3910 MessageLoop::current()->RunAllPending(); |
3768 } | 3911 } |
3769 | 3912 |
3770 // Test that previously tried username/passwords for a realm get re-used. | 3913 // Test that previously tried username/passwords for a realm get re-used. |
3771 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthCacheAndPreauth) { | 3914 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthCacheAndPreauth) { |
3772 SessionDependencies session_deps; | 3915 SessionDependencies session_deps; |
3773 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | 3916 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
3774 | 3917 |
3775 // Transaction 1: authenticate (foo, bar) on MyRealm1 | 3918 // Transaction 1: authenticate (foo, bar) on MyRealm1 |
(...skipping 5594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9370 StaticSocketDataProvider* data[] = { &data1, &data2 }; | 9513 StaticSocketDataProvider* data[] = { &data1, &data2 }; |
9371 | 9514 |
9372 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | 9515 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
9373 | 9516 |
9374 EXPECT_EQ(OK, out.rv); | 9517 EXPECT_EQ(OK, out.rv); |
9375 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | 9518 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
9376 EXPECT_EQ("hello world", out.response_data); | 9519 EXPECT_EQ("hello world", out.response_data); |
9377 } | 9520 } |
9378 | 9521 |
9379 } // namespace net | 9522 } // namespace net |
OLD | NEW |