test_cell_formats.c 47 KB

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