blob: 8930b2acbbd2e3564bf54ff7cc23e4cd40dc6bf6 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2020 The Chromium Authors
Daniel McArdle72692a12020-05-11 16:47:162// 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/strings/string_piece_forward.h"
Daniel McArdlebbfc1452020-05-18 15:11:5513#include "base/time/tick_clock.h"
Daniel McArdle72692a12020-05-11 16:47:1614#include "base/time/time.h"
15#include "net/base/backoff_entry.h"
16#include "net/base/backoff_entry_serializer.h"
17#include "net/base/backoff_entry_serializer_fuzzer_input.pb.h"
18#include "testing/libfuzzer/proto/json_proto_converter.h"
19#include "testing/libfuzzer/proto/lpm_interface.h"
Anton Bikineev068d2912021-05-15 20:43:5220#include "third_party/abseil-cpp/absl/types/optional.h"
Daniel McArdle72692a12020-05-11 16:47:1621
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 {
Ali Hijazif071ec0f2023-06-01 18:17:0835 return PolicyFromProto(input_->policy());
Daniel McArdle72692a12020-05-11 16:47:1636 }
Daniel McArdlebbfc1452020-05-18 15:11:5537 base::Time parse_time() const {
Ali Hijazif071ec0f2023-06-01 18:17:0838 return base::Time() + base::Microseconds(input_->parse_time());
Daniel McArdle72692a12020-05-11 16:47:1639 }
Dan McArdle82d70972021-04-12 15:54:2040 base::TimeTicks parse_time_ticks() const {
Ali Hijazif071ec0f2023-06-01 18:17:0841 return base::TimeTicks() + base::Microseconds(input_->parse_time());
Dan McArdle82d70972021-04-12 15:54:2042 }
Daniel McArdlebbfc1452020-05-18 15:11:5543 base::Time serialize_time() const {
Ali Hijazif071ec0f2023-06-01 18:17:0844 return base::Time() + base::Microseconds(input_->serialize_time());
Daniel McArdlebbfc1452020-05-18 15:11:5545 }
46 base::TimeTicks now_ticks() const {
Ali Hijazif071ec0f2023-06-01 18:17:0847 return base::TimeTicks() + base::Microseconds(input_->now_ticks());
Daniel McArdlebbfc1452020-05-18 15:11:5548 }
Anton Bikineev068d2912021-05-15 20:43:5249 absl::optional<base::Value> serialized_entry() const {
Daniel McArdle72692a12020-05-11 16:47:1650 json_proto::JsonProtoConverter converter;
Ali Hijazif071ec0f2023-06-01 18:17:0851 std::string json_array = converter.Convert(input_->serialized_entry());
Anton Bikineev068d2912021-05-15 20:43:5252 absl::optional<base::Value> value = base::JSONReader::Read(json_array);
Daniel McArdle72692a12020-05-11 16:47:1653 return value;
54 }
55
56 private:
Ali Hijazif071ec0f2023-06-01 18:17:0857 const raw_ref<const fuzz_proto::FuzzerInput> input_;
Daniel McArdle72692a12020-05-11 16:47:1658
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.
Anton Bikineev068d2912021-05-15 20:43:5290 absl::optional<base::Value> value = translator.serialized_entry();
Daniel McArdle72692a12020-05-11 16:47:1691 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;
Dan McArdle82d70972021-04-12 15:54:2098 clock.SetNow(translator.parse_time_ticks());
Daniel McArdlebbfc1452020-05-18 15:11:5599
Daniel McArdle72692a12020-05-11 16:47:16100 // Attempt to deserialize a BackoffEntry.
101 std::unique_ptr<BackoffEntry> entry =
Jiacheng Guo9dfe9f62022-09-21 11:35:47102 BackoffEntrySerializer::DeserializeFromList(
103 value->GetList(), &policy, &clock, translator.parse_time());
Daniel McArdle72692a12020-05-11 16:47:16104 if (!entry)
105 return;
106
Jiacheng Guo9dfe9f62022-09-21 11:35:47107 base::Value::List reserialized =
108 BackoffEntrySerializer::SerializeToList(*entry, translator.parse_time());
Daniel McArdlebbfc1452020-05-18 15:11:55109
110 // Due to fuzzy interpretation in BackoffEntrySerializer::
Jiacheng Guo9dfe9f62022-09-21 11:35:47111 // DeserializeFromList, we cannot assert that |*reserialized == *value|.
Daniel McArdlea8799f02020-08-04 15:44:16112 // Rather, we can deserialize |reserialized| and check that some weaker
113 // properties are preserved.
Daniel McArdlebbfc1452020-05-18 15:11:55114 std::unique_ptr<BackoffEntry> entry_reparsed =
Jiacheng Guo9dfe9f62022-09-21 11:35:47115 BackoffEntrySerializer::DeserializeFromList(reserialized, &policy, &clock,
116 translator.parse_time());
Daniel McArdlebbfc1452020-05-18 15:11:55117 CHECK(entry_reparsed);
Daniel McArdlea8799f02020-08-04 15:44:16118 CHECK_EQ(entry_reparsed->failure_count(), entry->failure_count());
119 CHECK_LE(entry_reparsed->GetReleaseTime(), entry->GetReleaseTime());
Daniel McArdle72692a12020-05-11 16:47:16120}
121
122// Tests the "serialize-deserialize" property. Serializes an arbitrary
123// BackoffEntry to JSON, deserializes to another BackoffEntry, and checks
124// equality of the two entries. Our notion of equality is *very weak* and needs
125// improvement.
126void TestSerialize(const ProtoTranslator& translator) {
127 BackoffEntry::Policy policy = translator.policy();
128
129 // Serialize the BackoffEntry.
130 BackoffEntry native_entry(&policy);
Jiacheng Guo9dfe9f62022-09-21 11:35:47131 base::Value::List serialized = BackoffEntrySerializer::SerializeToList(
Maks Orlovich691496c2021-05-18 15:03:21132 native_entry, translator.serialize_time());
Daniel McArdle72692a12020-05-11 16:47:16133
Daniel McArdlebbfc1452020-05-18 15:11:55134 MockClock clock;
135 clock.SetNow(translator.now_ticks());
136
Daniel McArdle72692a12020-05-11 16:47:16137 // Deserialize it.
138 std::unique_ptr<BackoffEntry> deserialized_entry =
Jiacheng Guo9dfe9f62022-09-21 11:35:47139 BackoffEntrySerializer::DeserializeFromList(serialized, &policy, &clock,
140 translator.parse_time());
141 // Even though SerializeToList was successful, we're not guaranteed to have a
Daniel McArdle72692a12020-05-11 16:47:16142 // |deserialized_entry|. One reason deserialization may fail is if the parsed
143 // |absolute_release_time_us| is below zero.
144 if (!deserialized_entry)
145 return;
146
147 // TODO(dmcardle) Develop a stronger equality check for BackoffEntry.
148
149 // Note that while |BackoffEntry::GetReleaseTime| looks like an accessor, it
150 // returns a |value that is computed based on a random double, so it's not
151 // suitable for CHECK_EQ here. See |BackoffEntry::CalculateReleaseTime|.
152
153 CHECK_EQ(native_entry.failure_count(), deserialized_entry->failure_count());
154}
155} // namespace
156
157DEFINE_PROTO_FUZZER(const fuzz_proto::FuzzerInput& input) {
158 static Environment env;
159
160 // Print the entire |input| protobuf if asked.
161 if (getenv("LPM_DUMP_NATIVE_INPUT")) {
162 std::cout << "input: " << input.DebugString();
163 }
164
165 ProtoTranslator translator(input);
Dan McArdle22c2f642021-03-18 23:40:10166 // Skip this input if any of the time values are infinite.
167 if (translator.now_ticks().is_inf() || translator.parse_time().is_inf() ||
168 translator.serialize_time().is_inf()) {
169 return;
170 }
Daniel McArdle72692a12020-05-11 16:47:16171 TestDeserialize(translator);
Daniel McArdlebbfc1452020-05-18 15:11:55172 TestSerialize(translator);
Daniel McArdle72692a12020-05-11 16:47:16173}
174
175} // namespace net