ksgi

Форк
0
/
test-template.c 
449 строк · 9.3 Кб
1
/*	$Id$ */
2
/*
3
 * Copyright (c) 2017--2018 Kristaps Dzonsons <kristaps@bsd.lv>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
#include "../config.h"
18

19
#include <stdarg.h>
20
#include <stdint.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <unistd.h>
24

25
#include "../kcgi.h"
26

27
static int
28
testmulti_cmp(size_t idx, void *arg)
29
{
30

31
	if (idx)
32
		return(0);
33
	kcgi_buf_puts(arg, "XXX");
34
	return(1);
35
}
36

37

38
static int
39
testempty_cmp(size_t idx, void *arg)
40
{
41

42
	if (idx)
43
		return(0);
44
	kcgi_buf_puts(arg, "XXX");
45
	return(1);
46
}
47

48
static int
49
test1_cmp(size_t idx, void *arg)
50
{
51
	const char *res = "foo";
52

53
	if (idx)
54
		return(0);
55

56
	kcgi_buf_puts(arg, res);
57
	return(1);
58
}
59

60
static int
61
test1_fallthrough(const char *k, size_t ksz, void *arg)
62
{
63
	const char *val = "foo", *key = "bar";
64
	size_t	 keysz;
65

66
	keysz = strlen(key);
67
	if (keysz == ksz && 0 == memcmp(key, k, keysz))
68
		kcgi_buf_puts(arg, val);
69
	return(1);
70
}
71

72
int
73
main(void)
74
{
75
	struct ktemplate t;
76
	struct ktemplatex tkx;
77
	const char	*keys[] = { "foobar" };
78
	const char	*multikeys[] = { "foobar", "foobar" };
79
	const char	*ekeys[] = { "" };
80
	const char	*test;
81
	const char	*r;
82
	size_t		 testsz, rsz;
83
	struct kcgi_buf	 b;
84
	int		 c = EXIT_FAILURE;
85
	enum kcgi_err	 rc;
86

87
	memset(&t, 0, sizeof(struct ktemplate));
88
	memset(&tkx, 0, sizeof(struct ktemplatex));
89
	memset(&b, 0, sizeof(struct kcgi_buf));
90

91
	tkx.writer = kcgi_buf_write;
92

93
	/* Not found: should go through unchanged. */
94

95
	test = "abc@@foobar@@def";
96
	testsz = strlen(test);
97
	t.key = NULL;
98
	t.keysz = 0;
99
	t.arg = &b;
100
	t.cb = test1_cmp;
101
	tkx.fbk = NULL;
102
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
103
	if (KCGI_OK != rc)
104
		goto out;
105
	if (b.sz != testsz || memcmp(test, b.buf, testsz))
106
		goto out;
107

108
	/* Found in keys. */
109

110
	free(b.buf);
111
	memset(&b, 0, sizeof(struct kcgi_buf));
112
	test = "abc@@foobar@@def";
113
	testsz = strlen(test);
114
	t.key = keys;
115
	t.keysz = 1;
116
	t.arg = &b;
117
	t.cb = test1_cmp;
118
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
119
	if (KCGI_OK != rc)
120
		goto out;
121
	r = "abcfoodef";
122
	rsz = strlen(r);
123
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
124
		goto out;
125

126
	/* Found in keys (use only first). */
127

128
	free(b.buf);
129
	memset(&b, 0, sizeof(struct kcgi_buf));
130
	test = "abc@@foobar@@def";
131
	testsz = strlen(test);
132
	t.key = multikeys;
133
	t.keysz = 2;
134
	t.arg = &b;
135
	t.cb = testmulti_cmp;
136
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
137
	if (KCGI_OK != rc)
138
		goto out;
139
	r = "abcXXXdef";
140
	rsz = strlen(r);
141
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
142
		goto out;
143

144
	/* Not found: unchanged. */
145

146
	free(b.buf);
147
	memset(&b, 0, sizeof(struct kcgi_buf));
148
	test = "abc@@bar@@def";
149
	testsz = strlen(test);
150
	t.key = keys;
151
	t.keysz = 1;
152
	t.arg = &b;
153
	t.cb = test1_cmp;
154
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
155
	if (KCGI_OK != rc)
156
		goto out;
157
	r = "abc@@bar@@def";
158
	rsz = strlen(r);
159
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
160
		goto out;
161

