test_conflux_cell.c (1524B)
1 /* Copyright (c) 2023, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 /** 5 * \file test_conflux_cell. 6 * \brief Test conflux cells. 7 */ 8 9 #include "test/test.h" 10 #include "test/test_helpers.h" 11 #include "test/log_test_helpers.h" 12 13 #include "core/or/conflux_cell.h" 14 #include "core/or/conflux_st.h" 15 #include "trunnel/conflux.h" 16 #include "core/or/relay_msg.h" 17 #include "core/or/relay_msg_st.h" 18 19 #include "lib/crypt_ops/crypto_rand.h" 20 21 static void 22 test_link(void *arg) 23 { 24 conflux_cell_link_t link; 25 conflux_cell_link_t *decoded_link = NULL; 26 relay_msg_t msg; 27 uint8_t buf0[RELAY_PAYLOAD_SIZE_MAX]; 28 uint8_t buf1[RELAY_PAYLOAD_SIZE_MAX]; 29 30 (void) arg; 31 32 memset(&link, 0, sizeof(link)); 33 34 link.desired_ux = CONFLUX_UX_HIGH_THROUGHPUT; 35 link.last_seqno_recv = 0; 36 link.last_seqno_sent = 0; 37 link.version = 0x01; 38 39 crypto_rand((char *) link.nonce, sizeof(link.nonce)); 40 41 ssize_t cell_len = build_link_cell(&link, buf0); 42 tt_int_op(cell_len, OP_GT, 0); 43 msg.length = cell_len; 44 msg.body = buf0; 45 46 decoded_link = conflux_cell_parse_link(&msg); 47 tt_assert(decoded_link); 48 49 ssize_t enc_cell_len = build_link_cell(decoded_link, buf1); 50 tt_int_op(cell_len, OP_EQ, enc_cell_len); 51 52 /* Validate the original link object with the decoded one. */ 53 tt_mem_op(&link, OP_EQ, decoded_link, sizeof(link)); 54 tor_free(decoded_link); 55 56 done: 57 relay_msg_clear(&msg); 58 tor_free(decoded_link); 59 } 60 61 struct testcase_t conflux_cell_tests[] = { 62 { "link", test_link, TT_FORK, NULL, NULL }, 63 64 END_OF_TESTCASES 65 };