From 7d510e2f4fda87a754662c94bd5b50386f93ee21 Mon Sep 17 00:00:00 2001 From: scribam Date: Thu, 5 Sep 2024 18:42:14 +0200 Subject: [PATCH] Attempt to fix unit tests --- .github/workflows/c-cpp.yml | 7 + Makefile | 5 +- include/pico_tree.h | 7 + test/dummy.c | 5 +- test/ppp.c | 13 +- test/test_tftp_app_client.c | 15 ++- test/tickless.c | 15 ++- test/unit/modunit_pico_6lowpan.c | 23 +--- test/unit/modunit_pico_802154.c | 6 +- test/unit/modunit_pico_dns_sd.c | 13 +- test/unit/modunit_pico_hotplug_detection.c | 15 ++- test/unit/modunit_pico_igmp.c | 37 ++++-- test/unit/modunit_pico_ipv6_pmtu.c | 15 ++- test/unit/modunit_pico_mdns.c | 50 +++++--- test/unit/modunit_pico_mld.c | 45 +++++-- test/unit/modunit_pico_stack.c | 5 +- test/unit/unit_arp.c | 47 ++++--- test/unit/unit_dhcp.c | 78 ++++++----- test/unit/unit_dns.c | 35 ++--- test/unit/unit_icmp4.c | 142 +++++++++++---------- test/unit/unit_ipv4.c | 130 +++++++++++-------- test/unit/unit_ipv6.c | 96 +++++++------- test/unit/unit_mocks.c | 8 +- test/unit/unit_pico_device.c | 4 +- test/unit/unit_socket.c | 34 ++--- test/unit/unit_timer.c | 23 ++-- test/units.c | 2 +- 27 files changed, 516 insertions(+), 359 deletions(-) diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index b8f89729..2f21a098 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -13,3 +13,10 @@ jobs: steps: - uses: actions/checkout@v4 - run: make + - name: Unit tests + run: | + sudo apt-get update + sudo apt-get install check + make clean + make units UNITS=1 && ./test/units.sh + if: runner.os == 'Linux' diff --git a/Makefile b/Makefile index 7b293d2e..36f4e829 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,7 @@ RANLIB:=$(CROSS_COMPILE)ranlib SIZE:=$(CROSS_COMPILE)size STRIP_BIN:=$(CROSS_COMPILE)strip TEST_LDFLAGS=-pthread $(PREFIX)/modules/*.o $(PREFIX)/lib/*.o -lvdeplug -UNIT_LDFLAGS=-lcheck -lm -pthread -lrt -ifneq ("$(wildcard /etc/debian-release)","") - UNIT_LDFLAGS+=-lsubunit -endif +UNIT_LDFLAGS=-lcheck -lsubunit -lm -pthread -lrt UNIT_CFLAGS= $(CFLAGS) -Wno-missing-braces LIBNAME:="libpicotcp.a" diff --git a/include/pico_tree.h b/include/pico_tree.h index 6cf5d825..9dc583b5 100644 --- a/include/pico_tree.h +++ b/include/pico_tree.h @@ -29,6 +29,13 @@ #include "pico_config.h" +/* This is used in unit tests to declare a new tree, leaf root by default */ +#define PICO_TREE_DECLARE(name, compareFunction) \ + struct pico_tree name = \ + { \ + &LEAF, \ + compareFunction \ + } #define USE_PICO_PAGE0_ZALLOC (1) #define USE_PICO_ZALLOC (2) diff --git a/test/dummy.c b/test/dummy.c index 74205819..eb0e2568 100644 --- a/test/dummy.c +++ b/test/dummy.c @@ -6,7 +6,8 @@ volatile uint32_t pico_ms_tick; int main(void) { - pico_stack_init(); - pico_stack_tick(); + struct pico_stack *stack = NULL; + pico_stack_init(&stack); + pico_stack_tick(stack); return 0; } diff --git a/test/ppp.c b/test/ppp.c index 5a95b3c9..c26c2f91 100644 --- a/test/ppp.c +++ b/test/ppp.c @@ -124,20 +124,21 @@ static void cb_sock(uint16_t ev, struct pico_socket *s) } -static void ping(void) +static void ping(struct pico_stack *stack) { struct pico_socket *s; struct pico_ip4 dst; pico_string_to_ipv4("80.68.95.85", &dst.addr); - s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, cb_sock); + s = pico_socket_open(stack, PICO_PROTO_IPV4, PICO_PROTO_TCP, cb_sock); pico_socket_connect(s, &dst, short_be(80)); - pico_icmp4_ping("80.68.95.85", 10, 1000, 4000, 8, cb_ping); + pico_icmp4_ping(stack, "80.68.95.85", 10, 1000, 4000, 8, cb_ping); } int main(int argc, const char *argv[]) { + struct pico_stack *stack = NULL; const char *path = MODEM; const char *apn = APN; const char *passwd = PASSWD; @@ -162,7 +163,7 @@ int main(int argc, const char *argv[]) signal(SIGUSR2, sigusr2_hdl); signal(SIGINT, sigusr2_hdl); - pico_stack_init(); + pico_stack_init(&stack); #if defined PICO_SUPPORT_POLARSSL || defined PICO_SUPPORT_CYASSL pico_register_md5sum(md5sum); @@ -183,11 +184,11 @@ int main(int argc, const char *argv[]) pico_ppp_connect(ppp); while(1 < 2) { - pico_stack_tick(); + pico_stack_tick(stack); usleep(1000); if (ppp->link_state(ppp) && !ping_on) { ping_on++; - ping(); + ping(stack); } } } diff --git a/test/test_tftp_app_client.c b/test/test_tftp_app_client.c index dd5ece38..69e6df59 100644 --- a/test/test_tftp_app_client.c +++ b/test/test_tftp_app_client.c @@ -27,7 +27,7 @@ int32_t get_filesize(const char *filename) return buf.st_size; } -void start_rx(struct pico_tftp_session *session, int *synchro, const char *filename, int options) +void start_rx(struct pico_stack *stack, struct pico_tftp_session *session, int *synchro, const char *filename, int options) { int ret; int fd; @@ -60,7 +60,7 @@ void start_rx(struct pico_tftp_session *session, int *synchro, const char *filen for(; left; left -= countdown) { usleep(2000); /* PICO_IDLE(); */ - pico_stack_tick(); + pico_stack_tick(stack); if (countdown) continue; @@ -93,7 +93,7 @@ void start_rx(struct pico_tftp_session *session, int *synchro, const char *filen } } -void start_tx(struct pico_tftp_session *session, int *synchro, const char *filename, int options) +void start_tx(struct pico_stack *stack, struct pico_tftp_session *session, int *synchro, const char *filename, int options) { int ret; int fd; @@ -133,7 +133,7 @@ void start_tx(struct pico_tftp_session *session, int *synchro, const char *filen for(; left; left -= countdown) { usleep(2000); /* PICO_IDLE(); */ - pico_stack_tick(); + pico_stack_tick(stack); if (countdown) continue; @@ -184,9 +184,10 @@ int main(int argc, char**argv) union pico_address server_address; struct pico_ip4 netmask; struct pico_tftp_session *session; + struct pico_stack *stack; int synchro; int options = 0; - void (*operation)(struct pico_tftp_session *session, int *synchro, const char *filename, int options); + void (*operation)(struct pico_stack *stack, struct pico_tftp_session *session, int *synchro, const char *filename, int options); unsigned char macaddr[6] = { 0, 0, 0, 0xa, 0xb, 0x0 @@ -221,7 +222,7 @@ int main(int argc, char**argv) } printf("%s start!\n", argv[0]); - pico_stack_init(); + pico_stack_init(&stack); pico_dev = (struct pico_device *) pico_vde_create("/tmp/vde_switch", "tap0", macaddr); if(!pico_dev) { @@ -229,7 +230,7 @@ int main(int argc, char**argv) exit(1); } - pico_ipv4_link_add(pico_dev, my_ip, netmask); + pico_ipv4_link_add(stack, pico_dev, my_ip, netmask); printf("Starting picoTCP loop\n"); session = pico_tftp_app_setup(&server_address, short_be(PICO_TFTP_PORT), PICO_PROTO_IPV4, &synchro); diff --git a/test/tickless.c b/test/tickless.c index 70aa4159..6e9abd9f 100644 --- a/test/tickless.c +++ b/test/tickless.c @@ -67,7 +67,7 @@ static void iperf_cb(uint16_t ev, struct pico_socket *s) } -static void socket_setup(void) +static void socket_setup(struct pico_stack *stack) { int yes = 1; uint16_t send_port = 0; @@ -78,7 +78,7 @@ static void socket_setup(void) pico_string_to_ipv4("192.168.2.1", &dst.ip4.addr); send_port = short_be(5001); - s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &iperf_cb); + s = pico_socket_open(stack, PICO_PROTO_IPV4, PICO_PROTO_TCP, &iperf_cb); pico_socket_connect(s, &dst.ip4, send_port); return; } @@ -92,16 +92,17 @@ int main(void) uint8_t mac[6] = {0x00,0x00,0x00,0x12,0x34,0x56}; struct pico_socket *s; + struct pico_stack *stack; - pico_stack_init(); + pico_stack_init(&stack); - tap = (struct pico_device *) pico_tap_create("tap0"); + tap = (struct pico_device *) pico_tap_create(stack, "tap0"); if (!tap) while (1); pico_string_to_ipv4(ipaddr, &my_eth_addr.addr); pico_string_to_ipv4("255.255.255.0", &netmask.addr); - pico_ipv4_link_add(tap, my_eth_addr, netmask); + pico_ipv4_link_add(stack, tap, my_eth_addr, netmask); #ifdef PICO_SUPPORT_IPV6 { struct pico_ip6 my_addr6, netmask6; @@ -111,10 +112,10 @@ int main(void) } #endif - socket_setup(); + socket_setup(stack); while(1) { - interval = pico_stack_go(); + interval = pico_stack_go(stack); if (interval != 0) { // printf("Interval: %lld\n", interval); pico_tap_WFI(tap, interval); diff --git a/test/unit/modunit_pico_6lowpan.c b/test/unit/modunit_pico_6lowpan.c index 166dd767..c1dc3c22 100644 --- a/test/unit/modunit_pico_6lowpan.c +++ b/test/unit/modunit_pico_6lowpan.c @@ -182,7 +182,6 @@ START_TEST(tc_ctx_lookup) pico_string_to_ipv6("2aaa:1234:5678:9145:0102:0304:0506:0708", b.addr); STARTING(); - pico_stack_init(); TRYING("To find a prefix in the context tree\n"); ret = ctx_insert(a, 13, 54, 0, PICO_IPHC_CTX_COMPRESS, NULL); @@ -524,8 +523,6 @@ START_TEST(tc_addr_comp_mode) STARTING(); - pico_stack_init(); - TRYING("With MAC derived address\n"); ret = addr_comp_mode(iphc, &local2, addr, &dev, SRC_SHIFT); OUTPUT(); @@ -571,8 +568,6 @@ START_TEST(tc_addr_comp_prefix) STARTING(); - pico_stack_init(); - TRYING("With MCAST address\n"); ret = addr_comp_prefix(iphc, &ip, 1); RESULTS(); @@ -623,7 +618,6 @@ START_TEST(tc_compressor_src) dev.mode = LL_MODE_IEEE802154; STARTING(); - pico_stack_init(); TRYING("With unspecified source address, should: set SAC, clear SAM\n"); ret = compressor_src(unspec.addr, buf, iphc, &mac, NULL, &dev); @@ -675,7 +669,6 @@ START_TEST(tc_compressor_src) FAIL_UNLESS(0 == memcmp(buf, ll_nmac_64.addr + 8, 8), test, "Should've copied IID of source address inline"); TRYING("With context derived address\n"); - pico_stack_init(); ctx_insert(ip_ctx, 13, 64, 0, PICO_IPHC_CTX_COMPRESS, NULL); ret = compressor_src(ip_ctx.addr, buf, iphc, &mac, NULL, &dev); OUTPUT(); @@ -743,7 +736,6 @@ START_TEST(tc_decompressor_src) uint8_t buf[PICO_SIZE_IP6] = { 0 }; dev.mode = LL_MODE_IEEE802154; - pico_stack_init(); STARTING(); TRYING("With statelessly compressed address\n"); @@ -756,7 +748,6 @@ START_TEST(tc_decompressor_src) memset(buf, 0, PICO_SIZE_IP6); TRYING("With context\n"); - pico_stack_init(); ctx_insert(ip2, 13, 64, 0, PICO_IPHC_CTX_COMPRESS, NULL); ret = decompressor_src(buf, buf2, iphc2, &mac, NULL, &dev); OUTPUT(); @@ -831,7 +822,6 @@ START_TEST(tc_compressor_dst) dev.mode = LL_MODE_IEEE802154; STARTING(); - pico_stack_init(); TRYING("48-bit derivable mcast address\n"); ret = compressor_dst(mcast1.addr, buf, iphc, NULL, &mac, &dev); @@ -900,7 +890,6 @@ START_TEST(tc_decompressor_dst) dev.mode = LL_MODE_IEEE802154; STARTING(); - pico_stack_init(); TRYING("48-bit compressed address\n"); ret = decompressor_dst(buf,buf1,iphc1,NULL, &mac,&dev); @@ -972,7 +961,6 @@ START_TEST(tc_compressor_iphc) f->dst = dst; STARTING(); - pico_stack_init(); TRYING("To compress a IPv6 frame from a sample capture\n"); buf = compressor_iphc(f, &compressed_len, &nh); @@ -1011,7 +999,6 @@ START_TEST(tc_decompressor_iphc) f->dst = dst; STARTING(); - pico_stack_init(); TRYING("To decompress a 6LoWPAN frame from a sampel capture\n"); buf = decompressor_iphc(f, &compressed_len); @@ -1248,7 +1235,6 @@ START_TEST(tc_pico_iphc_compress) f->dst = dst; STARTING(); - pico_stack_init(); TRYING("Trying to compress an IPv6 frame from an example capture\n"); new = pico_iphc_compress(f); @@ -1282,7 +1268,6 @@ START_TEST(tc_pico_iphc_decompress) f->dst = dst; STARTING(); - pico_stack_init(); TRYING("Trying to decompress a 6LoWPAN frame from an example capture\n"); new = pico_iphc_decompress(f); @@ -1396,6 +1381,7 @@ static char *cpy_arg(char **dst, char *str) static void app_ping(char *arg) { + struct pico_stack *S = NULL; char *dest = NULL; char *next = NULL; char *abort = NULL; @@ -1439,7 +1425,7 @@ static void app_ping(char *arg) printf("Initial delay: %u seconds\n", initial_delay); initial_delay = PICO_TIME_MS() + (initial_delay * 1000); while (PICO_TIME_MS() < initial_delay) { - pico_stack_tick(); + pico_stack_tick(S); usleep(10000); } } @@ -1486,9 +1472,6 @@ START_TEST(tc_tx_rx) n_area0 = (uint8_t) atoi(area0); n_area1 = (uint8_t) atoi(area1); - /* Initialize picoTCP */ - pico_stack_init(); - pico_string_to_ipv6(pan_addr, myaddr.addr); pico_string_to_ipv6(pan_addr, pan.addr); pico_string_to_ipv6(pan_netmask, netmask.addr); @@ -1513,7 +1496,7 @@ START_TEST(tc_tx_rx) printf("%s: launching PicoTCP loop\n", __FUNCTION__); while(!rx) { - pico_stack_tick(); + pico_stack_tick(S); usleep(2000); } OUTPUT(); diff --git a/test/unit/modunit_pico_802154.c b/test/unit/modunit_pico_802154.c index e7d6c844..8be01809 100644 --- a/test/unit/modunit_pico_802154.c +++ b/test/unit/modunit_pico_802154.c @@ -436,7 +436,8 @@ START_TEST(tc_802154_process_out) dev.hostvars.lowpan_flags = PICO_6LP_FLAG_LOWPAN; STARTING(); - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); // TEST 1 TRYING("Trying with bare frame\n"); @@ -484,7 +485,8 @@ START_TEST(tc_802154_process_in) f->dst.pan = dst; STARTING(); - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); TRYING("Apply processing function on predefined buffer\n"); ret = pico_802154_process_in(f); diff --git a/test/unit/modunit_pico_dns_sd.c b/test/unit/modunit_pico_dns_sd.c index 75a6f132..cb2dc6e0 100644 --- a/test/unit/modunit_pico_dns_sd.c +++ b/test/unit/modunit_pico_dns_sd.c @@ -40,6 +40,7 @@ void callback( pico_mdns_rtree *tree, int dns_sd_init() { struct mock_device *mock = NULL; + struct pico_stack *S = NULL; struct pico_ip4 local = { .addr = long_be(0x0a280064) @@ -48,11 +49,13 @@ int dns_sd_init() .addr = long_be(0xffffff00) }; - mock = pico_mock_create(NULL); + pico_stack_init(&S); + + mock = pico_mock_create(S, NULL); if (!mock) return -1; - pico_ipv4_link_add(mock->dev, local, netmask); + pico_ipv4_link_add(S, mock->dev, local, netmask); /* Try to initialise the mDNS module right */ return pico_dns_sd_init("host.local", local, callback, NULL); @@ -248,14 +251,16 @@ START_TEST(tc_dns_sd_create_service_url) END_TEST START_TEST(tc_dns_sd_init) { - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); fail_unless(dns_sd_init() == 0, "dns_sd_init failed!\n"); } END_TEST START_TEST(tc_dns_sd_register_service) { - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); dns_sd_init(); } END_TEST diff --git a/test/unit/modunit_pico_hotplug_detection.c b/test/unit/modunit_pico_hotplug_detection.c index cbcc32be..7692b029 100644 --- a/test/unit/modunit_pico_hotplug_detection.c +++ b/test/unit/modunit_pico_hotplug_detection.c @@ -71,8 +71,12 @@ START_TEST(tc_pico_hotplug_reg_dereg) { /* create some devices */ struct pico_device *dev_a, *dev_b; - dev_a = pico_null_create("dummy1"); - dev_b = pico_null_create("dummy2"); + struct pico_stack *S; + + pico_stack_init(&S); + + dev_a = pico_null_create(S, "dummy1"); + dev_b = pico_null_create(S, "dummy2"); dev_a->link_state = &link_state_a; dev_b->link_state = &link_state_b; @@ -116,9 +120,12 @@ START_TEST(tc_pico_hotplug_callbacks) { /* create some devices */ struct pico_device *dev_a, *dev_b; + struct pico_stack *S; + + pico_stack_init(&S); - dev_a = pico_null_create("dummy1"); - dev_b = pico_null_create("dummy2"); + dev_a = pico_null_create(S, "dummy1"); + dev_b = pico_null_create(S, "dummy2"); dev_a->link_state = &link_state_a; dev_b->link_state = &link_state_b; diff --git a/test/unit/modunit_pico_igmp.c b/test/unit/modunit_pico_igmp.c index bfc7c221..92a3ebc9 100644 --- a/test/unit/modunit_pico_igmp.c +++ b/test/unit/modunit_pico_igmp.c @@ -105,9 +105,12 @@ START_TEST(tc_pico_igmp_timer_expired) END_TEST START_TEST(tc_pico_igmp_v2querier_expired) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct igmp_timer *t = PICO_ZALLOC(sizeof(struct igmp_timer)); struct pico_ip4 addr = {0}; - struct pico_device *dev = pico_null_create("dummy2"); + struct pico_device *dev = pico_null_create(S, "dummy2"); struct pico_frame *f = pico_frame_alloc(sizeof(struct pico_frame)); t->f = f; pico_string_to_ipv4("192.168.1.1", &(addr.addr)); @@ -115,7 +118,7 @@ START_TEST(tc_pico_igmp_v2querier_expired) /* No link */ pico_igmp_v2querier_expired(t); f->dev = dev; - pico_ipv4_link_add(dev, addr, addr); + pico_ipv4_link_add(S, dev, addr, addr); pico_igmp_v2querier_expired(t); } END_TEST @@ -127,8 +130,11 @@ START_TEST(tc_pico_igmp_delete_parameter) END_TEST START_TEST(tc_pico_igmp_process_in) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct mcast_parameters *p; - struct pico_device *dev = pico_null_create("dummy3"); + struct pico_device *dev = pico_null_create(S, "dummy3"); struct pico_ipv4_link *link; uint8_t i, j, _i, _j; int result; @@ -140,7 +146,7 @@ START_TEST(tc_pico_igmp_process_in) /* no link */ fail_if(pico_igmp_generate_report(p) != -1); - pico_ipv4_link_add(dev, p->mcast_link.ip4, p->mcast_link.ip4); + pico_ipv4_link_add(S, dev, p->mcast_link.ip4, p->mcast_link.ip4); link = pico_ipv4_link_get(&p->mcast_link.ip4); link->mcast_compatibility = PICO_IGMPV2; g.mcast_addr.ip4 = p->mcast_group.ip4; @@ -201,8 +207,11 @@ START_TEST(tc_pico_igmp_find_parameter) END_TEST START_TEST(tc_pico_igmp_compatibility_mode) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct pico_frame *f; - struct pico_device *dev = pico_null_create("dummy1"); + struct pico_device *dev = pico_null_create(S, "dummy1"); struct pico_ip4 addr; struct pico_ipv4_hdr *hdr; struct igmp_message *query; @@ -214,7 +223,7 @@ START_TEST(tc_pico_igmp_compatibility_mode) query = (struct igmp_message *) f->transport_hdr; /* No link */ fail_if(pico_igmp_compatibility_mode(f) != -1); - pico_ipv4_link_add(dev, addr, addr); + pico_ipv4_link_add(S, dev, addr, addr); f->dev = dev; /* Igmpv3 query */ hdr->len = short_be((uint16_t)(12 + ihl)); @@ -232,15 +241,18 @@ START_TEST(tc_pico_igmp_compatibility_mode) END_TEST START_TEST(tc_pico_igmp_analyse_packet) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct pico_frame *f; - struct pico_device *dev = pico_null_create("dummy0"); + struct pico_device *dev = pico_null_create(S, "dummy0"); struct pico_ip4 addr; struct igmp_message *igmp; f = pico_proto_ipv4.alloc(&pico_proto_ipv4, dev, sizeof(struct igmp_message)); pico_string_to_ipv4("192.168.1.1", &addr.addr); /* No link */ fail_if(pico_igmp_analyse_packet(f) != NULL); - pico_ipv4_link_add(dev, addr, addr); + pico_ipv4_link_add(S, dev, addr, addr); f->dev = dev; igmp = (struct igmp_message *) (f->transport_hdr); @@ -267,13 +279,18 @@ END_TEST START_TEST(tc_srst) { struct mcast_parameters p; - struct pico_device *dev = pico_null_create("dummy0"); + struct pico_device *dev; struct pico_ipv4_link *link; + struct pico_stack *S; + + pico_stack_init(&S); + + dev = pico_null_create(S, "dummy0"); pico_string_to_ipv4("192.168.1.1", &p.mcast_link.ip4.addr); /* no link */ fail_if(srst(&p) != -1); - pico_ipv4_link_add(dev, p.mcast_link.ip4, p.mcast_link.ip4); + pico_ipv4_link_add(S, dev, p.mcast_link.ip4, p.mcast_link.ip4); link = pico_ipv4_link_get(&p.mcast_link.ip4); /* Not supported protocol for this call */ link->mcast_compatibility = PICO_IGMPV2; diff --git a/test/unit/modunit_pico_ipv6_pmtu.c b/test/unit/modunit_pico_ipv6_pmtu.c index 596f5cf3..603cd5da 100644 --- a/test/unit/modunit_pico_ipv6_pmtu.c +++ b/test/unit/modunit_pico_ipv6_pmtu.c @@ -516,7 +516,8 @@ START_TEST(pico_ipv6_path_cache) struct pico_ipv6_path_id path_id = {{{ 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x50, 0x56, 0xff, 0xfe, 0x87, 0x06, 0xb6 }}}; - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); /* Timer should be allocated */ fail_if(gc_timer.id == 0); /* Adding new paths should be OK */ @@ -529,34 +530,34 @@ START_TEST(pico_ipv6_path_cache) path_id.dst.addr[10] = i; fail_if(pico_ipv6_pmtu_get(&path_id) != default_mtu + i); } - pico_stack_tick(); /* No changes: cleanup only in (default) 10 minutes */ + pico_stack_tick(S); /* No changes: cleanup only in (default) 10 minutes */ pico_ipv6_path_init(5 * 1000); - pico_stack_tick(); /* Cleanup in 5s: all paths valid */ + pico_stack_tick(S); /* Cleanup in 5s: all paths valid */ for (i = 0; i < 0xff; i++) { path_id.dst.addr[10] = i; fail_if(pico_ipv6_pmtu_get(&path_id) != default_mtu + i); } sleep(6); - pico_stack_tick(); /* Paths marked as old */ + pico_stack_tick(S); /* Paths marked as old */ fail_if(gc_timer.id == 0); path_id.dst.addr[10] = 0xfe; fail_if(pico_ipv6_path_update(&path_id, default_mtu) != PICO_PMTU_OK); sleep(6); - pico_stack_tick(); /* Updated path available other paths are deleted */ + pico_stack_tick(S); /* Updated path available other paths are deleted */ fail_if(pico_ipv6_pmtu_get(&path_id) != default_mtu); for (i = 0; i < 0xfe; i++) { path_id.dst.addr[10] = i; fail_if(pico_ipv6_pmtu_get(&path_id) != 0); } sleep(6); - pico_stack_tick(); /* Path cache expired */ + pico_stack_tick(S); /* Path cache expired */ fail_if(gc_timer.id == 0); for (i = 0; i < 0xff; i++) { path_id.dst.addr[10] = i; fail_if(pico_ipv6_pmtu_get(&path_id) != 0); } sleep(8); - pico_stack_tick(); /* Cleanup empty cache */ + pico_stack_tick(S); /* Cleanup empty cache */ fail_if(gc_timer.id == 0); } END_TEST diff --git a/test/unit/modunit_pico_mdns.c b/test/unit/modunit_pico_mdns.c index 45688b20..52370b67 100644 --- a/test/unit/modunit_pico_mdns.c +++ b/test/unit/modunit_pico_mdns.c @@ -32,6 +32,7 @@ void callback( pico_mdns_rtree *tree, int mdns_init() /* MARK: Initialise mDNS module */ { struct mock_device *mock = NULL; + struct pico_stack *S = NULL; struct pico_ip4 local = { .addr = long_be(0x0a280064) @@ -40,12 +41,14 @@ int mdns_init() /* MARK: Initialise mDNS module */ .addr = long_be(0xffffff00) }; - mock = pico_mock_create(NULL); + pico_stack_init(&S); + + mock = pico_mock_create(S, NULL); if (!mock) { return -1; } - pico_ipv4_link_add(mock->dev, local, netmask); + pico_ipv4_link_add(S, mock->dev, local, netmask); /* Try to initialise the mDNS module right */ return pico_mdns_init("host.local", local, callback, NULL); @@ -70,7 +73,8 @@ START_TEST(tc_mdns_init) /* MARK: mdns_init */ printf("*********************** starting %s * \n", __func__); - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); /* Try to initialise the mDNS module wrong */ ret = pico_mdns_init(NULL, local, callback, NULL); @@ -529,7 +533,8 @@ START_TEST(tc_mdns_cookie_apply_spt) /* MARK: mdns_cookie_apply_spt */ a.type = PICO_MDNS_PACKET_TYPE_PROBE; /* Need to initialise the stack to allow timer scheduling IMPORTANT! */ - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); /* Create 2 exactly the same cookies */ pico_tree_insert(&(a.antree), &record1); @@ -771,7 +776,8 @@ START_TEST(tc_mdns_cookie_resolve_conflict) /* MARK: mdns_cookie_resolve_conflic callback, NULL); /* Need to initialise the stack to allow timer scheduling IMPORTANT! */ - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); ret = mdns_init(); fail_unless(0 == ret, "mdns_init failed!\n"); @@ -855,7 +861,8 @@ START_TEST(tc_mdns_record_resolve_conflict) /* MARK: mdns_record_resolve_conflic PICO_MDNS_RECORD_UNIQUE); fail_if(!(record->record), "Record could not be created!\n"); /* Need to initialise the stack to allow timer scheduling IMPORTANT! */ - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); ret = mdns_init(); fail_unless(0 == ret, "mdns_init failed!\n"); @@ -1768,7 +1775,8 @@ START_TEST(tc_mdns_send_query_packet) /* MARK: send_query_packet */ pico_tree_insert(&(a.qtree), question2); cookie.count = 2; - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); mdns_init(); pico_mdns_send_query_packet(0, &cookie); @@ -1806,7 +1814,8 @@ START_TEST(tc_mdns_getrecord) /* MARK: getrecord */ */ /* Init */ - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); mdns_init(); #if PICO_MDNS_ALLOW_CACHING == 1 @@ -1847,7 +1856,8 @@ START_TEST(tc_mdns_send_announcement_packet) /* MARK: send_announcement_packet * PICO_MDNS_PACKET_TYPE_ANNOUNCEMENT, callback, NULL); - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); mdns_init(); pico_mdns_send_announcement_packet(0, cookie); @@ -1857,9 +1867,11 @@ START_TEST(tc_mdns_send_announcement_packet) /* MARK: send_announcement_packet * END_TEST START_TEST(tc_mdns_announce) /* MARK: annonce */ { + struct pico_stack *S = NULL; + printf("*********************** starting %s * \n", __func__); add_records(); - pico_stack_init(); + pico_stack_init(&S); mdns_init(); fail_unless(0 == pico_mdns_announce(callback, NULL), @@ -1871,6 +1883,8 @@ END_TEST START_TEST(tc_mdns_send_probe_packet) /* MARK: send_probe_packet */ { struct pico_mdns_cookie *cookie = NULL; + struct pico_stack *S = NULL; + PICO_DNS_QTREE_DECLARE(qtree); PICO_MDNS_RTREE_DECLARE(antree); PICO_MDNS_RTREE_DECLARE(artree); @@ -1881,7 +1895,7 @@ START_TEST(tc_mdns_send_probe_packet) /* MARK: send_probe_packet */ PICO_MDNS_PACKET_TYPE_PROBE, callback, NULL); - pico_stack_init(); + pico_stack_init(&S); mdns_init(); pico_mdns_send_probe_packet(0, cookie); @@ -1928,9 +1942,11 @@ START_TEST(tc_mdns_add_probe_question) /* MARK: add_probe_question */ END_TEST START_TEST(tc_mdns_probe) /* MARK: probe */ { + struct pico_stack *S = NULL; + printf("*********************** starting %s * \n", __func__); add_records(); - pico_stack_init(); + pico_stack_init(&S); mdns_init(); fail_unless(0 == pico_mdns_probe(callback, NULL), @@ -1943,6 +1959,7 @@ START_TEST(tc_mdns_claim) /* MARK: mdns_claim */ { PICO_MDNS_RTREE_DECLARE(rtree); struct pico_mdns_record *record = NULL, *record1 = NULL; + struct pico_stack *S = NULL; char url[] = "foo.local"; char url2[] = "bar.local"; struct pico_ip4 rdata = { @@ -1963,7 +1980,7 @@ START_TEST(tc_mdns_claim) /* MARK: mdns_claim */ pico_tree_insert(&rtree, record); pico_tree_insert(&rtree, record1); - pico_stack_init(); + pico_stack_init(&S); mdns_init(); ret = pico_mdns_claim(rtree, callback, NULL); @@ -1974,10 +1991,11 @@ START_TEST(tc_mdns_claim) /* MARK: mdns_claim */ END_TEST START_TEST(tc_mdns_tryclaim_hostname) /* MARK: tryclaim_hostname */ { + struct pico_stack *S = NULL; int ret = 0; printf("*********************** starting %s * \n", __func__); - pico_stack_init(); + pico_stack_init(&S); mdns_init(); ret = pico_mdns_tryclaim_hostname("test.local", NULL); @@ -1989,8 +2007,10 @@ END_TEST START_TEST(tc_mdns_get_hostname) /* MARK: get_hostname */ { const char *c_hostname; + struct pico_stack *S = NULL; + printf("*********************** starting %s * \n", __func__); - pico_stack_init(); + pico_stack_init(&S); mdns_init(); c_hostname = pico_mdns_get_hostname(); diff --git a/test/unit/modunit_pico_mld.c b/test/unit/modunit_pico_mld.c index 5b17c1a6..b78894b0 100644 --- a/test/unit/modunit_pico_mld.c +++ b/test/unit/modunit_pico_mld.c @@ -82,9 +82,12 @@ START_TEST(tc_pico_mld_check_hopbyhop) END_TEST START_TEST(tc_pico_mld_v1querier_expired) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct mld_timer *t = PICO_ZALLOC(sizeof(struct mld_timer)); struct pico_ip6 addr = {{0}}; - struct pico_device *dev = pico_null_create("dummy2"); + struct pico_device *dev = pico_null_create(S, "dummy2"); struct pico_frame *f = pico_frame_alloc(sizeof(struct pico_frame)); t->f = f; pico_string_to_ipv6("AAAA::109", addr.addr); @@ -98,8 +101,11 @@ START_TEST(tc_pico_mld_v1querier_expired) END_TEST START_TEST(tc_pico_mld_send_report) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct pico_frame *f; - struct pico_device *dev = pico_null_create("dummy1"); + struct pico_device *dev = pico_null_create(S, "dummy1"); struct pico_ip6 addr; struct pico_ipv6_link *link; struct mcast_parameters p; @@ -203,8 +209,11 @@ START_TEST(tc_pico_mld_send_done) END_TEST START_TEST(tc_mld_stsdifs) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct mcast_parameters *p; - struct pico_device *dev = pico_null_create("dummy3"); + struct pico_device *dev = pico_null_create(S, "dummy3"); struct pico_ipv6_link *link; struct mld_timer *t = PICO_ZALLOC(sizeof(struct mld_timer)); /* Building example frame */ @@ -241,8 +250,11 @@ END_TEST START_TEST(tc_mld_srst) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct mcast_parameters *p; - struct pico_device *dev = pico_null_create("dummy3"); + struct pico_device *dev = pico_null_create(S, "dummy3"); struct pico_ipv6_link *link; struct pico_mcast_group g; /* Building example frame */ @@ -273,8 +285,11 @@ START_TEST(tc_mld_srst) END_TEST START_TEST(tc_mld_mrsrrt) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct mcast_parameters *p; - struct pico_device *dev = pico_null_create("dummy3"); + struct pico_device *dev = pico_null_create(S, "dummy3"); struct pico_ipv6_link *link; /* Building example frame */ p = PICO_ZALLOC(sizeof(struct mcast_parameters)); @@ -294,8 +309,11 @@ START_TEST(tc_mld_mrsrrt) END_TEST START_TEST(tc_pico_mld_process_in) { + struct pico_stack *S = NULL; + pico_stack_init(&S); + struct mcast_parameters *p; - struct pico_device *dev = pico_null_create("dummy3"); + struct pico_device *dev = pico_null_create(S, "dummy3"); struct pico_ipv6_link *link; uint8_t i, j, _i, _j; int result = 0; @@ -389,8 +407,13 @@ END_TEST START_TEST(tc_pico_mld_compatibility_mode) { struct pico_frame *f; - struct pico_device *dev = pico_null_create("dummy1"); + struct pico_device *dev; struct pico_ip6 addr; + struct pico_stack *S; + + pico_stack_init(&S); + + dev = pico_null_create(S, "dummy1"); f = pico_proto_ipv6.alloc(&pico_proto_ipv6, NULL, sizeof(struct mldv2_report) + MLD_ROUTER_ALERT_LEN + sizeof(struct mldv2_group_record) + (0 * sizeof(struct pico_ip6))); pico_string_to_ipv6("AAAA::104", addr.addr); @@ -445,12 +468,18 @@ END_TEST START_TEST(tc_pico_mld_analyse_packet) { struct pico_frame *f; - struct pico_device *dev = pico_null_create("dummy0"); + struct pico_device *dev; struct pico_ip6 addr; struct pico_ip6 local; struct pico_ipv6_hdr *ip6; struct pico_ipv6_hbhoption *hbh; struct pico_icmp6_hdr *mld; + struct pico_stack *S; + + pico_stack_init(&S); + + dev = pico_null_create(S, "dummy0"); + f = pico_proto_ipv6.alloc(&pico_proto_ipv6, dev, sizeof(struct mld_message) + MLD_ROUTER_ALERT_LEN); pico_string_to_ipv6("AAAA::108", addr.addr); pico_string_to_ipv6("FE80::1", local.addr); diff --git a/test/unit/modunit_pico_stack.c b/test/unit/modunit_pico_stack.c index bc516386..200d0873 100644 --- a/test/unit/modunit_pico_stack.c +++ b/test/unit/modunit_pico_stack.c @@ -77,12 +77,13 @@ void fake_timer(pico_time __attribute__((unused)) now, void __attribute__((unuse START_TEST(tc_stack_generic) { + struct pico_stack *S = NULL; #ifdef PICO_FAULTY printf("Testing with faulty memory in pico_stack_init (11)\n"); pico_set_mm_failure(13); - fail_if(pico_stack_init() != -1); + fail_if(pico_stack_init(&S) != -1); #endif - pico_stack_init(); + pico_stack_init(&S); #ifdef PICO_FAULTY printf("Testing with faulty memory in pico_timer_add (1)\n"); pico_set_mm_failure(1); diff --git a/test/unit/unit_arp.c b/test/unit/unit_arp.c index 6ef609d3..759e4442 100644 --- a/test/unit/unit_arp.c +++ b/test/unit/unit_arp.c @@ -1,3 +1,5 @@ +extern const uint8_t PICO_ETHADDR_ALL[6]; + static struct pico_frame *init_frame(struct pico_device *dev) { struct pico_frame *f = pico_frame_alloc(PICO_SIZE_ETHHDR + PICO_SIZE_ARPHDR); @@ -10,15 +12,17 @@ static struct pico_frame *init_frame(struct pico_device *dev) START_TEST (arp_update_max_arp_reqs_test) { - pico_stack_init(); + struct pico_stack *S = NULL; + + pico_stack_init(&S); max_arp_reqs = 0; usleep((PICO_ARP_INTERVAL + 1) * 1000); - pico_stack_tick(); + pico_stack_tick(S); fail_unless(max_arp_reqs > 0); max_arp_reqs = PICO_ARP_MAX_RATE; usleep((PICO_ARP_INTERVAL + 1) * 1000); - pico_stack_tick(); + pico_stack_tick(S); fail_unless(max_arp_reqs == PICO_ARP_MAX_RATE); } END_TEST @@ -43,19 +47,21 @@ START_TEST (arp_lookup_test) struct pico_eth *eth = NULL; char ipstr[] = "192.168.1.1"; struct pico_arp entry; + struct pico_stack *S = NULL; + + pico_stack_init(&S); - eth = pico_arp_lookup(&ip); + eth = pico_arp_lookup(S, &ip); fail_unless(eth == NULL); pico_string_to_ipv4(ipstr, &ip.addr); entry.ipv4 = ip; - pico_stack_init(); fail_unless(pico_arp_add_entry(&entry) == 0); entry.arp_status = PICO_ARP_STATUS_STALE; - eth = pico_arp_lookup(&ip); + eth = pico_arp_lookup(S, &ip); fail_unless(eth == NULL); - pico_tree_delete(&arp_tree, &entry); + pico_tree_delete(&S->arp_tree, &entry); } END_TEST @@ -78,20 +84,23 @@ START_TEST(tc_pico_arp_queue) int i; struct pico_frame *f = pico_frame_alloc(sizeof(struct pico_ipv4_hdr)); struct pico_ipv4_hdr *h = (struct pico_ipv4_hdr *) f->buffer; + struct pico_stack *S = NULL; fail_if(!f); f->net_hdr = (uint8_t *)h; h->dst.addr = addr.addr; + pico_stack_init(&S); + for (i = 0; i < PICO_ND_MAX_FRAMES_QUEUED; i++) { fail_if(frames_queued[i] != NULL); } - pico_arp_unreachable(&addr); + pico_arp_unreachable(S, &addr); for (i = 0; i < PICO_ND_MAX_FRAMES_QUEUED; i++) { fail_if(frames_queued[i] != NULL); } pico_arp_postpone(f); fail_if(frames_queued[0]->buffer != f->buffer); - pico_arp_unreachable(&addr); + pico_arp_unreachable(S, &addr); PICO_FREE(f); } END_TEST @@ -104,6 +113,7 @@ START_TEST (arp_receive_test) struct pico_frame *f = NULL; struct pico_arp_hdr *ah = NULL; struct pico_eth_hdr *eh = NULL; + struct pico_stack *S = NULL; uint8_t macaddr1[6] = { 0, 0, 0, 0xa, 0xb, 0xf }; @@ -120,12 +130,12 @@ START_TEST (arp_receive_test) .addr = long_be(0x0A2800AB) }; - pico_stack_init(); + pico_stack_init(&S); /* Create mock device */ - mock = pico_mock_create(macaddr1); + mock = pico_mock_create(S, macaddr1); fail_if(!mock, "MOCK DEVICE creation failed"); - fail_if(pico_ipv4_link_add(mock->dev, ip1, netmask), "add link to mock device failed"); + fail_if(pico_ipv4_link_add(S, mock->dev, ip1, netmask), "add link to mock device failed"); /* Normal ARP request */ f = init_frame(mock->dev); @@ -173,7 +183,7 @@ START_TEST (arp_receive_test) ah = (struct pico_arp_hdr *) f->net_hdr; ah->s_mac[0] = 0x01; fail_unless(pico_arp_receive(f) == -1); - pico_ipv4_link_del(mock->dev, ip1); + pico_ipv4_link_del(S, mock->dev, ip1); } END_TEST @@ -183,6 +193,7 @@ START_TEST (arp_get_test) struct mock_device *mock; struct pico_ipv4_hdr *hdr = NULL; struct pico_eth *eth = NULL; + struct pico_stack *S = NULL; uint8_t macaddr[6] = { 0, 0, 0xa, 0xa, 0xb, 0xf }; @@ -193,9 +204,11 @@ START_TEST (arp_get_test) .addr = long_be(0x0A28000B) }; - mock = pico_mock_create(macaddr); + pico_stack_init(&S); + + mock = pico_mock_create(S, macaddr); fail_if(!mock, "MOCK DEVICE creation failed"); - fail_if(pico_ipv4_link_add(mock->dev, ip, netmask), "add link to mock device failed"); + fail_if(pico_ipv4_link_add(S, mock->dev, ip, netmask), "add link to mock device failed"); f = pico_frame_alloc(PICO_SIZE_ETHHDR + sizeof(struct pico_ipv4_hdr)); f->net_hdr = f->start + PICO_SIZE_ETHHDR; @@ -204,8 +217,8 @@ START_TEST (arp_get_test) hdr = (struct pico_ipv4_hdr *) f->net_hdr; hdr->dst.addr = ip.addr; - eth = pico_arp_get(f); + eth = pico_arp_get(S, f); fail_unless(eth == &mock->dev->eth->mac); - pico_ipv4_link_del(mock->dev, ip); + pico_ipv4_link_del(S, mock->dev, ip); } END_TEST diff --git a/test/unit/unit_dhcp.c b/test/unit/unit_dhcp.c index 08124b9f..3381ea67 100644 --- a/test/unit/unit_dhcp.c +++ b/test/unit/unit_dhcp.c @@ -81,6 +81,7 @@ START_TEST (test_dhcp_server_api) ************************************************************************/ struct mock_device *mock; + struct pico_stack *S; uint8_t macaddr1[6] = { 0xc1, 0, 0, 0xa, 0xb, 0xf }; @@ -104,11 +105,13 @@ START_TEST (test_dhcp_server_api) printf("*********************** starting %s * \n", __func__); + pico_stack_init(&S); + /* Create mock device */ - mock = pico_mock_create(macaddr1); + mock = pico_mock_create(S, macaddr1); fail_if(!mock, "MOCK DEVICE creation failed"); fail_if(pico_mock_network_read(mock, buf, BUFLEN), "data on network that shouldn't be there"); - fail_if(pico_ipv4_link_add(mock->dev, serverip, netmask), "add link to mock device failed"); + fail_if(pico_ipv4_link_add(S, mock->dev, serverip, netmask), "add link to mock device failed"); /* test 0 */ /* Clear error code */ @@ -169,6 +172,7 @@ START_TEST (test_dhcp) int network_read = 0; uint8_t *buf; uint8_t printbufactive = 0; + struct pico_stack *S = NULL; buf = PICO_ZALLOC(600); @@ -181,33 +185,33 @@ START_TEST (test_dhcp) printbuf(&(buf[0]), len, "DHCP-DISCOVER packet", printbufactive); /*Initiate test setup*/ - pico_stack_init(); + pico_stack_init(&S); /* Create mock device */ - mock = pico_mock_create(macaddr2); + mock = pico_mock_create(S, macaddr2); fail_if(!mock, "MOCK DEVICE creation failed"); fail_if(pico_mock_network_read(mock, buf, BUFLEN), "data on network that shouldn't be there"); - fail_if(pico_ipv4_link_add(mock->dev, serverip, netmask), "add link to mock device failed"); + fail_if(pico_ipv4_link_add(S, mock->dev, serverip, netmask), "add link to mock device failed"); s.server_ip = serverip; fail_if(pico_dhcp_server_initiate(&s), "DHCP_SERVER> server initiation failed"); - dn = pico_dhcp_server_find_negotiation(xid.addr); + dn = pico_dhcp_server_find_negotiation(S, xid.addr); fail_unless(dn == NULL, "DCHP SERVER -> negotiation data available befor discover msg recvd"); /* simulate reception of a DISCOVER packet */ sock.local_addr.ip4 = serverip; pico_dhcp_server_recv(&sock, buf, len); - tick_it(3); + tick_it(S, 3); /* check if negotiation data is stored */ - dn = pico_dhcp_server_find_negotiation(xid.addr); + dn = pico_dhcp_server_find_negotiation(S, xid.addr); fail_if(dn == NULL, "DCHP SERVER -> no negotiation stored after discover msg recvd"); /* check if new ip is in ARP cache */ - stored_ipv4 = pico_arp_reverse_lookup(&dn->hwaddr); + stored_ipv4 = pico_arp_reverse_lookup(S, &dn->hwaddr); fail_if(stored_ipv4 == NULL, "DCHP SERVER -> new address is not inserted in ARP"); fail_unless(stored_ipv4->addr == dn->ciaddr.addr, "DCHP SERVER -> new ip not stored in negotiation data"); @@ -226,7 +230,7 @@ START_TEST (test_dhcp) pico_dhcp_server_recv(&sock, &(buf[0x2a]), len - 0x2a); fail_unless(dn->state == PICO_DHCP_STATE_BOUND, "DCHP SERVER -> negotiation state not changed to BOUND"); - tick_it(3); + tick_it(S, 3); /* check if state is changed and reply is received */ do { @@ -271,6 +275,7 @@ START_TEST (test_dhcp_server_ipninarp) 0 }; uint8_t printbufactive = 0; + struct pico_stack *S = NULL; printf("*********************** starting %s * \n", __func__); @@ -281,18 +286,18 @@ START_TEST (test_dhcp_server_ipninarp) printbuf(&(buf[0]), len, "DHCP-DISCOVER packet", printbufactive); /*Initiate test setup*/ - pico_stack_init(); + pico_stack_init(&S); /* Create mock device */ - mock = pico_mock_create(macaddr1); + mock = pico_mock_create(S, macaddr1); fail_if(!mock, "MOCK DEVICE creation failed"); fail_if(pico_mock_network_read(mock, buf, BUFLEN), "data on network that shouldn't be there"); - fail_if(pico_ipv4_link_add(mock->dev, serverip, netmask), "add link to mock device failed"); + fail_if(pico_ipv4_link_add(S, mock->dev, serverip, netmask), "add link to mock device failed"); s.server_ip = serverip; fail_if(pico_dhcp_server_initiate(&s), "DHCP_SERVER> server initiation failed"); - dn = pico_dhcp_server_find_negotiation(xid.addr); + dn = pico_dhcp_server_find_negotiation(S, xid.addr); fail_unless(dn == NULL, "DCHP SERVER -> negotiation data available before discover msg recvd"); /* simulate reception of a DISCOVER packet */ @@ -300,16 +305,16 @@ START_TEST (test_dhcp_server_ipninarp) pico_dhcp_server_recv(&sock, buf, len); /* check if negotiation data is stored */ - dn = pico_dhcp_server_find_negotiation(xid.addr); + dn = pico_dhcp_server_find_negotiation(S, xid.addr); fail_if(dn == NULL, "DCHP SERVER -> no negotiation stored after discover msg recvd"); /* check if new ip is in ARP cache */ - stored_ipv4 = pico_arp_reverse_lookup(&dn->hwaddr); + stored_ipv4 = pico_arp_reverse_lookup(S, &dn->hwaddr); fail_if(stored_ipv4 == NULL, "DCHP SERVER -> new address is not inserted in ARP"); fail_unless(stored_ipv4->addr == dn->ciaddr.addr, "DCHP SERVER -> new ip not stored in negotiation data"); /* check if new ip is in ARP cache */ - fail_if(pico_arp_reverse_lookup(&dn->hwaddr) == NULL, "DCHP SERVER -> new address is not inserted in ARP"); + fail_if(pico_arp_reverse_lookup(S, &dn->hwaddr) == NULL, "DCHP SERVER -> new address is not inserted in ARP"); } END_TEST @@ -340,6 +345,7 @@ START_TEST (test_dhcp_server_ipinarp) struct pico_ip4 *stored_ipv4 = NULL; struct pico_dhcp_server_negotiation *dn = NULL; struct pico_eth *arp_resp = NULL; + struct pico_stack *S = NULL; unsigned char macaddr1[6] = { 0xc1, 0, 0, 0xa, 0xb, 0xf }; @@ -350,19 +356,20 @@ START_TEST (test_dhcp_server_ipinarp) printf("*********************** starting %s * \n", __func__); + /*Initiate test setup*/ + pico_stack_init(&S); + /*Insert custom values in buffer*/ fail_if(generate_dhcp_msg(buf, &len, DHCP_MSG_TYPE_DISCOVER), "DHCP_SERVER->failed to generate buffer"); memcpy(&(buf[28]), &(macaddr1[0]), sizeof(struct pico_ip4)); memcpy(&(buf[4]), &(xid.addr), sizeof(struct pico_ip4)); /* Create mock device */ - mock = pico_mock_create(macaddr1); + mock = pico_mock_create(S, macaddr1); fail_if(!mock, "MOCK DEVICE creation failed"); - fail_if(pico_ipv4_link_add(mock->dev, serverip, netmask), "add link to mock device failed"); + fail_if(pico_ipv4_link_add(S, mock->dev, serverip, netmask), "add link to mock device failed"); s.server_ip = serverip; - /*Initiate test setup*/ - pico_stack_init(); pico_arp_create_entry(&(macaddr1[0]), ipv4address, s.dev); fail_if(pico_dhcp_server_initiate(&s), "DHCP_SERVER> server initiation failed"); @@ -372,16 +379,16 @@ START_TEST (test_dhcp_server_ipinarp) pico_dhcp_server_recv(&sock, buf, len); /* check if negotiation data is stored */ - dn = pico_dhcp_server_find_negotiation(xid.addr); + dn = pico_dhcp_server_find_negotiation(S, xid.addr); fail_if(dn == NULL, "DCHP SERVER -> no negotiation stored after discover msg recvd"); /* check if new ip is in ARP cache */ - stored_ipv4 = pico_arp_reverse_lookup(&dn->hwaddr); + stored_ipv4 = pico_arp_reverse_lookup(S, &dn->hwaddr); fail_if(stored_ipv4 == NULL, "DCHP SERVER -> new address is not inserted in ARP"); fail_unless(stored_ipv4->addr == dn->ciaddr.addr, "DCHP SERVER -> new ip not stored in negotiation data"); /* check if new ip is in ARP cache */ - arp_resp = pico_arp_lookup(&ipv4address); + arp_resp = pico_arp_lookup(S, &ipv4address); fail_if(arp_resp == NULL, "DCHP SERVER -> address unavailable in arp cache"); } END_TEST @@ -423,11 +430,12 @@ START_TEST (test_dhcp_client) uint32_t len = 0; uint32_t xid = 0; struct pico_dhcp_client_cookie *cli = NULL; + struct pico_stack *S = NULL; - pico_stack_init(); + pico_stack_init(&S); /* Create mock device */ - mock = pico_mock_create(macaddr1); + mock = pico_mock_create(S, macaddr1); fail_if(!mock, "MOCK DEVICE creation failed"); fail_if(pico_mock_network_read(mock, buf, BUFLEN), "data on network that shouldn't be there"); @@ -440,7 +448,7 @@ START_TEST (test_dhcp_client) fail_if(pico_mock_network_read(mock, buf, BUFLEN), "data on network that shouldn't be there"); /* push discover msg on network */ - tick_it(3); + tick_it(S, 3); /* read discover message from network */ len = pico_mock_network_read(mock, buf, BUFLEN ); @@ -479,7 +487,7 @@ START_TEST (test_dhcp_client) fail_unless(address.addr == yiaddr.addr, "Client address incorrect => yiaddr or pico_dhcp_get_address incorrect"); gateway = pico_dhcp_get_gateway(cli); fail_unless(gateway.addr == router.addr, "Gateway incorrect! => routeroption or pico_dhcp_get_gateway incorrect"); - tick_it(3); + tick_it(S, 3); len = pico_mock_network_read(mock, buf, BUFLEN); fail_unless(len, "received msg on network of %d bytes", len); @@ -503,15 +511,18 @@ START_TEST (test_dhcp_client_api) /* Declaration test 1 */ uint32_t xid1 = 0; struct pico_dhcp_client_cookie *cli1 = NULL; + struct pico_stack *S = NULL; printf("*********************** starting %s * \n", __func__); + pico_stack_init(&S); + /* test 0 */ /* Clear error code */ pico_err = PICO_ERR_NOERR; /* Test 0 statements */ pico_dhcp_initiate_negotiation(NULL, NULL, &xid0); - cli0 = pico_dhcp_client_find_cookie(xid0); + cli0 = pico_dhcp_client_find_cookie(S, xid0); fail_unless(cli0 == NULL, "DHCP_CLIENT> initiate succeeded after pointer to dev == NULL"); fail_unless(pico_err == PICO_ERR_EINVAL, "DHCP_SERVER> initiate succeeded without PICO_ERR_EINVAL after wrong parameter"); @@ -520,7 +531,7 @@ START_TEST (test_dhcp_client_api) pico_err = PICO_ERR_NOERR; /* Test 1 statements */ pico_dhcp_initiate_negotiation(NULL, &callback_dhcpclient, &xid1); - cli1 = pico_dhcp_client_find_cookie(xid1); + cli1 = pico_dhcp_client_find_cookie(S, xid1); fail_unless(cli1 == NULL, "DHCP_CLIENT> initiate succeeded after pointer to dev == NULL"); fail_unless(pico_err == PICO_ERR_EINVAL, "DHCP_SERVER> initiate succeeded without PICO_ERR_EINVAL after wrong parameter"); @@ -531,11 +542,14 @@ START_TEST (test_dhcp_client_api) struct pico_dhcp_client_cookie *cli2 = NULL; struct pico_device *dev2; struct mock_device *mock2 = NULL; + struct pico_stack *S = NULL; + + pico_stack_init(&S); /* test 2 */ /* Create device */ - dev2 = pico_null_create("dummy"); - mock2 = pico_mock_create(NULL); + dev2 = pico_null_create(S, "dummy"); + mock2 = pico_mock_create(S, NULL); fail_if(mock2 == NULL, "No device created"); /* Clear error code */ pico_err = PICO_ERR_NOERR; diff --git a/test/unit/unit_dns.c b/test/unit/unit_dns.c index 7b3e218b..ba69817d 100644 --- a/test/unit/unit_dns.c +++ b/test/unit/unit_dns.c @@ -22,56 +22,57 @@ START_TEST (test_dns) char url[] = "www.google.com"; char ip[] = "8.8.4.4"; struct pico_ip4 ns; + struct pico_stack *stack = NULL; ns.addr = long_be(0x0a00280a); /* 10.40.0.10 */ - pico_stack_init(); + pico_stack_init(&stack); printf("START DNS TEST\n"); /* testing nameserver API */ - ret = pico_dns_client_nameserver(NULL, PICO_DNS_NS_ADD); + ret = pico_dns_client_nameserver(stack, NULL, PICO_DNS_NS_ADD); fail_if(ret == 0, "dns> dns_client_nameserver add error"); - ret = pico_dns_client_nameserver(NULL, PICO_DNS_NS_DEL); + ret = pico_dns_client_nameserver(stack, NULL, PICO_DNS_NS_DEL); fail_if(ret == 0, "dns> dns_client_nameserver del error"); - ret = pico_dns_client_nameserver(NULL, 99); + ret = pico_dns_client_nameserver(stack, NULL, 99); fail_if(ret == 0, "dns> dns_client_nameserver wrong code"); - ret = pico_dns_client_nameserver(NULL, 0xFF); + ret = pico_dns_client_nameserver(stack, NULL, 0xFF); fail_if(ret == 0, "dns> dns_client_nameserver wrong code"); - ret = pico_dns_client_nameserver(&ns, PICO_DNS_NS_DEL); /* delete non added ns */ + ret = pico_dns_client_nameserver(stack, &ns, PICO_DNS_NS_DEL); /* delete non added ns */ fail_if(ret == 0, "dns> dns_client_nameserver del error"); - ret = pico_dns_client_nameserver(&ns, 99); + ret = pico_dns_client_nameserver(stack, &ns, 99); fail_if(ret == 0, "dns> dns_client_nameserver wrong code"); - ret = pico_dns_client_nameserver(&ns, PICO_DNS_NS_ADD); /* add correct one */ + ret = pico_dns_client_nameserver(stack, &ns, PICO_DNS_NS_ADD); /* add correct one */ fail_if(ret < 0, "dns> dns_client_nameserver add error: %s", strerror(pico_err)); - ret = pico_dns_client_nameserver(&ns, 99); + ret = pico_dns_client_nameserver(stack, &ns, 99); fail_if(ret == 0, "dns> dns_client_nameserver wrong code"); - ret = pico_dns_client_nameserver(&ns, PICO_DNS_NS_DEL); + ret = pico_dns_client_nameserver(stack, &ns, PICO_DNS_NS_DEL); fail_if(ret < 0, "dns> dns_client_nameserver del error: %s", strerror(pico_err)); - ret = pico_dns_client_nameserver(&ns, PICO_DNS_NS_ADD); /* add correct one */ + ret = pico_dns_client_nameserver(stack, &ns, PICO_DNS_NS_ADD); /* add correct one */ fail_if(ret < 0, "dns> dns_client_nameserver add error: %s", strerror(pico_err)); - ret = pico_dns_client_nameserver(&ns, PICO_DNS_NS_ADD); /* add correct one again */ + ret = pico_dns_client_nameserver(stack, &ns, PICO_DNS_NS_ADD); /* add correct one again */ fail_if(ret < 0, "dns> dns_client_nameserver add double failed"); /* testing getaddr API */ /* not testable since we do not have a stub for the pico_socket_send */ - /* ret = pico_dns_client_getaddr(url, cb_dns, NULL); / * ask correct one * / */ + /* ret = pico_dns_client_getaddr(stack, url, cb_dns, NULL); / * ask correct one * / */ /* fail_if(ret < 0, "dns> dns_client_getaddr: %s",strerror(pico_err)); */ - ret = pico_dns_client_getaddr(NULL, cb_dns, NULL); + ret = pico_dns_client_getaddr(stack, NULL, cb_dns, NULL); fail_if(ret == 0, "dns> dns_client_getaddr: no url"); - ret = pico_dns_client_getaddr(url, NULL, NULL); + ret = pico_dns_client_getaddr(stack, url, NULL, NULL); fail_if(ret == 0, "dns> dns_client_getaddr: no cb"); /* testing getname API */ @@ -79,10 +80,10 @@ START_TEST (test_dns) /* ret = pico_dns_client_getname(ip, cb_dns, NULL); / * ask correct one * / */ /* fail_if(ret < 0, "dns> dns_client_getname: %s",strerror(pico_err)); */ - ret = pico_dns_client_getname(NULL, cb_dns, NULL); + ret = pico_dns_client_getname(stack, NULL, cb_dns, NULL); fail_if(ret == 0, "dns> dns_client_getname: no ip"); - ret = pico_dns_client_getname(ip, NULL, NULL); + ret = pico_dns_client_getname(stack, ip, NULL, NULL); fail_if(ret == 0, "dns> dns_client_getname: no cb"); } END_TEST diff --git a/test/unit/unit_icmp4.c b/test/unit/unit_icmp4.c index 086a41c1..79060bde 100644 --- a/test/unit/unit_icmp4.c +++ b/test/unit/unit_icmp4.c @@ -35,6 +35,7 @@ START_TEST (test_icmp4_ping) 0 }; struct mock_device *mock = NULL; + struct pico_stack *S = NULL; char local_address[] = { "192.168.1.102" }; @@ -49,6 +50,7 @@ START_TEST (test_icmp4_ping) uint8_t buffer[bufferlen]; int len; uint8_t temp_buf[4]; + printf("*********************** starting %s * \n", __func__); pico_string_to_ipv4(local_address, &(local.addr)); @@ -57,24 +59,24 @@ START_TEST (test_icmp4_ping) pico_string_to_ipv4(remote_address, &(remote.addr)); pico_string_to_ipv4("255.255.255.0", &(netmask.addr)); - pico_stack_init(); + pico_stack_init(&S); - mock = pico_mock_create(NULL); + mock = pico_mock_create(S, NULL); fail_if(mock == NULL, "No device created"); - pico_ipv4_link_add(mock->dev, local, netmask); + pico_ipv4_link_add(S, mock->dev, local, netmask); - fail_if(pico_icmp4_ping(local_address, NUM_PING, interval, timeout, size, cb_ping) < 0); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + fail_if(pico_icmp4_ping(S, local_address, NUM_PING, interval, timeout, size, cb_ping) < 0); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); fail_if(ping_test_var != 1); - pico_icmp4_ping(remote_address, NUM_PING, interval, timeout, size, cb_ping); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_icmp4_ping(S, remote_address, NUM_PING, interval, timeout, size, cb_ping); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); /* get the packet from the mock_device */ memset(buffer, 0, (size_t)bufferlen); @@ -95,17 +97,17 @@ START_TEST (test_icmp4_ping) /* using the mock-device because otherwise I have to put everything in a pico_frame correctly myself. */ pico_mock_network_write(mock, buffer, len); /* check if it is received */ - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); fail_unless(ping_test_var == 2); /* repeat but make it an invalid reply... */ - pico_icmp4_ping(remote_address, NUM_PING, interval, timeout, size, cb_ping); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_icmp4_ping(S, remote_address, NUM_PING, interval, timeout, size, cb_ping); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); /* get the packet from the mock_device */ memset(buffer, 0, (size_t)bufferlen); @@ -126,9 +128,9 @@ START_TEST (test_icmp4_ping) /* using the mock-device because otherwise I have to put everything in a pico_frame correctly myself. */ pico_mock_network_write(mock, buffer, len); /* check if it is received */ - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); fail_unless(ping_test_var == 2); } END_TEST @@ -168,27 +170,29 @@ START_TEST (test_icmp4_incoming_ping) struct pico_ip4 netmask = { .addr = long_be(0xffffff00) }; - struct mock_device*mock; + struct mock_device *mock; struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) buffer; + struct pico_stack *S = NULL; + printf("*********************** starting %s * \n", __func__); - pico_stack_init(); + pico_stack_init(&S); - mock = pico_mock_create(NULL); + mock = pico_mock_create(S, NULL); fail_if(mock == NULL, "No device created"); - pico_ipv4_link_add(mock->dev, local, netmask); + pico_ipv4_link_add(S, mock->dev, local, netmask); hdr->crc = 0; hdr->crc = short_be(pico_checksum(hdr, PICO_SIZE_IP4HDR)); pico_mock_network_write(mock, buffer, bufferlen); /* check if it is received */ - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); len = pico_mock_network_read(mock, buffer2, buffer2len); @@ -236,24 +240,26 @@ START_TEST (test_icmp4_unreachable_send) 0x0a, 0x28, 0x00, 0x04 }; - struct pico_frame*f = PICO_ZALLOC(sizeof(struct pico_frame)); + struct pico_frame *f = PICO_ZALLOC(sizeof(struct pico_frame)); + struct pico_stack *S = NULL; + printf("*********************** starting %s * \n", __func__); f->net_hdr = buffer; f->buffer = buffer; - pico_stack_init(); + pico_stack_init(&S); - mock = pico_mock_create(NULL); + mock = pico_mock_create(S, NULL); fail_if(mock == NULL, "No device created"); - pico_ipv4_link_add(mock->dev, local, netmask); + pico_ipv4_link_add(S, mock->dev, local, netmask); - fail_if(pico_icmp4_dest_unreachable(f)); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + fail_if(pico_icmp4_dest_unreachable(S, f)); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); len = pico_mock_network_read(mock, buffer2, bufferlen); @@ -264,10 +270,10 @@ START_TEST (test_icmp4_unreachable_send) fail_unless(pico_checksum(buffer2 + 20, (uint32_t)(len - 20)) == 0); - fail_if(pico_icmp4_port_unreachable(f)); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + fail_if(pico_icmp4_port_unreachable(S, f)); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); len = pico_mock_network_read(mock, buffer2, bufferlen); @@ -278,10 +284,10 @@ START_TEST (test_icmp4_unreachable_send) fail_unless(pico_checksum(buffer2 + 20, (uint32_t)(len - 20)) == 0); - fail_if(pico_icmp4_proto_unreachable(f)); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + fail_if(pico_icmp4_proto_unreachable(S, f)); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); len = pico_mock_network_read(mock, buffer2, bufferlen); @@ -292,10 +298,10 @@ START_TEST (test_icmp4_unreachable_send) fail_unless(pico_checksum(buffer2 + 20, (uint32_t)(len - 20)) == 0); - fail_if(pico_icmp4_ttl_expired(f)); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + fail_if(pico_icmp4_ttl_expired(S, f)); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); len = pico_mock_network_read(mock, buffer2, bufferlen); @@ -308,10 +314,10 @@ START_TEST (test_icmp4_unreachable_send) f->net_hdr = buffer3; f->buffer = buffer3; - fail_if(pico_icmp4_proto_unreachable(f)); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + fail_if(pico_icmp4_proto_unreachable(S, f)); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); len = pico_mock_network_read(mock, buffer2, bufferlen); @@ -344,8 +350,8 @@ START_TEST (test_icmp4_unreachable_recv) struct pico_ip4 netmask = { .addr = long_be(0xffffff00) }; - struct mock_device*mock; - struct pico_socket*sock; + struct mock_device *mock; + struct pico_socket *sock; uint16_t port = short_be(7777); /* put a host unreachable in the queue, run a few stack ticks */ @@ -367,35 +373,37 @@ START_TEST (test_icmp4_unreachable_recv) 0x00, 0x00, 0x00, 0x00, }; struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) buffer; + struct pico_stack *S = NULL; printf("*********************** starting %s * \n", __func__); - pico_stack_init(); - mock = pico_mock_create(NULL); + pico_stack_init(&S); + + mock = pico_mock_create(S, NULL); fail_if(mock == NULL, "No device created"); - pico_ipv4_link_add(mock->dev, local, netmask); + pico_ipv4_link_add(S, mock->dev, local, netmask); /* open a socket */ - sock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &icmp4_unreach_socket_cb); + sock = pico_socket_open(S, PICO_PROTO_IPV4, PICO_PROTO_TCP, &icmp4_unreach_socket_cb); fail_if(sock == NULL); fail_if(pico_socket_bind(sock, &local, &port)); pico_socket_connect(sock, &remote, port); pico_socket_write(sock, "fooo", 4); /* see if my callback was called with the proper code */ - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); /* filling in the IP header and first 8 bytes */ hdr->crc = 0; hdr->crc = short_be(pico_checksum(hdr, PICO_SIZE_IP4HDR)); printf("read %d bytes\n", pico_mock_network_read(mock, buffer + 28, 28)); printf("wrote %d bytes\n", pico_mock_network_write(mock, buffer, 56)); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); fail_unless(icmp4_socket_unreach_status == 1); } END_TEST diff --git a/test/unit/unit_ipv4.c b/test/unit/unit_ipv4.c index 48e3b879..0f4bc1dc 100644 --- a/test/unit/unit_ipv4.c +++ b/test/unit/unit_ipv4.c @@ -14,8 +14,9 @@ START_TEST (test_ipv4) struct pico_frame *f_NULL = NULL; struct pico_ip4 *dst_NULL = NULL; + struct pico_stack *S = NULL; - pico_stack_init(); + pico_stack_init(&S); nm16.addr = long_be(0xFFFF0000); nm32.addr = long_be(0xFFFFFFFF); @@ -23,32 +24,32 @@ START_TEST (test_ipv4) /*link_add*/ for (i = 0; i < IP_TST_SIZ; i++) { snprintf(devname, 8, "nul%d", i); - dev[i] = pico_null_create(devname); + dev[i] = pico_null_create(S, devname); a[i].addr = long_be(0x0a000001u + (i << 16)); d[i].addr = long_be(0x0a000002u + (i << 16)); - fail_if(pico_ipv4_link_add(dev[i], a[i], nm16) != 0, "Error adding link"); + fail_if(pico_ipv4_link_add(S, dev[i], a[i], nm16) != 0, "Error adding link"); } /*link_find + link_get + route_add*/ for (i = 0; i < IP_TST_SIZ; i++) { gw[i].addr = long_be(0x0a0000f0u + (i << 16)); r[i].addr = long_be(0x0c00001u + (i << 16)); - fail_unless(pico_ipv4_link_find(&a[i]) == dev[i], "Error finding link"); - l[i] = pico_ipv4_link_get(&a[i]); + fail_unless(pico_ipv4_link_find(S, &a[i]) == dev[i], "Error finding link"); + l[i] = pico_ipv4_link_get(S, &a[i]); fail_if(l[i] == NULL, "Error getting link"); - fail_if(pico_ipv4_route_add(r[i], nm32, gw[i], 1, l[i]) != 0, "Error adding route"); - fail_if(pico_ipv4_route_add(d[i], nm32, gw[i], 1, l[i]) != 0, "Error adding route"); + fail_if(pico_ipv4_route_add(S, r[i], nm32, gw[i], 1, l[i]) != 0, "Error adding route"); + fail_if(pico_ipv4_route_add(S, d[i], nm32, gw[i], 1, l[i]) != 0, "Error adding route"); } /*get_gateway + source_find*/ for (i = 0; i < IP_TST_SIZ; i++) { - ret = pico_ipv4_route_get_gateway(&r[i]); + ret = pico_ipv4_route_get_gateway(S, &r[i]); fail_if(ret.addr != gw[i].addr, "Error get gateway: returned wrong route"); - source[i] = pico_ipv4_source_find(&d[i]); + source[i] = pico_ipv4_source_find(S, &d[i]); fail_if(source[i]->addr != a[i].addr, "Error find source: returned wrong route"); } /*route_del + link_del*/ for (i = 0; i < IP_TST_SIZ; i++) { - fail_if(pico_ipv4_route_del(r[i], nm32, 1) != 0, "Error deleting route"); - fail_if(pico_ipv4_link_del(dev[i], a[i]) != 0, "Error deleting link"); + fail_if(pico_ipv4_route_del(S, r[i], nm32, 1) != 0, "Error deleting route"); + fail_if(pico_ipv4_link_del(S, dev[i], a[i]) != 0, "Error deleting link"); } /*string_to_ipv4 + ipv4_to_string*/ pico_string_to_ipv4(ipstr, &(ipaddr.addr)); @@ -66,10 +67,10 @@ START_TEST (test_ipv4) fail_if((pico_ipv4_is_unicast(long_be(0xe0000001))) != 0, "Error checking unicast"); /*rebound*/ - fail_if(pico_ipv4_rebound(f_NULL) != -1, "Error rebound frame"); + fail_if(pico_ipv4_rebound(S, f_NULL) != -1, "Error rebound frame"); /*frame_push*/ - fail_if(pico_ipv4_frame_push(f_NULL, dst_NULL, PICO_PROTO_TCP) != -1, "Error push frame"); + fail_if(pico_ipv4_frame_push(S, f_NULL, dst_NULL, PICO_PROTO_TCP) != -1, "Error push frame"); } END_TEST @@ -78,11 +79,17 @@ START_TEST (test_nat_enable_disable) struct pico_ipv4_link link = { .address = {.addr = long_be(0x0a320001)} }; /* 10.50.0.1 */ - struct pico_frame *f = pico_ipv4_alloc(&pico_proto_ipv4, NULL, PICO_UDPHDR_SIZE); - struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr; - struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr; + struct pico_frame *f = NULL; + struct pico_ipv4_hdr *net = NULL; + struct pico_udp_hdr *udp = NULL; + struct pico_stack *S = NULL; const char *raw_data = "ello"; + pico_stack_init(&S); + + f = pico_ipv4_alloc(S, &pico_proto_ipv4, NULL, PICO_UDPHDR_SIZE); + + net = (struct pico_ipv4_hdr *)f->net_hdr; net->vhl = 0x45; /* version = 4, hdr len = 5 (32-bit words) */ net->tos = 0; net->len = short_be(32); /* hdr + data (bytes) */ @@ -94,6 +101,7 @@ START_TEST (test_nat_enable_disable) net->src.addr = long_be(0x0a280008); /* 10.40.0.8 */ net->dst.addr = long_be(0x0a320001); /* 10.50.0.1 */ + udp = (struct pico_udp_hdr *)f->transport_hdr; udp->trans.sport = short_be(5555); udp->trans.dport = short_be(6667); udp->len = 12; @@ -103,14 +111,13 @@ START_TEST (test_nat_enable_disable) memcpy(f->payload, raw_data, 4); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> NAT ENABLE/DISABLE TEST\n"); - pico_stack_init(); fail_if(pico_ipv4_nat_enable(&link)); fail_unless(nat_link->address.addr == link.address.addr); fail_unless(pico_ipv4_nat_is_enabled(&link.address)); - fail_if(pico_ipv4_nat_outbound(f, &net->dst)); - pico_ipv4_nat_table_cleanup(pico_tick, NULL); + fail_if(pico_ipv4_nat_outbound(S, f, &net->dst)); + pico_ipv4_nat_table_cleanup(S->pico_tick, NULL); fail_if(pico_ipv4_nat_disable()); fail_if(pico_ipv4_nat_is_enabled(&link.address)); @@ -122,9 +129,10 @@ START_TEST (test_nat_translation) struct pico_ipv4_link link = { .address = {.addr = long_be(0x0a320001)} }; /* 10.50.0.1 */ - struct pico_frame *f = pico_ipv4_alloc(&pico_proto_ipv4, NULL, PICO_UDPHDR_SIZE); - struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr; - struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr; + struct pico_frame *f = NULL; + struct pico_ipv4_hdr *net = NULL; + struct pico_udp_hdr *udp = NULL; + struct pico_stack *S = NULL; struct pico_ip4 src_ori = { .addr = long_be(0x0a280008) }; /* 10.40.0.8 */ @@ -139,6 +147,11 @@ START_TEST (test_nat_translation) uint16_t dport_ori = short_be(6667); uint16_t nat_port = 0; + pico_stack_init(&S); + + f = pico_ipv4_alloc(S, &pico_proto_ipv4, NULL, PICO_UDPHDR_SIZE); + + net = (struct pico_ipv4_hdr *)f->net_hdr; net->vhl = 0x45; /* version = 4, hdr len = 5 (32-bit words) */ net->tos = 0; net->len = short_be(32); /* hdr + data (bytes) */ @@ -150,6 +163,7 @@ START_TEST (test_nat_translation) net->src = src_ori; net->dst = dst_ori; + udp = (struct pico_udp_hdr *)f->transport_hdr; udp->trans.sport = sport_ori; udp->trans.dport = dport_ori; udp->len = 12; @@ -159,18 +173,18 @@ START_TEST (test_nat_translation) memcpy(f->payload, raw_data, 4); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> NAT TRANSLATION TEST\n"); - pico_stack_init(); + fail_if(pico_ipv4_nat_enable(&link)); /* perform outbound translation, check if source IP got translated */ - fail_if(pico_ipv4_nat_outbound(f, &nat_link->address)); + fail_if(pico_ipv4_nat_outbound(S, f, &nat_link->address)); fail_if(net->src.addr != link.address.addr, "source address not translated"); /* perform outbound translation of same packet, check if source IP and PORT got translated the same as previous packet */ nat_port = udp->trans.sport; net->src = src_ori; /* restore original src */ udp->trans.sport = sport_ori; /* restore original sport */ - fail_if(pico_ipv4_nat_outbound(f, &nat_link->address)); + fail_if(pico_ipv4_nat_outbound(S, f, &nat_link->address)); fail_if(net->src.addr != link.address.addr, "source address not translated"); fail_if(udp->trans.sport != nat_port, "frames with the same source IP, source PORT and PROTO did not get translated the same"); @@ -178,7 +192,7 @@ START_TEST (test_nat_translation) nat_port = udp->trans.sport; net->src = src_ori; /* restore original src */ udp->trans.sport = short_be(5556); /* change sport */ - fail_if(pico_ipv4_nat_outbound(f, &nat_link->address)); + fail_if(pico_ipv4_nat_outbound(S, f, &nat_link->address)); fail_if(net->src.addr != link.address.addr, "source address not translated"); fail_if(udp->trans.sport == short_be(sport_ori), "two frames with different sport get translated the same"); @@ -188,10 +202,10 @@ START_TEST (test_nat_translation) net->dst = nat; udp->trans.sport = sport_ori; udp->trans.dport = nat_port; - fail_if(pico_ipv4_nat_inbound(f, &nat_link->address)); + fail_if(pico_ipv4_nat_inbound(S, f, &nat_link->address)); fail_if(net->dst.addr != src_ori.addr, "destination address not translated correctly"); fail_if(udp->trans.dport != short_be(5556), "ports not translated correctly"); - pico_ipv4_nat_table_cleanup(pico_tick, NULL); + pico_ipv4_nat_table_cleanup(S->pico_tick, NULL); fail_if(pico_ipv4_nat_disable()); } @@ -202,9 +216,10 @@ START_TEST (test_nat_port_forwarding) struct pico_ipv4_link link = { .address = {.addr = long_be(0x0a320001)} }; /* 10.50.0.1 */ - struct pico_frame *f = pico_ipv4_alloc(&pico_proto_ipv4, NULL, PICO_UDPHDR_SIZE); - struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr; - struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr; + struct pico_frame *f = NULL; + struct pico_ipv4_hdr *net = NULL; + struct pico_udp_hdr *udp = NULL; + struct pico_stack *S = NULL; struct pico_ip4 src_addr = { .addr = long_be(0x0a280008) }; /* 10.40.0.8 */ @@ -219,6 +234,11 @@ START_TEST (test_nat_port_forwarding) uint16_t fport_pub = short_be(80); uint16_t fport_priv = short_be(8080); + pico_stack_init(&S); + + f = pico_ipv4_alloc(S, &pico_proto_ipv4, NULL, PICO_UDPHDR_SIZE); + + net = (struct pico_ipv4_hdr *)f->net_hdr; net->vhl = 0x45; /* version = 4, hdr len = 5 (32-bit words) */ net->tos = 0; net->len = short_be(32); /* hdr + data (bytes) */ @@ -230,6 +250,7 @@ START_TEST (test_nat_port_forwarding) net->src = dst_addr; net->dst = nat_addr; + udp = (struct pico_udp_hdr *)f->transport_hdr; udp->trans.sport = sport_ori; udp->trans.dport = fport_pub; udp->len = 12; @@ -239,23 +260,24 @@ START_TEST (test_nat_port_forwarding) memcpy(f->payload, raw_data, 4); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> NAT PORT FORWARD TEST\n"); - pico_stack_init(); + fail_if(pico_ipv4_nat_enable(&link)); - fail_if(pico_ipv4_port_forward(nat_addr, fport_pub, src_addr, fport_priv, 17, PICO_NAT_PORT_FORWARD_ADD)); + fail_if(pico_ipv4_port_forward(S, nat_addr, fport_pub, src_addr, fport_priv, 17, PICO_NAT_PORT_FORWARD_ADD)); - fail_if(pico_ipv4_nat_inbound(f, &nat_link->address)); + fail_if(pico_ipv4_nat_inbound(S, f, &nat_link->address)); fail_if(net->dst.addr != src_addr.addr, "destination address not translated correctly"); fail_if(udp->trans.dport != fport_priv, "destination port not translated correctly"); - fail_if(pico_ipv4_port_forward(nat_addr, fport_pub, src_addr, fport_priv, 17, PICO_NAT_PORT_FORWARD_DEL)); - pico_ipv4_nat_table_cleanup(pico_tick, NULL); + fail_if(pico_ipv4_port_forward(S, nat_addr, fport_pub, src_addr, fport_priv, 17, PICO_NAT_PORT_FORWARD_DEL)); + pico_ipv4_nat_table_cleanup(S->pico_tick, NULL); } END_TEST START_TEST (test_ipfilter) { struct pico_device *dev = NULL; + struct pico_stack *S = NULL; uint8_t proto = 0, tos = 0; uint16_t sport = 0, dport = 0; int8_t priority = 0; @@ -290,6 +312,8 @@ START_TEST (test_ipfilter) struct pico_frame *f; + pico_stack_init(&S); + printf("IP Filter> Adding a new filter...\n"); filter_id1 = pico_ipv4_filter_add(dev, proto, &src_addr, &saddr_netmask, &dst_addr, &daddr_netmask, sport, dport, priority, tos, action); fail_if(filter_id1 <= 0, "Error adding filter\n"); @@ -300,11 +324,11 @@ START_TEST (test_ipfilter) fail_if(ret > 0, "Error adding filter\n"); printf("IP Filter> Deleting added filter...\n"); - ret = pico_ipv4_filter_del(filter_id1); + ret = pico_ipv4_filter_del(S, filter_id1); fail_if(ret != 0, "Error deleting the filter\n"); printf("IP Filter> Trying to delete the same filter\n"); - ret = pico_ipv4_filter_del(filter_id1); + ret = pico_ipv4_filter_del(S, filter_id1); fail_if(ret != -1, "Deleting non existing filter failed\n"); f = (struct pico_frame *)PICO_ZALLOC(200); @@ -328,7 +352,7 @@ START_TEST (test_ipfilter) fail_if(ret != 1, "Frame wasn't filtered\n"); printf("IP Filter> Deleting added filter...\n"); - ret = pico_ipv4_filter_del(filter_id1); + ret = pico_ipv4_filter_del(S, filter_id1); fail_if(ret != 0, "Error deleting the filter\n"); printf("IP Filter> Adding masked filter...\n"); @@ -349,7 +373,7 @@ START_TEST (test_ipfilter) fail_if(ret != 1, "Mask filter failed to filter\n"); printf("IP Filter> Deleting added filter...\n"); - ret = pico_ipv4_filter_del(filter_id1); + ret = pico_ipv4_filter_del(S, filter_id1); fail_if(ret != 0, "Error deleting the filter\n"); printf("IP Filter> Adding bad filter..\n"); @@ -369,13 +393,15 @@ START_TEST (test_ipfilter) fail_if(ret != 0, "Filter shouldn't have filtered this frame\n"); printf("IP Filter> Deleting added filter...\n"); - ret = pico_ipv4_filter_del(filter_id1); + ret = pico_ipv4_filter_del(S, filter_id1); fail_if(ret != 0, "Error deleting the filter\n"); } END_TEST #ifdef PICO_SUPPORT_MCAST +static PICO_TREE_DECLARE(MCASTFilter, mcast_filter_cmp); + START_TEST (test_igmp_sockopts) { int i = 0, j = 0, k = 0, ret = 0; @@ -399,6 +425,7 @@ START_TEST (test_igmp_sockopts) struct pico_ip_mreq _mreq = {0}, mreq[16] = {0}; struct pico_ip_mreq_source mreq_source[128] = {0}; struct pico_tree_node *index = NULL; + struct pico_stack *S = NULL; int ttl = 64; int getttl = 0; @@ -408,7 +435,7 @@ START_TEST (test_igmp_sockopts) 0 }; - pico_stack_init(); + pico_stack_init(&S); printf("START IGMP SOCKOPTS TEST\n"); @@ -452,19 +479,19 @@ START_TEST (test_igmp_sockopts) mreq_source[(i * 8) + j].mcast_source_addr = inaddr_source[j]; } } - dev = pico_null_create("dummy0"); + dev = pico_null_create(S, "dummy0"); netmask.ip4.addr = long_be(0xFFFF0000); - ret = pico_ipv4_link_add(dev, inaddr_link[0].ip4, netmask.ip4); + ret = pico_ipv4_link_add(S, dev, inaddr_link[0].ip4, netmask.ip4); fail_if(ret < 0, "link add failed"); - dev = pico_null_create("dummy1"); + dev = pico_null_create(S, "dummy1"); netmask.ip4.addr = long_be(0xFFFF0000); - ret = pico_ipv4_link_add(dev, inaddr_link[1].ip4, netmask.ip4); + ret = pico_ipv4_link_add(S, dev, inaddr_link[1].ip4, netmask.ip4); fail_if(ret < 0, "link add failed"); - s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL); + s = pico_socket_open(S, PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL); fail_if(s == NULL, "UDP socket open failed"); - s1 = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL); + s1 = pico_socket_open(S, PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL); fail_if(s1 == NULL, "UDP socket open failed"); /* argument validation tests */ @@ -826,8 +853,9 @@ START_TEST (test_slaacv4) uint8_t macaddr1[6] = { 0xc3, 0, 0, 0xa, 0xc, 0xf }; + struct pico_stack *S = NULL; - + pico_stack_init(&S); /* verify min boundary*/ tmp = SLAACV4_CREATE_IPV4(0); @@ -841,11 +869,11 @@ START_TEST (test_slaacv4) fail_if(long_be(tmp) > (long_be(SLAACV4_NETWORK) | 0x0000FEFF)); /* verify case where dev->eth is NULL */ - dev = pico_null_create("dummy"); + dev = pico_null_create(S, "dummy"); tmp = pico_slaacv4_getip(dev, 0); fail_if(long_be(tmp) != (long_be(SLAACV4_NETWORK) | SLAACV4_MINRANGE)); /* verify nominal case; two runs of slaacv4_get_ip need to return same value */ - mock = pico_mock_create(macaddr1); + mock = pico_mock_create(S, macaddr1); tmp = pico_slaacv4_getip(mock->dev, 0); fail_if(tmp != pico_slaacv4_getip(mock->dev, 0)); diff --git a/test/unit/unit_ipv6.c b/test/unit/unit_ipv6.c index 1d918f19..500cac6b 100644 --- a/test/unit/unit_ipv6.c +++ b/test/unit/unit_ipv6.c @@ -1,5 +1,7 @@ #ifdef PICO_SUPPORT_IPV6 +static PICO_TREE_DECLARE(MCASTFilter_ipv6, mcast_filter_cmp_ipv6); + START_TEST (test_ipv6) { char ipstr[40] = { @@ -90,11 +92,12 @@ START_TEST (test_ipv6) struct pico_ipv6_link *l[IP_TST_SIZ]; struct pico_ipv6_link *_link = NULL; struct pico_ipv6_route *_route = NULL; + struct pico_stack *S = NULL; char devname[8]; int ret = 0; int i = 0; - pico_stack_init(); + pico_stack_init(&S); /* pico_string_to_ipv6 and pico_ipv6_to_string */ printf("pico_string_to_ipv6 valid conversion of %s\n", ipstr0); @@ -223,7 +226,7 @@ START_TEST (test_ipv6) /*link_add*/ for (i = 0; i < 10; ++i) { snprintf(devname, 8, "nul%d", i); - dev[i] = pico_null_create(devname); + dev[i] = pico_null_create(S, devname); a[i] = iphex_a; a[i].addr[4] = (uint8_t)(a[i].addr[4] + i); fail_if(pico_ipv6_link_add(dev[i], a[i], nm64) == NULL, "Error adding link"); @@ -232,24 +235,24 @@ START_TEST (test_ipv6) for (i = 0; i < 10; ++i) { gw[i] = iphex_gw; gw[i].addr[4] = (uint8_t)(gw[i].addr[4] + i); - fail_unless(pico_ipv6_link_find(&a[i]) == dev[i], "Error finding link"); - l[i] = pico_ipv6_link_get(&a[i]); + fail_unless(pico_ipv6_link_find(S, &a[i]) == dev[i], "Error finding link"); + l[i] = pico_ipv6_link_get(S, &a[i]); fail_if(l[i] == NULL, "Error getting link"); r[i] = iphex_r; r[i].addr[4] = (uint8_t)(r[i].addr[4] + i); - fail_if(pico_ipv6_route_add(r[i], nm128, a[i], 1, l[i]) != 0, "Error adding route"); + fail_if(pico_ipv6_route_add(S, r[i], nm128, a[i], 1, l[i]) != 0, "Error adding route"); } /*get_gateway*/ for (i = 0; i < 10; i++) { - _gw = pico_ipv6_route_get_gateway(&r[i]); + _gw = pico_ipv6_route_get_gateway(S, &r[i]); fail_if(memcmp(_gw.addr, a[i].addr, PICO_SIZE_IP6) != 0, "Error get gateway: returned wrong route"); - source[i] = pico_ipv6_source_find(&r[i]); + source[i] = pico_ipv6_source_find(S, &r[i]); fail_if(memcmp(source[i]->addr, a[i].addr, PICO_SIZE_IP6) != 0, "Error find source: returned wrong route"); } /*route_del + link_del*/ for (i = 0; i < 10; i++) { - fail_if(pico_ipv6_route_del(r[i], nm128, a[i], 1, l[i]) != 0, "Error deleting route"); - fail_if(pico_ipv6_link_del(dev[i], a[i]) != 0, "Error deleting link"); + fail_if(pico_ipv6_route_del(S, r[i], nm128, a[i], 1, l[i]) != 0, "Error deleting route"); + fail_if(pico_ipv6_link_del(S, dev[i], a[i]) != 0, "Error deleting link"); } /* add 2 links to dev[0] */ _link = pico_ipv6_link_add(dev[0], a[0], nm64); @@ -257,13 +260,13 @@ START_TEST (test_ipv6) _link = pico_ipv6_link_add(dev[0], a[1], nm64); fail_if (!_link, "Error adding link"); /* add 2 routes to each of the links */ - ret = pico_ipv6_route_add(r[0], nm128, a[0], 1, l[0]); + ret = pico_ipv6_route_add(S, r[0], nm128, a[0], 1, l[0]); fail_if(ret != 0, "Error adding route"); - ret = pico_ipv6_route_add(r[1], nm128, a[0], 1, l[0]); + ret = pico_ipv6_route_add(S, r[1], nm128, a[0], 1, l[0]); fail_if(ret != 0, "Error adding route"); - ret = pico_ipv6_route_add(r[2], nm128, a[1], 1, l[1]); + ret = pico_ipv6_route_add(S, r[2], nm128, a[1], 1, l[1]); fail_if(ret != 0, "Error adding route"); - ret = pico_ipv6_route_add(r[3], nm128, a[1], 1, l[1]); + ret = pico_ipv6_route_add(S, r[3], nm128, a[1], 1, l[1]); fail_if(ret != 0, "Error adding route"); /* add 2 links to dev[1] */ @@ -272,42 +275,42 @@ START_TEST (test_ipv6) _link = pico_ipv6_link_add(dev[1], a[9], nm64); fail_if (!_link, "Error adding link"); /* add 2 routes to each of the links */ - ret = pico_ipv6_route_add(r[6], nm128, a[8], 1, l[8]); + ret = pico_ipv6_route_add(S, r[6], nm128, a[8], 1, l[8]); fail_if(ret != 0, "Error adding route"); - ret = pico_ipv6_route_add(r[7], nm128, a[8], 1, l[8]); + ret = pico_ipv6_route_add(S, r[7], nm128, a[8], 1, l[8]); fail_if(ret != 0, "Error adding route"); - ret = pico_ipv6_route_add(r[8], nm128, a[9], 1, l[9]); + ret = pico_ipv6_route_add(S, r[8], nm128, a[9], 1, l[9]); fail_if(ret != 0, "Error adding route"); - ret = pico_ipv6_route_add(r[9], nm128, a[9], 1, l[9]); + ret = pico_ipv6_route_add(S, r[9], nm128, a[9], 1, l[9]); fail_if(ret != 0, "Error adding route"); /* destroy device, should clean up all links and routes */ pico_device_destroy(dev[0]); - _link = pico_ipv6_link_get(&a[0]); + _link = pico_ipv6_link_get(S, &a[0]); fail_if(_link != NULL, "Error destroying device"); - _link = pico_ipv6_link_get(&a[1]); + _link = pico_ipv6_link_get(S, &a[1]); fail_if(_link != NULL, "Error destroying device"); - _link = pico_ipv6_link_get(&a[8]); + _link = pico_ipv6_link_get(S, &a[8]); fail_if(_link == NULL, "Error destroying device"); - _link = pico_ipv6_link_get(&a[9]); + _link = pico_ipv6_link_get(S, &a[9]); fail_if(_link == NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[0]); + _route = pico_ipv6_route_find(S, &r[0]); fail_if(_route != NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[1]); + _route = pico_ipv6_route_find(S, &r[1]); fail_if(_route != NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[2]); + _route = pico_ipv6_route_find(S, &r[2]); fail_if(_route != NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[3]); + _route = pico_ipv6_route_find(S, &r[3]); fail_if(_route != NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[6]); + _route = pico_ipv6_route_find(S, &r[6]); fail_if(_route == NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[7]); + _route = pico_ipv6_route_find(S, &r[7]); fail_if(_route == NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[8]); + _route = pico_ipv6_route_find(S, &r[8]); fail_if(_route == NULL, "Error destroying device"); - _route = pico_ipv6_route_find(&r[9]); + _route = pico_ipv6_route_find(S, &r[9]); fail_if(_route == NULL, "Error destroying device"); } END_TEST @@ -345,7 +348,8 @@ START_TEST (test_mld_sockopts) 0 }; - pico_stack_init(); + struct pico_stack *S = NULL; + pico_stack_init(&S); printf("START MLD SOCKOPTS TEST\n"); @@ -389,17 +393,17 @@ START_TEST (test_mld_sockopts) mreq_source[(i * 8) + j].mcast_source_addr = inaddr_source[j]; } } - dev = pico_null_create("dummy0"); + dev = pico_null_create(S, "dummy0"); ret_link = pico_ipv6_link_add(dev, inaddr_link[0].ip6, netmask); fail_if(ret_link == NULL, "link add failed"); - dev = pico_null_create("dummy1"); + dev = pico_null_create(S, "dummy1"); ret_link = pico_ipv6_link_add(dev, inaddr_link[1].ip6, netmask); fail_if(ret_link == NULL, "link add failed"); - s = pico_socket_open(PICO_PROTO_IPV6, PICO_PROTO_UDP, NULL); + s = pico_socket_open(S, PICO_PROTO_IPV6, PICO_PROTO_UDP, NULL); fail_if(s == NULL, "UDP socket open failed"); - s1 = pico_socket_open(PICO_PROTO_IPV6, PICO_PROTO_UDP, NULL); + s1 = pico_socket_open(S, PICO_PROTO_IPV6, PICO_PROTO_UDP, NULL); fail_if(s1 == NULL, "UDP socket open failed"); @@ -620,10 +624,10 @@ START_TEST (test_mld_sockopts) fail_if(ret < 0, "PICO_IP_ADD_SOURCE_MEMBERSHIP failed\n"); i = 0; - pico_tree_foreach(index, &MCASTFilter) + pico_tree_foreach(index, &MCASTFilter_ipv6) { if (++i > 2) - fail("MCASTFilter (INCLUDE + INCLUDE) too many elements\n"); + fail("MCASTFilter_ipv6 (INCLUDE + INCLUDE) too many elements\n"); source = index->keyValue; if (memcmp(&source->ip6, &mreq_source[0].mcast_source_addr, sizeof(struct pico_ip6)) == 0) { /* OK */ @@ -631,7 +635,7 @@ START_TEST (test_mld_sockopts) else if (memcmp(&source->ip6, &mreq_source[1].mcast_source_addr, sizeof(struct pico_ip6)) == 0) { /* OK */ } else { - fail("MCASTFilter (INCLUDE + INCLUDE) incorrect\n"); + fail("MCASTFilter_ipv6 (INCLUDE + INCLUDE) incorrect\n"); } } @@ -653,16 +657,16 @@ START_TEST (test_mld_sockopts) ret = pico_socket_setoption(s1, PICO_IP_BLOCK_SOURCE, &mreq_source[2]); fail_if(ret < 0, "PICO_IP_BLOCK_SOURCE failed\n"); i = 0; - pico_tree_foreach(index, &MCASTFilter) + pico_tree_foreach(index, &MCASTFilter_ipv6) { if (++i > 1) - fail("MCASTFilter (INCLUDE + EXCLUDE) too many elements\n"); + fail("MCASTFilter_ipv6 (INCLUDE + EXCLUDE) too many elements\n"); source = index->keyValue; if (memcmp(&source->ip6, &mreq_source[2].mcast_source_addr, sizeof(struct pico_ip6)) == 0) { /* OK */ } else { - fail("MCASTFilter (INCLUDE + EXCLUDE) incorrect\n"); + fail("MCASTFilter_ipv6 (INCLUDE + EXCLUDE) incorrect\n"); } } ret = pico_socket_setoption(s, PICO_IP_DROP_MEMBERSHIP, &mreq[0]); @@ -687,10 +691,10 @@ START_TEST (test_mld_sockopts) fail_if(ret < 0, "PICO_IP_ADD_SOURCE_MEMBERSHIP failed\n"); i = 0; - pico_tree_foreach(index, &MCASTFilter) + pico_tree_foreach(index, &MCASTFilter_ipv6) { if (++i > 2) - fail("MCASTFilter (EXCLUDE + INCLUDE) too many elements\n"); + fail("MCASTFilter_ipv6 (EXCLUDE + INCLUDE) too many elements\n"); source = index->keyValue; if (memcmp(&source->ip6, &mreq_source[0].mcast_source_addr, sizeof(struct pico_ip6)) == 0) { /* OK */ @@ -698,7 +702,7 @@ START_TEST (test_mld_sockopts) else if (memcmp(&source->ip6, &mreq_source[1].mcast_source_addr, sizeof(struct pico_ip6)) == 0) { /* OK */ } else { - fail("MCASTFilter (EXCLUDE + INCLUDE) incorrect\n"); + fail("MCASTFilter_ipv6 (EXCLUDE + INCLUDE) incorrect\n"); } } ret = pico_socket_setoption(s, PICO_IP_DROP_MEMBERSHIP, &mreq[0]); @@ -728,10 +732,10 @@ START_TEST (test_mld_sockopts) ret = pico_socket_setoption(s1, PICO_IP_BLOCK_SOURCE, &mreq_source[6]); fail_if(ret < 0, "PICO_IP_BLOCK_SOURCE failed\n"); i = 0; - pico_tree_foreach(index, &MCASTFilter) + pico_tree_foreach(index, &MCASTFilter_ipv6) { if (++i > 2) - fail("MCASTFilter (EXCLUDE + EXCLUDE) too many elements\n"); + fail("MCASTFilter_ipv6 (EXCLUDE + EXCLUDE) too many elements\n"); source = index->keyValue; if (memcmp(&source->ip6, &mreq_source[3].mcast_source_addr, sizeof(struct pico_ip6) == 0)) { /* OK */ @@ -739,7 +743,7 @@ START_TEST (test_mld_sockopts) else if (memcmp(&source->ip6, &mreq_source[4].mcast_source_addr, sizeof(struct pico_ip6)) == 0) { /* OK */ } else { - fail("MCASTFilter (EXCLUDE + EXCLUDE) incorrect\n"); + fail("MCASTFilter_ipv6 (EXCLUDE + EXCLUDE) incorrect\n"); } } ret = pico_socket_setoption(s, PICO_IP_DROP_MEMBERSHIP, &mreq[0]); diff --git a/test/unit/unit_mocks.c b/test/unit/unit_mocks.c index 2f1381d1..58bdc1a7 100644 --- a/test/unit/unit_mocks.c +++ b/test/unit/unit_mocks.c @@ -1,8 +1,10 @@ +#include "pico_stack.h" + #define BUFLEN (576 + 14 + 20 + 8) int mock_print_protocol(uint8_t *buf); int printbuf(uint8_t *buf, uint32_t len, const char *str, uint8_t printbufactive); -int tick_it(uint32_t nticks); +int tick_it(struct pico_stack *S, uint32_t nticks); int mock_print_protocol(uint8_t *buf) { @@ -61,11 +63,11 @@ int printbuf(uint8_t *buf, uint32_t len, const char *str, uint8_t printbufactive #define DHCP_MSG_TYPE_OFFER (2) #define DHCP_MSG_TYPE_REQUEST (3) #define DHCP_MSG_TYPE_ACK (4) -int tick_it(uint32_t nticks) +int tick_it(struct pico_stack *S, uint32_t nticks) { uint32_t i = 0; for (i = 0; i < nticks; i++) { - pico_stack_tick(); + pico_stack_tick(S); } return 0; } diff --git a/test/unit/unit_pico_device.c b/test/unit/unit_pico_device.c index 6ec05961..ebecec8d 100644 --- a/test/unit/unit_pico_device.c +++ b/test/unit/unit_pico_device.c @@ -13,8 +13,10 @@ START_TEST(test_pico_device) }; const char *name = "device_test"; + struct pico_stack *S; + pico_stack_init(&S); dummy_device = PICO_ZALLOC(sizeof(struct pico_device)); - pico_device_init(dummy_device, name, mac); + pico_device_init(S, dummy_device, name, mac); pico_device_destroy(dummy_device); } END_TEST diff --git a/test/unit/unit_socket.c b/test/unit/unit_socket.c index ca040bda..8c4821bf 100644 --- a/test/unit/unit_socket.c +++ b/test/unit/unit_socket.c @@ -1,6 +1,7 @@ -int pico_aodv_init(void) +int pico_aodv_init(struct pico_stack *S) { + (void)S; return 0; } START_TEST (test_socket) @@ -11,6 +12,7 @@ START_TEST (test_socket) struct pico_socket *sk_tcp, *sk_udp, *s, *sl, *sa; struct pico_device *dev; struct pico_ip4 inaddr_dst, inaddr_link, inaddr_incorrect, inaddr_uni, inaddr_null, netmask, orig, inaddr_got; + struct pico_stack *S; int getnodelay = -1; int nodelay = -1; @@ -19,7 +21,7 @@ START_TEST (test_socket) uint32_t getsocket_buffer = 0; uint32_t socket_buffer = 0; - pico_stack_init(); + pico_stack_init(&S); printf("START SOCKET TEST\n"); @@ -29,27 +31,27 @@ START_TEST (test_socket) pico_string_to_ipv4("0.0.0.0", &inaddr_null.addr); pico_string_to_ipv4("10.40.0.3", &inaddr_uni.addr); - dev = pico_null_create("dummy"); + dev = pico_null_create(S, "dummy"); netmask.addr = long_be(0xFFFF0000); - ret = pico_ipv4_link_add(dev, inaddr_link, netmask); + ret = pico_ipv4_link_add(S, dev, inaddr_link, netmask); fail_if(ret < 0, "socket> error adding link"); /* socket_open passing wrong parameters */ - s = pico_socket_open(PICO_PROTO_IPV4, 99, NULL); + s = pico_socket_open(S, PICO_PROTO_IPV4, 99, NULL); fail_if(s != NULL, "Error got socket wrong parameters"); - s = pico_socket_open(PICO_PROTO_IPV4, 0xFFFF, NULL); + s = pico_socket_open(S, PICO_PROTO_IPV4, 0xFFFF, NULL); fail_if(s != NULL, "Error got socket"); - s = pico_socket_open(99, PICO_PROTO_UDP, NULL); + s = pico_socket_open(S, 99, PICO_PROTO_UDP, NULL); fail_if(s != NULL, "Error got socket"); - s = pico_socket_open(0xFFFF, PICO_PROTO_UDP, NULL); + s = pico_socket_open(S, 0xFFFF, PICO_PROTO_UDP, NULL); fail_if(s != NULL, "Error got socket"); - sk_tcp = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, NULL); + sk_tcp = pico_socket_open(S, PICO_PROTO_IPV4, PICO_PROTO_TCP, NULL); fail_if(sk_tcp == NULL, "socket> tcp socket open failed"); @@ -73,22 +75,22 @@ START_TEST (test_socket) /* socket_bind passing correct parameters */ ret = pico_socket_bind(sk_tcp, &inaddr_link, &port_be); fail_if(ret < 0, "socket> tcp socket bind failed"); - count = pico_count_sockets(PICO_PROTO_TCP); + count = pico_count_sockets(S, PICO_PROTO_TCP); printf("Count: %d\n", count); fail_unless(count == 1); - count = pico_count_sockets(0); + count = pico_count_sockets(S, 0); printf("Count: %d\n", count); fail_unless(count == 1); - sk_udp = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL); + sk_udp = pico_socket_open(S, PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL); fail_if(sk_udp == NULL, "socket> udp socket open failed"); port_be = short_be(5555); ret = pico_socket_bind(sk_udp, &inaddr_link, &port_be); fail_if(ret < 0, "socket> udp socket bind failed"); - fail_if (pico_count_sockets(PICO_PROTO_UDP) != 1); - fail_if (pico_count_sockets(0) != 2); + fail_if (pico_count_sockets(S, PICO_PROTO_UDP) != 1); + fail_if (pico_count_sockets(S, 0) != 2); ret = pico_socket_getname(sk_udp, &inaddr_got, &port_got, &proto); @@ -116,7 +118,7 @@ START_TEST (test_socket) /* testing listening socket */ - sl = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, NULL); + sl = pico_socket_open(S, PICO_PROTO_IPV4, PICO_PROTO_TCP, NULL); fail_if(sl == NULL, "socket> tcp socket open failed"); port_be = short_be(6666); ret = pico_socket_bind(sl, &inaddr_link, &port_be); @@ -442,7 +444,7 @@ START_TEST (test_crc_check) int ret = -1; printf("START CRC TEST\n"); - pico_stack_init(); + pico_stack_init(&S); /* IPv4 CRC unit tests */ /* Allocated memory will not be freed when pico_ipv4_crc_check fails */ diff --git a/test/unit/unit_timer.c b/test/unit/unit_timer.c index 92f17648..c659a4b4 100644 --- a/test/unit/unit_timer.c +++ b/test/unit/unit_timer.c @@ -6,35 +6,38 @@ START_TEST (test_timers) uint32_t T[128]; int i; struct pico_timer_ref *tref; - pico_stack_init(); + struct pico_stack *S; + + pico_stack_init(&S); + for (i = 0; i < 128; i++) { pico_time expire = (pico_time)(999999 + i); void (*timer)(pico_time, void *) =(void (*)(pico_time, void *))0xff00 + i; void *arg = ((void*)0xaa00 + i); - T[i] = pico_timer_add(expire, timer, arg); + T[i] = pico_timer_add(S, expire, timer, arg); printf("New timer %u\n", T[i]); } for (i = 0; i < 128; i++) { void (*timer)(pico_time, void *) =(void (*)(pico_time, void *))0xff00 + i; void *arg = ((void*)0xaa00 + i); - fail_if((uint32_t)(i + 1) > Timers->n); - tref = heap_get_element(Timers, (uint32_t)i + EXISTING_TIMERS); + fail_if((uint32_t)(i + 1) > S->Timers->n); + tref = heap_get_element(S->Timers, (uint32_t)i + EXISTING_TIMERS); fail_unless(tref->id == T[i]); fail_unless(tref->tmr->timer == timer); fail_unless(tref->tmr->arg == arg); } for (i = 127; i >= 0; i--) { printf("Deleting timer %d \n", i ); - pico_timer_cancel(T[i]); + pico_timer_cancel(S, T[i]); printf("Deleted timer %d \n", i ); - tref = heap_get_element(Timers, (uint32_t)i + EXISTING_TIMERS); + tref = heap_get_element(S->Timers, (uint32_t)i + EXISTING_TIMERS); fail_unless(tref->tmr == NULL); } - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); - pico_stack_tick(); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); + pico_stack_tick(S); } END_TEST diff --git a/test/units.c b/test/units.c index fec736f4..4d9d9987 100644 --- a/test/units.c +++ b/test/units.c @@ -98,7 +98,7 @@ END_TEST START_TEST (test_tick) { - pico_tick = (uint64_t)-1; + pico_time pico_tick = (uint64_t)-1; fail_if(pico_tick != 0xFFFFFFFFFFFFFFFF, "Failed to assign (uint64_t)-1 to pico_tick\n"); } END_TEST