test_cell_formats.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONNECTION_EDGE_PRIVATE
  7. #define RELAY_PRIVATE
  8. #include "or.h"
  9. #include "channel.h"
  10. #include "connection_edge.h"
  11. #include "connection_or.h"
  12. #include "onion.h"
  13. #include "onion_tap.h"
  14. #include "onion_fast.h"
  15. #include "onion_ntor.h"
  16. #include "relay.h"
  17. #include "test.h"
  18. #include <stdlib.h>
  19. #include <string.h>
  20. static void
  21. test_cfmt_relay_header(void *arg)
  22. {
  23. relay_header_t rh;
  24. const uint8_t hdr_1[RELAY_HEADER_SIZE] =
  25. "\x03" "\x00\x00" "\x21\x22" "ABCD" "\x01\x03";
  26. uint8_t hdr_out[RELAY_HEADER_SIZE];
  27. (void)arg;
  28. tt_int_op(sizeof(hdr_1), ==, RELAY_HEADER_SIZE);
  29. relay_header_unpack(&rh, hdr_1);
  30. tt_int_op(rh.command, ==, 3);
  31. tt_int_op(rh.recognized, ==, 0);
  32. tt_int_op(rh.stream_id, ==, 0x2122);
  33. test_mem_op(rh.integrity, ==, "ABCD", 4);
  34. tt_int_op(rh.length, ==, 0x103);
  35. relay_header_pack(hdr_out, &rh);
  36. test_mem_op(hdr_out, ==, hdr_1, RELAY_HEADER_SIZE);
  37. done:
  38. ;
  39. }
  40. static void
  41. make_relay_cell(cell_t *out, uint8_t command,
  42. const void *body, size_t bodylen)
  43. {
  44. relay_header_t rh;
  45. memset(&rh, 0, sizeof(rh));
  46. rh.stream_id = 5;
  47. rh.command = command;
  48. rh.length = bodylen;
  49. out->command = CELL_RELAY;
  50. out->circ_id = 10;
  51. relay_header_pack(out->payload, &rh);
  52. memcpy(out->payload + RELAY_HEADER_SIZE, body, bodylen);
  53. }
  54. static void
  55. test_cfmt_begin_cells(void *arg)
  56. {
  57. cell_t cell;
  58. begin_cell_t bcell;
  59. uint8_t end_reason;
  60. (void)arg;
  61. /* Try begindir. */
  62. memset(&bcell, 0x7f, sizeof(bcell));
  63. make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "", 0);
  64. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  65. tt_ptr_op(NULL, ==, bcell.address);
  66. tt_int_op(0, ==, bcell.flags);
  67. tt_int_op(0, ==, bcell.port);
  68. tt_int_op(5, ==, bcell.stream_id);
  69. tt_int_op(1, ==, bcell.is_begindir);
  70. /* A Begindir with extra stuff. */
  71. memset(&bcell, 0x7f, sizeof(bcell));
  72. make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "12345", 5);
  73. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  74. tt_ptr_op(NULL, ==, bcell.address);
  75. tt_int_op(0, ==, bcell.flags);
  76. tt_int_op(0, ==, bcell.port);
  77. tt_int_op(5, ==, bcell.stream_id);
  78. tt_int_op(1, ==, bcell.is_begindir);
  79. /* A short but valid begin cell */
  80. memset(&bcell, 0x7f, sizeof(bcell));
  81. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:9", 6);
  82. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  83. tt_str_op("a.b", ==, bcell.address);
  84. tt_int_op(0, ==, bcell.flags);
  85. tt_int_op(9, ==, bcell.port);
  86. tt_int_op(5, ==, bcell.stream_id);
  87. tt_int_op(0, ==, bcell.is_begindir);
  88. tor_free(bcell.address);
  89. /* A significantly loner begin cell */
  90. memset(&bcell, 0x7f, sizeof(bcell));
  91. {
  92. const char c[] = "here-is-a-nice-long.hostname.com:65535";
  93. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, strlen(c)+1);
  94. }
  95. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  96. tt_str_op("here-is-a-nice-long.hostname.com", ==, bcell.address);
  97. tt_int_op(0, ==, bcell.flags);
  98. tt_int_op(65535, ==, bcell.port);
  99. tt_int_op(5, ==, bcell.stream_id);
  100. tt_int_op(0, ==, bcell.is_begindir);
  101. tor_free(bcell.address);
  102. /* An IPv4 begin cell. */
  103. memset(&bcell, 0x7f, sizeof(bcell));
  104. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "18.9.22.169:80", 15);
  105. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  106. tt_str_op("18.9.22.169", ==, bcell.address);
  107. tt_int_op(0, ==, bcell.flags);
  108. tt_int_op(80, ==, bcell.port);
  109. tt_int_op(5, ==, bcell.stream_id);
  110. tt_int_op(0, ==, bcell.is_begindir);
  111. tor_free(bcell.address);
  112. /* An IPv6 begin cell. Let's make sure we handle colons*/
  113. memset(&bcell, 0x7f, sizeof(bcell));
  114. make_relay_cell(&cell, RELAY_COMMAND_BEGIN,
  115. "[2620::6b0:b:1a1a:0:26e5:480e]:80", 34);
  116. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  117. tt_str_op("[2620::6b0:b:1a1a:0:26e5:480e]", ==, bcell.address);
  118. tt_int_op(0, ==, bcell.flags);
  119. tt_int_op(80, ==, bcell.port);
  120. tt_int_op(5, ==, bcell.stream_id);
  121. tt_int_op(0, ==, bcell.is_begindir);
  122. tor_free(bcell.address);
  123. /* a begin cell with extra junk but not enough for flags. */
  124. memset(&bcell, 0x7f, sizeof(bcell));
  125. {
  126. const char c[] = "another.example.com:80\x00\x01\x02";
  127. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  128. }
  129. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  130. tt_str_op("another.example.com", ==, bcell.address);
  131. tt_int_op(0, ==, bcell.flags);
  132. tt_int_op(80, ==, bcell.port);
  133. tt_int_op(5, ==, bcell.stream_id);
  134. tt_int_op(0, ==, bcell.is_begindir);
  135. tor_free(bcell.address);
  136. /* a begin cell with flags. */
  137. memset(&bcell, 0x7f, sizeof(bcell));
  138. {
  139. const char c[] = "another.example.com:443\x00\x01\x02\x03\x04";
  140. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  141. }
  142. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  143. tt_str_op("another.example.com", ==, bcell.address);
  144. tt_int_op(0x1020304, ==, bcell.flags);
  145. tt_int_op(443, ==, bcell.port);
  146. tt_int_op(5, ==, bcell.stream_id);
  147. tt_int_op(0, ==, bcell.is_begindir);
  148. tor_free(bcell.address);
  149. /* a begin cell with flags and even more cruft after that. */
  150. memset(&bcell, 0x7f, sizeof(bcell));
  151. {
  152. const char c[] = "a-further.example.com:22\x00\xee\xaa\x00\xffHi mom";
  153. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  154. }
  155. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  156. tt_str_op("a-further.example.com", ==, bcell.address);
  157. tt_int_op(0xeeaa00ff, ==, bcell.flags);
  158. tt_int_op(22, ==, bcell.port);
  159. tt_int_op(5, ==, bcell.stream_id);
  160. tt_int_op(0, ==, bcell.is_begindir);
  161. tor_free(bcell.address);
  162. /* bad begin cell: impossible length. */
  163. memset(&bcell, 0x7f, sizeof(bcell));
  164. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 7);
  165. cell.payload[9] = 0x01; /* Set length to 510 */
  166. cell.payload[10] = 0xfe;
  167. {
  168. relay_header_t rh;
  169. relay_header_unpack(&rh, cell.payload);
  170. tt_int_op(rh.length, ==, 510);
  171. }
  172. tt_int_op(-2, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  173. /* Bad begin cell: no body. */
  174. memset(&bcell, 0x7f, sizeof(bcell));
  175. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
  176. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  177. /* bad begin cell: no body. */
  178. memset(&bcell, 0x7f, sizeof(bcell));
  179. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
  180. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  181. /* bad begin cell: no colon */
  182. memset(&bcell, 0x7f, sizeof(bcell));
  183. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b", 4);
  184. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  185. /* bad begin cell: no ports */
  186. memset(&bcell, 0x7f, sizeof(bcell));
  187. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:", 5);
  188. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  189. /* bad begin cell: bad port */
  190. memset(&bcell, 0x7f, sizeof(bcell));
  191. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:xyz", 8);
  192. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  193. memset(&bcell, 0x7f, sizeof(bcell));
  194. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:100000", 11);
  195. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  196. /* bad begin cell: no nul */
  197. memset(&bcell, 0x7f, sizeof(bcell));
  198. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 6);
  199. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  200. done:
  201. tor_free(bcell.address);
  202. }
  203. static void
  204. test_cfmt_connected_cells(void *arg)
  205. {
  206. relay_header_t rh;
  207. cell_t cell;
  208. tor_addr_t addr;
  209. int ttl, r;
  210. char *mem_op_hex_tmp = NULL;
  211. (void)arg;
  212. /* Let's try an oldschool one with nothing in it. */
  213. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "", 0);
  214. relay_header_unpack(&rh, cell.payload);
  215. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  216. tt_int_op(r, ==, 0);
  217. tt_int_op(tor_addr_family(&addr), ==, AF_UNSPEC);
  218. tt_int_op(ttl, ==, -1);
  219. /* A slightly less oldschool one: only an IPv4 address */
  220. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x20\x30\x40\x50", 4);
  221. relay_header_unpack(&rh, cell.payload);
  222. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  223. tt_int_op(r, ==, 0);
  224. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  225. tt_str_op(fmt_addr(&addr), ==, "32.48.64.80");
  226. tt_int_op(ttl, ==, -1);
  227. /* Bogus but understandable: truncated TTL */
  228. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x11\x12\x13\x14\x15", 5);
  229. relay_header_unpack(&rh, cell.payload);
  230. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  231. tt_int_op(r, ==, 0);
  232. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  233. tt_str_op(fmt_addr(&addr), ==, "17.18.19.20");
  234. tt_int_op(ttl, ==, -1);
  235. /* Regular IPv4 one: address and TTL */
  236. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  237. "\x02\x03\x04\x05\x00\x00\x0e\x10", 8);
  238. relay_header_unpack(&rh, cell.payload);
  239. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  240. tt_int_op(r, ==, 0);
  241. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  242. tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
  243. tt_int_op(ttl, ==, 3600);
  244. /* IPv4 with too-big TTL */
  245. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  246. "\x02\x03\x04\x05\xf0\x00\x00\x00", 8);
  247. relay_header_unpack(&rh, cell.payload);
  248. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  249. tt_int_op(r, ==, 0);
  250. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  251. tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
  252. tt_int_op(ttl, ==, -1);
  253. /* IPv6 (ttl is mandatory) */
  254. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  255. "\x00\x00\x00\x00\x06"
  256. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  257. "\x00\x00\x00\x00\x00\x00\x00\x68"
  258. "\x00\x00\x02\x58", 25);
  259. relay_header_unpack(&rh, cell.payload);
  260. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  261. tt_int_op(r, ==, 0);
  262. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  263. tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
  264. tt_int_op(ttl, ==, 600);
  265. /* IPv6 (ttl too big) */
  266. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  267. "\x00\x00\x00\x00\x06"
  268. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  269. "\x00\x00\x00\x00\x00\x00\x00\x68"
  270. "\x90\x00\x02\x58", 25);
  271. relay_header_unpack(&rh, cell.payload);
  272. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  273. tt_int_op(r, ==, 0);
  274. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  275. tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
  276. tt_int_op(ttl, ==, -1);
  277. /* Bogus size: 3. */
  278. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  279. "\x00\x01\x02", 3);
  280. relay_header_unpack(&rh, cell.payload);
  281. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  282. tt_int_op(r, ==, -1);
  283. /* Bogus family: 7. */
  284. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  285. "\x00\x00\x00\x00\x07"
  286. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  287. "\x00\x00\x00\x00\x00\x00\x00\x68"
  288. "\x90\x00\x02\x58", 25);
  289. relay_header_unpack(&rh, cell.payload);
  290. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  291. tt_int_op(r, ==, -1);
  292. /* Truncated IPv6. */
  293. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  294. "\x00\x00\x00\x00\x06"
  295. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  296. "\x00\x00\x00\x00\x00\x00\x00\x68"
  297. "\x00\x00\x02", 24);
  298. relay_header_unpack(&rh, cell.payload);
  299. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  300. tt_int_op(r, ==, -1);
  301. /* Now make sure we can generate connected cells correctly. */
  302. /* Try an IPv4 address */
  303. memset(&rh, 0, sizeof(rh));
  304. memset(&cell, 0, sizeof(cell));
  305. tor_addr_parse(&addr, "30.40.50.60");
  306. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  307. &addr, 128);
  308. tt_int_op(rh.length, ==, 8);
  309. test_memeq_hex(cell.payload+RELAY_HEADER_SIZE, "1e28323c" "00000080");
  310. /* Try parsing it. */
  311. tor_addr_make_unspec(&addr);
  312. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  313. tt_int_op(r, ==, 0);
  314. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  315. tt_str_op(fmt_addr(&addr), ==, "30.40.50.60");
  316. tt_int_op(ttl, ==, 128);
  317. /* Try an IPv6 address */
  318. memset(&rh, 0, sizeof(rh));
  319. memset(&cell, 0, sizeof(cell));
  320. tor_addr_parse(&addr, "2620::6b0:b:1a1a:0:26e5:480e");
  321. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  322. &addr, 3600);
  323. tt_int_op(rh.length, ==, 25);
  324. test_memeq_hex(cell.payload + RELAY_HEADER_SIZE,
  325. "00000000" "06"
  326. "2620000006b0000b1a1a000026e5480e" "00000e10");
  327. /* Try parsing it. */
  328. tor_addr_make_unspec(&addr);
  329. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  330. tt_int_op(r, ==, 0);
  331. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  332. tt_str_op(fmt_addr(&addr), ==, "2620:0:6b0:b:1a1a:0:26e5:480e");
  333. tt_int_op(ttl, ==, 3600);
  334. done:
  335. tor_free(mem_op_hex_tmp);
  336. }
  337. static void
  338. test_cfmt_create_cells(void *arg)
  339. {
  340. uint8_t b[MAX_ONIONSKIN_CHALLENGE_LEN];
  341. create_cell_t cc;
  342. cell_t cell;
  343. cell_t cell2;
  344. (void)arg;
  345. /* === Let's try parsing some good cells! */
  346. /* A valid create cell. */
  347. memset(&cell, 0, sizeof(cell));
  348. memset(b, 0, sizeof(b));
  349. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  350. cell.command = CELL_CREATE;
  351. memcpy(cell.payload, b, TAP_ONIONSKIN_CHALLENGE_LEN);
  352. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  353. tt_int_op(CELL_CREATE, ==, cc.cell_type);
  354. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
  355. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
  356. test_memeq(cc.onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  357. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  358. tt_int_op(cell.command, ==, cell2.command);
  359. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  360. /* A valid create_fast cell. */
  361. memset(&cell, 0, sizeof(cell));
  362. memset(b, 0, sizeof(b));
  363. crypto_rand((char*)b, CREATE_FAST_LEN);
  364. cell.command = CELL_CREATE_FAST;
  365. memcpy(cell.payload, b, CREATE_FAST_LEN);
  366. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  367. tt_int_op(CELL_CREATE_FAST, ==, cc.cell_type);
  368. tt_int_op(ONION_HANDSHAKE_TYPE_FAST, ==, cc.handshake_type);
  369. tt_int_op(CREATE_FAST_LEN, ==, cc.handshake_len);
  370. test_memeq(cc.onionskin, b, CREATE_FAST_LEN + 10);
  371. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  372. tt_int_op(cell.command, ==, cell2.command);
  373. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  374. /* A valid create2 cell with a TAP payload */
  375. memset(&cell, 0, sizeof(cell));
  376. memset(b, 0, sizeof(b));
  377. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  378. cell.command = CELL_CREATE2;
  379. memcpy(cell.payload, "\x00\x00\x00\xBA", 4); /* TAP, 186 bytes long */
  380. memcpy(cell.payload+4, b, TAP_ONIONSKIN_CHALLENGE_LEN);
  381. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  382. tt_int_op(CELL_CREATE2, ==, cc.cell_type);
  383. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
  384. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
  385. test_memeq(cc.onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  386. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  387. tt_int_op(cell.command, ==, cell2.command);
  388. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  389. /* A valid create2 cell with an ntor payload */
  390. memset(&cell, 0, sizeof(cell));
  391. memset(b, 0, sizeof(b));
  392. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  393. cell.command = CELL_CREATE2;
  394. memcpy(cell.payload, "\x00\x02\x00\x54", 4); /* ntor, 84 bytes long */
  395. memcpy(cell.payload+4, b, NTOR_ONIONSKIN_LEN);
  396. #ifdef CURVE25519_ENABLED
  397. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  398. tt_int_op(CELL_CREATE2, ==, cc.cell_type);
  399. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
  400. tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
  401. test_memeq(cc.onionskin, b, NTOR_ONIONSKIN_LEN + 10);
  402. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  403. tt_int_op(cell.command, ==, cell2.command);
  404. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  405. #else
  406. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  407. #endif
  408. /* A valid create cell with an ntor payload, in legacy format. */
  409. memset(&cell, 0, sizeof(cell));
  410. memset(b, 0, sizeof(b));
  411. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  412. cell.command = CELL_CREATE;
  413. memcpy(cell.payload, "ntorNTORntorNTOR", 16);
  414. memcpy(cell.payload+16, b, NTOR_ONIONSKIN_LEN);
  415. #ifdef CURVE25519_ENABLED
  416. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  417. tt_int_op(CELL_CREATE, ==, cc.cell_type);
  418. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
  419. tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
  420. test_memeq(cc.onionskin, b, NTOR_ONIONSKIN_LEN + 10);
  421. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  422. tt_int_op(cell.command, ==, cell2.command);
  423. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  424. #else
  425. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  426. #endif
  427. /* == Okay, now let's try to parse some impossible stuff. */
  428. /* It has to be some kind of a create cell! */
  429. cell.command = CELL_CREATED;
  430. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  431. /* You can't acutally make an unparseable CREATE or CREATE_FAST cell. */
  432. /* Try some CREATE2 cells. First with a bad type. */
  433. cell.command = CELL_CREATE2;
  434. memcpy(cell.payload, "\x00\x50\x00\x99", 4); /* Type 0x50???? */
  435. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  436. /* Now a good type with an incorrect length. */
  437. memcpy(cell.payload, "\x00\x00\x00\xBC", 4); /* TAP, 187 bytes.*/
  438. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  439. /* Now a good type with a ridiculous length. */
  440. memcpy(cell.payload, "\x00\x00\x02\x00", 4); /* TAP, 512 bytes.*/
  441. tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
  442. /* == Time to try formatting bad cells. The important thing is that
  443. we reject big lengths, so just check that for now. */
  444. cc.handshake_len = 512;
  445. tt_int_op(-1, ==, create_cell_format(&cell2, &cc));
  446. /* == Try formatting a create2 cell we don't understand. XXXX */
  447. done:
  448. ;
  449. }
  450. static void
  451. test_cfmt_created_cells(void *arg)
  452. {
  453. uint8_t b[512];
  454. created_cell_t cc;
  455. cell_t cell;
  456. cell_t cell2;
  457. (void)arg;
  458. /* A good CREATED cell */
  459. memset(&cell, 0, sizeof(cell));
  460. memset(b, 0, sizeof(b));
  461. crypto_rand((char*)b, TAP_ONIONSKIN_REPLY_LEN);
  462. cell.command = CELL_CREATED;
  463. memcpy(cell.payload, b, TAP_ONIONSKIN_REPLY_LEN);
  464. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  465. tt_int_op(CELL_CREATED, ==, cc.cell_type);
  466. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, cc.handshake_len);
  467. test_memeq(cc.reply, b, TAP_ONIONSKIN_REPLY_LEN + 10);
  468. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  469. tt_int_op(cell.command, ==, cell2.command);
  470. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  471. /* A good CREATED_FAST cell */
  472. memset(&cell, 0, sizeof(cell));
  473. memset(b, 0, sizeof(b));
  474. crypto_rand((char*)b, CREATED_FAST_LEN);
  475. cell.command = CELL_CREATED_FAST;
  476. memcpy(cell.payload, b, CREATED_FAST_LEN);
  477. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  478. tt_int_op(CELL_CREATED_FAST, ==, cc.cell_type);
  479. tt_int_op(CREATED_FAST_LEN, ==, cc.handshake_len);
  480. test_memeq(cc.reply, b, CREATED_FAST_LEN + 10);
  481. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  482. tt_int_op(cell.command, ==, cell2.command);
  483. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  484. /* A good CREATED2 cell with short reply */
  485. memset(&cell, 0, sizeof(cell));
  486. memset(b, 0, sizeof(b));
  487. crypto_rand((char*)b, 64);
  488. cell.command = CELL_CREATED2;
  489. memcpy(cell.payload, "\x00\x40", 2);
  490. memcpy(cell.payload+2, b, 64);
  491. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  492. tt_int_op(CELL_CREATED2, ==, cc.cell_type);
  493. tt_int_op(64, ==, cc.handshake_len);
  494. test_memeq(cc.reply, b, 80);
  495. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  496. tt_int_op(cell.command, ==, cell2.command);
  497. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  498. /* A good CREATED2 cell with maximal reply */
  499. memset(&cell, 0, sizeof(cell));
  500. memset(b, 0, sizeof(b));
  501. crypto_rand((char*)b, 496);
  502. cell.command = CELL_CREATED2;
  503. memcpy(cell.payload, "\x01\xF0", 2);
  504. memcpy(cell.payload+2, b, 496);
  505. tt_int_op(0, ==, created_cell_parse(&cc, &cell));
  506. tt_int_op(CELL_CREATED2, ==, cc.cell_type);
  507. tt_int_op(496, ==, cc.handshake_len);
  508. test_memeq(cc.reply, b, 496);
  509. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  510. tt_int_op(cell.command, ==, cell2.command);
  511. test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
  512. /* Bogus CREATED2 cell: too long! */
  513. memset(&cell, 0, sizeof(cell));
  514. memset(b, 0, sizeof(b));
  515. crypto_rand((char*)b, 496);
  516. cell.command = CELL_CREATED2;
  517. memcpy(cell.payload, "\x01\xF1", 2);
  518. tt_int_op(-1, ==, created_cell_parse(&cc, &cell));
  519. /* Unformattable CREATED2 cell: too long! */
  520. cc.handshake_len = 497;
  521. tt_int_op(-1, ==, created_cell_format(&cell2, &cc));
  522. done:
  523. ;
  524. }
  525. static void
  526. test_cfmt_extend_cells(void *arg)
  527. {
  528. cell_t cell;
  529. uint8_t b[512];
  530. extend_cell_t ec;
  531. create_cell_t *cc = &ec.create_cell;
  532. uint8_t p[RELAY_PAYLOAD_SIZE];
  533. uint8_t p2[RELAY_PAYLOAD_SIZE];
  534. uint8_t p2_cmd;
  535. uint16_t p2_len;
  536. char *mem_op_hex_tmp = NULL;
  537. (void) arg;
  538. /* Let's start with a simple EXTEND cell. */
  539. memset(p, 0, sizeof(p));
  540. memset(b, 0, sizeof(b));
  541. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  542. memcpy(p, "\x12\xf4\x00\x01\x01\x02", 6); /* 18 244 0 1 : 258 */
  543. memcpy(p+6,b,TAP_ONIONSKIN_CHALLENGE_LEN);
  544. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, "electroencephalogram", 20);
  545. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  546. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  547. tt_int_op(RELAY_COMMAND_EXTEND, ==, ec.cell_type);
  548. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  549. tt_int_op(258, ==, ec.orport_ipv4.port);
  550. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  551. test_memeq(ec.node_id, "electroencephalogram", 20);
  552. tt_int_op(cc->cell_type, ==, CELL_CREATE);
  553. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_TAP);
  554. tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_CHALLENGE_LEN);
  555. test_memeq(cc->onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN+20);
  556. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  557. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
  558. tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  559. test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
  560. /* Let's do an ntor stuffed in a legacy EXTEND cell */
  561. memset(p, 0, sizeof(p));
  562. memset(b, 0, sizeof(b));
  563. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  564. memcpy(p, "\x12\xf4\x00\x01\x01\x02", 6); /* 18 244 0 1 : 258 */
  565. memcpy(p+6,"ntorNTORntorNTOR", 16);
  566. memcpy(p+22, b, NTOR_ONIONSKIN_LEN);
  567. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, "electroencephalogram", 20);
  568. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  569. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  570. tt_int_op(RELAY_COMMAND_EXTEND, ==, ec.cell_type);
  571. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  572. tt_int_op(258, ==, ec.orport_ipv4.port);
  573. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  574. test_memeq(ec.node_id, "electroencephalogram", 20);
  575. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  576. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
  577. tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
  578. test_memeq(cc->onionskin, b, NTOR_ONIONSKIN_LEN+20);
  579. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  580. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
  581. tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  582. test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
  583. tt_int_op(0, ==, create_cell_format_relayed(&cell, cc));
  584. /* Now let's do a minimal ntor EXTEND2 cell. */
  585. memset(&ec, 0xff, sizeof(ec));
  586. memset(p, 0, sizeof(p));
  587. memset(b, 0, sizeof(b));
  588. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  589. /* 2 items; one 18.244.0.1:61681 */
  590. memcpy(p, "\x02\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  591. /* The other is a digest. */
  592. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  593. /* Prep for the handshake: type and length */
  594. memcpy(p+31, "\x00\x02\x00\x54", 4);
  595. memcpy(p+35, b, NTOR_ONIONSKIN_LEN);
  596. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  597. p, 35+NTOR_ONIONSKIN_LEN));
  598. tt_int_op(RELAY_COMMAND_EXTEND2, ==, ec.cell_type);
  599. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  600. tt_int_op(61681, ==, ec.orport_ipv4.port);
  601. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  602. test_memeq(ec.node_id, "anarchoindividualist", 20);
  603. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  604. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
  605. tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
  606. test_memeq(cc->onionskin, b, NTOR_ONIONSKIN_LEN+20);
  607. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  608. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
  609. tt_int_op(p2_len, ==, 35+NTOR_ONIONSKIN_LEN);
  610. test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
  611. /* Now let's do a fanciful EXTEND2 cell. */
  612. memset(&ec, 0xff, sizeof(ec));
  613. memset(p, 0, sizeof(p));
  614. memset(b, 0, sizeof(b));
  615. crypto_rand((char*)b, 99);
  616. /* 4 items; one 18 244 0 1 61681 */
  617. memcpy(p, "\x04\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  618. /* One is a digest. */
  619. memcpy(p+9, "\x02\x14" "anthropomorphization", 22);
  620. /* One is an ipv6 address */
  621. memcpy(p+31, "\x01\x12\x20\x02\x00\x00\x00\x00\x00\x00"
  622. "\x00\x00\x00\x00\x00\xf0\xc5\x1e\x11\x12", 20);
  623. /* One is the Konami code. */
  624. memcpy(p+51, "\xf0\x20upupdowndownleftrightleftrightba", 34);
  625. /* Prep for the handshake: weird type and length */
  626. memcpy(p+85, "\x01\x05\x00\x63", 4);
  627. memcpy(p+89, b, 99);
  628. tt_int_op(0, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, 89+99));
  629. tt_int_op(RELAY_COMMAND_EXTEND2, ==, ec.cell_type);
  630. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  631. tt_int_op(61681, ==, ec.orport_ipv4.port);
  632. tt_str_op("2002::f0:c51e", ==, fmt_addr(&ec.orport_ipv6.addr));
  633. tt_int_op(4370, ==, ec.orport_ipv6.port);
  634. test_memeq(ec.node_id, "anthropomorphization", 20);
  635. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  636. tt_int_op(cc->handshake_type, ==, 0x105);
  637. tt_int_op(cc->handshake_len, ==, 99);
  638. test_memeq(cc->onionskin, b, 99+20);
  639. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  640. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
  641. /* We'll generate it minus the IPv6 address and minus the konami code */
  642. tt_int_op(p2_len, ==, 89+99-34-20);
  643. test_memeq_hex(p2,
  644. /* Two items: one that same darn IP address. */
  645. "02000612F40001F0F1"
  646. /* The next is a digest : anthropomorphization */
  647. "0214616e7468726f706f6d6f727068697a6174696f6e"
  648. /* Now the handshake prologue */
  649. "01050063");
  650. test_memeq(p2+1+8+22+4, b, 99+20);
  651. tt_int_op(0, ==, create_cell_format_relayed(&cell, cc));
  652. /* == Now try parsing some junk */
  653. /* Try a too-long handshake */
  654. memset(p, 0, sizeof(p));
  655. memcpy(p, "\x02\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  656. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  657. memcpy(p+31, "\xff\xff\x01\xd0", 4);
  658. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  659. p, sizeof(p)));
  660. /* Try two identities. */
  661. memset(p, 0, sizeof(p));
  662. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  663. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  664. memcpy(p+31, "\x02\x14" "autodepolymerization", 22);
  665. memcpy(p+53, "\xff\xff\x00\x10", 4);
  666. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  667. p, sizeof(p)));
  668. /* No identities. */
  669. memset(p, 0, sizeof(p));
  670. memcpy(p, "\x01\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  671. memcpy(p+53, "\xff\xff\x00\x10", 4);
  672. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  673. p, sizeof(p)));
  674. /* Try a bad IPv4 address (too long, too short)*/
  675. memset(p, 0, sizeof(p));
  676. memcpy(p, "\x02\x00\x07\x12\xf4\x00\x01\xf0\xf1\xff", 10);
  677. memcpy(p+10, "\x02\x14" "anarchoindividualist", 22);
  678. memcpy(p+32, "\xff\xff\x00\x10", 4);
  679. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  680. p, sizeof(p)));
  681. memset(p, 0, sizeof(p));
  682. memcpy(p, "\x02\x00\x05\x12\xf4\x00\x01\xf0", 8);
  683. memcpy(p+8, "\x02\x14" "anarchoindividualist", 22);
  684. memcpy(p+30, "\xff\xff\x00\x10", 4);
  685. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  686. p, sizeof(p)));
  687. /* IPv6 address (too long, too short, no IPv4)*/
  688. memset(p, 0, sizeof(p));
  689. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  690. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  691. memcpy(p+31, "\x01\x13" "xxxxxxxxxxxxxxxxYYZ", 19);
  692. memcpy(p+50, "\xff\xff\x00\x20", 4);
  693. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  694. p, sizeof(p)));
  695. memset(p, 0, sizeof(p));
  696. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  697. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  698. memcpy(p+31, "\x01\x11" "xxxxxxxxxxxxxxxxY", 17);
  699. memcpy(p+48, "\xff\xff\x00\x20", 4);
  700. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  701. p, sizeof(p)));
  702. memset(p, 0, sizeof(p));
  703. memcpy(p, "\x02", 1);
  704. memcpy(p+1, "\x02\x14" "anarchoindividualist", 22);
  705. memcpy(p+23, "\x01\x12" "xxxxxxxxxxxxxxxxYY", 18);
  706. memcpy(p+41, "\xff\xff\x00\x20", 4);
  707. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  708. p, sizeof(p)));
  709. /* Running out of space in specifiers */
  710. memset(p,0,sizeof(p));
  711. memcpy(p, "\x05\x0a\xff", 3);
  712. memcpy(p+3+255, "\x0a\xff", 2);
  713. tt_int_op(-1, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  714. p, sizeof(p)));
  715. /* Fuzz, because why not. */
  716. memset(&ec, 0xff, sizeof(ec));
  717. {
  718. int i;
  719. memset(p, 0, sizeof(p));
  720. for (i = 0; i < 10000; ++i) {
  721. int n = crypto_rand_int(sizeof(p));
  722. crypto_rand((char *)p, n);
  723. extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, n);
  724. }
  725. }
  726. done:
  727. tor_free(mem_op_hex_tmp);
  728. }
  729. static void
  730. test_cfmt_extended_cells(void *arg)
  731. {
  732. uint8_t b[512];
  733. extended_cell_t ec;
  734. created_cell_t *cc = &ec.created_cell;
  735. uint8_t p[RELAY_PAYLOAD_SIZE];
  736. uint8_t p2[RELAY_PAYLOAD_SIZE];
  737. uint8_t p2_cmd;
  738. uint16_t p2_len;
  739. char *mem_op_hex_tmp = NULL;
  740. (void) arg;
  741. /* Try a regular EXTENDED cell. */
  742. memset(&ec, 0xff, sizeof(ec));
  743. memset(p, 0, sizeof(p));
  744. memset(b, 0, sizeof(b));
  745. crypto_rand((char*)b, TAP_ONIONSKIN_REPLY_LEN);
  746. memcpy(p,b,TAP_ONIONSKIN_REPLY_LEN);
  747. tt_int_op(0, ==, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED, p,
  748. TAP_ONIONSKIN_REPLY_LEN));
  749. tt_int_op(RELAY_COMMAND_EXTENDED, ==, ec.cell_type);
  750. tt_int_op(cc->cell_type, ==, CELL_CREATED);
  751. tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_REPLY_LEN);
  752. test_memeq(cc->reply, b, TAP_ONIONSKIN_REPLY_LEN);
  753. tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  754. tt_int_op(RELAY_COMMAND_EXTENDED, ==, p2_cmd);
  755. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, p2_len);
  756. test_memeq(p2, p, sizeof(p2));
  757. /* Try an EXTENDED2 cell */
  758. memset(&ec, 0xff, sizeof(ec));
  759. memset(p, 0, sizeof(p));
  760. memset(b, 0, sizeof(b));
  761. crypto_rand((char*)b, 42);
  762. memcpy(p,"\x00\x2a",2);
  763. memcpy(p+2,b,42);
  764. tt_int_op(0, ==, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, 2+42));
  765. tt_int_op(RELAY_COMMAND_EXTENDED2, ==, ec.cell_type);
  766. tt_int_op(cc->cell_type, ==, CELL_CREATED2);
  767. tt_int_op(cc->handshake_len, ==, 42);
  768. test_memeq(cc->reply, b, 42+10);
  769. tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  770. tt_int_op(RELAY_COMMAND_EXTENDED2, ==, p2_cmd);
  771. tt_int_op(2+42, ==, p2_len);
  772. test_memeq(p2, p, sizeof(p2));
  773. /* Try an almost-too-long EXTENDED2 cell */
  774. memcpy(p, "\x01\xf0", 2);
  775. tt_int_op(0, ==,
  776. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  777. /* Now try a too-long extended2 cell. That's the only misparse I can think
  778. * of. */
  779. memcpy(p, "\x01\xf1", 2);
  780. tt_int_op(-1, ==,
  781. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  782. done:
  783. tor_free(mem_op_hex_tmp);
  784. }
  785. static void
  786. test_cfmt_resolved_cells(void *arg)
  787. {
  788. smartlist_t *addrs = smartlist_new();
  789. relay_header_t rh;
  790. cell_t cell;
  791. int r, errcode;
  792. address_ttl_t *a;
  793. (void)arg;
  794. #define CLEAR_CELL() do { \
  795. memset(&cell, 0, sizeof(cell)); \
  796. memset(&rh, 0, sizeof(rh)); \
  797. } while (0)
  798. #define CLEAR_ADDRS() do { \
  799. SMARTLIST_FOREACH(addrs, address_ttl_t *, a, \
  800. address_ttl_free(a); ); \
  801. smartlist_clear(addrs); \
  802. } while (0)
  803. #define SET_CELL(s) do { \
  804. CLEAR_CELL(); \
  805. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  806. rh.length = sizeof((s))-1; \
  807. rh.command = RELAY_COMMAND_RESOLVED; \
  808. errcode = -1; \
  809. } while (0)
  810. /* The cell format is one or more answers; each of the form
  811. * type [1 byte---0:hostname, 4:ipv4, 6:ipv6, f0:err-transient, f1:err]
  812. * length [1 byte]
  813. * body [length bytes]
  814. * ttl [4 bytes]
  815. */
  816. /* Let's try an empty cell */
  817. SET_CELL("");
  818. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  819. tt_int_op(errcode, ==, 0);
  820. tt_int_op(r, ==, 0);
  821. tt_int_op(smartlist_len(addrs), ==, 0);
  822. CLEAR_ADDRS(); /* redundant but let's be consistent */
  823. /* Cell with one ipv4 addr */
  824. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  825. tt_int_op(rh.length, ==, 10);
  826. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  827. tt_int_op(errcode, ==, 0);
  828. tt_int_op(r, ==, 0);
  829. tt_int_op(smartlist_len(addrs), ==, 1);
  830. a = smartlist_get(addrs, 0);
  831. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  832. tt_ptr_op(a->hostname, ==, NULL);
  833. tt_int_op(a->ttl, ==, 256);
  834. CLEAR_ADDRS();
  835. /* Cell with one ipv6 addr */
  836. SET_CELL("\x06\x10"
  837. "\x20\x02\x90\x90\x00\x00\x00\x00"
  838. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  839. "\x02\00\x00\x01");
  840. tt_int_op(rh.length, ==, 22);
  841. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  842. tt_int_op(errcode, ==, 0);
  843. tt_int_op(r, ==, 0);
  844. tt_int_op(smartlist_len(addrs), ==, 1);
  845. a = smartlist_get(addrs, 0);
  846. tt_str_op(fmt_addr(&a->addr), ==, "2002:9090::f0f0:abcd");
  847. tt_ptr_op(a->hostname, ==, NULL);
  848. tt_int_op(a->ttl, ==, 0x2000001);
  849. CLEAR_ADDRS();
  850. /* Cell with one hostname */
  851. SET_CELL("\x00\x11"
  852. "motherbrain.zebes"
  853. "\x00\00\x00\x00");
  854. tt_int_op(rh.length, ==, 23);
  855. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  856. tt_int_op(errcode, ==, 0);
  857. tt_int_op(r, ==, 0);
  858. tt_int_op(smartlist_len(addrs), ==, 1);
  859. a = smartlist_get(addrs, 0);
  860. tt_assert(tor_addr_is_null(&a->addr));
  861. tt_str_op(a->hostname, ==, "motherbrain.zebes");
  862. tt_int_op(a->ttl, ==, 0);
  863. CLEAR_ADDRS();
  864. #define LONG_NAME \
  865. "this-hostname-has-255-characters.in-order-to-test-whether-very-long.ho" \
  866. "stnames-are-accepted.i-am-putting-it-in-a-macro-because-although.this-" \
  867. "function-is-already-very-full.of-copy-and-pasted-stuff.having-this-app" \
  868. "ear-more-than-once-would-bother-me-somehow.is"
  869. tt_int_op(strlen(LONG_NAME), ==, 255);
  870. SET_CELL("\x00\xff"
  871. LONG_NAME
  872. "\x00\01\x00\x00");
  873. tt_int_op(rh.length, ==, 261);
  874. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  875. tt_int_op(errcode, ==, 0);
  876. tt_int_op(r, ==, 0);
  877. tt_int_op(smartlist_len(addrs), ==, 1);
  878. a = smartlist_get(addrs, 0);
  879. tt_assert(tor_addr_is_null(&a->addr));
  880. tt_str_op(a->hostname, ==, LONG_NAME);
  881. tt_int_op(a->ttl, ==, 65536);
  882. CLEAR_ADDRS();
  883. /* Cells with an error */
  884. SET_CELL("\xf0\x2b"
  885. "I'm sorry, Dave. I'm afraid I can't do that"
  886. "\x00\x11\x22\x33");
  887. tt_int_op(rh.length, ==, 49);
  888. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  889. tt_int_op(errcode, ==, RESOLVED_TYPE_ERROR_TRANSIENT);
  890. tt_int_op(r, ==, 0);
  891. tt_int_op(smartlist_len(addrs), ==, 0);
  892. CLEAR_ADDRS();
  893. SET_CELL("\xf1\x40"
  894. "This hostname is too important for me to allow you to resolve it"
  895. "\x00\x00\x00\x00");
  896. tt_int_op(rh.length, ==, 70);
  897. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  898. tt_int_op(errcode, ==, RESOLVED_TYPE_ERROR);
  899. tt_int_op(r, ==, 0);
  900. tt_int_op(smartlist_len(addrs), ==, 0);
  901. CLEAR_ADDRS();
  902. /* Cell with an unrecognized type */
  903. SET_CELL("\xee\x16"
  904. "fault in the AE35 unit"
  905. "\x09\x09\x01\x01");
  906. tt_int_op(rh.length, ==, 28);
  907. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  908. tt_int_op(errcode, ==, 0);
  909. tt_int_op(r, ==, 0);
  910. tt_int_op(smartlist_len(addrs), ==, 0);
  911. CLEAR_ADDRS();
  912. /* Cell with one of each */
  913. SET_CELL(/* unrecognized: */
  914. "\xee\x16"
  915. "fault in the AE35 unit"
  916. "\x09\x09\x01\x01"
  917. /* error: */
  918. "\xf0\x2b"
  919. "I'm sorry, Dave. I'm afraid I can't do that"
  920. "\x00\x11\x22\x33"
  921. /* IPv6: */
  922. "\x06\x10"
  923. "\x20\x02\x90\x90\x00\x00\x00\x00"
  924. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  925. "\x02\00\x00\x01"
  926. /* IPv4: */
  927. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  928. /* Hostname: */
  929. "\x00\x11"
  930. "motherbrain.zebes"
  931. "\x00\00\x00\x00"
  932. );
  933. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  934. tt_int_op(errcode, ==, 0); /* no error reported; we got answers */
  935. tt_int_op(r, ==, 0);
  936. tt_int_op(smartlist_len(addrs), ==, 3);
  937. a = smartlist_get(addrs, 0);
  938. tt_str_op(fmt_addr(&a->addr), ==, "2002:9090::f0f0:abcd");
  939. tt_ptr_op(a->hostname, ==, NULL);
  940. tt_int_op(a->ttl, ==, 0x2000001);
  941. a = smartlist_get(addrs, 1);
  942. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  943. tt_ptr_op(a->hostname, ==, NULL);
  944. tt_int_op(a->ttl, ==, 256);
  945. a = smartlist_get(addrs, 2);
  946. tt_assert(tor_addr_is_null(&a->addr));
  947. tt_str_op(a->hostname, ==, "motherbrain.zebes");
  948. tt_int_op(a->ttl, ==, 0);
  949. CLEAR_ADDRS();
  950. /* Cell with several of similar type */
  951. SET_CELL(/* IPv4 */
  952. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  953. "\x04\x04" "\x08\x08\x08\x08" "\x00\00\x01\x05"
  954. "\x04\x04" "\x7f\xb0\x02\xb0" "\x00\01\xff\xff"
  955. /* IPv6 */
  956. "\x06\x10"
  957. "\x20\x02\x90\x00\x00\x00\x00\x00"
  958. "\x00\x00\x00\x00\xca\xfe\xf0\x0d"
  959. "\x00\00\x00\x01"
  960. "\x06\x10"
  961. "\x20\x02\x90\x01\x00\x00\x00\x00"
  962. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  963. "\x00\00\x00\x03");
  964. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  965. tt_int_op(errcode, ==, 0);
  966. tt_int_op(r, ==, 0);
  967. tt_int_op(smartlist_len(addrs), ==, 5);
  968. a = smartlist_get(addrs, 0);
  969. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  970. tt_ptr_op(a->hostname, ==, NULL);
  971. tt_int_op(a->ttl, ==, 256);
  972. a = smartlist_get(addrs, 1);
  973. tt_str_op(fmt_addr(&a->addr), ==, "8.8.8.8");
  974. tt_ptr_op(a->hostname, ==, NULL);
  975. tt_int_op(a->ttl, ==, 261);
  976. a = smartlist_get(addrs, 2);
  977. tt_str_op(fmt_addr(&a->addr), ==, "127.176.2.176");
  978. tt_ptr_op(a->hostname, ==, NULL);
  979. tt_int_op(a->ttl, ==, 131071);
  980. a = smartlist_get(addrs, 3);
  981. tt_str_op(fmt_addr(&a->addr), ==, "2002:9000::cafe:f00d");
  982. tt_ptr_op(a->hostname, ==, NULL);
  983. tt_int_op(a->ttl, ==, 1);
  984. a = smartlist_get(addrs, 4);
  985. tt_str_op(fmt_addr(&a->addr), ==, "2002:9001::fa:cade");
  986. tt_ptr_op(a->hostname, ==, NULL);
  987. tt_int_op(a->ttl, ==, 3);
  988. CLEAR_ADDRS();
  989. /* Full cell */
  990. #define LONG_NAME2 \
  991. "this-name-has-231-characters.so-that-it-plus-LONG_NAME-can-completely-" \
  992. "fill-up-the-payload-of-a-cell.its-important-to-check-for-the-full-thin" \
  993. "g-case.to-avoid-off-by-one-errors.where-full-things-are-misreported-as" \
  994. ".overflowing-by-one.z"
  995. tt_int_op(strlen(LONG_NAME2), ==, 231);
  996. SET_CELL("\x00\xff"
  997. LONG_NAME
  998. "\x00\01\x00\x00"
  999. "\x00\xe7"
  1000. LONG_NAME2
  1001. "\x00\01\x00\x00");
  1002. tt_int_op(rh.length, ==, RELAY_PAYLOAD_SIZE);
  1003. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1004. tt_int_op(errcode, ==, 0);
  1005. tt_int_op(r, ==, 0);
  1006. tt_int_op(smartlist_len(addrs), ==, 2);
  1007. a = smartlist_get(addrs, 0);
  1008. tt_str_op(a->hostname, ==, LONG_NAME);
  1009. a = smartlist_get(addrs, 1);
  1010. tt_str_op(a->hostname, ==, LONG_NAME2);
  1011. CLEAR_ADDRS();
  1012. /* BAD CELLS */
  1013. /* Invalid length on an IPv4 */
  1014. SET_CELL("\x04\x03zzz1234");
  1015. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1016. tt_int_op(errcode, ==, 0);
  1017. tt_int_op(r, ==, -1);
  1018. tt_int_op(smartlist_len(addrs), ==, 0);
  1019. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1020. "\x04\x05zzzzz1234");
  1021. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1022. tt_int_op(errcode, ==, 0);
  1023. tt_int_op(r, ==, -1);
  1024. tt_int_op(smartlist_len(addrs), ==, 0);
  1025. /* Invalid length on an IPv6 */
  1026. SET_CELL("\x06\x03zzz1234");
  1027. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1028. tt_int_op(errcode, ==, 0);
  1029. tt_int_op(r, ==, -1);
  1030. tt_int_op(smartlist_len(addrs), ==, 0);
  1031. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1032. "\x06\x17wwwwwwwwwwwwwwwww1234");
  1033. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1034. tt_int_op(errcode, ==, 0);
  1035. tt_int_op(r, ==, -1);
  1036. tt_int_op(smartlist_len(addrs), ==, 0);
  1037. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1038. "\x06\x10xxxx");
  1039. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1040. tt_int_op(errcode, ==, 0);
  1041. tt_int_op(r, ==, -1);
  1042. tt_int_op(smartlist_len(addrs), ==, 0);
  1043. /* Empty hostname */
  1044. SET_CELL("\x00\x00xxxx");
  1045. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1046. tt_int_op(errcode, ==, 0);
  1047. tt_int_op(r, ==, -1);
  1048. tt_int_op(smartlist_len(addrs), ==, 0);
  1049. /* rh.length out of range */
  1050. CLEAR_CELL();
  1051. rh.length = 499;
  1052. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1053. tt_int_op(errcode, ==, 0);
  1054. tt_int_op(r, ==, -1);
  1055. tt_int_op(smartlist_len(addrs), ==, 0);
  1056. /* Item length extends beyond rh.length */
  1057. CLEAR_CELL();
  1058. SET_CELL("\x00\xff"
  1059. LONG_NAME
  1060. "\x00\01\x00\x00");
  1061. rh.length -= 1;
  1062. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1063. tt_int_op(r, ==, -1);
  1064. tt_int_op(smartlist_len(addrs), ==, 0);
  1065. rh.length -= 5;
  1066. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1067. tt_int_op(r, ==, -1);
  1068. tt_int_op(smartlist_len(addrs), ==, 0);
  1069. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  1070. rh.length -= 1;
  1071. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1072. tt_int_op(r, ==, -1);
  1073. tt_int_op(smartlist_len(addrs), ==, 0);
  1074. SET_CELL("\xee\x10"
  1075. "\x20\x02\x90\x01\x00\x00\x00\x00"
  1076. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  1077. "\x00\00\x00\x03");
  1078. rh.length -= 1;
  1079. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1080. tt_int_op(r, ==, -1);
  1081. tt_int_op(smartlist_len(addrs), ==, 0);
  1082. /* Truncated item after first character */
  1083. SET_CELL("\x04");
  1084. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1085. tt_int_op(r, ==, -1);
  1086. tt_int_op(smartlist_len(addrs), ==, 0);
  1087. SET_CELL("\xee");
  1088. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1089. tt_int_op(r, ==, -1);
  1090. tt_int_op(smartlist_len(addrs), ==, 0);
  1091. done:
  1092. CLEAR_ADDRS();
  1093. CLEAR_CELL();
  1094. smartlist_free(addrs);
  1095. #undef CLEAR_ADDRS
  1096. #undef CLEAR_CELL
  1097. }
  1098. static void
  1099. test_cfmt_is_destroy(void *arg)
  1100. {
  1101. cell_t cell;
  1102. packed_cell_t packed;
  1103. circid_t circid = 0;
  1104. channel_t *chan;
  1105. (void)arg;
  1106. chan = tor_malloc_zero(sizeof(channel_t));
  1107. memset(&cell, 0xff, sizeof(cell));
  1108. cell.circ_id = 3003;
  1109. cell.command = CELL_RELAY;
  1110. cell_pack(&packed, &cell, 0);
  1111. chan->wide_circ_ids = 0;
  1112. tt_assert(! packed_cell_is_destroy(chan, &packed, &circid));
  1113. tt_int_op(circid, ==, 0);
  1114. cell_pack(&packed, &cell, 1);
  1115. chan->wide_circ_ids = 1;
  1116. tt_assert(! packed_cell_is_destroy(chan, &packed, &circid));
  1117. tt_int_op(circid, ==, 0);
  1118. cell.command = CELL_DESTROY;
  1119. cell_pack(&packed, &cell, 0);
  1120. chan->wide_circ_ids = 0;
  1121. tt_assert(packed_cell_is_destroy(chan, &packed, &circid));
  1122. tt_int_op(circid, ==, 3003);
  1123. circid = 0;
  1124. cell_pack(&packed, &cell, 1);
  1125. chan->wide_circ_ids = 1;
  1126. tt_assert(packed_cell_is_destroy(chan, &packed, &circid));
  1127. done:
  1128. tor_free(chan);
  1129. }
  1130. #define TEST(name, flags) \
  1131. { #name, test_cfmt_ ## name, flags, 0, NULL }
  1132. struct testcase_t cell_format_tests[] = {
  1133. TEST(relay_header, 0),
  1134. TEST(begin_cells, 0),
  1135. TEST(connected_cells, 0),
  1136. TEST(create_cells, 0),
  1137. TEST(created_cells, 0),
  1138. TEST(extend_cells, 0),
  1139. TEST(extended_cells, 0),
  1140. TEST(resolved_cells, 0),
  1141. TEST(is_destroy, 0),
  1142. END_OF_TESTCASES
  1143. };