]> jfr.im git - solanum.git/blame - tests/hostmask1.c
listener: refactor to use rb_dlink like the other lists in ircd
[solanum.git] / tests / hostmask1.c
CommitLineData
9ea60637
EK
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
33struct Client me;
34
35static 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
49static 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
64static 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
78static 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
93static 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
118static 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
141static 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
155static 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
170static 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
195int 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}