162
	/* Not found, fallthrough, not found (omitted). */
163

164
	free(b.buf);
165
	memset(&b, 0, sizeof(struct kcgi_buf));
166
	test = "abc@@foobar@@def";
167
	testsz = strlen(test);
168
	t.key = NULL;
169
	t.keysz = 0;
170
	t.arg = &b;
171
	t.cb = test1_cmp;
172
	tkx.fbk = test1_fallthrough;
173
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
174
	if (KCGI_OK != rc)
175
		goto out;
176
	r = "abcdef";
177
	rsz = strlen(r);
178
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
179
		goto out;
180

181
	/* Not found, fallthrough, found. */
182

183
	free(b.buf);
184
	memset(&b, 0, sizeof(struct kcgi_buf));
185
	test = "abc@@bar@@def";
186
	testsz = strlen(test);
187
	t.key = NULL;
188
	t.keysz = 0;
189
	t.arg = &b;
190
	t.cb = test1_cmp;
191
	tkx.fbk = test1_fallthrough;
192
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
193
	if (KCGI_OK != rc)
194
		goto out;
195
	r = "abcfoodef";
196
	rsz = strlen(r);
197
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
198
		goto out;
199

200
	/* First string found in keys, second omitted. */
201

202
	free(b.buf);
203
	memset(&b, 0, sizeof(struct kcgi_buf));
204
	test = "abc@@bar@@def@@moobar@@";
205
	testsz = strlen(test);
206
	t.key = NULL;
207
	t.keysz = 0;
208
	t.arg = &b;
209
	t.cb = test1_cmp;
210
	tkx.fbk = test1_fallthrough;
211
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
212
	if (KCGI_OK != rc)
213
		goto out;
214
	r = "abcfoodef";
215
	rsz = strlen(r);
216
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
217
		goto out;
218

219
	/* Found zero-length. */
220

221
	free(b.buf);
222
	memset(&b, 0, sizeof(struct kcgi_buf));
223
	test = "abc@@@@def";
224
	testsz = strlen(test);
225
	t.key = ekeys;
226
	t.keysz = 1;
227
	t.arg = &b;
228
	t.cb = testempty_cmp;
229
	tkx.fbk = NULL;
230
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
231
	if (KCGI_OK != rc)
232
		goto out;
233
	r = "abcXXXdef";
234
	rsz = strlen(r);
235
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
236
		goto out;
237

238
	/* Found zero-length after escaped. */
239

240
	free(b.buf);
241
	memset(&b, 0, sizeof(struct kcgi_buf));
242
	test = "abc\\@@@@@@def";
243
	testsz = strlen(test);
244
	t.key = ekeys;
245
	t.keysz = 1;
246
	t.arg = &b;
247
	t.cb = testempty_cmp;
248
	tkx.fbk = NULL;
249
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
250
	if (KCGI_OK != rc)
251
		goto out;
252
	r = "abc@@XXXdef";
253
	rsz = strlen(r);
254
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
255
		goto out;
256

257
	/* Not found, no fallthrough, kept. */
258

259
	free(b.buf);
260
	memset(&b, 0, sizeof(struct kcgi_buf));
261
	test = "abc@@@@def";
262
	testsz = strlen(test);
263
	t.key = NULL;
264
	t.keysz = 0;
265
	t.arg = &b;
266
	t.cb = test1_cmp;
267
	tkx.fbk = NULL;
268
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
269
	if (KCGI_OK != rc)
270
		goto out;
271
	r = "abc@@@@def";
272
	rsz = strlen(r);
273
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
274
		goto out;
275

276
	/* Not found, fallthrough, discarded. */
277

278
	free(b.buf);
279
	memset(&b, 0, sizeof(struct kcgi_buf));
280
	test = "abc@@@@def";
281
	testsz = strlen(test);
282
	t.key = NULL;
283
	t.keysz = 0;
284
	t.arg = &b;
285
	t.cb = test1_cmp;
286
	tkx.fbk = test1_fallthrough;
287
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
288
	if (KCGI_OK != rc)
289
		goto out;
290
	r = "abcdef";
291
	rsz = strlen(r);
292
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
293
		goto out;
294

295
	/* Error: not terminated. */
296

297
	free(b.buf);
298
	memset(&b, 0, sizeof(struct kcgi_buf));
299
	test = "abc@@def";
300
	testsz = strlen(test);
301
	t.key = keys;
