test_helpers.c 2.7 KB

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