test_extorport.c 20 KB

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