test_cell_formats.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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), OP_EQ, RELAY_HEADER_SIZE);
  29. relay_header_unpack(&rh, hdr_1);
  30. tt_int_op(rh.command, OP_EQ, 3);
  31. tt_int_op(rh.recognized, OP_EQ, 0);
  32. tt_int_op(rh.stream_id, OP_EQ, 0x2122);
  33. tt_mem_op(rh.integrity, OP_EQ, "ABCD", 4);
  34. tt_int_op(rh.length, OP_EQ, 0x103);
  35. relay_header_pack(hdr_out, &rh);
  36. tt_mem_op(hdr_out, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  65. tt_ptr_op(NULL, OP_EQ, bcell.address);
  66. tt_int_op(0, OP_EQ, bcell.flags);
  67. tt_int_op(0, OP_EQ, bcell.port);
  68. tt_int_op(5, OP_EQ, bcell.stream_id);
  69. tt_int_op(1, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  74. tt_ptr_op(NULL, OP_EQ, bcell.address);
  75. tt_int_op(0, OP_EQ, bcell.flags);
  76. tt_int_op(0, OP_EQ, bcell.port);
  77. tt_int_op(5, OP_EQ, bcell.stream_id);
  78. tt_int_op(1, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  83. tt_str_op("a.b", OP_EQ, bcell.address);
  84. tt_int_op(0, OP_EQ, bcell.flags);
  85. tt_int_op(9, OP_EQ, bcell.port);
  86. tt_int_op(5, OP_EQ, bcell.stream_id);
  87. tt_int_op(0, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  96. tt_str_op("here-is-a-nice-long.hostname.com", OP_EQ, bcell.address);
  97. tt_int_op(0, OP_EQ, bcell.flags);
  98. tt_int_op(65535, OP_EQ, bcell.port);
  99. tt_int_op(5, OP_EQ, bcell.stream_id);
  100. tt_int_op(0, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  106. tt_str_op("18.9.22.169", OP_EQ, bcell.address);
  107. tt_int_op(0, OP_EQ, bcell.flags);
  108. tt_int_op(80, OP_EQ, bcell.port);
  109. tt_int_op(5, OP_EQ, bcell.stream_id);
  110. tt_int_op(0, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  117. tt_str_op("[2620::6b0:b:1a1a:0:26e5:480e]", OP_EQ, bcell.address);
  118. tt_int_op(0, OP_EQ, bcell.flags);
  119. tt_int_op(80, OP_EQ, bcell.port);
  120. tt_int_op(5, OP_EQ, bcell.stream_id);
  121. tt_int_op(0, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  130. tt_str_op("another.example.com", OP_EQ, bcell.address);
  131. tt_int_op(0, OP_EQ, bcell.flags);
  132. tt_int_op(80, OP_EQ, bcell.port);
  133. tt_int_op(5, OP_EQ, bcell.stream_id);
  134. tt_int_op(0, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  143. tt_str_op("another.example.com", OP_EQ, bcell.address);
  144. tt_int_op(0x1020304, OP_EQ, bcell.flags);
  145. tt_int_op(443, OP_EQ, bcell.port);
  146. tt_int_op(5, OP_EQ, bcell.stream_id);
  147. tt_int_op(0, OP_EQ, 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, OP_EQ, begin_cell_parse(&cell, &bcell, &end_reason));
  156. tt_str_op("a-further.example.com", OP_EQ, bcell.address);
  157. tt_int_op(0xeeaa00ff, OP_EQ, bcell.flags);
  158. tt_int_op(22, OP_EQ, bcell.port);
  159. tt_int_op(5, OP_EQ, bcell.stream_id);
  160. tt_int_op(0, OP_EQ, 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, OP_EQ, 510);
  171. }
  172. tt_int_op(-2, OP_EQ, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 0);
  217. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_UNSPEC);
  218. tt_int_op(ttl, OP_EQ, -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, OP_EQ, 0);
  224. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET);
  225. tt_str_op(fmt_addr(&addr), OP_EQ, "32.48.64.80");
  226. tt_int_op(ttl, OP_EQ, -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, OP_EQ, 0);
  232. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET);
  233. tt_str_op(fmt_addr(&addr), OP_EQ, "17.18.19.20");
  234. tt_int_op(ttl, OP_EQ, -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, OP_EQ, 0);
  241. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET);
  242. tt_str_op(fmt_addr(&addr), OP_EQ, "2.3.4.5");
  243. tt_int_op(ttl, OP_EQ, 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, OP_EQ, 0);
  250. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET);
  251. tt_str_op(fmt_addr(&addr), OP_EQ, "2.3.4.5");
  252. tt_int_op(ttl, OP_EQ, -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, OP_EQ, 0);
  262. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET6);
  263. tt_str_op(fmt_addr(&addr), OP_EQ, "2607:f8b0:400c:c02::68");
  264. tt_int_op(ttl, OP_EQ, 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, OP_EQ, 0);
  274. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET6);
  275. tt_str_op(fmt_addr(&addr), OP_EQ, "2607:f8b0:400c:c02::68");
  276. tt_int_op(ttl, OP_EQ, -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, OP_EQ, -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, OP_EQ, -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, OP_EQ, -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, 1024);
  308. tt_int_op(rh.length, OP_EQ, 8);
  309. test_memeq_hex(cell.payload+RELAY_HEADER_SIZE, "1e28323c" "00000e10");
  310. /* Try parsing it. */
  311. tor_addr_make_unspec(&addr);
  312. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  313. tt_int_op(r, OP_EQ, 0);
  314. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET);
  315. tt_str_op(fmt_addr(&addr), OP_EQ, "30.40.50.60");
  316. tt_int_op(ttl, OP_EQ, 3600); /* not 1024, since we clipped to 3600 */
  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, OP_EQ, 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, OP_EQ, 0);
  331. tt_int_op(tor_addr_family(&addr), OP_EQ, AF_INET6);
  332. tt_str_op(fmt_addr(&addr), OP_EQ, "2620:0:6b0:b:1a1a:0:26e5:480e");
  333. tt_int_op(ttl, OP_EQ, 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, OP_EQ, create_cell_parse(&cc, &cell));
  353. tt_int_op(CELL_CREATE, OP_EQ, cc.cell_type);
  354. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, OP_EQ, cc.handshake_type);
  355. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, OP_EQ, cc.handshake_len);
  356. tt_mem_op(cc.onionskin,OP_EQ, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  357. tt_int_op(0, OP_EQ, create_cell_format(&cell2, &cc));
  358. tt_int_op(cell.command, OP_EQ, cell2.command);
  359. tt_mem_op(cell.payload,OP_EQ, 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, OP_EQ, create_cell_parse(&cc, &cell));
  367. tt_int_op(CELL_CREATE_FAST, OP_EQ, cc.cell_type);
  368. tt_int_op(ONION_HANDSHAKE_TYPE_FAST, OP_EQ, cc.handshake_type);
  369. tt_int_op(CREATE_FAST_LEN, OP_EQ, cc.handshake_len);
  370. tt_mem_op(cc.onionskin,OP_EQ, b, CREATE_FAST_LEN + 10);
  371. tt_int_op(0, OP_EQ, create_cell_format(&cell2, &cc));
  372. tt_int_op(cell.command, OP_EQ, cell2.command);
  373. tt_mem_op(cell.payload,OP_EQ, 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, OP_EQ, create_cell_parse(&cc, &cell));
  382. tt_int_op(CELL_CREATE2, OP_EQ, cc.cell_type);
  383. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, OP_EQ, cc.handshake_type);
  384. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, OP_EQ, cc.handshake_len);
  385. tt_mem_op(cc.onionskin,OP_EQ, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  386. tt_int_op(0, OP_EQ, create_cell_format(&cell2, &cc));
  387. tt_int_op(cell.command, OP_EQ, cell2.command);
  388. tt_mem_op(cell.payload,OP_EQ, 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. tt_int_op(0, OP_EQ, create_cell_parse(&cc, &cell));
  397. tt_int_op(CELL_CREATE2, OP_EQ, cc.cell_type);
  398. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, OP_EQ, cc.handshake_type);
  399. tt_int_op(NTOR_ONIONSKIN_LEN, OP_EQ, cc.handshake_len);
  400. tt_mem_op(cc.onionskin,OP_EQ, b, NTOR_ONIONSKIN_LEN + 10);
  401. tt_int_op(0, OP_EQ, create_cell_format(&cell2, &cc));
  402. tt_int_op(cell.command, OP_EQ, cell2.command);
  403. tt_mem_op(cell.payload,OP_EQ, cell2.payload, CELL_PAYLOAD_SIZE);
  404. /* A valid create cell with an ntor payload, in legacy format. */
  405. memset(&cell, 0, sizeof(cell));
  406. memset(b, 0, sizeof(b));
  407. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  408. cell.command = CELL_CREATE;
  409. memcpy(cell.payload, "ntorNTORntorNTOR", 16);
  410. memcpy(cell.payload+16, b, NTOR_ONIONSKIN_LEN);
  411. tt_int_op(0, OP_EQ, create_cell_parse(&cc, &cell));
  412. tt_int_op(CELL_CREATE, OP_EQ, cc.cell_type);
  413. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, OP_EQ, cc.handshake_type);
  414. tt_int_op(NTOR_ONIONSKIN_LEN, OP_EQ, cc.handshake_len);
  415. tt_mem_op(cc.onionskin,OP_EQ, b, NTOR_ONIONSKIN_LEN + 10);
  416. tt_int_op(0, OP_EQ, create_cell_format(&cell2, &cc));
  417. tt_int_op(cell.command, OP_EQ, cell2.command);
  418. tt_mem_op(cell.payload,OP_EQ, cell2.payload, CELL_PAYLOAD_SIZE);
  419. /* == Okay, now let's try to parse some impossible stuff. */
  420. /* It has to be some kind of a create cell! */
  421. cell.command = CELL_CREATED;
  422. tt_int_op(-1, OP_EQ, create_cell_parse(&cc, &cell));
  423. /* You can't acutally make an unparseable CREATE or CREATE_FAST cell. */
  424. /* Try some CREATE2 cells. First with a bad type. */
  425. cell.command = CELL_CREATE2;
  426. memcpy(cell.payload, "\x00\x50\x00\x99", 4); /* Type 0x50???? */
  427. tt_int_op(-1, OP_EQ, create_cell_parse(&cc, &cell));
  428. /* Now a good type with an incorrect length. */
  429. memcpy(cell.payload, "\x00\x00\x00\xBC", 4); /* TAP, 187 bytes.*/
  430. tt_int_op(-1, OP_EQ, create_cell_parse(&cc, &cell));
  431. /* Now a good type with a ridiculous length. */
  432. memcpy(cell.payload, "\x00\x00\x02\x00", 4); /* TAP, 512 bytes.*/
  433. tt_int_op(-1, OP_EQ, create_cell_parse(&cc, &cell));
  434. /* == Time to try formatting bad cells. The important thing is that
  435. we reject big lengths, so just check that for now. */
  436. cc.handshake_len = 512;
  437. tt_int_op(-1, OP_EQ, create_cell_format(&cell2, &cc));
  438. /* == Try formatting a create2 cell we don't understand. XXXX */
  439. done:
  440. ;
  441. }
  442. static void
  443. test_cfmt_created_cells(void *arg)
  444. {
  445. uint8_t b[512];
  446. created_cell_t cc;
  447. cell_t cell;
  448. cell_t cell2;
  449. (void)arg;
  450. /* A good CREATED cell */
  451. memset(&cell, 0, sizeof(cell));
  452. memset(b, 0, sizeof(b));
  453. crypto_rand((char*)b, TAP_ONIONSKIN_REPLY_LEN);
  454. cell.command = CELL_CREATED;
  455. memcpy(cell.payload, b, TAP_ONIONSKIN_REPLY_LEN);
  456. tt_int_op(0, OP_EQ, created_cell_parse(&cc, &cell));
  457. tt_int_op(CELL_CREATED, OP_EQ, cc.cell_type);
  458. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, OP_EQ, cc.handshake_len);
  459. tt_mem_op(cc.reply,OP_EQ, b, TAP_ONIONSKIN_REPLY_LEN + 10);
  460. tt_int_op(0, OP_EQ, created_cell_format(&cell2, &cc));
  461. tt_int_op(cell.command, OP_EQ, cell2.command);
  462. tt_mem_op(cell.payload,OP_EQ, cell2.payload, CELL_PAYLOAD_SIZE);
  463. /* A good CREATED_FAST cell */
  464. memset(&cell, 0, sizeof(cell));
  465. memset(b, 0, sizeof(b));
  466. crypto_rand((char*)b, CREATED_FAST_LEN);
  467. cell.command = CELL_CREATED_FAST;
  468. memcpy(cell.payload, b, CREATED_FAST_LEN);
  469. tt_int_op(0, OP_EQ, created_cell_parse(&cc, &cell));
  470. tt_int_op(CELL_CREATED_FAST, OP_EQ, cc.cell_type);
  471. tt_int_op(CREATED_FAST_LEN, OP_EQ, cc.handshake_len);
  472. tt_mem_op(cc.reply,OP_EQ, b, CREATED_FAST_LEN + 10);
  473. tt_int_op(0, OP_EQ, created_cell_format(&cell2, &cc));
  474. tt_int_op(cell.command, OP_EQ, cell2.command);
  475. tt_mem_op(cell.payload,OP_EQ, cell2.payload, CELL_PAYLOAD_SIZE);
  476. /* A good CREATED2 cell with short reply */
  477. memset(&cell, 0, sizeof(cell));
  478. memset(b, 0, sizeof(b));
  479. crypto_rand((char*)b, 64);
  480. cell.command = CELL_CREATED2;
  481. memcpy(cell.payload, "\x00\x40", 2);
  482. memcpy(cell.payload+2, b, 64);
  483. tt_int_op(0, OP_EQ, created_cell_parse(&cc, &cell));
  484. tt_int_op(CELL_CREATED2, OP_EQ, cc.cell_type);
  485. tt_int_op(64, OP_EQ, cc.handshake_len);
  486. tt_mem_op(cc.reply,OP_EQ, b, 80);
  487. tt_int_op(0, OP_EQ, created_cell_format(&cell2, &cc));
  488. tt_int_op(cell.command, OP_EQ, cell2.command);
  489. tt_mem_op(cell.payload,OP_EQ, cell2.payload, CELL_PAYLOAD_SIZE);
  490. /* A good CREATED2 cell with maximal reply */
  491. memset(&cell, 0, sizeof(cell));
  492. memset(b, 0, sizeof(b));
  493. crypto_rand((char*)b, 496);
  494. cell.command = CELL_CREATED2;
  495. memcpy(cell.payload, "\x01\xF0", 2);
  496. memcpy(cell.payload+2, b, 496);
  497. tt_int_op(0, OP_EQ, created_cell_parse(&cc, &cell));
  498. tt_int_op(CELL_CREATED2, OP_EQ, cc.cell_type);
  499. tt_int_op(496, OP_EQ, cc.handshake_len);
  500. tt_mem_op(cc.reply,OP_EQ, b, 496);
  501. tt_int_op(0, OP_EQ, created_cell_format(&cell2, &cc));
  502. tt_int_op(cell.command, OP_EQ, cell2.command);
  503. tt_mem_op(cell.payload,OP_EQ, cell2.payload, CELL_PAYLOAD_SIZE);
  504. /* Bogus CREATED2 cell: too long! */
  505. memset(&cell, 0, sizeof(cell));
  506. memset(b, 0, sizeof(b));
  507. crypto_rand((char*)b, 496);
  508. cell.command = CELL_CREATED2;
  509. memcpy(cell.payload, "\x01\xF1", 2);
  510. tt_int_op(-1, OP_EQ, created_cell_parse(&cc, &cell));
  511. /* Unformattable CREATED2 cell: too long! */
  512. cc.handshake_len = 497;
  513. tt_int_op(-1, OP_EQ, created_cell_format(&cell2, &cc));
  514. done:
  515. ;
  516. }
  517. static void
  518. test_cfmt_extend_cells(void *arg)
  519. {
  520. cell_t cell;
  521. uint8_t b[512];
  522. extend_cell_t ec;
  523. create_cell_t *cc = &ec.create_cell;
  524. uint8_t p[RELAY_PAYLOAD_SIZE];
  525. uint8_t p2[RELAY_PAYLOAD_SIZE];
  526. uint8_t p2_cmd;
  527. uint16_t p2_len;
  528. char *mem_op_hex_tmp = NULL;
  529. (void) arg;
  530. /* Let's start with a simple EXTEND cell. */
  531. memset(p, 0, sizeof(p));
  532. memset(b, 0, sizeof(b));
  533. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  534. memcpy(p, "\x12\xf4\x00\x01\x01\x02", 6); /* 18 244 0 1 : 258 */
  535. memcpy(p+6,b,TAP_ONIONSKIN_CHALLENGE_LEN);
  536. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, "electroencephalogram", 20);
  537. tt_int_op(0, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  538. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  539. tt_int_op(RELAY_COMMAND_EXTEND, OP_EQ, ec.cell_type);
  540. tt_str_op("18.244.0.1", OP_EQ, fmt_addr(&ec.orport_ipv4.addr));
  541. tt_int_op(258, OP_EQ, ec.orport_ipv4.port);
  542. tt_int_op(AF_UNSPEC, OP_EQ, tor_addr_family(&ec.orport_ipv6.addr));
  543. tt_mem_op(ec.node_id,OP_EQ, "electroencephalogram", 20);
  544. tt_int_op(cc->cell_type, OP_EQ, CELL_CREATE);
  545. tt_int_op(cc->handshake_type, OP_EQ, ONION_HANDSHAKE_TYPE_TAP);
  546. tt_int_op(cc->handshake_len, OP_EQ, TAP_ONIONSKIN_CHALLENGE_LEN);
  547. tt_mem_op(cc->onionskin,OP_EQ, b, TAP_ONIONSKIN_CHALLENGE_LEN+20);
  548. tt_int_op(0, OP_EQ, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  549. tt_int_op(p2_cmd, OP_EQ, RELAY_COMMAND_EXTEND);
  550. tt_int_op(p2_len, OP_EQ, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  551. tt_mem_op(p2,OP_EQ, p, RELAY_PAYLOAD_SIZE);
  552. /* Let's do an ntor stuffed in a legacy EXTEND cell */
  553. memset(p, 0, sizeof(p));
  554. memset(b, 0, sizeof(b));
  555. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  556. memcpy(p, "\x12\xf4\x00\x01\x01\x02", 6); /* 18 244 0 1 : 258 */
  557. memcpy(p+6,"ntorNTORntorNTOR", 16);
  558. memcpy(p+22, b, NTOR_ONIONSKIN_LEN);
  559. memcpy(p+6+TAP_ONIONSKIN_CHALLENGE_LEN, "electroencephalogram", 20);
  560. tt_int_op(0, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  561. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  562. tt_int_op(RELAY_COMMAND_EXTEND, OP_EQ, ec.cell_type);
  563. tt_str_op("18.244.0.1", OP_EQ, fmt_addr(&ec.orport_ipv4.addr));
  564. tt_int_op(258, OP_EQ, ec.orport_ipv4.port);
  565. tt_int_op(AF_UNSPEC, OP_EQ, tor_addr_family(&ec.orport_ipv6.addr));
  566. tt_mem_op(ec.node_id,OP_EQ, "electroencephalogram", 20);
  567. tt_int_op(cc->cell_type, OP_EQ, CELL_CREATE2);
  568. tt_int_op(cc->handshake_type, OP_EQ, ONION_HANDSHAKE_TYPE_NTOR);
  569. tt_int_op(cc->handshake_len, OP_EQ, NTOR_ONIONSKIN_LEN);
  570. tt_mem_op(cc->onionskin,OP_EQ, b, NTOR_ONIONSKIN_LEN+20);
  571. tt_int_op(0, OP_EQ, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  572. tt_int_op(p2_cmd, OP_EQ, RELAY_COMMAND_EXTEND);
  573. tt_int_op(p2_len, OP_EQ, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  574. tt_mem_op(p2,OP_EQ, p, RELAY_PAYLOAD_SIZE);
  575. tt_int_op(0, OP_EQ, create_cell_format_relayed(&cell, cc));
  576. /* Now let's do a minimal ntor EXTEND2 cell. */
  577. memset(&ec, 0xff, sizeof(ec));
  578. memset(p, 0, sizeof(p));
  579. memset(b, 0, sizeof(b));
  580. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  581. /* 2 items; one 18.244.0.1:61681 */
  582. memcpy(p, "\x02\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  583. /* The other is a digest. */
  584. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  585. /* Prep for the handshake: type and length */
  586. memcpy(p+31, "\x00\x02\x00\x54", 4);
  587. memcpy(p+35, b, NTOR_ONIONSKIN_LEN);
  588. tt_int_op(0, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  589. p, 35+NTOR_ONIONSKIN_LEN));
  590. tt_int_op(RELAY_COMMAND_EXTEND2, OP_EQ, ec.cell_type);
  591. tt_str_op("18.244.0.1", OP_EQ, fmt_addr(&ec.orport_ipv4.addr));
  592. tt_int_op(61681, OP_EQ, ec.orport_ipv4.port);
  593. tt_int_op(AF_UNSPEC, OP_EQ, tor_addr_family(&ec.orport_ipv6.addr));
  594. tt_mem_op(ec.node_id,OP_EQ, "anarchoindividualist", 20);
  595. tt_int_op(cc->cell_type, OP_EQ, CELL_CREATE2);
  596. tt_int_op(cc->handshake_type, OP_EQ, ONION_HANDSHAKE_TYPE_NTOR);
  597. tt_int_op(cc->handshake_len, OP_EQ, NTOR_ONIONSKIN_LEN);
  598. tt_mem_op(cc->onionskin,OP_EQ, b, NTOR_ONIONSKIN_LEN+20);
  599. tt_int_op(0, OP_EQ, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  600. tt_int_op(p2_cmd, OP_EQ, RELAY_COMMAND_EXTEND2);
  601. tt_int_op(p2_len, OP_EQ, 35+NTOR_ONIONSKIN_LEN);
  602. tt_mem_op(p2,OP_EQ, p, RELAY_PAYLOAD_SIZE);
  603. /* Now let's do a fanciful EXTEND2 cell. */
  604. memset(&ec, 0xff, sizeof(ec));
  605. memset(p, 0, sizeof(p));
  606. memset(b, 0, sizeof(b));
  607. crypto_rand((char*)b, 99);
  608. /* 4 items; one 18 244 0 1 61681 */
  609. memcpy(p, "\x04\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  610. /* One is a digest. */
  611. memcpy(p+9, "\x02\x14" "anthropomorphization", 22);
  612. /* One is an ipv6 address */
  613. memcpy(p+31, "\x01\x12\x20\x02\x00\x00\x00\x00\x00\x00"
  614. "\x00\x00\x00\x00\x00\xf0\xc5\x1e\x11\x12", 20);
  615. /* One is the Konami code. */
  616. memcpy(p+51, "\xf0\x20upupdowndownleftrightleftrightba", 34);
  617. /* Prep for the handshake: weird type and length */
  618. memcpy(p+85, "\x01\x05\x00\x63", 4);
  619. memcpy(p+89, b, 99);
  620. tt_int_op(0, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, 89+99));
  621. tt_int_op(RELAY_COMMAND_EXTEND2, OP_EQ, ec.cell_type);
  622. tt_str_op("18.244.0.1", OP_EQ, fmt_addr(&ec.orport_ipv4.addr));
  623. tt_int_op(61681, OP_EQ, ec.orport_ipv4.port);
  624. tt_str_op("2002::f0:c51e", OP_EQ, fmt_addr(&ec.orport_ipv6.addr));
  625. tt_int_op(4370, OP_EQ, ec.orport_ipv6.port);
  626. tt_mem_op(ec.node_id,OP_EQ, "anthropomorphization", 20);
  627. tt_int_op(cc->cell_type, OP_EQ, CELL_CREATE2);
  628. tt_int_op(cc->handshake_type, OP_EQ, 0x105);
  629. tt_int_op(cc->handshake_len, OP_EQ, 99);
  630. tt_mem_op(cc->onionskin,OP_EQ, b, 99+20);
  631. tt_int_op(0, OP_EQ, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  632. tt_int_op(p2_cmd, OP_EQ, RELAY_COMMAND_EXTEND2);
  633. /* We'll generate it minus the IPv6 address and minus the konami code */
  634. tt_int_op(p2_len, OP_EQ, 89+99-34-20);
  635. test_memeq_hex(p2,
  636. /* Two items: one that same darn IP address. */
  637. "02000612F40001F0F1"
  638. /* The next is a digest : anthropomorphization */
  639. "0214616e7468726f706f6d6f727068697a6174696f6e"
  640. /* Now the handshake prologue */
  641. "01050063");
  642. tt_mem_op(p2+1+8+22+4,OP_EQ, b, 99+20);
  643. tt_int_op(0, OP_EQ, create_cell_format_relayed(&cell, cc));
  644. /* == Now try parsing some junk */
  645. /* Try a too-long handshake */
  646. memset(p, 0, sizeof(p));
  647. memcpy(p, "\x02\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  648. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  649. memcpy(p+31, "\xff\xff\x01\xd0", 4);
  650. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  651. p, sizeof(p)));
  652. /* Try two identities. */
  653. memset(p, 0, sizeof(p));
  654. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  655. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  656. memcpy(p+31, "\x02\x14" "autodepolymerization", 22);
  657. memcpy(p+53, "\xff\xff\x00\x10", 4);
  658. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  659. p, sizeof(p)));
  660. /* No identities. */
  661. memset(p, 0, sizeof(p));
  662. memcpy(p, "\x01\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  663. memcpy(p+53, "\xff\xff\x00\x10", 4);
  664. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  665. p, sizeof(p)));
  666. /* Try a bad IPv4 address (too long, too short)*/
  667. memset(p, 0, sizeof(p));
  668. memcpy(p, "\x02\x00\x07\x12\xf4\x00\x01\xf0\xf1\xff", 10);
  669. memcpy(p+10, "\x02\x14" "anarchoindividualist", 22);
  670. memcpy(p+32, "\xff\xff\x00\x10", 4);
  671. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  672. p, sizeof(p)));
  673. memset(p, 0, sizeof(p));
  674. memcpy(p, "\x02\x00\x05\x12\xf4\x00\x01\xf0", 8);
  675. memcpy(p+8, "\x02\x14" "anarchoindividualist", 22);
  676. memcpy(p+30, "\xff\xff\x00\x10", 4);
  677. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  678. p, sizeof(p)));
  679. /* IPv6 address (too long, too short, no IPv4)*/
  680. memset(p, 0, sizeof(p));
  681. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  682. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  683. memcpy(p+31, "\x01\x13" "xxxxxxxxxxxxxxxxYYZ", 19);
  684. memcpy(p+50, "\xff\xff\x00\x20", 4);
  685. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  686. p, sizeof(p)));
  687. memset(p, 0, sizeof(p));
  688. memcpy(p, "\x03\x00\x06\x12\xf4\x00\x01\xf0\xf1", 9);
  689. memcpy(p+9, "\x02\x14" "anarchoindividualist", 22);
  690. memcpy(p+31, "\x01\x11" "xxxxxxxxxxxxxxxxY", 17);
  691. memcpy(p+48, "\xff\xff\x00\x20", 4);
  692. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  693. p, sizeof(p)));
  694. memset(p, 0, sizeof(p));
  695. memcpy(p, "\x02", 1);
  696. memcpy(p+1, "\x02\x14" "anarchoindividualist", 22);
  697. memcpy(p+23, "\x01\x12" "xxxxxxxxxxxxxxxxYY", 18);
  698. memcpy(p+41, "\xff\xff\x00\x20", 4);
  699. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  700. p, sizeof(p)));
  701. /* Running out of space in specifiers */
  702. memset(p,0,sizeof(p));
  703. memcpy(p, "\x05\x0a\xff", 3);
  704. memcpy(p+3+255, "\x0a\xff", 2);
  705. tt_int_op(-1, OP_EQ, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  706. p, sizeof(p)));
  707. /* Fuzz, because why not. */
  708. memset(&ec, 0xff, sizeof(ec));
  709. {
  710. int i;
  711. memset(p, 0, sizeof(p));
  712. for (i = 0; i < 10000; ++i) {
  713. int n = crypto_rand_int(sizeof(p));
  714. crypto_rand((char *)p, n);
  715. extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, n);
  716. }
  717. }
  718. done:
  719. tor_free(mem_op_hex_tmp);
  720. }
  721. static void
  722. test_cfmt_extended_cells(void *arg)
  723. {
  724. uint8_t b[512];
  725. extended_cell_t ec;
  726. created_cell_t *cc = &ec.created_cell;
  727. uint8_t p[RELAY_PAYLOAD_SIZE];
  728. uint8_t p2[RELAY_PAYLOAD_SIZE];
  729. uint8_t p2_cmd;
  730. uint16_t p2_len;
  731. char *mem_op_hex_tmp = NULL;
  732. (void) arg;
  733. /* Try a regular EXTENDED cell. */
  734. memset(&ec, 0xff, sizeof(ec));
  735. memset(p, 0, sizeof(p));
  736. memset(b, 0, sizeof(b));
  737. crypto_rand((char*)b, TAP_ONIONSKIN_REPLY_LEN);
  738. memcpy(p,b,TAP_ONIONSKIN_REPLY_LEN);
  739. tt_int_op(0, OP_EQ, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED, p,
  740. TAP_ONIONSKIN_REPLY_LEN));
  741. tt_int_op(RELAY_COMMAND_EXTENDED, OP_EQ, ec.cell_type);
  742. tt_int_op(cc->cell_type, OP_EQ, CELL_CREATED);
  743. tt_int_op(cc->handshake_len, OP_EQ, TAP_ONIONSKIN_REPLY_LEN);
  744. tt_mem_op(cc->reply,OP_EQ, b, TAP_ONIONSKIN_REPLY_LEN);
  745. tt_int_op(0, OP_EQ, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  746. tt_int_op(RELAY_COMMAND_EXTENDED, OP_EQ, p2_cmd);
  747. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, OP_EQ, p2_len);
  748. tt_mem_op(p2,OP_EQ, p, sizeof(p2));
  749. /* Try an EXTENDED2 cell */
  750. memset(&ec, 0xff, sizeof(ec));
  751. memset(p, 0, sizeof(p));
  752. memset(b, 0, sizeof(b));
  753. crypto_rand((char*)b, 42);
  754. memcpy(p,"\x00\x2a",2);
  755. memcpy(p+2,b,42);
  756. tt_int_op(0, OP_EQ,
  757. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, 2+42));
  758. tt_int_op(RELAY_COMMAND_EXTENDED2, OP_EQ, ec.cell_type);
  759. tt_int_op(cc->cell_type, OP_EQ, CELL_CREATED2);
  760. tt_int_op(cc->handshake_len, OP_EQ, 42);
  761. tt_mem_op(cc->reply,OP_EQ, b, 42+10);
  762. tt_int_op(0, OP_EQ, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  763. tt_int_op(RELAY_COMMAND_EXTENDED2, OP_EQ, p2_cmd);
  764. tt_int_op(2+42, OP_EQ, p2_len);
  765. tt_mem_op(p2,OP_EQ, p, sizeof(p2));
  766. /* Try an almost-too-long EXTENDED2 cell */
  767. memcpy(p, "\x01\xf0", 2);
  768. tt_int_op(0, OP_EQ,
  769. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  770. /* Now try a too-long extended2 cell. That's the only misparse I can think
  771. * of. */
  772. memcpy(p, "\x01\xf1", 2);
  773. tt_int_op(-1, OP_EQ,
  774. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  775. done:
  776. tor_free(mem_op_hex_tmp);
  777. }
  778. static void
  779. test_cfmt_resolved_cells(void *arg)
  780. {
  781. smartlist_t *addrs = smartlist_new();
  782. relay_header_t rh;
  783. cell_t cell;
  784. int r, errcode;
  785. address_ttl_t *a;
  786. (void)arg;
  787. #define CLEAR_CELL() do { \
  788. memset(&cell, 0, sizeof(cell)); \
  789. memset(&rh, 0, sizeof(rh)); \
  790. } while (0)
  791. #define CLEAR_ADDRS() do { \
  792. SMARTLIST_FOREACH(addrs, address_ttl_t *, aa_, \
  793. address_ttl_free(aa_); ); \
  794. smartlist_clear(addrs); \
  795. } while (0)
  796. #define SET_CELL(s) do { \
  797. CLEAR_CELL(); \
  798. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  799. rh.length = sizeof((s))-1; \
  800. rh.command = RELAY_COMMAND_RESOLVED; \
  801. errcode = -1; \
  802. } while (0)
  803. /* The cell format is one or more answers; each of the form
  804. * type [1 byte---0:hostname, 4:ipv4, 6:ipv6, f0:err-transient, f1:err]
  805. * length [1 byte]
  806. * body [length bytes]
  807. * ttl [4 bytes]
  808. */
  809. /* Let's try an empty cell */
  810. SET_CELL("");
  811. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  812. tt_int_op(errcode, OP_EQ, 0);
  813. tt_int_op(r, OP_EQ, 0);
  814. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  815. CLEAR_ADDRS(); /* redundant but let's be consistent */
  816. /* Cell with one ipv4 addr */
  817. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  818. tt_int_op(rh.length, OP_EQ, 10);
  819. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  820. tt_int_op(errcode, OP_EQ, 0);
  821. tt_int_op(r, OP_EQ, 0);
  822. tt_int_op(smartlist_len(addrs), OP_EQ, 1);
  823. a = smartlist_get(addrs, 0);
  824. tt_str_op(fmt_addr(&a->addr), OP_EQ, "127.0.2.10");
  825. tt_ptr_op(a->hostname, OP_EQ, NULL);
  826. tt_int_op(a->ttl, OP_EQ, 256);
  827. CLEAR_ADDRS();
  828. /* Cell with one ipv6 addr */
  829. SET_CELL("\x06\x10"
  830. "\x20\x02\x90\x90\x00\x00\x00\x00"
  831. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  832. "\x02\00\x00\x01");
  833. tt_int_op(rh.length, OP_EQ, 22);
  834. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  835. tt_int_op(errcode, OP_EQ, 0);
  836. tt_int_op(r, OP_EQ, 0);
  837. tt_int_op(smartlist_len(addrs), OP_EQ, 1);
  838. a = smartlist_get(addrs, 0);
  839. tt_str_op(fmt_addr(&a->addr), OP_EQ, "2002:9090::f0f0:abcd");
  840. tt_ptr_op(a->hostname, OP_EQ, NULL);
  841. tt_int_op(a->ttl, OP_EQ, 0x2000001);
  842. CLEAR_ADDRS();
  843. /* Cell with one hostname */
  844. SET_CELL("\x00\x11"
  845. "motherbrain.zebes"
  846. "\x00\00\x00\x00");
  847. tt_int_op(rh.length, OP_EQ, 23);
  848. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  849. tt_int_op(errcode, OP_EQ, 0);
  850. tt_int_op(r, OP_EQ, 0);
  851. tt_int_op(smartlist_len(addrs), OP_EQ, 1);
  852. a = smartlist_get(addrs, 0);
  853. tt_assert(tor_addr_is_null(&a->addr));
  854. tt_str_op(a->hostname, OP_EQ, "motherbrain.zebes");
  855. tt_int_op(a->ttl, OP_EQ, 0);
  856. CLEAR_ADDRS();
  857. #define LONG_NAME \
  858. "this-hostname-has-255-characters.in-order-to-test-whether-very-long.ho" \
  859. "stnames-are-accepted.i-am-putting-it-in-a-macro-because-although.this-" \
  860. "function-is-already-very-full.of-copy-and-pasted-stuff.having-this-app" \
  861. "ear-more-than-once-would-bother-me-somehow.is"
  862. tt_int_op(strlen(LONG_NAME), OP_EQ, 255);
  863. SET_CELL("\x00\xff"
  864. LONG_NAME
  865. "\x00\01\x00\x00");
  866. tt_int_op(rh.length, OP_EQ, 261);
  867. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  868. tt_int_op(errcode, OP_EQ, 0);
  869. tt_int_op(r, OP_EQ, 0);
  870. tt_int_op(smartlist_len(addrs), OP_EQ, 1);
  871. a = smartlist_get(addrs, 0);
  872. tt_assert(tor_addr_is_null(&a->addr));
  873. tt_str_op(a->hostname, OP_EQ, LONG_NAME);
  874. tt_int_op(a->ttl, OP_EQ, 65536);
  875. CLEAR_ADDRS();
  876. /* Cells with an error */
  877. SET_CELL("\xf0\x2b"
  878. "I'm sorry, Dave. I'm afraid I can't do that"
  879. "\x00\x11\x22\x33");
  880. tt_int_op(rh.length, OP_EQ, 49);
  881. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  882. tt_int_op(errcode, OP_EQ, RESOLVED_TYPE_ERROR_TRANSIENT);
  883. tt_int_op(r, OP_EQ, 0);
  884. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  885. CLEAR_ADDRS();
  886. SET_CELL("\xf1\x40"
  887. "This hostname is too important for me to allow you to resolve it"
  888. "\x00\x00\x00\x00");
  889. tt_int_op(rh.length, OP_EQ, 70);
  890. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  891. tt_int_op(errcode, OP_EQ, RESOLVED_TYPE_ERROR);
  892. tt_int_op(r, OP_EQ, 0);
  893. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  894. CLEAR_ADDRS();
  895. /* Cell with an unrecognized type */
  896. SET_CELL("\xee\x16"
  897. "fault in the AE35 unit"
  898. "\x09\x09\x01\x01");
  899. tt_int_op(rh.length, OP_EQ, 28);
  900. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  901. tt_int_op(errcode, OP_EQ, 0);
  902. tt_int_op(r, OP_EQ, 0);
  903. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  904. CLEAR_ADDRS();
  905. /* Cell with one of each */
  906. SET_CELL(/* unrecognized: */
  907. "\xee\x16"
  908. "fault in the AE35 unit"
  909. "\x09\x09\x01\x01"
  910. /* error: */
  911. "\xf0\x2b"
  912. "I'm sorry, Dave. I'm afraid I can't do that"
  913. "\x00\x11\x22\x33"
  914. /* IPv6: */
  915. "\x06\x10"
  916. "\x20\x02\x90\x90\x00\x00\x00\x00"
  917. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  918. "\x02\00\x00\x01"
  919. /* IPv4: */
  920. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  921. /* Hostname: */
  922. "\x00\x11"
  923. "motherbrain.zebes"
  924. "\x00\00\x00\x00"
  925. );
  926. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  927. tt_int_op(errcode, OP_EQ, 0); /* no error reported; we got answers */
  928. tt_int_op(r, OP_EQ, 0);
  929. tt_int_op(smartlist_len(addrs), OP_EQ, 3);
  930. a = smartlist_get(addrs, 0);
  931. tt_str_op(fmt_addr(&a->addr), OP_EQ, "2002:9090::f0f0:abcd");
  932. tt_ptr_op(a->hostname, OP_EQ, NULL);
  933. tt_int_op(a->ttl, OP_EQ, 0x2000001);
  934. a = smartlist_get(addrs, 1);
  935. tt_str_op(fmt_addr(&a->addr), OP_EQ, "127.0.2.10");
  936. tt_ptr_op(a->hostname, OP_EQ, NULL);
  937. tt_int_op(a->ttl, OP_EQ, 256);
  938. a = smartlist_get(addrs, 2);
  939. tt_assert(tor_addr_is_null(&a->addr));
  940. tt_str_op(a->hostname, OP_EQ, "motherbrain.zebes");
  941. tt_int_op(a->ttl, OP_EQ, 0);
  942. CLEAR_ADDRS();
  943. /* Cell with several of similar type */
  944. SET_CELL(/* IPv4 */
  945. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  946. "\x04\x04" "\x08\x08\x08\x08" "\x00\00\x01\x05"
  947. "\x04\x04" "\x7f\xb0\x02\xb0" "\x00\01\xff\xff"
  948. /* IPv6 */
  949. "\x06\x10"
  950. "\x20\x02\x90\x00\x00\x00\x00\x00"
  951. "\x00\x00\x00\x00\xca\xfe\xf0\x0d"
  952. "\x00\00\x00\x01"
  953. "\x06\x10"
  954. "\x20\x02\x90\x01\x00\x00\x00\x00"
  955. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  956. "\x00\00\x00\x03");
  957. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  958. tt_int_op(errcode, OP_EQ, 0);
  959. tt_int_op(r, OP_EQ, 0);
  960. tt_int_op(smartlist_len(addrs), OP_EQ, 5);
  961. a = smartlist_get(addrs, 0);
  962. tt_str_op(fmt_addr(&a->addr), OP_EQ, "127.0.2.10");
  963. tt_ptr_op(a->hostname, OP_EQ, NULL);
  964. tt_int_op(a->ttl, OP_EQ, 256);
  965. a = smartlist_get(addrs, 1);
  966. tt_str_op(fmt_addr(&a->addr), OP_EQ, "8.8.8.8");
  967. tt_ptr_op(a->hostname, OP_EQ, NULL);
  968. tt_int_op(a->ttl, OP_EQ, 261);
  969. a = smartlist_get(addrs, 2);
  970. tt_str_op(fmt_addr(&a->addr), OP_EQ, "127.176.2.176");
  971. tt_ptr_op(a->hostname, OP_EQ, NULL);
  972. tt_int_op(a->ttl, OP_EQ, 131071);
  973. a = smartlist_get(addrs, 3);
  974. tt_str_op(fmt_addr(&a->addr), OP_EQ, "2002:9000::cafe:f00d");
  975. tt_ptr_op(a->hostname, OP_EQ, NULL);
  976. tt_int_op(a->ttl, OP_EQ, 1);
  977. a = smartlist_get(addrs, 4);
  978. tt_str_op(fmt_addr(&a->addr), OP_EQ, "2002:9001::fa:cade");
  979. tt_ptr_op(a->hostname, OP_EQ, NULL);
  980. tt_int_op(a->ttl, OP_EQ, 3);
  981. CLEAR_ADDRS();
  982. /* Full cell */
  983. #define LONG_NAME2 \
  984. "this-name-has-231-characters.so-that-it-plus-LONG_NAME-can-completely-" \
  985. "fill-up-the-payload-of-a-cell.its-important-to-check-for-the-full-thin" \
  986. "g-case.to-avoid-off-by-one-errors.where-full-things-are-misreported-as" \
  987. ".overflowing-by-one.z"
  988. tt_int_op(strlen(LONG_NAME2), OP_EQ, 231);
  989. SET_CELL("\x00\xff"
  990. LONG_NAME
  991. "\x00\01\x00\x00"
  992. "\x00\xe7"
  993. LONG_NAME2
  994. "\x00\01\x00\x00");
  995. tt_int_op(rh.length, OP_EQ, RELAY_PAYLOAD_SIZE);
  996. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  997. tt_int_op(errcode, OP_EQ, 0);
  998. tt_int_op(r, OP_EQ, 0);
  999. tt_int_op(smartlist_len(addrs), OP_EQ, 2);
  1000. a = smartlist_get(addrs, 0);
  1001. tt_str_op(a->hostname, OP_EQ, LONG_NAME);
  1002. a = smartlist_get(addrs, 1);
  1003. tt_str_op(a->hostname, OP_EQ, LONG_NAME2);
  1004. CLEAR_ADDRS();
  1005. /* BAD CELLS */
  1006. /* Invalid length on an IPv4 */
  1007. SET_CELL("\x04\x03zzz1234");
  1008. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1009. tt_int_op(errcode, OP_EQ, 0);
  1010. tt_int_op(r, OP_EQ, -1);
  1011. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1012. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1013. "\x04\x05zzzzz1234");
  1014. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1015. tt_int_op(errcode, OP_EQ, 0);
  1016. tt_int_op(r, OP_EQ, -1);
  1017. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1018. /* Invalid length on an IPv6 */
  1019. SET_CELL("\x06\x03zzz1234");
  1020. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1021. tt_int_op(errcode, OP_EQ, 0);
  1022. tt_int_op(r, OP_EQ, -1);
  1023. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1024. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1025. "\x06\x17wwwwwwwwwwwwwwwww1234");
  1026. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1027. tt_int_op(errcode, OP_EQ, 0);
  1028. tt_int_op(r, OP_EQ, -1);
  1029. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1030. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1031. "\x06\x10xxxx");
  1032. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1033. tt_int_op(errcode, OP_EQ, 0);
  1034. tt_int_op(r, OP_EQ, -1);
  1035. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1036. /* Empty hostname */
  1037. SET_CELL("\x00\x00xxxx");
  1038. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1039. tt_int_op(errcode, OP_EQ, 0);
  1040. tt_int_op(r, OP_EQ, -1);
  1041. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1042. /* rh.length out of range */
  1043. CLEAR_CELL();
  1044. rh.length = 499;
  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. /* Item length extends beyond rh.length */
  1050. CLEAR_CELL();
  1051. SET_CELL("\x00\xff"
  1052. LONG_NAME
  1053. "\x00\01\x00\x00");
  1054. rh.length -= 1;
  1055. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1056. tt_int_op(r, OP_EQ, -1);
  1057. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1058. rh.length -= 5;
  1059. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1060. tt_int_op(r, OP_EQ, -1);
  1061. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1062. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  1063. rh.length -= 1;
  1064. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1065. tt_int_op(r, OP_EQ, -1);
  1066. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1067. SET_CELL("\xee\x10"
  1068. "\x20\x02\x90\x01\x00\x00\x00\x00"
  1069. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  1070. "\x00\00\x00\x03");
  1071. rh.length -= 1;
  1072. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1073. tt_int_op(r, OP_EQ, -1);
  1074. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1075. /* Truncated item after first character */
  1076. SET_CELL("\x04");
  1077. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1078. tt_int_op(r, OP_EQ, -1);
  1079. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1080. SET_CELL("\xee");
  1081. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1082. tt_int_op(r, OP_EQ, -1);
  1083. tt_int_op(smartlist_len(addrs), OP_EQ, 0);
  1084. done:
  1085. CLEAR_ADDRS();
  1086. CLEAR_CELL();
  1087. smartlist_free(addrs);
  1088. #undef CLEAR_ADDRS
  1089. #undef CLEAR_CELL
  1090. }
  1091. static void
  1092. test_cfmt_is_destroy(void *arg)
  1093. {
  1094. cell_t cell;
  1095. packed_cell_t packed;
  1096. circid_t circid = 0;
  1097. channel_t *chan;
  1098. (void)arg;
  1099. chan = tor_malloc_zero(sizeof(channel_t));
  1100. memset(&cell, 0xff, sizeof(cell));
  1101. cell.circ_id = 3003;
  1102. cell.command = CELL_RELAY;
  1103. cell_pack(&packed, &cell, 0);
  1104. chan->wide_circ_ids = 0;
  1105. tt_assert(! packed_cell_is_destroy(chan, &packed, &circid));
  1106. tt_int_op(circid, OP_EQ, 0);
  1107. cell_pack(&packed, &cell, 1);
  1108. chan->wide_circ_ids = 1;
  1109. tt_assert(! packed_cell_is_destroy(chan, &packed, &circid));
  1110. tt_int_op(circid, OP_EQ, 0);
  1111. cell.command = CELL_DESTROY;
  1112. cell_pack(&packed, &cell, 0);
  1113. chan->wide_circ_ids = 0;
  1114. tt_assert(packed_cell_is_destroy(chan, &packed, &circid));
  1115. tt_int_op(circid, OP_EQ, 3003);
  1116. circid = 0;
  1117. cell_pack(&packed, &cell, 1);
  1118. chan->wide_circ_ids = 1;
  1119. tt_assert(packed_cell_is_destroy(chan, &packed, &circid));
  1120. done:
  1121. tor_free(chan);
  1122. }
  1123. #define TEST(name, flags) \
  1124. { #name, test_cfmt_ ## name, flags, 0, NULL }
  1125. struct testcase_t cell_format_tests[] = {
  1126. TEST(relay_header, 0),
  1127. TEST(begin_cells, 0),
  1128. TEST(connected_cells, 0),
  1129. TEST(create_cells, 0),
  1130. TEST(created_cells, 0),
  1131. TEST(extend_cells, 0),
  1132. TEST(extended_cells, 0),
  1133. TEST(resolved_cells, 0),
  1134. TEST(is_destroy, 0),
  1135. END_OF_TESTCASES
  1136. };