fuzz_diff_apply.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (c) 2016-2019, 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, size_t len,
  10. consensus_digest_t *d)
  11. {
  12. (void)c;
  13. (void)len;
  14. memset(d->sha3_256, 3, sizeof(d->sha3_256));
  15. return 0;
  16. }
  17. static int
  18. mock_consensus_digest_eq_(const uint8_t *a, const uint8_t *b)
  19. {
  20. (void)a;
  21. (void)b;
  22. return 1;
  23. }
  24. int
  25. fuzz_init(void)
  26. {
  27. MOCK(consensus_compute_digest, mock_consensus_compute_digest_);
  28. MOCK(consensus_digest_eq, mock_consensus_digest_eq_);
  29. return 0;
  30. }
  31. int
  32. fuzz_cleanup(void)
  33. {
  34. UNMOCK(consensus_compute_digest);
  35. UNMOCK(consensus_digest_eq);
  36. return 0;
  37. }
  38. int
  39. fuzz_main(const uint8_t *stdin_buf, size_t data_size)
  40. {
  41. #define SEP "=====\n"
  42. #define SEPLEN strlen(SEP)
  43. const uint8_t *separator = tor_memmem(stdin_buf, data_size, SEP, SEPLEN);
  44. if (! separator)
  45. return 0;
  46. size_t c1_len = separator - stdin_buf;
  47. const char *c1 = (const char *)stdin_buf;
  48. size_t c2_len = data_size - c1_len - SEPLEN;
  49. const char *c2 = (const char *)separator + SEPLEN;
  50. char *c3 = consensus_diff_apply(c1, c1_len, c2, c2_len);
  51. tor_free(c3);
  52. return 0;
  53. }