fuzz_vrs.c 2.4 KB

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