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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <cstring> | 9 #include <cstring> |
10 #include <deque> | 10 #include <deque> |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 // complete in this step, but is marked asynchronous. Write() returns | 426 // complete in this step, but is marked asynchronous. Write() returns |
427 // ERR_IO_PENDING. The current step is still 1. At this point RunFor(1) is | 427 // ERR_IO_PENDING. The current step is still 1. At this point RunFor(1) is |
428 // called which will cause the write callback to be invoked, and will then | 428 // called which will cause the write callback to be invoked, and will then |
429 // stop. The current state is now 2. RunFor(1) is called again, which | 429 // stop. The current state is now 2. RunFor(1) is called again, which |
430 // causes the read callback to be invoked, and will then stop. Then current | 430 // causes the read callback to be invoked, and will then stop. Then current |
431 // step is 2. Write() is called again. Then next available write is | 431 // step is 2. Write() is called again. Then next available write is |
432 // synchronous so the call to Write() returns length. | 432 // synchronous so the call to Write() returns length. |
433 // | 433 // |
434 // For examples of how to use this class, see: | 434 // For examples of how to use this class, see: |
435 // deterministic_socket_data_unittests.cc | 435 // deterministic_socket_data_unittests.cc |
436 class DeterministicSocketData : public StaticSocketDataProvider, | 436 class DeterministicSocketData |
437 public base::RefCounted<DeterministicSocketData> { | 437 : public StaticSocketDataProvider, |
| 438 public base::RefCounted<DeterministicSocketData> { |
438 public: | 439 public: |
439 // |reads| the list of MockRead completions. | 440 // |reads| the list of MockRead completions. |
440 // |writes| the list of MockWrite completions. | 441 // |writes| the list of MockWrite completions. |
441 DeterministicSocketData(MockRead* reads, size_t reads_count, | 442 DeterministicSocketData(MockRead* reads, size_t reads_count, |
442 MockWrite* writes, size_t writes_count); | 443 MockWrite* writes, size_t writes_count); |
443 virtual ~DeterministicSocketData(); | |
444 | 444 |
445 // Consume all the data up to the give stop point (via SetStop()). | 445 // Consume all the data up to the give stop point (via SetStop()). |
446 void Run(); | 446 void Run(); |
447 | 447 |
448 // Set the stop point to be |steps| from now, and then invoke Run(). | 448 // Set the stop point to be |steps| from now, and then invoke Run(). |
449 void RunFor(int steps); | 449 void RunFor(int steps); |
450 | 450 |
451 // Stop at step |seq|, which must be in the future. | 451 // Stop at step |seq|, which must be in the future. |
452 virtual void SetStop(int seq); | 452 virtual void SetStop(int seq); |
453 | 453 |
(...skipping 14 matching lines...) Expand all Loading... |
468 // ERR_IO_PENDING or data. | 468 // ERR_IO_PENDING or data. |
469 virtual MockRead GetNextRead() OVERRIDE; | 469 virtual MockRead GetNextRead() OVERRIDE; |
470 | 470 |
471 // When the socket calls Write(), it always completes synchronously. OnWrite() | 471 // When the socket calls Write(), it always completes synchronously. OnWrite() |
472 // checks to make sure the written data matches the expected data. The | 472 // checks to make sure the written data matches the expected data. The |
473 // callback will not be invoked until its sequence number is reached. | 473 // callback will not be invoked until its sequence number is reached. |
474 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; | 474 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; |
475 virtual void Reset() OVERRIDE; | 475 virtual void Reset() OVERRIDE; |
476 virtual void CompleteRead() OVERRIDE {} | 476 virtual void CompleteRead() OVERRIDE {} |
477 | 477 |
| 478 protected: |
| 479 virtual ~DeterministicSocketData(); |
| 480 |
478 private: | 481 private: |
| 482 friend class base::RefCounted<DeterministicSocketData>; |
| 483 |
479 // Invoke the read and write callbacks, if the timing is appropriate. | 484 // Invoke the read and write callbacks, if the timing is appropriate. |
480 void InvokeCallbacks(); | 485 void InvokeCallbacks(); |
481 | 486 |
482 void NextStep(); | 487 void NextStep(); |
483 | 488 |
484 int sequence_number_; | 489 int sequence_number_; |
485 MockRead current_read_; | 490 MockRead current_read_; |
486 MockWrite current_write_; | 491 MockWrite current_write_; |
487 int stopping_sequence_number_; | 492 int stopping_sequence_number_; |
488 bool stopped_; | 493 bool stopped_; |
489 base::WeakPtr<DeterministicMockTCPClientSocket> socket_; | 494 base::WeakPtr<DeterministicMockTCPClientSocket> socket_; |
490 bool print_debug_; | 495 bool print_debug_; |
491 }; | 496 }; |
492 | 497 |
493 // Holds an array of SocketDataProvider elements. As Mock{TCP,SSL}StreamSocket | 498 // Holds an array of SocketDataProvider elements. As Mock{TCP,SSL}StreamSocket |
494 // objects get instantiated, they take their data from the i'th element of this | 499 // objects get instantiated, they take their data from the i'th element of this |
495 // array. | 500 // array. |
496 template<typename T> | 501 template<typename T> |
497 class SocketDataProviderArray { | 502 class SocketDataProviderArray { |
498 public: | 503 public: |
499 SocketDataProviderArray() : next_index_(0) { | 504 SocketDataProviderArray() : next_index_(0) {} |
500 } | |
501 | 505 |
502 T* GetNext() { | 506 T* GetNext() { |
503 DCHECK_LT(next_index_, data_providers_.size()); | 507 DCHECK_LT(next_index_, data_providers_.size()); |
504 return data_providers_[next_index_++]; | 508 return data_providers_[next_index_++]; |
505 } | 509 } |
506 | 510 |
507 void Add(T* data_provider) { | 511 void Add(T* data_provider) { |
508 DCHECK(data_provider); | 512 DCHECK(data_provider); |
509 data_providers_.push_back(data_provider); | 513 data_providers_.push_back(data_provider); |
510 } | 514 } |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1043 | 1047 |
1044 extern const char kSOCKS5OkRequest[]; | 1048 extern const char kSOCKS5OkRequest[]; |
1045 extern const int kSOCKS5OkRequestLength; | 1049 extern const int kSOCKS5OkRequestLength; |
1046 | 1050 |
1047 extern const char kSOCKS5OkResponse[]; | 1051 extern const char kSOCKS5OkResponse[]; |
1048 extern const int kSOCKS5OkResponseLength; | 1052 extern const int kSOCKS5OkResponseLength; |
1049 | 1053 |
1050 } // namespace net | 1054 } // namespace net |
1051 | 1055 |
1052 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1056 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
OLD | NEW |