test_channeltls.c 9.5 KB

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