avl_fuzzer.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "src/core/lib/avl/avl.h"
  15. #include "src/libfuzzer/libfuzzer_macro.h"
  16. #include "test/core/avl/avl_fuzzer.pb.h"
  17. bool squelch = true;
  18. bool leak_check = true;
  19. namespace grpc_core {
  20. class Fuzzer {
  21. public:
  22. void Run(const avl_fuzzer::Msg& msg) {
  23. CheckEqual();
  24. for (const auto& action : msg.actions()) {
  25. switch (action.action_case()) {
  26. case avl_fuzzer::Action::kSet:
  27. avl_ = avl_.Add(action.key(), action.set());
  28. map_[action.key()] = action.set();
  29. break;
  30. case avl_fuzzer::Action::kDel:
  31. avl_ = avl_.Remove(action.key());
  32. map_.erase(action.key());
  33. break;
  34. case avl_fuzzer::Action::kGet: {
  35. auto* p = avl_.Lookup(action.key());
  36. auto it = map_.find(action.key());
  37. if (it == map_.end() && p != nullptr) abort();
  38. if (it != map_.end() && p == nullptr) abort();
  39. if (it != map_.end() && it->second != *p) abort();
  40. } break;
  41. case avl_fuzzer::Action::ACTION_NOT_SET:
  42. break;
  43. }
  44. CheckEqual();
  45. }
  46. }
  47. private:
  48. void CheckEqual() {
  49. auto it = map_.begin();
  50. avl_.ForEach([&](int key, int value) {
  51. if (it == map_.end()) abort();
  52. if (it->first != key) abort();
  53. if (it->second != value) abort();
  54. ++it;
  55. });
  56. if (it != map_.end()) abort();
  57. }
  58. AVL<int, int> avl_;
  59. std::map<int, int> map_;
  60. };
  61. } // namespace grpc_core
  62. DEFINE_PROTO_FUZZER(const avl_fuzzer::Msg& msg) {
  63. grpc_core::Fuzzer().Run(msg);
  64. }