test_helpers.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_helpers.c
  5. * \brief Some helper functions to avoid code duplication in unit tests.
  6. */
  7. #define ROUTERLIST_PRIVATE
  8. #include "orconfig.h"
  9. #include "or.h"
  10. #include "relay.h"
  11. #include "routerlist.h"
  12. #include "nodelist.h"
  13. #include "buffers.h"
  14. #include "test.h"
  15. #include "test_helpers.h"
  16. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  17. DISABLE_GCC_WARNING(overlength-strings)
  18. /* We allow huge string constants in the unit tests, but not in the code
  19. * at large. */
  20. #endif
  21. #include "test_descriptors.inc"
  22. #include "or.h"
  23. #include "circuitlist.h"
  24. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  25. ENABLE_GCC_WARNING(overlength-strings)
  26. #endif
  27. /* Return a statically allocated string representing yesterday's date
  28. * in ISO format. We use it so that state file items are not found to
  29. * be outdated. */
  30. const char *
  31. get_yesterday_date_str(void)
  32. {
  33. static char buf[ISO_TIME_LEN+1];
  34. time_t yesterday = time(NULL) - 24*60*60;
  35. format_iso_time(buf, yesterday);
  36. return buf;
  37. }
  38. /* NOP replacement for router_descriptor_is_older_than() */
  39. static int
  40. router_descriptor_is_older_than_replacement(const routerinfo_t *router,
  41. int seconds)
  42. {
  43. (void) router;
  44. (void) seconds;
  45. return 0;
  46. }
  47. /** Parse a file containing router descriptors and load them to our
  48. routerlist. This function is used to setup an artificial network
  49. so that we can conduct tests on it. */
  50. void
  51. helper_setup_fake_routerlist(void)
  52. {
  53. int retval;
  54. routerlist_t *our_routerlist = NULL;
  55. smartlist_t *our_nodelist = NULL;
  56. /* Read the file that contains our test descriptors. */
  57. /* We need to mock this function otherwise the descriptors will not
  58. accepted as they are too old. */
  59. MOCK(router_descriptor_is_older_than,
  60. router_descriptor_is_older_than_replacement);
  61. /* Load all the test descriptors to the routerlist. */
  62. retval = router_load_routers_from_string(TEST_DESCRIPTORS,
  63. NULL, SAVED_IN_JOURNAL,
  64. NULL, 0, NULL);
  65. tt_int_op(retval, ==, HELPER_NUMBER_OF_DESCRIPTORS);
  66. /* Sanity checking of routerlist and nodelist. */
  67. our_routerlist = router_get_routerlist();
  68. tt_int_op(smartlist_len(our_routerlist->routers), ==,
  69. HELPER_NUMBER_OF_DESCRIPTORS);
  70. routerlist_assert_ok(our_routerlist);
  71. our_nodelist = nodelist_get_list();
  72. tt_int_op(smartlist_len(our_nodelist), ==, HELPER_NUMBER_OF_DESCRIPTORS);
  73. /* Mark all routers as non-guards but up and running! */
  74. SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
  75. node->is_running = 1;
  76. node->is_valid = 1;
  77. node->is_possible_guard = 0;
  78. } SMARTLIST_FOREACH_END(node);
  79. done:
  80. UNMOCK(router_descriptor_is_older_than);
  81. }
  82. void
  83. connection_write_to_buf_mock(const char *string, size_t len,
  84. connection_t *conn, int compressed)
  85. {
  86. (void) compressed;
  87. tor_assert(string);
  88. tor_assert(conn);
  89. write_to_buf(string, len, conn->outbuf);
  90. }
  91. /* Set up a fake origin circuit with the specified number of cells,
  92. * Return a pointer to the newly-created dummy circuit */
  93. circuit_t *
  94. dummy_origin_circuit_new(int n_cells)
  95. {
  96. origin_circuit_t *circ = origin_circuit_new();
  97. int i;
  98. cell_t cell;
  99. for (i=0; i < n_cells; ++i) {
  100. crypto_rand((void*)&cell, sizeof(cell));
  101. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  102. &TO_CIRCUIT(circ)->n_chan_cells,
  103. 1, &cell, 1, 0);
  104. }
  105. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  106. return TO_CIRCUIT(circ);
  107. }
  108. /** Mock-replacement. As tor_addr_lookup, but always fails on any
  109. * address containing a !. This is necessary for running the unit tests
  110. * on networks where DNS hijackers think it's helpful to give answers
  111. * for things like 1.2.3.4.5 or "invalidstuff!!"
  112. */
  113. int
  114. mock_tor_addr_lookup__fail_on_bad_addrs(const char *name,
  115. uint16_t family, tor_addr_t *out)
  116. {
  117. if (name && strchr(name, '!')) {
  118. return -1;
  119. }
  120. return tor_addr_lookup__real(name, family, out);
  121. }