xref: /aosp_15_r20/external/cronet/net/cookies/cookie_util_parsing_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 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 <string>
6 
7 #include <fuzzer/FuzzedDataProvider.h>
8 
9 #include "net/cookies/cookie_util.h"
10 #include "net/cookies/parsed_cookie.h"
11 
12 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)13 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
14   FuzzedDataProvider data_provider(data, size);
15 
16   // Generate a cookie line and parse it.
17   const std::string cookie_line = data_provider.ConsumeRandomLengthString();
18   net::cookie_util::ParsedRequestCookies parsed_cookies;
19   net::cookie_util::ParseRequestCookieLine(cookie_line, &parsed_cookies);
20 
21   // If any non-empty cookies were parsed, the re-serialized cookie line
22   // shouldn't be empty. The re-serialized cookie line may not match the
23   // original line if the input was malformed.
24   if (parsed_cookies.size() > 0) {
25     CHECK_GT(
26         net::cookie_util::SerializeRequestCookieLine(parsed_cookies).length(),
27         0U);
28   }
29 
30   return 0;
31 }
32