summaryrefslogtreecommitdiff
path: root/chromium/net/dns/host_cache_fuzzer.cc
blob: 6bc24b21d04941fae2202c610ef33693c315f20b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include "base/json/json_reader.h"
#include "base/strings/string_piece_forward.h"
#include "net/dns/host_cache.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace net {

struct Environment {
  Environment() { logging::SetMinLogLevel(logging::LOG_ERROR); }
};

// This fuzzer checks that parsing a JSON list to a HostCache and then
// re-serializing it recreates the original JSON list.
//
// A side effect of this technique is that our distribution of HostCaches only
// contains HostCaches that can be generated by RestoreFromListValue. It's
// conceivable that this doesn't capture all possible HostCaches.
//
// TODO(dmcardle): Check the other direction of this property. Starting from an
// arbitrary HostCache, serialize it and then parse a different HostCache.
// Verify that the two HostCaches are equal.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  static Environment env;

  // Attempt to read a JSON list from the fuzzed string.
  base::StringPiece data_view(reinterpret_cast<const char*>(data), size);
  absl::optional<base::Value> value = base::JSONReader::Read(data_view);
  if (!value || !value->is_list())
    return 0;
  const base::ListValue& list_input = base::Value::AsListValue(*value);

  // Parse the HostCache.
  constexpr size_t kMaxEntries = 1000;
  HostCache host_cache(kMaxEntries);
  if (!host_cache.RestoreFromListValue(list_input))
    return 0;

  // Serialize the HostCache.
  base::ListValue serialized;
  host_cache.GetAsListValue(
      &serialized /* entry_list */, true /* include_staleness */,
      HostCache::SerializationType::kRestorable /* serialization_type */);

  CHECK_EQ(list_input, serialized);
  return 0;
}
}  // namespace net