test_extorport.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONNECTION_PRIVATE
  4. #include "or.h"
  5. #include "connection.h"
  6. #include "ext_orport.h"
  7. #include "test.h"
  8. /* Test connection_or_remove_from_ext_or_id_map and
  9. * connection_or_set_ext_or_identifier */
  10. static void
  11. test_ext_or_id_map(void *arg)
  12. {
  13. or_connection_t *c1 = NULL, *c2 = NULL, *c3 = NULL;
  14. char *idp = NULL, *idp2 = NULL;
  15. (void)arg;
  16. /* pre-initialization */
  17. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx"));
  18. c1 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  19. c2 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  20. c3 = or_connection_new(CONN_TYPE_OR, AF_INET);
  21. tt_ptr_op(c1->ext_or_conn_id, !=, NULL);
  22. tt_ptr_op(c2->ext_or_conn_id, !=, NULL);
  23. tt_ptr_op(c3->ext_or_conn_id, ==, NULL);
  24. tt_ptr_op(c1, ==, connection_or_get_by_ext_or_id(c1->ext_or_conn_id));
  25. tt_ptr_op(c2, ==, connection_or_get_by_ext_or_id(c2->ext_or_conn_id));
  26. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx"));
  27. idp = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  28. /* Give c2 a new ID. */
  29. connection_or_set_ext_or_identifier(c2);
  30. test_mem_op(idp, !=, c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  31. idp2 = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  32. tt_assert(!tor_digest_is_zero(idp2));
  33. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp));
  34. tt_ptr_op(c2, ==, connection_or_get_by_ext_or_id(idp2));
  35. /* Now remove it. */
  36. connection_or_remove_from_ext_or_id_map(c2);
  37. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp));
  38. tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp2));
  39. done:
  40. if (c1)
  41. connection_free_(TO_CONN(c1));
  42. if (c2)
  43. connection_free_(TO_CONN(c2));
  44. if (c3)
  45. connection_free_(TO_CONN(c3));
  46. tor_free(idp);
  47. tor_free(idp2);
  48. connection_or_clear_ext_or_id_map();
  49. }
  50. struct testcase_t extorport_tests[] = {
  51. { "id_map", test_ext_or_id_map, TT_FORK, NULL, NULL },
  52. END_OF_TESTCASES
  53. };