diff --git a/src/core/ipv4/ip4_addr.c b/src/core/ipv4/ip4_addr.c index 79127ac95..f36f6dbae 100644 --- a/src/core/ipv4/ip4_addr.c +++ b/src/core/ipv4/ip4_addr.c @@ -93,7 +93,11 @@ u8_t ip4_addr_netmask_valid(u32_t netmask) { u32_t mask; - u32_t nm_hostorder = lwip_htonl(netmask); + u32_t nm_hostorder = lwip_ntohl(netmask); + + if (nm_hostorder == 0) { + return 0; + } /* first, check for the first zero */ for (mask = 1UL << 31 ; mask != 0; mask >>= 1) { @@ -177,8 +181,21 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr) break; val = (val * base) + (u32_t)(c - '0'); c = *++cp; +#if LWIP_NO_CTYPE_H + } else if (base == 16) { + u32_t a; + if (lwip_in_range(c, 'a', 'f')) { + a = 'a'; + } else if (lwip_in_range(c, 'A', 'F')) { + a = 'A'; + } else { + break; + } + val = (val << 4) | (u32_t)(c - a + 10); +#else /* LWIP_NO_CTYPE_H */ } else if (base == 16 && lwip_isxdigit(c)) { val = (val << 4) | (u32_t)(c + 10 - (lwip_islower(c) ? 'a' : 'A')); +#endif /* LWIP_NO_CTYPE_H */ c = *++cp; } else { break; @@ -212,10 +229,8 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr) */ switch (pp - parts + 1) { - case 0: - return 0; /* initial nondigit */ - - case 1: /* a -- 32 bits */ + default: /* a -- 32 bits */ + LWIP_ASSERT("unhandled", pp == parts); break; case 2: /* a.b -- 8.24 bits */ @@ -247,9 +262,6 @@ ip4addr_aton(const char *cp, ip4_addr_t *addr) } val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); break; - default: - LWIP_ASSERT("unhandled", 0); - break; } if (addr) { ip4_addr_set_u32(addr, lwip_htonl(val)); diff --git a/test/unit/core/test_def.c b/test/unit/core/test_def.c index 0ae2e9c1e..effbca75b 100644 --- a/test/unit/core/test_def.c +++ b/test/unit/core/test_def.c @@ -28,6 +28,71 @@ def_check_range_untouched(const char *buf, size_t len) } } +START_TEST(test_def_lwip_strnstr) +{ + const char *buffer = "abc"; + + LWIP_UNUSED_ARG(_i); + + fail_unless(lwip_strnstr(buffer, "", 3) == buffer); + fail_unless(lwip_strnstr(buffer, "bc", 3) == buffer + 1); + fail_unless(lwip_strnstr(buffer, "bx", 3) == NULL); + fail_unless(lwip_strnstr(buffer, "x", 3) == NULL); +} +END_TEST + +START_TEST(test_def_lwip_strnistr) +{ + const char *buffer = "aBC"; + + LWIP_UNUSED_ARG(_i); + + fail_unless(lwip_strnistr(buffer, "", 3) == buffer); + fail_unless(lwip_strnistr(buffer, "bc", 3) == buffer + 1); + fail_unless(lwip_strnistr(buffer, "bx", 3) == NULL); + fail_unless(lwip_strnistr(buffer, "x", 3) == NULL); +} +END_TEST + +START_TEST(test_def_lwip_stricmp) +{ + LWIP_UNUSED_ARG(_i); + + fail_unless(lwip_stricmp("", "") == 0); + fail_unless(lwip_stricmp("!", "!") == 0); + fail_unless(lwip_stricmp("!", "{") != 0); + fail_unless(lwip_stricmp("{", "!") != 0); + fail_unless(lwip_stricmp("{", "{") == 0); + fail_unless(lwip_stricmp("1", "1") == 0); + fail_unless(lwip_stricmp("1", "2") != 0); + fail_unless(lwip_stricmp("a", "a") == 0); + fail_unless(lwip_stricmp("a", "b") != 0); + fail_unless(lwip_stricmp("a", "A") == 0); + fail_unless(lwip_stricmp("a", "b") != 0); +} +END_TEST + +START_TEST(test_def_lwip_strincmp) +{ + int i; + + LWIP_UNUSED_ARG(_i); + + for (i = 2; i < 3; ++i) { + fail_unless(lwip_strnicmp("", "", i) == 0); + fail_unless(lwip_strnicmp("0!", "0!", i) == 0); + fail_unless(lwip_strnicmp("0!", "0{", i) != 0); + fail_unless(lwip_strnicmp("0{", "0!", i) != 0); + fail_unless(lwip_strnicmp("0{", "0{", i) == 0); + fail_unless(lwip_strnicmp("01", "01", i) == 0); + fail_unless(lwip_strnicmp("01", "02", i) != 0); + fail_unless(lwip_strnicmp("0a", "0a", i) == 0); + fail_unless(lwip_strnicmp("0a", "0b", i) != 0); + fail_unless(lwip_strnicmp("0a", "0A", i) == 0); + fail_unless(lwip_strnicmp("0a", "0b", i) != 0); + } +} + static void test_def_itoa(int number, const char *expected) { char buf[TEST_BUFSIZE]; @@ -60,8 +125,14 @@ static void test_def_itoa(int number, const char *expected) START_TEST(test_def_lwip_itoa) { + char ch; + LWIP_UNUSED_ARG(_i); + lwip_itoa(&ch, 0, 0); + lwip_itoa(&ch, 1, 0); + fail_unless(ch == '\0'); + test_def_itoa(0, "0"); test_def_itoa(1, "1"); test_def_itoa(-1, "-1"); @@ -73,12 +144,30 @@ START_TEST(test_def_lwip_itoa) } END_TEST +START_TEST(test_def_lwip_memcmp_consttime) +{ + char a = 'a'; + char b = 'b'; + + LWIP_UNUSED_ARG(_i); + + fail_unless(lwip_memcmp_consttime(NULL, NULL, 0) == 0); + fail_unless(lwip_memcmp_consttime(&a, &a, sizeof(a)) == 0); + fail_unless(lwip_memcmp_consttime(&a, &b, sizeof(a)) != 0); +} +END_TEST + /** Create the suite including all tests for this module */ Suite * def_suite(void) { testfunc tests[] = { - TESTFUNC(test_def_lwip_itoa) + TESTFUNC(test_def_lwip_strnstr), + TESTFUNC(test_def_lwip_strnistr), + TESTFUNC(test_def_lwip_stricmp), + TESTFUNC(test_def_lwip_strincmp), + TESTFUNC(test_def_lwip_itoa), + TESTFUNC(test_def_lwip_memcmp_consttime) }; return create_suite("DEF", tests, sizeof(tests)/sizeof(testfunc), def_setup, def_teardown); } diff --git a/test/unit/core/test_pbuf.c b/test/unit/core/test_pbuf.c index 9c5df33b7..255da9f55 100644 --- a/test/unit/core/test_pbuf.c +++ b/test/unit/core/test_pbuf.c @@ -2,6 +2,7 @@ #include "lwip/pbuf.h" #include "lwip/stats.h" +#include "lwip/tcpip.h" #if !LWIP_STATS || !MEM_STATS ||!MEMP_STATS #error "This tests needs MEM- and MEMP-statistics enabled" @@ -24,6 +25,145 @@ pbuf_teardown(void) lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT)); } +#if LWIP_SUPPORT_CUSTOM_PBUF +static struct pbuf *custom_free_p; + +static void +custom_free(struct pbuf *p) +{ + custom_free_p = p; +} + +START_TEST(test_pbuf_alloced_custom) +{ + struct pbuf_custom cp; + struct pbuf *p; + + memset(&cp, 0, sizeof(cp)); + cp.custom_free_function = custom_free; + + p = pbuf_alloced_custom(PBUF_IP, 0xffffU, PBUF_POOL, &cp, NULL, 0); + fail_unless(p == NULL); + + p = pbuf_alloced_custom(PBUF_IP, 0U, PBUF_POOL, &cp, NULL, 100); + fail_unless(p != NULL && p->payload == NULL); + + custom_free_p = NULL; + pbuf_free(p); + fail_unless(custom_free_p == p); + + p = pbuf_alloced_custom(PBUF_IP, 0U, PBUF_POOL, &cp, &cp, 100); + fail_unless(p != NULL && p->payload != NULL); + + pbuf_realloc(p, 0U); + + custom_free_p = NULL; + pbuf_ref(p); + pbuf_free(p); + fail_unless(custom_free_p == NULL); + pbuf_free(p); + fail_unless(custom_free_p == p); +} +#endif + +static void +free_allocated_pbufs(struct pbuf *head) +{ + struct pbuf *p; + + while (head != NULL) { + p = head; + head = (struct pbuf *)p->payload; + pbuf_free(p); + } +} + +START_TEST(test_pbuf_alloc_failures) +{ + struct pbuf *head; + struct pbuf *p; + struct pbuf *q; + struct pbuf *r; + int ret; + LWIP_UNUSED_ARG(_i); + + if (sizeof(u16_t) == sizeof(mem_size_t)) { + /* Payload length overflow */ + p = pbuf_alloc(PBUF_IP, 0xffffU, PBUF_RAM); + fail_unless(p == NULL); + + /* Allocation length overflow */ + p = pbuf_alloc(PBUF_RAW, 0xffffU, PBUF_RAM); + fail_unless(p == NULL); + } + + /* Exhaust MEMP_PBUF_POOL */ + + head = NULL; + + while (1) { + p = pbuf_alloc(PBUF_RAW, 0xffffU, PBUF_POOL); + if (p == NULL) { + break; + } + + p->payload = head; + head = p; + } + + free_allocated_pbufs(head); + + do { + ret = tcpip_thread_poll_one(); + } while (ret == 0); + + /* Exhaust MEMP_PBUF */ + + head = NULL; + + while (1) { + p = pbuf_alloc_reference(NULL, 0, PBUF_ROM); + if (p == NULL) { + break; + } + + p->payload = head; + head = p; + } + + free_allocated_pbufs(head); + + /* Exhaust mem_malloc() */ + + head = NULL; + + while (1) { + p = pbuf_alloc(PBUF_RAW, 0x8000U, PBUF_RAM); + + if (p == NULL) { + q = pbuf_alloc(PBUF_RAW, 0xffffU, PBUF_POOL); + fail_unless(q != NULL); + + if (q != NULL) { + r = pbuf_coalesce(q, PBUF_RAW); + fail_unless(r == q); + + r = pbuf_clone(PBUF_RAW, PBUF_RAM, q); + fail_unless(r == NULL); + + pbuf_free(q); + } + + break; + } + + p->payload = head; + head = p; + } + + free_allocated_pbufs(head); +} +END_TEST #define TESTBUFSIZE_1 65535 #define TESTBUFSIZE_2 65530 @@ -32,7 +172,7 @@ static u8_t testbuf_1[TESTBUFSIZE_1]; static u8_t testbuf_1a[TESTBUFSIZE_1]; static u8_t testbuf_2[TESTBUFSIZE_2]; static u8_t testbuf_2a[TESTBUFSIZE_2]; -static u8_t testbuf_3[TESTBUFSIZE_3]; +static u8_t testbuf_3[TESTBUFSIZE_3 + 1]; static u8_t testbuf_3a[TESTBUFSIZE_3]; /* Test functions */ @@ -67,6 +207,254 @@ START_TEST(test_pbuf_alloc_zero_pbufs) } END_TEST +START_TEST(test_pbuf_realloc) +{ + struct pbuf *p; + LWIP_UNUSED_ARG(_i); + + p = pbuf_alloc(PBUF_RAW, 0xffffU, PBUF_POOL); + fail_unless(p != NULL); + + if (p != NULL) { + pbuf_realloc(p, 0xffffU); + pbuf_realloc(p, 0x8000U); + pbuf_realloc(p, 0U); + pbuf_free(p); + } + + p = pbuf_alloc(PBUF_RAW, 100, PBUF_RAM); + fail_unless(p != NULL); + + if (p != NULL) { + pbuf_realloc(p, 100U); + pbuf_realloc(p, 50U); + pbuf_realloc(p, 0U); + pbuf_free(p); + } +} +END_TEST + +START_TEST(test_pbuf_header) +{ + struct pbuf *p; + struct pbuf *q; + u8_t err; + u8_t *payload; + LWIP_UNUSED_ARG(_i); + + p = pbuf_alloc(PBUF_IP, 100, PBUF_RAM); + fail_unless(p != NULL); + + if (p != NULL) { + payload = (u8_t *)p->payload; + +#ifdef LWIP_NOASSERT + err = pbuf_add_header(NULL, 0); + fail_unless(err == 1); +#endif + + err = pbuf_add_header(p, 0x10000U); + fail_unless(err == 1); + + err = pbuf_add_header(p, 0xffffU); + fail_unless(err == 1); + + err = pbuf_add_header(p, 200); + fail_unless(err == 1); + + err = pbuf_add_header(p, 0); + fail_unless(err == 0); + fail_unless(p->payload == payload); + + err = pbuf_header(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 1); + + err = pbuf_header_force(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 2); + + err = pbuf_add_header(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 3); + + err = pbuf_add_header_force(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 4); + + err = pbuf_header_force(p, -1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 3); + + err = pbuf_header(p, -1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 2); + + err = pbuf_remove_header(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 1); + + err = pbuf_remove_header(p, 0); + fail_unless(err == 0); + fail_unless(p->payload == payload - 1); + +#ifdef LWIP_NOASSERT + err = pbuf_remove_header(NULL, 0); + fail_unless(err == 1); +#endif + + err = pbuf_remove_header(p, 0x10000U); + fail_unless(err == 1); + + err = pbuf_remove_header(p, 0xffffU); + fail_unless(err == 1); + + pbuf_free(p); + } + + p = pbuf_alloc(PBUF_IP, 100, PBUF_REF); + fail_unless(p != NULL); + + if (p != NULL) { + payload = (u8_t *)p->payload; + + err = pbuf_add_header(p, 1); + fail_unless(err == 1); + + err = pbuf_add_header_force(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload - 1); + + err = pbuf_remove_header(p, 1); + fail_unless(err == 0); + fail_unless(p->payload == payload); + + pbuf_free(p); + } + + p = pbuf_alloc(PBUF_RAW, 0xffffU, PBUF_POOL); + + if (p != NULL) { + q = pbuf_free_header(p, 0x8000U); + fail_unless(q != NULL); + + p = pbuf_free_header(q, 0x8000U); + fail_unless(p == NULL); + } +} +END_TEST + +START_TEST(test_pbuf_chain) +{ + struct pbuf *p; + struct pbuf *q; + struct pbuf *r; + u16_t p_clen; + u16_t q_clen; + LWIP_UNUSED_ARG(_i); + + pbuf_ref(NULL); + pbuf_cat(NULL, NULL); + + p = pbuf_alloc(PBUF_IP, 100, PBUF_RAM); + fail_unless(p != NULL); + + if (p != NULL) { + p_clen = pbuf_clen(p); + pbuf_cat(NULL, p); + pbuf_cat(p, NULL); + + q = pbuf_alloc(PBUF_IP, 100, PBUF_RAM); + fail_unless(q != NULL); + + if (q != NULL) { + q_clen = pbuf_clen(q); + pbuf_cat(p, q); + fail_unless(pbuf_clen(p) == p_clen + q_clen); + p_clen += q_clen; + } + + q = pbuf_alloc(PBUF_IP, 100, PBUF_RAM); + fail_unless(q != NULL); + + if (q != NULL) { + q_clen = pbuf_clen(q); + pbuf_chain(p, q); + fail_unless(pbuf_clen(p) == p_clen + q_clen); + pbuf_free(q); + } + + pbuf_free(p); + } + + p = pbuf_alloc(PBUF_IP, 100, PBUF_RAM); + fail_unless(p != NULL); + + if (p != NULL) { + q = pbuf_alloc(PBUF_IP, 100, PBUF_RAM); + fail_unless(q != NULL); + + if (q != NULL) { + pbuf_chain(p, q); + r = pbuf_dechain(p); + fail_unless(r == q); + + r = pbuf_dechain(p); + fail_unless(r == NULL); + + pbuf_free(q); + } + + pbuf_free(p); + } +} +END_TEST + +START_TEST(test_pbuf_get_contiguous) +{ + u8_t buf[128]; + struct pbuf *p; + struct pbuf *q; + u8_t *b; + LWIP_UNUSED_ARG(_i); + + b = (u8_t *)pbuf_get_contiguous(NULL, NULL, 0, 0, 0); + fail_unless(b == NULL); + + p = pbuf_alloc(PBUF_RAW, 64, PBUF_RAM); + fail_unless(p != NULL); + + if (p != NULL) { + b = (u8_t *)pbuf_get_contiguous(p, buf, 0, 1, 0); + fail_unless(b == NULL); + + b = (u8_t *)pbuf_get_contiguous(p, buf, 128, 128, 0); + fail_unless(b == NULL); + + b = (u8_t *)pbuf_get_contiguous(p, buf, 128, 1, 1); + fail_unless(b - 1 == p->payload); + + b = (u8_t *)pbuf_get_contiguous(p, buf, 128, 1, 100); + fail_unless(b == NULL); + + q = pbuf_alloc(PBUF_RAW, 64, PBUF_RAM); + fail_unless(q != NULL); + + if (q != NULL) { + pbuf_cat(p, q); + + b = (u8_t *)pbuf_get_contiguous(p, NULL, 0, 200, 1); + fail_unless(b == NULL); + + b = (u8_t *)pbuf_get_contiguous(p, buf, 128, 128, 0); + fail_unless(b == buf); + } + + pbuf_free(p); + } +} +END_TEST + /** Call pbuf_copy on a pbuf with zero length */ START_TEST(test_pbuf_copy_zero_pbuf) { @@ -165,6 +553,12 @@ START_TEST(test_pbuf_copy_partial_pbuf) fail_unless(dest != NULL); memset(dest->payload, 0, dest->len); + /* From is NULL */ + err = pbuf_copy_partial_pbuf(dest, NULL, a->tot_len, 4); + fail_unless(err == ERR_ARG); + /* To is NULL */ + err = pbuf_copy_partial_pbuf(NULL, a, a->tot_len, 1); + fail_unless(err == ERR_ARG); /* Don't copy if data will not fit */ err = pbuf_copy_partial_pbuf(dest, a, a->tot_len, 4); fail_unless(err == ERR_ARG); @@ -229,10 +623,16 @@ START_TEST(test_pbuf_queueing_bigger_than_64k) fail_unless(p2 != NULL); p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL); fail_unless(p3 != NULL); + err = pbuf_take(NULL, testbuf_1, TESTBUFSIZE_1); + fail_unless(err == ERR_ARG); + err = pbuf_take(p1, NULL, TESTBUFSIZE_1); + fail_unless(err == ERR_ARG); err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1); fail_unless(err == ERR_OK); err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2); fail_unless(err == ERR_OK); + err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3 + 1); + fail_unless(err == ERR_MEM); err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3); fail_unless(err == ERR_OK); @@ -353,19 +753,85 @@ START_TEST(test_pbuf_get_put_at_edge) } END_TEST +START_TEST(test_pbuf_memstr) +{ + u8_t buf[2]; + char str[2]; + struct pbuf *p; + u16_t result; + LWIP_UNUSED_ARG(_i); + + p = pbuf_alloc(PBUF_RAW, 0x8000U, PBUF_POOL); + fail_unless(p != NULL); + + if (p != NULL) { + result = pbuf_memcmp(p, 0xffffU, buf, 0); + fail_unless(result == 0xffff); + + pbuf_put_at(p, 0x0U, 0); + pbuf_put_at(p, 0x1U, 1); + pbuf_put_at(p, 0x2U, 2); + pbuf_put_at(p, 0x7ffeU, 1); + pbuf_put_at(p, 0x7fffU, 2); + + buf[0] = 1; + buf[1] = 2; + result = pbuf_memcmp(p, 0x7ffeU, buf, 2); + fail_unless(result == 0); + + result = pbuf_memfind(p, buf, 2, 0x7ffeU); + fail_unless(result == 0x7ffe); + + result = pbuf_strstr(p, NULL); + fail_unless(result == 0xffff); + + str[0] = 0; + result = pbuf_strstr(p, str); + fail_unless(result == 0xffff); + + str[0] = 1; + str[1] = 0; + result = pbuf_strstr(p, str); + fail_unless(result == 0x1); + + buf[0] = 3; + + result = pbuf_memfind(p, buf, 2, 0x7ffeU); + fail_unless(result == 0xffff); + + result = pbuf_memfind(p, buf, 2, 0x9000U); + fail_unless(result == 0xffff); + + result = pbuf_memcmp(p, 0x7ffeU, buf, 2); + fail_unless(result == 1); + + pbuf_free(p); + } +} +END_TEST + /** Create the suite including all tests for this module */ Suite * pbuf_suite(void) { testfunc tests[] = { +#if LWIP_SUPPORT_CUSTOM_PBUF + TESTFUNC(test_pbuf_alloced_custom), +#endif + TESTFUNC(test_pbuf_alloc_failures), TESTFUNC(test_pbuf_alloc_zero_pbufs), + TESTFUNC(test_pbuf_realloc), + TESTFUNC(test_pbuf_header), + TESTFUNC(test_pbuf_chain), + TESTFUNC(test_pbuf_get_contiguous), TESTFUNC(test_pbuf_copy_zero_pbuf), TESTFUNC(test_pbuf_copy_unmatched_chains), TESTFUNC(test_pbuf_copy_partial_pbuf), TESTFUNC(test_pbuf_split_64k_on_small_pbufs), TESTFUNC(test_pbuf_queueing_bigger_than_64k), TESTFUNC(test_pbuf_take_at_edge), - TESTFUNC(test_pbuf_get_put_at_edge) + TESTFUNC(test_pbuf_get_put_at_edge), + TESTFUNC(test_pbuf_memstr) }; return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown); } diff --git a/test/unit/ip4/test_ip4.c b/test/unit/ip4/test_ip4.c index 64ec1c4fd..0284471ae 100644 --- a/test/unit/ip4/test_ip4.c +++ b/test/unit/ip4/test_ip4.c @@ -59,6 +59,8 @@ test_netif_add(void) NULL, test_netif_init, NULL); netif_set_default(&test_netif); netif_set_up(&test_netif); + linkoutput_ctr = 0; + linkoutput_byte_ctr = 0; } static void @@ -146,9 +148,6 @@ START_TEST(test_ip4_frag) err_t err; LWIP_UNUSED_ARG(_i); - linkoutput_ctr = 0; - linkoutput_byte_ctr = 0; - /* Verify that 8000 byte payload is split into six packets */ fail_unless(data != NULL); test_netif_add(); @@ -242,8 +241,6 @@ START_TEST(test_127_0_0_1) struct pbuf* p; LWIP_UNUSED_ARG(_i); - linkoutput_ctr = 0; - test_netif_add(); netif_set_down(netif_get_loopif()); @@ -257,9 +254,23 @@ START_TEST(test_127_0_0_1) } END_TEST +START_TEST(test_ipaddr_addr) +{ + u32_t addr32; + LWIP_UNUSED_ARG(_i); + + addr32 = ipaddr_addr("1.2.3.4"); + fail_unless(addr32 == PP_HTONL(0x01020304UL)); + + addr32 = ipaddr_addr("foobar"); + fail_unless(addr32 == PP_HTONL(0xffffffffUL)); +} +END_TEST + START_TEST(test_ip4addr_aton) { ip4_addr_t ip_addr; + u8_t ok; LWIP_UNUSED_ARG(_i); @@ -270,6 +281,211 @@ START_TEST(test_ip4addr_aton) fail_unless(ip4addr_aton("192.168.0xd3", &ip_addr) == 1); fail_unless(ip4addr_aton("192.168.0xz5", &ip_addr) == 0); fail_unless(ip4addr_aton("192.168.095", &ip_addr) == 0); + + ok = ip4addr_aton("foobar", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton(" ", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton(".", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x00000001UL)); + + ok = ip4addr_aton("01", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x00000001UL)); + + ok = ip4addr_aton("0y", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("0x!", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("0x?", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("0x[", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("0x{", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("0x", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x00000000UL)); + + ok = ip4addr_aton("0x0102030405", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x02030405UL)); + + ok = ip4addr_aton("0x01000000 ", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01000000UL)); + + ok = ip4addr_aton("0X01020000", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020000UL)); + + ok = ip4addr_aton("0x01020300", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020300UL)); + + ok = ip4addr_aton("0x0a0B0C0d", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x0a0b0c0dUL)); + + ok = ip4addr_aton("0x0F090c0x", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("0x0a0b0c0[", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.X", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("999.2", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.999999999", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2 ", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01000002UL)); + + ok = ip4addr_aton("1.2.X", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.999.3", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.999999", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("999.2.3", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.3 ", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020003UL)); + + ok = ip4addr_aton("1.2.3.X", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.999.X", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("999.2.3.4", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.999.3.4", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.999.4", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.3.999", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.3.4", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4 ", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4\f", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4\n", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4\r", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4\t", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4\v", &ip_addr); + fail_unless(ok == 1); + fail_unless(ip4_addr_get_u32(&ip_addr) == PP_HTONL(0x01020304UL)); + + ok = ip4addr_aton("1.2.3.4.5", &ip_addr); + fail_unless(ok == 0); + + ok = ip4addr_aton("1.2.3.4", NULL); + fail_unless(ok == 1); +} +END_TEST + +START_TEST(test_ip4addr_ntoa) +{ + u8_t ok; + ip4_addr_t addr; + char *s; + char buf[9]; + LWIP_UNUSED_ARG(_i); + + ok = ip4addr_aton("1.2.3.4", &addr); + fail_unless(ok == 1); + + s = ip4addr_ntoa(&addr); + fail_unless(s != NULL); + + if (s != NULL) { + fail_unless(strcmp("1.2.3.4", s) == 0); + } + + ok = ip4addr_aton("1.2.3.45", &addr); + fail_unless(ok == 1); + + memset(buf, 0, sizeof(buf)); + s = ip4addr_ntoa_r(&addr, buf, (int)sizeof(buf)); + fail_unless(s != NULL); + + if (s != NULL) { + fail_unless(strcmp("1.2.3.45", s) == 0); + } + + s = ip4addr_ntoa_r(&addr, buf, 0); + fail_unless(s == NULL); + + s = ip4addr_ntoa_r(&addr, buf, 1); + fail_unless(s == NULL); + + s = ip4addr_ntoa_r(&addr, buf, 2); + fail_unless(s == NULL); +} +END_TEST + +START_TEST(test_ip4_addr_netmask_valid) +{ + u8_t ok; + LWIP_UNUSED_ARG(_i); + + ok = ip4_addr_netmask_valid(PP_HTONL(0x00000000UL)); + fail_unless(ok == 0); + + ok = ip4_addr_netmask_valid(PP_HTONL(0xffffffffUL)); + fail_unless(ok == 1); + + ok = ip4_addr_netmask_valid(PP_HTONL(0x80000000UL)); + fail_unless(ok == 1); + + ok = ip4_addr_netmask_valid(PP_HTONL(0x80000001UL)); + fail_unless(ok == 0); } END_TEST @@ -285,8 +501,6 @@ START_TEST(test_ip4_icmp_replylen_short) const int icmp_len = IP_HLEN + sizeof(struct icmp_hdr); LWIP_UNUSED_ARG(_i); - linkoutput_ctr = 0; - test_netif_add(); test_netif.output = arpless_output; p = pbuf_alloc(PBUF_IP, sizeof(unknown_proto), PBUF_RAM); @@ -313,8 +527,6 @@ START_TEST(test_ip4_icmp_replylen_first_8) const int unreach_len = IP_HLEN + 8; LWIP_UNUSED_ARG(_i); - linkoutput_ctr = 0; - test_netif_add(); test_netif.output = arpless_output; p = pbuf_alloc(PBUF_IP, sizeof(unknown_proto), PBUF_RAM); @@ -335,7 +547,10 @@ ip4_suite(void) TESTFUNC(test_ip4_frag), TESTFUNC(test_ip4_reass), TESTFUNC(test_127_0_0_1), + TESTFUNC(test_ipaddr_addr), TESTFUNC(test_ip4addr_aton), + TESTFUNC(test_ip4addr_ntoa), + TESTFUNC(test_ip4_addr_netmask_valid), TESTFUNC(test_ip4_icmp_replylen_short), TESTFUNC(test_ip4_icmp_replylen_first_8), }; diff --git a/test/unit/udp/test_udp.c b/test/unit/udp/test_udp.c index a9a6b25b5..0ed60509a 100644 --- a/test/unit/udp/test_udp.c +++ b/test/unit/udp/test_udp.c @@ -199,6 +199,7 @@ test_udp_create_test_packet(u16_t length, u16_t port, u32_t dst_addr) fail_unless(!ret); ih = (struct ip_hdr *)p->payload; memset(ih, 0, sizeof(*ih)); + ih->src.addr = 0x08080808; ih->dest.addr = dst_addr; ih->_len = lwip_htons(p->tot_len); ih->_ttl = 32; @@ -350,16 +351,24 @@ START_TEST(test_udp_bind) ip_addr_set_any_val(1, ip2); pcb1 = udp_new_ip_type(IPADDR_TYPE_V4); +#if LWIP_IPV6 pcb2 = udp_new_ip_type(IPADDR_TYPE_V6); +#endif err1 = udp_bind(pcb1, &ip1, 2105); +#if LWIP_IPV6 err2 = udp_bind(pcb2, &ip2, 2105); +#endif fail_unless(err1 == ERR_OK); +#if LWIP_IPV6 fail_unless(err2 == ERR_OK); +#endif udp_remove(pcb1); +#if LWIP_IPV6 udp_remove(pcb2); +#endif /* bind on same port using SAME IPv4 address type */ ip_addr_set_any_val(0, ip1); @@ -395,41 +404,65 @@ START_TEST(test_udp_bind) /* Bind with different IP address type */ ip_addr_set_any_val(0, ip1); +#if LWIP_IPV6 ip_addr_set_any_val(1, ip2); +#endif +#if LWIP_IPV6 pcb1 = udp_new_ip_type(IPADDR_TYPE_V6); +#endif pcb2 = udp_new_ip_type(IPADDR_TYPE_V4); +#if LWIP_IPV6 err1 = udp_bind(pcb1, &ip1, 2105); +#endif err2 = udp_bind(pcb2, &ip2, 2105); +#if LWIP_IPV6 fail_unless(err1 == ERR_OK); +#endif fail_unless(err2 == ERR_OK); +#if LWIP_IPV6 udp_remove(pcb1); +#endif udp_remove(pcb2); /* Bind with different IP numbers */ +#if LWIP_IPV6 IP_ADDR4(&ip1, 1, 2, 3, 4); +#endif IP_ADDR4(&ip2, 4, 3, 2, 1); +#if LWIP_IPV6 pcb1 = udp_new_ip_type(IPADDR_TYPE_V6); +#endif pcb2 = udp_new_ip_type(IPADDR_TYPE_V4); +#if LWIP_IPV6 err1 = udp_bind(pcb1, &ip1, 2105); +#endif err2 = udp_bind(pcb2, &ip2, 2105); +#if LWIP_IPV6 fail_unless(err1 == ERR_OK); +#endif fail_unless(err2 == ERR_OK); +#if LWIP_IPV6 udp_remove(pcb1); +#endif udp_remove(pcb2); /* Bind with same IP numbers */ IP_ADDR4(&ip1, 1, 2, 3, 4); IP_ADDR4(&ip2, 1, 2, 3, 4); +#if LWIP_IPV6 pcb1 = udp_new_ip_type(IPADDR_TYPE_V6); +#else + pcb1 = udp_new_ip_type(IPADDR_TYPE_V4); +#endif pcb2 = udp_new_ip_type(IPADDR_TYPE_V4); err1 = udp_bind(pcb1, &ip1, 2105);