gurl_fuzzer.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 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 "base/at_exit.h"
  5. #include "base/check_op.h"
  6. #include "base/i18n/icu_util.h"
  7. #include "base/no_destructor.h"
  8. #include "url/gurl.h"
  9. struct TestCase {
  10. TestCase() { CHECK(base::i18n::InitializeICU()); }
  11. // used by ICU integration.
  12. base::AtExitManager at_exit_manager;
  13. };
  14. TestCase* test_case = new TestCase();
  15. // Checks that GURL's canonicalization is idempotent. This can help discover
  16. // issues like https://crbug.com/1128999.
  17. void CheckIdempotency(const GURL& url) {
  18. if (!url.is_valid())
  19. return;
  20. const std::string& spec = url.spec();
  21. GURL recanonicalized(spec);
  22. CHECK(recanonicalized.is_valid());
  23. CHECK_EQ(spec, recanonicalized.spec());
  24. }
  25. // Checks that |url.spec()| is preserved across a call to ReplaceComponents with
  26. // zero replacements, which is effectively a copy. This can help discover issues
  27. // like https://crbug.com/1075515.
  28. void CheckReplaceComponentsPreservesSpec(const GURL& url) {
  29. static const base::NoDestructor<GURL::Replacements> no_op;
  30. GURL copy = url.ReplaceComponents(*no_op);
  31. CHECK_EQ(url.is_valid(), copy.is_valid());
  32. if (url.is_valid()) {
  33. CHECK_EQ(url.spec(), copy.spec());
  34. }
  35. }
  36. // Entry point for LibFuzzer.
  37. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  38. if (size < 1)
  39. return 0;
  40. {
  41. base::StringPiece string_piece_input(reinterpret_cast<const char*>(data),
  42. size);
  43. const GURL url_from_string_piece(string_piece_input);
  44. CheckIdempotency(url_from_string_piece);
  45. CheckReplaceComponentsPreservesSpec(url_from_string_piece);
  46. }
  47. // Test for StringPiece16 if size is even.
  48. if (size % sizeof(char16_t) == 0) {
  49. base::StringPiece16 string_piece_input16(
  50. reinterpret_cast<const char16_t*>(data), size / sizeof(char16_t));
  51. const GURL url_from_string_piece16(string_piece_input16);
  52. CheckIdempotency(url_from_string_piece16);
  53. CheckReplaceComponentsPreservesSpec(url_from_string_piece16);
  54. }
  55. // Resolve relative url tests.
  56. {
  57. size_t size_t_bytes = sizeof(size_t);
  58. if (size < size_t_bytes + 1) {
  59. return 0;
  60. }
  61. size_t relative_size =
  62. *reinterpret_cast<const size_t*>(data) % (size - size_t_bytes);
  63. std::string relative_string(
  64. reinterpret_cast<const char*>(data + size_t_bytes), relative_size);
  65. base::StringPiece string_piece_part_input(
  66. reinterpret_cast<const char*>(data + size_t_bytes + relative_size),
  67. size - relative_size - size_t_bytes);
  68. const GURL url_from_string_piece_part(string_piece_part_input);
  69. CheckIdempotency(url_from_string_piece_part);
  70. CheckReplaceComponentsPreservesSpec(url_from_string_piece_part);
  71. url_from_string_piece_part.Resolve(relative_string);
  72. if (relative_size % sizeof(char16_t) == 0) {
  73. std::u16string relative_string16(
  74. reinterpret_cast<const char16_t*>(data + size_t_bytes),
  75. relative_size / sizeof(char16_t));
  76. url_from_string_piece_part.Resolve(relative_string16);
  77. }
  78. }
  79. return 0;
  80. }