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 <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
12 #include "base/test/fuzzed_data_provider.h" | 11 #include "base/test/fuzzed_data_provider.h" |
13 #include "base/test/scoped_task_scheduler.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
15 #include "net/http/http_request_headers.h" | 13 #include "net/http/http_request_headers.h" |
16 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 14 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
17 #include "net/url_request/data_protocol_handler.h" | 15 #include "net/url_request/data_protocol_handler.h" |
18 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
19 #include "net/url_request/url_request_job_factory_impl.h" | 17 #include "net/url_request/url_request_job_factory_impl.h" |
20 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
21 | 19 |
22 namespace { | 20 namespace { |
23 | 21 |
24 const size_t kMaxLengthForFuzzedRange = 32; | 22 const size_t kMaxLengthForFuzzedRange = 32; |
25 | 23 |
26 } // namespace | 24 } // namespace |
27 | 25 |
28 // This class tests creating and reading to completion a URLRequest with fuzzed | 26 // This class tests creating and reading to completion a URLRequest with fuzzed |
29 // input. The fuzzer provides a data: URL and optionally generates custom Range | 27 // input. The fuzzer provides a data: URL and optionally generates custom Range |
30 // headers. The amount of data read in each Read call is also fuzzed, as is | 28 // headers. The amount of data read in each Read call is also fuzzed, as is |
31 // the size of the IOBuffer to read data into. | 29 // the size of the IOBuffer to read data into. |
32 class URLRequestDataJobFuzzerHarness : public net::URLRequest::Delegate { | 30 class URLRequestDataJobFuzzerHarness : public net::URLRequest::Delegate { |
33 public: | 31 public: |
34 URLRequestDataJobFuzzerHarness() | 32 URLRequestDataJobFuzzerHarness() |
35 : scoped_task_scheduler_(base::MessageLoop::current()), | 33 : task_runner_(base::ThreadTaskRunnerHandle::Get()), context_(true) { |
36 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
37 context_(true) { | |
38 job_factory_.SetProtocolHandler( | 34 job_factory_.SetProtocolHandler( |
39 "data", base::MakeUnique<net::DataProtocolHandler>()); | 35 "data", base::MakeUnique<net::DataProtocolHandler>()); |
40 context_.set_job_factory(&job_factory_); | 36 context_.set_job_factory(&job_factory_); |
41 context_.Init(); | 37 context_.Init(); |
42 } | 38 } |
43 | 39 |
44 static URLRequestDataJobFuzzerHarness* GetInstance() { | 40 static URLRequestDataJobFuzzerHarness* GetInstance() { |
45 return base::Singleton<URLRequestDataJobFuzzerHarness>::get(); | 41 return base::Singleton<URLRequestDataJobFuzzerHarness>::get(); |
46 } | 42 } |
47 | 43 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 if (bytes_read > 0) { | 149 if (bytes_read > 0) { |
154 ReadFromRequest(request); | 150 ReadFromRequest(request); |
155 } else { | 151 } else { |
156 QuitLoop(); | 152 QuitLoop(); |
157 } | 153 } |
158 } | 154 } |
159 | 155 |
160 private: | 156 private: |
161 friend struct base::DefaultSingletonTraits<URLRequestDataJobFuzzerHarness>; | 157 friend struct base::DefaultSingletonTraits<URLRequestDataJobFuzzerHarness>; |
162 | 158 |
163 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
164 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 159 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
165 | 160 |
166 net::TestURLRequestContext context_; | 161 net::TestURLRequestContext context_; |
167 net::URLRequestJobFactoryImpl job_factory_; | 162 net::URLRequestJobFactoryImpl job_factory_; |
168 std::vector<size_t> read_lengths_; | 163 std::vector<size_t> read_lengths_; |
169 scoped_refptr<net::IOBuffer> buf_; | 164 scoped_refptr<net::IOBuffer> buf_; |
170 base::RunLoop* read_loop_; | 165 base::RunLoop* read_loop_; |
171 | 166 |
172 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); | 167 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); |
173 }; | 168 }; |
174 | 169 |
175 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 170 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
176 // Using a static singleton test harness lets the test run ~3-4x faster. | 171 // Using a static singleton test harness lets the test run ~3-4x faster. |
177 return URLRequestDataJobFuzzerHarness::GetInstance() | 172 return URLRequestDataJobFuzzerHarness::GetInstance() |
178 ->CreateAndReadFromDataURLRequest(data, size); | 173 ->CreateAndReadFromDataURLRequest(data, size); |
179 } | 174 } |
OLD | NEW |