data_url_fuzzer.cc 1.3 KB

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