OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <string.h> |
| 35 |
| 36 #include <grpc/support/alloc.h> |
| 37 #include <grpc/support/useful.h> |
| 38 #include <grpc/support/log.h> |
| 39 #include <grpc/support/string_util.h> |
| 40 #include "src/core/json/json.h" |
| 41 #include "src/core/support/string.h" |
| 42 |
| 43 #include "test/core/util/test_config.h" |
| 44 |
| 45 typedef struct testing_pair { |
| 46 const char *input; |
| 47 const char *output; |
| 48 } testing_pair; |
| 49 |
| 50 static testing_pair testing_pairs[] = { |
| 51 /* Testing valid parsing. */ |
| 52 /* Testing trivial parses, with de-indentation. */ |
| 53 {" 0 ", "0"}, |
| 54 {" 1 ", "1"}, |
| 55 {" \" \" ", "\" \""}, |
| 56 {" \"a\" ", "\"a\""}, |
| 57 {" true ", "true"}, |
| 58 /* Testing the parser's ability to decode trivial UTF-16. */ |
| 59 {"\"\\u0020\\\\\\u0010\\u000a\\u000D\"", "\" \\\\\\u0010\\n\\r\""}, |
| 60 /* Testing various UTF-8 sequences. */ |
| 61 {"\"ßâñć௵⇒\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, |
| 62 {"\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", |
| 63 "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, |
| 64 /* Testing UTF-8 character "𝄞", U+11D1E. */ |
| 65 {"\"\xf0\x9d\x84\x9e\"", "\"\\ud834\\udd1e\""}, |
| 66 {"\"\\ud834\\udd1e\"", "\"\\ud834\\udd1e\""}, |
| 67 /* Testing nested empty containers. */ |
| 68 { |
| 69 " [ [ ] , { } , [ ] ] ", "[[],{},[]]", |
| 70 }, |
| 71 /* Testing escapes and control chars in key strings. */ |
| 72 {" { \"\\u007f\x7f\\n\\r\\\"\\f\\b\\\\a , b\": 1, \"\": 0 } ", |
| 73 "{\"\\u007f\\u007f\\n\\r\\\"\\f\\b\\\\a , b\":1,\"\":0}"}, |
| 74 /* Testing the writer's ability to cut off invalid UTF-8 sequences. */ |
| 75 {"\"abc\xf0\x9d\x24\"", "\"abc\""}, |
| 76 {"\"\xff\"", "\"\""}, |
| 77 /* Testing valid number parsing. */ |
| 78 {"[0, 42 , 0.0123, 123.456]", "[0,42,0.0123,123.456]"}, |
| 79 {"[1e4,-53.235e-31, 0.3e+3]", "[1e4,-53.235e-31,0.3e+3]"}, |
| 80 /* Testing keywords parsing. */ |
| 81 {"[true, false, null]", "[true,false,null]"}, |
| 82 |
| 83 /* Testing invalid parsing. */ |
| 84 |
| 85 /* Testing plain invalid things, exercising the state machine. */ |
| 86 {"\\", NULL}, |
| 87 {"nu ll", NULL}, |
| 88 {"fals", NULL}, |
| 89 /* Testing unterminated string. */ |
| 90 {"\"\\x", NULL}, |
| 91 /* Testing invalid UTF-16 number. */ |
| 92 {"\"\\u123x", NULL}, |
| 93 /* Testing imbalanced surrogate pairs. */ |
| 94 {"\"\\ud834f", NULL}, |
| 95 {"\"\\ud834\\n", NULL}, |
| 96 {"\"\\udd1ef", NULL}, |
| 97 {"\"\\ud834\\ud834\"", NULL}, |
| 98 {"\"\\ud834\\u1234\"", NULL}, |
| 99 {"\"\\ud834]\"", NULL}, |
| 100 {"\"\\ud834 \"", NULL}, |
| 101 {"\"\\ud834\\\\\"", NULL}, |
| 102 /* Testing embedded invalid whitechars. */ |
| 103 {"\"\n\"", NULL}, |
| 104 {"\"\t\"", NULL}, |
| 105 /* Testing empty json data. */ |
| 106 {"", NULL}, |
| 107 /* Testing extra characters after end of parsing. */ |
| 108 {"{},", NULL}, |
| 109 /* Testing imbalanced containers. */ |
| 110 {"{}}", NULL}, |
| 111 {"[]]", NULL}, |
| 112 {"{{}", NULL}, |
| 113 {"[[]", NULL}, |
| 114 {"[}", NULL}, |
| 115 {"{]", NULL}, |
| 116 /* Testing bad containers. */ |
| 117 {"{x}", NULL}, |
| 118 {"{x=0,y}", NULL}, |
| 119 /* Testing trailing comma. */ |
| 120 {"{,}", NULL}, |
| 121 {"[1,2,3,4,]", NULL}, |
| 122 {"{\"a\": 1, }", NULL}, |
| 123 /* Testing after-ending characters. */ |
| 124 {"{}x", NULL}, |
| 125 /* Testing having a key syntax in an array. */ |
| 126 {"[\"x\":0]", NULL}, |
| 127 /* Testing invalid numbers. */ |
| 128 {"1.", NULL}, |
| 129 {"1e", NULL}, |
| 130 {".12", NULL}, |
| 131 {"1.x", NULL}, |
| 132 {"1.12x", NULL}, |
| 133 {"1ex", NULL}, |
| 134 {"1e12x", NULL}, |
| 135 {".12x", NULL}, |
| 136 {"000", NULL}, |
| 137 }; |
| 138 |
| 139 static void test_pairs() { |
| 140 unsigned i; |
| 141 |
| 142 for (i = 0; i < GPR_ARRAY_SIZE(testing_pairs); i++) { |
| 143 testing_pair *pair = testing_pairs + i; |
| 144 char *scratchpad = gpr_strdup(pair->input); |
| 145 grpc_json *json; |
| 146 |
| 147 gpr_log(GPR_INFO, "parsing string %i - should %s", i, |
| 148 pair->output ? "succeed" : "fail"); |
| 149 json = grpc_json_parse_string(scratchpad); |
| 150 |
| 151 if (pair->output) { |
| 152 char *output; |
| 153 |
| 154 GPR_ASSERT(json); |
| 155 output = grpc_json_dump_to_string(json, 0); |
| 156 GPR_ASSERT(output); |
| 157 gpr_log(GPR_INFO, "succeeded with output = %s", output); |
| 158 GPR_ASSERT(strcmp(output, pair->output) == 0); |
| 159 |
| 160 grpc_json_destroy(json); |
| 161 gpr_free(output); |
| 162 } else { |
| 163 gpr_log(GPR_INFO, "failed"); |
| 164 GPR_ASSERT(!json); |
| 165 } |
| 166 |
| 167 gpr_free(scratchpad); |
| 168 } |
| 169 } |
| 170 |
| 171 static void test_atypical() { |
| 172 char *scratchpad = gpr_strdup("[[],[],[]]"); |
| 173 grpc_json *json = grpc_json_parse_string(scratchpad); |
| 174 grpc_json *brother; |
| 175 |
| 176 GPR_ASSERT(json); |
| 177 GPR_ASSERT(json->child); |
| 178 brother = json->child->next; |
| 179 grpc_json_destroy(json->child); |
| 180 GPR_ASSERT(json->child == brother); |
| 181 grpc_json_destroy(json->child->next); |
| 182 grpc_json_destroy(json); |
| 183 gpr_free(scratchpad); |
| 184 } |
| 185 |
| 186 int main(int argc, char **argv) { |
| 187 grpc_test_init(argc, argv); |
| 188 test_pairs(); |
| 189 test_atypical(); |
| 190 gpr_log(GPR_INFO, "json_test success"); |
| 191 return 0; |
| 192 } |
OLD | NEW |