test_helpers.c 2.4 KB

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