fuzz_consensus.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define ROUTERPARSE_PRIVATE
  4. #include "or/or.h"
  5. #include "or/routerparse.h"
  6. #include "or/networkstatus.h"
  7. #include "test/fuzz/fuzzing.h"
  8. static void
  9. mock_dump_desc__nodump(const char *desc, const char *type)
  10. {
  11. (void)desc;
  12. (void)type;
  13. }
  14. static int
  15. mock_router_produce_hash_final__nohash(char *digest,
  16. const char *start, size_t len,
  17. digest_algorithm_t alg)
  18. {
  19. (void)start;
  20. (void)len;
  21. /* we could look at start[..] */
  22. if (alg == DIGEST_SHA1)
  23. memset(digest, 0x01, 20);
  24. else
  25. memset(digest, 0x02, 32);
  26. return 0;
  27. }
  28. static int
  29. mock_signed_digest_equals__yes(const uint8_t *d1, const uint8_t *d2,
  30. size_t len)
  31. {
  32. (void) tor_memeq(d1, d2, len);
  33. return 1;
  34. }
  35. int
  36. fuzz_init(void)
  37. {
  38. disable_signature_checking();
  39. MOCK(dump_desc, mock_dump_desc__nodump);
  40. MOCK(router_compute_hash_final, mock_router_produce_hash_final__nohash);
  41. MOCK(signed_digest_equals, mock_signed_digest_equals__yes);
  42. ed25519_init();
  43. return 0;
  44. }
  45. int
  46. fuzz_cleanup(void)
  47. {
  48. return 0;
  49. }
  50. int
  51. fuzz_main(const uint8_t *data, size_t sz)
  52. {
  53. networkstatus_t *ns;
  54. char *str = tor_memdup_nulterm(data, sz);
  55. const char *eos = NULL;
  56. networkstatus_type_t tp = NS_TYPE_CONSENSUS;
  57. if (tor_memstr(data, MIN(sz, 1024), "tus vote"))
  58. tp = NS_TYPE_VOTE;
  59. const char *what = (tp == NS_TYPE_CONSENSUS) ? "consensus" : "vote";
  60. ns = networkstatus_parse_vote_from_string(str,
  61. &eos,
  62. tp);
  63. if (ns) {
  64. log_debug(LD_GENERAL, "Parsing as %s okay", what);
  65. networkstatus_vote_free(ns);
  66. } else {
  67. log_debug(LD_GENERAL, "Parsing as %s failed", what);
  68. }
  69. tor_free(str);
  70. return 0;
  71. }