fuzz_vrs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define NS_PARSE_PRIVATE
  4. #define NETWORKSTATUS_PRIVATE
  5. #include "core/or/or.h"
  6. #include "feature/dirparse/ns_parse.h"
  7. #include "feature/dirparse/unparseable.h"
  8. #include "lib/memarea/memarea.h"
  9. #include "feature/nodelist/microdesc.h"
  10. #include "feature/nodelist/networkstatus.h"
  11. #include "feature/nodelist/networkstatus_st.h"
  12. #include "feature/nodelist/vote_routerstatus_st.h"
  13. #include "lib/crypt_ops/crypto_ed25519.h"
  14. #include "test/fuzz/fuzzing.h"
  15. static void
  16. mock_dump_desc__nodump(const char *desc, const char *type)
  17. {
  18. (void)desc;
  19. (void)type;
  20. }
  21. static networkstatus_t *dummy_vote = NULL;
  22. static memarea_t *area = NULL;
  23. int
  24. fuzz_init(void)
  25. {
  26. disable_signature_checking();
  27. MOCK(dump_desc, mock_dump_desc__nodump);
  28. ed25519_init();
  29. area = memarea_new();
  30. dummy_vote = tor_malloc_zero(sizeof(*dummy_vote));
  31. dummy_vote->known_flags = smartlist_new();
  32. smartlist_split_string(dummy_vote->known_flags,
  33. "Authority BadExit Exit Fast Guard HSDir "
  34. "NoEdConsensus Running Stable V2Dir Valid",
  35. " ", 0, 0);
  36. return 0;
  37. }
  38. int
  39. fuzz_cleanup(void)
  40. {
  41. SMARTLIST_FOREACH(dummy_vote->known_flags, char *, cp, tor_free(cp));
  42. smartlist_free(dummy_vote->known_flags);
  43. tor_free(dummy_vote);
  44. return 0;
  45. }
  46. int
  47. fuzz_main(const uint8_t *data, size_t sz)
  48. {
  49. char *str = tor_memdup_nulterm(data, sz);
  50. const char *s;
  51. routerstatus_t *rs_ns = NULL, *rs_md = NULL, *rs_vote = NULL;
  52. vote_routerstatus_t *vrs = tor_malloc_zero(sizeof(*vrs));
  53. smartlist_t *tokens = smartlist_new();
  54. s = str;
  55. rs_ns = routerstatus_parse_entry_from_string(area, &s, tokens,
  56. NULL, NULL, 26, FLAV_NS);
  57. tor_assert(smartlist_len(tokens) == 0);
  58. s = str;
  59. rs_md = routerstatus_parse_entry_from_string(area, &s, tokens,
  60. NULL, NULL, 26, FLAV_MICRODESC);
  61. tor_assert(smartlist_len(tokens) == 0);
  62. s = str;
  63. rs_vote = routerstatus_parse_entry_from_string(area, &s, tokens,
  64. dummy_vote, vrs, 26, FLAV_NS);
  65. tor_assert(smartlist_len(tokens) == 0);
  66. log_debug(LD_GENERAL,
  67. "ns=%p, md=%p, vote=%p", rs_ns, rs_md, rs_vote);
  68. routerstatus_free(rs_md);
  69. routerstatus_free(rs_ns);
  70. vote_routerstatus_free(vrs);
  71. memarea_clear(area);
  72. smartlist_free(tokens);
  73. tor_free(str);
  74. return 0;
  75. }