-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathgetxstring.testc
655 lines (599 loc) · 30.5 KB
/
getxstring.testc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/* Test the getxstring() function */
#include "config.h"
#include "cunit/cyrunit.h"
#include "prot.h"
#include "lib/libcyr_cfg.h"
#include "imap/global.h"
#define DBDIR "test-dbdir"
static int set_up(void)
{
/* need basic configuration for getxstring */
libcyrus_config_setstring(CYRUSOPT_CONFIG_DIR, DBDIR);
config_read_string(
"configdirectory: "DBDIR"/conf\n"
);
return 0;
}
static int tear_down(void)
{
int r;
config_reset();
r = system("rm -rf " DBDIR);
return r;
}
/*
* Here's the ABNF describing the various types of string from RF3501.
* This is included for amusement mainly, as the getxstring() code takes
* many liberties with it, being at times more liberal and at times more
* conservative than the strict interpretation of the ABNF. As these
* behaviours have been in the field a long time and clients may well
* depend on them, we test for the existing behaviour rather than strict
* RFC compliance.
*
* astring = 1*ASTRING-CHAR / string
*
* ASTRING-CHAR = ATOM-CHAR / resp-specials
*
* ATOM-CHAR = <any CHAR except atom-specials>
*
* atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
* quoted-specials / resp-specials
*
* string = quoted / literal
*
* list-wildcards = "%" / "*"
*
* literal = "{" number "}" CRLF *CHAR8
* ; Number represents the number of CHAR8s
*
* nil = "NIL"
*
* nstring = string / nil
*
* quoted = DQUOTE *QUOTED-CHAR DQUOTE
*
* QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
* "\" quoted-specials
*
* quoted-specials = DQUOTE / "\"
*
* resp-specials = "]"
*
* string = quoted / literal
*/
/*
* Run a single testcase.
*/
#define _TESTCASE_PRE(fut, input, retval, consumed) \
do { \
struct buf b = BUF_INITIALIZER; \
struct protstream *p; \
int c; \
long long _consumed = (consumed); \
p = prot_readmap(input, sizeof(input)-1); \
CU_ASSERT_PTR_NOT_NULL_FATAL(p); \
c = fut(p, NULL, &b); \
CU_ASSERT_EQUAL(c, retval); \
if (_consumed >= 0) { \
CU_ASSERT_EQUAL(prot_bytes_in(p), _consumed); \
} \
if (c != EOF) {
#define _TESTCASE_POST() \
} \
prot_free(p); \
buf_free(&b); \
} while (0)
#define TESTCASE(fut, input, retval, output, consumed) \
do { \
int outputlen = sizeof(output)-1; \
_TESTCASE_PRE(fut, input, retval, consumed); \
CU_ASSERT_EQUAL(b.len, outputlen); \
CU_ASSERT(!memcmp(b.s, output, outputlen)); \
_TESTCASE_POST(); \
} while(0)
#define TESTCASE_NULL(fut, input, retval, consumed) \
do { \
_TESTCASE_PRE(fut, input, retval, consumed); \
CU_ASSERT_EQUAL(b.len, 0); \
CU_ASSERT_PTR_NULL(b.s); \
_TESTCASE_POST(); \
} while(0)
/* strlen-y, but embedded NULs are okay
* N.B. this macro is ONLY valid for string literals! */
#define BLEN(s) (sizeof(s) - 1)
/*
* getastring() parses something vaguely like an astring, with a few differences.
*/
static void test_getastring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms */
TESTCASE(getastring, "hydrogen helium", ' ', "hydrogen", BLEN("hydrogen "));
TESTCASE(getastring, "258 uranium", ' ', "258", BLEN("258 "));
TESTCASE(getastring, "uranium258 plutonium", ' ', "uranium258", BLEN("uranium258 "));
/* The character sequence NIL is not special, it's parsed as an atom */
TESTCASE(getastring, "NIL by mouth", ' ', "NIL", BLEN("NIL "));
TESTCASE(getastring, "NELLY the lamb", ' ', "NELLY", BLEN("NELLY "));
TESTCASE(getastring, "NILE in Egypt", ' ', "NILE", BLEN("NILE "));
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings.
*/
TESTCASE(getastring, "foo*bar baz", ' ', "foo*bar", BLEN("foo*bar "));
TESTCASE(getastring, "baz%quux foo", ' ', "baz%quux", BLEN("baz%quux "));
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getastring, "foo(bar baz", '(', "foo", BLEN("foo("));
TESTCASE(getastring, "foo)bar baz", ')', "foo", BLEN("foo)"));
TESTCASE(getastring, "foo{bar baz", ' ', "foo{bar", BLEN("foo{bar ")); /* should be: '{', "foo" */
TESTCASE(getastring, "foo\"bar baz", '"', "foo", BLEN("foo\""));
TESTCASE(getastring, "foo\\bar baz", ' ', "foo\\bar", BLEN("foo\\bar ")); /* should be: '\\', "foo" */
TESTCASE(getastring, "foo]bar baz", ' ', "foo]bar", BLEN("foo]bar ")); /* should be ']', "foo" */
/*
* Quoted strings are astrings
*/
TESTCASE(getastring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
TESTCASE(getastring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getastring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getastring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getastring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getastring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getastring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getastring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getastring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\rbar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getastring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getastring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are astrings
*/
/* boring literal */
TESTCASE(getastring, "{3}\r\nfoo ", ' ', "foo", BLEN("{3}\r\nfoo "));
/* literal NIL */
TESTCASE(getastring, "{3}\r\nNIL ", ' ', "NIL", BLEN("{3}\r\nNIL "));
/* literals with embedded space */
TESTCASE(getastring, "{7}\r\nfoo bar ", ' ', "foo bar", BLEN("{7}\r\nfoo bar "));
/* literals with embedded \n or \r */
TESTCASE(getastring, "{7}\r\nfoo\nbar ", ' ', "foo\nbar", BLEN("{7}\r\nfoo\nbar "));
TESTCASE(getastring, "{7}\r\nfoo\rbar ", ' ', "foo\rbar", BLEN("{7}\r\nfoo\rbar "));
/* literals with 8-bit chars */
TESTCASE(getastring, "{7}\r\nfoo\277bar ", ' ', "foo\277bar", BLEN("{7}\r\nfoo\277bar "));
/* literals with embedded NUL - getastring() rejects these */
TESTCASE(getastring, "{7}\r\nfoo\0bar ", EOF, "", BLEN("{7}\r\nfoo\0bar")); /* should be ' ', "foo\0bar" */
}
/*
* getbastring() is just the same as getastring() but allows embedded
* NULs in literals.
*/
static void test_getbastring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms */
TESTCASE(getbastring, "hydrogen helium", ' ', "hydrogen", BLEN("hydrogen "));
TESTCASE(getbastring, "258 uranium", ' ', "258", BLEN("258 "));
TESTCASE(getbastring, "uranium258 plutonium", ' ', "uranium258", BLEN("uranium258 "));
/* The character sequence NIL is not special, it's parsed as an atom */
TESTCASE(getbastring, "NIL by mouth", ' ', "NIL", BLEN("NIL "));
TESTCASE(getbastring, "NELLY the lamb", ' ', "NELLY", BLEN("NELLY "));
TESTCASE(getbastring, "NILE in Egypt", ' ', "NILE", BLEN("NILE "));
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings. This is probably sheer laziness on Cyrus' part
* but it's a liberal-server interpretation which has been in the
* field a while now, so we ought to preserve it.
*/
TESTCASE(getbastring, "foo*bar baz", ' ', "foo*bar", BLEN("foo*bar "));
TESTCASE(getbastring, "baz%quux foo", ' ', "baz%quux", BLEN("baz%quux "));
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getbastring, "foo(bar baz", '(', "foo", BLEN("foo("));
TESTCASE(getbastring, "foo)bar baz", ')', "foo", BLEN("foo)"));
TESTCASE(getbastring, "foo{bar baz", ' ', "foo{bar", BLEN("foo{bar ")); /* should be: '{', "foo" */
TESTCASE(getbastring, "foo\"bar baz", '"', "foo", BLEN("foo\""));
TESTCASE(getbastring, "foo\\bar baz", ' ', "foo\\bar", BLEN("foo\\bar ")); /* should be: '\\', "foo" */
TESTCASE(getbastring, "foo]bar baz", ' ', "foo]bar", BLEN("foo]bar ")); /* should be ']', "foo" */
/*
* Quoted strings are astrings
*/
TESTCASE(getbastring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
TESTCASE(getbastring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getbastring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getbastring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getbastring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getbastring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getbastring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getbastring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getbastring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\rbar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getbastring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getbastring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are astrings
*/
/* boring literal */
TESTCASE(getbastring, "{3}\r\nfoo ", ' ', "foo", BLEN("{3}\r\nfoo "));
/* literal NIL */
TESTCASE(getbastring, "{3}\r\nNIL ", ' ', "NIL", BLEN("{3}\r\nNIL "));
/* literals with embedded space */
TESTCASE(getbastring, "{7}\r\nfoo bar ", ' ', "foo bar", BLEN("{7}\r\nfoo bar "));
/* literals with embedded \n or \r */
TESTCASE(getbastring, "{7}\r\nfoo\nbar ", ' ', "foo\nbar", BLEN("{7}\r\nfoo\nbar "));
TESTCASE(getbastring, "{7}\r\nfoo\rbar ", ' ', "foo\rbar", BLEN("{7}\r\nfoo\rbar "));
/* literals with 8-bit chars */
TESTCASE(getbastring, "{7}\r\nfoo\277bar ", ' ', "foo\277bar", BLEN("{7}\r\nfoo\277bar "));
/* literals with embedded NUL - getbastring() allows these */
TESTCASE(getbastring, "{7}\r\nfoo\0bar ", ' ', "foo\0bar", BLEN("{7}\r\nfoo\0bar "));
}
/*
* getstring() parses something very like a 'string' in the ABNF.
*/
static void test_getstring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms
* which are not strings */
TESTCASE(getstring, "hydrogen helium", EOF, "", 0);
TESTCASE(getstring, "258 uranium", EOF, "", 0);
TESTCASE(getstring, "uranium258 plutonium", EOF, "", 0);
/* The character sequence NIL is not special, it's parsed as an atom */
TESTCASE(getstring, "NIL by mouth", EOF, "", 0);
TESTCASE(getstring, "NELLY the lamb", EOF, "", 0);
TESTCASE(getstring, "NILE in Egypt", EOF, "", 0);
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings. This is probably sheer laziness on Cyrus' part
* but it's a liberal-server interpretation which has been in the
* field a while now, so we ought to preserve it.
*/
TESTCASE(getstring, "foo*bar baz", EOF, "", 0);
TESTCASE(getstring, "baz%quux foo", EOF, "", 0);
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getstring, "foo(bar baz", EOF, "", 0);
TESTCASE(getstring, "foo)bar baz", EOF, "", 0);
TESTCASE(getstring, "foo{bar baz", EOF, "", 0);
TESTCASE(getstring, "foo\"bar baz", EOF, "", 0);
TESTCASE(getstring, "foo\\bar baz", EOF, "", 0);
TESTCASE(getstring, "foo]bar baz", EOF, "", 0);
/*
* Quoted strings are strings
*/
TESTCASE(getstring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
TESTCASE(getstring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getstring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getstring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getstring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getstring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getstring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getstring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getstring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\rbar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getstring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getstring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are strings
*/
/* boring literal */
TESTCASE(getstring, "{3}\r\nfoo ", ' ', "foo", BLEN("{3}\r\nfoo "));
/* literal NIL */
TESTCASE(getstring, "{3}\r\nNIL ", ' ', "NIL", BLEN("{3}\r\nNIL "));
/* literals with embedded space */
TESTCASE(getstring, "{7}\r\nfoo bar ", ' ', "foo bar", BLEN("{7}\r\nfoo bar "));
/* literals with embedded \n or \r */
TESTCASE(getstring, "{7}\r\nfoo\nbar ", ' ', "foo\nbar", BLEN("{7}\r\nfoo\nbar "));
TESTCASE(getstring, "{7}\r\nfoo\rbar ", ' ', "foo\rbar", BLEN("{7}\r\nfoo\rbar "));
/* literals with 8-bit chars */
TESTCASE(getstring, "{7}\r\nfoo\277bar ", ' ', "foo\277bar", BLEN("{7}\r\nfoo\277bar "));
/* literals with embedded NUL - getstring() rejects these */
TESTCASE(getstring, "{7}\r\nfoo\0bar ", EOF, "", BLEN("{7}\r\nfoo\0bar")); /* should be ' ', "foo\0bar" */
}
/*
* getqstring() accepts something very like the qstring in the ABNF.
* Atoms, NIL and literals all fail.
*/
static void test_getqstring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms
* which are not qstrings */
TESTCASE(getqstring, "hydrogen helium", EOF, "", 0);
TESTCASE(getqstring, "258 uranium", EOF, "", 0);
TESTCASE(getqstring, "uranium258 plutonium", EOF, "", 0);
/* The character sequence NIL is not special, it's parsed as an atom */
TESTCASE(getqstring, "NIL by mouth", EOF, "", 0);
TESTCASE(getqstring, "NELLY the lamb", EOF, "", 0);
TESTCASE(getqstring, "NILE in Egypt", EOF, "", 0);
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings. This is probably sheer laziness on Cyrus' part
* but it's a liberal-server interpretation which has been in the
* field a while now, so we ought to preserve it.
*/
TESTCASE(getqstring, "foo*bar baz", EOF, "", 0);
TESTCASE(getqstring, "baz%quux foo", EOF, "", 0);
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getqstring, "foo(bar baz", EOF, "", 0);
TESTCASE(getqstring, "foo)bar baz", EOF, "", 0);
TESTCASE(getqstring, "foo{bar baz", EOF, "", 0);
TESTCASE(getqstring, "foo\"bar baz", EOF, "", 0);
TESTCASE(getqstring, "foo\\bar baz", EOF, "", 0);
TESTCASE(getqstring, "foo]bar baz", EOF, "", 0);
/*
* Quoted strings
*/
TESTCASE(getqstring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
TESTCASE(getqstring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getqstring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getqstring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getqstring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getqstring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getqstring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getqstring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getqstring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\rbar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getqstring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getqstring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are not qstrings
*/
/* boring literal */
TESTCASE(getqstring, "{3}\r\nfoo ", EOF, "", 0);
/* literal NIL */
TESTCASE(getqstring, "{3}\r\nNIL ", EOF, "", 0);
/* literals with embedded space */
TESTCASE(getqstring, "{7}\r\nfoo bar ", EOF, "", 0);
/* literals with embedded \n or \r */
TESTCASE(getqstring, "{7}\r\nfoo\nbar ", EOF, "", 0);
TESTCASE(getqstring, "{7}\r\nfoo\rbar ", EOF, "", 0);
/* literals with 8-bit chars */
TESTCASE(getqstring, "{7}\r\nfoo\277bar ", EOF, "", 0);
/* literals with embedded NUL */
TESTCASE(getqstring, "{7}\r\nfoo\0bar ", EOF, "", 0);
}
/*
* getnstring() parses something vaguely like an nstring, with a few differences.
*/
static void test_getnstring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms */
TESTCASE(getnstring, "hydrogen helium", EOF, "", 0);
TESTCASE(getnstring, "258 uranium", EOF, "", 0);
TESTCASE(getnstring, "uranium258 plutonium", EOF, "", 0);
/* The character sequence NIL is special for nstrings only */
TESTCASE_NULL(getnstring, "NIL by mouth", ' ', BLEN("NIL "));
TESTCASE(getnstring, "NELLY ", EOF, "", 0);
TESTCASE(getnstring, "NILE in Egypt", EOF, "", 0);
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings.
*/
TESTCASE(getnstring, "foo*bar baz", EOF, "", 0);
TESTCASE(getnstring, "baz%quux foo", EOF, "", 0);
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getnstring, "foo(bar baz", EOF, "", 0);
TESTCASE(getnstring, "foo)bar baz", EOF, "", 0);
TESTCASE(getnstring, "foo{bar baz", EOF, "", 0);
TESTCASE(getnstring, "foo\"bar baz", EOF, "", 0);
TESTCASE(getnstring, "foo\\bar baz", EOF, "", 0);
TESTCASE(getnstring, "foo]bar baz", EOF, "", 0);
/*
* Quoted strings are nstrings
*/
TESTCASE(getnstring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
TESTCASE(getnstring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getnstring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getnstring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getnstring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getnstring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getnstring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getnstring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getnstring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\\bar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getnstring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getnstring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are nstrings
*/
/* boring literal */
TESTCASE(getnstring, "{3}\r\nfoo ", ' ', "foo", BLEN("{3}\r\nfoo "));
/* literal NIL */
TESTCASE(getnstring, "{3}\r\nNIL ", ' ', "NIL", BLEN("{3}\r\nNIL "));
/* literals with embedded space */
TESTCASE(getnstring, "{7}\r\nfoo bar ", ' ', "foo bar", BLEN("{7}\r\nfoo bar "));
/* literals with embedded \n or \r */
TESTCASE(getnstring, "{7}\r\nfoo\nbar ", ' ', "foo\nbar", BLEN("{7}\r\nfoo\nbar "));
TESTCASE(getnstring, "{7}\r\nfoo\rbar ", ' ', "foo\rbar", BLEN("{7}\r\nfoo\rbar "));
/* literals with 8-bit chars */
TESTCASE(getnstring, "{7}\r\nfoo\277bar ", ' ', "foo\277bar", BLEN("{7}\r\nfoo\277bar "));
/* literals with embedded NUL - getnstring() rejects these */
TESTCASE(getnstring, "{7}\r\nfoo\0bar ", EOF, "", BLEN("{7}\r\nfoo\0bar")); /* should be ' ', "foo\0bar" */
}
/*
* getbnstring() is just like getnstring() but allows embedded NULs in
* literals.
*/
static void test_getbnstring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms */
TESTCASE(getbnstring, "hydrogen helium", EOF, "", 0);
TESTCASE(getbnstring, "258 uranium", EOF, "", 0);
TESTCASE(getbnstring, "uranium258 plutonium", EOF, "", 0);
/* The character sequence NIL is special for nstrings only */
TESTCASE_NULL(getbnstring, "NIL by mouth", ' ', BLEN("NIL "));
TESTCASE(getbnstring, "NELLY ", EOF, "", 0);
TESTCASE(getbnstring, "NILE in Egypt", EOF, "", 0);
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings.
*/
TESTCASE(getnstring, "foo*bar baz", EOF, "", 0);
TESTCASE(getbnstring, "baz%quux foo", EOF, "", 0);
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getbnstring, "foo(bar baz", EOF, "", 0);
TESTCASE(getbnstring, "foo)bar baz", EOF, "", 0);
TESTCASE(getbnstring, "foo{bar baz", EOF, "", 0);
TESTCASE(getbnstring, "foo\"bar baz", EOF, "", 0);
TESTCASE(getbnstring, "foo\\bar baz", EOF, "", 0);
TESTCASE(getbnstring, "foo]bar baz", EOF, "", 0);
/*
* Quoted strings are nstrings
*/
TESTCASE(getbnstring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
TESTCASE(getbnstring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getbnstring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getbnstring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getbnstring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getbnstring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getbnstring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getbnstring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getbnstring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\rbar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getbnstring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getbnstring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are nstrings
*/
/* boring literal */
TESTCASE(getbnstring, "{3}\r\nfoo ", ' ', "foo", BLEN("{3}\r\nfoo "));
/* literal NIL */
TESTCASE(getbnstring, "{3}\r\nNIL ", ' ', "NIL", BLEN("{3}\r\nNIL "));
/* literals with embedded space */
TESTCASE(getbnstring, "{7}\r\nfoo bar ", ' ', "foo bar", BLEN("{7}\r\nfoo bar "));
/* literals with embedded \n or \r */
TESTCASE(getbnstring, "{7}\r\nfoo\nbar ", ' ', "foo\nbar", BLEN("{7}\r\nfoo\nbar "));
TESTCASE(getbnstring, "{7}\r\nfoo\rbar ", ' ', "foo\rbar", BLEN("{7}\r\nfoo\rbar "));
/* literals with 8-bit chars */
TESTCASE(getbnstring, "{7}\r\nfoo\277bar ", ' ', "foo\277bar", BLEN("{7}\r\nfoo\277bar "));
/* literals with embedded NUL - getbnstring() allows these */
TESTCASE(getbnstring, "{7}\r\nfoo\0bar ", ' ', "foo\0bar", BLEN("{7}\r\nfoo\0bar "));
}
/*
* getnastring() gets an astring, but with NIL returning NULL
*/
static void test_getnastring(void)
{
/* Simple sequences of ascii alphanumerics characters are atoms */
TESTCASE(getnastring, "hydrogen helium", ' ', "hydrogen", BLEN("hydrogen "));
TESTCASE(getnastring, "258 uranium", ' ', "258", BLEN("258 "));
TESTCASE(getnastring, "uranium258 plutonium", ' ', "uranium258", BLEN("uranium258 "));
/* The character sequence NIL is special, unless quoted */
TESTCASE_NULL(getnastring, "NIL by mouth", ' ', BLEN("NIL "));
TESTCASE(getnastring, "NELLY the lamb", ' ', "NELLY", BLEN("NELLY "));
TESTCASE(getnastring, "NILE in Egypt", ' ', "NILE", BLEN("NILE "));
/*
* List wildcards aren't part of an atom, but Cyrus accepts them
* in order to implement the "mailbox" and "list-mailbox" rules,
* which are like astrings but also allow unquoted wildcards,
* as astrings.
*/
TESTCASE(getnastring, "foo*bar baz", ' ', "foo*bar", BLEN("foo*bar "));
TESTCASE(getnastring, "baz%quux foo", ' ', "baz%quux", BLEN("baz%quux "));
/*
* Various special characters are not part of atoms.
*
* Again the server code is very liberal in accepting all kinds of
* things which aren't in the ABNF, so we test for the liberal
* interpretation and note the conservative one in a comment.
*/
TESTCASE(getnastring, "foo(bar baz", '(', "foo", BLEN("foo("));
TESTCASE(getnastring, "foo)bar baz", ')', "foo", BLEN("foo)"));
TESTCASE(getnastring, "foo{bar baz", ' ', "foo{bar", BLEN("foo{bar ")); /* should be: '{', "foo" */
TESTCASE(getnastring, "foo\"bar baz", '"', "foo", BLEN("foo\""));
TESTCASE(getnastring, "foo\\bar baz", ' ', "foo\\bar", BLEN("foo\\bar ")); /* should be: '\\', "foo" */
TESTCASE(getnastring, "foo]bar baz", ' ', "foo]bar", BLEN("foo]bar ")); /* should be ']', "foo" */
/*
* Quoted strings are astrings
*/
TESTCASE(getnastring, "\"foo\" bar", ' ', "foo", BLEN("\"foo\" "));
/* literals with embedded space */
TESTCASE(getnastring, "\"NIL\" by mouth ", ' ', "NIL", BLEN("\"NIL\" "));
TESTCASE(getnastring, "\"foo bar\" baz", ' ', "foo bar", BLEN("\"foo bar\" "));
TESTCASE(getnastring, "\"foo bar", EOF, "", BLEN("\"foo bar"));
TESTCASE(getnastring, "\"foo\\\"bar\" baz", ' ', "foo\"bar", BLEN("\"foo\\\"bar\" "));
TESTCASE(getnastring, "\"foo\\\\bar\" baz", ' ', "foo\\bar", BLEN("\"foo\\\\bar\" "));
/* Any non-special char can be escaped with \ */
TESTCASE(getnastring, "\"foo\\bar\" baz", ' ', "foobar", BLEN("\"foo\\bar\" "));
/* \n and \r can be escaped with \ */
TESTCASE(getnastring, "\"foo\\\nbar\" baz", ' ', "foo\nbar", BLEN("\"foo\\\nbar\" "));
TESTCASE(getnastring, "\"foo\\\rbar\" baz", ' ', "foo\rbar", BLEN("\"foo\\\rbar\" "));
/* Non-escaped \n and \r. The server is actually more
* conversative than the ABNF and rejects these. */
TESTCASE(getnastring, "\"foo\nbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\nbar" */
TESTCASE(getnastring, "\"foo\rbar\" baz", EOF, "", BLEN("\"foo")); /* should be ' ', "foo\rbar" */
/*
* Literals are astrings
*/
/* boring literal */
TESTCASE(getnastring, "{3}\r\nfoo ", ' ', "foo", BLEN("{3}\r\nfoo "));
/* literal NIL */
TESTCASE(getnastring, "{3}\r\nNIL ", ' ', "NIL", BLEN("{3}\r\nNIL "));
/* literals with embedded space */
TESTCASE(getnastring, "{7}\r\nfoo bar ", ' ', "foo bar", BLEN("{7}\r\nfoo bar "));
/* literals with embedded \n or \r */
TESTCASE(getnastring, "{7}\r\nfoo\nbar ", ' ', "foo\nbar", BLEN("{7}\r\nfoo\nbar "));
TESTCASE(getnastring, "{7}\r\nfoo\rbar ", ' ', "foo\rbar", BLEN("{7}\r\nfoo\rbar "));
/* literals with 8-bit chars */
TESTCASE(getnastring, "{7}\r\nfoo\277bar ", ' ', "foo\277bar", BLEN("{7}\r\nfoo\277bar ") );
/* literals with embedded NUL - getastring() rejects these */
TESTCASE(getnastring, "{7}\r\nfoo\0bar ", EOF, "", BLEN("{7}\r\nfoo\0bar")); /* should be ' ', "foo\0bar" */
}
/* vim: set ft=c: */