test_extorport.c 20 KB

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