diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/core/echo/client.c | 4 | ||||
-rw-r--r-- | test/core/json/json_rewrite.c | 261 | ||||
-rw-r--r-- | test/core/json/json_rewrite_test.c | 322 | ||||
-rw-r--r-- | test/core/json/json_test.c | 177 | ||||
-rw-r--r-- | test/core/json/rewrite_test_input.json | 203 | ||||
-rw-r--r-- | test/core/json/rewrite_test_output_condensed.json | 1 | ||||
-rw-r--r-- | test/core/json/rewrite_test_output_indented.json | 272 | ||||
-rw-r--r-- | test/core/security/credentials_test.c | 26 | ||||
-rw-r--r-- | test/core/security/json_token_test.c | 143 |
9 files changed, 1338 insertions, 71 deletions
diff --git a/test/core/echo/client.c b/test/core/echo/client.c index 3c789f84f9..cc78c40bbc 100644 --- a/test/core/echo/client.c +++ b/test/core/echo/client.c @@ -78,7 +78,9 @@ int main(int argc, char **argv) { GPR_ASSERT(argc == 2); channel = grpc_channel_create(argv[1], NULL); - call = grpc_channel_create_call_old(channel, "/foo", "localhost", gpr_inf_future); + call = grpc_channel_create_call_old( + channel, "/foo", "localhost", + gpr_time_add(gpr_time_from_seconds(5), gpr_now())); GPR_ASSERT(grpc_call_invoke(call, cq, (void *)1, (void *)1, 0) == GRPC_CALL_OK); diff --git a/test/core/json/json_rewrite.c b/test/core/json/json_rewrite.c new file mode 100644 index 0000000000..edece98d57 --- /dev/null +++ b/test/core/json/json_rewrite.c @@ -0,0 +1,261 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <stdio.h> +#include <stdlib.h> + +#include <grpc/support/cmdline.h> +#include <grpc/support/alloc.h> + +#include "src/core/json/json_reader.h" +#include "src/core/json/json_writer.h" + +typedef struct json_writer_userdata { + FILE* out; +} json_writer_userdata; + +typedef struct stacked_container { + grpc_json_type type; + struct stacked_container* next; +} stacked_container; + +typedef struct json_reader_userdata { + FILE* in; + grpc_json_writer* writer; + char* scratchpad; + char* ptr; + size_t free_space; + size_t allocated; + size_t string_len; + stacked_container* top; +} json_reader_userdata; + +static void json_writer_output_char(void* userdata, char c) { + json_writer_userdata* state = userdata; + fputc(c, state->out); +} + +static void json_writer_output_string(void* userdata, const char* str) { + json_writer_userdata* state = userdata; + fputs(str, state->out); +} + +static void json_writer_output_string_with_len(void* userdata, const char* str, + size_t len) { + json_writer_userdata* state = userdata; + fwrite(str, len, 1, state->out); +} + +grpc_json_writer_vtable writer_vtable = { + json_writer_output_char, + json_writer_output_string, + json_writer_output_string_with_len +}; + +static void check_string(json_reader_userdata* state, size_t needed) { + if (state->free_space >= needed) return; + needed -= state->free_space; + needed = (needed + 0xff) & ~0xff; + state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); + state->free_space += needed; + state->allocated += needed; +} + +static void json_reader_string_clear(void* userdata) { + json_reader_userdata* state = userdata; + state->free_space = state->allocated; + state->string_len = 0; +} + +static void json_reader_string_add_char(void* userdata, gpr_uint32 c) { + json_reader_userdata* state = userdata; + check_string(state, 1); + state->scratchpad[state->string_len++] = c; +} + +static void json_reader_string_add_utf32(void* userdata, gpr_uint32 c) { + if (c <= 0x7f) { + json_reader_string_add_char(userdata, c); + } else if (c <= 0x7ff) { + int b1 = 0xc0 | ((c >> 6) & 0x1f); + int b2 = 0x80 | (c & 0x3f); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + } else if (c <= 0xffff) { + int b1 = 0xe0 | ((c >> 12) & 0x0f); + int b2 = 0x80 | ((c >> 6) & 0x3f); + int b3 = 0x80 | (c & 0x3f); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + } else if (c <= 0x1fffff) { + int b1 = 0xf0 | ((c >> 18) & 0x07); + int b2 = 0x80 | ((c >> 12) & 0x3f); + int b3 = 0x80 | ((c >> 6) & 0x3f); + int b4 = 0x80 | (c & 0x3f); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + json_reader_string_add_char(userdata, b4); + } +} + +static gpr_uint32 json_reader_read_char(void* userdata) { + gpr_uint32 r; + json_reader_userdata* state = userdata; + + r = fgetc(state->in); + if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; + return r; +} + +static void json_reader_container_begins(void* userdata, grpc_json_type type) { + json_reader_userdata* state = userdata; + stacked_container* container = gpr_malloc(sizeof(stacked_container)); + + container->type = type; + container->next = state->top; + state->top = container; + + grpc_json_writer_container_begins(state->writer, type); +} + +static grpc_json_type json_reader_container_ends(void* userdata) { + json_reader_userdata* state = userdata; + stacked_container* container = state->top; + + grpc_json_writer_container_ends(state->writer, container->type); + state->top = container->next; + gpr_free(container); + return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; +} + +static void json_reader_set_key(void* userdata) { + json_reader_userdata* state = userdata; + json_reader_string_add_char(userdata, 0); + + grpc_json_writer_object_key(state->writer, state->scratchpad); +} + +static void json_reader_set_string(void* userdata) { + json_reader_userdata* state = userdata; + json_reader_string_add_char(userdata, 0); + + grpc_json_writer_value_string(state->writer, state->scratchpad); +} + +static int json_reader_set_number(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, + state->string_len); + + return 1; +} + +static void json_reader_set_true(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, "true", 4); +} + +static void json_reader_set_false(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, "false", 5); +} + +static void json_reader_set_null(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, "null", 4); +} + +static grpc_json_reader_vtable reader_vtable = { + json_reader_string_clear, + json_reader_string_add_char, + json_reader_string_add_utf32, + json_reader_read_char, + json_reader_container_begins, + json_reader_container_ends, + json_reader_set_key, + json_reader_set_string, + json_reader_set_number, + json_reader_set_true, + json_reader_set_false, + json_reader_set_null +}; + +int rewrite(FILE* in, FILE* out, int indent) { + grpc_json_writer writer; + grpc_json_reader reader; + grpc_json_reader_status status; + json_writer_userdata writer_user; + json_reader_userdata reader_user; + + reader_user.writer = &writer; + reader_user.in = in; + reader_user.top = NULL; + reader_user.scratchpad = NULL; + reader_user.string_len = 0; + reader_user.free_space = 0; + reader_user.allocated = 0; + + writer_user.out = out; + + grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); + grpc_json_reader_init(&reader, &reader_vtable, &reader_user); + + status = grpc_json_reader_run(&reader); + + free(reader_user.scratchpad); + while (reader_user.top) { + stacked_container* container = reader_user.top; + reader_user.top = container->next; + free(container); + } + + return status == GRPC_JSON_DONE; +} + +int main(int argc, char** argv) { + int indent = 2; + gpr_cmdline* cl; + + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_int(cl, "indent", NULL, &indent); + gpr_cmdline_parse(cl, argc, argv); + gpr_cmdline_destroy(cl); + + return rewrite(stdin, stdout, indent) ? 0 : 1; +} diff --git a/test/core/json/json_rewrite_test.c b/test/core/json/json_rewrite_test.c new file mode 100644 index 0000000000..e47a418761 --- /dev/null +++ b/test/core/json/json_rewrite_test.c @@ -0,0 +1,322 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <stdio.h> +#include <stdlib.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/useful.h> +#include <grpc/support/log.h> +#include "test/core/util/test_config.h" + +#include "src/core/json/json_reader.h" +#include "src/core/json/json_writer.h" + +typedef struct json_writer_userdata { + FILE* cmp; +} json_writer_userdata; + +typedef struct stacked_container { + grpc_json_type type; + struct stacked_container* next; +} stacked_container; + +typedef struct json_reader_userdata { + FILE* in; + grpc_json_writer* writer; + char* scratchpad; + char* ptr; + size_t free_space; + size_t allocated; + size_t string_len; + stacked_container* top; + int did_eagain; +} json_reader_userdata; + +static void json_writer_output_char(void* userdata, char c) { + json_writer_userdata* state = userdata; + int cmp = fgetc(state->cmp); + GPR_ASSERT(cmp == c); +} + +static void json_writer_output_string(void* userdata, const char* str) { + while (*str) { + json_writer_output_char(userdata, *str++); + } +} + +static void json_writer_output_string_with_len(void* userdata, const char* str, + size_t len) { + size_t i; + for (i = 0; i < len; i++) { + json_writer_output_char(userdata, str[i]); + } +} + +grpc_json_writer_vtable writer_vtable = { + json_writer_output_char, + json_writer_output_string, + json_writer_output_string_with_len +}; + +static void check_string(json_reader_userdata* state, size_t needed) { + if (state->free_space >= needed) return; + needed -= state->free_space; + needed = (needed + 0xff) & ~0xff; + state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); + state->free_space += needed; + state->allocated += needed; +} + +static void json_reader_string_clear(void* userdata) { + json_reader_userdata* state = userdata; + state->free_space = state->allocated; + state->string_len = 0; +} + +static void json_reader_string_add_char(void* userdata, gpr_uint32 c) { + json_reader_userdata* state = userdata; + check_string(state, 1); + state->scratchpad[state->string_len++] = c; +} + +static void json_reader_string_add_utf32(void* userdata, gpr_uint32 c) { + if (c <= 0x7f) { + json_reader_string_add_char(userdata, c); + } else if (c <= 0x7ff) { + int b1 = 0xc0 | ((c >> 6) & 0x1f); + int b2 = 0x80 | (c & 0x3f); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + } else if (c <= 0xffff) { + int b1 = 0xe0 | ((c >> 12) & 0x0f); + int b2 = 0x80 | ((c >> 6) & 0x3f); + int b3 = 0x80 | (c & 0x3f); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + } else if (c <= 0x1fffff) { + int b1 = 0xf0 | ((c >> 18) & 0x07); + int b2 = 0x80 | ((c >> 12) & 0x3f); + int b3 = 0x80 | ((c >> 6) & 0x3f); + int b4 = 0x80 | (c & 0x3f); + json_reader_string_add_char(userdata, b1); + json_reader_string_add_char(userdata, b2); + json_reader_string_add_char(userdata, b3); + json_reader_string_add_char(userdata, b4); + } +} + +static gpr_uint32 json_reader_read_char(void* userdata) { + gpr_uint32 r; + json_reader_userdata* state = userdata; + + if (!state->did_eagain) { + state->did_eagain = 1; + return GRPC_JSON_READ_CHAR_EAGAIN; + } + + state->did_eagain = 0; + + r = fgetc(state->in); + if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; + return r; +} + +static void json_reader_container_begins(void* userdata, grpc_json_type type) { + json_reader_userdata* state = userdata; + stacked_container* container = gpr_malloc(sizeof(stacked_container)); + + container->type = type; + container->next = state->top; + state->top = container; + + grpc_json_writer_container_begins(state->writer, type); +} + +static grpc_json_type json_reader_container_ends(void* userdata) { + json_reader_userdata* state = userdata; + stacked_container* container = state->top; + + grpc_json_writer_container_ends(state->writer, container->type); + state->top = container->next; + gpr_free(container); + return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; +} + +static void json_reader_set_key(void* userdata) { + json_reader_userdata* state = userdata; + json_reader_string_add_char(userdata, 0); + + grpc_json_writer_object_key(state->writer, state->scratchpad); +} + +static void json_reader_set_string(void* userdata) { + json_reader_userdata* state = userdata; + json_reader_string_add_char(userdata, 0); + + grpc_json_writer_value_string(state->writer, state->scratchpad); +} + +static int json_reader_set_number(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, + state->string_len); + + return 1; +} + +static void json_reader_set_true(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, "true", 4); +} + +static void json_reader_set_false(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, "false", 5); +} + +static void json_reader_set_null(void* userdata) { + json_reader_userdata* state = userdata; + + grpc_json_writer_value_raw_with_len(state->writer, "null", 4); +} + +static grpc_json_reader_vtable reader_vtable = { + json_reader_string_clear, + json_reader_string_add_char, + json_reader_string_add_utf32, + json_reader_read_char, + json_reader_container_begins, + json_reader_container_ends, + json_reader_set_key, + json_reader_set_string, + json_reader_set_number, + json_reader_set_true, + json_reader_set_false, + json_reader_set_null +}; + +int rewrite_and_compare(FILE* in, FILE* cmp, int indent) { + grpc_json_writer writer; + grpc_json_reader reader; + grpc_json_reader_status status; + json_writer_userdata writer_user; + json_reader_userdata reader_user; + + GPR_ASSERT(in); + GPR_ASSERT(cmp); + + reader_user.writer = &writer; + reader_user.in = in; + reader_user.top = NULL; + reader_user.scratchpad = NULL; + reader_user.string_len = 0; + reader_user.free_space = 0; + reader_user.allocated = 0; + reader_user.did_eagain = 0; + + writer_user.cmp = cmp; + + grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); + grpc_json_reader_init(&reader, &reader_vtable, &reader_user); + + do { + status = grpc_json_reader_run(&reader); + } while (status == GRPC_JSON_EAGAIN); + + free(reader_user.scratchpad); + while (reader_user.top) { + stacked_container* container = reader_user.top; + reader_user.top = container->next; + free(container); + } + + return status == GRPC_JSON_DONE; +} + +typedef struct test_file { + const char* input; + const char* cmp; + int indent; +} test_file; + +static test_file test_files[] = { + { + "test/core/json/rewrite_test_input.json", + "test/core/json/rewrite_test_output_condensed.json", + 0 + }, + { + "test/core/json/rewrite_test_input.json", + "test/core/json/rewrite_test_output_indented.json", + 2 + }, + { + "test/core/json/rewrite_test_output_indented.json", + "test/core/json/rewrite_test_output_condensed.json", + 0 + }, + { + "test/core/json/rewrite_test_output_condensed.json", + "test/core/json/rewrite_test_output_indented.json", + 2 + }, +}; + +void test_rewrites() { + int i; + + for (i = 0; i < GPR_ARRAY_SIZE(test_files); i++) { + test_file* test = test_files + i; + FILE* input = fopen(test->input, "rb"); + FILE* cmp = fopen(test->cmp, "rb"); + int status; + gpr_log(GPR_INFO, "Testing file %s against %s using indent=%i", + test->input, test->cmp, test->indent); + status = rewrite_and_compare(input, cmp, test->indent); + GPR_ASSERT(status); + fclose(input); + fclose(cmp); + } +} + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + test_rewrites(); + gpr_log(GPR_INFO, "json_rewrite_test success"); + return 0; +} diff --git a/test/core/json/json_test.c b/test/core/json/json_test.c new file mode 100644 index 0000000000..c234eb9ede --- /dev/null +++ b/test/core/json/json_test.c @@ -0,0 +1,177 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <string.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/useful.h> +#include <grpc/support/log.h> +#include "src/core/json/json.h" +#include "src/core/support/string.h" + +#include "test/core/util/test_config.h" + +typedef struct testing_pair { + const char* input; + const char* output; +} testing_pair; + +static testing_pair testing_pairs[] = { + /* Testing valid parsing. */ + + /* Testing trivial parses, with de-indentation. */ + { " 0 ", "0" }, + { " 1 ", "1" }, + { " \"a\" ", "\"a\"" }, + { " true ", "true" }, + /* Testing the parser's ability to decode trivial UTF-16. */ + { "\"\\u0020\\\\\\u0010\\u000a\\u000D\"", "\" \\\\\\u0010\\n\\r\"" }, + /* Testing various UTF-8 sequences. */ + { "\"ßâñć௵⇒\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"" }, + { "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"" }, + /* Testing UTF-8 character "𝄞", U+11D1E. */ + { "\"\xf0\x9d\x84\x9e\"", "\"\\ud834\\udd1e\"" }, + { "\"\\ud834\\udd1e\"", "\"\\ud834\\udd1e\"" }, + /* Testing nested empty containers. */ + { " [ [ ] , { } , [ ] ] ", "[[],{},[]]", }, + /* Testing escapes and control chars in key strings. */ + { " { \"\\n\\\\a , b\": 1, \"\": 0 } ", "{\"\\n\\\\a , b\":1,\"\":0}" }, + /* Testing the writer's ability to cut off invalid UTF-8 sequences. */ + { "\"abc\xf0\x9d\x24\"", "\"abc\"" }, + { "\"\xff\"", "\"\"" }, + /* Testing valid number parsing. */ + { "[0, 42 , 0.0123, 123.456]", "[0,42,0.0123,123.456]"}, + { "[1e4,-53.235e-31, 0.3e+3]", "[1e4,-53.235e-31,0.3e+3]" }, + /* Testing keywords parsing. */ + { "[true, false, null]", "[true,false,null]" }, + + + /* Testing invalid parsing. */ + + /* Testing plain invalid things, exercising the state machine. */ + { "\\", NULL }, + { "nu ll", NULL }, + { "fals", NULL }, + /* Testing unterminated string. */ + { "\"\\x", NULL }, + /* Testing invalid UTF-16 number. */ + { "\"\\u123x", NULL }, + /* Testing imbalanced surrogate pairs. */ + { "\"\\ud834f", NULL }, + { "\"\\ud834\\n", NULL }, + { "\"\\udd1ef", NULL }, + { "\"\\ud834\\ud834\"", NULL }, + { "\"\\ud834\\u1234\"", NULL }, + /* Testing embedded invalid whitechars. */ + { "\"\n\"", NULL }, + { "\"\t\"", NULL }, + /* Testing empty json data. */ + { "", NULL }, + /* Testing extra characters after end of parsing. */ + { "{},", NULL }, + /* Testing imbalanced containers. */ + { "{}}", NULL }, + { "[]]", NULL }, + { "{{}", NULL }, + { "[[]", NULL }, + { "[}", NULL }, + { "{]", NULL }, + /*Testing trailing comma. */ + { "{,}", NULL }, + { "[1,2,3,4,]", NULL }, + /* Testing having a key syntax in an array. */ + { "[\"x\":0]", NULL }, + /* Testing invalid numbers. */ + { "1.", NULL }, + { "1e", NULL }, + { ".12", NULL }, + { "1.x", NULL }, + { "1.12x", NULL }, + { "1ex", NULL }, + { "1e12x", NULL }, + { ".12x", NULL }, + { "000", NULL }, +}; + +static void test_pairs() { + int i; + + for (i = 0; i < GPR_ARRAY_SIZE(testing_pairs); i++) { + testing_pair* pair = testing_pairs + i; + char* scratchpad = gpr_strdup(pair->input); + grpc_json* json; + + gpr_log(GPR_INFO, "parsing string %i - should %s", i, + pair->output ? "succeed" : "fail"); + json = grpc_json_parse_string(scratchpad); + + if (pair->output) { + char* output; + + GPR_ASSERT(json); + output = grpc_json_dump_to_string(json, 0); + GPR_ASSERT(output); + gpr_log(GPR_INFO, "succeeded with output = %s", output); + GPR_ASSERT(strcmp(output, pair->output) == 0); + + grpc_json_destroy(json); + gpr_free(output); + } else { + gpr_log(GPR_INFO, "failed"); + GPR_ASSERT(!json); + } + + free(scratchpad); + } +} + +static void test_atypical() { + char* scratchpad = gpr_strdup("[[],[]]"); + grpc_json* json = grpc_json_parse_string(scratchpad); + grpc_json* brother; + + GPR_ASSERT(json); + GPR_ASSERT(json->child); + brother = json->child->next; + grpc_json_destroy(json->child); + json->child = brother; + grpc_json_destroy(json); +} + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_pairs(); + test_atypical(); + gpr_log(GPR_INFO, "json_test success"); + return 0; +} diff --git a/test/core/json/rewrite_test_input.json b/test/core/json/rewrite_test_input.json new file mode 100644 index 0000000000..568891474f --- /dev/null +++ b/test/core/json/rewrite_test_input.json @@ -0,0 +1,203 @@ +{ +"unicode, escape and empty test": { "a\tb": "\u00eb", "empty": [{},[],{}] }, +"some more unicode tests": { + "typical utf-8 input (plane 0)": "ßâñć⇒", + "atypical utf-8 input (plane 1)": "𝄞" +}, + +"whitespace test": { "trying" : +"to" + , + + "break" + : + "the" , + "parser": "a bit" } , + +"#": "All these examples are from http://json.org/example", +"test1": +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "markup" + } + } + } + } +}, + +"test2": +{"menu": { + "id": "file", + "value": "File", + "popup": { + "menuitem": [ + {"value": "New", "onclick": "CreateNewDoc()"}, + {"value": "Open", "onclick": "OpenDoc()"}, + {"value": "Close", "onclick": "CloseDoc()"} + ] + } +}}, + +"test3": +{"widget": { + "debug": "on", + "window": { + "title": "Sample Konfabulator Widget", + "name": "main_window", + "width": 500, + "height": 500 + }, + "image": { + "src": "Images/Sun.png", + "name": "sun1", + "hOffset": 250, + "vOffset": 250, + "alignment": "center" + }, + "text": { + "data": "Click Here", + "size": 36, + "style": "bold", + "name": "text1", + "hOffset": 250, + "vOffset": 100, + "alignment": "center", + "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" + } +}}, + +"test4": +{"web-app": { + "servlet": [ + { + "servlet-name": "cofaxCDS", + "servlet-class": "org.cofax.cds.CDSServlet", + "init-param": { + "configGlossary:installationAt": "Philadelphia, PA", + "configGlossary:adminEmail": "ksm@pobox.com", + "configGlossary:poweredBy": "Cofax", + "configGlossary:poweredByIcon": "/images/cofax.gif", + "configGlossary:staticPath": "/content/static", + "templateProcessorClass": "org.cofax.WysiwygTemplate", + "templateLoaderClass": "org.cofax.FilesTemplateLoader", + "templatePath": "templates", + "templateOverridePath": "", + "defaultListTemplate": "listTemplate.htm", + "defaultFileTemplate": "articleTemplate.htm", + "useJSP": false, + "jspListTemplate": "listTemplate.jsp", + "jspFileTemplate": "articleTemplate.jsp", + "cachePackageTagsTrack": 200, + "cachePackageTagsStore": 200, + "cachePackageTagsRefresh": 60, + "cacheTemplatesTrack": 100, + "cacheTemplatesStore": 50, + "cacheTemplatesRefresh": 15, + "cachePagesTrack": 200, + "cachePagesStore": 100, + "cachePagesRefresh": 10, + "cachePagesDirtyRead": 10, + "searchEngineListTemplate": "forSearchEnginesList.htm", + "searchEngineFileTemplate": "forSearchEngines.htm", + "searchEngineRobotsDb": "WEB-INF/robots.db", + "useDataStore": true, + "dataStoreClass": "org.cofax.SqlDataStore", + "redirectionClass": "org.cofax.SqlRedirection", + "dataStoreName": "cofax", + "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", + "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", + "dataStoreUser": "sa", + "dataStorePassword": "dataStoreTestQuery", + "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", + "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", + "dataStoreInitConns": 10, + "dataStoreMaxConns": 100, + "dataStoreConnUsageLimit": 100, + "dataStoreLogLevel": "debug", + "maxUrlLength": 500}}, + { + "servlet-name": "cofaxEmail", + "servlet-class": "org.cofax.cds.EmailServlet", + "init-param": { + "mailHost": "mail1", + "mailHostOverride": "mail2"}}, + { + "servlet-name": "cofaxAdmin", + "servlet-class": "org.cofax.cds.AdminServlet"}, + + { + "servlet-name": "fileServlet", + "servlet-class": "org.cofax.cds.FileServlet"}, + { + "servlet-name": "cofaxTools", + "servlet-class": "org.cofax.cms.CofaxToolsServlet", + "init-param": { + "templatePath": "toolstemplates/", + "log": 1, + "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", + "logMaxSize": "", + "dataLog": 1, + "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", + "dataLogMaxSize": "", + "removePageCache": "/content/admin/remove?cache=pages&id=", + "removeTemplateCache": "/content/admin/remove?cache=templates&id=", + "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", + "lookInContext": 1, + "adminGroupID": 4, + "betaServer": true}}], + "servlet-mapping": { + "cofaxCDS": "/", + "cofaxEmail": "/cofaxutil/aemail/*", + "cofaxAdmin": "/admin/*", + "fileServlet": "/static/*", + "cofaxTools": "/tools/*"}, + + "taglib": { + "taglib-uri": "cofax.tld", + "taglib-location": "/WEB-INF/tlds/cofax.tld"}}}, + +"test5": +{"menu": { + "header": "SVG Viewer", + "items": [ + {"id": "Open"}, + {"id": "OpenNew", "label": "Open New"}, + null, + {"id": "ZoomIn", "label": "Zoom In"}, + {"id": "ZoomOut", "label": "Zoom Out"}, + {"id": "OriginalView", "label": "Original View"}, + null, + {"id": "Quality"}, + {"id": "Pause"}, + {"id": "Mute"}, + null, + {"id": "Find", "label": "Find..."}, + {"id": "FindAgain", "label": "Find Again"}, + {"id": "Copy"}, + {"id": "CopyAgain", "label": "Copy Again"}, + {"id": "CopySVG", "label": "Copy SVG"}, + {"id": "ViewSVG", "label": "View SVG"}, + {"id": "ViewSource", "label": "View Source"}, + {"id": "SaveAs", "label": "Save As"}, + null, + {"id": "Help"}, + {"id": "About", "label": "About Adobe CVG Viewer..."} + ] +}} + + +} diff --git a/test/core/json/rewrite_test_output_condensed.json b/test/core/json/rewrite_test_output_condensed.json new file mode 100644 index 0000000000..3adbbd99d6 --- /dev/null +++ b/test/core/json/rewrite_test_output_condensed.json @@ -0,0 +1 @@ +{"unicode, escape and empty test":{"a\tb":"\u00eb","empty":[{},[],{}]},"some more unicode tests":{"typical utf-8 input (plane 0)":"\u00df\u00e2\u00f1\u0107\u21d2","atypical utf-8 input (plane 1)":"\ud834\udd1e"},"whitespace test":{"trying":"to","break":"the","parser":"a bit"},"#":"All these examples are from http://json.org/example","test1":{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}},"test2":{"menu":{"id":"file","value":"File","popup":{"menuitem":[{"value":"New","onclick":"CreateNewDoc()"},{"value":"Open","onclick":"OpenDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}},"test3":{"widget":{"debug":"on","window":{"title":"Sample Konfabulator Widget","name":"main_window","width":500,"height":500},"image":{"src":"Images/Sun.png","name":"sun1","hOffset":250,"vOffset":250,"alignment":"center"},"text":{"data":"Click Here","size":36,"style":"bold","name":"text1","hOffset":250,"vOffset":100,"alignment":"center","onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;"}}},"test4":{"web-app":{"servlet":[{"servlet-name":"cofaxCDS","servlet-class":"org.cofax.cds.CDSServlet","init-param":{"configGlossary:installationAt":"Philadelphia, PA","configGlossary:adminEmail":"ksm@pobox.com","configGlossary:poweredBy":"Cofax","configGlossary:poweredByIcon":"/images/cofax.gif","configGlossary:staticPath":"/content/static","templateProcessorClass":"org.cofax.WysiwygTemplate","templateLoaderClass":"org.cofax.FilesTemplateLoader","templatePath":"templates","templateOverridePath":"","defaultListTemplate":"listTemplate.htm","defaultFileTemplate":"articleTemplate.htm","useJSP":false,"jspListTemplate":"listTemplate.jsp","jspFileTemplate":"articleTemplate.jsp","cachePackageTagsTrack":200,"cachePackageTagsStore":200,"cachePackageTagsRefresh":60,"cacheTemplatesTrack":100,"cacheTemplatesStore":50,"cacheTemplatesRefresh":15,"cachePagesTrack":200,"cachePagesStore":100,"cachePagesRefresh":10,"cachePagesDirtyRead":10,"searchEngineListTemplate":"forSearchEnginesList.htm","searchEngineFileTemplate":"forSearchEngines.htm","searchEngineRobotsDb":"WEB-INF/robots.db","useDataStore":true,"dataStoreClass":"org.cofax.SqlDataStore","redirectionClass":"org.cofax.SqlRedirection","dataStoreName":"cofax","dataStoreDriver":"com.microsoft.jdbc.sqlserver.SQLServerDriver","dataStoreUrl":"jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon","dataStoreUser":"sa","dataStorePassword":"dataStoreTestQuery","dataStoreTestQuery":"SET NOCOUNT ON;select test='test';","dataStoreLogFile":"/usr/local/tomcat/logs/datastore.log","dataStoreInitConns":10,"dataStoreMaxConns":100,"dataStoreConnUsageLimit":100,"dataStoreLogLevel":"debug","maxUrlLength":500}},{"servlet-name":"cofaxEmail","servlet-class":"org.cofax.cds.EmailServlet","init-param":{"mailHost":"mail1","mailHostOverride":"mail2"}},{"servlet-name":"cofaxAdmin","servlet-class":"org.cofax.cds.AdminServlet"},{"servlet-name":"fileServlet","servlet-class":"org.cofax.cds.FileServlet"},{"servlet-name":"cofaxTools","servlet-class":"org.cofax.cms.CofaxToolsServlet","init-param":{"templatePath":"toolstemplates/","log":1,"logLocation":"/usr/local/tomcat/logs/CofaxTools.log","logMaxSize":"","dataLog":1,"dataLogLocation":"/usr/local/tomcat/logs/dataLog.log","dataLogMaxSize":"","removePageCache":"/content/admin/remove?cache=pages&id=","removeTemplateCache":"/content/admin/remove?cache=templates&id=","fileTransferFolder":"/usr/local/tomcat/webapps/content/fileTransferFolder","lookInContext":1,"adminGroupID":4,"betaServer":true}}],"servlet-mapping":{"cofaxCDS":"/","cofaxEmail":"/cofaxutil/aemail/*","cofaxAdmin":"/admin/*","fileServlet":"/static/*","cofaxTools":"/tools/*"},"taglib":{"taglib-uri":"cofax.tld","taglib-location":"/WEB-INF/tlds/cofax.tld"}}},"test5":{"menu":{"header":"SVG Viewer","items":[{"id":"Open"},{"id":"OpenNew","label":"Open New"},null,{"id":"ZoomIn","label":"Zoom In"},{"id":"ZoomOut","label":"Zoom Out"},{"id":"OriginalView","label":"Original View"},null,{"id":"Quality"},{"id":"Pause"},{"id":"Mute"},null,{"id":"Find","label":"Find..."},{"id":"FindAgain","label":"Find Again"},{"id":"Copy"},{"id":"CopyAgain","label":"Copy Again"},{"id":"CopySVG","label":"Copy SVG"},{"id":"ViewSVG","label":"View SVG"},{"id":"ViewSource","label":"View Source"},{"id":"SaveAs","label":"Save As"},null,{"id":"Help"},{"id":"About","label":"About Adobe CVG Viewer..."}]}}}
\ No newline at end of file diff --git a/test/core/json/rewrite_test_output_indented.json b/test/core/json/rewrite_test_output_indented.json new file mode 100644 index 0000000000..7ac9f49f22 --- /dev/null +++ b/test/core/json/rewrite_test_output_indented.json @@ -0,0 +1,272 @@ +{ + "unicode, escape and empty test": { + "a\tb": "\u00eb", + "empty": [ + {}, + [], + {} + ] + }, + "some more unicode tests": { + "typical utf-8 input (plane 0)": "\u00df\u00e2\u00f1\u0107\u21d2", + "atypical utf-8 input (plane 1)": "\ud834\udd1e" + }, + "whitespace test": { + "trying": "to", + "break": "the", + "parser": "a bit" + }, + "#": "All these examples are from http://json.org/example", + "test1": { + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": [ + "GML", + "XML" + ] + }, + "GlossSee": "markup" + } + } + } + } + }, + "test2": { + "menu": { + "id": "file", + "value": "File", + "popup": { + "menuitem": [ + { + "value": "New", + "onclick": "CreateNewDoc()" + }, + { + "value": "Open", + "onclick": "OpenDoc()" + }, + { + "value": "Close", + "onclick": "CloseDoc()" + } + ] + } + } + }, + "test3": { + "widget": { + "debug": "on", + "window": { + "title": "Sample Konfabulator Widget", + "name": "main_window", + "width": 500, + "height": 500 + }, + "image": { + "src": "Images/Sun.png", + "name": "sun1", + "hOffset": 250, + "vOffset": 250, + "alignment": "center" + }, + "text": { + "data": "Click Here", + "size": 36, + "style": "bold", + "name": "text1", + "hOffset": 250, + "vOffset": 100, + "alignment": "center", + "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" + } + } + }, + "test4": { + "web-app": { + "servlet": [ + { + "servlet-name": "cofaxCDS", + "servlet-class": "org.cofax.cds.CDSServlet", + "init-param": { + "configGlossary:installationAt": "Philadelphia, PA", + "configGlossary:adminEmail": "ksm@pobox.com", + "configGlossary:poweredBy": "Cofax", + "configGlossary:poweredByIcon": "/images/cofax.gif", + "configGlossary:staticPath": "/content/static", + "templateProcessorClass": "org.cofax.WysiwygTemplate", + "templateLoaderClass": "org.cofax.FilesTemplateLoader", + "templatePath": "templates", + "templateOverridePath": "", + "defaultListTemplate": "listTemplate.htm", + "defaultFileTemplate": "articleTemplate.htm", + "useJSP": false, + "jspListTemplate": "listTemplate.jsp", + "jspFileTemplate": "articleTemplate.jsp", + "cachePackageTagsTrack": 200, + "cachePackageTagsStore": 200, + "cachePackageTagsRefresh": 60, + "cacheTemplatesTrack": 100, + "cacheTemplatesStore": 50, + "cacheTemplatesRefresh": 15, + "cachePagesTrack": 200, + "cachePagesStore": 100, + "cachePagesRefresh": 10, + "cachePagesDirtyRead": 10, + "searchEngineListTemplate": "forSearchEnginesList.htm", + "searchEngineFileTemplate": "forSearchEngines.htm", + "searchEngineRobotsDb": "WEB-INF/robots.db", + "useDataStore": true, + "dataStoreClass": "org.cofax.SqlDataStore", + "redirectionClass": "org.cofax.SqlRedirection", + "dataStoreName": "cofax", + "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", + "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", + "dataStoreUser": "sa", + "dataStorePassword": "dataStoreTestQuery", + "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", + "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", + "dataStoreInitConns": 10, + "dataStoreMaxConns": 100, + "dataStoreConnUsageLimit": 100, + "dataStoreLogLevel": "debug", + "maxUrlLength": 500 + } + }, + { + "servlet-name": "cofaxEmail", + "servlet-class": "org.cofax.cds.EmailServlet", + "init-param": { + "mailHost": "mail1", + "mailHostOverride": "mail2" + } + }, + { + "servlet-name": "cofaxAdmin", + "servlet-class": "org.cofax.cds.AdminServlet" + }, + { + "servlet-name": "fileServlet", + "servlet-class": "org.cofax.cds.FileServlet" + }, + { + "servlet-name": "cofaxTools", + "servlet-class": "org.cofax.cms.CofaxToolsServlet", + "init-param": { + "templatePath": "toolstemplates/", + "log": 1, + "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", + "logMaxSize": "", + "dataLog": 1, + "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", + "dataLogMaxSize": "", + "removePageCache": "/content/admin/remove?cache=pages&id=", + "removeTemplateCache": "/content/admin/remove?cache=templates&id=", + "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", + "lookInContext": 1, + "adminGroupID": 4, + "betaServer": true + } + } + ], + "servlet-mapping": { + "cofaxCDS": "/", + "cofaxEmail": "/cofaxutil/aemail/*", + "cofaxAdmin": "/admin/*", + "fileServlet": "/static/*", + "cofaxTools": "/tools/*" + }, + "taglib": { + "taglib-uri": "cofax.tld", + "taglib-location": "/WEB-INF/tlds/cofax.tld" + } + } + }, + "test5": { + "menu": { + "header": "SVG Viewer", + "items": [ + { + "id": "Open" + }, + { + "id": "OpenNew", + "label": "Open New" + }, + null, + { + "id": "ZoomIn", + "label": "Zoom In" + }, + { + "id": "ZoomOut", + "label": "Zoom Out" + }, + { + "id": "OriginalView", + "label": "Original View" + }, + null, + { + "id": "Quality" + }, + { + "id": "Pause" + }, + { + "id": "Mute" + }, + null, + { + "id": "Find", + "label": "Find..." + }, + { + "id": "FindAgain", + "label": "Find Again" + }, + { + "id": "Copy" + }, + { + "id": "CopyAgain", + "label": "Copy Again" + }, + { + "id": "CopySVG", + "label": "Copy SVG" + }, + { + "id": "ViewSVG", + "label": "View SVG" + }, + { + "id": "ViewSource", + "label": "View Source" + }, + { + "id": "SaveAs", + "label": "Save As" + }, + null, + { + "id": "Help" + }, + { + "id": "About", + "label": "About Adobe CVG Viewer..." + } + ] + } + } +}
\ No newline at end of file diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index 1c83cc8059..dd90a7edc8 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -55,23 +55,23 @@ static const char test_root_cert[] = "I am the root!"; Maximum size for a string literal is 509 chars in C89, yay! */ static const char test_json_key_str_part1[] = "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" - "\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\n7mJEqg" - "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\nyjSeg/" + "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJEqg" + "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" "rWBQvS4hle4LfijkP3J5BG+" - "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\nOnVF6N7dL3nTYZg+" - "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\nDZgSE6Bu/" - "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\n/" + "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" + "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" + "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" "8HpCqFYM9V8f34SBWfD4fRFT+n/" - "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\ngqXjDvpkypEusgXAykECQQD+"; + "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; static const char test_json_key_str_part2[] = - "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\nCslxoHQM8s+" - "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\nEkoy2L/" - "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\nAARh2QJBAMKeDAG" - "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\n8FZi5c8idxiwC36kbAL6HzA" - "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\n6z8RJm0+" + "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" + "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" + "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDAG" + "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6HzA" + "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" - "5nZ68ECQQDvYuI3\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZY" - "Ap6LI9W\nIqv4vr6y38N79TTC\n-----END PRIVATE KEY-----\n\", "; + "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZY" + "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; static const char test_json_key_str_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " diff --git a/test/core/security/json_token_test.c b/test/core/security/json_token_test.c index 3c26de683a..2a9c8f88b2 100644 --- a/test/core/security/json_token_test.c +++ b/test/core/security/json_token_test.c @@ -41,7 +41,7 @@ #include <grpc/support/log.h> #include <grpc/support/slice.h> #include "test/core/util/test_config.h" -#include "third_party/cJSON/cJSON.h" +#include "src/core/json/json.h" #include <openssl/evp.h> /* This JSON key was generated with the GCE console and revoked immediately. @@ -49,23 +49,23 @@ Maximum size for a string literal is 509 chars in C89, yay! */ static const char test_json_key_str_part1[] = "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" - "\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\n7mJEqg" - "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\nyjSeg/" + "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJEqg" + "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" "rWBQvS4hle4LfijkP3J5BG+" - "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\nOnVF6N7dL3nTYZg+" - "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\nDZgSE6Bu/" - "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\n/" + "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" + "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" + "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" "8HpCqFYM9V8f34SBWfD4fRFT+n/" - "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\ngqXjDvpkypEusgXAykECQQD+"; + "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; static const char test_json_key_str_part2[] = - "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\nCslxoHQM8s+" - "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\nEkoy2L/" - "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\nAARh2QJBAMKeDAG" - "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\n8FZi5c8idxiwC36kbAL6HzA" - "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\n6z8RJm0+" + "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" + "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" + "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDAG" + "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6HzA" + "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" - "5nZ68ECQQDvYuI3\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZY" - "Ap6LI9W\nIqv4vr6y38N79TTC\n-----END PRIVATE KEY-----\n\", "; + "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZY" + "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; static const char test_json_key_str_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " @@ -203,10 +203,11 @@ static void test_parse_json_key_failure_no_private_key(void) { grpc_auth_json_key_destruct(&json_key); } -static cJSON *parse_json_part_from_jwt(const char *str, size_t len) { +static grpc_json *parse_json_part_from_jwt(const char *str, size_t len, + char **scratchpad) { char *b64; char *decoded; - cJSON *json; + grpc_json *json; gpr_slice slice; b64 = gpr_malloc(len + 1); strncpy(b64, str, len); @@ -217,59 +218,84 @@ static cJSON *parse_json_part_from_jwt(const char *str, size_t len) { strncpy(decoded, (const char *)GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); decoded[GPR_SLICE_LENGTH(slice)] = '\0'; - json = cJSON_Parse(decoded); + json = grpc_json_parse_string(decoded); gpr_free(b64); - gpr_free(decoded); + *scratchpad = decoded; gpr_slice_unref(slice); return json; } -static void check_jwt_header(cJSON *header) { - cJSON *child = cJSON_GetObjectItem(header, "alg"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_String); - GPR_ASSERT(!strcmp(child->valuestring, "RS256")); - - child = cJSON_GetObjectItem(header, "typ"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_String); - GPR_ASSERT(!strcmp(child->valuestring, "JWT")); +static void check_jwt_header(grpc_json *header) { + grpc_json *ptr; + grpc_json *alg = NULL; + grpc_json *typ = NULL; + + for (ptr = header->child; ptr; ptr = ptr->next) { + if (strcmp(ptr->key, "alg") == 0) { + alg = ptr; + } else if (strcmp(ptr->key, "typ") == 0) { + typ = ptr; + } + } + GPR_ASSERT(alg != NULL); + GPR_ASSERT(alg->type == GRPC_JSON_STRING); + GPR_ASSERT(!strcmp(alg->value, "RS256")); + + GPR_ASSERT(typ != NULL); + GPR_ASSERT(typ->type == GRPC_JSON_STRING); + GPR_ASSERT(!strcmp(typ->value, "JWT")); } -static void check_jwt_claim(cJSON *claim) { - gpr_timespec exp = {0, 0}; +static void check_jwt_claim(grpc_json *claim) { + gpr_timespec expiration = {0, 0}; gpr_timespec issue_time = {0, 0}; gpr_timespec parsed_lifetime; - cJSON *child = cJSON_GetObjectItem(claim, "iss"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_String); + grpc_json *iss = NULL; + grpc_json *scope = NULL; + grpc_json *aud = NULL; + grpc_json *exp = NULL; + grpc_json *iat = NULL; + grpc_json *ptr; + + for (ptr = claim->child; ptr; ptr = ptr->next) { + if (strcmp(ptr->key, "iss") == 0) { + iss = ptr; + } else if (strcmp(ptr->key, "scope") == 0) { + scope = ptr; + } else if (strcmp(ptr->key, "aud") == 0) { + aud = ptr; + } else if (strcmp(ptr->key, "exp") == 0) { + exp = ptr; + } else if (strcmp(ptr->key, "iat") == 0) { + iat = ptr; + } + } + + GPR_ASSERT(iss != NULL); + GPR_ASSERT(iss->type == GRPC_JSON_STRING); GPR_ASSERT( !strcmp( - child->valuestring, + iss->value, "777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount.com")); - child = cJSON_GetObjectItem(claim, "scope"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_String); - GPR_ASSERT(!strcmp(child->valuestring, test_scope)); + GPR_ASSERT(scope != NULL); + GPR_ASSERT(scope->type == GRPC_JSON_STRING); + GPR_ASSERT(!strcmp(scope->value, test_scope)); - child = cJSON_GetObjectItem(claim, "aud"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_String); - GPR_ASSERT(!strcmp(child->valuestring, + GPR_ASSERT(aud != NULL); + GPR_ASSERT(aud->type == GRPC_JSON_STRING); + GPR_ASSERT(!strcmp(aud->value, "https://www.googleapis.com/oauth2/v3/token")); - child = cJSON_GetObjectItem(claim, "exp"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_Number); - exp.tv_sec = child->valueint; + GPR_ASSERT(exp != NULL); + GPR_ASSERT(exp->type == GRPC_JSON_NUMBER); + expiration.tv_sec = strtol(exp->value, NULL, 10); - child = cJSON_GetObjectItem(claim, "iat"); - GPR_ASSERT(child != NULL); - GPR_ASSERT(child->type == cJSON_Number); - issue_time.tv_sec = child->valueint; + GPR_ASSERT(iat != NULL); + GPR_ASSERT(iat->type == GRPC_JSON_NUMBER); + issue_time.tv_sec = strtol(iat->value, NULL, 10); - parsed_lifetime = gpr_time_sub(exp, issue_time); + parsed_lifetime = gpr_time_sub(expiration, issue_time); GPR_ASSERT(parsed_lifetime.tv_sec == grpc_max_auth_token_lifetime.tv_sec); } @@ -300,8 +326,9 @@ static void check_jwt_signature(const char *b64_signature, RSA *rsa_key, static void test_jwt_encode_and_sign(void) { char *json_string = test_json_key_str(NULL); - cJSON *parsed_header = NULL; - cJSON *parsed_claim = NULL; + grpc_json *parsed_header = NULL; + grpc_json *parsed_claim = NULL; + char *scratchpad; grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string(json_string); const char *b64_signature; @@ -310,17 +337,21 @@ static void test_jwt_encode_and_sign(void) { grpc_max_auth_token_lifetime); const char *dot = strchr(jwt, '.'); GPR_ASSERT(dot != NULL); - parsed_header = parse_json_part_from_jwt(jwt, dot - jwt); + parsed_header = parse_json_part_from_jwt(jwt, dot - jwt, &scratchpad); GPR_ASSERT(parsed_header != NULL); check_jwt_header(parsed_header); offset = dot - jwt + 1; + grpc_json_destroy(parsed_header); + gpr_free(scratchpad); dot = strchr(jwt + offset, '.'); GPR_ASSERT(dot != NULL); - parsed_claim = parse_json_part_from_jwt(jwt + offset, dot - (jwt + offset)); + parsed_claim = parse_json_part_from_jwt(jwt + offset, dot - (jwt + offset), &scratchpad); GPR_ASSERT(parsed_claim != NULL); check_jwt_claim(parsed_claim); offset = dot - jwt + 1; + grpc_json_destroy(parsed_claim); + gpr_free(scratchpad); dot = strchr(jwt + offset, '.'); GPR_ASSERT(dot == NULL); /* no more part. */ @@ -328,8 +359,6 @@ static void test_jwt_encode_and_sign(void) { check_jwt_signature(b64_signature, json_key.private_key, jwt, offset - 1); gpr_free(json_string); - cJSON_Delete(parsed_header); - cJSON_Delete(parsed_claim); grpc_auth_json_key_destruct(&json_key); gpr_free(jwt); } |