Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/http_stream_parser.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "net/base/address_list.h" | |
| 16 #include "net/base/io_buffer.h" | |
| 17 #include "net/base/net_errors.h" | |
| 18 #include "net/base/test_completion_callback.h" | |
| 19 #include "net/http/http_request_headers.h" | |
| 20 #include "net/http/http_request_info.h" | |
| 21 #include "net/http/http_response_info.h" | |
| 22 #include "net/log/net_log.h" | |
| 23 #include "net/log/test_net_log.h" | |
| 24 #include "net/socket/client_socket_handle.h" | |
| 25 #include "net/socket/socket_test_util.h" | |
| 26 #include "url/gurl.h" | |
| 27 | |
| 28 // Entry point for LibFuzzer. | |
| 29 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
|
eroman
2016/03/25 20:43:18
I was advised to include:
#include <stddef.h>
#i
mmenke
2016/03/25 21:33:37
Done.
| |
| 30 // Needed for thread checks and waits. | |
| 31 base::MessageLoopForIO message_loop; | |
| 32 | |
| 33 // This really shouldn't happen, but just in case... | |
| 34 if (size > std::numeric_limits<int>::max()) | |
|
eroman
2016/03/25 20:43:18
(This certainly will not happen, there is a limit
mmenke
2016/03/25 21:33:37
No longer relevant (Though if it shouldn't happen,
| |
| 35 size = std::numeric_limits<int>::max(); | |
|
eroman
2016/03/25 20:43:18
Since you are using int throughout the rest, I wou
mmenke
2016/03/25 21:33:37
I was being paranoid: 2*size can overflow, even i
| |
| 36 | |
| 37 const net::IoMode kIoModes[] = { | |
| 38 net::ASYNC, net::SYNCHRONOUS, | |
| 39 }; | |
| 40 | |
| 41 // Try both synchronous and async IO, as they follow slightly different | |
| 42 // paths. | |
| 43 for (net::IoMode io_mode : kIoModes) { | |
|
eroman
2016/03/25 20:43:18
optional nit: auto ?
mmenke
2016/03/25 21:33:37
Done. I tend to avoid auto except for ugly types,
| |
| 44 net::MockWrite writes[] = { | |
| 45 net::MockWrite(net::ASYNC, 0, "GET / HTTP/1.1\r\n\r\n"), | |
| 46 }; | |
| 47 | |
| 48 // Split the response into three reads of about equal size. Hopefully this | |
|
eroman
2016/03/25 20:43:18
Not sure if you have seen this:
https://code.goog
mmenke
2016/03/25 21:33:37
That doesn't quite work here. For correct chunk-e
| |
| 49 // will allow for catching issues related to multiple body or header reads. | |
| 50 int read_offset2 = static_cast<int>(size / 3); | |
| 51 int read_offset3 = static_cast<int>(2 * size / 3); | |
| 52 net::MockRead reads[] = { | |
| 53 net::MockRead(io_mode, reinterpret_cast<const char*>(data), | |
| 54 read_offset2, 1), | |
| 55 net::MockRead(io_mode, | |
| 56 reinterpret_cast<const char*>(data) + read_offset2, | |
| 57 read_offset3 - read_offset2, 2), | |
| 58 net::MockRead(io_mode, | |
| 59 reinterpret_cast<const char*>(data) + read_offset3, | |
| 60 static_cast<int>(size) - read_offset3, 3), | |
| 61 net::MockRead(io_mode, net::ERR_CONNECTION_CLOSED, 4), | |
| 62 }; | |
| 63 net::SequencedSocketData socket_data(reads, arraysize(reads), writes, | |
| 64 arraysize(writes)); | |
| 65 socket_data.set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK)); | |
| 66 | |
| 67 scoped_ptr<net::MockTCPClientSocket> socket(new net::MockTCPClientSocket( | |
| 68 net::AddressList(), nullptr, &socket_data)); | |
| 69 | |
| 70 net::TestCompletionCallback callback; | |
| 71 CHECK_EQ(net::OK, socket->Connect(callback.callback())); | |
| 72 | |
| 73 net::ClientSocketHandle socket_handle; | |
| 74 socket_handle.SetSocket(std::move(socket)); | |
| 75 | |
| 76 net::HttpRequestInfo request_info; | |
| 77 request_info.method = "GET"; | |
| 78 request_info.url = GURL("http://localhost/"); | |
| 79 | |
| 80 scoped_refptr<net::GrowableIOBuffer> read_buffer( | |
| 81 new net::GrowableIOBuffer()); | |
| 82 // Use a NetLog that listens to events, to get coverage of logging | |
| 83 // callbacks. | |
| 84 net::BoundTestNetLog net_log; | |
| 85 net::HttpStreamParser parser(&socket_handle, &request_info, | |
| 86 read_buffer.get(), net_log.bound()); | |
| 87 | |
| 88 net::HttpResponseInfo response_info; | |
| 89 int result = | |
| 90 parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(), | |
| 91 &response_info, callback.callback()); | |
| 92 CHECK_EQ(net::OK, callback.GetResult(result)); | |
| 93 | |
| 94 result = parser.ReadResponseHeaders(callback.callback()); | |
| 95 result = callback.GetResult(result); | |
| 96 | |
| 97 while (result != net::OK) { | |
|
eroman
2016/03/25 20:43:18
Can ReadResponseBody() not return an error in this
mmenke
2016/03/25 21:33:37
This is a bug. This should be while(result > 0).
| |
| 98 scoped_refptr<net::IOBufferWithSize> io_buffer( | |
| 99 new net::IOBufferWithSize(size)); | |
| 100 result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(), | |
| 101 callback.callback()); | |
| 102 result = callback.GetResult(result); | |
| 103 } | |
| 104 } | |
| 105 return 0; | |
| 106 } | |
| OLD | NEW |