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 #include "net/socket/fuzzed_socket.h" | 5 #include "net/socket/fuzzed_socket.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 bool sync; | 51 bool sync; |
52 int result; | 52 int result; |
53 | 53 |
54 if (net_error_ != OK) { | 54 if (net_error_ != OK) { |
55 // If an error has already been generated, use it to determine what to do. | 55 // If an error has already been generated, use it to determine what to do. |
56 result = net_error_; | 56 result = net_error_; |
57 sync = !error_pending_; | 57 sync = !error_pending_; |
58 } else { | 58 } else { |
59 // Otherwise, use |data_provider_|. | 59 // Otherwise, use |data_provider_|. |
60 sync = data_provider_->ConsumeBool(); | 60 sync = data_provider_->ConsumeBool(); |
61 result = data_provider_->ConsumeUint8(); | 61 std::string data = data_provider_->ConsumeRandomLengthString(buf_len); |
62 if (result > buf_len) | 62 result = data.size(); |
63 result = buf_len; | |
64 | 63 |
65 if (result > 0) { | 64 if (result > 0) { |
66 std::string data = data_provider_->ConsumeBytes(result); | |
67 result = data.size(); | |
68 std::copy(data.data(), data.data() + result, buf->data()); | 65 std::copy(data.data(), data.data() + result, buf->data()); |
69 } | 66 } else { |
70 | 67 result = ConsumeReadWriteErrorFromData(); |
71 if (result == 0) { | 68 net_error_ = result; |
72 net_error_ = ConsumeReadWriteErrorFromData(); | |
73 result = net_error_; | |
74 if (!sync) | 69 if (!sync) |
75 error_pending_ = true; | 70 error_pending_ = true; |
76 } | 71 } |
77 } | 72 } |
78 | 73 |
79 // Graceful close of a socket returns OK, at least in theory. This doesn't | 74 // Graceful close of a socket returns OK, at least in theory. This doesn't |
80 // perfectly reflect real socket behavior, but close enough. | 75 // perfectly reflect real socket behavior, but close enough. |
81 if (result == ERR_CONNECTION_CLOSED) | 76 if (result == ERR_CONNECTION_CLOSED) |
82 result = 0; | 77 result = 0; |
83 | 78 |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 } | 267 } |
273 callback.Run(result); | 268 callback.Run(result); |
274 } | 269 } |
275 | 270 |
276 void FuzzedSocket::OnConnectComplete(const CompletionCallback& callback, | 271 void FuzzedSocket::OnConnectComplete(const CompletionCallback& callback, |
277 int result) { | 272 int result) { |
278 CHECK(connect_pending_); | 273 CHECK(connect_pending_); |
279 connect_pending_ = false; | 274 connect_pending_ = false; |
280 if (result < 0) | 275 if (result < 0) |
281 error_pending_ = false; | 276 error_pending_ = false; |
| 277 net_error_ = result; |
282 callback.Run(result); | 278 callback.Run(result); |
283 } | 279 } |
284 | 280 |
285 } // namespace net | 281 } // namespace net |
OLD | NEW |