fuzz_diff.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, 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. MOCK(consensus_compute_digest_as_signed, mock_consensus_compute_digest_);
  20. return 0;
  21. }
  22. int
  23. fuzz_cleanup(void)
  24. {
  25. UNMOCK(consensus_compute_digest);
  26. UNMOCK(consensus_compute_digest_as_signed);
  27. return 0;
  28. }
  29. int
  30. fuzz_main(const uint8_t *stdin_buf, size_t data_size)
  31. {
  32. #define SEP "=====\n"
  33. #define SEPLEN strlen(SEP)
  34. const uint8_t *separator = tor_memmem(stdin_buf, data_size, SEP, SEPLEN);
  35. if (! separator)
  36. return 0;
  37. size_t c1_len = separator - stdin_buf;
  38. char *c1 = tor_memdup_nulterm(stdin_buf, c1_len);
  39. size_t c2_len = data_size - c1_len - SEPLEN;
  40. char *c2 = tor_memdup_nulterm(separator + SEPLEN, c2_len);
  41. char *c3 = consensus_diff_generate(c1, c2);
  42. if (c3) {
  43. char *c4 = consensus_diff_apply(c1, c3);
  44. tor_assert(c4);
  45. if (strcmp(c2, c4)) {
  46. printf("%s\n", escaped(c1));
  47. printf("%s\n", escaped(c2));
  48. printf("%s\n", escaped(c3));
  49. printf("%s\n", escaped(c4));
  50. }
  51. tor_assert(! strcmp(c2, c4));
  52. tor_free(c3);
  53. tor_free(c4);
  54. }
  55. tor_free(c1);
  56. tor_free(c2);
  57. return 0;
  58. }