Skip to content
Browse files

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

  • Loading branch information...
1 parent 34ad0a5 commit 3972451633a5cecfe04012e84c9ae932eeadedae @stbuehler stbuehler committed
Showing with 86 additions and 4 deletions.
  1. +3 −2 src/CMakeLists.txt
  2. +1 −1 src/common/ip_parsers.rl
  3. +1 −1 src/unittests/Makefile.am
  4. +81 −0 src/unittests/test-ip-parser.c
View
5 src/CMakeLists.txt
@@ -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)
View
2 src/common/ip_parsers.rl
@@ -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; }
View
2 src/unittests/Makefile.am
@@ -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)
View
81 src/unittests/test-ip-parser.c
@@ -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();
+}

0 comments on commit 3972451

Please sign in to comment.
Something went wrong with that request. Please try again.