aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/json/json_rewrite_test.cc
blob: 2fade12e13f9c3aa6dbaa9f632bf37f6683807fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed 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 <stdio.h>
#include <stdlib.h>

#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include "test/core/util/test_config.h"

#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/json/json_reader.h"
#include "src/core/lib/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 = static_cast<json_writer_userdata*>(userdata);
  int cmp = fgetc(state->cmp);

  /* treat CRLF as LF */
  if (cmp == '\r' && c == '\n') {
    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 + 0xffu) & ~0xffu;
  state->scratchpad = static_cast<char*>(
      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 = static_cast<json_reader_userdata*>(userdata);
  state->free_space = state->allocated;
  state->string_len = 0;
}

static void json_reader_string_add_char(void* userdata, uint32_t c) {
  json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
  check_string(state, 1);
  GPR_ASSERT(c <= 256);
  state->scratchpad[state->string_len++] = static_cast<char>(c);
}

static void json_reader_string_add_utf32(void* userdata, uint32_t c) {
  if (c <= 0x7f) {
    json_reader_string_add_char(userdata, c);
  } else if (c <= 0x7ffu) {
    uint32_t b1 = 0xc0u | ((c >> 6u) & 0x1fu);
    uint32_t b2 = 0x80u | (c & 0x3fu);
    json_reader_string_add_char(userdata, b1);
    json_reader_string_add_char(userdata, b2);
  } else if (c <= 0xffffu) {
    uint32_t b1 = 0xe0u | ((c >> 12u) & 0x0fu);
    uint32_t b2 = 0x80u | ((c >> 6u) & 0x3fu);
    uint32_t b3 = 0x80u | (c & 0x3fu);
    json_reader_string_add_char(userdata, b1);
    json_reader_string_add_char(userdata, b2);
    json_reader_string_add_char(userdata, b3);
  } else if (c <= 0x1fffffu) {
    uint32_t b1 = 0xf0u | ((c >> 18u) & 0x07u);
    uint32_t b2 = 0x80u | ((c >> 12u) & 0x3fu);
    uint32_t b3 = 0x80u | ((c >> 6u) & 0x3fu);
    uint32_t b4 = 0x80u | (c & 0x3fu);
    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 uint32_t json_reader_read_char(void* userdata) {
  int r;
  json_reader_userdata* state = static_cast<json_reader_userdata*>(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 static_cast<uint32_t>(r);
}

static void json_reader_container_begins(void* userdata, grpc_json_type type) {
  json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
  stacked_container* container =
      static_cast<stacked_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 = static_cast<json_reader_userdata*>(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 = static_cast<json_reader_userdata*>(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 = static_cast<json_reader_userdata*>(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 = static_cast<json_reader_userdata*>(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 = static_cast<json_reader_userdata*>(userdata);

  grpc_json_writer_value_raw_with_len(state->writer, "true", 4);
}

static void json_reader_set_false(void* userdata) {
  json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);

  grpc_json_writer_value_raw_with_len(state->writer, "false", 5);
}

static void json_reader_set_null(void* userdata) {
  json_reader_userdata* state = static_cast<json_reader_userdata*>(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 = nullptr;
  reader_user.scratchpad = nullptr;
  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() {
  unsigned 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;
}