Map_fuzz_test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "util/Assert.h"
  17. #include "wire/Message.h"
  18. #include "test/FuzzTest.h"
  19. #include "util/events/EventBase.h"
  20. #define Map_NAME OfLongsByInteger
  21. #define Map_KEY_TYPE uint32_t
  22. #define Map_VALUE_TYPE uint64_t
  23. #define Map_ENABLE_HANDLES
  24. #include "util/Map.h"
  25. void* CJDNS_FUZZ_INIT(struct Allocator* alloc, struct Random* rand)
  26. {
  27. return alloc;
  28. }
  29. void CJDNS_FUZZ_MAIN(void* vctx, struct Message* fuzz)
  30. {
  31. struct Allocator* alloc = (struct Allocator*) vctx;
  32. if (Message_getLength(fuzz) < 4) { return; }
  33. uint32_t size = Er_assert(Message_epop16be(fuzz)) % 4096;
  34. struct Map_OfLongsByInteger* map = Map_OfLongsByInteger_new(alloc);
  35. size = size % 4096;
  36. uint32_t* keys = Allocator_malloc(alloc, sizeof(uint32_t) * size);
  37. uint64_t* vals = Allocator_malloc(alloc, sizeof(uint64_t) * size);
  38. uint32_t key = 3;
  39. uint64_t val = 4;
  40. for (uint32_t i = 0; i < size; i++) {
  41. keys[i] = key;
  42. vals[i] = val;
  43. Map_OfLongsByInteger_put(&key, &val, map);
  44. key += ((val >> 13 ^ size << 19) & 0x000fffff) + 1;
  45. //Log_debug(logger, "%u", (val >> 13 ^ size << 19) & 0x000fffff);
  46. Assert_true(key > keys[i]);
  47. val += key >> 19 ^ i << 13;
  48. }
  49. // check all keys there
  50. for (uint32_t i = 0; i < size; ++i) {
  51. int index = Map_OfLongsByInteger_indexForKey(&keys[i], map);
  52. Assert_true(map->values[index] == vals[i]);
  53. }
  54. }