aboutsummaryrefslogtreecommitdiff
path: root/src/regex
diff options
context:
space:
mode:
authorMaximilian Szengel <gnunet@maxsz.de>2012-09-27 13:18:09 +0000
committerMaximilian Szengel <gnunet@maxsz.de>2012-09-27 13:18:09 +0000
commit8323699ba0ce8915d9251d687c825955144161f5 (patch)
tree9f7ec68f2799f14e2ea4cb49fc0f54eecf4b3122 /src/regex
parent5fb7998799e47ea2ff281b237024cde9a6fc1453 (diff)
downloadgnunet-8323699ba0ce8915d9251d687c825955144161f5.tar.gz
gnunet-8323699ba0ce8915d9251d687c825955144161f5.zip
iptoregex test
Diffstat (limited to 'src/regex')
-rw-r--r--src/regex/Makefile.am7
-rw-r--r--src/regex/regex.c6
-rw-r--r--src/regex/test_regex_iptoregex.c98
3 files changed, 107 insertions, 4 deletions
diff --git a/src/regex/Makefile.am b/src/regex/Makefile.am
index cd55d755e..ca7e52265 100644
--- a/src/regex/Makefile.am
+++ b/src/regex/Makefile.am
@@ -23,7 +23,8 @@ check_PROGRAMS = \
23 test_regex_eval_api \ 23 test_regex_eval_api \
24 test_regex_iterate_api \ 24 test_regex_iterate_api \
25 test_regex_proofs \ 25 test_regex_proofs \
26 test_regex_graph_api 26 test_regex_graph_api \
27 test_regex_iptoregex
27 28
28if ENABLE_TEST_RUN 29if ENABLE_TEST_RUN
29TESTS = $(check_PROGRAMS) 30TESTS = $(check_PROGRAMS)
@@ -53,6 +54,10 @@ test_regex_graph_api_LDADD = \
53$(top_builddir)/src/regex/libgnunetregex.la \ 54$(top_builddir)/src/regex/libgnunetregex.la \
54$(top_builddir)/src/util/libgnunetutil.la 55$(top_builddir)/src/util/libgnunetutil.la
55 56
57test_regex_iptoregex_SOURCES = \
58test_regex_iptoregex.c
59test_regex_iptoregex_LDADD = \
60$(top_builddir)/src/regex/libgnunetregex.la
56 61
57EXTRA_DIST = 62EXTRA_DIST =
58# test_regex_data.conf 63# test_regex_data.conf
diff --git a/src/regex/regex.c b/src/regex/regex.c
index 676685395..c92726096 100644
--- a/src/regex/regex.c
+++ b/src/regex/regex.c
@@ -3006,7 +3006,7 @@ ipv4netmasktoprefixlen (const char *netmask)
3006 if (1 != inet_pton (AF_INET, netmask, &a)) 3006 if (1 != inet_pton (AF_INET, netmask, &a))
3007 return 0; 3007 return 0;
3008 len = 32; 3008 len = 32;
3009 for (t = htonl (~a.s_addr); 0 != t; t >>= 1) 3009 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3010 len--; 3010 len--;
3011 return len; 3011 return len;
3012} 3012}
@@ -3043,8 +3043,8 @@ GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3043 * bytes long. 3043 * bytes long.
3044 */ 3044 */
3045void 3045void
3046GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, 3046GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3047 unsigned int prefixlen, char *rxstr) 3047 char *rxstr)
3048{ 3048{
3049 iptobinstr (AF_INET6, ipv6, rxstr); 3049 iptobinstr (AF_INET6, ipv6, rxstr);
3050 rxstr[prefixlen] = '\0'; 3050 rxstr[prefixlen] = '\0';
diff --git a/src/regex/test_regex_iptoregex.c b/src/regex/test_regex_iptoregex.c
new file mode 100644
index 000000000..7fe1c384f
--- /dev/null
+++ b/src/regex/test_regex_iptoregex.c
@@ -0,0 +1,98 @@
1/*
2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file regex/test_regex_iptoregex.c
22 * @brief simple test for regex.c iptoregex functions
23 * @author Maximilian Szengel
24 */
25#include "platform.h"
26#include "gnunet_regex_lib.h"
27
28
29static int
30test_iptoregex (const char *ipv4, const char *netmask, const char *expectedv4,
31 const char *ipv6, unsigned int prefixlen,
32 const char *expectedv6)
33{
34 int error = 0;
35
36 struct in_addr a;
37 struct in6_addr b;
38 char rxv4[GNUNET_REGEX_IPV4_REGEXLEN];
39 char rxv6[GNUNET_REGEX_IPV6_REGEXLEN];
40
41 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
42 GNUNET_REGEX_ipv4toregex (&a, netmask, rxv4);
43
44
45 if (0 != strcmp (rxv4, expectedv4))
46 {
47 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n",
48 expectedv4, rxv4);
49 error++;
50 }
51
52 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
53 GNUNET_REGEX_ipv6toregex (&b, prefixlen, rxv6);
54
55 if (0 != strcmp (rxv6, expectedv6))
56 {
57 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n",
58 expectedv6, rxv6);
59 error++;
60 }
61
62 return error;
63}
64
65int
66main (int argc, char *argv[])
67{
68 GNUNET_log_setup ("test-regex",
69#if VERBOSE
70 "DEBUG",
71#else
72 "WARNING",
73#endif
74 NULL);
75
76 int error;
77
78 error = 0;
79
80 error +=
81 test_iptoregex ("192.0.0.0", "255.255.255.0",
82 "110000000000000000000000(0|1)+", "FFFF::0", 16,
83 "1111111111111111(0|1)+");
84
85 error +=
86 test_iptoregex ("255.255.255.255", "255.255.255.255",
87 "11111111111111111111111111111111",
88 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 128,
89 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
90
91 error +=
92 test_iptoregex ("0.0.0.0", "255.255.255.255",
93 "00000000000000000000000000000000", "0::0", 128,
94 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
95
96
97 return error;
98}