cookie_partition_key_fuzzer.cc 1.3 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 <string>
  7. #include <fuzzer/FuzzedDataProvider.h>
  8. #include "base/test/scoped_feature_list.h"
  9. #include "net/base/features.h"
  10. #include "net/cookies/cookie_partition_key.h"
  11. #include "url/origin.h"
  12. namespace net {
  13. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  14. base::test::ScopedFeatureList scoped_feature_list;
  15. scoped_feature_list.InitAndEnableFeature(features::kPartitionedCookies);
  16. FuzzedDataProvider data_provider(data, size);
  17. std::string url_str = data_provider.ConsumeRandomLengthString(800);
  18. GURL url(url_str);
  19. if (!url.is_valid())
  20. return 0;
  21. absl::optional<CookiePartitionKey> partition_key =
  22. absl::make_optional(CookiePartitionKey::FromURLForTesting(url));
  23. bool is_opaque = url::Origin::Create(url).opaque();
  24. std::string tmp;
  25. CHECK_NE(is_opaque, CookiePartitionKey::Serialize(partition_key, tmp));
  26. CHECK_NE(is_opaque, CookiePartitionKey::Deserialize(url_str, partition_key));
  27. if (!is_opaque) {
  28. CHECK(absl::make_optional(CookiePartitionKey::FromURLForTesting(url)) ==
  29. partition_key);
  30. }
  31. return 0;
  32. }
  33. } // namespace net