From 2e57663cde1c1f6d25677402005d1ddc45169f8f Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Thu, 7 Oct 2021 08:37:00 +0100 Subject: [PATCH] increased test coverage (#844) * Start adding helper functions to the wire tests * Add tests for two more wire functions * Update test expects with new test data * Use the normal mac address format, like a boss --- tests/tests-wire.expected | 34 +++++++++ tools/tests-wire.c | 156 +++++++++++++++++++++++++++++++++----- 2 files changed, 170 insertions(+), 20 deletions(-) diff --git a/tests/tests-wire.expected b/tests/tests-wire.expected index 4624103..e4ba9de 100644 --- a/tests/tests-wire.expected +++ b/tests/tests-wire.expected @@ -4,6 +4,8 @@ environment: common.community = "abc123def456z" REGISTER: common.pc = 1 REGISTER: reg.cookie = 0 +REGISTER: reg.srcMac[] = 0:1:2:3:4:5 +REGISTER: reg.dstMac[] = 10:11:12:13:14:15 REGISTER: reg.dev_addr.net_addr = 0x20212223 REGISTER: reg.dev_addr.net_bitlen = 25 REGISTER: reg.dev_desc = "Dummy_Dev_Desc" @@ -15,3 +17,35 @@ REGISTER: output idx = 0x3d 020: 04 05 10 11 12 13 14 15 20 21 22 23 19 44 75 6d | !"# Dum| 030: 6d 79 5f 44 65 76 5f 44 65 73 63 00 00 |my_Dev_Desc | +REGISTER_SUPER: common.pc = 5 +REGISTER_SUPER: reg.cookie = 0 +REGISTER_SUPER: reg.edgeMac[] = 20:21:22:23:24:25 +REGISTER_SUPER: reg.dev_addr.net_addr = 0x20212223 +REGISTER_SUPER: reg.dev_addr.net_bitlen = 25 +REGISTER_SUPER: reg.dev_desc = "Dummy_Dev_Desc" +REGISTER_SUPER: reg.auth.scheme = 1 +REGISTER_SUPER: reg.auth.token_size = 16 +REGISTER_SUPER: reg.auth.token[0] = 0xfe +REGISTER_SUPER: reg.key_time = 600 + +REGISTER_SUPER: output retval = 0x36 +REGISTER_SUPER: output idx = 0x4f +000: 03 02 00 05 61 62 63 31 32 33 64 65 66 34 35 36 | abc123def456| +010: 7a 00 00 00 00 00 00 00 00 00 00 00 20 21 22 23 |z !"#| +020: 24 25 20 21 22 23 19 44 75 6d 6d 79 5f 44 65 76 |$% !"# Dummy_Dev| +030: 5f 44 65 73 63 00 00 00 01 00 10 fe 00 00 00 fd |_Desc | +040: 00 00 00 fc 00 00 00 00 00 00 fb 00 00 02 58 | X| + +UNREGISTER_SUPER: common.pc = 6 +UNREGISTER_SUPER: unreg.auth.scheme = 1 +UNREGISTER_SUPER: unreg.auth.token_size = 16 +UNREGISTER_SUPER: unreg.auth.token[0] = 0xfe +UNREGISTER_SUPER: unreg.srcMac[] = 30:31:32:33:34:35 + +UNREGISTER_SUPER: output retval = 0x19 +UNREGISTER_SUPER: output idx = 0x32 +000: 03 02 00 06 61 62 63 31 32 33 64 65 66 34 35 36 | abc123def456| +010: 7a 00 00 00 00 00 00 00 00 01 00 10 fe 00 00 00 |z | +020: fd 00 00 00 fc 00 00 00 00 00 00 fb 30 31 32 33 | 0123| +030: 34 35 |45| + diff --git a/tools/tests-wire.c b/tools/tests-wire.c index 010deb1..5dfa5de 100644 --- a/tools/tests-wire.c +++ b/tools/tests-wire.c @@ -21,6 +21,63 @@ #include "n2n.h" #include "hexdump.h" +void init_ip_subnet(n2n_ip_subnet_t * d) { + d->net_addr = 0x20212223; + d->net_bitlen = 25; +} + +void print_ip_subnet(char *test_name, char *field, n2n_ip_subnet_t * d) { + printf("%s: %s.net_addr = 0x%08x\n", + test_name, field, d->net_addr); + printf("%s: %s.net_bitlen = %i\n", + test_name, field, d->net_bitlen); +} + +void init_mac(n2n_mac_t mac, const uint8_t o0, const uint8_t o1, + const uint8_t o2, const uint8_t o3, + const uint8_t o4, const uint8_t o5) { + mac[0] = o0; + mac[1] = o1; + mac[2] = o2; + mac[3] = o3; + mac[4] = o4; + mac[5] = o5; +} + +void print_mac(char *test_name, char *field, n2n_mac_t mac) { + printf("%s: %s[] = %x:%x:%x:%x:%x:%x\n", + test_name, field, + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + +void init_auth(n2n_auth_t *auth) { + auth->scheme = n2n_auth_simple_id; + auth->token_size = 16; + auth->token[0] = 0xfe; + auth->token[4] = 0xfd; + auth->token[8] = 0xfc; + auth->token[15] = 0xfb; +} + +void print_auth(char *test_name, char *field, n2n_auth_t *auth) { + printf("%s: %s.scheme = %i\n", test_name, field, auth->scheme); + printf("%s: %s.token_size = %i\n", test_name, field, auth->token_size); + printf("%s: %s.token[0] = 0x%02x\n", test_name, field, auth->token[0]); +} + +void init_common(n2n_common_t *common, char *community) { + memset( common, 0, sizeof(*common) ); + common->ttl = N2N_DEFAULT_TTL; + common->flags = 0; + strncpy( (char *)common->community, community, N2N_COMMUNITY_SIZE ); +} + +void print_common(char *test_name, n2n_common_t *common) { + printf("%s: common.ttl = %i\n", test_name, common->ttl); + printf("%s: common.flags = %i\n", test_name, common->flags); + printf("%s: common.community = \"%s\"\n", test_name, common->community); +} + void test_REGISTER(n2n_common_t *common) { char *test_name = "REGISTER"; @@ -29,19 +86,16 @@ void test_REGISTER(n2n_common_t *common) { n2n_REGISTER_t reg; memset( ®, 0, sizeof(reg) ); - n2n_mac_t dummysrcMac={0,1,2,3,4,5}; - memcpy( reg.srcMac, dummysrcMac, sizeof(dummysrcMac)); - n2n_mac_t dummydstMac={0x10,0x11,0x12,0x13,0x14,0x15}; - memcpy( reg.dstMac, dummydstMac, sizeof(dummydstMac)); - reg.dev_addr.net_addr = 0x20212223; - reg.dev_addr.net_bitlen = 25; + init_mac( reg.srcMac, 0,1,2,3,4,5); + init_mac( reg.dstMac, 0x10,0x11,0x12,0x13,0x14,0x15); + init_ip_subnet(®.dev_addr); strcpy( (char *)reg.dev_desc, "Dummy_Dev_Desc" ); printf("%s: reg.cookie = %i\n", test_name, reg.cookie); - // TODO: print reg.srcMac, reg.dstMac + print_mac(test_name, "reg.srcMac", reg.srcMac); + print_mac(test_name, "reg.dstMac", reg.dstMac); // TODO: print reg.sock - printf("%s: reg.dev_addr.net_addr = 0x%08x\n", test_name, reg.dev_addr.net_addr); - printf("%s: reg.dev_addr.net_bitlen = %i\n", test_name, reg.dev_addr.net_bitlen); + print_ip_subnet(test_name, "reg.dev_addr", ®.dev_addr); printf("%s: reg.dev_desc = \"%s\"\n", test_name, reg.dev_desc); printf("\n"); @@ -59,24 +113,86 @@ void test_REGISTER(n2n_common_t *common) { printf("\n"); } +void test_REGISTER_SUPER(n2n_common_t *common) { + char *test_name = "REGISTER_SUPER"; + + common->pc = n2n_register_super; + printf("%s: common.pc = %i\n", test_name, common->pc); + + n2n_REGISTER_SUPER_t reg; + memset( ®, 0, sizeof(reg) ); + init_mac( reg.edgeMac, 0x20,0x21,0x22,0x23,0x24,0x25); + // n2n_sock_t sock + init_ip_subnet(®.dev_addr); + strcpy( (char *)reg.dev_desc, "Dummy_Dev_Desc" ); + init_auth(®.auth); + reg.key_time = 600; + + + printf("%s: reg.cookie = %i\n", test_name, reg.cookie); + print_mac(test_name, "reg.edgeMac", reg.edgeMac); + // TODO: print reg.sock + print_ip_subnet(test_name, "reg.dev_addr", ®.dev_addr); + printf("%s: reg.dev_desc = \"%s\"\n", test_name, reg.dev_desc); + print_auth(test_name, "reg.auth", ®.auth); + printf("%s: reg.key_time = %"PRIi32"\n", test_name, reg.key_time); + printf("\n"); + + uint8_t pktbuf[N2N_PKT_BUF_SIZE]; + size_t idx = 0; + size_t retval = encode_REGISTER_SUPER( pktbuf, &idx, common, ®); + + printf("%s: output retval = 0x%"PRIx64"\n", test_name, retval); + printf("%s: output idx = 0x%"PRIx64"\n", test_name, idx); + fhexdump(0, pktbuf, idx, stdout); + + // TODO: decode_REGISTER_SUPER() and print + + fprintf(stderr, "%s: tested\n", test_name); + printf("\n"); +} + +void test_UNREGISTER_SUPER(n2n_common_t *common) { + char *test_name = "UNREGISTER_SUPER"; + + common->pc = n2n_unregister_super; + printf("%s: common.pc = %i\n", test_name, common->pc); + + n2n_UNREGISTER_SUPER_t unreg; + memset( &unreg, 0, sizeof(unreg) ); + init_auth(&unreg.auth); + init_mac( unreg.srcMac, 0x30,0x31,0x32,0x33,0x34,0x35); + + + print_auth(test_name, "unreg.auth", &unreg.auth); + print_mac(test_name, "unreg.srcMac", unreg.srcMac); + printf("\n"); + + uint8_t pktbuf[N2N_PKT_BUF_SIZE]; + size_t idx = 0; + size_t retval = encode_UNREGISTER_SUPER( pktbuf, &idx, common, &unreg); + + printf("%s: output retval = 0x%"PRIx64"\n", test_name, retval); + printf("%s: output idx = 0x%"PRIx64"\n", test_name, idx); + fhexdump(0, pktbuf, idx, stdout); + + // TODO: decode_UNREGISTER_SUPER() and print + + fprintf(stderr, "%s: tested\n", test_name); + printf("\n"); +} + int main(int argc, char * argv[]) { char *test_name = "environment"; - n2n_community_t c; - strncpy((char *)c, "abc123def456z", sizeof(c)); - n2n_common_t common; - memset( &common, 0, sizeof(common) ); - common.ttl = N2N_DEFAULT_TTL; - common.flags = 0; - memcpy( common.community, c, N2N_COMMUNITY_SIZE ); - - printf("%s: common.ttl = %i\n", test_name, common.ttl); - printf("%s: common.flags = %i\n", test_name, common.flags); - printf("%s: common.community = \"%s\"\n", test_name, common.community); + init_common( &common, "abc123def456z" ); + print_common( test_name, &common ); printf("\n"); test_REGISTER(&common); + test_REGISTER_SUPER(&common); + test_UNREGISTER_SUPER(&common); // TODO: add more wire tests return 0;