test_dns.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "or.h"
  2. #include "test.h"
  3. #define DNS_PRIVATE
  4. #include "dns.h"
  5. #include "connection.h"
  6. static void
  7. test_dns_clip_ttl(void *arg)
  8. {
  9. (void)arg;
  10. uint32_t ttl_mid = MIN_DNS_TTL / 2 + MAX_DNS_TTL / 2;
  11. tt_int_op(dns_clip_ttl(MIN_DNS_TTL - 1),==,MIN_DNS_TTL);
  12. tt_int_op(dns_clip_ttl(ttl_mid),==,ttl_mid);
  13. tt_int_op(dns_clip_ttl(MAX_DNS_TTL + 1),==,MAX_DNS_TTL);
  14. done:
  15. return;
  16. }
  17. static void
  18. test_dns_expiry_ttl(void *arg)
  19. {
  20. (void)arg;
  21. uint32_t ttl_mid = MIN_DNS_TTL / 2 + MAX_DNS_ENTRY_AGE / 2;
  22. tt_int_op(dns_get_expiry_ttl(MIN_DNS_TTL - 1),==,MIN_DNS_TTL);
  23. tt_int_op(dns_get_expiry_ttl(ttl_mid),==,ttl_mid);
  24. tt_int_op(dns_get_expiry_ttl(MAX_DNS_ENTRY_AGE + 1),==,MAX_DNS_ENTRY_AGE);
  25. done:
  26. return;
  27. }
  28. static int resolve_retval = 0;
  29. static int resolve_made_conn_pending = 0;
  30. static char *resolved_name = NULL;
  31. static cached_resolve_t *cache_entry = NULL;
  32. static int n_fake_impl = 0;
  33. /** This will be our configurable substitute for <b>dns_resolve_impl</b> in
  34. * dns.c. It will return <b>resolve_retval</b>,
  35. * and set <b>resolve_made_conn_pending</b> to
  36. * <b>made_connection_pending_out</b>. It will set <b>hostname_out</b>
  37. * to a duplicate of <b>resolved_name</b> and it will set <b>resolve_out</b>
  38. * to <b>cache_entry</b>. Lastly, it will increment <b>n_fake_impl</b< by
  39. * 1.
  40. */
  41. static int
  42. dns_resolve_fake_impl(edge_connection_t *exitconn, int is_resolve,
  43. or_circuit_t *oncirc, char **hostname_out,
  44. int *made_connection_pending_out,
  45. cached_resolve_t **resolve_out)
  46. {
  47. (void)oncirc;
  48. (void)exitconn;
  49. (void)is_resolve;
  50. if (made_connection_pending_out)
  51. *made_connection_pending_out = resolve_made_conn_pending;
  52. if (hostname_out && resolved_name)
  53. *hostname_out = tor_strdup(resolved_name);
  54. if (resolve_out && cache_entry)
  55. *resolve_out = cache_entry;
  56. n_fake_impl++;
  57. return resolve_retval;
  58. }
  59. static edge_connection_t *conn_for_resolved_cell = NULL;
  60. static int n_send_resolved_cell_replacement = 0;
  61. static uint8_t last_answer_type = 0;
  62. static cached_resolve_t *last_resolved;
  63. static void
  64. send_resolved_cell_replacement(edge_connection_t *conn, uint8_t answer_type,
  65. const cached_resolve_t *resolved)
  66. {
  67. conn_for_resolved_cell = conn;
  68. last_answer_type = answer_type;
  69. last_resolved = (cached_resolve_t *)resolved;
  70. n_send_resolved_cell_replacement++;
  71. }
  72. static int n_send_resolved_hostname_cell_replacement = 0;
  73. static char *last_resolved_hostname = NULL;
  74. static void
  75. send_resolved_hostname_cell_replacement(edge_connection_t *conn,
  76. const char *hostname)
  77. {
  78. conn_for_resolved_cell = conn;
  79. last_resolved_hostname = (char *)hostname;
  80. n_send_resolved_hostname_cell_replacement++;
  81. }
  82. static int n_dns_cancel_pending_resolve_replacement = 0;
  83. static void
  84. dns_cancel_pending_resolve_replacement(const char *address)
  85. {
  86. (void) address;
  87. n_dns_cancel_pending_resolve_replacement++;
  88. }
  89. static int n_connection_free = 0;
  90. static connection_t *last_freed_conn = NULL;
  91. static void
  92. connection_free_replacement(connection_t *conn)
  93. {
  94. n_connection_free++;
  95. last_freed_conn = conn;
  96. }
  97. static void
  98. test_dns_resolve_outer(void *arg)
  99. {
  100. (void) arg;
  101. int retval;
  102. int prev_n_send_resolved_hostname_cell_replacement;
  103. int prev_n_send_resolved_cell_replacement;
  104. int prev_n_connection_free;
  105. cached_resolve_t *fake_resolved = tor_malloc(sizeof(cached_resolve_t));
  106. edge_connection_t *exitconn = tor_malloc(sizeof(edge_connection_t));
  107. edge_connection_t *nextconn = tor_malloc(sizeof(edge_connection_t));
  108. or_circuit_t *on_circuit = tor_malloc(sizeof(or_circuit_t));
  109. memset(on_circuit,0,sizeof(or_circuit_t));
  110. on_circuit->base_.magic = OR_CIRCUIT_MAGIC;
  111. memset(fake_resolved,0,sizeof(cached_resolve_t));
  112. memset(exitconn,0,sizeof(edge_connection_t));
  113. memset(nextconn,0,sizeof(edge_connection_t));
  114. MOCK(dns_resolve_impl,dns_resolve_fake_impl);
  115. MOCK(send_resolved_cell,send_resolved_cell_replacement);
  116. MOCK(send_resolved_hostname_cell,send_resolved_hostname_cell_replacement);
  117. /*
  118. * CASE 1: dns_resolve_impl returns 1 and sets a hostname. purpose is
  119. * EXIT_PURPOSE_RESOLVE.
  120. *
  121. * We want dns_resolve() to call send_resolved_hostname_cell() for a
  122. * given exit connection (represented by edge_connection_t object)
  123. * with a hostname it received from _impl.
  124. */
  125. prev_n_send_resolved_hostname_cell_replacement =
  126. n_send_resolved_hostname_cell_replacement;
  127. exitconn->base_.purpose = EXIT_PURPOSE_RESOLVE;
  128. exitconn->on_circuit = &(on_circuit->base_);
  129. resolve_retval = 1;
  130. resolved_name = tor_strdup("www.torproject.org");
  131. retval = dns_resolve(exitconn);
  132. tt_int_op(retval,==,1);
  133. tt_str_op(resolved_name,==,last_resolved_hostname);
  134. tt_assert(conn_for_resolved_cell == exitconn);
  135. tt_int_op(n_send_resolved_hostname_cell_replacement,==,
  136. prev_n_send_resolved_hostname_cell_replacement + 1);
  137. tt_assert(exitconn->on_circuit == NULL);
  138. last_resolved_hostname = NULL;
  139. /* CASE 2: dns_resolve_impl returns 1, but does not set hostname.
  140. * Instead, it yields cached_resolve_t object.
  141. *
  142. * We want dns_resolve to call send_resolved_cell on exitconn with
  143. * RESOLVED_TYPE_AUTO and the cached_resolve_t object from _impl.
  144. */
  145. tor_free(resolved_name);
  146. resolved_name = NULL;
  147. exitconn->on_circuit = &(on_circuit->base_);
  148. cache_entry = fake_resolved;
  149. prev_n_send_resolved_cell_replacement =
  150. n_send_resolved_cell_replacement;
  151. retval = dns_resolve(exitconn);
  152. tt_int_op(retval,==,1);
  153. tt_assert(conn_for_resolved_cell == exitconn);
  154. tt_int_op(n_send_resolved_cell_replacement,==,
  155. prev_n_send_resolved_cell_replacement + 1);
  156. tt_assert(last_resolved == fake_resolved);
  157. tt_int_op(last_answer_type,==,0xff);
  158. tt_assert(exitconn->on_circuit == NULL);
  159. /* CASE 3: The purpose of exit connection is not EXIT_PURPOSE_RESOLVE
  160. * and _impl returns 1.
  161. *
  162. * We want dns_resolve to prepend exitconn to n_streams linked list.
  163. * We don't want it to send any cells about hostname being resolved.
  164. */
  165. exitconn->base_.purpose = EXIT_PURPOSE_CONNECT;
  166. exitconn->on_circuit = &(on_circuit->base_);
  167. on_circuit->n_streams = nextconn;
  168. prev_n_send_resolved_cell_replacement =
  169. n_send_resolved_cell_replacement;
  170. prev_n_send_resolved_hostname_cell_replacement =
  171. n_send_resolved_hostname_cell_replacement;
  172. retval = dns_resolve(exitconn);
  173. tt_int_op(retval,==,1);
  174. tt_assert(on_circuit->n_streams == exitconn);
  175. tt_assert(exitconn->next_stream == nextconn);
  176. tt_int_op(prev_n_send_resolved_cell_replacement,==,
  177. n_send_resolved_cell_replacement);
  178. tt_int_op(prev_n_send_resolved_hostname_cell_replacement,==,
  179. n_send_resolved_hostname_cell_replacement);
  180. /* CASE 4: _impl returns 0.
  181. *
  182. * We want dns_resolve() to set exitconn state to
  183. * EXIT_CONN_STATE_RESOLVING and prepend exitconn to resolving_streams
  184. * linked list.
  185. */
  186. exitconn->on_circuit = &(on_circuit->base_);
  187. resolve_retval = 0;
  188. exitconn->next_stream = NULL;
  189. on_circuit->resolving_streams = nextconn;
  190. retval = dns_resolve(exitconn);
  191. tt_int_op(retval,==,0);
  192. tt_int_op(exitconn->base_.state,==,EXIT_CONN_STATE_RESOLVING);
  193. tt_assert(on_circuit->resolving_streams == exitconn);
  194. tt_assert(exitconn->next_stream == nextconn);
  195. /* CASE 5: _impl returns -1 when purpose of exitconn is
  196. * EXIT_PURPOSE_RESOLVE. We want dns_resolve to call send_resolved_cell
  197. * on exitconn with type being RESOLVED_TYPE_ERROR.
  198. */
  199. MOCK(dns_cancel_pending_resolve,dns_cancel_pending_resolve_replacement);
  200. MOCK(connection_free,connection_free_replacement);
  201. exitconn->on_circuit = &(on_circuit->base_);
  202. exitconn->base_.purpose = EXIT_PURPOSE_RESOLVE;
  203. resolve_retval = -1;
  204. prev_n_send_resolved_cell_replacement =
  205. n_send_resolved_cell_replacement;
  206. prev_n_connection_free = n_connection_free;
  207. retval = dns_resolve(exitconn);
  208. tt_int_op(retval,==,-1);
  209. tt_int_op(n_send_resolved_cell_replacement,==,
  210. prev_n_send_resolved_cell_replacement + 1);
  211. tt_int_op(last_answer_type,==,RESOLVED_TYPE_ERROR);
  212. tt_int_op(n_dns_cancel_pending_resolve_replacement,==,1);
  213. tt_int_op(n_connection_free,==,prev_n_connection_free + 1);
  214. tt_assert(last_freed_conn == TO_CONN(exitconn));
  215. done:
  216. UNMOCK(dns_resolve_impl);
  217. UNMOCK(send_resolved_cell);
  218. UNMOCK(send_resolved_hostname_cell);
  219. UNMOCK(dns_cancel_pending_resolve);
  220. UNMOCK(connection_free);
  221. tor_free(on_circuit);
  222. tor_free(exitconn);
  223. tor_free(nextconn);
  224. tor_free(resolved_name);
  225. tor_free(fake_resolved);
  226. return;
  227. }
  228. struct testcase_t dns_tests[] = {
  229. { "clip_ttl", test_dns_clip_ttl, 0, NULL, NULL },
  230. { "expiry_ttl", test_dns_expiry_ttl, 0, NULL, NULL },
  231. { "resolve_outer", test_dns_resolve_outer, TT_FORK, NULL, NULL },
  232. END_OF_TESTCASES
  233. };