blob: 08bb64fc735d6e8cd51a089000abca79b21d2826 [file] [log] [blame]
Daniel McArdle72692a12020-05-11 16:47:161// Copyright 2020 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 <stddef.h>
6#include <stdint.h>
7
8#include <memory>
9
10#include "base/json/json_reader.h"
Hans Wennborg725d0432020-06-18 13:54:1611#include "base/logging.h"
Daniel McArdle72692a12020-05-11 16:47:1612#include "base/optional.h"
13#include "base/strings/string_piece_forward.h"
Daniel McArdlebbfc1452020-05-18 15:11:5514#include "base/time/tick_clock.h"
Daniel McArdle72692a12020-05-11 16:47:1615#include "base/time/time.h"
16#include "net/base/backoff_entry.h"
17#include "net/base/backoff_entry_serializer.h"
18#include "net/base/backoff_entry_serializer_fuzzer_input.pb.h"
19#include "testing/libfuzzer/proto/json_proto_converter.h"
20#include "testing/libfuzzer/proto/lpm_interface.h"
21
22namespace net {
23
24namespace {
25struct Environment {
26 Environment() { logging::SetMinLogLevel(logging::LOG_ERROR); }
27};
28
29class ProtoTranslator {
30 public:
31 explicit ProtoTranslator(const fuzz_proto::FuzzerInput& input)
32 : input_(input) {}
33
34 BackoffEntry::Policy policy() const {
35 return PolicyFromProto(input_.policy());
36 }
Daniel McArdlebbfc1452020-05-18 15:11:5537 base::Time parse_time() const {
38 return base::Time() +
39 base::TimeDelta::FromMicroseconds(input_.parse_time());
Daniel McArdle72692a12020-05-11 16:47:1640 }
Daniel McArdlebbfc1452020-05-18 15:11:5541 base::Time serialize_time() const {
42 return base::Time() +
43 base::TimeDelta::FromMicroseconds(input_.serialize_time());
44 }
45 base::TimeTicks now_ticks() const {
46 return base::TimeTicks() +
47 base::TimeDelta::FromMicroseconds(input_.now_ticks());
48 }
Daniel McArdle72692a12020-05-11 16:47:1649 base::Optional<base::Value> serialized_entry() const {
50 json_proto::JsonProtoConverter converter;
51 std::string json_array = converter.Convert(input_.serialized_entry());
52 base::Optional<base::Value> value = base::JSONReader::Read(json_array);
53 return value;
54 }
55
56 private:
57 const fuzz_proto::FuzzerInput& input_;
58
59 static BackoffEntry::Policy PolicyFromProto(
60 const fuzz_proto::BackoffEntryPolicy& policy) {
Daniel McArdlebbfc1452020-05-18 15:11:5561 BackoffEntry::Policy new_policy;
62 new_policy.num_errors_to_ignore = policy.num_errors_to_ignore();
63 new_policy.initial_delay_ms = policy.initial_delay_ms();
64 new_policy.multiply_factor = policy.multiply_factor();
65 new_policy.jitter_factor = policy.jitter_factor();
66 new_policy.maximum_backoff_ms = policy.maximum_backoff_ms();
67 new_policy.entry_lifetime_ms = policy.entry_lifetime_ms();
68 new_policy.always_use_initial_delay = policy.always_use_initial_delay();
69 return new_policy;
Daniel McArdle72692a12020-05-11 16:47:1670 }
71};
72
Daniel McArdlebbfc1452020-05-18 15:11:5573class MockClock : public base::TickClock {
74 public:
75 MockClock() = default;
76 ~MockClock() override = default;
77
78 void SetNow(base::TimeTicks now) { now_ = now; }
79 base::TimeTicks NowTicks() const override { return now_; }
80
81 private:
82 base::TimeTicks now_;
83};
84
Daniel McArdle72692a12020-05-11 16:47:1685// Tests the "deserialize-reserialize" property. Deserializes a BackoffEntry
Daniel McArdlebbfc1452020-05-18 15:11:5586// from JSON, reserializes it, then deserializes again. Holding time constant,
87// we check that the parsed BackoffEntry values are equivalent.
Daniel McArdle72692a12020-05-11 16:47:1688void TestDeserialize(const ProtoTranslator& translator) {
89 // Attempt to convert the json_proto.ArrayValue to a base::Value.
90 base::Optional<base::Value> value = translator.serialized_entry();
91 if (!value)
92 return;
93 DCHECK(value->is_list());
94
95 BackoffEntry::Policy policy = translator.policy();
96
Daniel McArdlebbfc1452020-05-18 15:11:5597 MockClock clock;
98 clock.SetNow(translator.now_ticks());
99
Daniel McArdle72692a12020-05-11 16:47:16100 // Attempt to deserialize a BackoffEntry.
101 std::unique_ptr<BackoffEntry> entry =
Daniel McArdlebbfc1452020-05-18 15:11:55102 BackoffEntrySerializer::DeserializeFromValue(*value, &policy, &clock,
Daniel McArdle72692a12020-05-11 16:47:16103 translator.parse_time());
104 if (!entry)
105 return;
106
Daniel McArdle72692a12020-05-11 16:47:16107 std::unique_ptr<base::Value> reserialized =
Daniel McArdlebbfc1452020-05-18 15:11:55108 BackoffEntrySerializer::SerializeToValue(*entry, translator.parse_time());
Daniel McArdle72692a12020-05-11 16:47:16109 CHECK(reserialized);
Daniel McArdlebbfc1452020-05-18 15:11:55110
111 // Due to fuzzy interpretation in BackoffEntrySerializer::
112 // DeserializeFromValue, we cannot assert that |*reserialized == *value|.
Daniel McArdlea8799f02020-08-04 15:44:16113 // Rather, we can deserialize |reserialized| and check that some weaker
114 // properties are preserved.
Daniel McArdlebbfc1452020-05-18 15:11:55115 std::unique_ptr<BackoffEntry> entry_reparsed =
116 BackoffEntrySerializer::DeserializeFromValue(
117 *reserialized, &policy, &clock, translator.parse_time());
118 CHECK(entry_reparsed);
Daniel McArdlea8799f02020-08-04 15:44:16119 CHECK_EQ(entry_reparsed->failure_count(), entry->failure_count());
120 CHECK_LE(entry_reparsed->GetReleaseTime(), entry->GetReleaseTime());
Daniel McArdle72692a12020-05-11 16:47:16121}
122
123// Tests the "serialize-deserialize" property. Serializes an arbitrary
124// BackoffEntry to JSON, deserializes to another BackoffEntry, and checks
125// equality of the two entries. Our notion of equality is *very weak* and needs
126// improvement.
127void TestSerialize(const ProtoTranslator& translator) {
128 BackoffEntry::Policy policy = translator.policy();
129
130 // Serialize the BackoffEntry.
131 BackoffEntry native_entry(&policy);
132 std::unique_ptr<base::Value> serialized =
133 BackoffEntrySerializer::SerializeToValue(native_entry,
134 translator.serialize_time());
135 CHECK(serialized);
136
Daniel McArdlebbfc1452020-05-18 15:11:55137 MockClock clock;
138 clock.SetNow(translator.now_ticks());
139
Daniel McArdle72692a12020-05-11 16:47:16140 // Deserialize it.
141 std::unique_ptr<BackoffEntry> deserialized_entry =
Daniel McArdlebbfc1452020-05-18 15:11:55142 BackoffEntrySerializer::DeserializeFromValue(*serialized, &policy, &clock,
143 translator.parse_time());
Daniel McArdle72692a12020-05-11 16:47:16144 // Even though SerializeToValue was successful, we're not guaranteed to have a
145 // |deserialized_entry|. One reason deserialization may fail is if the parsed
146 // |absolute_release_time_us| is below zero.
147 if (!deserialized_entry)
148 return;
149
150 // TODO(dmcardle) Develop a stronger equality check for BackoffEntry.
151
152 // Note that while |BackoffEntry::GetReleaseTime| looks like an accessor, it
153 // returns a |value that is computed based on a random double, so it's not
154 // suitable for CHECK_EQ here. See |BackoffEntry::CalculateReleaseTime|.
155
156 CHECK_EQ(native_entry.failure_count(), deserialized_entry->failure_count());
157}
158} // namespace
159
160DEFINE_PROTO_FUZZER(const fuzz_proto::FuzzerInput& input) {
161 static Environment env;
162
163 // Print the entire |input| protobuf if asked.
164 if (getenv("LPM_DUMP_NATIVE_INPUT")) {
165 std::cout << "input: " << input.DebugString();
166 }
167
168 ProtoTranslator translator(input);
Daniel McArdle72692a12020-05-11 16:47:16169 TestDeserialize(translator);
Daniel McArdlebbfc1452020-05-18 15:11:55170 TestSerialize(translator);
Daniel McArdle72692a12020-05-11 16:47:16171}
172
173} // namespace net