| 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 <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 struct MockWriteResult { | 61 struct MockWriteResult { |
| 62 MockWriteResult(bool async, int result) : async(async), result(result) {} | 62 MockWriteResult(bool async, int result) : async(async), result(result) {} |
| 63 | 63 |
| 64 bool async; | 64 bool async; |
| 65 int result; | 65 int result; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 class MockSocket { | 68 class MockSocket { |
| 69 public: | 69 public: |
| 70 MockSocket() : unexpected_read_(true, ERR_UNEXPECTED) { | 70 MockSocket() {} |
| 71 } | |
| 72 | 71 |
| 73 virtual ~MockSocket() {} | 72 virtual ~MockSocket() {} |
| 74 virtual MockRead* GetNextRead() = 0; | 73 virtual MockRead GetNextRead() = 0; |
| 75 virtual MockWriteResult OnWrite(const std::string& data) = 0; | 74 virtual MockWriteResult OnWrite(const std::string& data) = 0; |
| 76 virtual void Reset() = 0; | 75 virtual void Reset() = 0; |
| 77 | 76 |
| 78 MockConnect connect_data() const { return connect_; } | 77 MockConnect connect_data() const { return connect_; } |
| 79 | 78 |
| 80 protected: | |
| 81 MockRead* unexpected_read() { return &unexpected_read_; } | |
| 82 | |
| 83 private: | 79 private: |
| 84 MockRead unexpected_read_; | |
| 85 MockConnect connect_; | 80 MockConnect connect_; |
| 86 | 81 |
| 87 DISALLOW_COPY_AND_ASSIGN(MockSocket); | 82 DISALLOW_COPY_AND_ASSIGN(MockSocket); |
| 88 }; | 83 }; |
| 89 | 84 |
| 90 // MockSocket which responds based on static tables of mock reads and writes. | 85 // MockSocket which responds based on static tables of mock reads and writes. |
| 91 class StaticMockSocket : public MockSocket { | 86 class StaticMockSocket : public MockSocket { |
| 92 public: | 87 public: |
| 93 StaticMockSocket() : reads_(NULL), read_index_(0), | 88 StaticMockSocket() : reads_(NULL), read_index_(0), |
| 94 writes_(NULL), write_index_(0) {} | 89 writes_(NULL), write_index_(0) {} |
| 95 StaticMockSocket(MockRead* r, MockWrite* w) : reads_(r), read_index_(0), | 90 StaticMockSocket(MockRead* r, MockWrite* w) : reads_(r), read_index_(0), |
| 96 writes_(w), write_index_(0) {} | 91 writes_(w), write_index_(0) {} |
| 97 | 92 |
| 98 // MockSocket methods: | 93 // MockSocket methods: |
| 99 virtual MockRead* GetNextRead(); | 94 virtual MockRead GetNextRead(); |
| 100 virtual MockWriteResult OnWrite(const std::string& data); | 95 virtual MockWriteResult OnWrite(const std::string& data); |
| 101 virtual void Reset(); | 96 virtual void Reset(); |
| 102 | 97 |
| 103 private: | 98 private: |
| 104 MockRead* reads_; | 99 MockRead* reads_; |
| 105 int read_index_; | 100 int read_index_; |
| 106 MockWrite* writes_; | 101 MockWrite* writes_; |
| 107 int write_index_; | 102 int write_index_; |
| 108 | 103 |
| 109 DISALLOW_COPY_AND_ASSIGN(StaticMockSocket); | 104 DISALLOW_COPY_AND_ASSIGN(StaticMockSocket); |
| 110 }; | 105 }; |
| 111 | 106 |
| 112 // MockSocket which can make decisions about next mock reads based on | 107 // MockSocket which can make decisions about next mock reads based on |
| 113 // received writes. It can also be used to enforce order of operations, | 108 // received writes. It can also be used to enforce order of operations, |
| 114 // for example that tested code must send the "Hello!" message before | 109 // for example that tested code must send the "Hello!" message before |
| 115 // receiving response. This is useful for testing conversation-like | 110 // receiving response. This is useful for testing conversation-like |
| 116 // protocols like FTP. | 111 // protocols like FTP. |
| 117 class DynamicMockSocket : public MockSocket { | 112 class DynamicMockSocket : public MockSocket { |
| 118 public: | 113 public: |
| 119 DynamicMockSocket(); | 114 DynamicMockSocket(); |
| 120 | 115 |
| 121 // MockSocket methods: | 116 // MockSocket methods: |
| 122 virtual MockRead* GetNextRead(); | 117 virtual MockRead GetNextRead(); |
| 123 virtual MockWriteResult OnWrite(const std::string& data) = 0; | 118 virtual MockWriteResult OnWrite(const std::string& data) = 0; |
| 124 virtual void Reset(); | 119 virtual void Reset(); |
| 125 | 120 |
| 121 int short_read_limit() const { return short_read_limit_; } |
| 122 void set_short_read_limit(int limit) { short_read_limit_ = limit; } |
| 123 |
| 126 protected: | 124 protected: |
| 127 // The next time there is a read from this socket, it will return |data|. | 125 // The next time there is a read from this socket, it will return |data|. |
| 128 // Before calling SimulateRead next time, the previous data must be consumed. | 126 // Before calling SimulateRead next time, the previous data must be consumed. |
| 129 void SimulateRead(const char* data); | 127 void SimulateRead(const char* data); |
| 130 | 128 |
| 131 private: | 129 private: |
| 132 MockRead read_; | 130 MockRead read_; |
| 133 bool has_read_; | 131 bool has_read_; |
| 134 bool consumed_read_; | 132 bool consumed_read_; |
| 135 | 133 |
| 134 // Max number of bytes we will read at a time. 0 means no limit. |
| 135 int short_read_limit_; |
| 136 |
| 136 DISALLOW_COPY_AND_ASSIGN(DynamicMockSocket); | 137 DISALLOW_COPY_AND_ASSIGN(DynamicMockSocket); |
| 137 }; | 138 }; |
| 138 | 139 |
| 139 // MockSSLSockets only need to keep track of the return code from calls to | 140 // MockSSLSockets only need to keep track of the return code from calls to |
| 140 // Connect(). | 141 // Connect(). |
| 141 struct MockSSLSocket { | 142 struct MockSSLSocket { |
| 142 MockSSLSocket(bool async, int result) : connect(async, result) { } | 143 MockSSLSocket(bool async, int result) : connect(async, result) { } |
| 143 | 144 |
| 144 MockConnect connect; | 145 MockConnect connect; |
| 145 }; | 146 }; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 const SSLConfig& ssl_config); | 196 const SSLConfig& ssl_config); |
| 196 | 197 |
| 197 private: | 198 private: |
| 198 MockSocketArray<MockSocket> mock_sockets_; | 199 MockSocketArray<MockSocket> mock_sockets_; |
| 199 MockSocketArray<MockSSLSocket> mock_ssl_sockets_; | 200 MockSocketArray<MockSSLSocket> mock_ssl_sockets_; |
| 200 }; | 201 }; |
| 201 | 202 |
| 202 } // namespace net | 203 } // namespace net |
| 203 | 204 |
| 204 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 205 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |