fuzz_diff.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONSDIFF_PRIVATE
  4. #include "orconfig.h"
  5. #include "or.h"
  6. #include "consdiff.h"
  7. #include "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. int
  16. fuzz_init(void)
  17. {
  18. MOCK(consensus_compute_digest, mock_consensus_compute_digest_);
  19. return 0;
  20. }
  21. int
  22. fuzz_cleanup(void)
  23. {
  24. UNMOCK(consensus_compute_digest);
  25. return 0;
  26. }
  27. int
  28. fuzz_main(const uint8_t *stdin_buf, size_t data_size)
  29. {
  30. #define SEP "=====\n"
  31. #define SEPLEN strlen(SEP)
  32. const uint8_t *separator = tor_memmem(stdin_buf, data_size, SEP, SEPLEN);
  33. if (! separator)
  34. return 0;
  35. size_t c1_len = separator - stdin_buf;
  36. char *c1 = tor_memdup_nulterm(stdin_buf, c1_len);
  37. size_t c2_len = data_size - c1_len - SEPLEN;
  38. char *c2 = tor_memdup_nulterm(separator + SEPLEN, c2_len);
  39. char *c3 = consensus_diff_generate(c1, c2);
  40. if (c3) {
  41. char *c4 = consensus_diff_apply(c1, c3);
  42. tor_assert(c4);
  43. if (strcmp(c2, c4)) {
  44. printf("%s\n", escaped(c1));
  45. printf("%s\n", escaped(c2));
  46. printf("%s\n", escaped(c3));
  47. printf("%s\n", escaped(c4));
  48. }
  49. tor_assert(! strcmp(c2, c4));
  50. tor_free(c3);
  51. tor_free(c4);
  52. }
  53. tor_free(c1);
  54. tor_free(c2);
  55. return 0;
  56. }