testhelper.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define ROUTERLIST_PRIVATE
  4. #include "or.h"
  5. #include "routerlist.h"
  6. #include "testhelper.h"
  7. #include "nodelist.h"
  8. #include "test.h"
  9. #include "test_descriptors.inc"
  10. /* NOP replacement for router_descriptor_is_older_than() */
  11. static int
  12. router_descriptor_is_older_than_replacement(const routerinfo_t *router,
  13. int seconds)
  14. {
  15. (void) router;
  16. (void) seconds;
  17. return 0;
  18. }
  19. /** Parse a file containing router descriptors and load them to our
  20. routerlist. This function is used to setup an artificial network
  21. so that we can conduct tests on it. */
  22. void
  23. helper_setup_fake_routerlist(void)
  24. {
  25. int retval;
  26. routerlist_t *our_routerlist = NULL;
  27. smartlist_t *our_nodelist = NULL;
  28. /* Read the file that contains our test descriptors. */
  29. /* We need to mock this function otherwise the descriptors will not
  30. accepted as they are too old. */
  31. MOCK(router_descriptor_is_older_than,
  32. router_descriptor_is_older_than_replacement);
  33. /* Load all the test descriptors to the routerlist. */
  34. retval = router_load_routers_from_string(TEST_DESCRIPTORS,
  35. NULL, SAVED_IN_JOURNAL,
  36. NULL, 0, NULL);
  37. tt_int_op(retval, ==, HELPER_NUMBER_OF_DESCRIPTORS);
  38. /* Sanity checking of routerlist and nodelist. */
  39. our_routerlist = router_get_routerlist();
  40. tt_int_op(smartlist_len(our_routerlist->routers), ==,
  41. HELPER_NUMBER_OF_DESCRIPTORS);
  42. routerlist_assert_ok(our_routerlist);
  43. our_nodelist = nodelist_get_list();
  44. tt_int_op(smartlist_len(our_nodelist), ==, HELPER_NUMBER_OF_DESCRIPTORS);
  45. /* Mark all routers as non-guards but up and running! */
  46. SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
  47. node->is_running = 1;
  48. node->is_valid = 1;
  49. node->is_possible_guard = 0;
  50. } SMARTLIST_FOREACH_END(node);
  51. done:
  52. UNMOCK(router_descriptor_is_older_than);
  53. }