diff options
author | David Lamparter <equinox@diac24.net> | 2019-06-14 21:48:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-14 21:48:34 +0200 |
commit | 277aee6fc968b3780302c9d141398e6138b54a50 (patch) | |
tree | 55205407525559071aa9d085e5d5332b1111eb5d /tests | |
parent | Merge pull request #4523 from LabNConsulting/working/master/issue4515 (diff) | |
parent | tests: add prefix2str test (diff) | |
download | frr-277aee6fc968b3780302c9d141398e6138b54a50.tar.xz frr-277aee6fc968b3780302c9d141398e6138b54a50.zip |
eliminate snprintf from AF_INET/AF_INET6 prefix2str, add prefix2str test (#4521)
eliminate snprintf from AF_INET/AF_INET6 prefix2str, add prefix2str test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/lib/test_prefix2str.c | 80 | ||||
-rw-r--r-- | tests/lib/test_prefix2str.py | 6 | ||||
-rw-r--r-- | tests/subdir.am | 6 |
4 files changed, 93 insertions, 0 deletions
diff --git a/tests/.gitignore b/tests/.gitignore index 4c6a51d47..6252cea82 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -31,6 +31,7 @@ /lib/test_memory /lib/test_nexthop_iter /lib/test_ntop +/lib/test_prefix2str /lib/test_printfrr /lib/test_privs /lib/test_ringbuf diff --git a/tests/lib/test_prefix2str.c b/tests/lib/test_prefix2str.c new file mode 100644 index 000000000..cbfc20a79 --- /dev/null +++ b/tests/lib/test_prefix2str.c @@ -0,0 +1,80 @@ +/* + * prefix2str() unit test + * Copyright (C) 2019 David Lamparter + * Portions: + * Copyright (C) 2019 Cumulus Networks, Inc + * Quentin Young + * + * 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 + */ +#include <zebra.h> + +#include "lib/prefix.h" + +#include "tests/helpers/c/prng.h" + +int main(int argc, char **argv) +{ + size_t i, j, k, l; + struct in6_addr i6; + char buf1[64], buf2[64], ntopbuf[64]; + struct prng *prng; + struct prefix p = {}; + + prng = prng_new(0); + /* IPv4 */ + p.family = AF_INET; + for (i = 0; i < 1000; i++) { + p.u.prefix = prng_rand(prng); + p.prefixlen = prng_rand(prng) >> 26; + snprintf(buf1, sizeof(buf1), "%s/%d", + inet_ntop(AF_INET, &p.u.prefix4, ntopbuf, + sizeof(ntopbuf)), + p.prefixlen); + prefix2str(&p, buf2, sizeof(buf2)); + assert(!strcmp(buf1, buf2)); + fprintf(stdout, "%s\n", buf1); + } + + /* IPv6 */ + p.family = AF_INET6; + 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; + } + + p.prefixlen = prng_rand(prng) >> 24; + memcpy(&p.u.prefix, &i6, sizeof(i6)); + snprintf(buf1, sizeof(buf1), "%s/%d", + inet_ntop(AF_INET6, &p.u.prefix6, ntopbuf, + sizeof(ntopbuf)), + p.prefixlen); + prefix2str(&p, buf2, sizeof(buf2)); + assert(!strcmp(buf1, buf2)); + fprintf(stdout, "%s\n", buf1); + } + + return 0; +} diff --git a/tests/lib/test_prefix2str.py b/tests/lib/test_prefix2str.py new file mode 100644 index 000000000..6e26d1b40 --- /dev/null +++ b/tests/lib/test_prefix2str.py @@ -0,0 +1,6 @@ +import frrtest + +class TestPrefix2str(frrtest.TestMultiOut): + program = './test_prefix2str' + +TestPrefix2str.exit_cleanly() diff --git a/tests/subdir.am b/tests/subdir.am index 41f1a4873..270c0811b 100644 --- a/tests/subdir.am +++ b/tests/subdir.am @@ -55,6 +55,7 @@ check_PROGRAMS = \ tests/lib/test_memory \ tests/lib/test_nexthop_iter \ tests/lib/test_ntop \ + tests/lib/test_prefix2str \ tests/lib/test_printfrr \ tests/lib/test_privs \ tests/lib/test_ringbuf \ @@ -236,6 +237,10 @@ 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_prefix2str_CFLAGS = $(TESTS_CFLAGS) +tests_lib_test_prefix2str_CPPFLAGS = $(TESTS_CPPFLAGS) +tests_lib_test_prefix2str_LDADD = $(ALL_TESTS_LDADD) +tests_lib_test_prefix2str_SOURCES = tests/lib/test_prefix2str.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) @@ -328,6 +333,7 @@ EXTRA_DIST += \ tests/lib/test_atomlist.py \ tests/lib/test_nexthop_iter.py \ tests/lib/test_ntop.py \ + tests/lib/test_prefix2str.py \ tests/lib/test_printfrr.py \ tests/lib/test_ringbuf.py \ tests/lib/test_srcdest_table.py \ |