3
* Copyright (c) 2017--2018 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
28
testmulti_cmp(size_t idx, void *arg)
33
kcgi_buf_puts(arg, "XXX");
39
testempty_cmp(size_t idx, void *arg)
44
kcgi_buf_puts(arg, "XXX");
49
test1_cmp(size_t idx, void *arg)
51
const char *res = "foo";
56
kcgi_buf_puts(arg, res);
61
test1_fallthrough(const char *k, size_t ksz, void *arg)
63
const char *val = "foo", *key = "bar";
67
if (keysz == ksz && 0 == memcmp(key, k, keysz))
68
kcgi_buf_puts(arg, val);
76
struct ktemplatex tkx;
77
const char *keys[] = { "foobar" };
78
const char *multikeys[] = { "foobar", "foobar" };
79
const char *ekeys[] = { "" };
87
memset(&t, 0, sizeof(struct ktemplate));
88
memset(&tkx, 0, sizeof(struct ktemplatex));
89
memset(&b, 0, sizeof(struct kcgi_buf));
91
tkx.writer = kcgi_buf_write;
93
/* Not found: should go through unchanged. */
95
test = "abc@@foobar@@def";
96
testsz = strlen(test);
102
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
105
if (b.sz != testsz || memcmp(test, b.buf, testsz))
111
memset(&b, 0, sizeof(struct kcgi_buf));
112
test = "abc@@foobar@@def";
113
testsz = strlen(test);
118
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
123
if (b.sz != rsz || memcmp(r, b.buf, rsz))
126
/* Found in keys (use only first). */
129
memset(&b, 0, sizeof(struct kcgi_buf));
130
test = "abc@@foobar@@def";
131
testsz = strlen(test);
135
t.cb = testmulti_cmp;
136
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
141
if (b.sz != rsz || memcmp(r, b.buf, rsz))
144
/* Not found: unchanged. */
147
memset(&b, 0, sizeof(struct kcgi_buf));
148
test = "abc@@bar@@def";
149
testsz = strlen(test);
154
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
159
if (b.sz != rsz || memcmp(r, b.buf, rsz))
162
/* Not found, fallthrough, not found (omitted). */
165
memset(&b, 0, sizeof(struct kcgi_buf));
166
test = "abc@@foobar@@def";
167
testsz = strlen(test);
172
tkx.fbk = test1_fallthrough;
173
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
178
if (b.sz != rsz || memcmp(r, b.buf, rsz))
181
/* Not found, fallthrough, found. */
184
memset(&b, 0, sizeof(struct kcgi_buf));
185
test = "abc@@bar@@def";
186
testsz = strlen(test);
191
tkx.fbk = test1_fallthrough;
192
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
197
if (b.sz != rsz || memcmp(r, b.buf, rsz))
200
/* First string found in keys, second omitted. */
203
memset(&b, 0, sizeof(struct kcgi_buf));
204
test = "abc@@bar@@def@@moobar@@";
205
testsz = strlen(test);
210
tkx.fbk = test1_fallthrough;
211
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
216
if (b.sz != rsz || memcmp(r, b.buf, rsz))
219
/* Found zero-length. */
222
memset(&b, 0, sizeof(struct kcgi_buf));
224
testsz = strlen(test);
228
t.cb = testempty_cmp;
230
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
235
if (b.sz != rsz || memcmp(r, b.buf, rsz))
238
/* Found zero-length after escaped. */
241
memset(&b, 0, sizeof(struct kcgi_buf));
242
test = "abc\\@@@@@@def";
243
testsz = strlen(test);
247
t.cb = testempty_cmp;
249
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
254
if (b.sz != rsz || memcmp(r, b.buf, rsz))
257
/* Not found, no fallthrough, kept. */
260
memset(&b, 0, sizeof(struct kcgi_buf));
262
testsz = strlen(test);
268
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
273
if (b.sz != rsz || memcmp(r, b.buf, rsz))
276
/* Not found, fallthrough, discarded. */
279
memset(&b, 0, sizeof(struct kcgi_buf));
281
testsz = strlen(test);
286
tkx.fbk = test1_fallthrough;
287
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
292
if (b.sz != rsz || memcmp(r, b.buf, rsz))
295
/* Error: not terminated. */
298
memset(&b, 0, sizeof(struct kcgi_buf));
300
testsz = strlen(test);
306
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
311
if (b.sz != rsz || memcmp(r, b.buf, rsz))
314
/* Error: not terminated (w/fallthrough). */
317
memset(&b, 0, sizeof(struct kcgi_buf));
319
testsz = strlen(test);
324
tkx.fbk = test1_fallthrough;
325
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
330
if (b.sz != rsz || memcmp(r, b.buf, rsz))
333
/* Error: not terminated at eof. */
336
memset(&b, 0, sizeof(struct kcgi_buf));
338
testsz = strlen(test);
343
tkx.fbk = test1_fallthrough;
344
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
349
if (b.sz != rsz || memcmp(r, b.buf, rsz))
355
memset(&b, 0, sizeof(struct kcgi_buf));
357
testsz = strlen(test);
362
tkx.fbk = test1_fallthrough;
363
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
368
if (b.sz != rsz || memcmp(r, b.buf, rsz))
374
memset(&b, 0, sizeof(struct kcgi_buf));
376
testsz = strlen(test);
381
tkx.fbk = test1_fallthrough;
382
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
387
if (b.sz != rsz || memcmp(r, b.buf, rsz))
393
memset(&b, 0, sizeof(struct kcgi_buf));
395
testsz = strlen(test);
400
tkx.fbk = test1_fallthrough;
401
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
406
if (b.sz != rsz || memcmp(r, b.buf, rsz))
412
memset(&b, 0, sizeof(struct kcgi_buf));
413
test = "abc\\@@foobar\\@@def";
414
testsz = strlen(test);
419
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
422
r = "abc@@foobar@@def";
424
if (b.sz != rsz || memcmp(r, b.buf, rsz))
427
/* Escaped at eof. */
430
memset(&b, 0, sizeof(struct kcgi_buf));
432
testsz = strlen(test);
437
rc = khttp_templatex_buf(&t, test, testsz, &tkx, &b);
442
if (b.sz != rsz || memcmp(r, b.buf, rsz))