test_cell_formats.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONNECTION_EDGE_PRIVATE
  7. #define RELAY_PRIVATE
  8. #include "or.h"
  9. #include "channel.h"
  10. #include "connection_edge.h"
  11. #include "connection_or.h"
  12. #include "onion.h"
  13. #include "onion_tap.h"
  14. #include "onion_fast.h"
  15. #include "onion_ntor.h"
  16. #include "relay.h"
  17. #include "test.h"
  18. #include <stdlib.h>
  19. #include <string.h>
  20. static void
  21. test_cfmt_relay_header(void *arg)
  22. {
  23. relay_header_t rh;
  24. const uint8_t hdr_1[RELAY_HEADER_SIZE] =
  25. "\x03" "\x00\x00" "\x21\x22" "ABCD" "\x01\x03";
  26. uint8_t hdr_out[RELAY_HEADER_SIZE];
  27. (void)arg;
  28. tt_int_op(sizeof(hdr_1), ==, RELAY_HEADER_SIZE);
  29. relay_header_unpack(&rh, hdr_1);
  30. tt_int_op(rh.command, ==, 3);
  31. tt_int_op(rh.recognized, ==, 0);
  32. tt_int_op(rh.stream_id, ==, 0x2122);
  33. tt_mem_op(rh.integrity, ==, "ABCD", 4);
  34. tt_int_op(rh.length, ==, 0x103);
  35. relay_header_pack(hdr_out, &rh);
  36. tt_mem_op(hdr_out, ==, hdr_1, RELAY_HEADER_SIZE);
  37. done:
  38. ;
  39. }
  40. static void
  41. make_relay_cell(cell_t *out, uint8_t command,
  42. const void *body, size_t bodylen)
  43. {
  44. relay_header_t rh;
  45. memset(&rh, 0, sizeof(rh));
  46. rh.stream_id = 5;
  47. rh.command = command;
  48. rh.length = bodylen;
  49. out->command = CELL_RELAY;
  50. out->circ_id = 10;
  51. relay_header_pack(out->payload, &rh);
  52. memcpy(out->payload + RELAY_HEADER_SIZE, body, bodylen);
  53. }
  54. static void
  55. test_cfmt_begin_cells(void *arg)
  56. {
  57. cell_t cell;
  58. begin_cell_t bcell;
  59. uint8_t end_reason;
  60. (void)arg;
  61. /* Try begindir. */
  62. memset(&bcell, 0x7f, sizeof(bcell));
  63. make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "", 0);
  64. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  65. tt_ptr_op(NULL, ==, bcell.address);
  66. tt_int_op(0, ==, bcell.flags);
  67. tt_int_op(0, ==, bcell.port);
  68. tt_int_op(5, ==, bcell.stream_id);
  69. tt_int_op(1, ==, bcell.is_begindir);
  70. /* A Begindir with extra stuff. */
  71. memset(&bcell, 0x7f, sizeof(bcell));
  72. make_relay_cell(&cell, RELAY_COMMAND_BEGIN_DIR, "12345", 5);
  73. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  74. tt_ptr_op(NULL, ==, bcell.address);
  75. tt_int_op(0, ==, bcell.flags);
  76. tt_int_op(0, ==, bcell.port);
  77. tt_int_op(5, ==, bcell.stream_id);
  78. tt_int_op(1, ==, bcell.is_begindir);
  79. /* A short but valid begin cell */
  80. memset(&bcell, 0x7f, sizeof(bcell));
  81. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:9", 6);
  82. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  83. tt_str_op("a.b", ==, bcell.address);
  84. tt_int_op(0, ==, bcell.flags);
  85. tt_int_op(9, ==, bcell.port);
  86. tt_int_op(5, ==, bcell.stream_id);
  87. tt_int_op(0, ==, bcell.is_begindir);
  88. tor_free(bcell.address);
  89. /* A significantly loner begin cell */
  90. memset(&bcell, 0x7f, sizeof(bcell));
  91. {
  92. const char c[] = "here-is-a-nice-long.hostname.com:65535";
  93. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, strlen(c)+1);
  94. }
  95. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  96. tt_str_op("here-is-a-nice-long.hostname.com", ==, bcell.address);
  97. tt_int_op(0, ==, bcell.flags);
  98. tt_int_op(65535, ==, bcell.port);
  99. tt_int_op(5, ==, bcell.stream_id);
  100. tt_int_op(0, ==, bcell.is_begindir);
  101. tor_free(bcell.address);
  102. /* An IPv4 begin cell. */
  103. memset(&bcell, 0x7f, sizeof(bcell));
  104. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "18.9.22.169:80", 15);
  105. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  106. tt_str_op("18.9.22.169", ==, bcell.address);
  107. tt_int_op(0, ==, bcell.flags);
  108. tt_int_op(80, ==, bcell.port);
  109. tt_int_op(5, ==, bcell.stream_id);
  110. tt_int_op(0, ==, bcell.is_begindir);
  111. tor_free(bcell.address);
  112. /* An IPv6 begin cell. Let's make sure we handle colons*/
  113. memset(&bcell, 0x7f, sizeof(bcell));
  114. make_relay_cell(&cell, RELAY_COMMAND_BEGIN,
  115. "[2620::6b0:b:1a1a:0:26e5:480e]:80", 34);
  116. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  117. tt_str_op("[2620::6b0:b:1a1a:0:26e5:480e]", ==, bcell.address);
  118. tt_int_op(0, ==, bcell.flags);
  119. tt_int_op(80, ==, bcell.port);
  120. tt_int_op(5, ==, bcell.stream_id);
  121. tt_int_op(0, ==, bcell.is_begindir);
  122. tor_free(bcell.address);
  123. /* a begin cell with extra junk but not enough for flags. */
  124. memset(&bcell, 0x7f, sizeof(bcell));
  125. {
  126. const char c[] = "another.example.com:80\x00\x01\x02";
  127. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  128. }
  129. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  130. tt_str_op("another.example.com", ==, bcell.address);
  131. tt_int_op(0, ==, bcell.flags);
  132. tt_int_op(80, ==, bcell.port);
  133. tt_int_op(5, ==, bcell.stream_id);
  134. tt_int_op(0, ==, bcell.is_begindir);
  135. tor_free(bcell.address);
  136. /* a begin cell with flags. */
  137. memset(&bcell, 0x7f, sizeof(bcell));
  138. {
  139. const char c[] = "another.example.com:443\x00\x01\x02\x03\x04";
  140. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  141. }
  142. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  143. tt_str_op("another.example.com", ==, bcell.address);
  144. tt_int_op(0x1020304, ==, bcell.flags);
  145. tt_int_op(443, ==, bcell.port);
  146. tt_int_op(5, ==, bcell.stream_id);
  147. tt_int_op(0, ==, bcell.is_begindir);
  148. tor_free(bcell.address);
  149. /* a begin cell with flags and even more cruft after that. */
  150. memset(&bcell, 0x7f, sizeof(bcell));
  151. {
  152. const char c[] = "a-further.example.com:22\x00\xee\xaa\x00\xffHi mom";
  153. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, c, sizeof(c)-1);
  154. }
  155. tt_int_op(0, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  156. tt_str_op("a-further.example.com", ==, bcell.address);
  157. tt_int_op(0xeeaa00ff, ==, bcell.flags);
  158. tt_int_op(22, ==, bcell.port);
  159. tt_int_op(5, ==, bcell.stream_id);
  160. tt_int_op(0, ==, bcell.is_begindir);
  161. tor_free(bcell.address);
  162. /* bad begin cell: impossible length. */
  163. memset(&bcell, 0x7f, sizeof(bcell));
  164. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 7);
  165. cell.payload[9] = 0x01; /* Set length to 510 */
  166. cell.payload[10] = 0xfe;
  167. {
  168. relay_header_t rh;
  169. relay_header_unpack(&rh, cell.payload);
  170. tt_int_op(rh.length, ==, 510);
  171. }
  172. tt_int_op(-2, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  173. /* Bad begin cell: no body. */
  174. memset(&bcell, 0x7f, sizeof(bcell));
  175. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
  176. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  177. /* bad begin cell: no body. */
  178. memset(&bcell, 0x7f, sizeof(bcell));
  179. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "", 0);
  180. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  181. /* bad begin cell: no colon */
  182. memset(&bcell, 0x7f, sizeof(bcell));
  183. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b", 4);
  184. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  185. /* bad begin cell: no ports */
  186. memset(&bcell, 0x7f, sizeof(bcell));
  187. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:", 5);
  188. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  189. /* bad begin cell: bad port */
  190. memset(&bcell, 0x7f, sizeof(bcell));
  191. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:xyz", 8);
  192. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  193. memset(&bcell, 0x7f, sizeof(bcell));
  194. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:100000", 11);
  195. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  196. /* bad begin cell: no nul */
  197. memset(&bcell, 0x7f, sizeof(bcell));
  198. make_relay_cell(&cell, RELAY_COMMAND_BEGIN, "a.b:80", 6);
  199. tt_int_op(-1, ==, begin_cell_parse(&cell, &bcell, &end_reason));
  200. done:
  201. tor_free(bcell.address);
  202. }
  203. static void
  204. test_cfmt_connected_cells(void *arg)
  205. {
  206. relay_header_t rh;
  207. cell_t cell;
  208. tor_addr_t addr;
  209. int ttl, r;
  210. char *mem_op_hex_tmp = NULL;
  211. (void)arg;
  212. /* Let's try an oldschool one with nothing in it. */
  213. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "", 0);
  214. relay_header_unpack(&rh, cell.payload);
  215. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  216. tt_int_op(r, ==, 0);
  217. tt_int_op(tor_addr_family(&addr), ==, AF_UNSPEC);
  218. tt_int_op(ttl, ==, -1);
  219. /* A slightly less oldschool one: only an IPv4 address */
  220. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x20\x30\x40\x50", 4);
  221. relay_header_unpack(&rh, cell.payload);
  222. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  223. tt_int_op(r, ==, 0);
  224. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  225. tt_str_op(fmt_addr(&addr), ==, "32.48.64.80");
  226. tt_int_op(ttl, ==, -1);
  227. /* Bogus but understandable: truncated TTL */
  228. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED, "\x11\x12\x13\x14\x15", 5);
  229. relay_header_unpack(&rh, cell.payload);
  230. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  231. tt_int_op(r, ==, 0);
  232. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  233. tt_str_op(fmt_addr(&addr), ==, "17.18.19.20");
  234. tt_int_op(ttl, ==, -1);
  235. /* Regular IPv4 one: address and TTL */
  236. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  237. "\x02\x03\x04\x05\x00\x00\x0e\x10", 8);
  238. relay_header_unpack(&rh, cell.payload);
  239. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  240. tt_int_op(r, ==, 0);
  241. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  242. tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
  243. tt_int_op(ttl, ==, 3600);
  244. /* IPv4 with too-big TTL */
  245. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  246. "\x02\x03\x04\x05\xf0\x00\x00\x00", 8);
  247. relay_header_unpack(&rh, cell.payload);
  248. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  249. tt_int_op(r, ==, 0);
  250. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  251. tt_str_op(fmt_addr(&addr), ==, "2.3.4.5");
  252. tt_int_op(ttl, ==, -1);
  253. /* IPv6 (ttl is mandatory) */
  254. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  255. "\x00\x00\x00\x00\x06"
  256. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  257. "\x00\x00\x00\x00\x00\x00\x00\x68"
  258. "\x00\x00\x02\x58", 25);
  259. relay_header_unpack(&rh, cell.payload);
  260. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  261. tt_int_op(r, ==, 0);
  262. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  263. tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
  264. tt_int_op(ttl, ==, 600);
  265. /* IPv6 (ttl too big) */
  266. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  267. "\x00\x00\x00\x00\x06"
  268. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  269. "\x00\x00\x00\x00\x00\x00\x00\x68"
  270. "\x90\x00\x02\x58", 25);
  271. relay_header_unpack(&rh, cell.payload);
  272. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  273. tt_int_op(r, ==, 0);
  274. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  275. tt_str_op(fmt_addr(&addr), ==, "2607:f8b0:400c:c02::68");
  276. tt_int_op(ttl, ==, -1);
  277. /* Bogus size: 3. */
  278. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  279. "\x00\x01\x02", 3);
  280. relay_header_unpack(&rh, cell.payload);
  281. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  282. tt_int_op(r, ==, -1);
  283. /* Bogus family: 7. */
  284. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  285. "\x00\x00\x00\x00\x07"
  286. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  287. "\x00\x00\x00\x00\x00\x00\x00\x68"
  288. "\x90\x00\x02\x58", 25);
  289. relay_header_unpack(&rh, cell.payload);
  290. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  291. tt_int_op(r, ==, -1);
  292. /* Truncated IPv6. */
  293. make_relay_cell(&cell, RELAY_COMMAND_CONNECTED,
  294. "\x00\x00\x00\x00\x06"
  295. "\x26\x07\xf8\xb0\x40\x0c\x0c\x02"
  296. "\x00\x00\x00\x00\x00\x00\x00\x68"
  297. "\x00\x00\x02", 24);
  298. relay_header_unpack(&rh, cell.payload);
  299. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  300. tt_int_op(r, ==, -1);
  301. /* Now make sure we can generate connected cells correctly. */
  302. /* Try an IPv4 address */
  303. memset(&rh, 0, sizeof(rh));
  304. memset(&cell, 0, sizeof(cell));
  305. tor_addr_parse(&addr, "30.40.50.60");
  306. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  307. &addr, 128);
  308. tt_int_op(rh.length, ==, 8);
  309. test_memeq_hex(cell.payload+RELAY_HEADER_SIZE, "1e28323c" "00000080");
  310. /* Try parsing it. */
  311. tor_addr_make_unspec(&addr);
  312. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  313. tt_int_op(r, ==, 0);
  314. tt_int_op(tor_addr_family(&addr), ==, AF_INET);
  315. tt_str_op(fmt_addr(&addr), ==, "30.40.50.60");
  316. tt_int_op(ttl, ==, 128);
  317. /* Try an IPv6 address */
  318. memset(&rh, 0, sizeof(rh));
  319. memset(&cell, 0, sizeof(cell));
  320. tor_addr_parse(&addr, "2620::6b0:b:1a1a:0:26e5:480e");
  321. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  322. &addr, 3600);
  323. tt_int_op(rh.length, ==, 25);
  324. test_memeq_hex(cell.payload + RELAY_HEADER_SIZE,
  325. "00000000" "06"
  326. "2620000006b0000b1a1a000026e5480e" "00000e10");
  327. /* Try parsing it. */
  328. tor_addr_make_unspec(&addr);
  329. r = connected_cell_parse(&rh, &cell, &addr, &ttl);
  330. tt_int_op(r, ==, 0);
  331. tt_int_op(tor_addr_family(&addr), ==, AF_INET6);
  332. tt_str_op(fmt_addr(&addr), ==, "2620:0:6b0:b:1a1a:0:26e5:480e");
  333. tt_int_op(ttl, ==, 3600);
  334. done:
  335. tor_free(mem_op_hex_tmp);
  336. }
  337. static void
  338. test_cfmt_create_cells(void *arg)
  339. {
  340. uint8_t b[MAX_ONIONSKIN_CHALLENGE_LEN];
  341. create_cell_t cc;
  342. cell_t cell;
  343. cell_t cell2;
  344. (void)arg;
  345. /* === Let's try parsing some good cells! */
  346. /* A valid create cell. */
  347. memset(&cell, 0, sizeof(cell));
  348. memset(b, 0, sizeof(b));
  349. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  350. cell.command = CELL_CREATE;
  351. memcpy(cell.payload, b, TAP_ONIONSKIN_CHALLENGE_LEN);
  352. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  353. tt_int_op(CELL_CREATE, ==, cc.cell_type);
  354. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
  355. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
  356. tt_mem_op(cc.onionskin,==, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  357. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  358. tt_int_op(cell.command, ==, cell2.command);
  359. tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
  360. /* A valid create_fast cell. */
  361. memset(&cell, 0, sizeof(cell));
  362. memset(b, 0, sizeof(b));
  363. crypto_rand((char*)b, CREATE_FAST_LEN);
  364. cell.command = CELL_CREATE_FAST;
  365. memcpy(cell.payload, b, CREATE_FAST_LEN);
  366. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  367. tt_int_op(CELL_CREATE_FAST, ==, cc.cell_type);
  368. tt_int_op(ONION_HANDSHAKE_TYPE_FAST, ==, cc.handshake_type);
  369. tt_int_op(CREATE_FAST_LEN, ==, cc.handshake_len);
  370. tt_mem_op(cc.onionskin,==, b, CREATE_FAST_LEN + 10);
  371. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  372. tt_int_op(cell.command, ==, cell2.command);
  373. tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
  374. /* A valid create2 cell with a TAP payload */
  375. memset(&cell, 0, sizeof(cell));
  376. memset(b, 0, sizeof(b));
  377. crypto_rand((char*)b, TAP_ONIONSKIN_CHALLENGE_LEN);
  378. cell.command = CELL_CREATE2;
  379. memcpy(cell.payload, "\x00\x00\x00\xBA", 4); /* TAP, 186 bytes long */
  380. memcpy(cell.payload+4, b, TAP_ONIONSKIN_CHALLENGE_LEN);
  381. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  382. tt_int_op(CELL_CREATE2, ==, cc.cell_type);
  383. tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
  384. tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
  385. tt_mem_op(cc.onionskin,==, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
  386. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  387. tt_int_op(cell.command, ==, cell2.command);
  388. tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
  389. /* A valid create2 cell with an ntor payload */
  390. memset(&cell, 0, sizeof(cell));
  391. memset(b, 0, sizeof(b));
  392. crypto_rand((char*)b, NTOR_ONIONSKIN_LEN);
  393. cell.command = CELL_CREATE2;
  394. memcpy(cell.payload, "\x00\x02\x00\x54", 4); /* ntor, 84 bytes long */
  395. memcpy(cell.payload+4, b, NTOR_ONIONSKIN_LEN);
  396. tt_int_op(0, ==, create_cell_parse(&cc, &cell));
  397. tt_int_op(CELL_CREATE2, ==, cc.cell_type);
  398. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
  399. tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
  400. tt_mem_op(cc.onionskin,==, b, NTOR_ONIONSKIN_LEN + 10);
  401. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  402. tt_int_op(cell.command, ==, cell2.command);
  403. tt_mem_op(cell.payload,==, 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, ==, create_cell_parse(&cc, &cell));
  412. tt_int_op(CELL_CREATE, ==, cc.cell_type);
  413. tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
  414. tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
  415. tt_mem_op(cc.onionskin,==, b, NTOR_ONIONSKIN_LEN + 10);
  416. tt_int_op(0, ==, create_cell_format(&cell2, &cc));
  417. tt_int_op(cell.command, ==, cell2.command);
  418. tt_mem_op(cell.payload,==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, created_cell_parse(&cc, &cell));
  457. tt_int_op(CELL_CREATED, ==, cc.cell_type);
  458. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, cc.handshake_len);
  459. tt_mem_op(cc.reply,==, b, TAP_ONIONSKIN_REPLY_LEN + 10);
  460. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  461. tt_int_op(cell.command, ==, cell2.command);
  462. tt_mem_op(cell.payload,==, 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, ==, created_cell_parse(&cc, &cell));
  470. tt_int_op(CELL_CREATED_FAST, ==, cc.cell_type);
  471. tt_int_op(CREATED_FAST_LEN, ==, cc.handshake_len);
  472. tt_mem_op(cc.reply,==, b, CREATED_FAST_LEN + 10);
  473. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  474. tt_int_op(cell.command, ==, cell2.command);
  475. tt_mem_op(cell.payload,==, 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, ==, created_cell_parse(&cc, &cell));
  484. tt_int_op(CELL_CREATED2, ==, cc.cell_type);
  485. tt_int_op(64, ==, cc.handshake_len);
  486. tt_mem_op(cc.reply,==, b, 80);
  487. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  488. tt_int_op(cell.command, ==, cell2.command);
  489. tt_mem_op(cell.payload,==, 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, ==, created_cell_parse(&cc, &cell));
  498. tt_int_op(CELL_CREATED2, ==, cc.cell_type);
  499. tt_int_op(496, ==, cc.handshake_len);
  500. tt_mem_op(cc.reply,==, b, 496);
  501. tt_int_op(0, ==, created_cell_format(&cell2, &cc));
  502. tt_int_op(cell.command, ==, cell2.command);
  503. tt_mem_op(cell.payload,==, 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, ==, created_cell_parse(&cc, &cell));
  511. /* Unformattable CREATED2 cell: too long! */
  512. cc.handshake_len = 497;
  513. tt_int_op(-1, ==, 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, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  538. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  539. tt_int_op(RELAY_COMMAND_EXTEND, ==, ec.cell_type);
  540. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  541. tt_int_op(258, ==, ec.orport_ipv4.port);
  542. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  543. tt_mem_op(ec.node_id,==, "electroencephalogram", 20);
  544. tt_int_op(cc->cell_type, ==, CELL_CREATE);
  545. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_TAP);
  546. tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_CHALLENGE_LEN);
  547. tt_mem_op(cc->onionskin,==, b, TAP_ONIONSKIN_CHALLENGE_LEN+20);
  548. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  549. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
  550. tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  551. tt_mem_op(p2,==, 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, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND,
  561. p, 26+TAP_ONIONSKIN_CHALLENGE_LEN));
  562. tt_int_op(RELAY_COMMAND_EXTEND, ==, ec.cell_type);
  563. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  564. tt_int_op(258, ==, ec.orport_ipv4.port);
  565. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  566. tt_mem_op(ec.node_id,==, "electroencephalogram", 20);
  567. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  568. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
  569. tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
  570. tt_mem_op(cc->onionskin,==, b, NTOR_ONIONSKIN_LEN+20);
  571. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  572. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
  573. tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
  574. tt_mem_op(p2,==, p, RELAY_PAYLOAD_SIZE);
  575. tt_int_op(0, ==, 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, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2,
  589. p, 35+NTOR_ONIONSKIN_LEN));
  590. tt_int_op(RELAY_COMMAND_EXTEND2, ==, ec.cell_type);
  591. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  592. tt_int_op(61681, ==, ec.orport_ipv4.port);
  593. tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
  594. tt_mem_op(ec.node_id,==, "anarchoindividualist", 20);
  595. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  596. tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
  597. tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
  598. tt_mem_op(cc->onionskin,==, b, NTOR_ONIONSKIN_LEN+20);
  599. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  600. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
  601. tt_int_op(p2_len, ==, 35+NTOR_ONIONSKIN_LEN);
  602. tt_mem_op(p2,==, 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, ==, extend_cell_parse(&ec, RELAY_COMMAND_EXTEND2, p, 89+99));
  621. tt_int_op(RELAY_COMMAND_EXTEND2, ==, ec.cell_type);
  622. tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
  623. tt_int_op(61681, ==, ec.orport_ipv4.port);
  624. tt_str_op("2002::f0:c51e", ==, fmt_addr(&ec.orport_ipv6.addr));
  625. tt_int_op(4370, ==, ec.orport_ipv6.port);
  626. tt_mem_op(ec.node_id,==, "anthropomorphization", 20);
  627. tt_int_op(cc->cell_type, ==, CELL_CREATE2);
  628. tt_int_op(cc->handshake_type, ==, 0x105);
  629. tt_int_op(cc->handshake_len, ==, 99);
  630. tt_mem_op(cc->onionskin,==, b, 99+20);
  631. tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
  632. tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
  633. /* We'll generate it minus the IPv6 address and minus the konami code */
  634. tt_int_op(p2_len, ==, 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,==, b, 99+20);
  643. tt_int_op(0, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED, p,
  740. TAP_ONIONSKIN_REPLY_LEN));
  741. tt_int_op(RELAY_COMMAND_EXTENDED, ==, ec.cell_type);
  742. tt_int_op(cc->cell_type, ==, CELL_CREATED);
  743. tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_REPLY_LEN);
  744. tt_mem_op(cc->reply,==, b, TAP_ONIONSKIN_REPLY_LEN);
  745. tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  746. tt_int_op(RELAY_COMMAND_EXTENDED, ==, p2_cmd);
  747. tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, p2_len);
  748. tt_mem_op(p2,==, 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, ==, extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, 2+42));
  757. tt_int_op(RELAY_COMMAND_EXTENDED2, ==, ec.cell_type);
  758. tt_int_op(cc->cell_type, ==, CELL_CREATED2);
  759. tt_int_op(cc->handshake_len, ==, 42);
  760. tt_mem_op(cc->reply,==, b, 42+10);
  761. tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
  762. tt_int_op(RELAY_COMMAND_EXTENDED2, ==, p2_cmd);
  763. tt_int_op(2+42, ==, p2_len);
  764. tt_mem_op(p2,==, p, sizeof(p2));
  765. /* Try an almost-too-long EXTENDED2 cell */
  766. memcpy(p, "\x01\xf0", 2);
  767. tt_int_op(0, ==,
  768. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  769. /* Now try a too-long extended2 cell. That's the only misparse I can think
  770. * of. */
  771. memcpy(p, "\x01\xf1", 2);
  772. tt_int_op(-1, ==,
  773. extended_cell_parse(&ec, RELAY_COMMAND_EXTENDED2, p, sizeof(p)));
  774. done:
  775. tor_free(mem_op_hex_tmp);
  776. }
  777. static void
  778. test_cfmt_resolved_cells(void *arg)
  779. {
  780. smartlist_t *addrs = smartlist_new();
  781. relay_header_t rh;
  782. cell_t cell;
  783. int r, errcode;
  784. address_ttl_t *a;
  785. (void)arg;
  786. #define CLEAR_CELL() do { \
  787. memset(&cell, 0, sizeof(cell)); \
  788. memset(&rh, 0, sizeof(rh)); \
  789. } while (0)
  790. #define CLEAR_ADDRS() do { \
  791. SMARTLIST_FOREACH(addrs, address_ttl_t *, a, \
  792. address_ttl_free(a); ); \
  793. smartlist_clear(addrs); \
  794. } while (0)
  795. #define SET_CELL(s) do { \
  796. CLEAR_CELL(); \
  797. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  798. rh.length = sizeof((s))-1; \
  799. rh.command = RELAY_COMMAND_RESOLVED; \
  800. errcode = -1; \
  801. } while (0)
  802. /* The cell format is one or more answers; each of the form
  803. * type [1 byte---0:hostname, 4:ipv4, 6:ipv6, f0:err-transient, f1:err]
  804. * length [1 byte]
  805. * body [length bytes]
  806. * ttl [4 bytes]
  807. */
  808. /* Let's try an empty cell */
  809. SET_CELL("");
  810. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  811. tt_int_op(errcode, ==, 0);
  812. tt_int_op(r, ==, 0);
  813. tt_int_op(smartlist_len(addrs), ==, 0);
  814. CLEAR_ADDRS(); /* redundant but let's be consistent */
  815. /* Cell with one ipv4 addr */
  816. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  817. tt_int_op(rh.length, ==, 10);
  818. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  819. tt_int_op(errcode, ==, 0);
  820. tt_int_op(r, ==, 0);
  821. tt_int_op(smartlist_len(addrs), ==, 1);
  822. a = smartlist_get(addrs, 0);
  823. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  824. tt_ptr_op(a->hostname, ==, NULL);
  825. tt_int_op(a->ttl, ==, 256);
  826. CLEAR_ADDRS();
  827. /* Cell with one ipv6 addr */
  828. SET_CELL("\x06\x10"
  829. "\x20\x02\x90\x90\x00\x00\x00\x00"
  830. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  831. "\x02\00\x00\x01");
  832. tt_int_op(rh.length, ==, 22);
  833. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  834. tt_int_op(errcode, ==, 0);
  835. tt_int_op(r, ==, 0);
  836. tt_int_op(smartlist_len(addrs), ==, 1);
  837. a = smartlist_get(addrs, 0);
  838. tt_str_op(fmt_addr(&a->addr), ==, "2002:9090::f0f0:abcd");
  839. tt_ptr_op(a->hostname, ==, NULL);
  840. tt_int_op(a->ttl, ==, 0x2000001);
  841. CLEAR_ADDRS();
  842. /* Cell with one hostname */
  843. SET_CELL("\x00\x11"
  844. "motherbrain.zebes"
  845. "\x00\00\x00\x00");
  846. tt_int_op(rh.length, ==, 23);
  847. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  848. tt_int_op(errcode, ==, 0);
  849. tt_int_op(r, ==, 0);
  850. tt_int_op(smartlist_len(addrs), ==, 1);
  851. a = smartlist_get(addrs, 0);
  852. tt_assert(tor_addr_is_null(&a->addr));
  853. tt_str_op(a->hostname, ==, "motherbrain.zebes");
  854. tt_int_op(a->ttl, ==, 0);
  855. CLEAR_ADDRS();
  856. #define LONG_NAME \
  857. "this-hostname-has-255-characters.in-order-to-test-whether-very-long.ho" \
  858. "stnames-are-accepted.i-am-putting-it-in-a-macro-because-although.this-" \
  859. "function-is-already-very-full.of-copy-and-pasted-stuff.having-this-app" \
  860. "ear-more-than-once-would-bother-me-somehow.is"
  861. tt_int_op(strlen(LONG_NAME), ==, 255);
  862. SET_CELL("\x00\xff"
  863. LONG_NAME
  864. "\x00\01\x00\x00");
  865. tt_int_op(rh.length, ==, 261);
  866. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  867. tt_int_op(errcode, ==, 0);
  868. tt_int_op(r, ==, 0);
  869. tt_int_op(smartlist_len(addrs), ==, 1);
  870. a = smartlist_get(addrs, 0);
  871. tt_assert(tor_addr_is_null(&a->addr));
  872. tt_str_op(a->hostname, ==, LONG_NAME);
  873. tt_int_op(a->ttl, ==, 65536);
  874. CLEAR_ADDRS();
  875. /* Cells with an error */
  876. SET_CELL("\xf0\x2b"
  877. "I'm sorry, Dave. I'm afraid I can't do that"
  878. "\x00\x11\x22\x33");
  879. tt_int_op(rh.length, ==, 49);
  880. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  881. tt_int_op(errcode, ==, RESOLVED_TYPE_ERROR_TRANSIENT);
  882. tt_int_op(r, ==, 0);
  883. tt_int_op(smartlist_len(addrs), ==, 0);
  884. CLEAR_ADDRS();
  885. SET_CELL("\xf1\x40"
  886. "This hostname is too important for me to allow you to resolve it"
  887. "\x00\x00\x00\x00");
  888. tt_int_op(rh.length, ==, 70);
  889. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  890. tt_int_op(errcode, ==, RESOLVED_TYPE_ERROR);
  891. tt_int_op(r, ==, 0);
  892. tt_int_op(smartlist_len(addrs), ==, 0);
  893. CLEAR_ADDRS();
  894. /* Cell with an unrecognized type */
  895. SET_CELL("\xee\x16"
  896. "fault in the AE35 unit"
  897. "\x09\x09\x01\x01");
  898. tt_int_op(rh.length, ==, 28);
  899. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  900. tt_int_op(errcode, ==, 0);
  901. tt_int_op(r, ==, 0);
  902. tt_int_op(smartlist_len(addrs), ==, 0);
  903. CLEAR_ADDRS();
  904. /* Cell with one of each */
  905. SET_CELL(/* unrecognized: */
  906. "\xee\x16"
  907. "fault in the AE35 unit"
  908. "\x09\x09\x01\x01"
  909. /* error: */
  910. "\xf0\x2b"
  911. "I'm sorry, Dave. I'm afraid I can't do that"
  912. "\x00\x11\x22\x33"
  913. /* IPv6: */
  914. "\x06\x10"
  915. "\x20\x02\x90\x90\x00\x00\x00\x00"
  916. "\x00\x00\x00\x00\xf0\xf0\xab\xcd"
  917. "\x02\00\x00\x01"
  918. /* IPv4: */
  919. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  920. /* Hostname: */
  921. "\x00\x11"
  922. "motherbrain.zebes"
  923. "\x00\00\x00\x00"
  924. );
  925. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  926. tt_int_op(errcode, ==, 0); /* no error reported; we got answers */
  927. tt_int_op(r, ==, 0);
  928. tt_int_op(smartlist_len(addrs), ==, 3);
  929. a = smartlist_get(addrs, 0);
  930. tt_str_op(fmt_addr(&a->addr), ==, "2002:9090::f0f0:abcd");
  931. tt_ptr_op(a->hostname, ==, NULL);
  932. tt_int_op(a->ttl, ==, 0x2000001);
  933. a = smartlist_get(addrs, 1);
  934. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  935. tt_ptr_op(a->hostname, ==, NULL);
  936. tt_int_op(a->ttl, ==, 256);
  937. a = smartlist_get(addrs, 2);
  938. tt_assert(tor_addr_is_null(&a->addr));
  939. tt_str_op(a->hostname, ==, "motherbrain.zebes");
  940. tt_int_op(a->ttl, ==, 0);
  941. CLEAR_ADDRS();
  942. /* Cell with several of similar type */
  943. SET_CELL(/* IPv4 */
  944. "\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  945. "\x04\x04" "\x08\x08\x08\x08" "\x00\00\x01\x05"
  946. "\x04\x04" "\x7f\xb0\x02\xb0" "\x00\01\xff\xff"
  947. /* IPv6 */
  948. "\x06\x10"
  949. "\x20\x02\x90\x00\x00\x00\x00\x00"
  950. "\x00\x00\x00\x00\xca\xfe\xf0\x0d"
  951. "\x00\00\x00\x01"
  952. "\x06\x10"
  953. "\x20\x02\x90\x01\x00\x00\x00\x00"
  954. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  955. "\x00\00\x00\x03");
  956. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  957. tt_int_op(errcode, ==, 0);
  958. tt_int_op(r, ==, 0);
  959. tt_int_op(smartlist_len(addrs), ==, 5);
  960. a = smartlist_get(addrs, 0);
  961. tt_str_op(fmt_addr(&a->addr), ==, "127.0.2.10");
  962. tt_ptr_op(a->hostname, ==, NULL);
  963. tt_int_op(a->ttl, ==, 256);
  964. a = smartlist_get(addrs, 1);
  965. tt_str_op(fmt_addr(&a->addr), ==, "8.8.8.8");
  966. tt_ptr_op(a->hostname, ==, NULL);
  967. tt_int_op(a->ttl, ==, 261);
  968. a = smartlist_get(addrs, 2);
  969. tt_str_op(fmt_addr(&a->addr), ==, "127.176.2.176");
  970. tt_ptr_op(a->hostname, ==, NULL);
  971. tt_int_op(a->ttl, ==, 131071);
  972. a = smartlist_get(addrs, 3);
  973. tt_str_op(fmt_addr(&a->addr), ==, "2002:9000::cafe:f00d");
  974. tt_ptr_op(a->hostname, ==, NULL);
  975. tt_int_op(a->ttl, ==, 1);
  976. a = smartlist_get(addrs, 4);
  977. tt_str_op(fmt_addr(&a->addr), ==, "2002:9001::fa:cade");
  978. tt_ptr_op(a->hostname, ==, NULL);
  979. tt_int_op(a->ttl, ==, 3);
  980. CLEAR_ADDRS();
  981. /* Full cell */
  982. #define LONG_NAME2 \
  983. "this-name-has-231-characters.so-that-it-plus-LONG_NAME-can-completely-" \
  984. "fill-up-the-payload-of-a-cell.its-important-to-check-for-the-full-thin" \
  985. "g-case.to-avoid-off-by-one-errors.where-full-things-are-misreported-as" \
  986. ".overflowing-by-one.z"
  987. tt_int_op(strlen(LONG_NAME2), ==, 231);
  988. SET_CELL("\x00\xff"
  989. LONG_NAME
  990. "\x00\01\x00\x00"
  991. "\x00\xe7"
  992. LONG_NAME2
  993. "\x00\01\x00\x00");
  994. tt_int_op(rh.length, ==, RELAY_PAYLOAD_SIZE);
  995. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  996. tt_int_op(errcode, ==, 0);
  997. tt_int_op(r, ==, 0);
  998. tt_int_op(smartlist_len(addrs), ==, 2);
  999. a = smartlist_get(addrs, 0);
  1000. tt_str_op(a->hostname, ==, LONG_NAME);
  1001. a = smartlist_get(addrs, 1);
  1002. tt_str_op(a->hostname, ==, LONG_NAME2);
  1003. CLEAR_ADDRS();
  1004. /* BAD CELLS */
  1005. /* Invalid length on an IPv4 */
  1006. SET_CELL("\x04\x03zzz1234");
  1007. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1008. tt_int_op(errcode, ==, 0);
  1009. tt_int_op(r, ==, -1);
  1010. tt_int_op(smartlist_len(addrs), ==, 0);
  1011. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1012. "\x04\x05zzzzz1234");
  1013. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1014. tt_int_op(errcode, ==, 0);
  1015. tt_int_op(r, ==, -1);
  1016. tt_int_op(smartlist_len(addrs), ==, 0);
  1017. /* Invalid length on an IPv6 */
  1018. SET_CELL("\x06\x03zzz1234");
  1019. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1020. tt_int_op(errcode, ==, 0);
  1021. tt_int_op(r, ==, -1);
  1022. tt_int_op(smartlist_len(addrs), ==, 0);
  1023. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1024. "\x06\x17wwwwwwwwwwwwwwwww1234");
  1025. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1026. tt_int_op(errcode, ==, 0);
  1027. tt_int_op(r, ==, -1);
  1028. tt_int_op(smartlist_len(addrs), ==, 0);
  1029. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00"
  1030. "\x06\x10xxxx");
  1031. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1032. tt_int_op(errcode, ==, 0);
  1033. tt_int_op(r, ==, -1);
  1034. tt_int_op(smartlist_len(addrs), ==, 0);
  1035. /* Empty hostname */
  1036. SET_CELL("\x00\x00xxxx");
  1037. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1038. tt_int_op(errcode, ==, 0);
  1039. tt_int_op(r, ==, -1);
  1040. tt_int_op(smartlist_len(addrs), ==, 0);
  1041. /* rh.length out of range */
  1042. CLEAR_CELL();
  1043. rh.length = 499;
  1044. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1045. tt_int_op(errcode, ==, 0);
  1046. tt_int_op(r, ==, -1);
  1047. tt_int_op(smartlist_len(addrs), ==, 0);
  1048. /* Item length extends beyond rh.length */
  1049. CLEAR_CELL();
  1050. SET_CELL("\x00\xff"
  1051. LONG_NAME
  1052. "\x00\01\x00\x00");
  1053. rh.length -= 1;
  1054. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1055. tt_int_op(r, ==, -1);
  1056. tt_int_op(smartlist_len(addrs), ==, 0);
  1057. rh.length -= 5;
  1058. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1059. tt_int_op(r, ==, -1);
  1060. tt_int_op(smartlist_len(addrs), ==, 0);
  1061. SET_CELL("\x04\x04" "\x7f\x00\x02\x0a" "\x00\00\x01\x00");
  1062. rh.length -= 1;
  1063. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1064. tt_int_op(r, ==, -1);
  1065. tt_int_op(smartlist_len(addrs), ==, 0);
  1066. SET_CELL("\xee\x10"
  1067. "\x20\x02\x90\x01\x00\x00\x00\x00"
  1068. "\x00\x00\x00\x00\x00\xfa\xca\xde"
  1069. "\x00\00\x00\x03");
  1070. rh.length -= 1;
  1071. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1072. tt_int_op(r, ==, -1);
  1073. tt_int_op(smartlist_len(addrs), ==, 0);
  1074. /* Truncated item after first character */
  1075. SET_CELL("\x04");
  1076. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1077. tt_int_op(r, ==, -1);
  1078. tt_int_op(smartlist_len(addrs), ==, 0);
  1079. SET_CELL("\xee");
  1080. r = resolved_cell_parse(&cell, &rh, addrs, &errcode);
  1081. tt_int_op(r, ==, -1);
  1082. tt_int_op(smartlist_len(addrs), ==, 0);
  1083. done:
  1084. CLEAR_ADDRS();
  1085. CLEAR_CELL();
  1086. smartlist_free(addrs);
  1087. #undef CLEAR_ADDRS
  1088. #undef CLEAR_CELL
  1089. }
  1090. static void
  1091. test_cfmt_is_destroy(void *arg)
  1092. {
  1093. cell_t cell;
  1094. packed_cell_t packed;
  1095. circid_t circid = 0;
  1096. channel_t *chan;
  1097. (void)arg;
  1098. chan = tor_malloc_zero(sizeof(channel_t));
  1099. memset(&cell, 0xff, sizeof(cell));
  1100. cell.circ_id = 3003;
  1101. cell.command = CELL_RELAY;
  1102. cell_pack(&packed, &cell, 0);
  1103. chan->wide_circ_ids = 0;
  1104. tt_assert(! packed_cell_is_destroy(chan, &packed, &circid));
  1105. tt_int_op(circid, ==, 0);
  1106. cell_pack(&packed, &cell, 1);
  1107. chan->wide_circ_ids = 1;
  1108. tt_assert(! packed_cell_is_destroy(chan, &packed, &circid));
  1109. tt_int_op(circid, ==, 0);
  1110. cell.command = CELL_DESTROY;
  1111. cell_pack(&packed, &cell, 0);
  1112. chan->wide_circ_ids = 0;
  1113. tt_assert(packed_cell_is_destroy(chan, &packed, &circid));
  1114. tt_int_op(circid, ==, 3003);
  1115. circid = 0;
  1116. cell_pack(&packed, &cell, 1);
  1117. chan->wide_circ_ids = 1;
  1118. tt_assert(packed_cell_is_destroy(chan, &packed, &circid));
  1119. done:
  1120. tor_free(chan);
  1121. }
  1122. #define TEST(name, flags) \
  1123. { #name, test_cfmt_ ## name, flags, 0, NULL }
  1124. struct testcase_t cell_format_tests[] = {
  1125. TEST(relay_header, 0),
  1126. TEST(begin_cells, 0),
  1127. TEST(connected_cells, 0),
  1128. TEST(create_cells, 0),
  1129. TEST(created_cells, 0),
  1130. TEST(extend_cells, 0),
  1131. TEST(extended_cells, 0),
  1132. TEST(resolved_cells, 0),
  1133. TEST(is_destroy, 0),
  1134. END_OF_TESTCASES
  1135. };