test_cell_formats.c 47 KB

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