]> git.proxmox.com Git - mirror_frr.git/commitdiff
tests: exercise frr_inet_ntop()
authorDavid Lamparter <equinox@diac24.net>
Thu, 6 Jun 2019 17:35:03 +0000 (19:35 +0200)
committerDavid Lamparter <equinox@diac24.net>
Thu, 6 Jun 2019 18:59:54 +0000 (20:59 +0200)
Signed-off-by: David Lamparter <equinox@diac24.net>
lib/ntop.c
tests/.gitignore
tests/lib/test_ntop.c [new file with mode: 0644]
tests/lib/test_ntop.py [new file with mode: 0644]
tests/subdir.am

index 5500f988e036beb11c1fb3407ebf95f28cb85fb9..d47a0b697aa699dde6e3e080c6889cc35126a003 100644 (file)
@@ -165,8 +165,10 @@ inet4:
        return dst;
 }
 
+#ifndef INET_NTOP_NO_OVERRIDE
 /* we want to override libc inet_ntop, but make sure it shows up in backtraces
  * as frr_inet_ntop (to avoid confusion while debugging)
  */
 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
        __attribute__((alias ("frr_inet_ntop"))) DSO_SELF;
+#endif
index 7177165e4a2fdafe7bf5f925edeb83dc7b80443d..4c6a51d475f4af4ff3d0d177a60bc334d16f251c 100644 (file)
@@ -30,6 +30,7 @@
 /lib/test_idalloc
 /lib/test_memory
 /lib/test_nexthop_iter
+/lib/test_ntop
 /lib/test_printfrr
 /lib/test_privs
 /lib/test_ringbuf
diff --git a/tests/lib/test_ntop.c b/tests/lib/test_ntop.c
new file mode 100644 (file)
index 0000000..1806059
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * frr_inet_ntop() unit test
+ * Copyright (C) 2019  David Lamparter
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <assert.h>
+
+#include "tests/helpers/c/prng.h"
+
+/* NB: libfrr is NOT linked for this unit test! */
+
+#define INET_NTOP_NO_OVERRIDE
+#include "lib/ntop.c"
+
+int main(int argc, char **argv)
+{
+       size_t i, j, k, l;
+       struct in_addr i4;
+       struct in6_addr i6, i6check;
+       char buf1[64], buf2[64];
+       const char *rv;
+       struct prng *prng;
+
+       prng = prng_new(0);
+       /* IPv4 */
+       for (i = 0; i < 1000; i++) {
+               i4.s_addr = prng_rand(prng);
+               assert(frr_inet_ntop(AF_INET, &i4, buf1, sizeof(buf1)));
+               assert(inet_ntop(AF_INET, &i4, buf2, sizeof(buf2)));
+               assert(!strcmp(buf1, buf2));
+       }
+
+       /* check size limit */
+       for (i = 0; i < sizeof(buf1); i++) {
+               memset(buf2, 0xcc, sizeof(buf2));
+               rv = frr_inet_ntop(AF_INET, &i4, buf2, i);
+               if (i < strlen(buf1) + 1)
+                       assert(!rv);
+               else
+                       assert(rv && !strcmp(buf1, buf2));
+       }
+
+       /* IPv6 */
+       for (i = 0; i < 10000; i++) {
+               uint16_t *i6w = (uint16_t *)&i6;
+               for (j = 0; j < 8; j++)
+                       i6w[j] = prng_rand(prng);
+
+               /* clear some words */
+               l = prng_rand(prng) & 7;
+               for (j = 0; j < l; j++) {
+                       uint32_t num = __builtin_ctz(prng_rand(prng));
+                       uint32_t where = prng_rand(prng) & 7;
+
+                       for (k = where; k < where + num && k < 8; k++)
+                               i6w[k] = 0;
+               }
+
+               assert(frr_inet_ntop(AF_INET6, &i6, buf1, sizeof(buf1)));
+               assert(inet_ntop(AF_INET6, &i6, buf2, sizeof(buf2)));
+               if (strcmp(buf1, buf2))
+                       printf("%-40s (FRR) != (SYS) %-40s\n", buf1, buf2);
+
+               assert(inet_pton(AF_INET6, buf1, &i6check));
+               assert(!memcmp(&i6, &i6check, sizeof(i6)));
+               assert(strlen(buf1) <= strlen(buf2));
+       }
+       return 0;
+}
diff --git a/tests/lib/test_ntop.py b/tests/lib/test_ntop.py
new file mode 100644 (file)
index 0000000..2526f53
--- /dev/null
@@ -0,0 +1,6 @@
+import frrtest
+
+class TestNtop(frrtest.TestMultiOut):
+    program = './test_ntop'
+
+TestNtop.exit_cleanly()
index bbab8cd86a777189791e66eac48039a2bd3f8e06..99f5f87facbf2af4f50f8fdbfe34d40e31bf4699 100644 (file)
@@ -56,6 +56,7 @@ check_PROGRAMS = \
        tests/lib/test_idalloc \
        tests/lib/test_memory \
        tests/lib/test_nexthop_iter \
+       tests/lib/test_ntop \
        tests/lib/test_printfrr \
        tests/lib/test_privs \
        tests/lib/test_ringbuf \
@@ -233,6 +234,10 @@ tests_lib_test_nexthop_iter_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_nexthop_iter_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_nexthop_iter_LDADD = $(ALL_TESTS_LDADD)
 tests_lib_test_nexthop_iter_SOURCES = tests/lib/test_nexthop_iter.c tests/helpers/c/prng.c
+tests_lib_test_ntop_CFLAGS = $(TESTS_CFLAGS)
+tests_lib_test_ntop_CPPFLAGS = $(TESTS_CPPFLAGS)
+tests_lib_test_ntop_LDADD = # none
+tests_lib_test_ntop_SOURCES = tests/lib/test_ntop.c tests/helpers/c/prng.c
 tests_lib_test_printfrr_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_printfrr_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_printfrr_LDADD = $(ALL_TESTS_LDADD)
@@ -324,6 +329,7 @@ EXTRA_DIST += \
        tests/lib/northbound/test_oper_data.refout \
        tests/lib/test_atomlist.py \
        tests/lib/test_nexthop_iter.py \
+       tests/lib/test_ntop.py \
        tests/lib/test_printfrr.py \
        tests/lib/test_ringbuf.py \
        tests/lib/test_srcdest_table.py \