bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\t\"key\"\t:\t\t\"string\","
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen " \"key2\" : 1234, \n"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\"key3\":true,"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\"key4\":false,"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen "\"skip1\": \"jsifjaisfjiasji\","
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen "\"skip2\": { \"x\":{ \"y\":123}, \"z\":[5,[6],{\"k\":0},3]},"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\"key5\":null,"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\"key6\": {},"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\"key7\": {"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen " \"sub1\":\"value\""
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "\"key8\": {"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen " \"sub2\":-12.456,\n"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen " \"sub3\":12.456e9,\n"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen " \"sub4\":0.456e-789"
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "\"key9\": \"foo\\\\\\\"\\b\\f\\n\\r\\t\\u0001\\u10ff\","
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "\"key10\": \"foo\\\\\\\"\\b\\f\\n\\r\\t\\u0001\\u10ff\","
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen "\"key11\": [],"
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "\"key12\": [ \"foo\" , 5.24,[true],{\"aobj\":[]}],"
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "\"key13\": \"\\ud801\\udc37\""
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainenstatic const struct {
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi { JSON_TYPE_STRING, "foo\\\"\b\f\n\r\t\001\xe1\x83\xbf" },
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi { TYPE_STREAM, "foo\\\"\b\f\n\r\t\001\xe1\x83\xbf" },
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenstream_read_value(struct istream **input, const char **value_r)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic void test_json_parser_success(bool full_size)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen unsigned int i, pos, json_input_len = strlen(json_input);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen test_begin(full_size ? "json parser" : "json parser (nonblocking)");
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen input = test_istream_create_data(json_input, json_input_len);
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen json_output[pos].type == (enum json_type)TYPE_SKIP) {
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen json_output[pos].type != (enum json_type)TYPE_STREAM) {
95f5b08fa73ddd9a9de40a97aa141e9c74e0645eAki Tuomi test_assert_idx(json_output[pos].type == type, pos);
95f5b08fa73ddd9a9de40a97aa141e9c74e0645eAki Tuomi test_assert_idx(null_strcmp(json_output[pos].value, value) == 0, pos);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen test_assert(json_parse_next(parser, &type, &value) == -1);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen test_assert(json_parser_deinit(&parser, &error) == 0);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen "[ 1, {\"foo\": 1 }, 2, \"bar\", 3, 1.234, 4, [], 5, [[]], 6, true ]";
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen input = test_istream_create_data(test_input, strlen(test_input));
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen parser = json_parser_init_flags(input, JSON_PARSER_NO_ROOT_OBJECT);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen test_assert(json_parse_next(parser, &type, &value) > 0 &&
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen test_assert(json_parse_next(parser, &type, &value) > 0 &&
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen type == JSON_TYPE_NUMBER && atoi(value) == i);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen test_assert(json_parse_next(parser, &type, &value) > 0 &&
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen test_assert(json_parser_deinit(&parser, &error) == 0);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainentest_json_parse_input(const char *test_input, enum json_parser_flags flags)
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen input = test_istream_create_data(test_input, strlen(test_input));
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen parser = json_parser_init_flags(input, flags);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen while (json_parse_next(parser, &type, &value) > 0)
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainenstatic void test_json_parser_primitive_values(void)
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen unsigned int i;
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen test_assert_idx(test_json_parse_input(test_inputs[i].str, JSON_PARSER_NO_ROOT_OBJECT) == test_inputs[i].ret, i);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\":}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\" []}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\": [1}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\": [1,]}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\": [1,]}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\": 1,}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen "{\"foo\": 1.}}",
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "{\"foo\": 1},{}",
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "{\"foo\": \"\\ud808\"}",
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi "{\"foo\": \"\\udfff\"}",
c980536af173efb82839bdd3d5ce2bfddac9fd9bAki Tuomi "{\"foo\": \"\\uyyyy\"}",
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen unsigned int i;
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen test_assert_idx(test_json_parse_input(test_inputs[i], 0) < 0, i);
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi json_append_escaped(str, "\b\f\r\n\t\"\\\001\002-\xC3\xA4\xf0\x90\x90\xb7");
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi test_assert(strcmp(str_c(str), "\\b\\f\\r\\n\\t\\\"\\\\\\u0001\\u0002-\\u00e4\\ud801\\udc37") == 0);
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainenstatic void test_json_append_escaped_data(void)
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi "\b\f\r\n\t\"\\\000\001\002-\xC3\xA4\xf0\x90\x90\xb7";
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen json_append_escaped_data(str, test_input, sizeof(test_input)-1);
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi test_assert(strcmp(str_c(str), "\\b\\f\\r\\n\\t\\\"\\\\\\u0000\\u0001\\u0002-\\u00e4\\ud801\\udc37") == 0);