test_helpers.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (c) 2014-2016, 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 "routerlist.h"
  11. #include "nodelist.h"
  12. #include "buffers.h"
  13. #include "test.h"
  14. #include "test_helpers.h"
  15. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  16. DISABLE_GCC_WARNING(overlength-strings)
  17. /* We allow huge string constants in the unit tests, but not in the code
  18. * at large. */
  19. #endif
  20. #include "test_descriptors.inc"
  21. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  22. ENABLE_GCC_WARNING(overlength-strings)
  23. #endif
  24. /* Return a statically allocated string representing yesterday's date
  25. * in ISO format. We use it so that state file items are not found to
  26. * be outdated. */
  27. const char *
  28. get_yesterday_date_str(void)
  29. {
  30. static char buf[ISO_TIME_LEN+1];
  31. time_t yesterday = time(NULL) - 24*60*60;
  32. format_iso_time(buf, yesterday);
  33. return buf;
  34. }
  35. /* NOP replacement for router_descriptor_is_older_than() */
  36. static int
  37. router_descriptor_is_older_than_replacement(const routerinfo_t *router,
  38. int seconds)
  39. {
  40. (void) router;
  41. (void) seconds;
  42. return 0;
  43. }
  44. /** Parse a file containing router descriptors and load them to our
  45. routerlist. This function is used to setup an artificial network
  46. so that we can conduct tests on it. */
  47. void
  48. helper_setup_fake_routerlist(void)
  49. {
  50. int retval;
  51. routerlist_t *our_routerlist = NULL;
  52. smartlist_t *our_nodelist = NULL;
  53. /* Read the file that contains our test descriptors. */
  54. /* We need to mock this function otherwise the descriptors will not
  55. accepted as they are too old. */
  56. MOCK(router_descriptor_is_older_than,
  57. router_descriptor_is_older_than_replacement);
  58. /* Load all the test descriptors to the routerlist. */
  59. retval = router_load_routers_from_string(TEST_DESCRIPTORS,
  60. NULL, SAVED_IN_JOURNAL,
  61. NULL, 0, NULL);
  62. tt_int_op(retval, ==, HELPER_NUMBER_OF_DESCRIPTORS);
  63. /* Sanity checking of routerlist and nodelist. */
  64. our_routerlist = router_get_routerlist();
  65. tt_int_op(smartlist_len(our_routerlist->routers), ==,
  66. HELPER_NUMBER_OF_DESCRIPTORS);
  67. routerlist_assert_ok(our_routerlist);
  68. our_nodelist = nodelist_get_list();
  69. tt_int_op(smartlist_len(our_nodelist), ==, HELPER_NUMBER_OF_DESCRIPTORS);
  70. /* Mark all routers as non-guards but up and running! */
  71. SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
  72. node->is_running = 1;
  73. node->is_valid = 1;
  74. node->is_possible_guard = 0;
  75. } SMARTLIST_FOREACH_END(node);
  76. done:
  77. UNMOCK(router_descriptor_is_older_than);
  78. }
  79. void
  80. connection_write_to_buf_mock(const char *string, size_t len,
  81. connection_t *conn, int zlib)
  82. {
  83. (void) zlib;
  84. tor_assert(string);
  85. tor_assert(conn);
  86. write_to_buf(string, len, conn->outbuf);
  87. }