gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r24054 - in gnunet/src: include regex


From: gnunet
Subject: [GNUnet-SVN] r24054 - in gnunet/src: include regex
Date: Thu, 27 Sep 2012 15:18:11 +0200

Author: szengel
Date: 2012-09-27 15:18:09 +0200 (Thu, 27 Sep 2012)
New Revision: 24054

Added:
   gnunet/src/regex/test_regex_iptoregex.c
Modified:
   gnunet/src/include/gnunet_regex_lib.h
   gnunet/src/regex/Makefile.am
   gnunet/src/regex/regex.c
Log:
iptoregex test


Modified: gnunet/src/include/gnunet_regex_lib.h
===================================================================
--- gnunet/src/include/gnunet_regex_lib.h       2012-09-27 13:06:42 UTC (rev 
24053)
+++ gnunet/src/include/gnunet_regex_lib.h       2012-09-27 13:18:09 UTC (rev 
24054)
@@ -37,7 +37,20 @@
 #endif
 #endif
 
+
 /**
+ * Maximum regex string length for use with GNUNET_REGEX_ipv4toregex
+ */
+#define GNUNET_REGEX_IPV4_REGEXLEN 32 + 6
+
+
+/**
+ * Maximum regex string length for use with GNUNET_REGEX_ipv6toregex
+ */
+#define GNUNET_REGEX_IPV6_REGEXLEN 128 + 6
+
+
+/**
  * Automaton (NFA/DFA) representation.
  */
 struct GNUNET_REGEX_Automaton;
@@ -206,6 +219,32 @@
                                 void *iterator_cls);
 
 
+/**
+ * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
+ *
+ * @param ip IPv4 representation.
+ * @param netmask netmask for the ip.
+ * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
+ *              bytes long.
+ */
+void
+GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
+                          char *rxstr);
+
+
+/**
+ * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
+ *
+ * @param ipv6 IPv6 representation.
+ * @param prefixlen length of the ipv6 prefix.
+ * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
+ *              bytes long.
+ */
+void
+GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6,
+                          unsigned int prefixlen, char *rxstr);
+
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif

Modified: gnunet/src/regex/Makefile.am
===================================================================
--- gnunet/src/regex/Makefile.am        2012-09-27 13:06:42 UTC (rev 24053)
+++ gnunet/src/regex/Makefile.am        2012-09-27 13:18:09 UTC (rev 24054)
@@ -23,7 +23,8 @@
  test_regex_eval_api \
  test_regex_iterate_api \
  test_regex_proofs \
- test_regex_graph_api
+ test_regex_graph_api \
+ test_regex_iptoregex
 
 if ENABLE_TEST_RUN
 TESTS = $(check_PROGRAMS)
@@ -53,6 +54,10 @@
 $(top_builddir)/src/regex/libgnunetregex.la \
 $(top_builddir)/src/util/libgnunetutil.la
 
+test_regex_iptoregex_SOURCES = \
+test_regex_iptoregex.c
+test_regex_iptoregex_LDADD = \
+$(top_builddir)/src/regex/libgnunetregex.la
 
 EXTRA_DIST =
 # test_regex_data.conf

Modified: gnunet/src/regex/regex.c
===================================================================
--- gnunet/src/regex/regex.c    2012-09-27 13:06:42 UTC (rev 24053)
+++ gnunet/src/regex/regex.c    2012-09-27 13:18:09 UTC (rev 24054)
@@ -3006,7 +3006,7 @@
   if (1 != inet_pton (AF_INET, netmask, &a))
     return 0;
   len = 32;
-  for (t = htonl (~a.s_addr); 0 != t; t >>= 1) 
+  for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
     len--;
   return len;
 }
@@ -3043,8 +3043,8 @@
  *              bytes long.
  */
 void
-GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6,
-                         unsigned int prefixlen, char *rxstr)
+GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
+                          char *rxstr)
 {
   iptobinstr (AF_INET6, ipv6, rxstr);
   rxstr[prefixlen] = '\0';

Added: gnunet/src/regex/test_regex_iptoregex.c
===================================================================
--- gnunet/src/regex/test_regex_iptoregex.c                             (rev 0)
+++ gnunet/src/regex/test_regex_iptoregex.c     2012-09-27 13:18:09 UTC (rev 
24054)
@@ -0,0 +1,98 @@
+/*
+     This file is part of GNUnet
+     (C) 2012 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+/**
+ * @file regex/test_regex_iptoregex.c
+ * @brief simple test for regex.c iptoregex functions
+ * @author Maximilian Szengel
+ */
+#include "platform.h"
+#include "gnunet_regex_lib.h"
+
+
+static int
+test_iptoregex (const char *ipv4, const char *netmask, const char *expectedv4,
+                const char *ipv6, unsigned int prefixlen,
+                const char *expectedv6)
+{
+  int error = 0;
+
+  struct in_addr a;
+  struct in6_addr b;
+  char rxv4[GNUNET_REGEX_IPV4_REGEXLEN];
+  char rxv6[GNUNET_REGEX_IPV6_REGEXLEN];
+
+  GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
+  GNUNET_REGEX_ipv4toregex (&a, netmask, rxv4);
+
+
+  if (0 != strcmp (rxv4, expectedv4))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n",
+                expectedv4, rxv4);
+    error++;
+  }
+
+  GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
+  GNUNET_REGEX_ipv6toregex (&b, prefixlen, rxv6);
+
+  if (0 != strcmp (rxv6, expectedv6))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n",
+                expectedv6, rxv6);
+    error++;
+  }
+
+  return error;
+}
+
+int
+main (int argc, char *argv[])
+{
+  GNUNET_log_setup ("test-regex",
+#if VERBOSE
+                    "DEBUG",
+#else
+                    "WARNING",
+#endif
+                    NULL);
+
+  int error;
+
+  error = 0;
+
+  error +=
+      test_iptoregex ("192.0.0.0", "255.255.255.0",
+                      "110000000000000000000000(0|1)+", "FFFF::0", 16,
+                      "1111111111111111(0|1)+");
+
+  error +=
+      test_iptoregex ("255.255.255.255", "255.255.255.255",
+                      "11111111111111111111111111111111",
+                      "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 128,
+                      
"11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
+
+  error +=
+      test_iptoregex ("0.0.0.0", "255.255.255.255",
+                      "00000000000000000000000000000000", "0::0", 128,
+                      
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
+
+
+  return error;
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]