diff options
author | minfrin <minfrin@13f79535-47bb-0310-9956-ffa450edef68> | 2018-07-08 11:26:00 +0000 |
---|---|---|
committer | minfrin <minfrin@13f79535-47bb-0310-9956-ffa450edef68> | 2018-07-08 11:26:00 +0000 |
commit | 7822f42caaa6b65d5742b48b153f1e82543b8fe5 (patch) | |
tree | 8484d07ccaed8f5e7b5a0cd4e0af015e7163e3fe /test | |
parent | 9d2258f6c89d82228e5146b7ab2846c357fbf116 (diff) | |
download | libapr-7822f42caaa6b65d5742b48b153f1e82543b8fe5.tar.gz |
apr_json: Add support for encoding and decoding RFC8259 JSON.
Submitted by: Moriyoshi Koizumi <mozo mozo jp>
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@1835348 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.in | 2 | ||||
-rw-r--r-- | test/abts_tests.h | 3 | ||||
-rw-r--r-- | test/testjson.c | 139 | ||||
-rw-r--r-- | test/testutil.h | 1 |
4 files changed, 143 insertions, 2 deletions
diff --git a/test/Makefile.in b/test/Makefile.in index be2d65c4e..291a76e71 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -37,7 +37,7 @@ TESTS = testtime.lo teststr.lo testvsn.lo testipsub.lo testshm.lo \ testbuckets.lo testxml.lo testdbm.lo testuuid.lo testmd5.lo \ testreslist.lo testbase64.lo testhooks.lo testlfsabi.lo \ testlfsabi32.lo testlfsabi64.lo testescape.lo testskiplist.lo \ - testsiphash.lo testredis.lo testencode.lo + testsiphash.lo testredis.lo testencode.lo testjson.lo OTHER_PROGRAMS = \ echod@EXEEXT@ \ diff --git a/test/abts_tests.h b/test/abts_tests.h index 312098668..82cce0ce2 100644 --- a/test/abts_tests.h +++ b/test/abts_tests.h @@ -91,7 +91,8 @@ const struct testlist { {testreslist}, {testlfsabi}, {testskiplist}, - {testsiphash} + {testsiphash}, + {testjson} }; #endif /* APR_TEST_INCLUDES */ diff --git a/test/testjson.c b/test/testjson.c new file mode 100644 index 000000000..338149d4f --- /dev/null +++ b/test/testjson.c @@ -0,0 +1,139 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include "apr_json.h" + +#include "abts.h" +#include "testutil.h" + +static void test_json_identity(abts_case * tc, void *data) +{ + apr_json_value_t *json = NULL; + apr_json_kv_t *image, *width, *ids, *title, + *animated, *thumbnail, *height; + apr_bucket_alloc_t *ba; + apr_bucket_brigade *bb; + const char *src; + char buf[1024]; + apr_size_t len = sizeof(buf); + apr_off_t offset = 0; + + ba = apr_bucket_alloc_create(p); + bb = apr_brigade_create(p, ba); + + src = "{" + " \"Image\" : {" + " \"Width\" : 800 ," + " \"IDs\" : [116, 943, 234, 38793]," + " \"Title\" : \"View from 15th Floor\"," + " \"Animated\" : false," + " \"Thumbnail\" : {" + " \"Height\" : 125," + " \"Width\" : 100," + " \"Url\" : \"http://www.example.com/image/481989943\"" + " }," + " \"Height\" : 600 " + " }" + "}"; + + apr_json_decode(&json, src, APR_JSON_VALUE_STRING, &offset, APR_JSON_FLAGS_WHITESPACE, + 10, p); + apr_json_encode(bb, NULL, NULL, json, APR_JSON_FLAGS_WHITESPACE, p); + apr_brigade_flatten(bb, buf, &len); + apr_json_decode(&json, buf, len, &offset, APR_JSON_FLAGS_WHITESPACE, 10, p); + + ABTS_STR_NEQUAL(tc, src, buf, len); + + ABTS_INT_EQUAL(tc, len, offset); + ABTS_INT_EQUAL(tc, APR_JSON_OBJECT, json->type); + image = apr_hash_get(json->value.object->hash, "Image", 5); + ABTS_PTR_NOTNULL(tc, image); + width = apr_hash_get(image->v->value.object->hash, "Width", 5); + ABTS_PTR_NOTNULL(tc, width); + ABTS_INT_EQUAL(tc, APR_JSON_LONG, width->v->type); + ABTS_INT_EQUAL(tc, 800, width->v->value.lnumber); + ids = apr_hash_get(image->v->value.object->hash, "IDs", 3); + ABTS_PTR_NOTNULL(tc, ids); + ABTS_INT_EQUAL(tc, APR_JSON_ARRAY, ids->v->type); + title = apr_hash_get(image->v->value.object->hash, "Title", 5); + ABTS_PTR_NOTNULL(tc, title); + ABTS_INT_EQUAL(tc, APR_JSON_STRING, title->v->type); + animated = apr_hash_get(image->v->value.object->hash, "Animated", 8); + ABTS_PTR_NOTNULL(tc, animated); + ABTS_INT_EQUAL(tc, APR_JSON_BOOLEAN, animated->v->type); + ABTS_TRUE(tc, !animated->v->value.boolean); + thumbnail = apr_hash_get(image->v->value.object->hash, "Thumbnail", 9); + ABTS_PTR_NOTNULL(tc, thumbnail); + ABTS_INT_EQUAL(tc, APR_JSON_OBJECT, thumbnail->v->type); + height = apr_hash_get(image->v->value.object->hash, "Height", 6); + ABTS_PTR_NOTNULL(tc, height); + ABTS_INT_EQUAL(tc, APR_JSON_LONG, height->v->type); + ABTS_INT_EQUAL(tc, 600, height->v->value.lnumber); + +} + +static void test_json_level(abts_case * tc, void *data) +{ + apr_json_value_t *json = NULL; + apr_status_t status; + const char *src; + apr_off_t offset = 0; + + src = "{" + "\"One\":{" + "\"Two\":{" + "\"Three\":{"; + + status = apr_json_decode(&json, src, APR_JSON_VALUE_STRING, &offset, + APR_JSON_FLAGS_WHITESPACE, 2, p); + + ABTS_INT_EQUAL(tc, APR_EINVAL, status); + +} + +static void test_json_eof(abts_case * tc, void *data) +{ + apr_json_value_t *json = NULL; + apr_status_t status; + const char *src; + apr_off_t offset = 0; + + src = "{" + "\"One\":{" + "\"Two\":{" + "\"Three\":{"; + + status = apr_json_decode(&json, src, APR_JSON_VALUE_STRING, &offset, + APR_JSON_FLAGS_WHITESPACE, 10, p); + + ABTS_INT_EQUAL(tc, APR_EOF, status); + +} + +abts_suite *testjson(abts_suite * suite) +{ + suite = ADD_SUITE(suite); + + abts_run_test(suite, test_json_identity, NULL); + abts_run_test(suite, test_json_level, NULL); + abts_run_test(suite, test_json_eof, NULL); + + return suite; +} diff --git a/test/testutil.h b/test/testutil.h index 3a3ad2416..261dfd250 100644 --- a/test/testutil.h +++ b/test/testutil.h @@ -134,5 +134,6 @@ abts_suite *testdbm(abts_suite *suite); abts_suite *testlfsabi(abts_suite *suite); abts_suite *testskiplist(abts_suite *suite); abts_suite *testsiphash(abts_suite *suite); +abts_suite *testjson(abts_suite *suite); #endif /* APR_TEST_INCLUDES */ |