| 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 #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 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <cstring> | 11 #include <cstring> |
| 12 #include <deque> | 12 #include <deque> |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/callback_helpers.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 21 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
| 22 #include "base/memory/scoped_vector.h" | 23 #include "base/memory/scoped_vector.h" |
| 23 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
| 24 #include "base/strings/string16.h" | 25 #include "base/strings/string16.h" |
| 25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 26 #include "net/base/address_list.h" | 27 #include "net/base/address_list.h" |
| 27 #include "net/base/io_buffer.h" | 28 #include "net/base/io_buffer.h" |
| 29 #include "net/base/ip_address.h" |
| 30 #include "net/base/ip_endpoint.h" |
| 28 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
| 29 #include "net/base/test_completion_callback.h" | 32 #include "net/base/test_completion_callback.h" |
| 30 #include "net/http/http_auth_controller.h" | 33 #include "net/http/http_auth_controller.h" |
| 31 #include "net/http/http_proxy_client_socket_pool.h" | 34 #include "net/http/http_proxy_client_socket_pool.h" |
| 32 #include "net/log/net_log.h" | 35 #include "net/log/net_log.h" |
| 33 #include "net/socket/client_socket_factory.h" | 36 #include "net/socket/client_socket_factory.h" |
| 34 #include "net/socket/client_socket_handle.h" | 37 #include "net/socket/client_socket_handle.h" |
| 35 #include "net/socket/connection_attempts.h" | 38 #include "net/socket/connection_attempts.h" |
| 36 #include "net/socket/socket_performance_watcher.h" | 39 #include "net/socket/socket_performance_watcher.h" |
| 37 #include "net/socket/socks_client_socket_pool.h" | 40 #include "net/socket/socks_client_socket_pool.h" |
| 38 #include "net/socket/ssl_client_socket.h" | 41 #include "net/socket/ssl_client_socket.h" |
| 39 #include "net/socket/ssl_client_socket_pool.h" | 42 #include "net/socket/ssl_client_socket_pool.h" |
| 43 #include "net/socket/stream_socket.h" |
| 40 #include "net/socket/transport_client_socket_pool.h" | 44 #include "net/socket/transport_client_socket_pool.h" |
| 41 #include "net/ssl/ssl_config_service.h" | 45 #include "net/ssl/ssl_config_service.h" |
| 42 #include "net/udp/datagram_client_socket.h" | 46 #include "net/udp/datagram_client_socket.h" |
| 43 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| 44 | 48 |
| 45 namespace base { | 49 namespace base { |
| 46 class RunLoop; | 50 class RunLoop; |
| 47 } | 51 } |
| 48 | 52 |
| 49 namespace net { | 53 namespace net { |
| 50 | 54 |
| 51 const NetworkChangeNotifier::NetworkHandle kDefaultNetworkForTests = 1; | 55 const NetworkChangeNotifier::NetworkHandle kDefaultNetworkForTests = 1; |
| 52 const NetworkChangeNotifier::NetworkHandle kNewNetworkForTests = 2; | 56 const NetworkChangeNotifier::NetworkHandle kNewNetworkForTests = 2; |
| 53 | 57 |
| 54 enum { | 58 enum { |
| 55 // A private network error code used by the socket test utility classes. | 59 // A private network error code used by the socket test utility classes. |
| 56 // If the |result| member of a MockRead is | 60 // If the |result| member of a MockRead is |
| 57 // ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, that MockRead is just a | 61 // ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, that MockRead is just a |
| 58 // marker that indicates the peer will close the connection after the next | 62 // marker that indicates the peer will close the connection after the next |
| 59 // MockRead. The other members of that MockRead are ignored. | 63 // MockRead. The other members of that MockRead are ignored. |
| 60 ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ = -10000, | 64 ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ = -10000, |
| 61 }; | 65 }; |
| 62 | 66 |
| 63 class AsyncSocket; | 67 class AsyncSocket; |
| 64 class ChannelIDService; | 68 class ChannelIDService; |
| 65 class MockClientSocket; | 69 class MockClientSocket; |
| 66 class SSLClientSocket; | 70 class SSLClientSocket; |
| 67 class StreamSocket; | |
| 68 | 71 |
| 69 enum IoMode { | 72 enum IoMode { |
| 70 ASYNC, | 73 ASYNC, |
| 71 SYNCHRONOUS | 74 SYNCHRONOUS |
| 72 }; | 75 }; |
| 73 | 76 |
| 74 struct MockConnect { | 77 struct MockConnect { |
| 75 // Asynchronous connection success. | 78 // Asynchronous connection success. |
| 76 // Creates a MockConnect with |mode| ASYC, |result| OK, and | 79 // Creates a MockConnect with |mode| ASYC, |result| OK, and |
| 77 // |peer_addr| 192.0.2.33. | 80 // |peer_addr| 192.0.2.33. |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 | 489 |
| 487 private: | 490 private: |
| 488 // Index of the next |data_providers_| element to use. Not an iterator | 491 // Index of the next |data_providers_| element to use. Not an iterator |
| 489 // because those are invalidated on vector reallocation. | 492 // because those are invalidated on vector reallocation. |
| 490 size_t next_index_; | 493 size_t next_index_; |
| 491 | 494 |
| 492 // SocketDataProviders to be returned. | 495 // SocketDataProviders to be returned. |
| 493 std::vector<T*> data_providers_; | 496 std::vector<T*> data_providers_; |
| 494 }; | 497 }; |
| 495 | 498 |
| 499 class FakeDataChannel { |
| 500 public: |
| 501 FakeDataChannel(); |
| 502 ~FakeDataChannel(); |
| 503 |
| 504 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 505 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 506 |
| 507 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
| 508 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
| 509 // after the FakeDataChannel is closed, the first Write() call completes |
| 510 // asynchronously, which is necessary to reproduce bug 127822. |
| 511 void Close(); |
| 512 |
| 513 private: |
| 514 void DoReadCallback(); |
| 515 void DoWriteCallback(); |
| 516 |
| 517 int PropagateData(scoped_refptr<IOBuffer> read_buf, int read_buf_len); |
| 518 |
| 519 CompletionCallback read_callback_; |
| 520 scoped_refptr<IOBuffer> read_buf_; |
| 521 int read_buf_len_; |
| 522 |
| 523 CompletionCallback write_callback_; |
| 524 |
| 525 std::queue<scoped_refptr<DrainableIOBuffer>> data_; |
| 526 |
| 527 // True if Close() has been called. |
| 528 bool closed_; |
| 529 |
| 530 // Controls the completion of Write() after the FakeDataChannel is closed. |
| 531 // After the FakeDataChannel is closed, the first Write() call completes |
| 532 // asynchronously. |
| 533 bool write_called_after_close_; |
| 534 |
| 535 base::WeakPtrFactory<FakeDataChannel> weak_factory_; |
| 536 |
| 537 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); |
| 538 }; |
| 539 |
| 540 class FakeSocket : public StreamSocket { |
| 541 public: |
| 542 FakeSocket(FakeDataChannel* incoming_channel, |
| 543 FakeDataChannel* outgoing_channel) |
| 544 : incoming_(incoming_channel), outgoing_(outgoing_channel) {} |
| 545 |
| 546 ~FakeSocket() override; |
| 547 |
| 548 int Read(IOBuffer* buf, |
| 549 int buf_len, |
| 550 const CompletionCallback& callback) override; |
| 551 |
| 552 int Write(IOBuffer* buf, |
| 553 int buf_len, |
| 554 const CompletionCallback& callback) override; |
| 555 |
| 556 int SetReceiveBufferSize(int32_t size) override; |
| 557 |
| 558 int SetSendBufferSize(int32_t size) override; |
| 559 |
| 560 int Connect(const CompletionCallback& callback) override; |
| 561 |
| 562 void Disconnect() override; |
| 563 |
| 564 bool IsConnected() const override; |
| 565 |
| 566 bool IsConnectedAndIdle() const override; |
| 567 |
| 568 int GetPeerAddress(IPEndPoint* address) const override; |
| 569 |
| 570 int GetLocalAddress(IPEndPoint* address) const override; |
| 571 |
| 572 const BoundNetLog& NetLog() const override; |
| 573 |
| 574 void SetSubresourceSpeculation() override; |
| 575 void SetOmniboxSpeculation() override; |
| 576 |
| 577 bool WasEverUsed() const override; |
| 578 |
| 579 bool WasNpnNegotiated() const override; |
| 580 |
| 581 NextProto GetNegotiatedProtocol() const override; |
| 582 |
| 583 bool GetSSLInfo(SSLInfo* ssl_info) override; |
| 584 |
| 585 void GetConnectionAttempts(ConnectionAttempts* out) const override; |
| 586 |
| 587 void ClearConnectionAttempts() override; |
| 588 |
| 589 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; |
| 590 |
| 591 int64_t GetTotalReceivedBytes() const override; |
| 592 |
| 593 private: |
| 594 BoundNetLog net_log_; |
| 595 FakeDataChannel* incoming_; |
| 596 FakeDataChannel* outgoing_; |
| 597 |
| 598 DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
| 599 }; |
| 600 |
| 496 class MockUDPClientSocket; | 601 class MockUDPClientSocket; |
| 497 class MockTCPClientSocket; | 602 class MockTCPClientSocket; |
| 498 class MockSSLClientSocket; | 603 class MockSSLClientSocket; |
| 499 | 604 |
| 500 // ClientSocketFactory which contains arrays of sockets of each type. | 605 // ClientSocketFactory which contains arrays of sockets of each type. |
| 501 // You should first fill the arrays using AddMock{SSL,}Socket. When the factory | 606 // You should first fill the arrays using AddMock{SSL,}Socket. When the factory |
| 502 // is asked to create a socket, it takes next entry from appropriate array. | 607 // is asked to create a socket, it takes next entry from appropriate array. |
| 503 // You can use ResetNextMockIndexes to reset that next entry index for all mock | 608 // You can use ResetNextMockIndexes to reset that next entry index for all mock |
| 504 // socket types. | 609 // socket types. |
| 505 class MockClientSocketFactory : public ClientSocketFactory { | 610 class MockClientSocketFactory : public ClientSocketFactory { |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1008 | 1113 |
| 1009 // Helper function to get the total data size of the MockReads in |reads|. | 1114 // Helper function to get the total data size of the MockReads in |reads|. |
| 1010 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); | 1115 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); |
| 1011 | 1116 |
| 1012 // Helper function to get the total data size of the MockWrites in |writes|. | 1117 // Helper function to get the total data size of the MockWrites in |writes|. |
| 1013 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); | 1118 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); |
| 1014 | 1119 |
| 1015 } // namespace net | 1120 } // namespace net |
| 1016 | 1121 |
| 1017 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1122 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |