All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Prestwood <prestwoj@gmail.com>
To: iwd@lists.01.org
Subject: [PATCH 2/2] unit: add test for util_ip_prefix_tohl
Date: Mon, 26 Oct 2020 13:35:14 -0700	[thread overview]
Message-ID: <20201026203514.2164608-2-prestwoj@gmail.com> (raw)
In-Reply-To: <20201026203514.2164608-1-prestwoj@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2878 bytes --]

---
 unit/test-util.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

 - Added a very long invalid IP string test
 - Added a valid IP string that is maximum length possible

diff --git a/unit/test-util.c b/unit/test-util.c
index f091f2c2..c3f8c673 100644
--- a/unit/test-util.c
+++ b/unit/test-util.c
@@ -27,6 +27,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include <ell/ell.h>
 
 #include "src/util.h"
@@ -115,6 +117,68 @@ static void get_username_test(const void *data)
 	assert(strcmp(test, "username") == 0);
 }
 
+static void ip_prefix_test(const void *data)
+{
+	unsigned int i;
+	char *invalid[] = {
+		"192.168.0.0", /* Not prefix notation */
+		"192.168./22", /* Incomplete notation */
+		"192.168.0.1/255", /* Too long prefix */
+		"192.168.0.1/0", /* Too short prefix */
+		"192.168.0.1/16", /* Invalid prefix */
+		"192.168.1.2.3/24", /* IP too long */
+		"192.168.111.222.333.444/20", /* IP way too long */
+	};
+
+	struct {
+		char *ip_prefix;
+		uint8_t prefix;
+		char *start;
+		char *end;
+		char *mask;
+	} valid[] = {
+		{"192.168.80.0/22", 22, "192.168.80.1",
+				"192.168.83.254", "255.255.252.0"},
+		{"192.168.128.0/20", 20, "192.168.128.1",
+				"192.168.143.254", "255.255.240.0"},
+		{"192.168.0.0/25", 25, "192.168.0.1",
+				"192.168.0.126", "255.255.255.128"},
+		{"192.168.0.0/29", 29, "192.168.0.1",
+				"192.168.0.6", "255.255.255.248"},
+		{"192.168.0.128/25", 25, "192.168.0.129",
+				"192.168.0.254", "255.255.255.128"},
+		/* Valid notation which is maximum length */
+		{"192.168.111.108/30", 30, "192.168.111.109",
+				"192.168.111.110", "255.255.255.252"},
+	};
+
+	for (i = 0; i < L_ARRAY_SIZE(invalid); i++)
+		assert(!util_ip_prefix_tohl(invalid[i], NULL, NULL,
+						NULL, NULL));
+
+	for (i = 0; i < L_ARRAY_SIZE(valid); i++) {
+		uint8_t prefix;
+		uint32_t start;
+		uint32_t end;
+		uint32_t mask;
+		struct in_addr ia;
+
+		assert(util_ip_prefix_tohl(valid[i].ip_prefix,
+						&prefix, &start, &end, &mask));
+
+		assert(valid[i].prefix == prefix);
+
+		ia.s_addr = htonl(start);
+		assert(strcmp(inet_ntoa(ia), valid[i].start) == 0);
+
+		ia.s_addr = htonl(end);
+		assert(strcmp(inet_ntoa(ia), valid[i].end) == 0);
+
+		ia.s_addr = htonl(mask);
+		assert(strcmp(inet_ntoa(ia), valid[i].mask) == 0);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -122,6 +186,7 @@ int main(int argc, char *argv[])
 	l_test_add("/util/ssid_to_utf8/", ssid_to_utf8, ssid_samples);
 	l_test_add("/util/get_domain/", get_domain_test, NULL);
 	l_test_add("/util/get_username/", get_username_test, NULL);
+	l_test_add("/util/ip_prefix/", ip_prefix_test, NULL);
 
 	return l_test_run();
 }
-- 
2.26.2

  reply	other threads:[~2020-10-26 20:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-26 20:35 [PATCH 1/2] util: add util_ip_prefix_tohl James Prestwood
2020-10-26 20:35 ` James Prestwood [this message]
2020-10-26 21:17 ` Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201026203514.2164608-2-prestwoj@gmail.com \
    --to=prestwoj@gmail.com \
    --cc=iwd@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.