Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/core/ipv4/ip4_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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));
Expand Down
91 changes: 90 additions & 1 deletion test/unit/core/test_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
Loading
Loading