2020-07-12 13:26:30 +00:00
|
|
|
#include "webfuse_provider/impl/json/doc.h"
|
2020-07-11 19:05:00 +00:00
|
|
|
#include "webfuse_provider/impl/json/node.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
wfp_json_doc * parse_json(char * text)
|
|
|
|
{
|
2020-07-12 13:26:30 +00:00
|
|
|
return wfp_impl_json_doc_loadb(text, strlen(text));
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 14:03:38 +00:00
|
|
|
TEST(json_parser, parse_null)
|
|
|
|
{
|
|
|
|
char text[] = "null";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_null(root));
|
|
|
|
|
|
|
|
wfp_impl_json_doc_dispose(doc);
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:05:00 +00:00
|
|
|
TEST(json_parser, parse_true)
|
|
|
|
{
|
|
|
|
char text[] = "true";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_bool(root));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_bool_get(root));
|
2020-07-11 19:05:00 +00:00
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_false)
|
|
|
|
{
|
|
|
|
char text[] = "false";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_bool(root));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_FALSE(wfp_impl_json_bool_get(root));
|
2020-07-11 19:05:00 +00:00
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_int)
|
|
|
|
{
|
|
|
|
char text[] = "42";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_int(root));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_EQ(42, wfp_impl_json_int_get(root));
|
2020-07-11 19:05:00 +00:00
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_negative_int)
|
|
|
|
{
|
|
|
|
char text[] = "-1234";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_int(root));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_EQ(-1234, wfp_impl_json_int_get(root));
|
2020-07-11 19:05:00 +00:00
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_string)
|
|
|
|
{
|
|
|
|
char text[] = "\"brummni\"";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(root));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_STREQ("brummni", wfp_impl_json_string_get(root));
|
2020-07-11 19:05:00 +00:00
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_array)
|
|
|
|
{
|
|
|
|
char text[] = "[true,1,\"foo\",[42]]";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_array(root));
|
|
|
|
ASSERT_EQ(4, wfp_impl_json_array_size(root));
|
|
|
|
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_bool(wfp_impl_json_array_get(root, 0)));
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_int(wfp_impl_json_array_get(root, 1)));
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(wfp_impl_json_array_get(root, 2)));
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_array(wfp_impl_json_array_get(root, 3)));
|
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_object)
|
|
|
|
{
|
|
|
|
char text[] = "{\"method\":\"add\",\"params\":[1,2],\"id\":42}";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_NE(nullptr, doc);
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_json const * root = wfp_impl_json_doc_root(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_object(root));
|
|
|
|
ASSERT_EQ(3, wfp_impl_json_object_size(root));
|
|
|
|
|
|
|
|
ASSERT_STREQ("method", wfp_impl_json_object_key(root, 0));
|
|
|
|
ASSERT_STREQ("params", wfp_impl_json_object_key(root, 1));
|
|
|
|
ASSERT_STREQ("id", wfp_impl_json_object_key(root, 2));
|
|
|
|
|
2020-07-12 13:26:30 +00:00
|
|
|
wfp_impl_json_doc_dispose(doc);
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(json_parser, parse_fail_invalid_json)
|
|
|
|
{
|
|
|
|
{
|
2020-07-12 14:03:38 +00:00
|
|
|
char text[] = "invalid";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "nul";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "tru";
|
2020-07-11 19:05:00 +00:00
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "flas";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "[1,2,3}";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
2020-07-12 14:03:38 +00:00
|
|
|
{
|
|
|
|
char text[] = "[1 2 3]";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "[1,2,3";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "[1,2,]";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:05:00 +00:00
|
|
|
{
|
|
|
|
char text[] = "{\"method\":\"add\",\"params\":[1,2],\"id\":42";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
2020-07-11 22:50:00 +00:00
|
|
|
{
|
|
|
|
char text[] = "[\"method\",[], {}, \"params\":,42]";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
2020-07-12 14:03:38 +00:00
|
|
|
{
|
|
|
|
char text[] = "{\"key\" \"value\"}";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "{\"key\": }";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "{\"key\": \"value\"";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "{\"key\" \"value\"]";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char text[] = "{\"key\": \"value\", }";
|
|
|
|
wfp_json_doc * doc = parse_json(text);
|
|
|
|
ASSERT_EQ(nullptr, doc);
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:05:00 +00:00
|
|
|
}
|