| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <math.h> // ceil | 5 #include <math.h> // ceil |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "net/base/completion_callback.h" | 8 #include "net/base/completion_callback.h" |
| 9 #include "net/base/mock_host_resolver.h" | 9 #include "net/base/mock_host_resolver.h" |
| 10 #include "net/base/ssl_config_service_defaults.h" | 10 #include "net/base/ssl_config_service_defaults.h" |
| (...skipping 859 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 "Connection: keep-alive\r\n\r\n"), | 870 "Connection: keep-alive\r\n\r\n"), |
| 871 | 871 |
| 872 // After calling trans->RestartWithAuth(), this is the request we should | 872 // After calling trans->RestartWithAuth(), this is the request we should |
| 873 // be issuing -- the final header line contains the credentials. | 873 // be issuing -- the final header line contains the credentials. |
| 874 MockWrite("GET / HTTP/1.1\r\n" | 874 MockWrite("GET / HTTP/1.1\r\n" |
| 875 "Host: www.google.com\r\n" | 875 "Host: www.google.com\r\n" |
| 876 "Connection: keep-alive\r\n" | 876 "Connection: keep-alive\r\n" |
| 877 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | 877 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 878 }; | 878 }; |
| 879 | 879 |
| 880 // Respond with 5 kb of response body. | |
| 881 std::string large_body_string("Unauthorized"); | |
| 882 large_body_string.append(5 * 1024, ' '); | |
| 883 large_body_string.append("\r\n"); | |
| 884 | |
| 885 MockRead data_reads1[] = { | 880 MockRead data_reads1[] = { |
| 886 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | 881 MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 887 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | 882 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 888 MockRead("Content-Length: 0\r\n\r\n"), | 883 MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
| 889 | 884 |
| 890 // Lastly, the server responds with the actual content. | 885 // Lastly, the server responds with the actual content. |
| 891 MockRead("HTTP/1.1 200 OK\r\n"), | 886 MockRead("HTTP/1.1 200 OK\r\n"), |
| 892 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | 887 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 893 MockRead("Content-Length: 100\r\n\r\n"), | 888 MockRead("Content-Length: 100\r\n\r\n"), |
| 894 MockRead(false, OK), | 889 MockRead(false, OK), |
| 895 }; | 890 }; |
| 896 | 891 |
| 897 StaticSocketDataProvider data1(data_reads1, data_writes1); | 892 StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 898 session_deps.socket_factory.AddSocketDataProvider(&data1); | 893 session_deps.socket_factory.AddSocketDataProvider(&data1); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1003 rv = callback2.WaitForResult(); | 998 rv = callback2.WaitForResult(); |
| 1004 EXPECT_EQ(OK, rv); | 999 EXPECT_EQ(OK, rv); |
| 1005 | 1000 |
| 1006 response = trans->GetResponseInfo(); | 1001 response = trans->GetResponseInfo(); |
| 1007 EXPECT_FALSE(response == NULL); | 1002 EXPECT_FALSE(response == NULL); |
| 1008 EXPECT_TRUE(response->auth_challenge.get() == NULL); | 1003 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1009 EXPECT_EQ(100, response->headers->GetContentLength()); | 1004 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1010 } | 1005 } |
| 1011 | 1006 |
| 1012 // Test the request-challenge-retry sequence for basic auth, over a keep-alive | 1007 // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1008 // connection, but the server gets impatient and closes the connection. |
| 1009 TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
| 1010 SessionDependencies session_deps; |
| 1011 scoped_ptr<HttpTransaction> trans( |
| 1012 new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1013 |
| 1014 HttpRequestInfo request; |
| 1015 request.method = "GET"; |
| 1016 request.url = GURL("http://www.google.com/"); |
| 1017 request.load_flags = 0; |
| 1018 |
| 1019 MockWrite data_writes1[] = { |
| 1020 MockWrite("GET / HTTP/1.1\r\n" |
| 1021 "Host: www.google.com\r\n" |
| 1022 "Connection: keep-alive\r\n\r\n"), |
| 1023 // This simulates the seemingly successful write to a closed connection |
| 1024 // if the bug is not fixed. |
| 1025 MockWrite("GET / HTTP/1.1\r\n" |
| 1026 "Host: www.google.com\r\n" |
| 1027 "Connection: keep-alive\r\n" |
| 1028 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1029 }; |
| 1030 |
| 1031 MockRead data_reads1[] = { |
| 1032 MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1033 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1034 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1035 MockRead("Content-Length: 14\r\n\r\n"), |
| 1036 // Tell MockTCPClientSocket to simulate the server closing the connection. |
| 1037 MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 1038 MockRead("Unauthorized\r\n"), |
| 1039 MockRead(false, OK), // The server closes the connection. |
| 1040 }; |
| 1041 |
| 1042 // After calling trans->RestartWithAuth(), this is the request we should |
| 1043 // be issuing -- the final header line contains the credentials. |
| 1044 MockWrite data_writes2[] = { |
| 1045 MockWrite("GET / HTTP/1.1\r\n" |
| 1046 "Host: www.google.com\r\n" |
| 1047 "Connection: keep-alive\r\n" |
| 1048 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1049 }; |
| 1050 |
| 1051 // Lastly, the server responds with the actual content. |
| 1052 MockRead data_reads2[] = { |
| 1053 MockRead("HTTP/1.1 200 OK\r\n"), |
| 1054 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1055 MockRead("Content-Length: 100\r\n\r\n"), |
| 1056 MockRead(false, OK), |
| 1057 }; |
| 1058 |
| 1059 StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 1060 StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 1061 session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1062 session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1063 |
| 1064 TestCompletionCallback callback1; |
| 1065 |
| 1066 int rv = trans->Start(&request, &callback1, NULL); |
| 1067 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1068 |
| 1069 rv = callback1.WaitForResult(); |
| 1070 EXPECT_EQ(OK, rv); |
| 1071 |
| 1072 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1073 EXPECT_FALSE(response == NULL); |
| 1074 |
| 1075 // The password prompt info should have been set in response->auth_challenge. |
| 1076 EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1077 |
| 1078 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 1079 EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1080 EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1081 |
| 1082 TestCompletionCallback callback2; |
| 1083 |
| 1084 rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
| 1085 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1086 |
| 1087 rv = callback2.WaitForResult(); |
| 1088 EXPECT_EQ(OK, rv); |
| 1089 |
| 1090 response = trans->GetResponseInfo(); |
| 1091 ASSERT_FALSE(response == NULL); |
| 1092 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1093 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1094 } |
| 1095 |
| 1096 // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1013 // proxy connection, when setting up an SSL tunnel. | 1097 // proxy connection, when setting up an SSL tunnel. |
| 1014 TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { | 1098 TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
| 1015 // Configure against proxy server "myproxy:70". | 1099 // Configure against proxy server "myproxy:70". |
| 1016 SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); | 1100 SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 1017 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | 1101 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1018 | 1102 |
| 1019 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); | 1103 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1020 | 1104 |
| 1021 HttpRequestInfo request; | 1105 HttpRequestInfo request; |
| 1022 request.method = "GET"; | 1106 request.method = "GET"; |
| (...skipping 2763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3786 new HttpNetworkTransaction(CreateSession(&session_deps))); | 3870 new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3787 | 3871 |
| 3788 int rv = trans->Start(&request, &callback, NULL); | 3872 int rv = trans->Start(&request, &callback, NULL); |
| 3789 EXPECT_EQ(ERR_IO_PENDING, rv); | 3873 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3790 | 3874 |
| 3791 rv = callback.WaitForResult(); | 3875 rv = callback.WaitForResult(); |
| 3792 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | 3876 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
| 3793 } | 3877 } |
| 3794 | 3878 |
| 3795 } // namespace net | 3879 } // namespace net |
| OLD | NEW |