]> jfr.im git - solanum.git/blob - tests/substitution1.c
remove unused variables
[solanum.git] / tests / substitution1.c
1 /*
2 * substition.c: Test substitution under various conditions
3 * Copyright 2017 Simon Arlott
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 "substitution.h"
30
31 #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
32
33 struct Client me;
34 static const char text[] =
35 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
36 "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
37 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
38 "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
39 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
40 "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
42 "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
43 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
44 "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 ;
46
47 #define MKTEXT(n) &text[sizeof(text) - ((n) + 1)]
48
49 static rb_dlink_list varlist = { NULL, NULL, 0 };
50 static rb_dlink_list varlist2 = { NULL, NULL, 0 };
51
52 static void valid_variable1(void)
53 {
54 is_string("TEST1", substitution_parse("${test1}", &varlist), MSG);
55 }
56
57 static void valid_variable2(void)
58 {
59 is_string("TEST1TEST5", substitution_parse("${test1}${test5}", &varlist), MSG);
60 }
61
62 static void missing_variable1(void)
63 {
64 is_string("", substitution_parse("${test6}", &varlist), MSG);
65 }
66
67 static void missing_variable2(void)
68 {
69 is_string("", substitution_parse("${test6}${test7}", &varlist), MSG);
70 }
71
72 static void unterminated_variable1a(void)
73 {
74 is_string("TEST2", substitution_parse("${test2", &varlist), MSG);
75 }
76
77 static void unterminated_variable1b(void)
78 {
79 is_string("TEST2", substitution_parse("${test2\0${test8", &varlist), MSG);
80 }
81
82 static void unterminated_variable2a(void)
83 {
84 is_string("TEST3TEST4", substitution_parse("${test3${test4", &varlist), MSG);
85 }
86
87 static void unterminated_variable2b(void)
88 {
89 is_string("TEST3TEST4", substitution_parse("${test3${test4\0${test9", &varlist), MSG);
90 }
91
92 static void unterminated_missing_variable1(void)
93 {
94 is_string("", substitution_parse("${test6", &varlist), MSG);
95 }
96
97 static void unterminated_missing_variable2(void)
98 {
99 is_string("", substitution_parse("${test6${test7", &varlist), MSG);
100 }
101
102 static void empty_variable1(void)
103 {
104 is_string("", substitution_parse("${}", &varlist), MSG);
105 }
106
107 static void empty_variable2(void)
108 {
109 is_string("}", substitution_parse("${${}}", &varlist), MSG);
110 }
111
112 static void empty_unterminated_variable1(void)
113 {
114 is_string("", substitution_parse("${", &varlist), MSG);
115 }
116
117 static void empty_unterminated_variable2(void)
118 {
119 is_string("", substitution_parse("${${", &varlist), MSG);
120 }
121
122 static void long_variable1a(void) {
123 is_int(512, BUFSIZE, MSG);
124 is_string(MKTEXT(510), substitution_parse("${text510}", &varlist2), MSG);
125 }
126
127 static void long_variable1b(void) {
128 is_int(512, BUFSIZE, MSG);
129 is_string(MKTEXT(511), substitution_parse("${text511}", &varlist2), MSG);
130 }
131
132 static void too_long_variable1a(void) {
133 char tmp2[2048];
134
135 strcpy(tmp2, MKTEXT(512));
136 tmp2[511] = 0; /* text will be truncated to 511 chars */
137
138 is_int(512, BUFSIZE, MSG);
139 is_string(tmp2, substitution_parse("${text512}", &varlist2), MSG);
140 }
141
142 static void too_long_variable1b(void) {
143 char tmp2[2048];
144
145 strcpy(tmp2, MKTEXT(513));
146 tmp2[511] = 0; /* text will be truncated to 511 chars */
147
148 is_int(512, BUFSIZE, MSG);
149 is_string(tmp2, substitution_parse("${text513}", &varlist2), MSG);
150 }
151
152 static void too_long_variable1c(void) {
153 char tmp2[2048];
154
155 strcpy(tmp2, MKTEXT(600));
156 tmp2[511] = 0; /* text will be truncated to 511 chars */
157
158 is_int(512, BUFSIZE, MSG);
159 is_string(tmp2, substitution_parse("${text600}", &varlist2), MSG);
160 }
161
162 static void long_variable2a(void) {
163 char tmp2[2048];
164
165 strcpy(tmp2, MKTEXT(500));
166 strcat(tmp2, MKTEXT(10));
167
168 is_int(512, BUFSIZE, MSG);
169 is_string(tmp2, substitution_parse("${text500}${text10}", &varlist2), MSG);
170 }
171
172 static void long_variable2b(void) {
173 char tmp2[2048];
174
175 strcpy(tmp2, MKTEXT(500));
176 strcat(tmp2, MKTEXT(11));
177
178 is_int(512, BUFSIZE, MSG);
179 is_string(tmp2, substitution_parse("${text500}${text11}", &varlist2), MSG);
180 }
181
182 static void too_long_variable2a(void) {
183 char tmp2[2048];
184
185 strcpy(tmp2, MKTEXT(500));
186 strcat(tmp2, MKTEXT(12));
187 tmp2[511] = 0; /* text will be truncated to 511 chars */
188
189 is_int(512, BUFSIZE, MSG);
190 is_string(tmp2, substitution_parse("${text500}${text12}", &varlist2), MSG);
191 }
192
193 static void too_long_variable2b(void) {
194 char tmp2[2048];
195
196 strcpy(tmp2, MKTEXT(500));
197 strcat(tmp2, MKTEXT(13));
198 tmp2[511] = 0; /* text will be truncated to 511 chars */
199
200 is_int(512, BUFSIZE, MSG);
201 is_string(tmp2, substitution_parse("${text500}${text13}", &varlist2), MSG);
202 }
203
204 static void too_long_variable2c(void) {
205 char tmp2[2048];
206
207 strcpy(tmp2, MKTEXT(600));
208 strcat(tmp2, MKTEXT(10));
209 tmp2[511] = 0; /* text will be truncated to 511 chars */
210
211 is_int(512, BUFSIZE, MSG);
212 is_string(tmp2, substitution_parse("${text600}${text10}", &varlist2), MSG);
213 }
214
215 static void long_variable3a(void) {
216 char tmp2[2048];
217
218 strcpy(tmp2, MKTEXT(100));
219 strcat(tmp2, MKTEXT(400));
220 strcat(tmp2, MKTEXT(10));
221
222 is_int(512, BUFSIZE, MSG);
223 is_string(tmp2, substitution_parse("${text100}${text400}${text10}", &varlist2), MSG);
224 }
225
226 static void long_variable3b(void) {
227 char tmp2[2048];
228
229 strcpy(tmp2, MKTEXT(100));
230 strcat(tmp2, MKTEXT(400));
231 strcat(tmp2, MKTEXT(11));
232
233 is_int(512, BUFSIZE, MSG);
234 is_string(tmp2, substitution_parse("${text100}${text400}${text11}", &varlist2), MSG);
235 }
236
237 static void too_long_variable3a(void) {
238 char tmp2[2048];
239
240 strcpy(tmp2, MKTEXT(100));
241 strcat(tmp2, MKTEXT(400));
242 strcat(tmp2, MKTEXT(12));
243 tmp2[511] = 0; /* text will be truncated to 511 chars */
244
245 is_int(512, BUFSIZE, MSG);
246 is_string(tmp2, substitution_parse("${text100}${text400}${text12}", &varlist2), MSG);
247 }
248
249 static void too_long_variable3b(void) {
250 char tmp2[2048];
251
252 strcpy(tmp2, MKTEXT(100));
253 strcat(tmp2, MKTEXT(400));
254 strcat(tmp2, MKTEXT(13));
255 tmp2[511] = 0; /* text will be truncated to 511 chars */
256
257 is_int(512, BUFSIZE, MSG);
258 is_string(tmp2, substitution_parse("${text100}${text400}${text13}", &varlist2), MSG);
259 }
260
261 static void too_long_variable3c(void) {
262 char tmp2[2048];
263
264 strcpy(tmp2, MKTEXT(200));
265 strcat(tmp2, MKTEXT(400));
266 strcat(tmp2, MKTEXT(100));
267 tmp2[511] = 0; /* text will be truncated to 511 chars */
268
269 is_int(512, BUFSIZE, MSG);
270 is_string(tmp2, substitution_parse("${text200}${text400}${text100}", &varlist2), MSG);
271 }
272
273 int main(int argc, char *argv[])
274 {
275 memset(&me, 0, sizeof(me));
276 strcpy(me.name, "me.name.");
277
278 rb_lib_init(NULL, NULL, NULL, 0, 1024, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
279 rb_linebuf_init(LINEBUF_HEAP_SIZE);
280
281 substitution_append_var(&varlist, "testB", "TESTB");
282 substitution_append_var(&varlist, "test1", "TEST1");
283 substitution_append_var(&varlist, "test2", "TEST2");
284 substitution_append_var(&varlist, "test3", "TEST3");
285 substitution_append_var(&varlist, "test4", "TEST4");
286 substitution_append_var(&varlist, "test5", "TEST5");
287 substitution_append_var(&varlist, "test9", "TEST9");
288 substitution_append_var(&varlist, "testE", "TESTE");
289
290 substitution_append_var(&varlist2, "text10", MKTEXT(10));
291 substitution_append_var(&varlist2, "text11", MKTEXT(11));
292 substitution_append_var(&varlist2, "text12", MKTEXT(12));
293 substitution_append_var(&varlist2, "text13", MKTEXT(13));
294 substitution_append_var(&varlist2, "text14", MKTEXT(14));
295 substitution_append_var(&varlist2, "text100", MKTEXT(100));
296 substitution_append_var(&varlist2, "text200", MKTEXT(200));
297 substitution_append_var(&varlist2, "text400", MKTEXT(400));
298 substitution_append_var(&varlist2, "text500", MKTEXT(500));
299 substitution_append_var(&varlist2, "text510", MKTEXT(510));
300 substitution_append_var(&varlist2, "text511", MKTEXT(511));
301 substitution_append_var(&varlist2, "text512", MKTEXT(512));
302 substitution_append_var(&varlist2, "text513", MKTEXT(513));
303 substitution_append_var(&varlist2, "text514", MKTEXT(514));
304 substitution_append_var(&varlist2, "text600", MKTEXT(600));
305
306 plan_lazy();
307
308 valid_variable1();
309 valid_variable2();
310 missing_variable1();
311 missing_variable2();
312 unterminated_variable1a();
313 unterminated_variable1b();
314 unterminated_variable2a();
315 unterminated_variable2b();
316 unterminated_missing_variable1();
317 unterminated_missing_variable2();
318 empty_variable1();
319 empty_variable2();
320 empty_unterminated_variable1();
321 empty_unterminated_variable2();
322
323 long_variable1a();
324 long_variable1b();
325 long_variable2a();
326 long_variable2b();
327 long_variable3a();
328 long_variable3b();
329 too_long_variable1a();
330 too_long_variable1b();
331 too_long_variable1c();
332 too_long_variable2a();
333 too_long_variable2b();
334 too_long_variable2c();
335 too_long_variable3a();
336 too_long_variable3b();
337 too_long_variable3c();
338
339 return 0;
340 }