OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 MockConnect connect_; | 114 MockConnect connect_; |
115 MockClientSocket* socket_; | 115 MockClientSocket* socket_; |
116 | 116 |
117 DISALLOW_COPY_AND_ASSIGN(SocketDataProvider); | 117 DISALLOW_COPY_AND_ASSIGN(SocketDataProvider); |
118 }; | 118 }; |
119 | 119 |
120 // SocketDataProvider which responds based on static tables of mock reads and | 120 // SocketDataProvider which responds based on static tables of mock reads and |
121 // writes. | 121 // writes. |
122 class StaticSocketDataProvider : public SocketDataProvider { | 122 class StaticSocketDataProvider : public SocketDataProvider { |
123 public: | 123 public: |
124 StaticSocketDataProvider() : reads_(NULL), read_index_(0), | 124 StaticSocketDataProvider() : reads_(NULL), read_index_(0), read_count_(0), |
125 writes_(NULL), write_index_(0) {} | 125 writes_(NULL), write_index_(0), write_count_(0) {} |
126 StaticSocketDataProvider(MockRead* r, MockWrite* w) : reads_(r), | 126 StaticSocketDataProvider(MockRead* reads, size_t reads_count, |
127 read_index_(0), writes_(w), write_index_(0) {} | 127 MockWrite* writes, size_t writes_count) |
| 128 : reads_(reads), |
| 129 read_index_(0), |
| 130 read_count_(reads_count), |
| 131 writes_(writes), |
| 132 write_index_(0), |
| 133 write_count_(writes_count) { |
| 134 } |
128 | 135 |
129 // SocketDataProvider methods: | 136 // SocketDataProvider methods: |
130 virtual MockRead GetNextRead(); | 137 virtual MockRead GetNextRead(); |
131 virtual MockWriteResult OnWrite(const std::string& data); | 138 virtual MockWriteResult OnWrite(const std::string& data); |
132 virtual void Reset(); | 139 virtual void Reset(); |
133 | 140 |
134 // If the test wishes to verify that all data is consumed, it can include | 141 bool at_read_eof() const { return read_index_ >= read_count_; } |
135 // a EOF MockRead or MockWrite, which is a zero-length Read or Write. | 142 bool at_write_eof() const { return write_index_ >= write_count_; } |
136 // The test can then call at_read_eof() or at_write_eof() to verify that | |
137 // all data has been consumed. | |
138 bool at_read_eof() const { return reads_[read_index_].data_len == 0; } | |
139 bool at_write_eof() const { return writes_[write_index_].data_len == 0; } | |
140 | 143 |
141 private: | 144 private: |
142 MockRead* reads_; | 145 MockRead* reads_; |
143 int read_index_; | 146 size_t read_index_; |
| 147 size_t read_count_; |
144 MockWrite* writes_; | 148 MockWrite* writes_; |
145 int write_index_; | 149 size_t write_index_; |
| 150 size_t write_count_; |
146 | 151 |
147 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); | 152 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); |
148 }; | 153 }; |
149 | 154 |
150 // SocketDataProvider which can make decisions about next mock reads based on | 155 // SocketDataProvider which can make decisions about next mock reads based on |
151 // received writes. It can also be used to enforce order of operations, for | 156 // received writes. It can also be used to enforce order of operations, for |
152 // example that tested code must send the "Hello!" message before receiving | 157 // example that tested code must send the "Hello!" message before receiving |
153 // response. This is useful for testing conversation-like protocols like FTP. | 158 // response. This is useful for testing conversation-like protocols like FTP. |
154 class DynamicSocketDataProvider : public SocketDataProvider { | 159 class DynamicSocketDataProvider : public SocketDataProvider { |
155 public: | 160 public: |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 void ReleaseAllConnections(KeepAlive keep_alive); | 449 void ReleaseAllConnections(KeepAlive keep_alive); |
445 | 450 |
446 ScopedVector<TestSocketRequest> requests_; | 451 ScopedVector<TestSocketRequest> requests_; |
447 std::vector<TestSocketRequest*> request_order_; | 452 std::vector<TestSocketRequest*> request_order_; |
448 size_t completion_count_; | 453 size_t completion_count_; |
449 }; | 454 }; |
450 | 455 |
451 } // namespace net | 456 } // namespace net |
452 | 457 |
453 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 458 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
OLD | NEW |