[READ-ONLY] Mirror of https://github.com/VibeDevelopers/Comet.
0

Configure Feed

Select the types of activity you want to include in your feed.

Add tests for parse_netmask

Ed Kellett (Jul 26, 2020, 10:03 PM +0100) 9ea60637 7d9e8e9d

+225
+4
ircd/hostmask.c
··· 67 67 if (endp == ptr || n < 0) 68 68 return HM_HOST; 69 69 if (n > 128 || *endp != '\0') 70 + { 70 71 if (strict) 71 72 return HM_ERROR; 72 73 else 73 74 n = 128; 75 + } 74 76 *b = n; 75 77 } else 76 78 *b = 128; ··· 89 91 if (endp == ptr || n < 0) 90 92 return HM_HOST; 91 93 if (n > 32 || *endp != '\0') 94 + { 92 95 if (strict) 93 96 return HM_ERROR; 94 97 else 95 98 n = 32; 99 + } 96 100 *b = n; 97 101 } else 98 102 *b = 32;
+2
tests/Makefile.am
··· 1 1 check_PROGRAMS = runtests \ 2 2 msgbuf_parse1 \ 3 3 msgbuf_unparse1 \ 4 + hostmask1 \ 4 5 rb_dictionary1 \ 5 6 rb_snprintf_append1 \ 6 7 rb_snprintf_try_append1 \ ··· 24 25 25 26 msgbuf_parse1_SOURCES = msgbuf_parse1.c 26 27 msgbuf_unparse1_SOURCES = msgbuf_unparse1.c 28 + hostmask1_SOURCES = hostmask1.c 27 29 rb_dictionary1_SOURCES = rb_dictionary1.c 28 30 rb_snprintf_append1_SOURCES = rb_snprintf_append1.c 29 31 rb_snprintf_try_append1_SOURCES = rb_snprintf_try_append1.c
+1
tests/TESTS
··· 1 1 msgbuf_parse1 2 2 msgbuf_unparse1 3 + hostmask1 3 4 rb_dictionary1 4 5 rb_snprintf_append1 5 6 rb_snprintf_try_append1
+218
tests/hostmask1.c
··· 1 + /* 2 + * hostmask1.c: Test parse_netmask 3 + * Copyright 2020 Ed Kellett 4 + * 5 + * This program is free software; you can redistribute it and/or modify 6 + * it under the terms of the GNU General Public License as published by 7 + * the Free Software Foundation; either version 2 of the License, or 8 + * (at your option) any later version. 9 + * 10 + * This program is distributed in the hope that it will be useful, 11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 + * GNU General Public License for more details. 14 + * 15 + * You should have received a copy of the GNU General Public License 16 + * along with this program; if not, write to the Free Software 17 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 18 + * USA 19 + */ 20 + #include <stdio.h> 21 + #include <string.h> 22 + #include <stdlib.h> 23 + #include <unistd.h> 24 + #include "tap/basic.h" 25 + 26 + #include "stdinc.h" 27 + #include "ircd_defs.h" 28 + #include "client.h" 29 + #include "hostmask.h" 30 + 31 + #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ 32 + 33 + struct Client me; 34 + 35 + static void plain_hostmask(void) 36 + { 37 + int ty; 38 + 39 + ty = parse_netmask("foo.example", NULL, NULL); 40 + is_int(HM_HOST, ty, MSG); 41 + 42 + ty = parse_netmask("*.example", NULL, NULL); 43 + is_int(HM_HOST, ty, MSG); 44 + 45 + ty = parse_netmask("foo.examp?", NULL, NULL); 46 + is_int(HM_HOST, ty, MSG); 47 + } 48 + 49 + static void valid_ipv4(void) 50 + { 51 + int ty, nb; 52 + struct rb_sockaddr_storage addr; 53 + char ip[1024]; 54 + 55 + ty = parse_netmask("10.20.30.40", &addr, &nb); 56 + is_int(HM_IPV4, ty, MSG); 57 + if (ty == HM_IPV4) 58 + { 59 + is_string("10.20.30.40", rb_inet_ntop_sock((struct sockaddr *)&addr, ip, sizeof ip), MSG); 60 + is_int(32, nb, MSG); 61 + } 62 + } 63 + 64 + static void invalid_ipv4(void) 65 + { 66 + int ty; 67 + 68 + ty = parse_netmask(".1", NULL, NULL); 69 + is_int(HM_HOST, ty, MSG); 70 + 71 + ty = parse_netmask("10.20.30", NULL, NULL); 72 + is_int(HM_HOST, ty, MSG); 73 + 74 + ty = parse_netmask("10.20.30.40.50", NULL, NULL); 75 + is_int(HM_HOST, ty, MSG); 76 + } 77 + 78 + static void valid_ipv4_cidr(void) 79 + { 80 + int ty, nb; 81 + struct rb_sockaddr_storage addr; 82 + char ip[1024]; 83 + 84 + ty = parse_netmask("10.20.30.40/7", &addr, &nb); 85 + is_int(HM_IPV4, ty, MSG); 86 + if (ty == HM_IPV4) 87 + { 88 + is_string("10.20.30.40", rb_inet_ntop_sock((struct sockaddr *)&addr, ip, sizeof ip), MSG); 89 + is_int(7, nb, MSG); 90 + } 91 + } 92 + 93 + static void invalid_ipv4_cidr(void) 94 + { 95 + int ty, nb; 96 + 97 + ty = parse_netmask("10.20.30.40/aaa", NULL, NULL); 98 + is_int(HM_HOST, ty, MSG); 99 + 100 + ty = parse_netmask("10.20.30.40/-1", NULL, NULL); 101 + is_int(HM_HOST, ty, MSG); 102 + 103 + ty = parse_netmask("10.20.30.40/1000", NULL, &nb); 104 + is_int(HM_IPV4, ty, MSG); 105 + is_int(32, nb, MSG); 106 + 107 + ty = parse_netmask("10.20.30.40/1a", NULL, &nb); 108 + is_int(HM_IPV4, ty, MSG); 109 + is_int(32, nb, MSG); 110 + 111 + ty = parse_netmask_strict("10.20.30.40/1000", NULL, NULL); 112 + is_int(HM_ERROR, ty, MSG); 113 + 114 + ty = parse_netmask_strict("10.20.30.40/1a", NULL, NULL); 115 + is_int(HM_ERROR, ty, MSG); 116 + } 117 + 118 + static void valid_ipv6(void) 119 + { 120 + int ty, nb; 121 + struct rb_sockaddr_storage addr; 122 + char ip[1024]; 123 + 124 + ty = parse_netmask("1:2:3:4:5:6:7:8", &addr, &nb); 125 + is_int(HM_IPV6, ty, MSG); 126 + if (ty == HM_IPV6) 127 + { 128 + is_string("1:2:3:4:5:6:7:8", rb_inet_ntop_sock((struct sockaddr *)&addr, ip, sizeof ip), MSG); 129 + is_int(128, nb, MSG); 130 + } 131 + 132 + ty = parse_netmask("1:2::7:8", &addr, &nb); 133 + is_int(HM_IPV6, ty, MSG); 134 + if (ty == HM_IPV6) 135 + { 136 + is_string("1:2::7:8", rb_inet_ntop_sock((struct sockaddr *)&addr, ip, sizeof ip), MSG); 137 + is_int(128, nb, MSG); 138 + } 139 + } 140 + 141 + static void invalid_ipv6(void) 142 + { 143 + int ty; 144 + 145 + ty = parse_netmask(":1", NULL, NULL); 146 + is_int(HM_HOST, ty, MSG); 147 + 148 + ty = parse_netmask("1:2:3:4:5", NULL, NULL); 149 + is_int(HM_HOST, ty, MSG); 150 + 151 + ty = parse_netmask("1:2:3:4:5:6:7:8:9", NULL, NULL); 152 + is_int(HM_HOST, ty, MSG); 153 + } 154 + 155 + static void valid_ipv6_cidr(void) 156 + { 157 + int ty, nb; 158 + struct rb_sockaddr_storage addr; 159 + char ip[1024]; 160 + 161 + ty = parse_netmask("1:2:3:4:5:6:7:8/96", &addr, &nb); 162 + is_int(HM_IPV6, ty, MSG); 163 + if (ty == HM_IPV6) 164 + { 165 + is_string("1:2:3:4:5:6:7:8", rb_inet_ntop_sock((struct sockaddr *)&addr, ip, sizeof ip), MSG); 166 + is_int(96, nb, MSG); 167 + } 168 + } 169 + 170 + static void invalid_ipv6_cidr(void) 171 + { 172 + int ty, nb; 173 + 174 + ty = parse_netmask("1:2::7:8/aaa", NULL, NULL); 175 + is_int(HM_HOST, ty, MSG); 176 + 177 + ty = parse_netmask("1:2::7:8/-1", NULL, NULL); 178 + is_int(HM_HOST, ty, MSG); 179 + 180 + ty = parse_netmask("1:2::7:8/1000", NULL, &nb); 181 + is_int(HM_IPV6, ty, MSG); 182 + is_int(128, nb, MSG); 183 + 184 + ty = parse_netmask("1:2::7:8/1a", NULL, &nb); 185 + is_int(HM_IPV6, ty, MSG); 186 + is_int(128, nb, MSG); 187 + 188 + ty = parse_netmask_strict("1:2::7:8/1000", NULL, NULL); 189 + is_int(HM_ERROR, ty, MSG); 190 + 191 + ty = parse_netmask_strict("1:2::7:8/1a", NULL, NULL); 192 + is_int(HM_ERROR, ty, MSG); 193 + } 194 + 195 + int main(int argc, char *argv[]) 196 + { 197 + memset(&me, 0, sizeof(me)); 198 + strcpy(me.name, "me.name."); 199 + 200 + rb_lib_init(NULL, NULL, NULL, 0, 1024, DNODE_HEAP_SIZE, FD_HEAP_SIZE); 201 + rb_linebuf_init(LINEBUF_HEAP_SIZE); 202 + 203 + plan_lazy(); 204 + 205 + plain_hostmask(); 206 + 207 + valid_ipv4(); 208 + invalid_ipv4(); 209 + valid_ipv4_cidr(); 210 + invalid_ipv4_cidr(); 211 + 212 + valid_ipv6(); 213 + invalid_ipv6(); 214 + valid_ipv6_cidr(); 215 + invalid_ipv6_cidr(); 216 + 217 + return 0; 218 + }