| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/client_socket_factory.h" | 8 #include "net/base/client_socket_factory.h" |
| 9 #include "net/base/completion_callback.h" | 9 #include "net/base/completion_callback.h" |
| 10 #include "net/base/ssl_client_socket.h" | 10 #include "net/base/ssl_client_socket.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 connected_ = false; | 114 connected_ = false; |
| 115 callback_ = NULL; | 115 callback_ = NULL; |
| 116 } | 116 } |
| 117 virtual bool IsConnected() const { | 117 virtual bool IsConnected() const { |
| 118 return connected_; | 118 return connected_; |
| 119 } | 119 } |
| 120 virtual bool IsConnectedAndIdle() const { | 120 virtual bool IsConnectedAndIdle() const { |
| 121 return connected_; | 121 return connected_; |
| 122 } | 122 } |
| 123 // Socket methods: | 123 // Socket methods: |
| 124 virtual int Read(char* buf, int buf_len, | 124 virtual int Read(IOBuffer* buf, int buf_len, |
| 125 CompletionCallback* callback) = 0; | 125 CompletionCallback* callback) = 0; |
| 126 virtual int Write(const char* buf, int buf_len, | 126 virtual int Write(IOBuffer* buf, int buf_len, |
| 127 CompletionCallback* callback) = 0; | 127 CompletionCallback* callback) = 0; |
| 128 | 128 |
| 129 #if defined(OS_LINUX) | 129 #if defined(OS_LINUX) |
| 130 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen) { | 130 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen) { |
| 131 memset(reinterpret_cast<char *>(name), 0, *namelen); | 131 memset(reinterpret_cast<char *>(name), 0, *namelen); |
| 132 return OK; | 132 return OK; |
| 133 } | 133 } |
| 134 #endif | 134 #endif |
| 135 | 135 |
| 136 | 136 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 return OK; | 171 return OK; |
| 172 connected_ = true; | 172 connected_ = true; |
| 173 if (data_->connect.async) { | 173 if (data_->connect.async) { |
| 174 RunCallbackAsync(callback, data_->connect.result); | 174 RunCallbackAsync(callback, data_->connect.result); |
| 175 return ERR_IO_PENDING; | 175 return ERR_IO_PENDING; |
| 176 } | 176 } |
| 177 return data_->connect.result; | 177 return data_->connect.result; |
| 178 } | 178 } |
| 179 | 179 |
| 180 // Socket methods: | 180 // Socket methods: |
| 181 virtual int Read(char* buf, int buf_len, CompletionCallback* callback) { | 181 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback) { |
| 182 DCHECK(!callback_); | 182 DCHECK(!callback_); |
| 183 MockRead& r = data_->reads[read_index_]; | 183 MockRead& r = data_->reads[read_index_]; |
| 184 int result = r.result; | 184 int result = r.result; |
| 185 if (r.data) { | 185 if (r.data) { |
| 186 if (r.data_len - read_offset_ > 0) { | 186 if (r.data_len - read_offset_ > 0) { |
| 187 result = std::min(buf_len, r.data_len - read_offset_); | 187 result = std::min(buf_len, r.data_len - read_offset_); |
| 188 memcpy(buf, r.data + read_offset_, result); | 188 memcpy(buf->data(), r.data + read_offset_, result); |
| 189 read_offset_ += result; | 189 read_offset_ += result; |
| 190 if (read_offset_ == r.data_len) { | 190 if (read_offset_ == r.data_len) { |
| 191 read_index_++; | 191 read_index_++; |
| 192 read_offset_ = 0; | 192 read_offset_ = 0; |
| 193 } | 193 } |
| 194 } else { | 194 } else { |
| 195 result = 0; // EOF | 195 result = 0; // EOF |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 if (r.async) { | 198 if (r.async) { |
| 199 RunCallbackAsync(callback, result); | 199 RunCallbackAsync(callback, result); |
| 200 return ERR_IO_PENDING; | 200 return ERR_IO_PENDING; |
| 201 } | 201 } |
| 202 return result; | 202 return result; |
| 203 } | 203 } |
| 204 | 204 |
| 205 virtual int Write(const char* buf, int buf_len, | 205 virtual int Write(IOBuffer* buf, int buf_len, |
| 206 CompletionCallback* callback) { | 206 CompletionCallback* callback) { |
| 207 DCHECK(buf); | 207 DCHECK(buf); |
| 208 DCHECK(buf_len > 0); | 208 DCHECK(buf_len > 0); |
| 209 DCHECK(!callback_); | 209 DCHECK(!callback_); |
| 210 // Not using mock writes; succeed synchronously. | 210 // Not using mock writes; succeed synchronously. |
| 211 if (!data_->writes) | 211 if (!data_->writes) |
| 212 return buf_len; | 212 return buf_len; |
| 213 | 213 |
| 214 // Check that what we are writing matches the expectation. | 214 // Check that what we are writing matches the expectation. |
| 215 // Then give the mocked return value. | 215 // Then give the mocked return value. |
| 216 MockWrite& w = data_->writes[write_index_++]; | 216 MockWrite& w = data_->writes[write_index_++]; |
| 217 int result = w.result; | 217 int result = w.result; |
| 218 if (w.data) { | 218 if (w.data) { |
| 219 std::string expected_data(w.data, w.data_len); | 219 std::string expected_data(w.data, w.data_len); |
| 220 std::string actual_data(buf, buf_len); | 220 std::string actual_data(buf->data(), buf_len); |
| 221 EXPECT_EQ(expected_data, actual_data); | 221 EXPECT_EQ(expected_data, actual_data); |
| 222 if (expected_data != actual_data) | 222 if (expected_data != actual_data) |
| 223 return ERR_UNEXPECTED; | 223 return ERR_UNEXPECTED; |
| 224 if (result == OK) | 224 if (result == OK) |
| 225 result = w.data_len; | 225 result = w.data_len; |
| 226 } | 226 } |
| 227 if (w.async) { | 227 if (w.async) { |
| 228 RunCallbackAsync(callback, result); | 228 RunCallbackAsync(callback, result); |
| 229 return ERR_IO_PENDING; | 229 return ERR_IO_PENDING; |
| 230 } | 230 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 return rv; | 303 return rv; |
| 304 } | 304 } |
| 305 | 305 |
| 306 virtual void Disconnect() { | 306 virtual void Disconnect() { |
| 307 MockClientSocket::Disconnect(); | 307 MockClientSocket::Disconnect(); |
| 308 if (transport_ != NULL) | 308 if (transport_ != NULL) |
| 309 transport_->Disconnect(); | 309 transport_->Disconnect(); |
| 310 } | 310 } |
| 311 | 311 |
| 312 // Socket methods: | 312 // Socket methods: |
| 313 virtual int Read(char* buf, int buf_len, CompletionCallback* callback) { | 313 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback) { |
| 314 DCHECK(!callback_); | 314 DCHECK(!callback_); |
| 315 return transport_->Read(buf, buf_len, callback); | 315 return transport_->Read(buf, buf_len, callback); |
| 316 } | 316 } |
| 317 | 317 |
| 318 virtual int Write(const char* buf, int buf_len, | 318 virtual int Write(IOBuffer* buf, int buf_len, |
| 319 CompletionCallback* callback) { | 319 CompletionCallback* callback) { |
| 320 DCHECK(!callback_); | 320 DCHECK(!callback_); |
| 321 return transport_->Write(buf, buf_len, callback); | 321 return transport_->Write(buf, buf_len, callback); |
| 322 } | 322 } |
| 323 | 323 |
| 324 private: | 324 private: |
| 325 scoped_ptr<ClientSocket> transport_; | 325 scoped_ptr<ClientSocket> transport_; |
| 326 MockSSLSocket* data_; | 326 MockSSLSocket* data_; |
| 327 }; | 327 }; |
| 328 | 328 |
| (...skipping 2579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2908 } | 2908 } |
| 2909 | 2909 |
| 2910 // Test the ResetStateForRestart() private method. | 2910 // Test the ResetStateForRestart() private method. |
| 2911 TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { | 2911 TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 2912 // Create a transaction (the dependencies aren't important). | 2912 // Create a transaction (the dependencies aren't important). |
| 2913 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService()); | 2913 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService()); |
| 2914 scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction( | 2914 scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction( |
| 2915 CreateSession(proxy_service.get()), &mock_socket_factory)); | 2915 CreateSession(proxy_service.get()), &mock_socket_factory)); |
| 2916 | 2916 |
| 2917 // Setup some state (which we expect ResetStateForRestart() will clear). | 2917 // Setup some state (which we expect ResetStateForRestart() will clear). |
| 2918 trans->header_buf_.reset(static_cast<char*>(malloc(10))); | 2918 trans->header_buf_->Realloc(10); |
| 2919 trans->header_buf_capacity_ = 10; | 2919 trans->header_buf_capacity_ = 10; |
| 2920 trans->header_buf_len_ = 3; | 2920 trans->header_buf_len_ = 3; |
| 2921 trans->header_buf_body_offset_ = 11; | 2921 trans->header_buf_body_offset_ = 11; |
| 2922 trans->header_buf_http_offset_ = 0; | 2922 trans->header_buf_http_offset_ = 0; |
| 2923 trans->response_body_length_ = 100; | 2923 trans->response_body_length_ = 100; |
| 2924 trans->response_body_read_ = 1; | 2924 trans->response_body_read_ = 1; |
| 2925 trans->read_buf_ = new IOBuffer(15); | 2925 trans->read_buf_ = new IOBuffer(15); |
| 2926 trans->read_buf_len_ = 15; | 2926 trans->read_buf_len_ = 15; |
| 2927 trans->request_headers_ = "Authorization: NTLM"; | 2927 trans->request_headers_->headers_ = "Authorization: NTLM"; |
| 2928 trans->request_headers_bytes_sent_ = 3; | 2928 trans->request_headers_bytes_sent_ = 3; |
| 2929 | 2929 |
| 2930 // Setup state in response_ | 2930 // Setup state in response_ |
| 2931 trans->response_.auth_challenge = new AuthChallengeInfo(); | 2931 trans->response_.auth_challenge = new AuthChallengeInfo(); |
| 2932 trans->response_.ssl_info.cert_status = -15; | 2932 trans->response_.ssl_info.cert_status = -15; |
| 2933 trans->response_.response_time = base::Time::Now(); | 2933 trans->response_.response_time = base::Time::Now(); |
| 2934 trans->response_.was_cached = true; // (Wouldn't ever actually be true...) | 2934 trans->response_.was_cached = true; // (Wouldn't ever actually be true...) |
| 2935 | 2935 |
| 2936 { // Setup state for response_.vary_data | 2936 { // Setup state for response_.vary_data |
| 2937 HttpRequestInfo request; | 2937 HttpRequestInfo request; |
| 2938 std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); | 2938 std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 2939 std::replace(temp.begin(), temp.end(), '\n', '\0'); | 2939 std::replace(temp.begin(), temp.end(), '\n', '\0'); |
| 2940 scoped_refptr<HttpResponseHeaders> response = new HttpResponseHeaders(temp); | 2940 scoped_refptr<HttpResponseHeaders> response = new HttpResponseHeaders(temp); |
| 2941 request.extra_headers = "Foo: 1\nbar: 23"; | 2941 request.extra_headers = "Foo: 1\nbar: 23"; |
| 2942 EXPECT_TRUE(trans->response_.vary_data.Init(request, *response)); | 2942 EXPECT_TRUE(trans->response_.vary_data.Init(request, *response)); |
| 2943 } | 2943 } |
| 2944 | 2944 |
| 2945 // Cause the above state to be reset. | 2945 // Cause the above state to be reset. |
| 2946 trans->ResetStateForRestart(); | 2946 trans->ResetStateForRestart(); |
| 2947 | 2947 |
| 2948 // Verify that the state that needed to be reset, has been reset. | 2948 // Verify that the state that needed to be reset, has been reset. |
| 2949 EXPECT_EQ(NULL, trans->header_buf_.get()); | 2949 EXPECT_EQ(NULL, trans->header_buf_->headers()); |
| 2950 EXPECT_EQ(0, trans->header_buf_capacity_); | 2950 EXPECT_EQ(0, trans->header_buf_capacity_); |
| 2951 EXPECT_EQ(0, trans->header_buf_len_); | 2951 EXPECT_EQ(0, trans->header_buf_len_); |
| 2952 EXPECT_EQ(-1, trans->header_buf_body_offset_); | 2952 EXPECT_EQ(-1, trans->header_buf_body_offset_); |
| 2953 EXPECT_EQ(-1, trans->header_buf_http_offset_); | 2953 EXPECT_EQ(-1, trans->header_buf_http_offset_); |
| 2954 EXPECT_EQ(-1, trans->response_body_length_); | 2954 EXPECT_EQ(-1, trans->response_body_length_); |
| 2955 EXPECT_EQ(0, trans->response_body_read_); | 2955 EXPECT_EQ(0, trans->response_body_read_); |
| 2956 EXPECT_EQ(NULL, trans->read_buf_.get()); | 2956 EXPECT_EQ(NULL, trans->read_buf_.get()); |
| 2957 EXPECT_EQ(0, trans->read_buf_len_); | 2957 EXPECT_EQ(0, trans->read_buf_len_); |
| 2958 EXPECT_EQ("", trans->request_headers_); | 2958 EXPECT_EQ("", trans->request_headers_->headers_); |
| 2959 EXPECT_EQ(0U, trans->request_headers_bytes_sent_); | 2959 EXPECT_EQ(0U, trans->request_headers_bytes_sent_); |
| 2960 EXPECT_EQ(NULL, trans->response_.auth_challenge.get()); | 2960 EXPECT_EQ(NULL, trans->response_.auth_challenge.get()); |
| 2961 EXPECT_EQ(NULL, trans->response_.headers.get()); | 2961 EXPECT_EQ(NULL, trans->response_.headers.get()); |
| 2962 EXPECT_EQ(false, trans->response_.was_cached); | 2962 EXPECT_EQ(false, trans->response_.was_cached); |
| 2963 EXPECT_EQ(base::kInvalidPlatformFileValue, | 2963 EXPECT_EQ(base::kInvalidPlatformFileValue, |
| 2964 trans->response_.response_data_file); | 2964 trans->response_.response_data_file); |
| 2965 EXPECT_EQ(0, trans->response_.ssl_info.cert_status); | 2965 EXPECT_EQ(0, trans->response_.ssl_info.cert_status); |
| 2966 EXPECT_FALSE(trans->response_.vary_data.is_valid()); | 2966 EXPECT_FALSE(trans->response_.vary_data.is_valid()); |
| 2967 } | 2967 } |
| 2968 | 2968 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3095 EXPECT_EQ(OK, rv); | 3095 EXPECT_EQ(OK, rv); |
| 3096 | 3096 |
| 3097 const HttpResponseInfo* response = trans->GetResponseInfo(); | 3097 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3098 | 3098 |
| 3099 EXPECT_FALSE(response == NULL); | 3099 EXPECT_FALSE(response == NULL); |
| 3100 EXPECT_EQ(100, response->headers->GetContentLength()); | 3100 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3101 } | 3101 } |
| 3102 } | 3102 } |
| 3103 | 3103 |
| 3104 } // namespace net | 3104 } // namespace net |
| OLD | NEW |