Chromium Code Reviews| 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" | |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/test/fuzzed_data_provider.h" | 12 #include "base/test/fuzzed_data_provider.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/test/scoped_task_scheduler.h" |
| 13 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 14 #include "net/url_request/data_protocol_handler.h" | 15 #include "net/url_request/data_protocol_handler.h" |
| 15 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 16 #include "net/url_request/url_request_job_factory_impl.h" | 17 #include "net/url_request/url_request_job_factory_impl.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const size_t kMaxLengthForFuzzedRange = 32; | 22 const size_t kMaxLengthForFuzzedRange = 32; |
| 22 | 23 |
| 23 } // namespace | 24 } // namespace |
| 24 | 25 |
| 25 // 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 |
| 26 // 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 |
| 27 // 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 |
| 28 // the size of the IOBuffer to read data into. | 29 // the size of the IOBuffer to read data into. |
| 29 class URLRequestDataJobFuzzerHarness : public net::URLRequest::Delegate { | 30 class URLRequestDataJobFuzzerHarness : public net::URLRequest::Delegate { |
| 30 public: | 31 public: |
| 31 URLRequestDataJobFuzzerHarness() | 32 URLRequestDataJobFuzzerHarness() |
| 32 : context_(true), task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 33 : scoped_task_scheduler_(base::MessageLoop::current()), context_(true) { |
| 33 job_factory_.SetProtocolHandler( | 34 job_factory_.SetProtocolHandler( |
| 34 "data", base::MakeUnique<net::DataProtocolHandler>()); | 35 "data", base::MakeUnique<net::DataProtocolHandler>()); |
| 35 context_.set_job_factory(&job_factory_); | 36 context_.set_job_factory(&job_factory_); |
| 36 context_.Init(); | 37 context_.Init(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 static URLRequestDataJobFuzzerHarness* GetInstance() { | 40 static URLRequestDataJobFuzzerHarness* GetInstance() { |
| 40 return base::Singleton<URLRequestDataJobFuzzerHarness>::get(); | 41 return base::Singleton<URLRequestDataJobFuzzerHarness>::get(); |
| 41 } | 42 } |
| 42 | 43 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 base::RunLoop read_loop; | 89 base::RunLoop read_loop; |
| 89 read_loop_ = &read_loop; | 90 read_loop_ = &read_loop; |
| 90 request->Start(); | 91 request->Start(); |
| 91 read_loop.Run(); | 92 read_loop.Run(); |
| 92 read_loop_ = nullptr; | 93 read_loop_ = nullptr; |
| 93 return 0; | 94 return 0; |
| 94 } | 95 } |
| 95 | 96 |
| 96 void QuitLoop() { | 97 void QuitLoop() { |
| 97 DCHECK(read_loop_); | 98 DCHECK(read_loop_); |
| 98 task_runner_->PostTask(FROM_HERE, read_loop_->QuitClosure()); | 99 read_loop_->QuitWhenIdle(); |
|
fdoray
2017/02/14 18:24:30
QuitWhenIdle() quits the loop when there are no mo
xunjieli
2017/02/14 18:55:31
QuitWhenIdle() can be flaky in some cases. I know
fdoray
2017/02/16 15:08:59
Done.
| |
| 99 } | 100 } |
| 100 | 101 |
| 101 void ReadFromRequest(net::URLRequest* request) { | 102 void ReadFromRequest(net::URLRequest* request) { |
| 102 int bytes_read = 0; | 103 int bytes_read = 0; |
| 103 do { | 104 do { |
| 104 // If possible, pop the next read size. If none exists, then this should | 105 // If possible, pop the next read size. If none exists, then this should |
| 105 // be the last call to Read. | 106 // be the last call to Read. |
| 106 bool using_populated_read = read_lengths_.size() > 0; | 107 bool using_populated_read = read_lengths_.size() > 0; |
| 107 size_t read_size = 1; | 108 size_t read_size = 1; |
| 108 if (using_populated_read) { | 109 if (using_populated_read) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 if (bytes_read > 0) { | 149 if (bytes_read > 0) { |
| 149 ReadFromRequest(request); | 150 ReadFromRequest(request); |
| 150 } else { | 151 } else { |
| 151 QuitLoop(); | 152 QuitLoop(); |
| 152 } | 153 } |
| 153 } | 154 } |
| 154 | 155 |
| 155 private: | 156 private: |
| 156 friend struct base::DefaultSingletonTraits<URLRequestDataJobFuzzerHarness>; | 157 friend struct base::DefaultSingletonTraits<URLRequestDataJobFuzzerHarness>; |
| 157 | 158 |
| 159 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
| 158 net::TestURLRequestContext context_; | 160 net::TestURLRequestContext context_; |
| 159 net::URLRequestJobFactoryImpl job_factory_; | 161 net::URLRequestJobFactoryImpl job_factory_; |
| 160 std::vector<size_t> read_lengths_; | 162 std::vector<size_t> read_lengths_; |
| 161 scoped_refptr<net::IOBuffer> buf_; | 163 scoped_refptr<net::IOBuffer> buf_; |
| 162 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 163 base::RunLoop* read_loop_; | 164 base::RunLoop* read_loop_; |
| 164 | 165 |
| 165 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); | 166 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); |
| 166 }; | 167 }; |
| 167 | 168 |
| 168 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 169 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 169 // Using a static singleton test harness lets the test run ~3-4x faster. | 170 // Using a static singleton test harness lets the test run ~3-4x faster. |
| 170 return URLRequestDataJobFuzzerHarness::GetInstance() | 171 return URLRequestDataJobFuzzerHarness::GetInstance() |
| 171 ->CreateAndReadFromDataURLRequest(data, size); | 172 ->CreateAndReadFromDataURLRequest(data, size); |
| 172 } | 173 } |
| OLD | NEW |