diff options
author | Martin Ukrop <mukrop@redhat.com> | 2016-08-03 19:40:55 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-08-09 10:49:27 +0200 |
commit | b590e04be15d9a8e872716a89c8d8eb3fc5f35b1 (patch) | |
tree | 9f0d489bcaca5528f8aaece83e6ed0b496d1d3a5 /tests | |
parent | 99019fb45bab67ed7ca7289fd955edfdd292594c (diff) | |
download | gnutls-b590e04be15d9a8e872716a89c8d8eb3fc5f35b1.tar.gz |
tests: Add more IP conversion unit tests
- Renamed ip-in-cidr test to ip-utils.
- Added built binary to .gitignore.
- Added new tests for gnutls_x509_cidr_to_rfc5280.
Signed-off-by: Martin Ukrop <mukrop@redhat.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/ip-utils.c (renamed from tests/ip-in-cidr.c) | 66 |
2 files changed, 62 insertions, 6 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index e9d72d2a69..8a9aa2edb3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -121,7 +121,7 @@ endif if HAVE_CMOCKA LDADD += $(CMOCKA_LIBS) -ctests += dtls-sliding-window ip-in-cidr +ctests += dtls-sliding-window ip-utils endif mini_dtls_pthread_LDADD = $(LDADD) -lpthread diff --git a/tests/ip-in-cidr.c b/tests/ip-utils.c index b531bbabf1..0e164aba56 100644 --- a/tests/ip-in-cidr.c +++ b/tests/ip-utils.c @@ -1,7 +1,7 @@ /* * Copyright (C) 2016 Red Hat, Inc. * - * Author: Nikos Mavrogiannopoulos + * Authors: Nikos Mavrogiannopoulos, Martin Ukrop * * This file is part of GnuTLS. * @@ -35,9 +35,9 @@ # include <cmocka.h> # include <arpa/inet.h> -#define _gnutls_hard_log(...) -#define _gnutls_ip_to_string(...) -#define _gnutls_cidr_to_string(...) +#define _gnutls_hard_log(...) +#define _gnutls_ip_to_string(...) +#define _gnutls_cidr_to_string(...) #include "../lib/x509/ip-in-cidr.h" #define _MATCH_FUNC(fname, CIDR, IP, status) \ @@ -48,7 +48,7 @@ static void fname(void **glob_state) \ const char ip[] = IP; \ char xip[4]; \ gnutls_datum_t dip = {(unsigned char*)xip, sizeof(xip)}; \ - assert_int_equal(gnutls_x509_cidr_to_rfc5280format(cidr, &dcidr), 0); \ + assert_int_equal(gnutls_x509_cidr_to_rfc5280(cidr, &dcidr), 0); \ assert_int_equal(inet_pton(AF_INET, ip, xip), 1); \ assert_int_equal(ip_in_cidr(&dip, &dcidr), status); \ gnutls_free(dcidr.data); \ @@ -71,6 +71,43 @@ MATCH_FUNC_NOT_OK(check_ip5_not_match, "192.168.1.0/28", "192.168.1.64"); MATCH_FUNC_NOT_OK(check_ip6_not_match, "192.168.1.0/24", "10.0.0.0"); MATCH_FUNC_NOT_OK(check_ip7_not_match, "192.168.1.0/24", "192.169.1.0"); +#define CIDR_MATCH(fname, CIDR, EXPECTED) \ +static void fname(void **glob_state) \ +{ \ + gnutls_datum_t dcidr; \ + const char cidr[] = CIDR; \ + assert_int_equal(gnutls_x509_cidr_to_rfc5280(cidr, &dcidr), 0); \ + assert_memory_equal(EXPECTED, dcidr.data, dcidr.size); \ + gnutls_free(dcidr.data); \ +} + +#define CIDR_FAIL(fname, CIDR) \ +static void fname(void **glob_state) \ +{ \ + gnutls_datum_t dcidr; \ + const char cidr[] = CIDR; \ + assert_int_not_equal(gnutls_x509_cidr_to_rfc5280(cidr, &dcidr), 0); \ +} + +CIDR_MATCH(check_cidr_ok1, "0.0.0.0/32","\x00\x00\x00\x00\xff\xff\xff\xff"); +CIDR_MATCH(check_cidr_ok2, "192.168.1.1/12", "\xc0\xa0\x00\x00\xff\xf0\x00\x00"); +CIDR_MATCH(check_cidr_ok3, "192.168.1.1/0", "\x00\x00\x00\x00\x00\x00\x00\x00"); +CIDR_MATCH(check_cidr_ok4, "::/19", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xe0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); +CIDR_MATCH(check_cidr_ok5, "::1/128", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"); +CIDR_MATCH(check_cidr_ok6, "2001:db8::/48", "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); + +CIDR_FAIL(check_cidr_fail1, "0.0.0.0/100"); +CIDR_FAIL(check_cidr_fail2, "1.2.3.4/-1"); +CIDR_FAIL(check_cidr_fail3, "1.300.3.4/-1"); +CIDR_FAIL(check_cidr_fail4, "1.2.3/-1"); +CIDR_FAIL(check_cidr_fail5, "1.2.3.4.5/-1"); +CIDR_FAIL(check_cidr_fail6, "1.2.3.4"); +CIDR_FAIL(check_cidr_fail7, ":://128"); +CIDR_FAIL(check_cidr_fail8, "192.168.1.1/"); +CIDR_FAIL(check_cidr_fail9, "192.168.1.1/33"); +CIDR_FAIL(check_cidr_fail10, "::/"); +CIDR_FAIL(check_cidr_fail11, "::/129"); + int main(void) { const struct CMUnitTest tests[] = { @@ -86,6 +123,25 @@ int main(void) cmocka_unit_test(check_ip5_not_match), cmocka_unit_test(check_ip6_not_match), cmocka_unit_test(check_ip7_not_match), + + cmocka_unit_test(check_cidr_ok1), + cmocka_unit_test(check_cidr_ok2), + cmocka_unit_test(check_cidr_ok3), + cmocka_unit_test(check_cidr_ok4), + cmocka_unit_test(check_cidr_ok5), + cmocka_unit_test(check_cidr_ok6), + + cmocka_unit_test(check_cidr_fail1), + cmocka_unit_test(check_cidr_fail2), + cmocka_unit_test(check_cidr_fail3), + cmocka_unit_test(check_cidr_fail4), + cmocka_unit_test(check_cidr_fail5), + cmocka_unit_test(check_cidr_fail6), + cmocka_unit_test(check_cidr_fail7), + cmocka_unit_test(check_cidr_fail8), + cmocka_unit_test(check_cidr_fail9), + cmocka_unit_test(check_cidr_fail10), + cmocka_unit_test(check_cidr_fail11), }; return cmocka_run_group_tests(tests, NULL, NULL); } |