test-json-parser.c revision 10972f2a15f5538860fcc1d4adda227d59d2d757
/* Copyright (c) 2013-2016 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "str.h"
#include "istream-private.h"
#include "json-parser.h"
#define TYPE_SKIP 100
#define TYPE_STREAM 101
static const char json_input[] =
"{\n"
"\t\"key\"\t:\t\t\"string\","
" \"key2\" : 1234, \n"
"\"key3\":true,"
"\"key4\":false,"
"\"skip1\": \"jsifjaisfjiasji\","
"\"skip2\": { \"x\":{ \"y\":123}, \"z\":[5,[6],{\"k\":0},3]},"
"\"key5\":null,"
"\"key6\": {},"
"\"key7\": {"
" \"sub1\":\"value\""
"},"
"\"key8\": {"
" \"sub2\":-12.456,\n"
" \"sub3\":12.456e9,\n"
" \"sub4\":0.456e-789"
"},"
"\"key9\": \"foo\\\\\\\"\\b\\f\\n\\r\\t\\u0001\\uffff\","
"\"key10\": \"foo\\\\\\\"\\b\\f\\n\\r\\t\\u0001\\uffff\","
"\"key11\": [],"
"\"key12\": [ \"foo\" , 5.24,[true],{\"aobj\":[]}]"
"}\n";
static struct {
const char *value;
} json_output[] = {
{ JSON_TYPE_OBJECT_KEY, "key" },
{ JSON_TYPE_STRING, "string" },
{ JSON_TYPE_OBJECT_KEY, "key2" },
{ JSON_TYPE_NUMBER, "1234" },
{ JSON_TYPE_OBJECT_KEY, "key3" },
{ JSON_TYPE_TRUE, "true" },
{ JSON_TYPE_OBJECT_KEY, "key4" },
{ JSON_TYPE_FALSE, "false" },
{ JSON_TYPE_OBJECT_KEY, "skip1" },
{ JSON_TYPE_OBJECT_KEY, "skip2" },
{ JSON_TYPE_OBJECT_KEY, "key5" },
{ JSON_TYPE_NULL, NULL },
{ JSON_TYPE_OBJECT_KEY, "key6" },
{ JSON_TYPE_OBJECT, NULL },
{ JSON_TYPE_OBJECT_END, NULL },
{ JSON_TYPE_OBJECT_KEY, "key7" },
{ JSON_TYPE_OBJECT, NULL },
{ JSON_TYPE_OBJECT_KEY, "sub1" },
{ JSON_TYPE_STRING, "value" },
{ JSON_TYPE_OBJECT_END, NULL },
{ JSON_TYPE_OBJECT_KEY, "key8" },
{ JSON_TYPE_OBJECT, NULL },
{ JSON_TYPE_OBJECT_KEY, "sub2" },
{ JSON_TYPE_NUMBER, "-12.456" },
{ JSON_TYPE_OBJECT_KEY, "sub3" },
{ JSON_TYPE_NUMBER, "12.456e9" },
{ JSON_TYPE_OBJECT_KEY, "sub4" },
{ JSON_TYPE_NUMBER, "0.456e-789" },
{ JSON_TYPE_OBJECT_END, NULL },
{ JSON_TYPE_OBJECT_KEY, "key9" },
{ JSON_TYPE_STRING, "foo\\\"\b\f\n\r\t\001\xef\xbf\xbf" },
{ JSON_TYPE_OBJECT_KEY, "key10" },
{ TYPE_STREAM, "foo\\\"\b\f\n\r\t\001\xef\xbf\xbf" },
{ JSON_TYPE_OBJECT_KEY, "key11" },
{ JSON_TYPE_ARRAY, NULL },
{ JSON_TYPE_ARRAY_END, NULL },
{ JSON_TYPE_OBJECT_KEY, "key12" },
{ JSON_TYPE_ARRAY, NULL },
{ JSON_TYPE_STRING, "foo" },
{ JSON_TYPE_NUMBER, "5.24" },
{ JSON_TYPE_ARRAY, NULL },
{ JSON_TYPE_TRUE, "true" },
{ JSON_TYPE_ARRAY_END, NULL },
{ JSON_TYPE_OBJECT, NULL },
{ JSON_TYPE_OBJECT_KEY, "aobj" },
{ JSON_TYPE_ARRAY, NULL },
{ JSON_TYPE_ARRAY_END, NULL },
{ JSON_TYPE_OBJECT_END, NULL },
{ JSON_TYPE_ARRAY_END, NULL }
};
static int
{
const unsigned char *data;
if (ret == 0)
return 0;
if ((*input)->stream_errno != 0)
return -1;
return 1;
}
static void test_json_parser_success(bool full_size)
{
struct json_parser *parser;
int ret = 0;
i = full_size ? json_input_len : 0;
for (pos = 0; i <= json_input_len; i++) {
for (;;) {
pos++;
continue;
} else {
type = TYPE_STREAM;
}
if (ret <= 0)
break;
pos++;
}
test_assert(ret == 0);
}
test_end();
}
static void test_json_append_escaped(void)
{
test_begin("json_append_escaped()");
test_end();
}
void test_json_parser(void)
{
}