LCOV - code coverage report
Current view: top level - test/core/json - json_rewrite_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 137 146 93.8 %
Date: 2015-10-10 Functions: 18 19 94.7 %

          Line data    Source code
       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 <stdio.h>
      35             : #include <stdlib.h>
      36             : 
      37             : #include <grpc/support/alloc.h>
      38             : #include <grpc/support/useful.h>
      39             : #include <grpc/support/log.h>
      40             : #include "test/core/util/test_config.h"
      41             : 
      42             : #include "src/core/json/json_reader.h"
      43             : #include "src/core/json/json_writer.h"
      44             : 
      45             : typedef struct json_writer_userdata { FILE *cmp; } json_writer_userdata;
      46             : 
      47             : typedef struct stacked_container {
      48             :   grpc_json_type type;
      49             :   struct stacked_container *next;
      50             : } stacked_container;
      51             : 
      52             : typedef struct json_reader_userdata {
      53             :   FILE *in;
      54             :   grpc_json_writer *writer;
      55             :   char *scratchpad;
      56             :   char *ptr;
      57             :   size_t free_space;
      58             :   size_t allocated;
      59             :   size_t string_len;
      60             :   stacked_container *top;
      61             :   int did_eagain;
      62             : } json_reader_userdata;
      63             : 
      64       24158 : static void json_writer_output_char(void *userdata, char c) {
      65       24158 :   json_writer_userdata *state = userdata;
      66       24158 :   int cmp = fgetc(state->cmp);
      67             : 
      68             :   /* treat CRLF as LF */
      69       24158 :   if (cmp == '\r' && c == '\n') {
      70           0 :     cmp = fgetc(state->cmp);
      71             :   }
      72       24158 :   GPR_ASSERT(cmp == c);
      73       24158 : }
      74             : 
      75           0 : static void json_writer_output_string(void *userdata, const char *str) {
      76           0 :   while (*str) {
      77           0 :     json_writer_output_char(userdata, *str++);
      78             :   }
      79           0 : }
      80             : 
      81         704 : static void json_writer_output_string_with_len(void *userdata, const char *str,
      82             :                                                size_t len) {
      83             :   size_t i;
      84        5852 :   for (i = 0; i < len; i++) {
      85        5148 :     json_writer_output_char(userdata, str[i]);
      86             :   }
      87         704 : }
      88             : 
      89             : grpc_json_writer_vtable writer_vtable = {json_writer_output_char,
      90             :                                          json_writer_output_string,
      91             :                                          json_writer_output_string_with_len};
      92             : 
      93       15280 : static void check_string(json_reader_userdata *state, size_t needed) {
      94       30560 :   if (state->free_space >= needed) return;
      95           4 :   needed -= state->free_space;
      96           4 :   needed = (needed + 0xffu) & ~0xffu;
      97           4 :   state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed);
      98           4 :   state->free_space += needed;
      99           4 :   state->allocated += needed;
     100             : }
     101             : 
     102        1300 : static void json_reader_string_clear(void *userdata) {
     103        1300 :   json_reader_userdata *state = userdata;
     104        1300 :   state->free_space = state->allocated;
     105        1300 :   state->string_len = 0;
     106        1300 : }
     107             : 
     108       15280 : static void json_reader_string_add_char(void *userdata, gpr_uint32 c) {
     109       15280 :   json_reader_userdata *state = userdata;
     110       15280 :   check_string(state, 1);
     111       15280 :   GPR_ASSERT(c <= 256);
     112       15280 :   state->scratchpad[state->string_len++] = (char)c;
     113       15280 : }
     114             : 
     115          16 : static void json_reader_string_add_utf32(void *userdata, gpr_uint32 c) {
     116          16 :   if (c <= 0x7f) {
     117           0 :     json_reader_string_add_char(userdata, c);
     118          16 :   } else if (c <= 0x7ffu) {
     119          12 :     gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu);
     120          12 :     gpr_uint32 b2 = 0x80u | (c & 0x3fu);
     121          12 :     json_reader_string_add_char(userdata, b1);
     122          12 :     json_reader_string_add_char(userdata, b2);
     123           4 :   } else if (c <= 0xffffu) {
     124           2 :     gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu);
     125           2 :     gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu);
     126           2 :     gpr_uint32 b3 = 0x80u | (c & 0x3fu);
     127           2 :     json_reader_string_add_char(userdata, b1);
     128           2 :     json_reader_string_add_char(userdata, b2);
     129           2 :     json_reader_string_add_char(userdata, b3);
     130           2 :   } else if (c <= 0x1fffffu) {
     131           2 :     gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u);
     132           2 :     gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu);
     133           2 :     gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu);
     134           2 :     gpr_uint32 b4 = 0x80u | (c & 0x3fu);
     135           2 :     json_reader_string_add_char(userdata, b1);
     136           2 :     json_reader_string_add_char(userdata, b2);
     137           2 :     json_reader_string_add_char(userdata, b3);
     138           2 :     json_reader_string_add_char(userdata, b4);
     139             :   }
     140          16 : }
     141             : 
     142       49434 : static gpr_uint32 json_reader_read_char(void *userdata) {
     143             :   int r;
     144       49434 :   json_reader_userdata *state = userdata;
     145             : 
     146       49434 :   if (!state->did_eagain) {
     147       24717 :     state->did_eagain = 1;
     148       24717 :     return GRPC_JSON_READ_CHAR_EAGAIN;
     149             :   }
     150             : 
     151       24717 :   state->did_eagain = 0;
     152             : 
     153       24717 :   r = fgetc(state->in);
     154       24717 :   if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF;
     155       24717 :   return (gpr_uint32)r;
     156             : }
     157             : 
     158         244 : static void json_reader_container_begins(void *userdata, grpc_json_type type) {
     159         244 :   json_reader_userdata *state = userdata;
     160         244 :   stacked_container *container = gpr_malloc(sizeof(stacked_container));
     161             : 
     162         244 :   container->type = type;
     163         244 :   container->next = state->top;
     164         244 :   state->top = container;
     165             : 
     166         244 :   grpc_json_writer_container_begins(state->writer, type);
     167         244 : }
     168             : 
     169         244 : static grpc_json_type json_reader_container_ends(void *userdata) {
     170         244 :   json_reader_userdata *state = userdata;
     171         244 :   stacked_container *container = state->top;
     172             : 
     173         244 :   grpc_json_writer_container_ends(state->writer, container->type);
     174         244 :   state->top = container->next;
     175         244 :   gpr_free(container);
     176         244 :   return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL;
     177             : }
     178             : 
     179         712 : static void json_reader_set_key(void *userdata) {
     180         712 :   json_reader_userdata *state = userdata;
     181         712 :   json_reader_string_add_char(userdata, 0);
     182             : 
     183         712 :   grpc_json_writer_object_key(state->writer, state->scratchpad);
     184         712 : }
     185             : 
     186         484 : static void json_reader_set_string(void *userdata) {
     187         484 :   json_reader_userdata *state = userdata;
     188         484 :   json_reader_string_add_char(userdata, 0);
     189             : 
     190         484 :   grpc_json_writer_value_string(state->writer, state->scratchpad);
     191         484 : }
     192             : 
     193         100 : static int json_reader_set_number(void *userdata) {
     194         100 :   json_reader_userdata *state = userdata;
     195             : 
     196         100 :   grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad,
     197             :                                       state->string_len);
     198             : 
     199         100 :   return 1;
     200             : }
     201             : 
     202           8 : static void json_reader_set_true(void *userdata) {
     203           8 :   json_reader_userdata *state = userdata;
     204             : 
     205           8 :   grpc_json_writer_value_raw_with_len(state->writer, "true", 4);
     206           8 : }
     207             : 
     208           4 : static void json_reader_set_false(void *userdata) {
     209           4 :   json_reader_userdata *state = userdata;
     210             : 
     211           4 :   grpc_json_writer_value_raw_with_len(state->writer, "false", 5);
     212           4 : }
     213             : 
     214          16 : static void json_reader_set_null(void *userdata) {
     215          16 :   json_reader_userdata *state = userdata;
     216             : 
     217          16 :   grpc_json_writer_value_raw_with_len(state->writer, "null", 4);
     218          16 : }
     219             : 
     220             : static grpc_json_reader_vtable reader_vtable = {
     221             :     json_reader_string_clear,     json_reader_string_add_char,
     222             :     json_reader_string_add_utf32, json_reader_read_char,
     223             :     json_reader_container_begins, json_reader_container_ends,
     224             :     json_reader_set_key,          json_reader_set_string,
     225             :     json_reader_set_number,       json_reader_set_true,
     226             :     json_reader_set_false,        json_reader_set_null};
     227             : 
     228           4 : int rewrite_and_compare(FILE *in, FILE *cmp, int indent) {
     229             :   grpc_json_writer writer;
     230             :   grpc_json_reader reader;
     231             :   grpc_json_reader_status status;
     232             :   json_writer_userdata writer_user;
     233             :   json_reader_userdata reader_user;
     234             : 
     235           4 :   GPR_ASSERT(in);
     236           4 :   GPR_ASSERT(cmp);
     237             : 
     238           4 :   reader_user.writer = &writer;
     239           4 :   reader_user.in = in;
     240           4 :   reader_user.top = NULL;
     241           4 :   reader_user.scratchpad = NULL;
     242           4 :   reader_user.string_len = 0;
     243           4 :   reader_user.free_space = 0;
     244           4 :   reader_user.allocated = 0;
     245           4 :   reader_user.did_eagain = 0;
     246             : 
     247           4 :   writer_user.cmp = cmp;
     248             : 
     249           4 :   grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user);
     250           4 :   grpc_json_reader_init(&reader, &reader_vtable, &reader_user);
     251             : 
     252             :   do {
     253       24721 :     status = grpc_json_reader_run(&reader);
     254       24721 :   } while (status == GRPC_JSON_EAGAIN);
     255             : 
     256           4 :   free(reader_user.scratchpad);
     257           8 :   while (reader_user.top) {
     258           0 :     stacked_container *container = reader_user.top;
     259           0 :     reader_user.top = container->next;
     260           0 :     free(container);
     261             :   }
     262             : 
     263           4 :   return status == GRPC_JSON_DONE;
     264             : }
     265             : 
     266             : typedef struct test_file {
     267             :   const char *input;
     268             :   const char *cmp;
     269             :   int indent;
     270             : } test_file;
     271             : 
     272             : static test_file test_files[] = {
     273             :     {"test/core/json/rewrite_test_input.json",
     274             :      "test/core/json/rewrite_test_output_condensed.json", 0},
     275             :     {"test/core/json/rewrite_test_input.json",
     276             :      "test/core/json/rewrite_test_output_indented.json", 2},
     277             :     {"test/core/json/rewrite_test_output_indented.json",
     278             :      "test/core/json/rewrite_test_output_condensed.json", 0},
     279             :     {"test/core/json/rewrite_test_output_condensed.json",
     280             :      "test/core/json/rewrite_test_output_indented.json", 2},
     281             : };
     282             : 
     283           1 : void test_rewrites() {
     284             :   unsigned i;
     285             : 
     286           5 :   for (i = 0; i < GPR_ARRAY_SIZE(test_files); i++) {
     287           4 :     test_file *test = test_files + i;
     288           4 :     FILE *input = fopen(test->input, "rb");
     289           4 :     FILE *cmp = fopen(test->cmp, "rb");
     290             :     int status;
     291           4 :     gpr_log(GPR_INFO, "Testing file %s against %s using indent=%i", test->input,
     292             :             test->cmp, test->indent);
     293           4 :     status = rewrite_and_compare(input, cmp, test->indent);
     294           4 :     GPR_ASSERT(status);
     295           4 :     fclose(input);
     296           4 :     fclose(cmp);
     297             :   }
     298           1 : }
     299             : 
     300           1 : int main(int argc, char **argv) {
     301           1 :   grpc_test_init(argc, argv);
     302           1 :   test_rewrites();
     303           1 :   gpr_log(GPR_INFO, "json_rewrite_test success");
     304           1 :   return 0;
     305             : }

Generated by: LCOV version 1.10