test_extorport.c 20 KB

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