fuzz_http.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define BUFFERS_PRIVATE
  5. #define DIRECTORY_PRIVATE
  6. #include "or.h"
  7. #include "backtrace.h"
  8. #include "buffers.h"
  9. #include "config.h"
  10. #include "connection.h"
  11. #include "directory.h"
  12. #include "torlog.h"
  13. #include "fuzzing.h"
  14. static int mock_get_options_calls = 0;
  15. static or_options_t *mock_options = NULL;
  16. static void
  17. reset_options(or_options_t *options, int *get_options_calls)
  18. {
  19. memset(options, 0, sizeof(or_options_t));
  20. options->TestingTorNetwork = 1;
  21. *get_options_calls = 0;
  22. }
  23. static const or_options_t*
  24. mock_get_options(void)
  25. {
  26. ++mock_get_options_calls;
  27. tor_assert(mock_options);
  28. return mock_options;
  29. }
  30. static void
  31. mock_connection_write_to_buf_impl_(const char *string, size_t len,
  32. connection_t *conn, int zlib)
  33. {
  34. log_debug(LD_GENERAL, "%sResponse:\n%zu\nConnection: %p\n%s\n",
  35. zlib ? "Compressed " : "", len, conn, string);
  36. }
  37. int
  38. fuzz_init(void)
  39. {
  40. mock_options = tor_malloc(sizeof(or_options_t));
  41. reset_options(mock_options, &mock_get_options_calls);
  42. MOCK(get_options, mock_get_options);
  43. /* Set up fake response handler */
  44. MOCK(connection_write_to_buf_impl_, mock_connection_write_to_buf_impl_);
  45. return 0;
  46. }
  47. int
  48. fuzz_cleanup(void)
  49. {
  50. tor_free(mock_options);
  51. UNMOCK(get_options);
  52. UNMOCK(connection_write_to_buf_impl_);
  53. return 0;
  54. }
  55. int
  56. fuzz_main(const uint8_t *stdin_buf, size_t data_size)
  57. {
  58. dir_connection_t dir_conn;
  59. /* Set up the fake connection */
  60. memset(&dir_conn, 0, sizeof(dir_connection_t));
  61. dir_conn.base_.type = CONN_TYPE_DIR;
  62. /* Apparently tor sets this before directory_handle_command() is called. */
  63. dir_conn.base_.address = tor_strdup("replace-this-address.example.com");
  64. dir_conn.base_.inbuf = buf_new_with_data((char*)stdin_buf, data_size);
  65. if (!dir_conn.base_.inbuf) {
  66. log_debug(LD_GENERAL, "Zero-Length-Input\n");
  67. return 0;
  68. }
  69. /* Parse the headers */
  70. int rv = directory_handle_command(&dir_conn);
  71. /* TODO: check the output is correctly parsed based on the input */
  72. /* Report the parsed origin address */
  73. if (dir_conn.base_.address) {
  74. log_debug(LD_GENERAL, "Address:\n%s\n", dir_conn.base_.address);
  75. }
  76. log_debug(LD_GENERAL, "Result:\n%d\n", rv);
  77. /* Reset. */
  78. tor_free(dir_conn.base_.address);
  79. buf_free(dir_conn.base_.inbuf);
  80. dir_conn.base_.inbuf = NULL;
  81. return 0;
  82. }