Chromium Code Reviews| 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 #ifndef NET_SOCKET_SOCKET_TEST_UTIL_H_ | 5 #ifndef NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| 6 #define NET_SOCKET_SOCKET_TEST_UTIL_H_ | 6 #define NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <cstring> | 9 #include <cstring> |
| 10 #include <deque> | 10 #include <deque> |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 std::string server_protos; | 266 std::string server_protos; |
| 267 bool was_npn_negotiated; | 267 bool was_npn_negotiated; |
| 268 SSLClientSocket::NextProto protocol_negotiated; | 268 SSLClientSocket::NextProto protocol_negotiated; |
| 269 bool client_cert_sent; | 269 bool client_cert_sent; |
| 270 SSLCertRequestInfo* cert_request_info; | 270 SSLCertRequestInfo* cert_request_info; |
| 271 scoped_refptr<X509Certificate> cert; | 271 scoped_refptr<X509Certificate> cert; |
| 272 }; | 272 }; |
| 273 | 273 |
| 274 // A DataProvider where the client must write a request before the reads (e.g. | 274 // A DataProvider where the client must write a request before the reads (e.g. |
| 275 // the response) will complete. | 275 // the response) will complete. |
| 276 class DelayedSocketData : public StaticSocketDataProvider, | 276 class DelayedSocketData : public StaticSocketDataProvider { |
| 277 public base::RefCounted<DelayedSocketData> { | |
| 278 public: | 277 public: |
| 279 // |write_delay| the number of MockWrites to complete before allowing | 278 // |write_delay| the number of MockWrites to complete before allowing |
| 280 // a MockRead to complete. | 279 // a MockRead to complete. |
| 281 // |reads| the list of MockRead completions. | 280 // |reads| the list of MockRead completions. |
| 282 // |writes| the list of MockWrite completions. | 281 // |writes| the list of MockWrite completions. |
| 283 // Note: All MockReads and MockWrites must be async. | 282 // Note: All MockReads and MockWrites must be async. |
| 284 // Note: The MockRead and MockWrite lists musts end with a EOF | 283 // Note: For stream sockets, the MockRead list must end with a EOF, e.g., a |
| 285 // e.g. a MockRead(true, 0, 0); | 284 // MockRead(true, 0, 0); |
| 286 DelayedSocketData(int write_delay, | 285 DelayedSocketData(int write_delay, |
| 287 MockRead* reads, size_t reads_count, | 286 MockRead* reads, size_t reads_count, |
| 288 MockWrite* writes, size_t writes_count); | 287 MockWrite* writes, size_t writes_count); |
| 289 | 288 |
| 290 // |connect| the result for the connect phase. | 289 // |connect| the result for the connect phase. |
| 291 // |reads| the list of MockRead completions. | 290 // |reads| the list of MockRead completions. |
| 292 // |write_delay| the number of MockWrites to complete before allowing | 291 // |write_delay| the number of MockWrites to complete before allowing |
| 293 // a MockRead to complete. | 292 // a MockRead to complete. |
| 294 // |writes| the list of MockWrite completions. | 293 // |writes| the list of MockWrite completions. |
| 295 // Note: All MockReads and MockWrites must be async. | 294 // Note: All MockReads and MockWrites must be async. |
| 296 // Note: The MockRead and MockWrite lists musts end with a EOF | 295 // Note: For stream sockets, the MockRead list must end with a EOF, e.g., a |
| 297 // e.g. a MockRead(true, 0, 0); | 296 // MockRead(true, 0, 0); |
| 298 DelayedSocketData(const MockConnect& connect, int write_delay, | 297 DelayedSocketData(const MockConnect& connect, int write_delay, |
| 299 MockRead* reads, size_t reads_count, | 298 MockRead* reads, size_t reads_count, |
| 300 MockWrite* writes, size_t writes_count); | 299 MockWrite* writes, size_t writes_count); |
| 301 virtual ~DelayedSocketData(); | 300 virtual ~DelayedSocketData(); |
| 302 | 301 |
| 303 void ForceNextRead(); | 302 void ForceNextRead(); |
| 304 | 303 |
| 305 // StaticSocketDataProvider: | 304 // StaticSocketDataProvider: |
| 306 virtual MockRead GetNextRead() OVERRIDE; | 305 virtual MockRead GetNextRead() OVERRIDE; |
| 307 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; | 306 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; |
| 308 virtual void Reset() OVERRIDE; | 307 virtual void Reset() OVERRIDE; |
| 309 virtual void CompleteRead() OVERRIDE; | 308 virtual void CompleteRead() OVERRIDE; |
| 310 | 309 |
| 311 private: | 310 private: |
| 312 int write_delay_; | 311 int write_delay_; |
| 312 bool blocked_; | |
|
eroman
2012/01/21 01:18:30
something like "read_pending_" or "read_in_progres
| |
| 313 base::WeakPtrFactory<DelayedSocketData> weak_factory_; | 313 base::WeakPtrFactory<DelayedSocketData> weak_factory_; |
| 314 }; | 314 }; |
| 315 | 315 |
| 316 // A DataProvider where the reads are ordered. | 316 // A DataProvider where the reads are ordered. |
| 317 // If a read is requested before its sequence number is reached, we return an | 317 // If a read is requested before its sequence number is reached, we return an |
| 318 // ERR_IO_PENDING (that way we don't have to explicitly add a MockRead just to | 318 // ERR_IO_PENDING (that way we don't have to explicitly add a MockRead just to |
| 319 // wait). | 319 // wait). |
| 320 // The sequence number is incremented on every read and write operation. | 320 // The sequence number is incremented on every read and write operation. |
| 321 // The message loop may be interrupted by setting the high bit of the sequence | 321 // The message loop may be interrupted by setting the high bit of the sequence |
| 322 // number in the MockRead's sequence number. When that MockRead is reached, | 322 // number in the MockRead's sequence number. When that MockRead is reached, |
| 323 // we post a Quit message to the loop. This allows us to interrupt the reading | 323 // we post a Quit message to the loop. This allows us to interrupt the reading |
| 324 // of data before a complete message has arrived, and provides support for | 324 // of data before a complete message has arrived, and provides support for |
| 325 // testing server push when the request is issued while the response is in the | 325 // testing server push when the request is issued while the response is in the |
| 326 // middle of being received. | 326 // middle of being received. |
| 327 class OrderedSocketData : public StaticSocketDataProvider, | 327 class OrderedSocketData : public StaticSocketDataProvider { |
| 328 public base::RefCounted<OrderedSocketData> { | |
| 329 public: | 328 public: |
| 330 // |reads| the list of MockRead completions. | 329 // |reads| the list of MockRead completions. |
| 331 // |writes| the list of MockWrite completions. | 330 // |writes| the list of MockWrite completions. |
| 332 // Note: All MockReads and MockWrites must be async. | 331 // Note: All MockReads and MockWrites must be async. |
| 333 // Note: The MockRead and MockWrite lists musts end with a EOF | 332 // Note: For stream sockets, the MockRead list must end with a EOF, e.g., a |
| 334 // e.g. a MockRead(true, 0, 0); | 333 // MockRead(true, 0, 0); |
| 335 OrderedSocketData(MockRead* reads, size_t reads_count, | 334 OrderedSocketData(MockRead* reads, size_t reads_count, |
| 336 MockWrite* writes, size_t writes_count); | 335 MockWrite* writes, size_t writes_count); |
| 336 virtual ~OrderedSocketData(); | |
| 337 | 337 |
| 338 // |connect| the result for the connect phase. | 338 // |connect| the result for the connect phase. |
| 339 // |reads| the list of MockRead completions. | 339 // |reads| the list of MockRead completions. |
| 340 // |writes| the list of MockWrite completions. | 340 // |writes| the list of MockWrite completions. |
| 341 // Note: All MockReads and MockWrites must be async. | 341 // Note: All MockReads and MockWrites must be async. |
| 342 // Note: The MockRead and MockWrite lists musts end with a EOF | 342 // Note: For stream sockets, the MockRead list must end with a EOF, e.g., a |
| 343 // e.g. a MockRead(true, 0, 0); | 343 // MockRead(true, 0, 0); |
| 344 OrderedSocketData(const MockConnect& connect, | 344 OrderedSocketData(const MockConnect& connect, |
| 345 MockRead* reads, size_t reads_count, | 345 MockRead* reads, size_t reads_count, |
| 346 MockWrite* writes, size_t writes_count); | 346 MockWrite* writes, size_t writes_count); |
| 347 | 347 |
| 348 // Posts a quit message to the current message loop, if one is running. | 348 // Posts a quit message to the current message loop, if one is running. |
| 349 void EndLoop(); | 349 void EndLoop(); |
| 350 | 350 |
| 351 // StaticSocketDataProvider: | 351 // StaticSocketDataProvider: |
| 352 virtual MockRead GetNextRead() OVERRIDE; | 352 virtual MockRead GetNextRead() OVERRIDE; |
| 353 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; | 353 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; |
| 354 virtual void Reset() OVERRIDE; | 354 virtual void Reset() OVERRIDE; |
| 355 virtual void CompleteRead() OVERRIDE; | 355 virtual void CompleteRead() OVERRIDE; |
| 356 | 356 |
| 357 private: | 357 private: |
| 358 friend class base::RefCounted<OrderedSocketData>; | |
| 359 virtual ~OrderedSocketData(); | |
| 360 | |
| 361 int sequence_number_; | 358 int sequence_number_; |
| 362 int loop_stop_stage_; | 359 int loop_stop_stage_; |
| 363 bool blocked_; | 360 bool blocked_; |
| 364 base::WeakPtrFactory<OrderedSocketData> weak_factory_; | 361 base::WeakPtrFactory<OrderedSocketData> weak_factory_; |
| 365 }; | 362 }; |
| 366 | 363 |
| 367 class DeterministicMockTCPClientSocket; | 364 class DeterministicMockTCPClientSocket; |
| 368 | 365 |
| 369 // This class gives the user full control over the network activity, | 366 // This class gives the user full control over the network activity, |
| 370 // specifically the timing of the COMPLETION of I/O operations. Regardless of | 367 // specifically the timing of the COMPLETION of I/O operations. Regardless of |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 // socket types. | 520 // socket types. |
| 524 class MockClientSocketFactory : public ClientSocketFactory { | 521 class MockClientSocketFactory : public ClientSocketFactory { |
| 525 public: | 522 public: |
| 526 MockClientSocketFactory(); | 523 MockClientSocketFactory(); |
| 527 virtual ~MockClientSocketFactory(); | 524 virtual ~MockClientSocketFactory(); |
| 528 | 525 |
| 529 void AddSocketDataProvider(SocketDataProvider* socket); | 526 void AddSocketDataProvider(SocketDataProvider* socket); |
| 530 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); | 527 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); |
| 531 void ResetNextMockIndexes(); | 528 void ResetNextMockIndexes(); |
| 532 | 529 |
| 533 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory | |
| 534 // created. | |
| 535 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; | |
| 536 | |
| 537 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory | |
| 538 // created. | |
| 539 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; | |
| 540 | |
| 541 SocketDataProviderArray<SocketDataProvider>& mock_data() { | 530 SocketDataProviderArray<SocketDataProvider>& mock_data() { |
| 542 return mock_data_; | 531 return mock_data_; |
| 543 } | 532 } |
| 544 std::vector<MockTCPClientSocket*>& tcp_client_sockets() { | |
| 545 return tcp_client_sockets_; | |
| 546 } | |
| 547 std::vector<MockUDPClientSocket*>& udp_client_sockets() { | |
| 548 return udp_client_sockets_; | |
| 549 } | |
| 550 | 533 |
| 551 // ClientSocketFactory | 534 // ClientSocketFactory |
| 552 virtual DatagramClientSocket* CreateDatagramClientSocket( | 535 virtual DatagramClientSocket* CreateDatagramClientSocket( |
| 553 DatagramSocket::BindType bind_type, | 536 DatagramSocket::BindType bind_type, |
| 554 const RandIntCallback& rand_int_cb, | 537 const RandIntCallback& rand_int_cb, |
| 555 NetLog* net_log, | 538 NetLog* net_log, |
| 556 const NetLog::Source& source) OVERRIDE; | 539 const NetLog::Source& source) OVERRIDE; |
| 557 virtual StreamSocket* CreateTransportClientSocket( | 540 virtual StreamSocket* CreateTransportClientSocket( |
| 558 const AddressList& addresses, | 541 const AddressList& addresses, |
| 559 NetLog* net_log, | 542 NetLog* net_log, |
| 560 const NetLog::Source& source) OVERRIDE; | 543 const NetLog::Source& source) OVERRIDE; |
| 561 virtual SSLClientSocket* CreateSSLClientSocket( | 544 virtual SSLClientSocket* CreateSSLClientSocket( |
| 562 ClientSocketHandle* transport_socket, | 545 ClientSocketHandle* transport_socket, |
| 563 const HostPortPair& host_and_port, | 546 const HostPortPair& host_and_port, |
| 564 const SSLConfig& ssl_config, | 547 const SSLConfig& ssl_config, |
| 565 SSLHostInfo* ssl_host_info, | 548 SSLHostInfo* ssl_host_info, |
| 566 const SSLClientSocketContext& context) OVERRIDE; | 549 const SSLClientSocketContext& context) OVERRIDE; |
| 567 virtual void ClearSSLSessionCache() OVERRIDE; | 550 virtual void ClearSSLSessionCache() OVERRIDE; |
| 568 | 551 |
| 569 private: | 552 private: |
| 570 SocketDataProviderArray<SocketDataProvider> mock_data_; | 553 SocketDataProviderArray<SocketDataProvider> mock_data_; |
| 571 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; | 554 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; |
| 572 | |
| 573 // Store pointers to handed out sockets in case the test wants to get them. | |
| 574 std::vector<MockUDPClientSocket*> udp_client_sockets_; | |
| 575 std::vector<MockTCPClientSocket*> tcp_client_sockets_; | |
| 576 std::vector<MockSSLClientSocket*> ssl_client_sockets_; | |
| 577 }; | 555 }; |
| 578 | 556 |
| 579 class MockClientSocket : public SSLClientSocket { | 557 class MockClientSocket : public SSLClientSocket { |
| 580 public: | 558 public: |
| 581 // TODO(ajwong): Why do we need net::NetLog? | 559 // TODO(ajwong): Why do we need net::NetLog? |
| 582 explicit MockClientSocket(net::NetLog* net_log); | 560 explicit MockClientSocket(net::NetLog* net_log); |
| 583 | 561 |
| 584 // Socket implementation. | 562 // Socket implementation. |
| 585 virtual int Read(IOBuffer* buf, int buf_len, | 563 virtual int Read(IOBuffer* buf, int buf_len, |
| 586 const CompletionCallback& callback) = 0; | 564 const CompletionCallback& callback) = 0; |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1044 | 1022 |
| 1045 extern const char kSOCKS5OkRequest[]; | 1023 extern const char kSOCKS5OkRequest[]; |
| 1046 extern const int kSOCKS5OkRequestLength; | 1024 extern const int kSOCKS5OkRequestLength; |
| 1047 | 1025 |
| 1048 extern const char kSOCKS5OkResponse[]; | 1026 extern const char kSOCKS5OkResponse[]; |
| 1049 extern const int kSOCKS5OkResponseLength; | 1027 extern const int kSOCKS5OkResponseLength; |
| 1050 | 1028 |
| 1051 } // namespace net | 1029 } // namespace net |
| 1052 | 1030 |
| 1053 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1031 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |