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/filter/fuzzed_source_stream.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/test/fuzzed_data_provider.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Common net error codes that can be returned by a SourceStream. | |
| 19 const Error kReadErrors[] = {OK, ERR_FAILED, ERR_CONTENT_DECODING_FAILED}; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 FuzzedSourceStream::FuzzedSourceStream(base::FuzzedDataProvider* data_provider) | |
| 24 : SourceStream(SourceStream::TYPE_NONE), | |
| 25 data_provider_(data_provider), | |
| 26 read_pending_(false) {} | |
| 27 | |
| 28 FuzzedSourceStream::~FuzzedSourceStream() { | |
| 29 DCHECK(!read_pending_); | |
| 30 } | |
| 31 | |
| 32 int FuzzedSourceStream::Read(IOBuffer* buf, | |
| 33 int buf_len, | |
| 34 const CompletionCallback& callback) { | |
| 35 DCHECK(!read_pending_); | |
|
mmenke
2016/10/27 19:31:11
Maybe DCHECK that Read isn't called after we're re
xunjieli
2016/10/28 15:21:47
Done.
| |
| 36 | |
| 37 bool sync = data_provider_->ConsumeBool(); | |
| 38 int result = data_provider_->ConsumeUint8(); | |
|
mmenke
2016/10/27 19:31:11
I suggest just doing:
int result = data_provider_
xunjieli
2016/10/28 15:21:47
Done.
| |
| 39 | |
| 40 if (result > buf_len) | |
| 41 result = buf_len; | |
| 42 | |
| 43 if (result > 0) { | |
| 44 std::string data = data_provider_->ConsumeBytes(result); | |
| 45 result = data.size(); | |
| 46 std::copy(data.data(), data.data() + result, buf->data()); | |
|
mmenke
2016/10/27 19:31:11
I think we need <algorithm> for std::copy?
mmenke
2016/10/27 19:31:11
optional: Could be a little more rigorous by not
xunjieli
2016/10/28 15:21:47
Done.
xunjieli
2016/10/28 15:21:47
Done.
| |
| 47 } | |
| 48 if (result == 0) | |
| 49 result = data_provider_->PickValueInArray(kReadErrors); | |
| 50 | |
| 51 if (sync) | |
| 52 return result; | |
| 53 | |
| 54 read_pending_ = true; | |
| 55 // |this| is owned by the caller so use base::Unretained is safe. | |
| 56 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 57 FROM_HERE, base::Bind(&FuzzedSourceStream::OnReadComplete, | |
| 58 base::Unretained(this), callback, result)); | |
| 59 return ERR_IO_PENDING; | |
| 60 } | |
| 61 | |
| 62 std::string FuzzedSourceStream::Description() const { | |
| 63 return ""; | |
| 64 } | |
| 65 | |
| 66 void FuzzedSourceStream::OnReadComplete(const CompletionCallback& callback, | |
| 67 int result) { | |
| 68 DCHECK(read_pending_); | |
| 69 | |
| 70 read_pending_ = false; | |
| 71 callback.Run(result); | |
| 72 } | |
| 73 | |
| 74 } // namespace net | |
| OLD | NEW |