]> andersk Git - libyaml.git/blob - tests/test-reader.c
Prepare the initial release.
[libyaml.git] / tests / test-reader.c
1 #include <yaml.h>
2
3 YAML_DECLARE(int)
4 yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <assert.h>
9
10 /*
11  * Test cases are stolen from
12  * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
13  */
14
15 typedef struct {
16     char *title;
17     char *test;
18     int result;
19 } test_case;
20
21 test_case utf8_sequences[] = {
22     /* {"title", "test 1|test 2|...|test N!", (0 or 1)}, */
23
24     {"a simple test", "'test' is '\xd0\xbf\xd1\x80\xd0\xbe\xd0\xb2\xd0\xb5\xd1\x80\xd0\xba\xd0\xb0' in Russian!", 1},
25     {"an empty line", "!", 1},
26
27     {"u-0 is a control character", "\x00!", 0},
28     {"u-80 is a control character", "\xc2\x80!", 0},
29     {"u-800 is valid", "\xe0\xa0\x80!", 1},
30     {"u-10000 is valid", "\xf0\x90\x80\x80!", 1},
31     {"5 bytes sequences are not allowed", "\xf8\x88\x80\x80\x80!", 0},
32     {"6 bytes sequences are not allowed", "\xfc\x84\x80\x80\x80\x80!", 0},
33
34     {"u-7f is a control character", "\x7f!", 0},
35     {"u-7FF is valid", "\xdf\xbf!", 1},
36     {"u-FFFF is a control character", "\xef\xbf\xbf!", 0},
37     {"u-1FFFFF is too large", "\xf7\xbf\xbf\xbf!", 0},
38     {"u-3FFFFFF is 5 bytes", "\xfb\xbf\xbf\xbf\xbf!", 0},
39     {"u-7FFFFFFF is 6 bytes", "\xfd\xbf\xbf\xbf\xbf\xbf!", 0},
40
41     {"u-D7FF", "\xed\x9f\xbf!", 1},
42     {"u-E000", "\xee\x80\x80!", 1},
43     {"u-FFFD", "\xef\xbf\xbd!", 1},
44     {"u-10FFFF", "\xf4\x8f\xbf\xbf!", 1},
45     {"u-110000", "\xf4\x90\x80\x80!", 0},
46
47     {"first continuation byte", "\x80!", 0},
48     {"last continuation byte", "\xbf!", 0},
49
50     {"2 continuation bytes", "\x80\xbf!", 0},
51     {"3 continuation bytes", "\x80\xbf\x80!", 0},
52     {"4 continuation bytes", "\x80\xbf\x80\xbf!", 0},
53     {"5 continuation bytes", "\x80\xbf\x80\xbf\x80!", 0},
54     {"6 continuation bytes", "\x80\xbf\x80\xbf\x80\xbf!", 0},
55     {"7 continuation bytes", "\x80\xbf\x80\xbf\x80\xbf\x80!", 0},
56
57     {"sequence of all 64 possible continuation bytes",
58      "\x80|\x81|\x82|\x83|\x84|\x85|\x86|\x87|\x88|\x89|\x8a|\x8b|\x8c|\x8d|\x8e|\x8f|"
59      "\x90|\x91|\x92|\x93|\x94|\x95|\x96|\x97|\x98|\x99|\x9a|\x9b|\x9c|\x9d|\x9e|\x9f|"
60      "\xa0|\xa1|\xa2|\xa3|\xa4|\xa5|\xa6|\xa7|\xa8|\xa9|\xaa|\xab|\xac|\xad|\xae|\xaf|"
61      "\xb0|\xb1|\xb2|\xb3|\xb4|\xb5|\xb6|\xb7|\xb8|\xb9|\xba|\xbb|\xbc|\xbd|\xbe|\xbf!", 0},
62     {"32 first bytes of 2-byte sequences {0xc0-0xdf}",
63      "\xc0 |\xc1 |\xc2 |\xc3 |\xc4 |\xc5 |\xc6 |\xc7 |\xc8 |\xc9 |\xca |\xcb |\xcc |\xcd |\xce |\xcf |"
64      "\xd0 |\xd1 |\xd2 |\xd3 |\xd4 |\xd5 |\xd6 |\xd7 |\xd8 |\xd9 |\xda |\xdb |\xdc |\xdd |\xde |\xdf !", 0},
65     {"16 first bytes of 3-byte sequences {0xe0-0xef}",
66      "\xe0 |\xe1 |\xe2 |\xe3 |\xe4 |\xe5 |\xe6 |\xe7 |\xe8 |\xe9 |\xea |\xeb |\xec |\xed |\xee |\xef !", 0},
67     {"8 first bytes of 4-byte sequences {0xf0-0xf7}", "\xf0 |\xf1 |\xf2 |\xf3 |\xf4 |\xf5 |\xf6 |\xf7 !", 0},
68     {"4 first bytes of 5-byte sequences {0xf8-0xfb}", "\xf8 |\xf9 |\xfa |\xfb !", 0},
69     {"2 first bytes of 6-byte sequences {0xfc-0xfd}", "\xfc |\xfd !", 0},
70
71     {"sequences with last byte missing {u-0}",
72      "\xc0|\xe0\x80|\xf0\x80\x80|\xf8\x80\x80\x80|\xfc\x80\x80\x80\x80!", 0},
73     {"sequences with last byte missing {u-...FF}",
74      "\xdf|\xef\xbf|\xf7\xbf\xbf|\xfb\xbf\xbf\xbf|\xfd\xbf\xbf\xbf\xbf!", 0},
75
76     {"impossible bytes", "\xfe|\xff|\xfe\xfe\xff\xff!", 0},
77
78     {"overlong sequences {u-2f}",
79      "\xc0\xaf|\xe0\x80\xaf|\xf0\x80\x80\xaf|\xf8\x80\x80\x80\xaf|\xfc\x80\x80\x80\x80\xaf!", 0},
80
81     {"maximum overlong sequences",
82      "\xc1\xbf|\xe0\x9f\xbf|\xf0\x8f\xbf\xbf|\xf8\x87\xbf\xbf\xbf|\xfc\x83\xbf\xbf\xbf\xbf!", 0},
83
84     {"overlong representation of the NUL character",
85      "\xc0\x80|\xe0\x80\x80|\xf0\x80\x80\x80|\xf8\x80\x80\x80\x80|\xfc\x80\x80\x80\x80\x80!", 0},
86
87     {"single UTF-16 surrogates",
88      "\xed\xa0\x80|\xed\xad\xbf|\xed\xae\x80|\xed\xaf\xbf|\xed\xb0\x80|\xed\xbe\x80|\xed\xbf\xbf!", 0},
89
90     {"paired UTF-16 surrogates",
91      "\xed\xa0\x80\xed\xb0\x80|\xed\xa0\x80\xed\xbf\xbf|\xed\xad\xbf\xed\xb0\x80|"
92      "\xed\xad\xbf\xed\xbf\xbf|\xed\xae\x80\xed\xb0\x80|\xed\xae\x80\xed\xbf\xbf|"
93      "\xed\xaf\xbf\xed\xb0\x80|\xed\xaf\xbf\xed\xbf\xbf!", 0},
94
95     {"other illegal code positions", "\xef\xbf\xbe|\xef\xbf\xbf!", 0},
96
97     {NULL, NULL, 0}
98 };
99
100 test_case boms[] = {
101     
102     /* {"title", "test!", lenth}, */
103     
104     {"no bom (utf-8)", "Hi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!", 13},
105     {"bom (utf-8)", "\xef\xbb\xbfHi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!", 13},
106     {"bom (utf-16-le)", "\xff\xfeH\x00i\x00 \x00i\x00s\x00 \x00\x1f\x04@\x04""8\x04""2\x04""5\x04""B\x04!", 13},
107     {"bom (utf-16-be)", "\xfe\xff\x00H\x00i\x00 \x00i\x00s\x00 \x04\x1f\x04@\x04""8\x04""2\x04""5\x04""B!", 13},
108     {NULL, NULL, 0}
109 };
110
111 char *bom_original = "Hi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82";
112
113 int check_utf8_sequences(void)
114 {
115     yaml_parser_t parser;
116     int failed = 0;
117     int k;
118     printf("checking utf-8 sequences...\n");
119     for (k = 0; utf8_sequences[k].test; k++) {
120         char *title = utf8_sequences[k].title;
121         int check = utf8_sequences[k].result;
122         int result;
123         char *start = utf8_sequences[k].test;
124         char *end = start;
125         printf("\t%s:\n", title);
126         while(1) {
127             while (*end != '|' && *end != '!') end++;
128             yaml_parser_initialize(&parser);
129             yaml_parser_set_input_string(&parser, (unsigned char *)start, end-start);
130             result = yaml_parser_update_buffer(&parser, end-start);
131             if (result != check) {
132                 printf("\t\t- ");
133                 failed ++;
134             }
135             else {
136                 printf("\t\t+ ");
137             }
138             if (!parser.error) {
139                 printf("(no error)\n");
140             }
141             else if (parser.error == YAML_READER_ERROR) {
142                 if (parser.problem_value != -1) {
143                     printf("(reader error: %s: #%X at %d)\n",
144                             parser.problem, parser.problem_value, parser.problem_offset);
145                 }
146                 else {
147                     printf("(reader error: %s at %d)\n",
148                             parser.problem, parser.problem_offset);
149                 }
150             }
151             if (*end == '!') break;
152             start = ++end;
153             yaml_parser_delete(&parser);
154         };
155         printf("\n");
156     }
157     printf("checking utf-8 sequences: %d fail(s)\n", failed);
158     return failed;
159 }
160
161 int check_boms(void)
162 {
163     yaml_parser_t parser;
164     int failed = 0;
165     int k;
166     printf("checking boms...\n");
167     for (k = 0; boms[k].test; k++) {
168         char *title = boms[k].title;
169         int check = boms[k].result;
170         int result;
171         char *start = boms[k].test;
172         char *end = start;
173         while (*end != '!') end++;
174         printf("\t%s: ", title);
175         yaml_parser_initialize(&parser);
176         yaml_parser_set_input_string(&parser, (unsigned char *)start, end-start);
177         result = yaml_parser_update_buffer(&parser, end-start);
178         if (!result) {
179             printf("- (reader error: %s at %d)\n", parser.problem, parser.problem_offset);
180             failed++;
181         }
182         else {
183             if (parser.unread != check) {
184                 printf("- (length=%d while expected length=%d)\n", parser.unread, check);
185                 failed++;
186             }
187             else if (memcmp(parser.buffer.start, bom_original, check) != 0) {
188                 printf("- (value '%s' does not equal to the original value '%s')\n", parser.buffer.start, bom_original);
189                 failed++;
190             }
191             else {
192                 printf("+\n");
193             }
194         }
195         yaml_parser_delete(&parser);
196     }
197     printf("checking boms: %d fail(s)\n", failed);
198     return failed;
199 }
200
201 #define LONG    100000
202
203 int check_long_utf8(void)
204 {
205     yaml_parser_t parser;
206     int k = 0;
207     int j;
208     int failed = 0;
209     unsigned char ch0, ch1;
210     unsigned char *buffer = malloc(3+LONG*2);
211     assert(buffer);
212     printf("checking a long utf8 sequence...\n");
213     buffer[k++] = '\xef';
214     buffer[k++] = '\xbb';
215     buffer[k++] = '\xbf';
216     for (j = 0; j < LONG; j ++) {
217         if (j % 2) {
218             buffer[k++] = '\xd0';
219             buffer[k++] = '\x90';
220         }
221         else {
222             buffer[k++] = '\xd0';
223             buffer[k++] = '\xaf';
224         }
225     }
226     yaml_parser_initialize(&parser);
227     yaml_parser_set_input_string(&parser, buffer, 3+LONG*2);
228     for (k = 0; k < LONG; k++) {
229         if (!parser.unread) {
230             if (!yaml_parser_update_buffer(&parser, 1)) {
231                 printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset);
232                 failed = 1;
233                 break;
234             }
235         }
236         if (!parser.unread) {
237             printf("\tnot enough characters at %d\n", k);
238             failed = 1;
239             break;
240         }
241         if (k % 2) {
242             ch0 = '\xd0';
243             ch1 = '\x90';
244         }
245         else {
246             ch0 = '\xd0';
247             ch1 = '\xaf';
248         }
249         if (parser.buffer.pointer[0] != ch0 || parser.buffer.pointer[1] != ch1) {
250             printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n",
251                     (int)parser.buffer.pointer[0], (int)parser.buffer.pointer[1],
252                     (int)ch0, (int)ch1);
253             failed = 1;
254             break;
255         }
256         parser.buffer.pointer += 2;
257         parser.unread -= 1;
258     }
259     if (!failed) {
260         if (!yaml_parser_update_buffer(&parser, 1)) {
261             printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset);
262             failed = 1;
263         }
264         else if (parser.buffer.pointer[0] != '\0') {
265             printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread);
266             failed = 1;
267         }
268     }
269     yaml_parser_delete(&parser);
270     free(buffer);
271     printf("checking a long utf8 sequence: %d fail(s)\n", failed);
272     return failed;
273 }
274
275 int check_long_utf16(void)
276 {
277     yaml_parser_t parser;
278     int k = 0;
279     int j;
280     int failed = 0;
281     unsigned char ch0, ch1;
282     unsigned char *buffer = malloc(2+LONG*2);
283     assert(buffer);
284     printf("checking a long utf16 sequence...\n");
285     buffer[k++] = '\xff';
286     buffer[k++] = '\xfe';
287     for (j = 0; j < LONG; j ++) {
288         if (j % 2) {
289             buffer[k++] = '\x10';
290             buffer[k++] = '\x04';
291         }
292         else {
293             buffer[k++] = '/';
294             buffer[k++] = '\x04';
295         }
296     }
297     yaml_parser_initialize(&parser);
298     yaml_parser_set_input_string(&parser, buffer, 2+LONG*2);
299     for (k = 0; k < LONG; k++) {
300         if (!parser.unread) {
301             if (!yaml_parser_update_buffer(&parser, 1)) {
302                 printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset);
303                 failed = 1;
304                 break;
305             }
306         }
307         if (!parser.unread) {
308             printf("\tnot enough characters at %d\n", k);
309             failed = 1;
310             break;
311         }
312         if (k % 2) {
313             ch0 = '\xd0';
314             ch1 = '\x90';
315         }
316         else {
317             ch0 = '\xd0';
318             ch1 = '\xaf';
319         }
320         if (parser.buffer.pointer[0] != ch0 || parser.buffer.pointer[1] != ch1) {
321             printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n",
322                     (int)parser.buffer.pointer[0], (int)parser.buffer.pointer[1],
323                     (int)ch0, (int)ch1);
324             failed = 1;
325             break;
326         }
327         parser.buffer.pointer += 2;
328         parser.unread -= 1;
329     }
330     if (!failed) {
331         if (!yaml_parser_update_buffer(&parser, 1)) {
332             printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset);
333             failed = 1;
334         }
335         else if (parser.buffer.pointer[0] != '\0') {
336             printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread);
337             failed = 1;
338         }
339     }
340     yaml_parser_delete(&parser);
341     free(buffer);
342     printf("checking a long utf16 sequence: %d fail(s)\n", failed);
343     return failed;
344 }
345
346 int
347 main(void)
348 {
349     return check_utf8_sequences() + check_boms() + check_long_utf8() + check_long_utf16();
350 }
This page took 0.056115 seconds and 5 git commands to generate.