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