url_pattern_fuzzer.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2021 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 <stddef.h>
  5. #include <stdint.h>
  6. #include <fuzzer/FuzzedDataProvider.h>
  7. #include "base/at_exit.h"
  8. #include "base/check.h"
  9. #include "base/i18n/icu_util.h"
  10. #include "extensions/common/url_pattern.h"
  11. #include "url/gurl.h"
  12. namespace extensions {
  13. namespace {
  14. struct Environment {
  15. Environment() { CHECK(base::i18n::InitializeICU()); }
  16. // Initialize the "at exit manager" singleton used by the tested code.
  17. base::AtExitManager at_exit_manager;
  18. };
  19. } // namespace
  20. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  21. static Environment env;
  22. FuzzedDataProvider fuzzed_data_provider(data, size);
  23. URLPattern url_pattern(
  24. /*valid_schemes=*/fuzzed_data_provider.ConsumeIntegral<int>());
  25. if (url_pattern.Parse(fuzzed_data_provider.ConsumeRandomLengthString()) !=
  26. URLPattern::ParseResult::kSuccess) {
  27. return 0;
  28. }
  29. GURL url(fuzzed_data_provider.ConsumeRandomLengthString());
  30. url_pattern.MatchesURL(url);
  31. return 0;
  32. }
  33. } // namespace extensions