test_cell_formats.c 47 KB

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