| 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 <cstring> | 8 #include <cstring> |
| 9 #include <deque> | 9 #include <deque> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // |peer_addr| 192.0.2.33. | 65 // |peer_addr| 192.0.2.33. |
| 66 MockConnect(IoMode io_mode, int r); | 66 MockConnect(IoMode io_mode, int r); |
| 67 MockConnect(IoMode io_mode, int r, IPEndPoint addr); | 67 MockConnect(IoMode io_mode, int r, IPEndPoint addr); |
| 68 ~MockConnect(); | 68 ~MockConnect(); |
| 69 | 69 |
| 70 IoMode mode; | 70 IoMode mode; |
| 71 int result; | 71 int result; |
| 72 IPEndPoint peer_addr; | 72 IPEndPoint peer_addr; |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 struct MockRead { | 75 // MockRead and MockWrite shares the same interface and members, but we'd like |
| 76 // to have distinct types because we don't want to have them used |
| 77 // interchangably. To do this, a struct template is defined, and MockRead and |
| 78 // MockWrite are instantiated by using this template. Template parameter |type| |
| 79 // is not used in the struct definition (it purely exists for creating a new |
| 80 // type). |
| 81 // |
| 82 // |data| in MockRead and MockWrite has different meanings: |data| in MockRead |
| 83 // is the data returned from the socket when MockTCPClientSocket::Read() is |
| 84 // attempted, while |data| in MockWrite is the expected data that should be |
| 85 // given in MockTCPClientSocket::Write(). |
| 86 enum MockReadWriteType { |
| 87 MOCK_READ, |
| 88 MOCK_WRITE |
| 89 }; |
| 90 |
| 91 template <MockReadWriteType type> |
| 92 struct MockReadWrite { |
| 76 // Flag to indicate that the message loop should be terminated. | 93 // Flag to indicate that the message loop should be terminated. |
| 77 enum { | 94 enum { |
| 78 STOPLOOP = 1 << 31 | 95 STOPLOOP = 1 << 31 |
| 79 }; | 96 }; |
| 80 | 97 |
| 81 // Default | 98 // Default |
| 82 MockRead() : mode(SYNCHRONOUS), result(0), data(NULL), data_len(0), | 99 MockReadWrite() : mode(SYNCHRONOUS), result(0), data(NULL), data_len(0), |
| 83 sequence_number(0), time_stamp(base::Time::Now()) {} | 100 sequence_number(0), time_stamp(base::Time::Now()) {} |
| 84 | 101 |
| 85 // Read failure (no data). | 102 // Read/write failure (no data). |
| 86 MockRead(IoMode io_mode, int result) : mode(io_mode), result(result), | 103 MockReadWrite(IoMode io_mode, int result) : mode(io_mode), result(result), |
| 87 data(NULL), data_len(0), sequence_number(0), | 104 data(NULL), data_len(0), sequence_number(0), |
| 88 time_stamp(base::Time::Now()) { } | 105 time_stamp(base::Time::Now()) { } |
| 89 | 106 |
| 90 // Read failure (no data), with sequence information. | 107 // Read/write failure (no data), with sequence information. |
| 91 MockRead(IoMode io_mode, int result, int seq) : mode(io_mode), | 108 MockReadWrite(IoMode io_mode, int result, int seq) : mode(io_mode), |
| 92 result(result), data(NULL), data_len(0), sequence_number(seq), | 109 result(result), data(NULL), data_len(0), sequence_number(seq), |
| 93 time_stamp(base::Time::Now()) { } | 110 time_stamp(base::Time::Now()) { } |
| 94 | 111 |
| 95 // Asynchronous read success (inferred data length). | 112 // Asynchronous read/write success (inferred data length). |
| 96 explicit MockRead(const char* data) : mode(ASYNC), result(0), data(data), | 113 explicit MockReadWrite(const char* data) : mode(ASYNC), result(0), |
| 97 data_len(strlen(data)), sequence_number(0), | |
| 98 time_stamp(base::Time::Now()) { } | |
| 99 | |
| 100 // Read success (inferred data length). | |
| 101 MockRead(IoMode io_mode, const char* data) : mode(io_mode), result(0), | |
| 102 data(data), data_len(strlen(data)), sequence_number(0), | 114 data(data), data_len(strlen(data)), sequence_number(0), |
| 103 time_stamp(base::Time::Now()) { } | 115 time_stamp(base::Time::Now()) { } |
| 104 | 116 |
| 105 // Read success. | 117 // Read/write success (inferred data length). |
| 106 MockRead(IoMode io_mode, const char* data, int data_len) : mode(io_mode), | 118 MockReadWrite(IoMode io_mode, const char* data) : mode(io_mode), result(0), |
| 119 data(data), data_len(strlen(data)), sequence_number(0), |
| 120 time_stamp(base::Time::Now()) { } |
| 121 |
| 122 // Read/write success. |
| 123 MockReadWrite(IoMode io_mode, const char* data, int data_len) : mode(io_mode), |
| 107 result(0), data(data), data_len(data_len), sequence_number(0), | 124 result(0), data(data), data_len(data_len), sequence_number(0), |
| 108 time_stamp(base::Time::Now()) { } | 125 time_stamp(base::Time::Now()) { } |
| 109 | 126 |
| 110 // Read success (inferred data length) with sequence information. | 127 // Read/write success (inferred data length) with sequence information. |
| 111 MockRead(IoMode io_mode, int seq, const char* data) : mode(io_mode), | 128 MockReadWrite(IoMode io_mode, int seq, const char* data) : mode(io_mode), |
| 112 result(0), data(data), data_len(strlen(data)), sequence_number(seq), | 129 result(0), data(data), data_len(strlen(data)), sequence_number(seq), |
| 113 time_stamp(base::Time::Now()) { } | 130 time_stamp(base::Time::Now()) { } |
| 114 | 131 |
| 115 // Read success with sequence information. | 132 // Read/write success with sequence information. |
| 116 MockRead(IoMode io_mode, const char* data, int data_len, int seq) : | 133 MockReadWrite(IoMode io_mode, const char* data, int data_len, int seq) : |
| 117 mode(io_mode), result(0), data(data), data_len(data_len), | 134 mode(io_mode), result(0), data(data), data_len(data_len), |
| 118 sequence_number(seq), time_stamp(base::Time::Now()) { } | 135 sequence_number(seq), time_stamp(base::Time::Now()) { } |
| 119 | 136 |
| 120 IoMode mode; | 137 IoMode mode; |
| 121 int result; | 138 int result; |
| 122 const char* data; | 139 const char* data; |
| 123 int data_len; | 140 int data_len; |
| 124 | 141 |
| 125 // For OrderedSocketData, which only allows reads to occur in a particular | 142 // For OrderedSocketData, which only allows reads to occur in a particular |
| 126 // sequence. If a read occurs before the given |sequence_number| is reached, | 143 // sequence. If a read occurs before the given |sequence_number| is reached, |
| 127 // an ERR_IO_PENDING is returned. | 144 // an ERR_IO_PENDING is returned. |
| 128 int sequence_number; // The sequence number at which a read is allowed | 145 int sequence_number; // The sequence number at which a read is allowed |
| 129 // to occur. | 146 // to occur. |
| 130 base::Time time_stamp; // The time stamp at which the operation occurred. | 147 base::Time time_stamp; // The time stamp at which the operation occurred. |
| 131 }; | 148 }; |
| 132 | 149 |
| 133 // MockWrite uses the same member fields as MockRead, but with different | 150 typedef MockReadWrite<MOCK_READ> MockRead; |
| 134 // meanings. The expected input to MockTCPClientSocket::Write() is given | 151 typedef MockReadWrite<MOCK_WRITE> MockWrite; |
| 135 // by {data, data_len}, and the return value of Write() is controlled by | |
| 136 // {async, result}. | |
| 137 typedef MockRead MockWrite; | |
| 138 | 152 |
| 139 struct MockWriteResult { | 153 struct MockWriteResult { |
| 140 MockWriteResult(IoMode io_mode, int result) | 154 MockWriteResult(IoMode io_mode, int result) |
| 141 : mode(io_mode), | 155 : mode(io_mode), |
| 142 result(result) {} | 156 result(result) {} |
| 143 | 157 |
| 144 IoMode mode; | 158 IoMode mode; |
| 145 int result; | 159 int result; |
| 146 }; | 160 }; |
| 147 | 161 |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 void RunFor(int steps); | 468 void RunFor(int steps); |
| 455 | 469 |
| 456 // Stop at step |seq|, which must be in the future. | 470 // Stop at step |seq|, which must be in the future. |
| 457 virtual void SetStop(int seq); | 471 virtual void SetStop(int seq); |
| 458 | 472 |
| 459 // Stop |seq| steps after the current step. | 473 // Stop |seq| steps after the current step. |
| 460 virtual void StopAfter(int seq); | 474 virtual void StopAfter(int seq); |
| 461 bool stopped() const { return stopped_; } | 475 bool stopped() const { return stopped_; } |
| 462 void SetStopped(bool val) { stopped_ = val; } | 476 void SetStopped(bool val) { stopped_ = val; } |
| 463 MockRead& current_read() { return current_read_; } | 477 MockRead& current_read() { return current_read_; } |
| 464 MockRead& current_write() { return current_write_; } | 478 MockWrite& current_write() { return current_write_; } |
| 465 int sequence_number() const { return sequence_number_; } | 479 int sequence_number() const { return sequence_number_; } |
| 466 void set_socket(base::WeakPtr<DeterministicMockTCPClientSocket> socket) { | 480 void set_socket(base::WeakPtr<DeterministicMockTCPClientSocket> socket) { |
| 467 socket_ = socket; | 481 socket_ = socket; |
| 468 } | 482 } |
| 469 | 483 |
| 470 // StaticSocketDataProvider: | 484 // StaticSocketDataProvider: |
| 471 | 485 |
| 472 // When the socket calls Read(), that calls GetNextRead(), and expects either | 486 // When the socket calls Read(), that calls GetNextRead(), and expects either |
| 473 // ERR_IO_PENDING or data. | 487 // ERR_IO_PENDING or data. |
| 474 virtual MockRead GetNextRead() OVERRIDE; | 488 virtual MockRead GetNextRead() OVERRIDE; |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1053 | 1067 |
| 1054 extern const char kSOCKS5OkRequest[]; | 1068 extern const char kSOCKS5OkRequest[]; |
| 1055 extern const int kSOCKS5OkRequestLength; | 1069 extern const int kSOCKS5OkRequestLength; |
| 1056 | 1070 |
| 1057 extern const char kSOCKS5OkResponse[]; | 1071 extern const char kSOCKS5OkResponse[]; |
| 1058 extern const int kSOCKS5OkResponseLength; | 1072 extern const int kSOCKS5OkResponseLength; |
| 1059 | 1073 |
| 1060 } // namespace net | 1074 } // namespace net |
| 1061 | 1075 |
| 1062 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1076 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |