LCOV - code coverage report
Current view: top level - test - test_cell_formats.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 865 865 100.0 %
Date: 2021-11-24 03:28:48 Functions: 10 10 100.0 %

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

Generated by: LCOV version 1.14