Debian Codegraph @

test/core/avl/avl_fuzzer.cc

raw
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 <stdlib.h>
16
17 #include <algorithm>
18 #include <map>
19 #include <utility>
20
21 #include "src/core/lib/avl/avl.h"
22 #include "src/libfuzzer/libfuzzer_macro.h"
23 #include "test/core/avl/avl_fuzzer.pb.h"
24
25 bool squelch = true;
26 bool leak_check = true;
27
28 namespace grpc_core {
29
30 class Fuzzer {
31 public:
32 Fuzzer() { CheckEqual(); }
33 ~Fuzzer() { CheckEqual(); }
34 void Run(const avl_fuzzer::Action& action) {
35 switch (action.action_case()) {
36 case avl_fuzzer::Action::kSet:
37 avl_ = avl_.Add(action.key(), action.set());
38 map_[action.key()] = action.set();
39 break;
40 case avl_fuzzer::Action::kDel:
41 avl_ = avl_.Remove(action.key());
42 map_.erase(action.key());
43 break;
44 case avl_fuzzer::Action::kGet: {
45 auto* p = avl_.Lookup(action.key());
46 auto it = map_.find(action.key());
47 if (it == map_.end() && p != nullptr) abort();
48 if (it != map_.end() && p == nullptr) abort();
49 if (it != map_.end() && it->second != *p) abort();
50 } break;
51 case avl_fuzzer::Action::ACTION_NOT_SET:
52 break;
53 }
54 }
55
56 private:
57 void CheckEqual() {
58 auto it = map_.begin();
59 avl_.ForEach([&](int key, int value) {
60 if (it == map_.end()) abort();
61 if (it->first != key) abort();
62 if (it->second != value) abort();
63 ++it;
64 });
65 if (it != map_.end()) abort();
66 }
67
68 AVL<int, int> avl_;
69 std::map<int, int> map_;
70 };
71
72 template <typename RepeatedField>
73 AVL<int, int> AvlFromProto(const RepeatedField& p) {
74 AVL<int, int> a;
75 for (const auto& kv : p) {
76 a = a.Add(kv.key(), kv.value());
77 }
78 return a;
79 }
80
81 template <typename RepeatedField>
82 std::map<int, int> MapFromProto(const RepeatedField& p) {
83 std::map<int, int> a;
84 for (const auto& kv : p) {
85 a[kv.key()] = kv.value();
86 }
87 return a;
88 }
89
90 } // namespace grpc_core
91
92 DEFINE_PROTO_FUZZER(const avl_fuzzer::Msg& msg) {
93 grpc_core::Fuzzer fuzzer;
94 for (const auto& action : msg.actions()) {
95 grpc_core::Fuzzer().Run(action);
96 }
97
98 for (const auto& cmp : msg.compares()) {
99 auto left_avl = grpc_core::AvlFromProto(cmp.left());
100 auto left_map = grpc_core::MapFromProto(cmp.left());
101 auto right_avl = grpc_core::AvlFromProto(cmp.right());
102 auto right_map = grpc_core::MapFromProto(cmp.right());
103 if ((left_avl == right_avl) != (left_map == right_map)) abort();
104 if ((left_avl < right_avl) != (left_map < right_map)) abort();
105 }
106 }