fuzz_consensus.c 1.9 KB

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