test_regex.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2012, 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file tun/test_regex.c
  18. * @brief simple test for regex.c iptoregex functions
  19. * @author Maximilian Szengel
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. /**
  24. * 'wildcard', matches all possible values (for HEX encoding).
  25. */
  26. #define DOT "(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"
  27. static int
  28. test_iptoregex (const char *ipv4,
  29. uint16_t port,
  30. const char *expectedv4,
  31. const char *ipv6,
  32. uint16_t port6,
  33. const char *expectedv6)
  34. {
  35. int error = 0;
  36. struct in_addr a;
  37. struct in6_addr b;
  38. char rxv4[GNUNET_TUN_IPV4_REGEXLEN];
  39. char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
  40. GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
  41. GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
  42. if (0 != strcmp (rxv4, expectedv4))
  43. {
  44. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  45. "Expected: %s but got: %s\n",
  46. expectedv4,
  47. rxv4);
  48. error++;
  49. }
  50. GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
  51. GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
  52. if (0 != strcmp (rxv6, expectedv6))
  53. {
  54. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  55. "Expected: %s but got: %s\n",
  56. expectedv6, rxv6);
  57. error++;
  58. }
  59. return error;
  60. }
  61. static int
  62. test_policy4toregex (const char *policy,
  63. const char *regex)
  64. {
  65. char *r;
  66. int ret;
  67. ret = 0;
  68. r = GNUNET_TUN_ipv4policy2regex (policy);
  69. if (NULL == r)
  70. {
  71. GNUNET_break (0);
  72. return 1;
  73. }
  74. if (0 != strcmp (regex, r))
  75. {
  76. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  77. "Expected: `%s' but got: `%s'\n",
  78. regex, r);
  79. ret = 2;
  80. }
  81. GNUNET_free (r);
  82. return ret;
  83. }
  84. static int
  85. test_policy6toregex (const char *policy,
  86. const char *regex)
  87. {
  88. char *r;
  89. int ret;
  90. ret = 0;
  91. r = GNUNET_TUN_ipv6policy2regex (policy);
  92. if (NULL == r)
  93. {
  94. GNUNET_break (0);
  95. return 1;
  96. }
  97. if (0 != strcmp (regex, r))
  98. {
  99. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  100. "Expected: `%s' but got: `%s'\n",
  101. regex, r);
  102. ret = 2;
  103. }
  104. GNUNET_free (r);
  105. return ret;
  106. }
  107. int
  108. main (int argc, char *argv[])
  109. {
  110. int error;
  111. char *r;
  112. GNUNET_log_setup ("test-regex", "WARNING", NULL);
  113. error = 0;
  114. /* this is just a performance test ... */
  115. r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
  116. GNUNET_break (NULL != r);
  117. GNUNET_free (r);
  118. error +=
  119. test_iptoregex ("192.1.2.3", 2086,
  120. "4-0826-C0010203",
  121. "FFFF::1", 8080,
  122. "6-1F90-FFFF0000000000000000000000000001");
  123. error +=
  124. test_iptoregex ("187.238.255.0", 80,
  125. "4-0050-BBEEFF00",
  126. "E1E1:73F9:51BE::0", 49,
  127. "6-0031-E1E173F951BE00000000000000000000");
  128. error +=
  129. test_policy4toregex ("192.1.2.0/24:80;",
  130. "4-0050-C00102" DOT DOT);
  131. error +=
  132. test_policy4toregex ("192.1.0.0/16;",
  133. "4-" DOT DOT DOT DOT "-C001" DOT DOT DOT DOT);
  134. error +=
  135. test_policy4toregex ("192.1.0.0/16:80-81;",
  136. "4-(0050|0051)-C001" DOT DOT DOT DOT);
  137. error +=
  138. test_policy4toregex ("192.1.0.0/8:!3-65535;",
  139. "4-000(0|1|2)-C0" DOT DOT DOT DOT DOT DOT);
  140. error +=
  141. test_policy4toregex ("192.1.0.0/8:!25-56;",
  142. "4-(0(0(0"DOT
  143. "|1(0|1|2|3|4|5|6|7|8)|3(9|A|B|C|D|E|F)|(4|5|6|7|8|9|A|B|C|D|E|F)"DOT
  144. ")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT
  145. ")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT
  146. DOT ")-C0"DOT DOT DOT DOT DOT DOT);
  147. error +=
  148. test_policy6toregex ("E1E1::1;",
  149. "6-"DOT DOT DOT
  150. DOT "-E1E10000000000000000000000000001");
  151. error +=
  152. test_policy6toregex ("E1E1:ABCD::1/120;",
  153. "6-"DOT DOT DOT
  154. DOT "-E1E1ABCD0000000000000000000000" DOT DOT);
  155. error +=
  156. test_policy6toregex ("E1E1:ABCD::ABCD/126;",
  157. "6-"DOT DOT DOT
  158. DOT "-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
  159. error +=
  160. test_policy6toregex ("E1E1:ABCD::ABCD/127;",
  161. "6-"DOT DOT DOT
  162. DOT "-E1E1ABCD00000000000000000000ABC(C|D)");
  163. error +=
  164. test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
  165. "6-0050-E1E1ABCD00000000000000000000ABCD");
  166. return error;
  167. }