OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_FUZZED_SOCKET_H_ | 5 #ifndef NET_SOCKET_FUZZED_SOCKET_H_ |
6 #define NET_SOCKET_FUZZED_SOCKET_H_ | 6 #define NET_SOCKET_FUZZED_SOCKET_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
17 #include "net/socket/stream_socket.h" | 17 #include "net/socket/stream_socket.h" |
18 | 18 |
| 19 namespace base { |
| 20 class FuzzedDataProvider; |
| 21 } |
| 22 |
19 namespace net { | 23 namespace net { |
20 | 24 |
21 class FuzzedDataProvider; | |
22 class IPEndPoint; | 25 class IPEndPoint; |
23 class IOBuffer; | 26 class IOBuffer; |
24 | 27 |
25 // A StreamSocket that uses a FuzzedDataProvider to generate responses. Writes | 28 // A StreamSocket that uses a FuzzedDataProvider to generate responses. Writes |
26 // can succeed synchronously or asynchronously, can write some or all of the | 29 // can succeed synchronously or asynchronously, can write some or all of the |
27 // provided data, and can fail with several different errors. Reads can do the | 30 // provided data, and can fail with several different errors. Reads can do the |
28 // same, but the read data is also generated from the FuzzedDataProvider. The | 31 // same, but the read data is also generated from the FuzzedDataProvider. The |
29 // number of bytes written/read from a single call is currently capped at 255 | 32 // number of bytes written/read from a single call is currently capped at 255 |
30 // bytes. | 33 // bytes. |
31 // | 34 // |
32 // Reads and writes are executed independently of one another, so to guarantee | 35 // Reads and writes are executed independently of one another, so to guarantee |
33 // the fuzzer behaves the same across repeated runs with the same input, the | 36 // the fuzzer behaves the same across repeated runs with the same input, the |
34 // reads and writes must be done in a deterministic order and for a | 37 // reads and writes must be done in a deterministic order and for a |
35 // deterministic number of bytes, every time the fuzzer is run with the same | 38 // deterministic number of bytes, every time the fuzzer is run with the same |
36 // data. | 39 // data. |
37 class FuzzedSocket : public StreamSocket { | 40 class FuzzedSocket : public StreamSocket { |
38 public: | 41 public: |
39 // |data_provider| is used as to determine behavior of the FuzzedSocket. It | 42 // |data_provider| is used as to determine behavior of the FuzzedSocket. It |
40 // must remain valid until after the FuzzedSocket is destroyed. | 43 // must remain valid until after the FuzzedSocket is destroyed. |
41 FuzzedSocket(FuzzedDataProvider* data_provider, net::NetLog* net_log); | 44 FuzzedSocket(base::FuzzedDataProvider* data_provider, net::NetLog* net_log); |
42 ~FuzzedSocket() override; | 45 ~FuzzedSocket() override; |
43 | 46 |
44 // If set to true, the socket will fuzz the result of the Connect() call. | 47 // If set to true, the socket will fuzz the result of the Connect() call. |
45 // It can fail or succeed, and return synchronously or asynchronously. If | 48 // It can fail or succeed, and return synchronously or asynchronously. If |
46 // false, Connect() succeeds synchronously. Defaults to false. | 49 // false, Connect() succeeds synchronously. Defaults to false. |
47 void set_fuzz_connect_result(bool fuzz_connect_result) { | 50 void set_fuzz_connect_result(bool fuzz_connect_result) { |
48 fuzz_connect_result_ = fuzz_connect_result; | 51 fuzz_connect_result_ = fuzz_connect_result; |
49 } | 52 } |
50 | 53 |
51 // Sets the remote address the socket claims to be using. | 54 // Sets the remote address the socket claims to be using. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 88 |
86 private: | 89 private: |
87 // Returns a net::Error that can be returned by a read or a write. Reads and | 90 // Returns a net::Error that can be returned by a read or a write. Reads and |
88 // writes return basically the same set of errors, at the TCP socket layer. | 91 // writes return basically the same set of errors, at the TCP socket layer. |
89 Error ConsumeReadWriteErrorFromData(); | 92 Error ConsumeReadWriteErrorFromData(); |
90 | 93 |
91 void OnReadComplete(const CompletionCallback& callback, int result); | 94 void OnReadComplete(const CompletionCallback& callback, int result); |
92 void OnWriteComplete(const CompletionCallback& callback, int result); | 95 void OnWriteComplete(const CompletionCallback& callback, int result); |
93 void OnConnectComplete(const CompletionCallback& callback, int result); | 96 void OnConnectComplete(const CompletionCallback& callback, int result); |
94 | 97 |
95 FuzzedDataProvider* data_provider_; | 98 base::FuzzedDataProvider* data_provider_; |
96 | 99 |
97 // If true, the result of the Connect() call is fuzzed - it can succeed or | 100 // If true, the result of the Connect() call is fuzzed - it can succeed or |
98 // fail with a variety of connection errors, and it can complete synchronously | 101 // fail with a variety of connection errors, and it can complete synchronously |
99 // or asynchronously. | 102 // or asynchronously. |
100 bool fuzz_connect_result_ = false; | 103 bool fuzz_connect_result_ = false; |
101 | 104 |
102 bool connect_pending_ = false; | 105 bool connect_pending_ = false; |
103 bool read_pending_ = false; | 106 bool read_pending_ = false; |
104 bool write_pending_ = false; | 107 bool write_pending_ = false; |
105 | 108 |
(...skipping 14 matching lines...) Expand all Loading... |
120 IPEndPoint remote_address_; | 123 IPEndPoint remote_address_; |
121 | 124 |
122 base::WeakPtrFactory<FuzzedSocket> weak_factory_; | 125 base::WeakPtrFactory<FuzzedSocket> weak_factory_; |
123 | 126 |
124 DISALLOW_COPY_AND_ASSIGN(FuzzedSocket); | 127 DISALLOW_COPY_AND_ASSIGN(FuzzedSocket); |
125 }; | 128 }; |
126 | 129 |
127 } // namespace net | 130 } // namespace net |
128 | 131 |
129 #endif // NET_SOCKET_FUZZED_SOCKET_H_ | 132 #endif // NET_SOCKET_FUZZED_SOCKET_H_ |
OLD | NEW |