[ip-parser] fix ipv4 netmask for /0, added some unit tests

personal/stbuehler/wip
Stefan Bühler 11 years ago
parent 34ad0a52e4
commit 3972451633
  1. 5
      src/CMakeLists.txt
  2. 2
      src/common/ip_parsers.rl
  3. 2
      src/unittests/Makefile.am
  4. 81
      src/unittests/test-ip-parser.c

@ -446,9 +446,10 @@ IF(BUILD_UNIT_TESTS)
ADD_TEST(${TESTNAME} ${EXENAME})
ENDMACRO(ADD_TEST_BINARY)
ADD_TEST_BINARY(Utils-UnitTest test-utils unittests/test-utils.c)
ADD_TEST_BINARY(Chunk-UnitTest test-chunk unittests/test-chunk.c)
ADD_TEST_BINARY(RangeParser-UnitTest test-range-parser unittests/test-range-parser.c)
ADD_TEST_BINARY(IpParser-UnitTest test-ip-parser unittests/test-ip-parser.c)
ADD_TEST_BINARY(Radix-UnitTest test-radix unittests/test-radix.c)
ADD_TEST_BINARY(RangeParser-UnitTest test-range-parser unittests/test-range-parser.c)
ADD_TEST_BINARY(Utils-UnitTest test-utils unittests/test-utils.c)
ENDIF(BUILD_UNIT_TESTS)

@ -15,7 +15,7 @@
ipv4_data = octet "." octet "." octet "." octet;
netmask = "/" decint %{
if (tmpval > 32) { res = FALSE; fbreak; }
*netmask = htonl(~((1 << (32-tmpval)) - 1));
*netmask = htonl(tmpval ? ~((1 << (32-tmpval)) - 1) : 0);
};
port = ":" decint %{
if (tmpval > 65535) { res = FALSE; fbreak; }

@ -5,7 +5,7 @@ AM_CFLAGS += $(GTHREAD_CFLAGS) $(GMODULE_CFLAGS) $(LIBEV_CFLAGS) $(LUA_CFLAGS)
AM_LDFLAGS = -export-dynamic -avoid-version -no-undefined $(GTHREAD_LIBS) $(GMODULE_LIBS) $(LIBEV_LIBS) $(LUA_LIBS)
LDADD = ../common/liblighttpd2-common.la ../main/liblighttpd2-shared.la
test_binaries=test-chunk test-range-parser test-utils test-radix
test_binaries=test-chunk test-ip-parser test-range-parser test-utils test-radix
check_PROGRAMS=$(test_binaries)

@ -0,0 +1,81 @@
#include <lighttpd/base.h>
#define perror(msg) g_error("(%s:%i) %s failed: %s", __FILE__, __LINE__, msg, g_strerror(errno))
typedef struct {
struct {
guint32 addr;
guint32 networkmask;
guint16 port;
} ipv4;
struct {
guint8 addr[16];
guint network;
guint16 port;
} ipv6;
struct {
GString *path;
} unix_socket;
} netrange;
static void test_ipv4_net1(void) {
netrange range;
liSocketAddress addr;
const char str0[] = "0.0.0.0/0:80";
GString str1 = li_const_gstring(CONST_STR_LEN("127.0.0.1"));
struct sockaddr_in *ipv4;
g_assert(!li_parse_ipv6(str0, range.ipv6.addr, &range.ipv6.network, &range.ipv6.port));
g_assert(li_parse_ipv4(str0, &range.ipv4.addr, &range.ipv4.networkmask, &range.ipv4.port));
g_assert_cmpuint(range.ipv4.addr, ==, 0);
g_assert_cmpuint(range.ipv4.networkmask, ==, 0);
g_assert_cmpuint(range.ipv4.port, ==, 80);
addr = li_sockaddr_from_string(&str1, 80);
g_assert(addr.addr);
ipv4 = &addr.addr->ipv4;
g_assert_cmpuint(ipv4->sin_addr.s_addr, ==, htonl(0x7f000001u));
g_assert(li_ipv4_in_ipv4_net(ipv4->sin_addr.s_addr, range.ipv4.addr, range.ipv4.networkmask));
g_assert_cmpuint(ipv4->sin_port, ==, htons(range.ipv4.port));
li_sockaddr_clear(&addr);
}
static void test_ipv6_net1(void) {
netrange range;
liSocketAddress addr;
const char str0[] = "[::/0]:80";
GString str1 = li_const_gstring(CONST_STR_LEN("::1"));
struct sockaddr_in6 *ipv6;
g_assert(!li_parse_ipv4(str0, &range.ipv4.addr, &range.ipv4.networkmask, &range.ipv4.port));
g_assert(li_parse_ipv6(str0, range.ipv6.addr, &range.ipv6.network, &range.ipv6.port));
g_assert_cmpuint(range.ipv6.network, ==, 0);
g_assert_cmpuint(range.ipv6.port, ==, 80);
addr = li_sockaddr_from_string(&str1, 80);
g_assert(addr.addr);
ipv6 = &addr.addr->ipv6;
g_assert(li_ipv6_in_ipv6_net(ipv6->sin6_addr.s6_addr, range.ipv6.addr, range.ipv6.network));
g_assert_cmpuint(ipv6->sin6_port, ==, htons(range.ipv6.port));
li_sockaddr_clear(&addr);
}
int main(int argc, char **argv) {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/ip-parser/test-localhost-in-all-ipv4-net", test_ipv4_net1);
g_test_add_func("/ip-parser/test-localhost-in-all-ipv6-net", test_ipv6_net1);
return g_test_run();
}
Loading…
Cancel
Save