]>
jfr.im git - solanum.git/blob - tests/hostmask1.c
2 * hostmask1.c: Test parse_netmask
3 * Copyright 2020 Ed Kellett
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.
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.
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
24 #include "tap/basic.h"
27 #include "ircd_defs.h"
31 #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
35 static void plain_hostmask(void)
39 ty
= parse_netmask("foo.example", NULL
, NULL
);
40 is_int(HM_HOST
, ty
, MSG
);
42 ty
= parse_netmask("*.example", NULL
, NULL
);
43 is_int(HM_HOST
, ty
, MSG
);
45 ty
= parse_netmask("foo.examp?", NULL
, NULL
);
46 is_int(HM_HOST
, ty
, MSG
);
49 static void valid_ipv4(void)
52 struct rb_sockaddr_storage addr
;
55 ty
= parse_netmask("10.20.30.40", &addr
, &nb
);
56 is_int(HM_IPV4
, ty
, MSG
);
59 is_string("10.20.30.40", rb_inet_ntop_sock((struct sockaddr
*)&addr
, ip
, sizeof ip
), MSG
);
64 static void invalid_ipv4(void)
68 ty
= parse_netmask(".1", NULL
, NULL
);
69 is_int(HM_HOST
, ty
, MSG
);
71 ty
= parse_netmask("10.20.30", NULL
, NULL
);
72 is_int(HM_HOST
, ty
, MSG
);
74 ty
= parse_netmask("10.20.30.40.50", NULL
, NULL
);
75 is_int(HM_HOST
, ty
, MSG
);
78 static void valid_ipv4_cidr(void)
81 struct rb_sockaddr_storage addr
;
84 ty
= parse_netmask("10.20.30.40/7", &addr
, &nb
);
85 is_int(HM_IPV4
, ty
, MSG
);
88 is_string("10.20.30.40", rb_inet_ntop_sock((struct sockaddr
*)&addr
, ip
, sizeof ip
), MSG
);
93 static void invalid_ipv4_cidr(void)
97 ty
= parse_netmask("10.20.30.40/aaa", NULL
, NULL
);
98 is_int(HM_HOST
, ty
, MSG
);
100 ty
= parse_netmask("10.20.30.40/-1", NULL
, NULL
);
101 is_int(HM_HOST
, ty
, MSG
);
103 ty
= parse_netmask("10.20.30.40/1000", NULL
, &nb
);
104 is_int(HM_IPV4
, ty
, MSG
);
107 ty
= parse_netmask("10.20.30.40/1a", NULL
, &nb
);
108 is_int(HM_IPV4
, ty
, MSG
);
111 ty
= parse_netmask_strict("10.20.30.40/1000", NULL
, NULL
);
112 is_int(HM_ERROR
, ty
, MSG
);
114 ty
= parse_netmask_strict("10.20.30.40/1a", NULL
, NULL
);
115 is_int(HM_ERROR
, ty
, MSG
);
118 static void valid_ipv6(void)
121 struct rb_sockaddr_storage addr
;
124 ty
= parse_netmask("1:2:3:4:5:6:7:8", &addr
, &nb
);
125 is_int(HM_IPV6
, ty
, MSG
);
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
);
132 ty
= parse_netmask("1:2::7:8", &addr
, &nb
);
133 is_int(HM_IPV6
, ty
, MSG
);
136 is_string("1:2::7:8", rb_inet_ntop_sock((struct sockaddr
*)&addr
, ip
, sizeof ip
), MSG
);
137 is_int(128, nb
, MSG
);
141 static void invalid_ipv6(void)
145 ty
= parse_netmask(":1", NULL
, NULL
);
146 is_int(HM_HOST
, ty
, MSG
);
148 ty
= parse_netmask("1:2:3:4:5", NULL
, NULL
);
149 is_int(HM_HOST
, ty
, MSG
);
151 ty
= parse_netmask("1:2:3:4:5:6:7:8:9", NULL
, NULL
);
152 is_int(HM_HOST
, ty
, MSG
);
155 static void valid_ipv6_cidr(void)
158 struct rb_sockaddr_storage addr
;
161 ty
= parse_netmask("1:2:3:4:5:6:7:8/96", &addr
, &nb
);
162 is_int(HM_IPV6
, ty
, MSG
);
165 is_string("1:2:3:4:5:6:7:8", rb_inet_ntop_sock((struct sockaddr
*)&addr
, ip
, sizeof ip
), MSG
);
170 static void invalid_ipv6_cidr(void)
174 ty
= parse_netmask("1:2::7:8/aaa", NULL
, NULL
);
175 is_int(HM_HOST
, ty
, MSG
);
177 ty
= parse_netmask("1:2::7:8/-1", NULL
, NULL
);
178 is_int(HM_HOST
, ty
, MSG
);
180 ty
= parse_netmask("1:2::7:8/1000", NULL
, &nb
);
181 is_int(HM_IPV6
, ty
, MSG
);
182 is_int(128, nb
, MSG
);
184 ty
= parse_netmask("1:2::7:8/1a", NULL
, &nb
);
185 is_int(HM_IPV6
, ty
, MSG
);
186 is_int(128, nb
, MSG
);
188 ty
= parse_netmask_strict("1:2::7:8/1000", NULL
, NULL
);
189 is_int(HM_ERROR
, ty
, MSG
);
191 ty
= parse_netmask_strict("1:2::7:8/1a", NULL
, NULL
);
192 is_int(HM_ERROR
, ty
, MSG
);
195 int main(int argc
, char *argv
[])
197 memset(&me
, 0, sizeof(me
));
198 strcpy(me
.name
, "me.name.");
200 rb_lib_init(NULL
, NULL
, NULL
, 0, 1024, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
201 rb_linebuf_init(LINEBUF_HEAP_SIZE
);