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/base/mime_sniffer.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Finds the line break in |input|, removes every thing up to and including the |
| 18 // line break from |input|, and returns everything up to the line break as a |
| 19 // string. |
| 20 std::string GetNextArgument(base::StringPiece* input) { |
| 21 base::StringPiece::size_type argument_end = input->find('\n'); |
| 22 if (argument_end == base::StringPiece::npos) |
| 23 argument_end = input->size(); |
| 24 base::StringPiece argument = input->substr(0, argument_end); |
| 25 *input = input->substr(argument_end + 1); |
| 26 return argument.as_string(); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 // Fuzzer for the two main mime sniffing functions: |
| 32 // SniffMimeType and SniffMimeTypeFromLocalData. |
| 33 // |
| 34 // Breaks |data| up into 3 substrings: URL, MIME type hint, and content, and |
| 35 // passes them to the MIME sniffing functions (SniffMimeTypeFromLocalData |
| 36 // does not take all 3 arguments). The first two substrings are each on their |
| 37 // own line, and content is everything after them. Since neither URLs nor |
| 38 // content-encoding headers can have line breaks, this doesn't reduce coverage. |
| 39 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 40 base::StringPiece input(reinterpret_cast<const char*>(data), size); |
| 41 GURL url(GetNextArgument(&input)); |
| 42 |
| 43 std::string mime_type_hint = GetNextArgument(&input); |
| 44 |
| 45 std::string result; |
| 46 net::SniffMimeType(input.data(), input.length(), url, mime_type_hint, |
| 47 &result); |
| 48 |
| 49 net::SniffMimeTypeFromLocalData(input.data(), input.length(), &result); |
| 50 |
| 51 return 0; |
| 52 } |
OLD | NEW |