test-json-parser.c revision 10972f2a15f5538860fcc1d4adda227d59d2d757
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2013-2016 Dovecot authors, see the included COPYING file */
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainenstatic const char json_input[] =
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen "\t\"key\"\t:\t\t\"string\","
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen " \"key2\" : 1234, \n"
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen "\"key3\":true,"
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen "\"key4\":false,"
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen "\"skip1\": \"jsifjaisfjiasji\","
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen "\"skip2\": { \"x\":{ \"y\":123}, \"z\":[5,[6],{\"k\":0},3]},"
6ded8819b9002150a95a7615e4f64f091c250464Timo Sirainen "\"key5\":null,"
6ded8819b9002150a95a7615e4f64f091c250464Timo Sirainen "\"key6\": {},"
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen "\"key7\": {"
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen " \"sub1\":\"value\""
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen "\"key8\": {"
2f8da04d700cc23fcd6630226a4866e828b761bdTimo Sirainen " \"sub2\":-12.456,\n"
2f8da04d700cc23fcd6630226a4866e828b761bdTimo Sirainen " \"sub3\":12.456e9,\n"
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen " \"sub4\":0.456e-789"
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen "\"key9\": \"foo\\\\\\\"\\b\\f\\n\\r\\t\\u0001\\uffff\","
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen "\"key10\": \"foo\\\\\\\"\\b\\f\\n\\r\\t\\u0001\\uffff\","
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen "\"key11\": [],"
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen "\"key12\": [ \"foo\" , 5.24,[true],{\"aobj\":[]}]"
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainenstatic struct {
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen { JSON_TYPE_STRING, "foo\\\"\b\f\n\r\t\001\xef\xbf\xbf" },
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen { TYPE_STREAM, "foo\\\"\b\f\n\r\t\001\xef\xbf\xbf" },
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainenstream_read_value(struct istream **input, const char **value_r)
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen const unsigned char *data;
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainenstatic void test_json_parser_success(bool full_size)
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen unsigned int i, pos, json_input_len = strlen(json_input);
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen test_begin(full_size ? "json parser" : "json parser (nonblocking)");
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen input = test_istream_create_data(json_input, json_input_len);
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen json_output[pos].type == (enum json_type)TYPE_SKIP) {
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen json_output[pos].type != (enum json_type)TYPE_STREAM) {
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen test_assert(null_strcmp(json_output[pos].value, value) == 0);
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen test_assert(json_parse_next(parser, &type, &value) == -1);
8fcf84e6b7a55049a6d407b17af50d6ae8c6386fPhil Carmody test_assert(json_parser_deinit(&parser, &error) == 0);
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen json_append_escaped(str, "\b\f\r\n\t\"\\\001\002-\xC3\xA4");
5550482ce58e51584f83c10a4c63d35b66431742Timo Sirainen test_assert(strcmp(str_c(str), "\\b\\f\\r\\n\\t\\\"\\\\\\u0001\\u0002-\xC3\xA4") == 0);
void test_json_parser(void)