fuzz_diff_apply.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONSDIFF_PRIVATE
  4. #include "orconfig.h"
  5. #include "core/or/or.h"
  6. #include "feature/dircommon/consdiff.h"
  7. #include "test/fuzz/fuzzing.h"
  8. static int
  9. mock_consensus_compute_digest_(const char *c, consensus_digest_t *d)
  10. {
  11. (void)c;
  12. memset(d->sha3_256, 3, sizeof(d->sha3_256));
  13. return 0;
  14. }
  15. static int
  16. mock_consensus_digest_eq_(const uint8_t *a, const uint8_t *b)
  17. {
  18. (void)a;
  19. (void)b;
  20. return 1;
  21. }
  22. int
  23. fuzz_init(void)
  24. {
  25. MOCK(consensus_compute_digest, mock_consensus_compute_digest_);
  26. MOCK(consensus_digest_eq, mock_consensus_digest_eq_);
  27. return 0;
  28. }
  29. int
  30. fuzz_cleanup(void)
  31. {
  32. UNMOCK(consensus_compute_digest);
  33. UNMOCK(consensus_digest_eq);
  34. return 0;
  35. }
  36. int
  37. fuzz_main(const uint8_t *stdin_buf, size_t data_size)
  38. {
  39. #define SEP "=====\n"
  40. #define SEPLEN strlen(SEP)
  41. const uint8_t *separator = tor_memmem(stdin_buf, data_size, SEP, SEPLEN);
  42. if (! separator)
  43. return 0;
  44. size_t c1_len = separator - stdin_buf;
  45. char *c1 = tor_memdup_nulterm(stdin_buf, c1_len);
  46. size_t c2_len = data_size - c1_len - SEPLEN;
  47. char *c2 = tor_memdup_nulterm(separator + SEPLEN, c2_len);
  48. char *c3 = consensus_diff_apply(c1, c2);
  49. tor_free(c1);
  50. tor_free(c2);
  51. tor_free(c3);
  52. return 0;
  53. }