3
* test_config.c - test suite for nih/config.c
5
* Copyright Ā© 2009 Scott James Remnant <scott@netsplit.com>.
6
* Copyright Ā© 2009 Canonical Ltd.
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License version 2, as
10
* published by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License along
18
* with this program; if not, write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
#include <nih/macros.h>
29
#include <nih/alloc.h>
30
#include <nih/config.h>
32
#include <nih/error.h>
33
#include <nih/errors.h>
43
TEST_FUNCTION ("nih_config_has_token");
44
strcpy (buf, "this is a test # comment\n");
46
/* Check that an ordinary token character at the start of the line
47
* causes the function to return TRUE.
49
TEST_FEATURE ("with token at start of string");
50
ret = nih_config_has_token (buf, strlen (buf), NULL, NULL);
55
/* Check that an ordinary token inside the string causes the function
58
TEST_FEATURE ("with token inside string");
60
ret = nih_config_has_token (buf, strlen (buf), &pos, NULL);
65
/* Check that a piece of whitespace causes the function to return TRUE.
67
TEST_FEATURE ("with whitespace");
69
ret = nih_config_has_token (buf, strlen (buf), &pos, NULL);
74
/* Check that a comment character causes the function to return FALSE.
76
TEST_FEATURE ("with start of comment");
78
ret = nih_config_has_token (buf, strlen (buf), &pos, NULL);
83
/* Check that a newline character causes the function to return FALSE.
85
TEST_FEATURE ("with newline");
87
ret = nih_config_has_token (buf, strlen (buf), &pos, NULL);
92
/* Check that the end of file causes the function to return FALSE.
94
TEST_FEATURE ("at end of file");
96
ret = nih_config_has_token (buf, strlen (buf), &pos, NULL);
105
char buf[1024], dest[1024];
106
size_t pos, lineno, len;
110
TEST_FUNCTION ("nih_config_token");
111
program_name = "test";
113
/* Check that we can obtain the length of the first simple token
114
* in a string, and that the position is updated past it. The
115
* length of the token should be returned.
117
TEST_FEATURE ("with token at start of string");
118
strcpy (buf, "this is a test");
122
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
123
NULL, " ", FALSE, &len);
130
/* Check that we can obtain a length of a token that entirely fills
131
* the remainder of the file.
133
TEST_FEATURE ("with token filling string");
134
strcpy (buf, "wibble");
137
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
138
NULL, " ", FALSE, &len);
145
/* Check that we can extract a token from the string and have it
146
* copied into our destination buffer.
148
TEST_FEATURE ("with token to extract");
149
strcpy (buf, "this is a test");
150
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
151
dest, " ", FALSE, NULL);
154
TEST_EQ_STR (dest, "this");
157
/* Check that we can obtain the length of a simple token inside the
158
* string, and the the position is updated past it.
160
TEST_FEATURE ("with token inside string");
163
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
164
NULL, " ", FALSE, &len);
171
/* Check that we can obtain the length of a token that contains
172
* double quotes around the delimeter, the length should include
173
* the quoted part at the quotes.
175
TEST_FEATURE ("with double quotes inside token");
176
strcpy (buf, "\"this is a\" test");
179
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
180
NULL, " ", FALSE, &len);
187
/* Check that we can extract a token that is surrounded by double
188
* quotes, we should still get those.
190
TEST_FEATURE ("with double quotes around token to extract");
192
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
193
dest, " ", FALSE, &len);
197
TEST_EQ_STR (dest, "\"this is a\"");
200
/* Check that we can obtain the length of the quoted portion, with
201
* the quotes removed; the position should still point past it.
203
TEST_FEATURE ("with double quotes and dequoting");
206
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
207
NULL, " ", TRUE, &len);
214
/* Check that we can extract a quoted token and have the quotes
217
TEST_FEATURE ("with double quotes and extract with dequoting");
218
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
219
dest, " ", TRUE, NULL);
222
TEST_EQ_STR (dest, "this is a");
225
/* Check that we can obtain the length of a token that contains
226
* single quotes around the delimeter, the length should include
227
* the quoted part at the quotes.
229
TEST_FEATURE ("with single quotes inside token");
230
strcpy (buf, "\'this is a\' test");
233
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
234
NULL, " ", FALSE, &len);
241
/* Check that we can obtain the length of a token that contains
242
* escaped spaces around the delimeter, the length should include
245
TEST_FEATURE ("with escaped spaces inside token");
246
strcpy (buf, "this\\ is\\ a test");
249
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
250
NULL, " ", FALSE, &len);
257
/* Check that we can extract a token that contains escaped spaces
258
* around the delimiter.
260
TEST_FEATURE ("with escaped spaces within extracted token");
261
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
262
dest, " ", FALSE, &len);
266
TEST_EQ_STR (dest, "this\\ is\\ a");
269
/* Check that we can obtain the length of a token that contains
270
* escaped spaces around the delimeter, without the blackslashes.
272
TEST_FEATURE ("with escaped spaces inside token and dequoting");
275
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
276
NULL, " ", TRUE, &len);
283
/* Check that we can extract a token that contains escaped spaces
284
* around the delimiter, while removing them.
286
TEST_FEATURE ("with escaped spaces within extracted dequoted token");
288
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
289
dest, " ", TRUE, &len);
293
TEST_EQ_STR (dest, "this is a");
296
/* Check that a newline inside a quoted string, and surrounding
297
* whitespace, is treated as a single space character.
299
TEST_FEATURE ("with newline inside quoted string");
300
strcpy (buf, "\"this is \n a\" test");
304
ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
305
NULL, " ", FALSE, &len);
313
/* Check that extracting a token with a newline inside a quoted
314
* string only returns a single space for the newline.
316
TEST_FEATURE ("with newline inside extracted quoted string");
318
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
319
dest, " ", FALSE, &len);
323
TEST_EQ_STR (dest, "\"this is a\"");
326
/* Check that lineno is incremented when we encounter a newline
327
* inside a quoted string.
329
TEST_FEATURE ("with newline inside quoted string and lineno set");
333
ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
334
NULL, " ", FALSE, &len);
342
/* Check that an escaped newline, and surrounding whitespace, is
343
* treated as a single space character.
345
TEST_FEATURE ("with escaped newline");
346
strcpy (buf, "this \\\n is a:test");
350
ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
351
NULL, ":", FALSE, &len);
359
/* Check that extracting a token with an escaped newline inside it only
360
* returns a single space for the newline.
362
TEST_FEATURE ("with escaped newline inside extracted string");
363
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
364
dest, ":", FALSE, NULL);
367
TEST_EQ_STR (dest, "this is a");
370
/* Check that lineno is incremented when we encounter an escaped
373
TEST_FEATURE ("with escaped newline inside string and lineno set");
377
ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
378
NULL, ":", FALSE, &len);
386
/* Check that we can obtain the length of a token that contains
387
* escaped characters, the length should include the backslashes.
389
TEST_FEATURE ("with escaped characters inside token");
390
strcpy (buf, "this\\$FOO");
393
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
394
NULL, " ", FALSE, &len);
401
/* Check that we can extract a token that contains escaped
404
TEST_FEATURE ("with escaped characters within extracted token");
405
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
406
dest, " ", FALSE, &len);
410
TEST_EQ_STR (dest, "this\\$FOO");
413
/* Check that we can obtain the length of a token that contains
414
* escaped characters, including the backslashes, even though
417
TEST_FEATURE ("with escaped characters inside token and dequoting");
420
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
421
NULL, " ", TRUE, &len);
428
/* Check that we can extract a token that contains escaped characters,
429
* which should include the backslashes even though we're dequoting.
431
TEST_FEATURE ("with escaped characters within extracted dequoted token");
433
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
434
dest, " ", TRUE, &len);
438
TEST_EQ_STR (dest, "this\\$FOO");
441
/* Check that we can obtain the length of a token that contains
442
* escaped backslashes, the length should include the backslashes.
444
TEST_FEATURE ("with escaped backslashes inside token");
445
strcpy (buf, "this\\\\FOO");
448
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
449
NULL, " ", FALSE, &len);
456
/* Check that we can extract a token that contains escaped
459
TEST_FEATURE ("with escaped backslashes within extracted token");
460
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
461
dest, " ", FALSE, &len);
465
TEST_EQ_STR (dest, "this\\\\FOO");
468
/* Check that we can obtain the length of a token that contains
469
* escaped blackslashes, reduced to one since we're dequoting
471
TEST_FEATURE ("with escaped backslashes inside token and dequoting");
474
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
475
NULL, " ", TRUE, &len);
482
/* Check that we can extract a token that contains escaped backslashes,
483
* which should include only one of the backslashes because
486
TEST_FEATURE ("with escaped backslashes within extracted dequoted token");
488
ret = nih_config_token (buf, strlen (buf), NULL, NULL,
489
dest, " ", TRUE, &len);
493
TEST_EQ_STR (dest, "this\\FOO");
496
/* Check that a slash at the end of the file causes a parser error
497
* to be raised with pos and lineno set to the offending location.
499
TEST_FEATURE ("with slash at end of string");
500
strcpy (buf, "wibble\\");
505
ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
506
NULL, " ", FALSE, NULL);
512
err = nih_error_get ();
513
TEST_EQ (err->number, NIH_CONFIG_TRAILING_SLASH);
517
/* Ceck that an unterminated quote causes a parser error to be
518
* raised, with pos and lineno set to the offending location.
520
TEST_FEATURE ("with unterminated quote");
521
strcpy (buf, "\"wibble\n");
525
ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
526
NULL, " ", FALSE, NULL);
532
err = nih_error_get ();
533
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
537
/* Check that an empty token results in the position left unchanged
538
* and zero being returned,
540
TEST_FEATURE ("with empty token");
541
strcpy (buf, " wibble");
544
ret = nih_config_token (buf, strlen (buf), &pos, NULL,
545
NULL, " ", FALSE, &len);
553
test_next_token (void)
560
TEST_FUNCTION ("nih_config_next_token");
562
/* Check that we can extract a token at the start of a string,
563
* and have the position pointing past the whitespace to the next
566
TEST_FEATURE ("with token at start of string");
568
strcpy (buf, "this is a test");
571
str = nih_config_next_token (NULL, buf,
572
strlen (buf), &pos, NULL,
573
NIH_CONFIG_CNLWS, FALSE);
575
if (test_alloc_failed) {
576
TEST_EQ_P (str, NULL);
579
err = nih_error_get ();
580
TEST_EQ (err->number, ENOMEM);
586
TEST_ALLOC_SIZE (str, 5);
587
TEST_EQ_STR (str, "this");
593
/* Check that we can extract an argument inside a string
595
TEST_FEATURE ("with token inside string");
597
strcpy (buf, "this is a test");
600
str = nih_config_next_token (NULL, buf,
601
strlen (buf), &pos, NULL,
602
NIH_CONFIG_CNLWS, FALSE);
604
if (test_alloc_failed) {
605
TEST_EQ_P (str, NULL);
608
err = nih_error_get ();
609
TEST_EQ (err->number, ENOMEM);
615
TEST_ALLOC_SIZE (str, 3);
616
TEST_EQ_STR (str, "is");
622
/* Check that all trailing whitespace is eaten after the token. */
623
TEST_FEATURE ("with consecutive whitespace after token");
625
strcpy (buf, "this \t is a test");
628
str = nih_config_next_token (NULL, buf,
629
strlen (buf), &pos, NULL,
630
NIH_CONFIG_CNLWS, FALSE);
632
if (test_alloc_failed) {
633
TEST_EQ_P (str, NULL);
636
err = nih_error_get ();
637
TEST_EQ (err->number, ENOMEM);
643
TEST_ALLOC_SIZE (str, 5);
644
TEST_EQ_STR (str, "this");
650
/* Check that any escaped newlines in the whitespace are skipped
653
TEST_FEATURE ("with escaped newlines in whitespace");
655
strcpy (buf, "this \\\n is a test");
658
str = nih_config_next_token (NULL, buf,
659
strlen (buf), &pos, NULL,
660
NIH_CONFIG_CNLWS, FALSE);
662
if (test_alloc_failed) {
663
TEST_EQ_P (str, NULL);
666
err = nih_error_get ();
667
TEST_EQ (err->number, ENOMEM);
673
TEST_ALLOC_SIZE (str, 5);
674
TEST_EQ_STR (str, "this");
680
/* Check that the line number is incremented for any escaped newlines
683
TEST_FEATURE ("with line number set");
688
str = nih_config_next_token (NULL, buf,
689
strlen (buf), &pos, &lineno,
690
NIH_CONFIG_CNLWS, FALSE);
692
if (test_alloc_failed) {
693
TEST_EQ_P (str, NULL);
697
err = nih_error_get ();
698
TEST_EQ (err->number, ENOMEM);
705
TEST_ALLOC_SIZE (str, 5);
706
TEST_EQ_STR (str, "this");
712
/* Check that the returned token can have the quotes left in it,
713
* but the whitespace around the newline collapsed.
715
TEST_FEATURE ("with token containing quotes");
717
strcpy (buf, "\"this \\\n is\" a test");
720
str = nih_config_next_token (NULL, buf,
721
strlen (buf), &pos, NULL,
722
NIH_CONFIG_CNLWS, FALSE);
724
if (test_alloc_failed) {
725
TEST_EQ_P (str, NULL);
728
err = nih_error_get ();
729
TEST_EQ (err->number, ENOMEM);
735
TEST_ALLOC_SIZE (str, 10);
736
TEST_EQ_STR (str, "\"this is\"");
742
/* Check that the returned token can be thoroughly dequoted and any
743
* whitespace around an embedded newline collapsed to a single
746
TEST_FEATURE ("with quoted whitespace and newline in token");
748
strcpy (buf, "\"this \\\n is\" a test");
751
str = nih_config_next_token (NULL, buf,
752
strlen (buf), &pos, NULL,
753
NIH_CONFIG_CNLWS, TRUE);
755
if (test_alloc_failed) {
756
TEST_EQ_P (str, NULL);
759
err = nih_error_get ();
760
TEST_EQ (err->number, ENOMEM);
766
TEST_ALLOC_SIZE (str, 8);
767
TEST_EQ_STR (str, "this is");
773
/* Check that an error is raised if there is no token at that
776
TEST_FEATURE ("with empty line");
778
strcpy (buf, "\nthis is a test");
782
str = nih_config_next_token (NULL, buf,
783
strlen (buf), &pos, &lineno,
784
NIH_CONFIG_CNLWS, FALSE);
786
TEST_EQ_P (str, NULL);
790
err = nih_error_get ();
791
TEST_EQ (err->number, NIH_CONFIG_EXPECTED_TOKEN);
796
/* Check that a parse error being found with the argument causes an
797
* error to be raised, with pos and lineno at the site of the error.
799
TEST_FEATURE ("with parser error");
801
strcpy (buf, "\"this is a test\nand so is this");
805
str = nih_config_next_token (NULL, buf,
806
strlen (buf), &pos, &lineno,
807
NIH_CONFIG_CNLWS, FALSE);
809
TEST_EQ_P (str, NULL);
813
err = nih_error_get ();
814
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
827
TEST_FUNCTION ("nih_config_next_arg");
829
/* Check that we can extract an argument at the start of a string,
830
* and have the position pointing past the whitespace to the next
833
TEST_FEATURE ("with argument at start of string");
835
strcpy (buf, "this is a test");
838
str = nih_config_next_arg (NULL, buf,
839
strlen (buf), &pos, NULL);
841
if (test_alloc_failed) {
842
TEST_EQ_P (str, NULL);
845
err = nih_error_get ();
846
TEST_EQ (err->number, ENOMEM);
852
TEST_ALLOC_SIZE (str, 5);
853
TEST_EQ_STR (str, "this");
859
/* Check that we can extract an argument inside a string
861
TEST_FEATURE ("with argument inside string");
863
strcpy (buf, "this is a test");
866
str = nih_config_next_arg (NULL, buf,
867
strlen (buf), &pos, NULL);
869
if (test_alloc_failed) {
870
TEST_EQ_P (str, NULL);
873
err = nih_error_get ();
874
TEST_EQ (err->number, ENOMEM);
880
TEST_ALLOC_SIZE (str, 3);
881
TEST_EQ_STR (str, "is");
887
/* Check that all trailing whitespace is eaten after the argument. */
888
TEST_FEATURE ("with consecutive whitespace after argument");
890
strcpy (buf, "this \t is a test");
893
str = nih_config_next_arg (NULL, buf,
894
strlen (buf), &pos, NULL);
896
if (test_alloc_failed) {
897
TEST_EQ_P (str, NULL);
900
err = nih_error_get ();
901
TEST_EQ (err->number, ENOMEM);
907
TEST_ALLOC_SIZE (str, 5);
908
TEST_EQ_STR (str, "this");
914
/* Check that any escaped newlines in the whitespace are skipped
917
TEST_FEATURE ("with escaped newlines in whitespace");
919
strcpy (buf, "this \\\n is a test");
922
str = nih_config_next_arg (NULL, buf,
923
strlen (buf), &pos, NULL);
925
if (test_alloc_failed) {
926
TEST_EQ_P (str, NULL);
929
err = nih_error_get ();
930
TEST_EQ (err->number, ENOMEM);
936
TEST_ALLOC_SIZE (str, 5);
937
TEST_EQ_STR (str, "this");
943
/* Check that the line number is incremented for any escaped newlines
946
TEST_FEATURE ("with line number set");
951
str = nih_config_next_arg (NULL, buf,
952
strlen (buf), &pos, &lineno);
954
if (test_alloc_failed) {
955
TEST_EQ_P (str, NULL);
959
err = nih_error_get ();
960
TEST_EQ (err->number, ENOMEM);
967
TEST_ALLOC_SIZE (str, 5);
968
TEST_EQ_STR (str, "this");
974
/* Check that the returned argument is thoroughly dequoted and any
975
* whitespace around an embedded newline collapsed to a single
978
TEST_FEATURE ("with quoted whitespace and newline in arg");
980
strcpy (buf, "\"this \\\n is\" a test");
983
str = nih_config_next_arg (NULL, buf,
984
strlen (buf), &pos, NULL);
986
if (test_alloc_failed) {
987
TEST_EQ_P (str, NULL);
990
err = nih_error_get ();
991
TEST_EQ (err->number, ENOMEM);
997
TEST_ALLOC_SIZE (str, 8);
998
TEST_EQ_STR (str, "this is");
1004
/* Check that an error is raised if there is no argument at that
1007
TEST_FEATURE ("with empty line");
1009
strcpy (buf, "\nthis is a test");
1013
str = nih_config_next_arg (NULL, buf,
1014
strlen (buf), &pos, &lineno);
1016
TEST_EQ_P (str, NULL);
1018
TEST_EQ (lineno, 1);
1020
err = nih_error_get ();
1021
TEST_EQ (err->number, NIH_CONFIG_EXPECTED_TOKEN);
1026
/* Check that a parse error being found with the argument causes an
1027
* error to be raised, with pos and lineno at the site of the error.
1029
TEST_FEATURE ("with parser error");
1031
strcpy (buf, "\"this is a test\nand so is this");
1035
str = nih_config_next_arg (NULL, buf,
1036
strlen (buf), &pos, &lineno);
1038
TEST_EQ_P (str, NULL);
1040
TEST_EQ (lineno, 2);
1042
err = nih_error_get ();
1043
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
1049
test_next_line (void)
1054
TEST_FUNCTION ("nih_config_next_line");
1056
/* Check that we can skip a number of characters until the newline,
1057
* pointing pos past it.
1059
TEST_FEATURE ("with simple string");
1060
strcpy (buf, "this is a test\nand so is this\n");
1063
nih_config_next_line (buf, strlen (buf), &pos, NULL);
1068
/* Check that lineno is incremented when we step over it.
1070
TEST_FEATURE ("with line number set");
1074
nih_config_next_line (buf, strlen (buf), &pos, &lineno);
1077
TEST_EQ (lineno, 2);
1080
/* Check that pos is only incremented by a single step if the
1081
* character underneath is a newline.
1083
TEST_FEATURE ("with newline at position");
1084
strcpy (buf, "\nthis is a test");
1088
nih_config_next_line (buf, strlen (buf), &pos, &lineno);
1091
TEST_EQ (lineno, 2);
1094
/* Check that the end of file can be reached without error.
1096
TEST_FEATURE ("with no newline before end of file");
1097
strcpy (buf, "this is a test");
1100
nih_config_next_line (buf, strlen (buf), &pos, NULL);
1106
test_skip_whitespace (void)
1111
TEST_FUNCTION ("nih_config_next_whitespace");
1113
/* Check that we can skip an amount of plain whitespace characters
1114
* until the next token, pointing pos at is.
1116
TEST_FEATURE ("with plain whitespace");
1117
strcpy (buf, "a plain string\n");
1121
nih_config_skip_whitespace (buf, strlen (buf), &pos, &lineno);
1124
TEST_EQ (lineno, 1);
1127
/* Check that we can skip a more complex series of whitespace
1128
* characters until the next token.
1130
TEST_FEATURE ("with complex whitespace");
1131
strcpy (buf, "a more \t \r complex string\n");
1135
nih_config_skip_whitespace (buf, strlen (buf), &pos, &lineno);
1138
TEST_EQ (lineno, 1);
1141
/* Check that we can skip whitespace characters up until the end
1142
* of the line, but that we don't step over it.
1144
TEST_FEATURE ("with whitespace at end of line");
1145
strcpy (buf, "trailing whitespace \t\r\n");
1149
nih_config_skip_whitespace (buf, strlen (buf), &pos, &lineno);
1152
TEST_EQ (lineno, 1);
1155
/* Check that we step over an escaped newline embedded in the
1156
* whitespace, and increment lineno.
1158
TEST_FEATURE ("with escaped newline");
1159
strcpy (buf, "this has \\\n a newline");
1163
nih_config_skip_whitespace (buf, strlen (buf), &pos, &lineno);
1166
TEST_EQ (lineno, 2);
1170
test_skip_comment (void)
1177
TEST_FUNCTION ("nih_config_next_line");
1179
/* Check that we can skip a number of comment characters until the
1180
* newline, pointing pos past it.
1182
TEST_FEATURE ("with simple string");
1183
strcpy (buf, "# this is a test\nand so is this\n");
1186
ret = nih_config_skip_comment (buf, strlen (buf), &pos, NULL);
1192
/* Check that lineno is incremented when we step over it.
1194
TEST_FEATURE ("with line number set");
1198
ret = nih_config_skip_comment (buf, strlen (buf), &pos, &lineno);
1202
TEST_EQ (lineno, 2);
1205
/* Check that pos is only incremented by a single step if the
1206
* character underneath is a newline.
1208
TEST_FEATURE ("with newline at position");
1209
strcpy (buf, "\nthis is a test");
1213
ret = nih_config_skip_comment (buf, strlen (buf), &pos, &lineno);
1217
TEST_EQ (lineno, 2);
1220
/* Check that the end of file can be reached without error.
1222
TEST_FEATURE ("with no newline before end of file");
1223
strcpy (buf, "# this is a test");
1226
ret = nih_config_skip_comment (buf, strlen (buf), &pos, NULL);
1232
/* Check that attempting to skip an ordinary argument results in
1235
TEST_FEATURE ("with attempt to skip argument");
1236
strcpy (buf, "this is a test\nand so it this\n");
1239
ret = nih_config_skip_comment (buf, strlen (buf), &pos, NULL);
1244
err = nih_error_get ();
1245
TEST_EQ (err->number, NIH_CONFIG_UNEXPECTED_TOKEN);
1251
test_parse_args (void)
1258
TEST_FUNCTION ("nih_config_parse_args");
1260
/* Check that we can parse a list of arguments from the start of
1261
* a simple string. They should be returned as a NULL-terminated
1262
* array of strings, and the position should be updated to point to
1263
* the start of the next line.
1265
TEST_FEATURE ("with args at start of simple string");
1267
strcpy (buf, "this is a test\nand so is this\n");
1270
args = nih_config_parse_args (NULL, buf,
1271
strlen (buf), &pos, NULL);
1273
if (test_alloc_failed) {
1274
TEST_EQ_P (args, NULL);
1276
err = nih_error_get ();
1277
TEST_EQ (err->number, ENOMEM);
1283
TEST_ALLOC_SIZE (args, sizeof (char *) * 5);
1284
TEST_ALLOC_PARENT (args[0], args);
1285
TEST_ALLOC_PARENT (args[1], args);
1286
TEST_ALLOC_PARENT (args[2], args);
1287
TEST_ALLOC_PARENT (args[3], args);
1288
TEST_EQ_STR (args[0], "this");
1289
TEST_EQ_STR (args[1], "is");
1290
TEST_EQ_STR (args[2], "a");
1291
TEST_EQ_STR (args[3], "test");
1292
TEST_EQ_P (args[4], NULL);
1298
/* Check that we can parse a list of arguments from a position
1299
* inside an existing string.
1301
TEST_FEATURE ("with args inside simple string");
1305
args = nih_config_parse_args (NULL, buf,
1306
strlen (buf), &pos, NULL);
1308
if (test_alloc_failed) {
1309
TEST_EQ_P (args, NULL);
1311
err = nih_error_get ();
1312
TEST_EQ (err->number, ENOMEM);
1318
TEST_ALLOC_SIZE (args, sizeof (char *) * 4);
1319
TEST_EQ_STR (args[0], "is");
1320
TEST_EQ_STR (args[1], "a");
1321
TEST_EQ_STR (args[2], "test");
1322
TEST_EQ_P (args[3], NULL);
1328
/* Check that we can parse a list of arguments up to the end of the
1329
* file, which doesn't have a newline.
1331
TEST_FEATURE ("with args up to end of string");
1333
strcpy (buf, "this is a test");
1336
args = nih_config_parse_args (NULL, buf,
1337
strlen (buf), &pos, NULL);
1339
if (test_alloc_failed) {
1340
TEST_EQ_P (args, NULL);
1342
err = nih_error_get ();
1343
TEST_EQ (err->number, ENOMEM);
1349
TEST_ALLOC_SIZE (args, sizeof (char *) * 5);
1350
TEST_EQ_STR (args[0], "this");
1351
TEST_EQ_STR (args[1], "is");
1352
TEST_EQ_STR (args[2], "a");
1353
TEST_EQ_STR (args[3], "test");
1354
TEST_EQ_P (args[4], NULL);
1360
/* Check that we can ignore a comment at the end of the line, the
1361
* position should be updated past the comment onto the next line.
1363
TEST_FEATURE ("with args up to comment");
1365
strcpy (buf, "this is a test # comment\nand so is this\n");
1368
args = nih_config_parse_args (NULL, buf,
1369
strlen (buf), &pos, NULL);
1371
if (test_alloc_failed) {
1372
TEST_EQ_P (args, NULL);
1374
err = nih_error_get ();
1375
TEST_EQ (err->number, ENOMEM);
1381
TEST_ALLOC_SIZE (args, sizeof (char *) * 5);
1382
TEST_EQ_STR (args[0], "this");
1383
TEST_EQ_STR (args[1], "is");
1384
TEST_EQ_STR (args[2], "a");
1385
TEST_EQ_STR (args[3], "test");
1386
TEST_EQ_P (args[4], NULL);
1392
/* Check that we can ignore a comment at the end of the file, the
1393
* position should be updated past the end.
1395
TEST_FEATURE ("with args up to comment at end of file");
1397
strcpy (buf, "this is a test # comment");
1400
args = nih_config_parse_args (NULL, buf,
1401
strlen (buf), &pos, NULL);
1403
if (test_alloc_failed) {
1404
TEST_EQ_P (args, NULL);
1406
err = nih_error_get ();
1407
TEST_EQ (err->number, ENOMEM);
1413
TEST_ALLOC_SIZE (args, sizeof (char *) * 5);
1414
TEST_EQ_STR (args[0], "this");
1415
TEST_EQ_STR (args[1], "is");
1416
TEST_EQ_STR (args[2], "a");
1417
TEST_EQ_STR (args[3], "test");
1418
TEST_EQ_P (args[4], NULL);
1424
/* Check that the line number is incremented when a new line is
1427
TEST_FEATURE ("with line number given");
1429
strcpy (buf, "this is a test\nand so is this\n");
1433
args = nih_config_parse_args (NULL, buf,
1434
strlen (buf), &pos, &lineno);
1436
if (test_alloc_failed) {
1437
TEST_EQ_P (args, NULL);
1439
err = nih_error_get ();
1440
TEST_EQ (err->number, ENOMEM);
1446
TEST_EQ (lineno, 2);
1452
/* Check that consecutive whitespace, including escaped newlines,
1453
* are treated as a single delimeter. The line number should be
1454
* incremented for both the embedded one and final one.
1456
TEST_FEATURE ("with multiple whitespace between arguments");
1458
strcpy (buf, "this is \t a \\\n test\nand so is this\n");
1462
args = nih_config_parse_args (NULL, buf,
1463
strlen (buf), &pos, &lineno);
1465
if (test_alloc_failed) {
1466
TEST_EQ_P (args, NULL);
1468
err = nih_error_get ();
1469
TEST_EQ (err->number, ENOMEM);
1475
TEST_EQ (lineno, 3);
1476
TEST_ALLOC_SIZE (args, sizeof (char *) * 5);
1477
TEST_EQ_STR (args[0], "this");
1478
TEST_EQ_STR (args[1], "is");
1479
TEST_EQ_STR (args[2], "a");
1480
TEST_EQ_STR (args[3], "test");
1481
TEST_EQ_P (args[4], NULL);
1487
/* Check that each argument can be delimited by quotes, contain
1488
* quoted newlines, and each is dequoted before being stored in the
1491
TEST_FEATURE ("with whitespace inside arguments");
1493
strcpy (buf, "\"this is\" \"a\ntest\" \\\n and so\nis this\n");
1497
args = nih_config_parse_args (NULL, buf,
1498
strlen (buf), &pos, &lineno);
1500
if (test_alloc_failed) {
1501
TEST_EQ_P (args, NULL);
1503
err = nih_error_get ();
1504
TEST_EQ (err->number, ENOMEM);
1510
TEST_EQ (lineno, 4);
1511
TEST_ALLOC_SIZE (args, sizeof (char *) * 5);
1512
TEST_EQ_STR (args[0], "this is");
1513
TEST_EQ_STR (args[1], "a test");
1514
TEST_EQ_STR (args[2], "and");
1515
TEST_EQ_STR (args[3], "so");
1516
TEST_EQ_P (args[4], NULL);
1522
/* Check that an empty line results in a one element array being
1523
* returned containing only NULL, and the position being incremented
1524
* past the empty line.
1526
TEST_FEATURE ("with empty line");
1528
strcpy (buf, "\nand so is this\n");
1531
args = nih_config_parse_args (NULL, buf,
1532
strlen (buf), &pos, NULL);
1534
if (test_alloc_failed) {
1535
TEST_EQ_P (args, NULL);
1537
err = nih_error_get ();
1538
TEST_EQ (err->number, ENOMEM);
1544
TEST_ALLOC_SIZE (args, sizeof (char *) * 1);
1545
TEST_EQ_P (args[0], NULL);
1551
/* Check that a line containing only a comment results in a one
1552
* element array being returned containing only NULL, and the
1553
* position being incremented past the comment and newline.
1555
TEST_FEATURE ("with only comment in line");
1557
strcpy (buf, "# line with comment\nand so is this\n");
1560
args = nih_config_parse_args (NULL, buf,
1561
strlen (buf), &pos, NULL);
1563
if (test_alloc_failed) {
1564
TEST_EQ_P (args, NULL);
1566
err = nih_error_get ();
1567
TEST_EQ (err->number, ENOMEM);
1573
TEST_ALLOC_SIZE (args, sizeof (char *) * 1);
1574
TEST_EQ_P (args[0], NULL);
1580
/* Check that an error parsing the arguments results in NULL being
1581
* returned and the error raised.
1583
TEST_FEATURE ("with parser error");
1585
strcpy (buf, "this is a \"test\nand so is this\n");
1589
args = nih_config_parse_args (NULL, buf,
1590
strlen (buf), &pos, &lineno);
1592
TEST_EQ_P (args, NULL);
1593
if (! test_alloc_failed) {
1595
TEST_EQ (lineno, 3);
1598
err = nih_error_get ();
1599
if (! test_alloc_failed)
1600
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
1606
test_parse_command (void)
1613
TEST_FUNCTION ("nih_config_parse_command");
1615
/* Check that we can parse a command from the start of a simple
1616
* string. It should be returned as an allocated string and the
1617
* position should be updated to point to the start of the next line.
1619
TEST_FEATURE ("with command at start of simple string");
1621
strcpy (buf, "this is a test\nand so is this\n");
1624
str = nih_config_parse_command (NULL, buf,
1625
strlen (buf), &pos, NULL);
1627
if (test_alloc_failed) {
1628
TEST_EQ_P (str, NULL);
1631
err = nih_error_get ();
1632
TEST_EQ (err->number, ENOMEM);
1638
TEST_ALLOC_SIZE (str, 15);
1639
TEST_EQ_STR (str, "this is a test");
1645
/* Check that we can parse a command from inside a string.
1647
TEST_FEATURE ("with command inside simple string");
1649
strcpy (buf, "this is a test\nand so is this\n");
1652
str = nih_config_parse_command (NULL, buf,
1653
strlen (buf), &pos, NULL);
1655
if (test_alloc_failed) {
1656
TEST_EQ_P (str, NULL);
1659
err = nih_error_get ();
1660
TEST_EQ (err->number, ENOMEM);
1666
TEST_ALLOC_SIZE (str, 10);
1667
TEST_EQ_STR (str, "is a test");
1673
/* Check that we can parse a command that ends with the end of file.
1675
TEST_FEATURE ("with command at end of file");
1677
strcpy (buf, "this is a test");
1680
str = nih_config_parse_command (NULL, buf,
1681
strlen (buf), &pos, NULL);
1683
if (test_alloc_failed) {
1684
TEST_EQ_P (str, NULL);
1687
err = nih_error_get ();
1688
TEST_EQ (err->number, ENOMEM);
1694
TEST_ALLOC_SIZE (str, 15);
1695
TEST_EQ_STR (str, "this is a test");
1701
/* Check that we can parse a command that ends with a comment,
1702
* but the position should be incremented past the end of the comment.
1704
TEST_FEATURE ("with command up to comment");
1706
strcpy (buf, ("this is a test # this is a comment\n"
1707
"and so is this\n"));
1711
str = nih_config_parse_command (NULL, buf, strlen (buf), &pos,
1714
if (test_alloc_failed) {
1715
TEST_EQ_P (str, NULL);
1717
TEST_EQ (lineno, 2);
1719
err = nih_error_get ();
1720
TEST_EQ (err->number, ENOMEM);
1726
TEST_EQ (lineno, 2);
1727
TEST_ALLOC_SIZE (str, 15);
1728
TEST_EQ_STR (str, "this is a test");
1734
/* Check that we can parse a command that ends with a comment which
1735
* runs up to the end of file.
1737
TEST_FEATURE ("with command up to comment at end of file");
1739
strcpy (buf, "this is a test # this is a comment");
1743
str = nih_config_parse_command (NULL, buf, strlen (buf), &pos,
1746
if (test_alloc_failed) {
1747
TEST_EQ_P (str, NULL);
1749
TEST_EQ (lineno, 1);
1751
err = nih_error_get ();
1752
TEST_EQ (err->number, ENOMEM);
1758
TEST_EQ (lineno, 1);
1759
TEST_ALLOC_SIZE (str, 15);
1760
TEST_EQ_STR (str, "this is a test");
1766
/* Check that the command is returned including any quotes,
1767
* consecutive whitespace, but with any whitespace around a quoted
1768
* or escaped newline collapsed to a single space.
1770
TEST_FEATURE ("with quotes, whitespace and newlines in string");
1772
strcpy (buf, ("\"this is\" a \"test \\\n of\" \\\n "
1773
"commands\nfoo\n"));
1777
str = nih_config_parse_command (NULL, buf, strlen (buf), &pos,
1780
if (test_alloc_failed) {
1781
TEST_EQ_P (str, NULL);
1784
err = nih_error_get ();
1785
TEST_EQ (err->number, ENOMEM);
1791
TEST_EQ (lineno, 4);
1792
TEST_ALLOC_SIZE (str, 33);
1793
TEST_EQ_STR (str, "\"this is\" a \"test of\" commands");
1799
/* Check that we can parse an empty line, and have the empty string
1800
* returned. The position should be updated past the newline.
1802
TEST_FEATURE ("with empty line");
1804
strcpy (buf, "\nthis is a test\n");
1807
str = nih_config_parse_command (NULL, buf,
1808
strlen (buf), &pos, NULL);
1810
if (test_alloc_failed) {
1811
TEST_EQ_P (str, NULL);
1814
err = nih_error_get ();
1815
TEST_EQ (err->number, ENOMEM);
1821
TEST_ALLOC_SIZE (str, 1);
1822
TEST_EQ_STR (str, "");
1828
/* Check that we can parse a line containing only whitespace, and
1829
* have the empty string returned. The position should be updated
1832
TEST_FEATURE ("with only whitespace in line");
1834
strcpy (buf, " \t \nthis is a test\n");
1837
str = nih_config_parse_command (NULL, buf,
1838
strlen (buf), &pos, NULL);
1840
if (test_alloc_failed) {
1841
TEST_EQ_P (str, NULL);
1844
err = nih_error_get ();
1845
TEST_EQ (err->number, ENOMEM);
1851
TEST_ALLOC_SIZE (str, 1);
1852
TEST_EQ_STR (str, "");
1858
/* Check that we can parse a line with a comment in it, and have
1859
* the empty string returned. The position should be updated past
1862
TEST_FEATURE ("with only comment in line");
1864
strcpy (buf, "# this is a test\nthis is a test\n");
1867
str = nih_config_parse_command (NULL, buf,
1868
strlen (buf), &pos, NULL);
1870
if (test_alloc_failed) {
1871
TEST_EQ_P (str, NULL);
1874
err = nih_error_get ();
1875
TEST_EQ (err->number, ENOMEM);
1881
TEST_ALLOC_SIZE (str, 1);
1882
TEST_EQ_STR (str, "");
1888
/* Check that we can parse a line with whitespace before a comment,
1889
* and have the empty string returned. The position should be updated
1892
TEST_FEATURE ("with whitespace and comment in line");
1894
strcpy (buf, " # this is a test\nthis is a test\n");
1897
str = nih_config_parse_command (NULL, buf,
1898
strlen (buf), &pos, NULL);
1900
if (test_alloc_failed) {
1901
TEST_EQ_P (str, NULL);
1904
err = nih_error_get ();
1905
TEST_EQ (err->number, ENOMEM);
1911
TEST_ALLOC_SIZE (str, 1);
1912
TEST_EQ_STR (str, "");
1918
/* Check that a parser error while reading the command results in
1919
* NULL being returned and the error raised.
1921
TEST_FEATURE ("with parser error");
1923
strcpy (buf, "this is a \"test\nand so is this\n");
1927
str = nih_config_parse_command (NULL, buf, strlen (buf), &pos,
1930
TEST_EQ_P (str, NULL);
1932
TEST_EQ (lineno, 3);
1934
err = nih_error_get ();
1935
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
1942
test_parse_block (void)
1949
TEST_FUNCTION ("nih_config_parse_block");
1950
program_name = "test";
1953
/* Check that we can parse consecutive lines until we reach one
1954
* that ends the block. The block should be returned as an allocated
1955
* string with each line in it, except the terminator; the position
1956
* should be positioned after the end of the terminator.
1958
TEST_FEATURE ("with simple block");
1960
strcpy (buf, "this is\na test\nend foo\nblah\n");
1963
str = nih_config_parse_block (NULL, buf,
1964
strlen (buf), &pos, NULL, "foo");
1966
if (test_alloc_failed) {
1967
TEST_EQ_P (str, NULL);
1970
err = nih_error_get ();
1971
TEST_EQ (err->number, ENOMEM);
1977
TEST_ALLOC_SIZE (str, 16);
1978
TEST_EQ_STR (str, "this is\na test\n");
1984
/* Check that the line number is incremented for each line that we
1985
* discover in the block, including the terminating line.
1987
TEST_FEATURE ("with line number set");
1992
str = nih_config_parse_block (NULL, buf,
1993
strlen (buf), &pos, &lineno,
1996
if (test_alloc_failed) {
1997
TEST_EQ_P (str, NULL);
2000
err = nih_error_get ();
2001
TEST_EQ (err->number, ENOMEM);
2007
TEST_EQ (lineno, 5);
2008
TEST_ALLOC_SIZE (str, 16);
2009
TEST_EQ_STR (str, "this is\na test\n");
2015
/* Check that the common initial whitespace from each line is stripped,
2016
* where common is defined as identical character sequences, not number
2017
* of whitespace chars.
2019
TEST_FEATURE ("with whitespace at start of block");
2021
strcpy (buf, " this is\n \t a test\nend foo\nblah\n");
2024
str = nih_config_parse_block (NULL, buf,
2025
strlen (buf), &pos, NULL, "foo");
2027
if (test_alloc_failed) {
2028
TEST_EQ_P (str, NULL);
2031
err = nih_error_get ();
2032
TEST_EQ (err->number, ENOMEM);
2038
TEST_ALLOC_SIZE (str, 20);
2039
TEST_EQ_STR (str, " this is\n\t a test\n");
2045
/* Check that we can parse a block that ends in a terminator with
2046
* extraneous whitespace around the words.
2048
TEST_FEATURE ("with whitespace in terminator");
2050
strcpy (buf, "this is\na test\n end \t foo \nblah\n");
2053
str = nih_config_parse_block (NULL, buf,
2054
strlen (buf), &pos, NULL, "foo");
2056
if (test_alloc_failed) {
2057
TEST_EQ_P (str, NULL);
2060
err = nih_error_get ();
2061
TEST_EQ (err->number, ENOMEM);
2067
TEST_ALLOC_SIZE (str, 16);
2068
TEST_EQ_STR (str, "this is\na test\n");
2074
/* Check that we can parse a block that ends in a terminator which
2075
* is at the end of the file.
2077
TEST_FEATURE ("with terminator at end of file");
2079
strcpy (buf, "this is\na test\nend foo");
2082
str = nih_config_parse_block (NULL, buf,
2083
strlen (buf), &pos, NULL, "foo");
2085
if (test_alloc_failed) {
2086
TEST_EQ_P (str, NULL);
2089
err = nih_error_get ();
2090
TEST_EQ (err->number, ENOMEM);
2096
TEST_ALLOC_SIZE (str, 16);
2097
TEST_EQ_STR (str, "this is\na test\n");
2103
/* Check that we can parse a block that ends in a terminator which
2104
* has a comment following it.
2106
TEST_FEATURE ("with terminator and comment");
2108
strcpy (buf, "this is\na test\nend foo # comment\ntest\n");
2111
str = nih_config_parse_block (NULL, buf,
2112
strlen (buf), &pos, NULL, "foo");
2114
if (test_alloc_failed) {
2115
TEST_EQ_P (str, NULL);
2118
err = nih_error_get ();
2119
TEST_EQ (err->number, ENOMEM);
2125
TEST_ALLOC_SIZE (str, 16);
2126
TEST_EQ_STR (str, "this is\na test\n");
2132
/* Check that we can parse a block that ends in a terminator which
2133
* has a comment and then the end of file.
2135
TEST_FEATURE ("with terminator and comment at end of file");
2137
strcpy (buf, "this is\na test\nend foo # comment");
2140
str = nih_config_parse_block (NULL, buf,
2141
strlen (buf), &pos, NULL, "foo");
2143
if (test_alloc_failed) {
2144
TEST_EQ_P (str, NULL);
2147
err = nih_error_get ();
2148
TEST_EQ (err->number, ENOMEM);
2154
TEST_ALLOC_SIZE (str, 16);
2155
TEST_EQ_STR (str, "this is\na test\n");
2161
/* Check that various bogus forms of terminator are ignored.
2163
TEST_FEATURE ("with various things that aren't terminators");
2165
strcpy (buf, "endfoo\nend a\nend fooish\nend foo\ntest\n");
2168
str = nih_config_parse_block (NULL, buf,
2169
strlen (buf), &pos, NULL, "foo");
2171
if (test_alloc_failed) {
2172
TEST_EQ_P (str, NULL);
2175
err = nih_error_get ();
2176
TEST_EQ (err->number, ENOMEM);
2182
TEST_ALLOC_SIZE (str, 25);
2183
TEST_EQ_STR (str, "endfoo\nend a\nend fooish\n");
2189
/* Check that reaching the end of the file without finding the block
2190
* terminator causes an error to be raised and NULL to be returned.
2192
TEST_FEATURE ("with no terminator before end of file");
2194
strcpy (buf, "this is\na test\n");
2198
str = nih_config_parse_block (NULL, buf,
2199
strlen (buf), &pos, &lineno,
2202
TEST_EQ_P (str, NULL);
2204
TEST_EQ (lineno, 4);
2206
err = nih_error_get ();
2207
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_BLOCK);
2213
test_skip_block (void)
2217
size_t pos, lineno, endpos;
2220
TEST_FUNCTION ("nih_config_skip_block");
2221
program_name = "test";
2224
/* Check that we can find the end of a simple block. pos should be
2225
* updated to point past the block, and the returned endpos should
2226
* point at the end of the block itself.
2228
TEST_FEATURE ("with simple block");
2229
strcpy (buf, "this is\na test\nend foo\nblah\n");
2232
ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
2237
TEST_EQ (endpos, 15);
2240
/* Check that the line number is incremented for each line that we
2241
* discover in the block, including the terminating line.
2243
TEST_FEATURE ("with line number set");
2247
ret = nih_config_skip_block (buf, strlen (buf), &pos, &lineno,
2252
TEST_EQ (endpos, 15);
2253
TEST_EQ (lineno, 5);
2256
/* Check that we can find the end of a block that ends in a terminator
2257
* with extraneous whitespace around the words.
2259
TEST_FEATURE ("with whitespace in terminator");
2260
strcpy (buf, "this is\na test\n end \t foo \nblah\n");
2263
ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
2268
TEST_EQ (endpos, 15);
2271
/* Check that we can find the end of a block that ends in a
2272
* terminator which is at the end of the file.
2274
TEST_FEATURE ("with terminator at end of file");
2275
strcpy (buf, "this is\na test\nend foo");
2278
ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
2283
TEST_EQ (endpos, 15);
2286
/* Check that we can find the end of a block that ends in a
2287
* terminator which has a comment following it.
2289
TEST_FEATURE ("with terminator and comment");
2290
strcpy (buf, "this is\na test\nend foo # comment\ntest\n");
2293
ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
2298
TEST_EQ (endpos, 15);
2301
/* Check that we can find the end of a block that ends in a
2302
* terminator which has a comment and then the end of file.
2304
TEST_FEATURE ("with terminator and comment at end of file");
2305
strcpy (buf, "this is\na test\nend foo # comment");
2308
ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
2313
TEST_EQ (endpos, 15);
2316
/* Check that various bogus forms of terminator are ignored.
2318
TEST_FEATURE ("with various things that aren't terminators");
2319
strcpy (buf, "endfoo\nend a\nend fooish\nend foo\ntest\n");
2322
ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
2327
TEST_EQ (endpos, 24);
2330
/* Check that reaching the end of the file without finding the block
2331
* terminator causes an error to be raised and NULL to be returned.
2333
TEST_FEATURE ("with no terminator before end of file");
2334
strcpy (buf, "this is\na test\n");
2338
ret = nih_config_skip_block (buf, strlen (buf), &pos, &lineno,
2343
TEST_EQ (lineno, 4);
2345
err = nih_error_get ();
2346
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_BLOCK);
2351
static int handler_called = 0;
2352
static void *last_data = NULL;
2353
static NihConfigStanza *last_stanza = NULL;
2354
static const char *last_file = NULL;
2355
static size_t last_len = 0;
2356
static size_t last_pos = 0;
2357
static size_t last_lineno = 0;
2360
my_handler (void *data,
2361
NihConfigStanza *stanza,
2370
last_stanza = stanza;
2375
last_lineno = *lineno;
2380
if (strcmp (stanza->name, "foo"))
2381
nih_config_next_line (file, len, pos, lineno);
2386
static NihConfigStanza stanzas[] = {
2387
{ "foo", my_handler },
2388
{ "bar", my_handler },
2390
{ "frodo", my_handler },
2391
{ "bilbo", my_handler },
2396
static NihConfigStanza any_stanzas[] = {
2403
test_parse_stanza (void)
2410
TEST_FUNCTION ("nih_config_stanza");
2411
program_name = "test";
2414
/* Check that the handler is called with all of the right arguments
2415
* if the stanza is found at the start of the string. The pos should
2416
* only be incremented up to the point after the first argument,
2417
* leaving it up to the stanza handler to increment it.
2419
TEST_FEATURE ("with stanza at start of string");
2420
strcpy (buf, "foo this is a test\nwibble\n");
2429
ret = nih_config_parse_stanza (buf, strlen (buf), NULL, NULL,
2432
TEST_TRUE (handler_called);
2433
TEST_EQ_P (last_data, &ret);
2434
TEST_EQ_P (last_file, buf);
2435
TEST_EQ (last_len, strlen (buf));
2436
TEST_EQ (last_pos, 4);
2437
TEST_EQ (last_lineno, (size_t)-1);
2442
/* Check that the handler can be called with a position inside the
2445
TEST_FEATURE ("with stanza inside string");
2446
strcpy (buf, "snarf foo this is a test\nwibble\n");
2456
ret = nih_config_parse_stanza (buf, strlen (buf), &pos, NULL,
2459
TEST_TRUE (handler_called);
2460
TEST_EQ_P (last_data, &ret);
2461
TEST_EQ_P (last_file, buf);
2462
TEST_EQ (last_len, strlen (buf));
2463
TEST_EQ (last_pos, 10);
2464
TEST_EQ (last_lineno, (size_t)-1);
2470
/* Check that the position can be updated by the handler function
2471
* to point wherever it thinks the stanza ends.
2473
TEST_FEATURE ("with position moved by stanza");
2474
strcpy (buf, "bar this is a test\nwibble\n");
2485
ret = nih_config_parse_stanza (buf, strlen (buf), &pos, &lineno,
2488
TEST_TRUE (handler_called);
2489
TEST_EQ_P (last_data, &ret);
2490
TEST_EQ_P (last_file, buf);
2491
TEST_EQ (last_len, strlen (buf));
2492
TEST_EQ (last_pos, 4);
2493
TEST_EQ (last_lineno, 1);
2497
TEST_EQ (lineno, 2);
2500
/* Check that finding an unknown stanza results in an error being
2501
* raised, and no handler called.
2503
TEST_FEATURE ("with unknown stanza");
2504
strcpy (buf, "wibble this is a test\nwibble\n");
2510
ret = nih_config_parse_stanza (buf, strlen (buf), &pos, &lineno,
2513
TEST_FALSE (handler_called);
2516
TEST_EQ (lineno, 1);
2518
err = nih_error_get ();
2519
TEST_EQ (err->number, NIH_CONFIG_UNKNOWN_STANZA);
2523
/* Check that unknown stanzas can be handled by an entry in the
2524
* table with a zero-length name.
2526
TEST_FEATURE ("with unknown stanza and catch-all");
2536
ret = nih_config_parse_stanza (buf, strlen (buf), NULL, NULL,
2539
TEST_TRUE (handler_called);
2540
TEST_EQ_P (last_data, &ret);
2541
TEST_EQ_P (last_file, buf);
2542
TEST_EQ (last_len, strlen (buf));
2543
TEST_EQ (last_pos, 7);
2544
TEST_EQ (last_lineno, (size_t)-1);
2549
/* Check that an error is raised if there is no stanza at this
2550
* position in the file.
2552
TEST_FEATURE ("with empty line");
2553
strcpy (buf, "\nfoo this is a test\n");
2559
ret = nih_config_parse_stanza (buf, strlen (buf), &pos, &lineno,
2562
TEST_FALSE (handler_called);
2565
TEST_EQ (lineno, 1);
2567
err = nih_error_get ();
2568
TEST_EQ (err->number, NIH_CONFIG_EXPECTED_TOKEN);
2574
test_parse_file (void)
2581
TEST_FUNCTION ("nih_config_parse_file");
2584
/* Check that a simple sequence of stanzas is parsed, with the
2585
* handler being called for each. When finished, the position
2586
* should be past the end of the file.
2588
TEST_FEATURE ("with simple lines");
2589
strcpy (buf, "frodo test\nbilbo test\n");
2600
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2606
TEST_EQ (handler_called, 2);
2607
TEST_EQ_P (last_data, &buf);
2608
TEST_EQ_P (last_file, buf);
2609
TEST_EQ (last_len, strlen (buf));
2610
TEST_EQ (last_pos, 17);
2611
TEST_EQ (last_lineno, 2);
2614
/* Check that a line ending in a comment can be parsed, with the
2617
TEST_FEATURE ("with comment at end of line");
2618
strcpy (buf, "frodo test # foo comment\nbilbo test\n");
2629
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2635
TEST_EQ (handler_called, 2);
2636
TEST_EQ_P (last_data, &buf);
2637
TEST_EQ_P (last_file, buf);
2638
TEST_EQ (last_len, strlen (buf));
2639
TEST_EQ (last_pos, 31);
2640
TEST_EQ (last_lineno, 2);
2643
/* Check that whitespace at the start of a line is skipped. */
2644
TEST_FEATURE ("with whitespace at start of line");
2645
strcpy (buf, " frodo test\n \t \t bilbo test\n");
2656
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2662
TEST_EQ (handler_called, 2);
2663
TEST_EQ_P (last_data, &buf);
2664
TEST_EQ_P (last_file, buf);
2665
TEST_EQ (last_len, strlen (buf));
2666
TEST_EQ (last_pos, 27);
2667
TEST_EQ (last_lineno, 2);
2670
/* Check that an empty line is skipped over properly. */
2671
TEST_FEATURE ("with empty line");
2672
strcpy (buf, "\nfrodo test\nbilbo test\n");
2683
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2689
TEST_EQ (handler_called, 2);
2690
TEST_EQ_P (last_data, &buf);
2691
TEST_EQ_P (last_file, buf);
2692
TEST_EQ (last_len, strlen (buf));
2693
TEST_EQ (last_pos, 18);
2694
TEST_EQ (last_lineno, 3);
2697
/* Check that a line containing whitespace is skipped over. */
2698
TEST_FEATURE ("with line containing only whitespace");
2699
strcpy (buf, " \t \nfrodo test\nbilbo test\n");
2710
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2716
TEST_EQ (handler_called, 2);
2717
TEST_EQ_P (last_data, &buf);
2718
TEST_EQ_P (last_file, buf);
2719
TEST_EQ (last_len, strlen (buf));
2720
TEST_EQ (last_pos, 23);
2721
TEST_EQ (last_lineno, 3);
2724
/* Check that a line containing a comment is skipped over. */
2725
TEST_FEATURE ("with line containing only a comment");
2726
strcpy (buf, "# hello\nfrodo test\nbilbo test\n");
2737
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2743
TEST_EQ (handler_called, 2);
2744
TEST_EQ_P (last_data, &buf);
2745
TEST_EQ_P (last_file, buf);
2746
TEST_EQ (last_len, strlen (buf));
2747
TEST_EQ (last_pos, 25);
2748
TEST_EQ (last_lineno, 3);
2751
/* Check that a line containing a comment after some whitespace
2754
TEST_FEATURE ("with line containing a comment and whitespace");
2755
strcpy (buf, " \t # hello\nfrodo test\nbilbo test\n");
2766
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2772
TEST_EQ (handler_called, 2);
2773
TEST_EQ_P (last_data, &buf);
2774
TEST_EQ_P (last_file, buf);
2775
TEST_EQ (last_len, strlen (buf));
2776
TEST_EQ (last_pos, 30);
2777
TEST_EQ (last_lineno, 3);
2780
/* Check that a parser error is raised with the position and line
2781
* number set to where it was found. Only handlers up to that point
2784
TEST_FEATURE ("with parser error");
2785
strcpy (buf, "frodo test\n\"bilbo test\n");
2796
ret = nih_config_parse_file (buf, strlen (buf), &pos, &lineno,
2801
TEST_EQ (lineno, 3);
2803
TEST_EQ (handler_called, 1);
2804
TEST_EQ_P (last_data, &buf);
2805
TEST_EQ_P (last_file, buf);
2806
TEST_EQ (last_len, strlen (buf));
2807
TEST_EQ (last_pos, 6);
2808
TEST_EQ (last_lineno, 1);
2810
err = nih_error_get ();
2811
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
2819
char filename[PATH_MAX];
2824
TEST_FUNCTION ("nih_config_parse");
2826
/* Check that a file that exists is parsed, with the handlers
2827
* called and zero returned.
2829
TEST_FEATURE ("with existing file");
2830
TEST_FILENAME (filename);
2832
fd = fopen (filename, "w");
2833
fprintf (fd, "frodo test\n");
2834
fprintf (fd, "bilbo test\n");
2846
ret = nih_config_parse (filename, NULL, &lineno, stanzas, &ret);
2850
TEST_EQ (handler_called, 2);
2851
TEST_EQ_P (last_data, &ret);
2852
TEST_NE_P (last_file, NULL);
2853
TEST_EQ (last_len, 22);
2854
TEST_EQ (last_pos, 17);
2855
TEST_EQ (last_lineno, 2);
2860
/* Check that an error is raised if the file doesn't exist. */
2861
TEST_FEATURE ("with non-existant file");
2864
ret = nih_config_parse (filename, NULL, NULL, stanzas, &ret);
2867
TEST_FALSE (handler_called);
2869
err = nih_error_get ();
2870
TEST_EQ (err->number, ENOENT);
2874
/* Check that a parser error is raised with the position and line
2875
* number set to where it was found.
2877
TEST_FEATURE ("with parser error");
2878
fd = fopen (filename, "w");
2879
fprintf (fd, "# first line comment\n");
2881
fprintf (fd, "frodo test\n");
2882
fprintf (fd, "\"bilbo test\n");
2883
fprintf (fd, "wibble\n");
2896
ret = nih_config_parse (filename, &pos, &lineno, stanzas, &ret);
2900
TEST_EQ (handler_called, 1);
2901
TEST_EQ_P (last_data, &ret);
2902
TEST_NE_P (last_file, NULL);
2903
TEST_EQ (last_len, 52);
2904
TEST_EQ (last_pos, 28);
2905
TEST_EQ (last_lineno, 3);
2908
TEST_EQ (lineno, 6);
2910
err = nih_error_get ();
2911
TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_QUOTE);
2927
test_skip_whitespace ();
2928
test_skip_comment ();
2930
test_parse_command ();
2931
test_parse_block ();
2933
test_parse_stanza ();