xref: /aosp_15_r20/external/cronet/net/base/schemeful_site_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 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/schemeful_site.h"
6 
7 #include <stdlib.h>
8 
9 #include <iostream>
10 #include <optional>
11 #include <string>
12 
13 #include "testing/libfuzzer/proto/lpm_interface.h"
14 #include "testing/libfuzzer/proto/url.pb.h"
15 #include "testing/libfuzzer/proto/url_proto_converter.h"
16 #include "url/gurl.h"
17 #include "url/origin.h"
18 
DEFINE_PROTO_FUZZER(const url_proto::Url & url_message)19 DEFINE_PROTO_FUZZER(const url_proto::Url& url_message) {
20   std::string native_input = url_proto::Convert(url_message);
21 
22   if (getenv("LPM_DUMP_NATIVE_INPUT"))
23     std::cout << native_input << std::endl;
24 
25   url::Origin origin = url::Origin::Create((GURL(native_input)));
26 
27   // We don't run the fuzzer on inputs whose hosts will contain "..". The ".."
28   // causes SchemefulSite to consider the registrable domain to start with the
29   // second ".".
30   if (origin.host().find("..") != std::string::npos)
31     return;
32 
33   net::SchemefulSite site(origin);
34 
35   std::optional<net::SchemefulSite> site_with_registrable_domain =
36       net::SchemefulSite::CreateIfHasRegisterableDomain(origin);
37 
38   if (site_with_registrable_domain) {
39     CHECK_EQ(site_with_registrable_domain->GetInternalOriginForTesting(),
40              site.GetInternalOriginForTesting());
41     CHECK(site.has_registrable_domain_or_host());
42     const std::string& scheme = site.GetInternalOriginForTesting().scheme();
43     if (scheme == "http" || scheme == "https") {
44       CHECK_NE(site.registrable_domain_or_host_for_testing().front(), '.');
45     }
46   }
47 }
48