| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 const MockRead& PeekRead(size_t index) const; | 175 const MockRead& PeekRead(size_t index) const; |
| 176 const MockWrite& PeekWrite(size_t index) const; | 176 const MockWrite& PeekWrite(size_t index) const; |
| 177 size_t read_index() const { return read_index_; } | 177 size_t read_index() const { return read_index_; } |
| 178 size_t write_index() const { return write_index_; } | 178 size_t write_index() const { return write_index_; } |
| 179 size_t read_count() const { return read_count_; } | 179 size_t read_count() const { return read_count_; } |
| 180 size_t write_count() const { return write_count_; } | 180 size_t write_count() const { return write_count_; } |
| 181 | 181 |
| 182 bool at_read_eof() const { return read_index_ >= read_count_; } | 182 bool at_read_eof() const { return read_index_ >= read_count_; } |
| 183 bool at_write_eof() const { return write_index_ >= write_count_; } | 183 bool at_write_eof() const { return write_index_ >= write_count_; } |
| 184 | 184 |
| 185 virtual void CompleteRead() {} |
| 186 |
| 185 // SocketDataProvider methods: | 187 // SocketDataProvider methods: |
| 186 virtual MockRead GetNextRead(); | 188 virtual MockRead GetNextRead(); |
| 187 virtual MockWriteResult OnWrite(const std::string& data); | 189 virtual MockWriteResult OnWrite(const std::string& data); |
| 188 virtual void Reset(); | 190 virtual void Reset(); |
| 189 virtual void CompleteRead() {} | |
| 190 | 191 |
| 191 private: | 192 private: |
| 192 MockRead* reads_; | 193 MockRead* reads_; |
| 193 size_t read_index_; | 194 size_t read_index_; |
| 194 size_t read_count_; | 195 size_t read_count_; |
| 195 MockWrite* writes_; | 196 MockWrite* writes_; |
| 196 size_t write_index_; | 197 size_t write_index_; |
| 197 size_t write_count_; | 198 size_t write_count_; |
| 198 | 199 |
| 199 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); | 200 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); |
| 200 }; | 201 }; |
| 201 | 202 |
| 202 // SocketDataProvider which can make decisions about next mock reads based on | 203 // SocketDataProvider which can make decisions about next mock reads based on |
| 203 // received writes. It can also be used to enforce order of operations, for | 204 // received writes. It can also be used to enforce order of operations, for |
| 204 // example that tested code must send the "Hello!" message before receiving | 205 // example that tested code must send the "Hello!" message before receiving |
| 205 // response. This is useful for testing conversation-like protocols like FTP. | 206 // response. This is useful for testing conversation-like protocols like FTP. |
| 206 class DynamicSocketDataProvider : public SocketDataProvider { | 207 class DynamicSocketDataProvider : public SocketDataProvider { |
| 207 public: | 208 public: |
| 208 DynamicSocketDataProvider(); | 209 DynamicSocketDataProvider(); |
| 209 virtual ~DynamicSocketDataProvider(); | 210 virtual ~DynamicSocketDataProvider(); |
| 210 | 211 |
| 212 int short_read_limit() const { return short_read_limit_; } |
| 213 void set_short_read_limit(int limit) { short_read_limit_ = limit; } |
| 214 |
| 215 void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; } |
| 216 |
| 211 // SocketDataProvider methods: | 217 // SocketDataProvider methods: |
| 212 virtual MockRead GetNextRead(); | 218 virtual MockRead GetNextRead(); |
| 213 virtual MockWriteResult OnWrite(const std::string& data) = 0; | 219 virtual MockWriteResult OnWrite(const std::string& data) = 0; |
| 214 virtual void Reset(); | 220 virtual void Reset(); |
| 215 | 221 |
| 216 int short_read_limit() const { return short_read_limit_; } | |
| 217 void set_short_read_limit(int limit) { short_read_limit_ = limit; } | |
| 218 | |
| 219 void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; } | |
| 220 | |
| 221 protected: | 222 protected: |
| 222 // The next time there is a read from this socket, it will return |data|. | 223 // The next time there is a read from this socket, it will return |data|. |
| 223 // Before calling SimulateRead next time, the previous data must be consumed. | 224 // Before calling SimulateRead next time, the previous data must be consumed. |
| 224 void SimulateRead(const char* data, size_t length); | 225 void SimulateRead(const char* data, size_t length); |
| 225 void SimulateRead(const char* data) { | 226 void SimulateRead(const char* data) { |
| 226 SimulateRead(data, std::strlen(data)); | 227 SimulateRead(data, std::strlen(data)); |
| 227 } | 228 } |
| 228 | 229 |
| 229 private: | 230 private: |
| 230 std::deque<MockRead> reads_; | 231 std::deque<MockRead> reads_; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 // a MockRead to complete. | 278 // a MockRead to complete. |
| 278 // |writes| the list of MockWrite completions. | 279 // |writes| the list of MockWrite completions. |
| 279 // Note: All MockReads and MockWrites must be async. | 280 // Note: All MockReads and MockWrites must be async. |
| 280 // Note: The MockRead and MockWrite lists musts end with a EOF | 281 // Note: The MockRead and MockWrite lists musts end with a EOF |
| 281 // e.g. a MockRead(true, 0, 0); | 282 // e.g. a MockRead(true, 0, 0); |
| 282 DelayedSocketData(const MockConnect& connect, int write_delay, | 283 DelayedSocketData(const MockConnect& connect, int write_delay, |
| 283 MockRead* reads, size_t reads_count, | 284 MockRead* reads, size_t reads_count, |
| 284 MockWrite* writes, size_t writes_count); | 285 MockWrite* writes, size_t writes_count); |
| 285 ~DelayedSocketData(); | 286 ~DelayedSocketData(); |
| 286 | 287 |
| 288 void ForceNextRead(); |
| 289 |
| 290 // StaticSocketDataProvider: |
| 287 virtual MockRead GetNextRead(); | 291 virtual MockRead GetNextRead(); |
| 288 virtual MockWriteResult OnWrite(const std::string& data); | 292 virtual MockWriteResult OnWrite(const std::string& data); |
| 289 virtual void Reset(); | 293 virtual void Reset(); |
| 290 virtual void CompleteRead(); | 294 virtual void CompleteRead(); |
| 291 void ForceNextRead(); | |
| 292 | 295 |
| 293 private: | 296 private: |
| 294 int write_delay_; | 297 int write_delay_; |
| 295 ScopedRunnableMethodFactory<DelayedSocketData> factory_; | 298 ScopedRunnableMethodFactory<DelayedSocketData> factory_; |
| 296 }; | 299 }; |
| 297 | 300 |
| 298 // A DataProvider where the reads are ordered. | 301 // A DataProvider where the reads are ordered. |
| 299 // If a read is requested before its sequence number is reached, we return an | 302 // If a read is requested before its sequence number is reached, we return an |
| 300 // ERR_IO_PENDING (that way we don't have to explicitly add a MockRead just to | 303 // ERR_IO_PENDING (that way we don't have to explicitly add a MockRead just to |
| 301 // wait). | 304 // wait). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 320 // |connect| the result for the connect phase. | 323 // |connect| the result for the connect phase. |
| 321 // |reads| the list of MockRead completions. | 324 // |reads| the list of MockRead completions. |
| 322 // |writes| the list of MockWrite completions. | 325 // |writes| the list of MockWrite completions. |
| 323 // Note: All MockReads and MockWrites must be async. | 326 // Note: All MockReads and MockWrites must be async. |
| 324 // Note: The MockRead and MockWrite lists musts end with a EOF | 327 // Note: The MockRead and MockWrite lists musts end with a EOF |
| 325 // e.g. a MockRead(true, 0, 0); | 328 // e.g. a MockRead(true, 0, 0); |
| 326 OrderedSocketData(const MockConnect& connect, | 329 OrderedSocketData(const MockConnect& connect, |
| 327 MockRead* reads, size_t reads_count, | 330 MockRead* reads, size_t reads_count, |
| 328 MockWrite* writes, size_t writes_count); | 331 MockWrite* writes, size_t writes_count); |
| 329 | 332 |
| 330 virtual MockRead GetNextRead(); | |
| 331 virtual MockWriteResult OnWrite(const std::string& data); | |
| 332 virtual void Reset(); | |
| 333 virtual void CompleteRead(); | |
| 334 | |
| 335 void SetCompletionCallback(CompletionCallback* callback) { | 333 void SetCompletionCallback(CompletionCallback* callback) { |
| 336 callback_ = callback; | 334 callback_ = callback; |
| 337 } | 335 } |
| 338 | 336 |
| 339 // Posts a quit message to the current message loop, if one is running. | 337 // Posts a quit message to the current message loop, if one is running. |
| 340 void EndLoop(); | 338 void EndLoop(); |
| 341 | 339 |
| 340 // StaticSocketDataProvider: |
| 341 virtual MockRead GetNextRead(); |
| 342 virtual MockWriteResult OnWrite(const std::string& data); |
| 343 virtual void Reset(); |
| 344 virtual void CompleteRead(); |
| 345 |
| 342 private: | 346 private: |
| 343 friend class base::RefCounted<OrderedSocketData>; | 347 friend class base::RefCounted<OrderedSocketData>; |
| 344 virtual ~OrderedSocketData(); | 348 virtual ~OrderedSocketData(); |
| 345 | 349 |
| 346 int sequence_number_; | 350 int sequence_number_; |
| 347 int loop_stop_stage_; | 351 int loop_stop_stage_; |
| 348 CompletionCallback* callback_; | 352 CompletionCallback* callback_; |
| 349 bool blocked_; | 353 bool blocked_; |
| 350 ScopedRunnableMethodFactory<OrderedSocketData> factory_; | 354 ScopedRunnableMethodFactory<OrderedSocketData> factory_; |
| 351 }; | 355 }; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 // | 413 // |
| 410 // For examples of how to use this class, see: | 414 // For examples of how to use this class, see: |
| 411 // deterministic_socket_data_unittests.cc | 415 // deterministic_socket_data_unittests.cc |
| 412 class DeterministicSocketData : public StaticSocketDataProvider, | 416 class DeterministicSocketData : public StaticSocketDataProvider, |
| 413 public base::RefCounted<DeterministicSocketData> { | 417 public base::RefCounted<DeterministicSocketData> { |
| 414 public: | 418 public: |
| 415 // |reads| the list of MockRead completions. | 419 // |reads| the list of MockRead completions. |
| 416 // |writes| the list of MockWrite completions. | 420 // |writes| the list of MockWrite completions. |
| 417 DeterministicSocketData(MockRead* reads, size_t reads_count, | 421 DeterministicSocketData(MockRead* reads, size_t reads_count, |
| 418 MockWrite* writes, size_t writes_count); | 422 MockWrite* writes, size_t writes_count); |
| 419 | 423 virtual ~DeterministicSocketData(); |
| 420 // When the socket calls Read(), that calls GetNextRead(), and expects either | |
| 421 // ERR_IO_PENDING or data. | |
| 422 virtual MockRead GetNextRead(); | |
| 423 | |
| 424 // When the socket calls Write(), it always completes synchronously. OnWrite() | |
| 425 // checks to make sure the written data matches the expected data. The | |
| 426 // callback will not be invoked until its sequence number is reached. | |
| 427 virtual MockWriteResult OnWrite(const std::string& data); | |
| 428 | |
| 429 virtual void Reset(); | |
| 430 | |
| 431 virtual void CompleteRead() {} | |
| 432 | 424 |
| 433 // Consume all the data up to the give stop point (via SetStop()). | 425 // Consume all the data up to the give stop point (via SetStop()). |
| 434 void Run(); | 426 void Run(); |
| 435 | 427 |
| 436 // Set the stop point to be |steps| from now, and then invoke Run(). | 428 // Set the stop point to be |steps| from now, and then invoke Run(). |
| 437 void RunFor(int steps); | 429 void RunFor(int steps); |
| 438 | 430 |
| 439 // Stop at step |seq|, which must be in the future. | 431 // Stop at step |seq|, which must be in the future. |
| 440 virtual void SetStop(int seq) { | 432 virtual void SetStop(int seq); |
| 441 DCHECK_LT(sequence_number_, seq); | |
| 442 stopping_sequence_number_ = seq; | |
| 443 stopped_ = false; | |
| 444 } | |
| 445 | 433 |
| 446 // Stop |seq| steps after the current step. | 434 // Stop |seq| steps after the current step. |
| 447 virtual void StopAfter(int seq) { | 435 virtual void StopAfter(int seq); |
| 448 SetStop(sequence_number_ + seq); | |
| 449 } | |
| 450 bool stopped() const { return stopped_; } | 436 bool stopped() const { return stopped_; } |
| 451 void SetStopped(bool val) { stopped_ = val; } | 437 void SetStopped(bool val) { stopped_ = val; } |
| 452 MockRead& current_read() { return current_read_; } | 438 MockRead& current_read() { return current_read_; } |
| 453 MockRead& current_write() { return current_write_; } | 439 MockRead& current_write() { return current_write_; } |
| 454 int sequence_number() const { return sequence_number_; } | 440 int sequence_number() const { return sequence_number_; } |
| 455 void set_socket(base::WeakPtr<DeterministicMockTCPClientSocket> socket) { | 441 void set_socket(base::WeakPtr<DeterministicMockTCPClientSocket> socket) { |
| 456 socket_ = socket; | 442 socket_ = socket; |
| 457 } | 443 } |
| 458 | 444 |
| 445 // StaticSocketDataProvider: |
| 446 |
| 447 // When the socket calls Read(), that calls GetNextRead(), and expects either |
| 448 // ERR_IO_PENDING or data. |
| 449 virtual MockRead GetNextRead(); |
| 450 |
| 451 // When the socket calls Write(), it always completes synchronously. OnWrite() |
| 452 // checks to make sure the written data matches the expected data. The |
| 453 // callback will not be invoked until its sequence number is reached. |
| 454 virtual MockWriteResult OnWrite(const std::string& data); |
| 455 virtual void Reset(); |
| 456 virtual void CompleteRead() {} |
| 457 |
| 459 private: | 458 private: |
| 460 // Invoke the read and write callbacks, if the timing is appropriate. | 459 // Invoke the read and write callbacks, if the timing is appropriate. |
| 461 void InvokeCallbacks(); | 460 void InvokeCallbacks(); |
| 462 | 461 |
| 463 void NextStep(); | 462 void NextStep(); |
| 464 | 463 |
| 465 int sequence_number_; | 464 int sequence_number_; |
| 466 MockRead current_read_; | 465 MockRead current_read_; |
| 467 MockWrite current_write_; | 466 MockWrite current_write_; |
| 468 int stopping_sequence_number_; | 467 int stopping_sequence_number_; |
| 469 bool stopped_; | 468 bool stopped_; |
| 470 base::WeakPtr<DeterministicMockTCPClientSocket> socket_; | 469 base::WeakPtr<DeterministicMockTCPClientSocket> socket_; |
| 471 bool print_debug_; | 470 bool print_debug_; |
| 472 }; | 471 }; |
| 473 | 472 |
| 474 | |
| 475 // Holds an array of SocketDataProvider elements. As Mock{TCP,SSL}ClientSocket | 473 // Holds an array of SocketDataProvider elements. As Mock{TCP,SSL}ClientSocket |
| 476 // objects get instantiated, they take their data from the i'th element of this | 474 // objects get instantiated, they take their data from the i'th element of this |
| 477 // array. | 475 // array. |
| 478 template<typename T> | 476 template<typename T> |
| 479 class SocketDataProviderArray { | 477 class SocketDataProviderArray { |
| 480 public: | 478 public: |
| 481 SocketDataProviderArray() : next_index_(0) { | 479 SocketDataProviderArray() : next_index_(0) { |
| 482 } | 480 } |
| 483 | 481 |
| 484 T* GetNext() { | 482 T* GetNext() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 void ResetNextMockIndexes(); | 520 void ResetNextMockIndexes(); |
| 523 | 521 |
| 524 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory | 522 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory |
| 525 // created. | 523 // created. |
| 526 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; | 524 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; |
| 527 | 525 |
| 528 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory | 526 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory |
| 529 // created. | 527 // created. |
| 530 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; | 528 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; |
| 531 | 529 |
| 530 SocketDataProviderArray<SocketDataProvider>& mock_data() { |
| 531 return mock_data_; |
| 532 } |
| 533 std::vector<MockTCPClientSocket*>& tcp_client_sockets() { |
| 534 return tcp_client_sockets_; |
| 535 } |
| 536 |
| 532 // ClientSocketFactory | 537 // ClientSocketFactory |
| 533 virtual ClientSocket* CreateTCPClientSocket( | 538 virtual ClientSocket* CreateTCPClientSocket( |
| 534 const AddressList& addresses, | 539 const AddressList& addresses, |
| 535 NetLog* net_log, | 540 NetLog* net_log, |
| 536 const NetLog::Source& source); | 541 const NetLog::Source& source); |
| 537 virtual SSLClientSocket* CreateSSLClientSocket( | 542 virtual SSLClientSocket* CreateSSLClientSocket( |
| 538 ClientSocketHandle* transport_socket, | 543 ClientSocketHandle* transport_socket, |
| 539 const HostPortPair& host_and_port, | 544 const HostPortPair& host_and_port, |
| 540 const SSLConfig& ssl_config, | 545 const SSLConfig& ssl_config, |
| 541 SSLHostInfo* ssl_host_info, | 546 SSLHostInfo* ssl_host_info, |
| 542 CertVerifier* cert_verifier, | 547 CertVerifier* cert_verifier, |
| 543 DnsCertProvenanceChecker* dns_cert_checker); | 548 DnsCertProvenanceChecker* dns_cert_checker); |
| 544 SocketDataProviderArray<SocketDataProvider>& mock_data() { | |
| 545 return mock_data_; | |
| 546 } | |
| 547 std::vector<MockTCPClientSocket*>& tcp_client_sockets() { | |
| 548 return tcp_client_sockets_; | |
| 549 } | |
| 550 | 549 |
| 551 private: | 550 private: |
| 552 SocketDataProviderArray<SocketDataProvider> mock_data_; | 551 SocketDataProviderArray<SocketDataProvider> mock_data_; |
| 553 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; | 552 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; |
| 554 | 553 |
| 555 // Store pointers to handed out sockets in case the test wants to get them. | 554 // Store pointers to handed out sockets in case the test wants to get them. |
| 556 std::vector<MockTCPClientSocket*> tcp_client_sockets_; | 555 std::vector<MockTCPClientSocket*> tcp_client_sockets_; |
| 557 std::vector<MockSSLClientSocket*> ssl_client_sockets_; | 556 std::vector<MockSSLClientSocket*> ssl_client_sockets_; |
| 558 }; | 557 }; |
| 559 | 558 |
| 560 class MockClientSocket : public net::SSLClientSocket { | 559 class MockClientSocket : public net::SSLClientSocket { |
| 561 public: | 560 public: |
| 562 explicit MockClientSocket(net::NetLog* net_log); | 561 explicit MockClientSocket(net::NetLog* net_log); |
| 562 |
| 563 // If an async IO is pending because the SocketDataProvider returned |
| 564 // ERR_IO_PENDING, then the MockClientSocket waits until this OnReadComplete |
| 565 // is called to complete the asynchronous read operation. |
| 566 // data.async is ignored, and this read is completed synchronously as |
| 567 // part of this call. |
| 568 virtual void OnReadComplete(const MockRead& data) = 0; |
| 569 |
| 570 // Socket methods: |
| 571 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 572 net::CompletionCallback* callback) = 0; |
| 573 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 574 net::CompletionCallback* callback) = 0; |
| 575 virtual bool SetReceiveBufferSize(int32 size); |
| 576 virtual bool SetSendBufferSize(int32 size); |
| 577 |
| 563 // ClientSocket methods: | 578 // ClientSocket methods: |
| 564 virtual int Connect(net::CompletionCallback* callback) = 0; | 579 virtual int Connect(net::CompletionCallback* callback) = 0; |
| 565 virtual void Disconnect(); | 580 virtual void Disconnect(); |
| 566 virtual bool IsConnected() const; | 581 virtual bool IsConnected() const; |
| 567 virtual bool IsConnectedAndIdle() const; | 582 virtual bool IsConnectedAndIdle() const; |
| 568 virtual int GetPeerAddress(AddressList* address) const; | 583 virtual int GetPeerAddress(AddressList* address) const; |
| 569 virtual const BoundNetLog& NetLog() const { return net_log_;} | 584 virtual const BoundNetLog& NetLog() const; |
| 570 virtual void SetSubresourceSpeculation() {} | 585 virtual void SetSubresourceSpeculation() {} |
| 571 virtual void SetOmniboxSpeculation() {} | 586 virtual void SetOmniboxSpeculation() {} |
| 572 | 587 |
| 573 // SSLClientSocket methods: | 588 // SSLClientSocket methods: |
| 574 virtual void GetSSLInfo(net::SSLInfo* ssl_info); | 589 virtual void GetSSLInfo(net::SSLInfo* ssl_info); |
| 575 virtual void GetSSLCertRequestInfo( | 590 virtual void GetSSLCertRequestInfo( |
| 576 net::SSLCertRequestInfo* cert_request_info); | 591 net::SSLCertRequestInfo* cert_request_info); |
| 577 virtual NextProtoStatus GetNextProto(std::string* proto); | 592 virtual NextProtoStatus GetNextProto(std::string* proto); |
| 578 | 593 |
| 579 // Socket methods: | |
| 580 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 581 net::CompletionCallback* callback) = 0; | |
| 582 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 583 net::CompletionCallback* callback) = 0; | |
| 584 virtual bool SetReceiveBufferSize(int32 size) { return true; } | |
| 585 virtual bool SetSendBufferSize(int32 size) { return true; } | |
| 586 | |
| 587 // If an async IO is pending because the SocketDataProvider returned | |
| 588 // ERR_IO_PENDING, then the MockClientSocket waits until this OnReadComplete | |
| 589 // is called to complete the asynchronous read operation. | |
| 590 // data.async is ignored, and this read is completed synchronously as | |
| 591 // part of this call. | |
| 592 virtual void OnReadComplete(const MockRead& data) = 0; | |
| 593 | |
| 594 protected: | 594 protected: |
| 595 virtual ~MockClientSocket() {} | 595 virtual ~MockClientSocket(); |
| 596 void RunCallbackAsync(net::CompletionCallback* callback, int result); | 596 void RunCallbackAsync(net::CompletionCallback* callback, int result); |
| 597 void RunCallback(net::CompletionCallback*, int result); | 597 void RunCallback(net::CompletionCallback*, int result); |
| 598 | 598 |
| 599 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; | 599 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; |
| 600 | 600 |
| 601 // True if Connect completed successfully and Disconnect hasn't been called. | 601 // True if Connect completed successfully and Disconnect hasn't been called. |
| 602 bool connected_; | 602 bool connected_; |
| 603 | 603 |
| 604 net::BoundNetLog net_log_; | 604 net::BoundNetLog net_log_; |
| 605 }; | 605 }; |
| 606 | 606 |
| 607 class MockTCPClientSocket : public MockClientSocket { | 607 class MockTCPClientSocket : public MockClientSocket { |
| 608 public: | 608 public: |
| 609 MockTCPClientSocket(const net::AddressList& addresses, net::NetLog* net_log, | 609 MockTCPClientSocket(const net::AddressList& addresses, net::NetLog* net_log, |
| 610 net::SocketDataProvider* socket); | 610 net::SocketDataProvider* socket); |
| 611 | 611 |
| 612 // ClientSocket methods: | 612 net::AddressList addresses() const { return addresses_; } |
| 613 virtual int Connect(net::CompletionCallback* callback); | |
| 614 virtual void Disconnect(); | |
| 615 virtual bool IsConnected() const; | |
| 616 virtual bool IsConnectedAndIdle() const { return IsConnected(); } | |
| 617 virtual bool WasEverUsed() const { return was_used_to_convey_data_; } | |
| 618 virtual bool UsingTCPFastOpen() const { return false; } | |
| 619 | 613 |
| 620 // Socket methods: | 614 // Socket methods: |
| 621 virtual int Read(net::IOBuffer* buf, int buf_len, | 615 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 622 net::CompletionCallback* callback); | 616 net::CompletionCallback* callback); |
| 623 virtual int Write(net::IOBuffer* buf, int buf_len, | 617 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 624 net::CompletionCallback* callback); | 618 net::CompletionCallback* callback); |
| 625 | 619 |
| 620 // ClientSocket methods: |
| 621 virtual int Connect(net::CompletionCallback* callback); |
| 622 virtual void Disconnect(); |
| 623 virtual bool IsConnected() const; |
| 624 virtual bool IsConnectedAndIdle() const; |
| 625 virtual bool WasEverUsed() const; |
| 626 virtual bool UsingTCPFastOpen() const; |
| 627 |
| 628 // MockClientSocket: |
| 626 virtual void OnReadComplete(const MockRead& data); | 629 virtual void OnReadComplete(const MockRead& data); |
| 627 | 630 |
| 628 net::AddressList addresses() const { return addresses_; } | |
| 629 | |
| 630 private: | 631 private: |
| 631 int CompleteRead(); | 632 int CompleteRead(); |
| 632 | 633 |
| 633 net::AddressList addresses_; | 634 net::AddressList addresses_; |
| 634 | 635 |
| 635 net::SocketDataProvider* data_; | 636 net::SocketDataProvider* data_; |
| 636 int read_offset_; | 637 int read_offset_; |
| 637 net::MockRead read_data_; | 638 net::MockRead read_data_; |
| 638 bool need_read_data_; | 639 bool need_read_data_; |
| 639 | 640 |
| 640 // True if the peer has closed the connection. This allows us to simulate | 641 // True if the peer has closed the connection. This allows us to simulate |
| 641 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real | 642 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real |
| 642 // TCPClientSocket. | 643 // TCPClientSocket. |
| 643 bool peer_closed_connection_; | 644 bool peer_closed_connection_; |
| 644 | 645 |
| 645 // While an asynchronous IO is pending, we save our user-buffer state. | 646 // While an asynchronous IO is pending, we save our user-buffer state. |
| 646 net::IOBuffer* pending_buf_; | 647 net::IOBuffer* pending_buf_; |
| 647 int pending_buf_len_; | 648 int pending_buf_len_; |
| 648 net::CompletionCallback* pending_callback_; | 649 net::CompletionCallback* pending_callback_; |
| 649 bool was_used_to_convey_data_; | 650 bool was_used_to_convey_data_; |
| 650 }; | 651 }; |
| 651 | 652 |
| 652 class DeterministicMockTCPClientSocket : public MockClientSocket, | 653 class DeterministicMockTCPClientSocket : public MockClientSocket, |
| 653 public base::SupportsWeakPtr<DeterministicMockTCPClientSocket> { | 654 public base::SupportsWeakPtr<DeterministicMockTCPClientSocket> { |
| 654 public: | 655 public: |
| 655 DeterministicMockTCPClientSocket(net::NetLog* net_log, | 656 DeterministicMockTCPClientSocket(net::NetLog* net_log, |
| 656 net::DeterministicSocketData* data); | 657 net::DeterministicSocketData* data); |
| 657 | 658 virtual ~DeterministicMockTCPClientSocket(); |
| 658 // ClientSocket methods: | |
| 659 virtual int Connect(net::CompletionCallback* callback); | |
| 660 virtual void Disconnect(); | |
| 661 virtual bool IsConnected() const; | |
| 662 virtual bool IsConnectedAndIdle() const { return IsConnected(); } | |
| 663 virtual bool WasEverUsed() const { return was_used_to_convey_data_; } | |
| 664 virtual bool UsingTCPFastOpen() const { return false; } | |
| 665 | |
| 666 // Socket methods: | |
| 667 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 668 net::CompletionCallback* callback); | |
| 669 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 670 net::CompletionCallback* callback); | |
| 671 | 659 |
| 672 bool write_pending() const { return write_pending_; } | 660 bool write_pending() const { return write_pending_; } |
| 673 bool read_pending() const { return read_pending_; } | 661 bool read_pending() const { return read_pending_; } |
| 674 | 662 |
| 675 void CompleteWrite(); | 663 void CompleteWrite(); |
| 676 int CompleteRead(); | 664 int CompleteRead(); |
| 677 void OnReadComplete(const MockRead& data); | 665 |
| 666 // Socket: |
| 667 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 668 net::CompletionCallback* callback); |
| 669 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 670 net::CompletionCallback* callback); |
| 671 |
| 672 // ClientSocket: |
| 673 virtual int Connect(net::CompletionCallback* callback); |
| 674 virtual void Disconnect(); |
| 675 virtual bool IsConnected() const; |
| 676 virtual bool IsConnectedAndIdle() const; |
| 677 virtual bool WasEverUsed() const; |
| 678 virtual bool UsingTCPFastOpen() const; |
| 679 |
| 680 // MockClientSocket: |
| 681 virtual void OnReadComplete(const MockRead& data); |
| 678 | 682 |
| 679 private: | 683 private: |
| 680 bool write_pending_; | 684 bool write_pending_; |
| 681 net::CompletionCallback* write_callback_; | 685 net::CompletionCallback* write_callback_; |
| 682 int write_result_; | 686 int write_result_; |
| 683 | 687 |
| 684 net::MockRead read_data_; | 688 net::MockRead read_data_; |
| 685 | 689 |
| 686 net::IOBuffer* read_buf_; | 690 net::IOBuffer* read_buf_; |
| 687 int read_buf_len_; | 691 int read_buf_len_; |
| 688 bool read_pending_; | 692 bool read_pending_; |
| 689 net::CompletionCallback* read_callback_; | 693 net::CompletionCallback* read_callback_; |
| 690 net::DeterministicSocketData* data_; | 694 net::DeterministicSocketData* data_; |
| 691 bool was_used_to_convey_data_; | 695 bool was_used_to_convey_data_; |
| 692 }; | 696 }; |
| 693 | 697 |
| 694 class MockSSLClientSocket : public MockClientSocket { | 698 class MockSSLClientSocket : public MockClientSocket { |
| 695 public: | 699 public: |
| 696 MockSSLClientSocket( | 700 MockSSLClientSocket( |
| 697 net::ClientSocketHandle* transport_socket, | 701 net::ClientSocketHandle* transport_socket, |
| 698 const HostPortPair& host_and_port, | 702 const HostPortPair& host_and_port, |
| 699 const net::SSLConfig& ssl_config, | 703 const net::SSLConfig& ssl_config, |
| 700 SSLHostInfo* ssl_host_info, | 704 SSLHostInfo* ssl_host_info, |
| 701 net::SSLSocketDataProvider* socket); | 705 net::SSLSocketDataProvider* socket); |
| 702 ~MockSSLClientSocket(); | 706 virtual ~MockSSLClientSocket(); |
| 707 |
| 708 // Socket methods: |
| 709 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 710 net::CompletionCallback* callback); |
| 711 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 712 net::CompletionCallback* callback); |
| 703 | 713 |
| 704 // ClientSocket methods: | 714 // ClientSocket methods: |
| 705 virtual int Connect(net::CompletionCallback* callback); | 715 virtual int Connect(net::CompletionCallback* callback); |
| 706 virtual void Disconnect(); | 716 virtual void Disconnect(); |
| 707 virtual bool IsConnected() const; | 717 virtual bool IsConnected() const; |
| 708 virtual bool WasEverUsed() const; | 718 virtual bool WasEverUsed() const; |
| 709 virtual bool UsingTCPFastOpen() const; | 719 virtual bool UsingTCPFastOpen() const; |
| 710 | 720 |
| 711 // Socket methods: | |
| 712 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 713 net::CompletionCallback* callback); | |
| 714 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 715 net::CompletionCallback* callback); | |
| 716 | |
| 717 // SSLClientSocket methods: | 721 // SSLClientSocket methods: |
| 718 virtual void GetSSLInfo(net::SSLInfo* ssl_info); | 722 virtual void GetSSLInfo(net::SSLInfo* ssl_info); |
| 719 virtual void GetSSLCertRequestInfo( | 723 virtual void GetSSLCertRequestInfo( |
| 720 net::SSLCertRequestInfo* cert_request_info); | 724 net::SSLCertRequestInfo* cert_request_info); |
| 721 virtual NextProtoStatus GetNextProto(std::string* proto); | 725 virtual NextProtoStatus GetNextProto(std::string* proto); |
| 722 virtual bool was_npn_negotiated() const; | 726 virtual bool was_npn_negotiated() const; |
| 723 virtual bool set_was_npn_negotiated(bool negotiated); | 727 virtual bool set_was_npn_negotiated(bool negotiated); |
| 724 | 728 |
| 725 // This MockSocket does not implement the manual async IO feature. | 729 // This MockSocket does not implement the manual async IO feature. |
| 726 virtual void OnReadComplete(const MockRead& data) { NOTIMPLEMENTED(); } | 730 virtual void OnReadComplete(const MockRead& data); |
| 727 | 731 |
| 728 private: | 732 private: |
| 729 class ConnectCallback; | 733 class ConnectCallback; |
| 730 | 734 |
| 731 scoped_ptr<ClientSocketHandle> transport_; | 735 scoped_ptr<ClientSocketHandle> transport_; |
| 732 net::SSLSocketDataProvider* data_; | 736 net::SSLSocketDataProvider* data_; |
| 733 bool is_npn_state_set_; | 737 bool is_npn_state_set_; |
| 734 bool new_npn_value_; | 738 bool new_npn_value_; |
| 735 bool was_used_to_convey_data_; | 739 bool was_used_to_convey_data_; |
| 736 }; | 740 }; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 virtual ~DeterministicMockClientSocketFactory(); | 875 virtual ~DeterministicMockClientSocketFactory(); |
| 872 | 876 |
| 873 void AddSocketDataProvider(DeterministicSocketData* socket); | 877 void AddSocketDataProvider(DeterministicSocketData* socket); |
| 874 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); | 878 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); |
| 875 void ResetNextMockIndexes(); | 879 void ResetNextMockIndexes(); |
| 876 | 880 |
| 877 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory | 881 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory |
| 878 // created. | 882 // created. |
| 879 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; | 883 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; |
| 880 | 884 |
| 885 SocketDataProviderArray<DeterministicSocketData>& mock_data() { |
| 886 return mock_data_; |
| 887 } |
| 888 std::vector<DeterministicMockTCPClientSocket*>& tcp_client_sockets() { |
| 889 return tcp_client_sockets_; |
| 890 } |
| 891 |
| 881 // ClientSocketFactory | 892 // ClientSocketFactory |
| 882 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses, | 893 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses, |
| 883 NetLog* net_log, | 894 NetLog* net_log, |
| 884 const NetLog::Source& source); | 895 const NetLog::Source& source); |
| 885 virtual SSLClientSocket* CreateSSLClientSocket( | 896 virtual SSLClientSocket* CreateSSLClientSocket( |
| 886 ClientSocketHandle* transport_socket, | 897 ClientSocketHandle* transport_socket, |
| 887 const HostPortPair& host_and_port, | 898 const HostPortPair& host_and_port, |
| 888 const SSLConfig& ssl_config, | 899 const SSLConfig& ssl_config, |
| 889 SSLHostInfo* ssl_host_info, | 900 SSLHostInfo* ssl_host_info, |
| 890 CertVerifier* cert_verifier, | 901 CertVerifier* cert_verifier, |
| 891 DnsCertProvenanceChecker* dns_cert_checker); | 902 DnsCertProvenanceChecker* dns_cert_checker); |
| 892 | 903 |
| 893 SocketDataProviderArray<DeterministicSocketData>& mock_data() { | |
| 894 return mock_data_; | |
| 895 } | |
| 896 std::vector<DeterministicMockTCPClientSocket*>& tcp_client_sockets() { | |
| 897 return tcp_client_sockets_; | |
| 898 } | |
| 899 | |
| 900 private: | 904 private: |
| 901 SocketDataProviderArray<DeterministicSocketData> mock_data_; | 905 SocketDataProviderArray<DeterministicSocketData> mock_data_; |
| 902 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; | 906 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; |
| 903 | 907 |
| 904 // Store pointers to handed out sockets in case the test wants to get them. | 908 // Store pointers to handed out sockets in case the test wants to get them. |
| 905 std::vector<DeterministicMockTCPClientSocket*> tcp_client_sockets_; | 909 std::vector<DeterministicMockTCPClientSocket*> tcp_client_sockets_; |
| 906 std::vector<MockSSLClientSocket*> ssl_client_sockets_; | 910 std::vector<MockSSLClientSocket*> ssl_client_sockets_; |
| 907 }; | 911 }; |
| 908 | 912 |
| 909 class MockSOCKSClientSocketPool : public SOCKSClientSocketPool { | 913 class MockSOCKSClientSocketPool : public SOCKSClientSocketPool { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 944 | 948 |
| 945 extern const char kSOCKS5OkRequest[]; | 949 extern const char kSOCKS5OkRequest[]; |
| 946 extern const int kSOCKS5OkRequestLength; | 950 extern const int kSOCKS5OkRequestLength; |
| 947 | 951 |
| 948 extern const char kSOCKS5OkResponse[]; | 952 extern const char kSOCKS5OkResponse[]; |
| 949 extern const int kSOCKS5OkResponseLength; | 953 extern const int kSOCKS5OkResponseLength; |
| 950 | 954 |
| 951 } // namespace net | 955 } // namespace net |
| 952 | 956 |
| 953 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 957 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |