Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / url / gurl_fuzzer.cc
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
5 #include "base/at_exit.h"
6 #include "base/i18n/icu_util.h"
7 #include "url/gurl.h"
8
9 struct TestCase {
10   TestCase() { CHECK(base::i18n::InitializeICU()); }
11
12   // used by ICU integration.
13   base::AtExitManager at_exit_manager;
14 };
15
16 TestCase* test_case = new TestCase();
17
18 // Entry point for LibFuzzer.
19 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
20   if (size < 1)
21     return 0;
22
23   base::StringPiece string_piece_input(reinterpret_cast<const char*>(data),
24                                        size);
25   GURL url_from_string_piece(string_piece_input);
26
27   // Test for StringPiece16 if size is even.
28   if (size % 2 == 0) {
29     base::StringPiece16 string_piece_input16(
30         reinterpret_cast<const base::char16*>(data), size / 2);
31
32     GURL url_from_string_piece16(string_piece_input16);
33   }
34
35   // Resolve relative url tests.
36   size_t size_t_bytes = sizeof(size_t);
37   if (size < size_t_bytes + 1) {
38     return 0;
39   }
40   size_t relative_size =
41       *reinterpret_cast<const size_t*>(data) % (size - size_t_bytes);
42   std::string relative_string(
43       reinterpret_cast<const char*>(data + size_t_bytes), relative_size);
44   base::StringPiece string_piece_part_input(
45       reinterpret_cast<const char*>(data + size_t_bytes + relative_size),
46       size - relative_size - size_t_bytes);
47   GURL url_from_string_piece_part(string_piece_part_input);
48   url_from_string_piece_part.Resolve(relative_string);
49
50   if (relative_size % 2 == 0) {
51     base::string16 relative_string16(
52         reinterpret_cast<const base::char16*>(data + size_t_bytes),
53         relative_size / 2);
54     url_from_string_piece_part.Resolve(relative_string16);
55   }
56   return 0;
57 }