test_extorport.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* Copyright (c) 2013-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONNECTION_PRIVATE
  4. #define EXT_ORPORT_PRIVATE
  5. #define MAINLOOP_PRIVATE
  6. #include "core/or/or.h"
  7. #include "lib/buf/buffers.h"
  8. #include "core/mainloop/connection.h"
  9. #include "core/or/connection_or.h"
  10. #include "app/config/config.h"
  11. #include "feature/control/control_events.h"
  12. #include "lib/crypt_ops/crypto_rand.h"
  13. #include "feature/relay/ext_orport.h"
  14. #include "core/mainloop/mainloop.h"
  15. #include "core/or/or_connection_st.h"
  16. #include "test/test.h"
  17. #include "test/test_helpers.h"
  18. #include "test/rng_test_helpers.h"
  19. #ifdef HAVE_SYS_STAT_H
  20. #include <sys/stat.h>
  21. #endif
  22. /* Test connection_or_remove_from_ext_or_id_map and
  23. * connection_or_set_ext_or_identifier */
  24. static void
  25. test_ext_or_id_map(void *arg)
  26. {
  27. or_connection_t *c1 = NULL, *c2 = NULL, *c3 = NULL;
  28. char *idp = NULL, *idp2 = NULL;
  29. (void)arg;
  30. /* pre-initialization */
  31. tt_ptr_op(NULL, OP_EQ,
  32. connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx"));
  33. c1 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  34. c2 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  35. c3 = or_connection_new(CONN_TYPE_OR, AF_INET);
  36. tt_ptr_op(c1->ext_or_conn_id, OP_NE, NULL);
  37. tt_ptr_op(c2->ext_or_conn_id, OP_NE, NULL);
  38. tt_ptr_op(c3->ext_or_conn_id, OP_EQ, NULL);
  39. tt_ptr_op(c1, OP_EQ, connection_or_get_by_ext_or_id(c1->ext_or_conn_id));
  40. tt_ptr_op(c2, OP_EQ, connection_or_get_by_ext_or_id(c2->ext_or_conn_id));
  41. tt_ptr_op(NULL, OP_EQ,
  42. connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx"));
  43. idp = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  44. /* Give c2 a new ID. */
  45. connection_or_set_ext_or_identifier(c2);
  46. tt_mem_op(idp, OP_NE, c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  47. idp2 = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
  48. tt_assert(!tor_digest_is_zero(idp2));
  49. tt_ptr_op(NULL, OP_EQ, connection_or_get_by_ext_or_id(idp));
  50. tt_ptr_op(c2, OP_EQ, connection_or_get_by_ext_or_id(idp2));
  51. /* Now remove it. */
  52. connection_or_remove_from_ext_or_id_map(c2);
  53. tt_ptr_op(NULL, OP_EQ, connection_or_get_by_ext_or_id(idp));
  54. tt_ptr_op(NULL, OP_EQ, connection_or_get_by_ext_or_id(idp2));
  55. done:
  56. if (c1)
  57. connection_free_minimal(TO_CONN(c1));
  58. if (c2)
  59. connection_free_minimal(TO_CONN(c2));
  60. if (c3)
  61. connection_free_minimal(TO_CONN(c3));
  62. tor_free(idp);
  63. tor_free(idp2);
  64. connection_or_clear_ext_or_id_map();
  65. }
  66. /* Simple connection_write_to_buf_impl_ replacement that unconditionally
  67. * writes to outbuf. */
  68. static void
  69. connection_write_to_buf_impl_replacement(const char *string, size_t len,
  70. connection_t *conn, int compressed)
  71. {
  72. (void) compressed;
  73. tor_assert(string);
  74. tor_assert(conn);
  75. buf_add(conn->outbuf, string, len);
  76. }
  77. static void
  78. test_ext_or_write_command(void *arg)
  79. {
  80. or_connection_t *c1;
  81. char *cp = NULL;
  82. char *buf = NULL;
  83. size_t sz;
  84. (void) arg;
  85. MOCK(connection_write_to_buf_impl_,
  86. connection_write_to_buf_impl_replacement);
  87. c1 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  88. tt_assert(c1);
  89. /* Length too long */
  90. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 100, "X", 100000),
  91. OP_LT, 0);
  92. /* Empty command */
  93. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 0x99, NULL, 0),
  94. OP_EQ, 0);
  95. cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
  96. tt_int_op(sz, OP_EQ, 4);
  97. tt_mem_op(cp, OP_EQ, "\x00\x99\x00\x00", 4);
  98. tor_free(cp);
  99. /* Medium command. */
  100. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 0x99,
  101. "Wai\0Hello", 9), OP_EQ, 0);
  102. cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
  103. tt_int_op(sz, OP_EQ, 13);
  104. tt_mem_op(cp, OP_EQ, "\x00\x99\x00\x09Wai\x00Hello", 13);
  105. tor_free(cp);
  106. /* Long command */
  107. buf = tor_malloc(65535);
  108. memset(buf, 'x', 65535);
  109. tt_int_op(connection_write_ext_or_command(TO_CONN(c1), 0xf00d,
  110. buf, 65535), OP_EQ, 0);
  111. cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
  112. tt_int_op(sz, OP_EQ, 65539);
  113. tt_mem_op(cp, OP_EQ, "\xf0\x0d\xff\xff", 4);
  114. tt_mem_op(cp+4, OP_EQ, buf, 65535);
  115. tor_free(cp);
  116. done:
  117. if (c1)
  118. connection_free_minimal(TO_CONN(c1));
  119. tor_free(cp);
  120. tor_free(buf);
  121. UNMOCK(connection_write_to_buf_impl_);
  122. }
  123. static int
  124. write_bytes_to_file_fail(const char *fname, const char *str, size_t len,
  125. int bin)
  126. {
  127. (void) fname;
  128. (void) str;
  129. (void) len;
  130. (void) bin;
  131. return -1;
  132. }
  133. static void
  134. test_ext_or_init_auth(void *arg)
  135. {
  136. or_options_t *options = get_options_mutable();
  137. const char *fn;
  138. char *cp = NULL;
  139. struct stat st;
  140. char cookie0[32];
  141. (void)arg;
  142. /* Check default filename location */
  143. tor_free(options->DataDirectory);
  144. options->DataDirectory = tor_strdup("foo");
  145. cp = get_ext_or_auth_cookie_file_name();
  146. tt_str_op(cp, OP_EQ, "foo"PATH_SEPARATOR"extended_orport_auth_cookie");
  147. tor_free(cp);
  148. /* Shouldn't be initialized already, or our tests will be a bit
  149. * meaningless */
  150. ext_or_auth_cookie = tor_malloc_zero(32);
  151. tt_assert(fast_mem_is_zero((char*)ext_or_auth_cookie, 32));
  152. /* Now make sure we use a temporary file */
  153. fn = get_fname("ext_cookie_file");
  154. options->ExtORPortCookieAuthFile = tor_strdup(fn);
  155. cp = get_ext_or_auth_cookie_file_name();
  156. tt_str_op(cp, OP_EQ, fn);
  157. tor_free(cp);
  158. /* Test the initialization function with a broken
  159. write_bytes_to_file(). See if the problem is handled properly. */
  160. MOCK(write_bytes_to_file, write_bytes_to_file_fail);
  161. tt_int_op(-1, OP_EQ, init_ext_or_cookie_authentication(1));
  162. tt_int_op(ext_or_auth_cookie_is_set, OP_EQ, 0);
  163. UNMOCK(write_bytes_to_file);
  164. /* Now do the actual initialization. */
  165. tt_int_op(0, OP_EQ, init_ext_or_cookie_authentication(1));
  166. tt_int_op(ext_or_auth_cookie_is_set, OP_EQ, 1);
  167. cp = read_file_to_str(fn, RFTS_BIN, &st);
  168. tt_ptr_op(cp, OP_NE, NULL);
  169. tt_u64_op((uint64_t)st.st_size, OP_EQ, 64);
  170. tt_mem_op(cp,OP_EQ, "! Extended ORPort Auth Cookie !\x0a", 32);
  171. tt_mem_op(cp+32,OP_EQ, ext_or_auth_cookie, 32);
  172. memcpy(cookie0, ext_or_auth_cookie, 32);
  173. tt_assert(!fast_mem_is_zero((char*)ext_or_auth_cookie, 32));
  174. /* Operation should be idempotent. */
  175. tt_int_op(0, OP_EQ, init_ext_or_cookie_authentication(1));
  176. tt_mem_op(cookie0,OP_EQ, ext_or_auth_cookie, 32);
  177. done:
  178. tor_free(cp);
  179. ext_orport_free_all();
  180. }
  181. static void
  182. test_ext_or_cookie_auth(void *arg)
  183. {
  184. char *reply=NULL, *reply2=NULL, *client_hash=NULL, *client_hash2=NULL;
  185. size_t reply_len=0;
  186. char hmac1[32], hmac2[32];
  187. const char client_nonce[32] =
  188. "Who is the third who walks alway";
  189. char server_hash_input[] =
  190. "ExtORPort authentication server-to-client hash"
  191. "Who is the third who walks alway"
  192. "................................";
  193. char client_hash_input[] =
  194. "ExtORPort authentication client-to-server hash"
  195. "Who is the third who walks alway"
  196. "................................";
  197. (void)arg;
  198. tt_int_op(strlen(client_hash_input), OP_EQ, 46+32+32);
  199. tt_int_op(strlen(server_hash_input), OP_EQ, 46+32+32);
  200. ext_or_auth_cookie = tor_malloc_zero(32);
  201. memcpy(ext_or_auth_cookie, "s beside you? When I count, ther", 32);
  202. ext_or_auth_cookie_is_set = 1;
  203. /* For this authentication, the client sends 32 random bytes (ClientNonce)
  204. * The server replies with 32 byte ServerHash and 32 byte ServerNonce,
  205. * where ServerHash is:
  206. * HMAC-SHA256(CookieString,
  207. * "ExtORPort authentication server-to-client hash" | ClientNonce |
  208. * ServerNonce)"
  209. * The client must reply with 32-byte ClientHash, which we compute as:
  210. * ClientHash is computed as:
  211. * HMAC-SHA256(CookieString,
  212. * "ExtORPort authentication client-to-server hash" | ClientNonce |
  213. * ServerNonce)
  214. */
  215. /* Wrong length */
  216. tt_int_op(-1, OP_EQ,
  217. handle_client_auth_nonce(client_nonce, 33, &client_hash, &reply,
  218. &reply_len));
  219. tt_int_op(-1, OP_EQ,
  220. handle_client_auth_nonce(client_nonce, 31, &client_hash, &reply,
  221. &reply_len));
  222. /* Now let's try this for real! */
  223. tt_int_op(0, OP_EQ,
  224. handle_client_auth_nonce(client_nonce, 32, &client_hash, &reply,
  225. &reply_len));
  226. tt_int_op(reply_len, OP_EQ, 64);
  227. tt_ptr_op(reply, OP_NE, NULL);
  228. tt_ptr_op(client_hash, OP_NE, NULL);
  229. /* Fill in the server nonce into the hash inputs... */
  230. memcpy(server_hash_input+46+32, reply+32, 32);
  231. memcpy(client_hash_input+46+32, reply+32, 32);
  232. /* Check the HMACs are correct... */
  233. crypto_hmac_sha256(hmac1, (char*)ext_or_auth_cookie, 32, server_hash_input,
  234. 46+32+32);
  235. crypto_hmac_sha256(hmac2, (char*)ext_or_auth_cookie, 32, client_hash_input,
  236. 46+32+32);
  237. tt_mem_op(hmac1,OP_EQ, reply, 32);
  238. tt_mem_op(hmac2,OP_EQ, client_hash, 32);
  239. /* Now do it again and make sure that the results are *different* */
  240. tt_int_op(0, OP_EQ,
  241. handle_client_auth_nonce(client_nonce, 32, &client_hash2, &reply2,
  242. &reply_len));
  243. tt_mem_op(reply2,OP_NE, reply, reply_len);
  244. tt_mem_op(client_hash2,OP_NE, client_hash, 32);
  245. /* But that this one checks out too. */
  246. memcpy(server_hash_input+46+32, reply2+32, 32);
  247. memcpy(client_hash_input+46+32, reply2+32, 32);
  248. /* Check the HMACs are correct... */
  249. crypto_hmac_sha256(hmac1, (char*)ext_or_auth_cookie, 32, server_hash_input,
  250. 46+32+32);
  251. crypto_hmac_sha256(hmac2, (char*)ext_or_auth_cookie, 32, client_hash_input,
  252. 46+32+32);
  253. tt_mem_op(hmac1,OP_EQ, reply2, 32);
  254. tt_mem_op(hmac2,OP_EQ, client_hash2, 32);
  255. done:
  256. tor_free(reply);
  257. tor_free(client_hash);
  258. tor_free(reply2);
  259. tor_free(client_hash2);
  260. }
  261. static void
  262. test_ext_or_cookie_auth_testvec(void *arg)
  263. {
  264. char *reply=NULL, *client_hash=NULL;
  265. size_t reply_len;
  266. char *mem_op_hex_tmp=NULL;
  267. const char client_nonce[] = "But when I look ahead up the whi";
  268. (void)arg;
  269. ext_or_auth_cookie = tor_malloc_zero(32);
  270. memcpy(ext_or_auth_cookie, "Gliding wrapt in a brown mantle," , 32);
  271. ext_or_auth_cookie_is_set = 1;
  272. testing_enable_prefilled_rng("te road There is always another ", 32);
  273. tt_int_op(0, OP_EQ,
  274. handle_client_auth_nonce(client_nonce, 32, &client_hash, &reply,
  275. &reply_len));
  276. tt_ptr_op(reply, OP_NE, NULL );
  277. tt_uint_op(reply_len, OP_EQ, 64);
  278. tt_mem_op(reply+32,OP_EQ, "te road There is always another ", 32);
  279. /* HMACSHA256("Gliding wrapt in a brown mantle,"
  280. * "ExtORPort authentication server-to-client hash"
  281. * "But when I look ahead up the write road There is always another ");
  282. */
  283. test_memeq_hex(reply,
  284. "ec80ed6e546d3b36fdfc22fe1315416b"
  285. "029f1ade7610d910878b62eeb7403821");
  286. /* HMACSHA256("Gliding wrapt in a brown mantle,"
  287. * "ExtORPort authentication client-to-server hash"
  288. * "But when I look ahead up the write road There is always another ");
  289. * (Both values computed using Python CLI.)
  290. */
  291. test_memeq_hex(client_hash,
  292. "ab391732dd2ed968cd40c087d1b1f25b"
  293. "33b3cd77ff79bd80c2074bbf438119a2");
  294. done:
  295. testing_disable_prefilled_rng();
  296. tor_free(reply);
  297. tor_free(client_hash);
  298. tor_free(mem_op_hex_tmp);
  299. }
  300. static void
  301. ignore_bootstrap_problem(const char *warn, int reason,
  302. or_connection_t *conn)
  303. {
  304. (void)warn;
  305. (void)reason;
  306. (void)conn;
  307. }
  308. static int is_reading = 1;
  309. static int handshake_start_called = 0;
  310. static void
  311. note_read_stopped(connection_t *conn)
  312. {
  313. (void)conn;
  314. is_reading=0;
  315. }
  316. static void
  317. note_read_started(connection_t *conn)
  318. {
  319. (void)conn;
  320. is_reading=1;
  321. }
  322. static int
  323. handshake_start(or_connection_t *conn, int receiving)
  324. {
  325. if (!conn || !receiving)
  326. TT_FAIL(("Bad arguments to handshake_start"));
  327. handshake_start_called = 1;
  328. return 0;
  329. }
  330. #define WRITE(s,n) \
  331. do { \
  332. buf_add(TO_CONN(conn)->inbuf, (s), (n)); \
  333. } while (0)
  334. #define CONTAINS(s,n) \
  335. do { \
  336. tt_int_op((n), OP_LE, sizeof(b)); \
  337. tt_int_op(buf_datalen(TO_CONN(conn)->outbuf), OP_EQ, (n)); \
  338. if ((n)) { \
  339. buf_get_bytes(TO_CONN(conn)->outbuf, b, (n)); \
  340. tt_mem_op(b, OP_EQ, (s), (n)); \
  341. } \
  342. } while (0)
  343. /* Helper: Do a successful Extended ORPort authentication handshake. */
  344. static void
  345. do_ext_or_handshake(or_connection_t *conn)
  346. {
  347. char b[256];
  348. tt_int_op(0, OP_EQ, connection_ext_or_start_auth(conn));
  349. CONTAINS("\x01\x00", 2);
  350. WRITE("\x01", 1);
  351. WRITE("But when I look ahead up the whi", 32);
  352. testing_enable_prefilled_rng("te road There is always another ", 32);
  353. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  354. testing_disable_prefilled_rng();
  355. tt_int_op(TO_CONN(conn)->state, OP_EQ,
  356. EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH);
  357. CONTAINS("\xec\x80\xed\x6e\x54\x6d\x3b\x36\xfd\xfc\x22\xfe\x13\x15\x41\x6b"
  358. "\x02\x9f\x1a\xde\x76\x10\xd9\x10\x87\x8b\x62\xee\xb7\x40\x38\x21"
  359. "te road There is always another ", 64);
  360. /* Send the right response this time. */
  361. WRITE("\xab\x39\x17\x32\xdd\x2e\xd9\x68\xcd\x40\xc0\x87\xd1\xb1\xf2\x5b"
  362. "\x33\xb3\xcd\x77\xff\x79\xbd\x80\xc2\x07\x4b\xbf\x43\x81\x19\xa2",
  363. 32);
  364. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  365. CONTAINS("\x01", 1);
  366. tt_assert(! TO_CONN(conn)->marked_for_close);
  367. tt_int_op(TO_CONN(conn)->state, OP_EQ, EXT_OR_CONN_STATE_OPEN);
  368. done: ;
  369. }
  370. static void
  371. test_ext_or_handshake(void *arg)
  372. {
  373. or_connection_t *conn=NULL;
  374. char b[256];
  375. (void) arg;
  376. MOCK(connection_write_to_buf_impl_,
  377. connection_write_to_buf_impl_replacement);
  378. /* Use same authenticators as for test_ext_or_cookie_auth_testvec */
  379. ext_or_auth_cookie = tor_malloc_zero(32);
  380. memcpy(ext_or_auth_cookie, "Gliding wrapt in a brown mantle," , 32);
  381. ext_or_auth_cookie_is_set = 1;
  382. tor_init_connection_lists();
  383. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  384. tt_int_op(0, OP_EQ, connection_ext_or_start_auth(conn));
  385. /* The server starts by telling us about the one supported authtype. */
  386. CONTAINS("\x01\x00", 2);
  387. /* Say the client hasn't responded yet. */
  388. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  389. /* Let's say the client replies badly. */
  390. WRITE("\x99", 1);
  391. tt_int_op(-1, OP_EQ, connection_ext_or_process_inbuf(conn));
  392. CONTAINS("", 0);
  393. tt_assert(TO_CONN(conn)->marked_for_close);
  394. close_closeable_connections();
  395. conn = NULL;
  396. /* Okay, try again. */
  397. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  398. tt_int_op(0, OP_EQ, connection_ext_or_start_auth(conn));
  399. CONTAINS("\x01\x00", 2);
  400. /* Let's say the client replies sensibly this time. "Yes, AUTHTYPE_COOKIE
  401. * sounds delicious. Let's have some of that!" */
  402. WRITE("\x01", 1);
  403. /* Let's say that the client also sends part of a nonce. */
  404. WRITE("But when I look ", 16);
  405. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  406. CONTAINS("", 0);
  407. tt_int_op(TO_CONN(conn)->state, OP_EQ,
  408. EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE);
  409. /* Pump it again. Nothing should happen. */
  410. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  411. /* send the rest of the nonce. */
  412. WRITE("ahead up the whi", 16);
  413. testing_enable_prefilled_rng("te road There is always another ", 32);
  414. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  415. testing_disable_prefilled_rng();
  416. /* We should get the right reply from the server. */
  417. CONTAINS("\xec\x80\xed\x6e\x54\x6d\x3b\x36\xfd\xfc\x22\xfe\x13\x15\x41\x6b"
  418. "\x02\x9f\x1a\xde\x76\x10\xd9\x10\x87\x8b\x62\xee\xb7\x40\x38\x21"
  419. "te road There is always another ", 64);
  420. /* Send the wrong response. */
  421. WRITE("not with a bang but a whimper...", 32);
  422. MOCK(control_event_bootstrap_prob_or, ignore_bootstrap_problem);
  423. tt_int_op(-1, OP_EQ, connection_ext_or_process_inbuf(conn));
  424. CONTAINS("\x00", 1);
  425. tt_assert(TO_CONN(conn)->marked_for_close);
  426. /* XXXX Hold-open-until-flushed. */
  427. close_closeable_connections();
  428. conn = NULL;
  429. UNMOCK(control_event_bootstrap_prob_or);
  430. MOCK(connection_start_reading, note_read_started);
  431. MOCK(connection_stop_reading, note_read_stopped);
  432. MOCK(connection_tls_start_handshake, handshake_start);
  433. /* Okay, this time let's succeed. */
  434. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  435. do_ext_or_handshake(conn);
  436. /* Now let's run through some messages. */
  437. /* First let's send some junk and make sure it's ignored. */
  438. WRITE("\xff\xf0\x00\x03""ABC", 7);
  439. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  440. CONTAINS("", 0);
  441. /* Now let's send a USERADDR command. */
  442. WRITE("\x00\x01\x00\x0c""1.2.3.4:5678", 16);
  443. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  444. tt_int_op(TO_CONN(conn)->port, OP_EQ, 5678);
  445. tt_int_op(tor_addr_to_ipv4h(&TO_CONN(conn)->addr), OP_EQ, 0x01020304);
  446. /* Now let's send a TRANSPORT command. */
  447. WRITE("\x00\x02\x00\x07""rfc1149", 11);
  448. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  449. tt_ptr_op(NULL, OP_NE, conn->ext_or_transport);
  450. tt_str_op("rfc1149", OP_EQ, conn->ext_or_transport);
  451. tt_int_op(is_reading,OP_EQ,1);
  452. tt_int_op(TO_CONN(conn)->state, OP_EQ, EXT_OR_CONN_STATE_OPEN);
  453. /* DONE */
  454. WRITE("\x00\x00\x00\x00", 4);
  455. tt_int_op(0, OP_EQ, connection_ext_or_process_inbuf(conn));
  456. tt_int_op(TO_CONN(conn)->state, OP_EQ, EXT_OR_CONN_STATE_FLUSHING);
  457. tt_int_op(is_reading,OP_EQ,0);
  458. CONTAINS("\x10\x00\x00\x00", 4);
  459. tt_int_op(handshake_start_called,OP_EQ,0);
  460. tt_int_op(0, OP_EQ, connection_ext_or_finished_flushing(conn));
  461. tt_int_op(is_reading,OP_EQ,1);
  462. tt_int_op(handshake_start_called,OP_EQ,1);
  463. tt_int_op(TO_CONN(conn)->type, OP_EQ, CONN_TYPE_OR);
  464. tt_int_op(TO_CONN(conn)->state, OP_EQ, 0);
  465. close_closeable_connections();
  466. conn = NULL;
  467. /* Okay, this time let's succeed the handshake but fail the USERADDR
  468. command. */
  469. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  470. do_ext_or_handshake(conn);
  471. /* USERADDR command with an extra NUL byte */
  472. WRITE("\x00\x01\x00\x0d""1.2.3.4:5678\x00", 17);
  473. MOCK(control_event_bootstrap_prob_or, ignore_bootstrap_problem);
  474. tt_int_op(-1, OP_EQ, connection_ext_or_process_inbuf(conn));
  475. CONTAINS("", 0);
  476. tt_assert(TO_CONN(conn)->marked_for_close);
  477. close_closeable_connections();
  478. conn = NULL;
  479. UNMOCK(control_event_bootstrap_prob_or);
  480. /* Now fail the TRANSPORT command. */
  481. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  482. do_ext_or_handshake(conn);
  483. /* TRANSPORT command with an extra NUL byte */
  484. WRITE("\x00\x02\x00\x08""rfc1149\x00", 12);
  485. MOCK(control_event_bootstrap_prob_or, ignore_bootstrap_problem);
  486. tt_int_op(-1, OP_EQ, connection_ext_or_process_inbuf(conn));
  487. CONTAINS("", 0);
  488. tt_assert(TO_CONN(conn)->marked_for_close);
  489. close_closeable_connections();
  490. conn = NULL;
  491. UNMOCK(control_event_bootstrap_prob_or);
  492. /* Now fail the TRANSPORT command. */
  493. conn = or_connection_new(CONN_TYPE_EXT_OR, AF_INET);
  494. do_ext_or_handshake(conn);
  495. /* TRANSPORT command with transport name with symbols (not a
  496. C-identifier) */
  497. WRITE("\x00\x02\x00\x07""rf*1149", 11);
  498. MOCK(control_event_bootstrap_prob_or, ignore_bootstrap_problem);
  499. tt_int_op(-1, OP_EQ, connection_ext_or_process_inbuf(conn));
  500. CONTAINS("", 0);
  501. tt_assert(TO_CONN(conn)->marked_for_close);
  502. close_closeable_connections();
  503. conn = NULL;
  504. UNMOCK(control_event_bootstrap_prob_or);
  505. done:
  506. UNMOCK(connection_write_to_buf_impl_);
  507. testing_disable_prefilled_rng();
  508. if (conn)
  509. connection_free_minimal(TO_CONN(conn));
  510. #undef CONTAINS
  511. #undef WRITE
  512. }
  513. struct testcase_t extorport_tests[] = {
  514. { "id_map", test_ext_or_id_map, TT_FORK, NULL, NULL },
  515. { "write_command", test_ext_or_write_command, TT_FORK, NULL, NULL },
  516. { "init_auth", test_ext_or_init_auth, TT_FORK, NULL, NULL },
  517. { "cookie_auth", test_ext_or_cookie_auth, TT_FORK, NULL, NULL },
  518. { "cookie_auth_testvec", test_ext_or_cookie_auth_testvec, TT_FORK,
  519. NULL, NULL },
  520. { "handshake", test_ext_or_handshake, TT_FORK, &helper_pubsub_setup, NULL },
  521. END_OF_TESTCASES
  522. };