test_channeltls.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include <math.h>
  4. #define TOR_CHANNEL_INTERNAL_
  5. #include "or.h"
  6. #include "address.h"
  7. #include "buffers.h"
  8. #include "channel.h"
  9. #include "channeltls.h"
  10. #include "connection_or.h"
  11. #include "config.h"
  12. /* For init/free stuff */
  13. #include "scheduler.h"
  14. #include "tortls.h"
  15. /* Test suite stuff */
  16. #include "test.h"
  17. #include "fakechans.h"
  18. /* The channeltls unit tests */
  19. static void test_channeltls_create(void *arg);
  20. static void test_channeltls_num_bytes_queued(void *arg);
  21. static void test_channeltls_overhead_estimate(void *arg);
  22. /* Mocks used by channeltls unit tests */
  23. static size_t tlschan_buf_datalen_mock(const buf_t *buf);
  24. static or_connection_t * tlschan_connection_or_connect_mock(
  25. const tor_addr_t *addr,
  26. uint16_t port,
  27. const char *digest,
  28. channel_tls_t *tlschan);
  29. static int tlschan_is_local_addr_mock(const tor_addr_t *addr);
  30. /* Fake close method */
  31. static void tlschan_fake_close_method(channel_t *chan);
  32. /* Flags controlling behavior of channeltls unit test mocks */
  33. static int tlschan_local = 0;
  34. static const buf_t * tlschan_buf_datalen_mock_target = NULL;
  35. static size_t tlschan_buf_datalen_mock_size = 0;
  36. /* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */
  37. static int fake_tortls = 0; /* Bleh... */
  38. static void
  39. test_channeltls_create(void *arg)
  40. {
  41. tor_addr_t test_addr;
  42. channel_t *ch = NULL;
  43. const char test_digest[DIGEST_LEN] = {
  44. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  45. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
  46. (void)arg;
  47. /* Set up a fake address to fake-connect to */
  48. test_addr.family = AF_INET;
  49. test_addr.addr.in_addr.s_addr = htonl(0x01020304);
  50. /* For this test we always want the address to be treated as non-local */
  51. tlschan_local = 0;
  52. /* Install is_local_addr() mock */
  53. MOCK(is_local_addr, tlschan_is_local_addr_mock);
  54. /* Install mock for connection_or_connect() */
  55. MOCK(connection_or_connect, tlschan_connection_or_connect_mock);
  56. /* Try connecting */
  57. ch = channel_tls_connect(&test_addr, 567, test_digest);
  58. tt_assert(ch != NULL);
  59. done:
  60. if (ch) {
  61. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  62. /*
  63. * Use fake close method that doesn't try to do too much to fake
  64. * orconn
  65. */
  66. ch->close = tlschan_fake_close_method;
  67. channel_mark_for_close(ch);
  68. free_fake_channel(ch);
  69. UNMOCK(scheduler_release_channel);
  70. }
  71. UNMOCK(connection_or_connect);
  72. UNMOCK(is_local_addr);
  73. return;
  74. }
  75. static void
  76. test_channeltls_num_bytes_queued(void *arg)
  77. {
  78. tor_addr_t test_addr;
  79. channel_t *ch = NULL;
  80. const char test_digest[DIGEST_LEN] = {
  81. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  82. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
  83. channel_tls_t *tlschan = NULL;
  84. size_t len;
  85. int fake_outbuf = 0, n;
  86. (void)arg;
  87. /* Set up a fake address to fake-connect to */
  88. test_addr.family = AF_INET;
  89. test_addr.addr.in_addr.s_addr = htonl(0x01020304);
  90. /* For this test we always want the address to be treated as non-local */
  91. tlschan_local = 0;
  92. /* Install is_local_addr() mock */
  93. MOCK(is_local_addr, tlschan_is_local_addr_mock);
  94. /* Install mock for connection_or_connect() */
  95. MOCK(connection_or_connect, tlschan_connection_or_connect_mock);
  96. /* Try connecting */
  97. ch = channel_tls_connect(&test_addr, 567, test_digest);
  98. tt_assert(ch != NULL);
  99. /*
  100. * Next, we have to test ch->num_bytes_queued, which is
  101. * channel_tls_num_bytes_queued_method. We can't mock
  102. * connection_get_outbuf_len() directly because it's static inline
  103. * in connection.h, but we can mock buf_datalen().
  104. */
  105. tt_assert(ch->num_bytes_queued != NULL);
  106. tlschan = BASE_CHAN_TO_TLS(ch);
  107. tt_assert(tlschan != NULL);
  108. if (TO_CONN(tlschan->conn)->outbuf == NULL) {
  109. /* We need an outbuf to make sure buf_datalen() gets called */
  110. fake_outbuf = 1;
  111. TO_CONN(tlschan->conn)->outbuf = buf_new();
  112. }
  113. tlschan_buf_datalen_mock_target = TO_CONN(tlschan->conn)->outbuf;
  114. tlschan_buf_datalen_mock_size = 1024;
  115. MOCK(buf_datalen, tlschan_buf_datalen_mock);
  116. len = ch->num_bytes_queued(ch);
  117. tt_int_op(len, ==, tlschan_buf_datalen_mock_size);
  118. /*
  119. * We also cover num_cells_writeable here; since wide_circ_ids = 0 on
  120. * the fake tlschans, cell_network_size returns 512, and so with
  121. * tlschan_buf_datalen_mock_size == 1024, we should be able to write
  122. * ceil((OR_CONN_HIGHWATER - 1024) / 512) = ceil(OR_CONN_HIGHWATER / 512)
  123. * - 2 cells.
  124. */
  125. n = ch->num_cells_writeable(ch);
  126. tt_int_op(n, ==, CEIL_DIV(OR_CONN_HIGHWATER, 512) - 2);
  127. UNMOCK(buf_datalen);
  128. tlschan_buf_datalen_mock_target = NULL;
  129. tlschan_buf_datalen_mock_size = 0;
  130. if (fake_outbuf) {
  131. buf_free(TO_CONN(tlschan->conn)->outbuf);
  132. TO_CONN(tlschan->conn)->outbuf = NULL;
  133. }
  134. done:
  135. if (ch) {
  136. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  137. /*
  138. * Use fake close method that doesn't try to do too much to fake
  139. * orconn
  140. */
  141. ch->close = tlschan_fake_close_method;
  142. channel_mark_for_close(ch);
  143. free_fake_channel(ch);
  144. UNMOCK(scheduler_release_channel);
  145. }
  146. UNMOCK(connection_or_connect);
  147. UNMOCK(is_local_addr);
  148. return;
  149. }
  150. static void
  151. test_channeltls_overhead_estimate(void *arg)
  152. {
  153. tor_addr_t test_addr;
  154. channel_t *ch = NULL;
  155. const char test_digest[DIGEST_LEN] = {
  156. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  157. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
  158. double r;
  159. channel_tls_t *tlschan = NULL;
  160. (void)arg;
  161. /* Set up a fake address to fake-connect to */
  162. test_addr.family = AF_INET;
  163. test_addr.addr.in_addr.s_addr = htonl(0x01020304);
  164. /* For this test we always want the address to be treated as non-local */
  165. tlschan_local = 0;
  166. /* Install is_local_addr() mock */
  167. MOCK(is_local_addr, tlschan_is_local_addr_mock);
  168. /* Install mock for connection_or_connect() */
  169. MOCK(connection_or_connect, tlschan_connection_or_connect_mock);
  170. /* Try connecting */
  171. ch = channel_tls_connect(&test_addr, 567, test_digest);
  172. tt_assert(ch != NULL);
  173. /* First case: silly low ratios should get clamped to 1.0 */
  174. tlschan = BASE_CHAN_TO_TLS(ch);
  175. tt_assert(tlschan != NULL);
  176. tlschan->conn->bytes_xmitted = 128;
  177. tlschan->conn->bytes_xmitted_by_tls = 64;
  178. r = ch->get_overhead_estimate(ch);
  179. tt_assert(fabs(r - 1.0) < 1E-12);
  180. tlschan->conn->bytes_xmitted_by_tls = 127;
  181. r = ch->get_overhead_estimate(ch);
  182. tt_assert(fabs(r - 1.0) < 1E-12);
  183. /* Now middle of the range */
  184. tlschan->conn->bytes_xmitted_by_tls = 192;
  185. r = ch->get_overhead_estimate(ch);
  186. tt_assert(fabs(r - 1.5) < 1E-12);
  187. /* Now above the 2.0 clamp */
  188. tlschan->conn->bytes_xmitted_by_tls = 257;
  189. r = ch->get_overhead_estimate(ch);
  190. tt_assert(fabs(r - 2.0) < 1E-12);
  191. tlschan->conn->bytes_xmitted_by_tls = 512;
  192. r = ch->get_overhead_estimate(ch);
  193. tt_assert(fabs(r - 2.0) < 1E-12);
  194. done:
  195. if (ch) {
  196. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  197. /*
  198. * Use fake close method that doesn't try to do too much to fake
  199. * orconn
  200. */
  201. ch->close = tlschan_fake_close_method;
  202. channel_mark_for_close(ch);
  203. free_fake_channel(ch);
  204. UNMOCK(scheduler_release_channel);
  205. }
  206. UNMOCK(connection_or_connect);
  207. UNMOCK(is_local_addr);
  208. return;
  209. }
  210. static size_t
  211. tlschan_buf_datalen_mock(const buf_t *buf)
  212. {
  213. if (buf != NULL && buf == tlschan_buf_datalen_mock_target) {
  214. return tlschan_buf_datalen_mock_size;
  215. } else {
  216. return buf_datalen__real(buf);
  217. }
  218. }
  219. static or_connection_t *
  220. tlschan_connection_or_connect_mock(const tor_addr_t *addr,
  221. uint16_t port,
  222. const char *digest,
  223. channel_tls_t *tlschan)
  224. {
  225. or_connection_t *result = NULL;
  226. tt_assert(addr != NULL);
  227. tt_assert(port != 0);
  228. tt_assert(digest != NULL);
  229. tt_assert(tlschan != NULL);
  230. /* Make a fake orconn */
  231. result = tor_malloc_zero(sizeof(*result));
  232. result->base_.magic = OR_CONNECTION_MAGIC;
  233. result->base_.state = OR_CONN_STATE_OPEN;
  234. result->base_.type = CONN_TYPE_OR;
  235. result->base_.socket_family = addr->family;
  236. result->base_.address = tor_strdup("<fake>");
  237. memcpy(&(result->base_.addr), addr, sizeof(tor_addr_t));
  238. result->base_.port = port;
  239. memcpy(result->identity_digest, digest, DIGEST_LEN);
  240. result->chan = tlschan;
  241. memcpy(&(result->real_addr), addr, sizeof(tor_addr_t));
  242. result->tls = (tor_tls_t *)((void *)(&fake_tortls));
  243. done:
  244. return result;
  245. }
  246. static void
  247. tlschan_fake_close_method(channel_t *chan)
  248. {
  249. channel_tls_t *tlschan = NULL;
  250. tt_assert(chan != NULL);
  251. tt_int_op(chan->magic, ==, TLS_CHAN_MAGIC);
  252. tlschan = BASE_CHAN_TO_TLS(chan);
  253. tt_assert(tlschan != NULL);
  254. /* Just free the fake orconn */
  255. tor_free(tlschan->conn->base_.address);
  256. tor_free(tlschan->conn);
  257. channel_closed(chan);
  258. done:
  259. return;
  260. }
  261. static int
  262. tlschan_is_local_addr_mock(const tor_addr_t *addr)
  263. {
  264. tt_assert(addr != NULL);
  265. done:
  266. return tlschan_local;
  267. }
  268. struct testcase_t channeltls_tests[] = {
  269. { "create", test_channeltls_create, TT_FORK, NULL, NULL },
  270. { "num_bytes_queued", test_channeltls_num_bytes_queued,
  271. TT_FORK, NULL, NULL },
  272. { "overhead_estimate", test_channeltls_overhead_estimate,
  273. TT_FORK, NULL, NULL },
  274. END_OF_TESTCASES
  275. };