Map_fuzz_test.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #define Map_NAME OfLongsByInteger
  20. #define Map_KEY_TYPE uint32_t
  21. #define Map_VALUE_TYPE uint64_t
  22. #define Map_ENABLE_HANDLES
  23. #include "util/Map.h"
  24. void* CJDNS_FUZZ_INIT(struct Allocator* alloc, struct Random* rand)
  25. {
  26. return alloc;
  27. }
  28. void CJDNS_FUZZ_MAIN(void* vctx, Message_t* fuzz)
  29. {
  30. struct Allocator* alloc = (struct Allocator*) vctx;
  31. if (Message_getLength(fuzz) < 4) { return; }
  32. uint16_t size = 0;
  33. Err_assert(Message_epop16be(&size, fuzz));
  34. size %= 4096;
  35. struct Map_OfLongsByInteger* map = Map_OfLongsByInteger_new(alloc);
  36. size = size % 4096;
  37. uint32_t* keys = Allocator_malloc(alloc, sizeof(uint32_t) * size);
  38. uint64_t* vals = Allocator_malloc(alloc, sizeof(uint64_t) * size);
  39. uint32_t key = 3;
  40. uint64_t val = 4;
  41. for (uint32_t i = 0; i < size; i++) {
  42. keys[i] = key;
  43. vals[i] = val;
  44. Map_OfLongsByInteger_put(&key, &val, map);
  45. key += ((val >> 13 ^ size << 19) & 0x000fffff) + 1;
  46. //Log_debug(logger, "%u", (val >> 13 ^ size << 19) & 0x000fffff);
  47. Assert_true(key > keys[i]);
  48. val += key >> 19 ^ i << 13;
  49. }
  50. // check all keys there
  51. for (uint32_t i = 0; i < size; ++i) {
  52. int index = Map_OfLongsByInteger_indexForKey(&keys[i], map);
  53. Assert_true(map->values[index] == vals[i]);
  54. }
  55. }