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