xref: /aosp_15_r20/external/cronet/net/base/data_url_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
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/data_url.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <fuzzer/FuzzedDataProvider.h>
11 
12 #include <string>
13 
14 #include "base/check_op.h"
15 #include "base/memory/ref_counted.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_response_headers.h"
18 #include "url/gurl.h"
19 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)20 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
21   FuzzedDataProvider provider(data, size);
22   std::string method = provider.ConsumeRandomLengthString(256);
23   // Don't restrict to data URLs.
24   GURL url(provider.ConsumeRemainingBytesAsString());
25 
26   std::string mime_type;
27   std::string charset;
28   std::string body;
29 
30   std::string mime_type2;
31   std::string charset2;
32   std::string body2;
33   scoped_refptr<net::HttpResponseHeaders> headers;
34 
35   // Run the URL through DataURL::Parse() and DataURL::BuildResponse(). They
36   // should succeed and fail in exactly the same cases.
37   CHECK_EQ(net::DataURL::Parse(url, &mime_type, &charset, &body),
38            net::OK == net::DataURL::BuildResponse(url, method, &mime_type2,
39                                                   &charset2, &body2, &headers));
40   return 0;
41 }
42