fuzz_vrs.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/dirauth/dirvote.h"
  7. #include "feature/dirparse/ns_parse.h"
  8. #include "feature/dirparse/unparseable.h"
  9. #include "lib/memarea/memarea.h"
  10. #include "feature/nodelist/microdesc.h"
  11. #include "feature/nodelist/networkstatus.h"
  12. #include "feature/nodelist/networkstatus_st.h"
  13. #include "feature/nodelist/vote_routerstatus_st.h"
  14. #include "lib/crypt_ops/crypto_ed25519.h"
  15. #include "test/fuzz/fuzzing.h"
  16. static void
  17. mock_dump_desc__nodump(const char *desc, const char *type)
  18. {
  19. (void)desc;
  20. (void)type;
  21. }
  22. static networkstatus_t *dummy_vote = NULL;
  23. static memarea_t *area = NULL;
  24. int
  25. fuzz_init(void)
  26. {
  27. disable_signature_checking();
  28. MOCK(dump_desc, mock_dump_desc__nodump);
  29. ed25519_init();
  30. area = memarea_new();
  31. dummy_vote = tor_malloc_zero(sizeof(*dummy_vote));
  32. dummy_vote->known_flags = smartlist_new();
  33. smartlist_split_string(dummy_vote->known_flags,
  34. DIRVOTE_UNIVERSAL_FLAGS,
  35. " ", 0, 0);
  36. smartlist_split_string(dummy_vote->known_flags,
  37. DIRVOTE_OPTIONAL_FLAGS,
  38. " ", 0, 0);
  39. smartlist_sort_strings(dummy_vote->known_flags);
  40. return 0;
  41. }
  42. int
  43. fuzz_cleanup(void)
  44. {
  45. SMARTLIST_FOREACH(dummy_vote->known_flags, char *, cp, tor_free(cp));
  46. smartlist_free(dummy_vote->known_flags);
  47. tor_free(dummy_vote);
  48. return 0;
  49. }
  50. int
  51. fuzz_main(const uint8_t *data, size_t sz)
  52. {
  53. const char *s;
  54. routerstatus_t *rs_ns = NULL, *rs_md = NULL, *rs_vote = NULL;
  55. vote_routerstatus_t *vrs = tor_malloc_zero(sizeof(*vrs));
  56. smartlist_t *tokens = smartlist_new();
  57. const char *eos = (const char *)data + sz;
  58. s = (const char *)data;
  59. rs_ns = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
  60. NULL, NULL, 26, FLAV_NS);
  61. tor_assert(smartlist_len(tokens) == 0);
  62. s = (const char *)data;
  63. rs_md = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
  64. NULL, NULL, 26, FLAV_MICRODESC);
  65. tor_assert(smartlist_len(tokens) == 0);
  66. s = (const char *)data;
  67. rs_vote = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
  68. dummy_vote, vrs, 26, FLAV_NS);
  69. tor_assert(smartlist_len(tokens) == 0);
  70. log_debug(LD_GENERAL,
  71. "ns=%p, md=%p, vote=%p", rs_ns, rs_md, rs_vote);
  72. routerstatus_free(rs_md);
  73. routerstatus_free(rs_ns);
  74. vote_routerstatus_free(vrs);
  75. memarea_clear(area);
  76. smartlist_free(tokens);
  77. return 0;
  78. }