302
	t.keysz = 1;
303
	t.arg = &b;
304
	t.cb = test1_cmp;
305
	tkx.fbk = NULL;
306
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
307
	if (KCGI_OK != rc)
308
		goto out;
309
	r = "abc@@def";
310
	rsz = strlen(r);
311
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
312
		goto out;
313

314
	/* Error: not terminated (w/fallthrough). */
315

316
	free(b.buf);
317
	memset(&b, 0, sizeof(struct kcgi_buf));
318
	test = "abc@@def";
319
	testsz = strlen(test);
320
	t.key = keys;
321
	t.keysz = 1;
322
	t.arg = &b;
323
	t.cb = test1_cmp;
324
	tkx.fbk = test1_fallthrough;
325
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
326
	if (KCGI_OK != rc)
327
		goto out;
328
	r = "abc@@def";
329
	rsz = strlen(r);
330
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
331
		goto out;
332

333
	/* Error: not terminated at eof. */
334

335
	free(b.buf);
336
	memset(&b, 0, sizeof(struct kcgi_buf));
337
	test = "abc@@";
338
	testsz = strlen(test);
339
	t.key = keys;
340
	t.keysz = 1;
341
	t.arg = &b;
342
	t.cb = test1_cmp;
343
	tkx.fbk = test1_fallthrough;
344
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
345
	if (KCGI_OK != rc)
346
		goto out;
347
	r = "abc@@";
348
	rsz = strlen(r);
349
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
350
		goto out;
351

352
	/* Full span. */
353

354
	free(b.buf);
355
	memset(&b, 0, sizeof(struct kcgi_buf));
356
	test = "@@foobar@@";
357
	testsz = strlen(test);
358
	t.key = keys;
359
	t.keysz = 1;
360
	t.arg = &b;
361
	t.cb = test1_cmp;
362
	tkx.fbk = test1_fallthrough;
363
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
364
	if (KCGI_OK != rc)
365
		goto out;
366
	r = "foo";
367
	rsz = strlen(r);
368
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
369
		goto out;
370

371
	/* Empty string. */
372

373
	free(b.buf);
374
	memset(&b, 0, sizeof(struct kcgi_buf));
375
	test = "";
376
	testsz = strlen(test);
377
	t.key = keys;
378
	t.keysz = 1;
379
	t.arg = &b;
380
	t.cb = test1_cmp;
381
	tkx.fbk = test1_fallthrough;
382
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
383
	if (KCGI_OK != rc)
384
		goto out;
385
	r = "";
386
	rsz = strlen(r);
387
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
388
		goto out;
389

390
	/* Only delim. */
391

392
	free(b.buf);
393
	memset(&b, 0, sizeof(struct kcgi_buf));
394
	test = "@@";
395
	testsz = strlen(test);
396
	t.key = keys;
397
	t.keysz = 1;
398
	t.arg = &b;
399
	t.cb = test1_cmp;
400
	tkx.fbk = test1_fallthrough;
401
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
402
	if (KCGI_OK != rc)
403
		goto out;
404
	r = "@@";
405
	rsz = strlen(r);
406
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
407
		goto out;
408

409
	/* Escaped. */
410

411
	free(b.buf);
412
	memset(&b, 0, sizeof(struct kcgi_buf));
413
	test = "abc\\@@foobar\\@@def";
414
	testsz = strlen(test);
415
	t.key = keys;
416
	t.keysz = 1;
417
	t.arg = &b;
418
	t.cb = test1_cmp;
419
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
420
	if (KCGI_OK != rc)
421
		goto out;
422
	r = "abc@@foobar@@def";
423
	rsz = strlen(r);
424
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
425
		goto out;
426

427
	/* Escaped at eof. */
428

429
	free(b.buf);
430
	memset(&b, 0, sizeof(struct kcgi_buf));
431
	test = "abc\\@@";
432
	testsz = strlen(test);
433
	t.key = keys;
434
	t.keysz = 1;
435
	t.arg = &b;
436
	t.cb = test1_cmp;
437
	rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
438
	if (KCGI_OK != rc)
439
		goto out;
440
	r = "abc@@";
441
	rsz = strlen(r);
442
	if (b.sz != rsz || memcmp(r, b.buf, rsz))
443
		goto out;
444

445
	c = EXIT_SUCCESS;
446
out:
447
	free(b.buf);
448
	return(c);
449
}
450

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.