avl_fuzzer.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/core/lib/avl/avl.h"
16 #include "src/libfuzzer/libfuzzer_macro.h"
17 #include "test/core/avl/avl_fuzzer.pb.h"
18 
19 bool squelch = true;
20 bool leak_check = true;
21 
22 namespace grpc_core {
23 
24 class Fuzzer {
25  public:
26  Fuzzer() { CheckEqual(); }
28  void Run(const avl_fuzzer::Action& action) {
29  switch (action.action_case()) {
30  case avl_fuzzer::Action::kSet:
31  avl_ = avl_.Add(action.key(), action.set());
32  map_[action.key()] = action.set();
33  break;
34  case avl_fuzzer::Action::kDel:
35  avl_ = avl_.Remove(action.key());
36  map_.erase(action.key());
37  break;
38  case avl_fuzzer::Action::kGet: {
39  auto* p = avl_.Lookup(action.key());
40  auto it = map_.find(action.key());
41  if (it == map_.end() && p != nullptr) abort();
42  if (it != map_.end() && p == nullptr) abort();
43  if (it != map_.end() && it->second != *p) abort();
44  } break;
45  case avl_fuzzer::Action::ACTION_NOT_SET:
46  break;
47  }
48  }
49 
50  private:
51  void CheckEqual() {
52  auto it = map_.begin();
53  avl_.ForEach([&](int key, int value) {
54  if (it == map_.end()) abort();
55  if (it->first != key) abort();
56  if (it->second != value) abort();
57  ++it;
58  });
59  if (it != map_.end()) abort();
60  }
61 
63  std::map<int, int> map_;
64 };
65 
66 template <typename RepeatedField>
69  for (const auto& kv : p) {
70  a = a.Add(kv.key(), kv.value());
71  }
72  return a;
73 }
74 
75 template <typename RepeatedField>
76 std::map<int, int> MapFromProto(const RepeatedField& p) {
77  std::map<int, int> a;
78  for (const auto& kv : p) {
79  a[kv.key()] = kv.value();
80  }
81  return a;
82 }
83 
84 } // namespace grpc_core
85 
86 DEFINE_PROTO_FUZZER(const avl_fuzzer::Msg& msg) {
88  for (const auto& action : msg.actions()) {
90  }
91 
92  for (const auto& cmp : msg.compares()) {
93  auto left_avl = grpc_core::AvlFromProto(cmp.left());
94  auto left_map = grpc_core::MapFromProto(cmp.left());
95  auto right_avl = grpc_core::AvlFromProto(cmp.right());
96  auto right_map = grpc_core::MapFromProto(cmp.right());
97  if ((left_avl == right_avl) != (left_map == right_map)) abort();
98  if ((left_avl < right_avl) != (left_map < right_map)) abort();
99  }
100 }
regen-readme.it
it
Definition: regen-readme.py:15
grpc_core::AVL::Lookup
const V * Lookup(const SomethingLikeK &key) const
Definition: avl.h:42
fuzzer
Fuzzer * fuzzer
Definition: promise_fuzzer.cc:124
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Fuzzer::Run
void Run(const avl_fuzzer::Action &action)
Definition: avl_fuzzer.cc:28
grpc_core::Fuzzer::Fuzzer
Fuzzer()
Definition: avl_fuzzer.cc:26
avl.h
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_core::Fuzzer::avl_
AVL< int, int > avl_
Definition: avl_fuzzer.cc:62
grpc_core::AVL::Remove
AVL Remove(const SomethingLikeK &key) const
Definition: avl.h:38
grpc_core::AvlFromProto
AVL< int, int > AvlFromProto(const RepeatedField &p)
Definition: avl_fuzzer.cc:67
DEFINE_PROTO_FUZZER
DEFINE_PROTO_FUZZER(const avl_fuzzer::Msg &msg)
Definition: avl_fuzzer.cc:86
grpc_core::Fuzzer::~Fuzzer
~Fuzzer()
Definition: avl_fuzzer.cc:27
leak_check
bool leak_check
Definition: avl_fuzzer.cc:20
grpc_core::AVL::Add
AVL Add(K key, V value) const
Definition: avl.h:34
grpc_core::Fuzzer::map_
std::map< int, int > map_
Definition: avl_fuzzer.cc:63
tests.google.protobuf.internal.message_test.cmp
cmp
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:61
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
grpc_core::MapFromProto
std::map< int, int > MapFromProto(const RepeatedField &p)
Definition: avl_fuzzer.cc:76
squelch
bool squelch
Definition: avl_fuzzer.cc:19
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::AVL< int, int >
grpc_core::Fuzzer::CheckEqual
void CheckEqual()
Definition: avl_fuzzer.cc:51
grpc_core::Fuzzer
Definition: avl_fuzzer.cc:24
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_core::AVL::ForEach
void ForEach(F &&f) const
Definition: avl.h:55
client.action
action
Definition: examples/python/xds/client.py:49
RepeatedField
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:403


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45