Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: net/socket/fuzzed_socket.h

Issue 1917503002: URLRequest fuzzer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fuzz
Patch Set: Add missing include Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/net_errors.h" 15 #include "net/base/net_errors.h"
15 #include "net/log/net_log.h" 16 #include "net/log/net_log.h"
16 #include "net/socket/stream_socket.h" 17 #include "net/socket/stream_socket.h"
17 18
18 namespace net { 19 namespace net {
19 20
21 class FuzzedDataProvider;
22 class IPEndPoint;
20 class IOBuffer; 23 class IOBuffer;
21 24
22 // A StreamSocket that uses a single block of data to generate responses for use 25 // A StreamSocket that uses a FuzzedDataProvider to generate responses. Writes
23 // with fuzzers. Writes can succeed synchronously or asynchronously, can write 26 // can succeed synchronously or asynchronously, can write some or all of the
24 // some or all of the provided data, and can fail with several different errors. 27 // provided data, and can fail with several different errors. Reads can do the
25 // Reads can do the same, but the read data is also generated from the initial 28 // same, but the read data is also generated from the FuzzedDataProvider. The
26 // input data. The number of bytes written/read from a single call is currently 29 // number of bytes written/read from a single call is currently capped at 255
27 // capped at 127 bytes. 30 // bytes.
28 // 31 //
29 // Reads and writes are executed independently of one another, so to guarantee 32 // Reads and writes are executed independently of one another, so to guarantee
30 // the fuzzer behaves the same across repeated runs with the same input, the 33 // the fuzzer behaves the same across repeated runs with the same input, the
31 // reads and writes must be done in a deterministic order and for a 34 // reads and writes must be done in a deterministic order and for a
32 // deterministic number of bytes, every time the fuzzer is run with the same 35 // deterministic number of bytes, every time the fuzzer is run with the same
33 // data. 36 // data.
34 class FuzzedSocket : public StreamSocket { 37 class FuzzedSocket : public StreamSocket {
35 public: 38 public:
36 // |data| must be of length |data_size| and is used as to determine behavior 39 // |data_provider| is used as to determine behavior of the FuzzedSocket. It
37 // of the FuzzedSocket. It must remain valid until the FuzzedSocket is 40 // must remain valid until after the FuzzedSocket is destroyed.
38 // destroyed. 41 FuzzedSocket(FuzzedDataProvider* data_provider, net::NetLog* net_log);
39 FuzzedSocket(const uint8_t* data,
40 size_t data_size,
41 const BoundNetLog& bound_net_log);
42 ~FuzzedSocket() override; 42 ~FuzzedSocket() override;
43 43
44 // 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
46 // false, Connect() succeeds synchronously. Defaults to false.
47 void set_fuzz_connect_result(bool fuzz_connect_result) {
48 fuzz_connect_result_ = fuzz_connect_result;
49 }
50
51 // Sets the remote address the socket claims to be using.
52 void set_remote_address(const IPEndPoint& remote_address) {
53 remote_address_ = remote_address;
54 }
55
44 // Socket implementation: 56 // Socket implementation:
45 int Read(IOBuffer* buf, 57 int Read(IOBuffer* buf,
46 int buf_len, 58 int buf_len,
47 const CompletionCallback& callback) override; 59 const CompletionCallback& callback) override;
48 int Write(IOBuffer* buf, 60 int Write(IOBuffer* buf,
49 int buf_len, 61 int buf_len,
50 const CompletionCallback& callback) override; 62 const CompletionCallback& callback) override;
51 int SetReceiveBufferSize(int32_t size) override; 63 int SetReceiveBufferSize(int32_t size) override;
52 int SetSendBufferSize(int32_t size) override; 64 int SetSendBufferSize(int32_t size) override;
53 65
(...skipping 11 matching lines...) Expand all
65 void EnableTCPFastOpenIfSupported() override; 77 void EnableTCPFastOpenIfSupported() override;
66 bool WasNpnNegotiated() const override; 78 bool WasNpnNegotiated() const override;
67 NextProto GetNegotiatedProtocol() const override; 79 NextProto GetNegotiatedProtocol() const override;
68 bool GetSSLInfo(SSLInfo* ssl_info) override; 80 bool GetSSLInfo(SSLInfo* ssl_info) override;
69 void GetConnectionAttempts(ConnectionAttempts* out) const override; 81 void GetConnectionAttempts(ConnectionAttempts* out) const override;
70 void ClearConnectionAttempts() override; 82 void ClearConnectionAttempts() override;
71 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; 83 void AddConnectionAttempts(const ConnectionAttempts& attempts) override;
72 int64_t GetTotalReceivedBytes() const override; 84 int64_t GetTotalReceivedBytes() const override;
73 85
74 private: 86 private:
75 // Returns a uint8_t removed from the back of |data_|. Bytes read from the
76 // socket are taken from the front of the stream, so this will keep read bytes
77 // more consistent between test runs. If no data is left, returns 0.
78 uint8_t ConsumeUint8FromData();
79
80 // Returns a net::Error that can be returned by a read or a write. Reads and 87 // Returns a net::Error that can be returned by a read or a write. Reads and
81 // writes return basically the same set of errors, at the TCP socket layer. 88 // writes return basically the same set of errors, at the TCP socket layer.
82 // Which error is determined by a call to ConsumeUint8FromData().
83 Error ConsumeReadWriteErrorFromData(); 89 Error ConsumeReadWriteErrorFromData();
84 90
85 void OnReadComplete(const CompletionCallback& callback, int result); 91 void OnReadComplete(const CompletionCallback& callback, int result);
86 void OnWriteComplete(const CompletionCallback& callback, int result); 92 void OnWriteComplete(const CompletionCallback& callback, int result);
93 void OnConnectComplete(const CompletionCallback& callback, int result);
87 94
88 // The unconsumed portion of the input data that |this| was created with. 95 FuzzedDataProvider* data_provider_;
89 base::StringPiece data_;
90 96
97 // 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
99 // or asynchronously.
100 bool fuzz_connect_result_ = false;
101
102 bool connect_pending_ = false;
91 bool read_pending_ = false; 103 bool read_pending_ = false;
92 bool write_pending_ = false; 104 bool write_pending_ = false;
93 105
94 // This is true when the first callback returning an error is pending in the 106 // This is true when the first callback returning an error is pending in the
95 // message queue. If true, the socket acts like it's connected until that task 107 // message queue. If true, the socket acts like it's connected until that task
96 // is run (Or Disconnect() is called), and reads / writes will return the same 108 // is run (Or Disconnect() is called), and reads / writes will return the same
97 // error asynchronously, until it becomes false, at which point they'll return 109 // error asynchronously, until it becomes false, at which point they'll return
98 // it synchronously. 110 // it synchronously.
99 bool error_pending_ = false; 111 bool error_pending_ = false;
100 // If this is not OK, all reads/writes will fail with this error. 112 // If this is not OK, all reads/writes will fail with this error.
101 int net_error_ = ERR_CONNECTION_CLOSED; 113 int net_error_ = ERR_CONNECTION_CLOSED;
102 114
103 int64_t total_bytes_read_ = 0; 115 int64_t total_bytes_read_ = 0;
104 int64_t total_bytes_written_ = 0; 116 int64_t total_bytes_written_ = 0;
105 117
106 BoundNetLog bound_net_log_; 118 BoundNetLog bound_net_log_;
107 119
120 IPEndPoint remote_address_;
121
108 base::WeakPtrFactory<FuzzedSocket> weak_factory_; 122 base::WeakPtrFactory<FuzzedSocket> weak_factory_;
109 123
110 DISALLOW_COPY_AND_ASSIGN(FuzzedSocket); 124 DISALLOW_COPY_AND_ASSIGN(FuzzedSocket);
111 }; 125 };
112 126
113 } // namespace net 127 } // namespace net
114 128
115 #endif // NET_SOCKET_FUZZED_SOCKET_H 129 #endif // NET_SOCKET_FUZZED_SOCKET_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698