summary refs log tree commit diff
path: root/libnm-util/tests/test-general.c
diff options
context:
space:
mode:
authorSjoerd Simons <sjoerd@luon.net>2014-04-26 20:55:09 +0200
committerSjoerd Simons <sjoerd@luon.net>2014-04-26 20:55:09 +0200
commit59c3714a494c3b3765657c0551ad82842d98a7d2 (patch)
treee4dcdf9791fc40581f02bfaf870c30b102b3133a /libnm-util/tests/test-general.c
parenta4256940da049f91bbbea5e53e469a59ea9c10f7 (diff)
Imported Upstream version 0.9.8.10 upstream/0.9.8.10
Diffstat (limited to 'libnm-util/tests/test-general.c')
-rw-r--r--libnm-util/tests/test-general.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/libnm-util/tests/test-general.c b/libnm-util/tests/test-general.c
index 9576f05a..20a99472 100644
--- a/libnm-util/tests/test-general.c
+++ b/libnm-util/tests/test-general.c
@@ -1363,6 +1363,66 @@ test_hwaddr_aton_malformed (void)
 }
 
 static void
+test_ip4_prefix_to_netmask (void)
+{
+	int i;
+
+	for (i = 0; i<=32; i++) {
+		guint32 netmask = nm_utils_ip4_prefix_to_netmask (i);
+		int plen = nm_utils_ip4_netmask_to_prefix (netmask);
+
+		g_assert_cmpint (i, ==, plen);
+		{
+			guint32 msk = 0x80000000;
+			guint32 netmask2 = 0;
+			guint32 prefix = i;
+			while (prefix > 0) {
+				netmask2 |= msk;
+				msk >>= 1;
+				prefix--;
+			}
+			g_assert_cmpint (netmask, ==, (guint32) htonl (netmask2));
+		}
+	}
+}
+
+static void
+test_ip4_netmask_to_prefix (void)
+{
+	int i, j;
+
+	GRand *rand = g_rand_new ();
+
+	g_rand_set_seed (rand, 1);
+
+	for (i = 2; i<=32; i++) {
+		guint32 netmask = nm_utils_ip4_prefix_to_netmask (i);
+		guint32 netmask_lowest_bit = netmask & ~nm_utils_ip4_prefix_to_netmask (i-1);
+
+		g_assert_cmpint (i, ==, nm_utils_ip4_netmask_to_prefix (netmask));
+
+		for (j = 0; j < 2*i; j++) {
+			guint32 r = g_rand_int (rand);
+			guint32 netmask_holey;
+			guint32 prefix_holey;
+
+			netmask_holey = (netmask & r) | netmask_lowest_bit;
+
+			if (netmask_holey == netmask)
+				continue;
+
+			/* create an invalid netmask with holes and check that the function
+			 * returns the longest prefix. */
+			prefix_holey = nm_utils_ip4_netmask_to_prefix (netmask_holey);
+
+			g_assert_cmpint (i, ==, prefix_holey);
+		}
+	}
+
+	g_rand_free (rand);
+}
+
+static void
 test_setting_old_uuid (void)
 {
 	GError *error = NULL;
@@ -1432,6 +1492,8 @@ int main (int argc, char **argv)
 	test_hwaddr_aton_ib_normal ();
 	test_hwaddr_aton_no_leading_zeros ();
 	test_hwaddr_aton_malformed ();
+	test_ip4_prefix_to_netmask ();
+	test_ip4_netmask_to_prefix ();
 
 	base = g_path_get_basename (argv[0]);
 	fprintf (stdout, "%s: SUCCESS\n", base);