changeset 19535:60e922d0b318

lib: Added unit tests for net_is_in_network() about IPv4-mapped IPv6 addresses
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Mon, 04 Jan 2016 14:10:44 -0500
parents 25e892203e2b
children c4f960341b0c
files src/lib/net.h src/lib/test-net.c
diffstat 2 files changed, 9 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/net.h	Mon Jan 04 14:00:19 2016 -0500
+++ b/src/lib/net.h	Mon Jan 04 14:10:44 2016 -0500
@@ -170,9 +170,10 @@
 /* Parse network as ip/bits. Returns 0 if successful, -1 if invalid input. */
 int net_parse_range(const char *network, struct ip_addr *ip_r,
 		    unsigned int *bits_r);
-/* Returns TRUE if ip is in net_ip/bits network. IPv6 mapped IPv4 addresses
-   are converted to plain IPv4 addresses before matching. Invalid IPs
-   (family=0) never match anything. */
+/* Returns TRUE if ip is in net_ip/bits network. IPv4-mapped IPv6 addresses
+   in "ip" parameter are converted to plain IPv4 addresses before matching.
+   No conversion is done to net_ip though, so using IPv4-mapped IPv6 addresses
+   there will always fail. Invalid IPs (family=0) never match anything. */
 bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip,
 		       unsigned int bits) ATTR_PURE;
 
--- a/src/lib/test-net.c	Mon Jan 04 14:00:19 2016 -0500
+++ b/src/lib/test-net.c	Mon Jan 04 14:10:44 2016 -0500
@@ -31,20 +31,20 @@
 		{ "1234:5678::abcf", "1234:5678::abce", 127, TRUE },
 		{ "1234:5678::abcd", "1234:5678::abce", 127, FALSE },
 		{ "123e::ffff", "123e::0", 15, TRUE },
-		{ "123d::ffff", "123e::0", 15, FALSE }
+		{ "::ffff:1.2.3.4", "1.2.3.4", 32, TRUE },
+		{ "::ffff:1.2.3.4", "1.2.3.3", 32, FALSE },
+		{ "::ffff:1.2.3.4", "::ffff:1.2.3.4", 0, FALSE }
 #endif
 	};
 	struct ip_addr ip, net_ip;
 	unsigned int i;
-	bool success;
 
 	test_begin("net_is_in_network()");
 	for (i = 0; i < N_ELEMENTS(input); i++) {
 		test_assert(net_addr2ip(input[i].ip, &ip) == 0);
 		test_assert(net_addr2ip(input[i].net, &net_ip) == 0);
-		success = net_is_in_network(&ip, &net_ip, input[i].bits) ==
-			input[i].ret;
-		test_out(t_strdup_printf("net_is_in_network(%u)", i), success);
+		test_assert_idx(net_is_in_network(&ip, &net_ip, input[i].bits) ==
+				input[i].ret, i);
 	}
 	/* make sure non-IPv4 and non-IPv6 ip_addrs fail */
 	test_assert(net_addr2ip("127.0.0.1", &ip) == 0);