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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
*
* 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 "src/core/lib/transport/metadata.h"
#include <stdio.h>
#include <string.h>
#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/support/string.h"
#include "src/core/lib/transport/static_metadata.h"
#include "test/core/util/test_config.h"
/* a large number */
#define MANY 10000
static void test_no_op(void) {
gpr_log(GPR_INFO, "test_no_op");
grpc_init();
grpc_shutdown();
}
static grpc_slice maybe_intern(grpc_slice in, bool intern) {
grpc_slice out = intern ? grpc_slice_intern(in) : grpc_slice_ref(in);
grpc_slice_unref(in);
return out;
}
static grpc_slice maybe_dup(grpc_slice in, bool dup) {
grpc_slice out = dup ? grpc_slice_dup(in) : grpc_slice_ref(in);
grpc_slice_unref(in);
return out;
}
static void test_create_metadata(bool intern_keys, bool intern_values) {
grpc_mdelem m1, m2, m3;
gpr_log(GPR_INFO, "test_create_metadata: intern_keys=%d intern_values=%d",
intern_keys, intern_values);
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
m1 = grpc_mdelem_from_slices(
&exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values));
m2 = grpc_mdelem_from_slices(
&exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values));
m3 = grpc_mdelem_from_slices(
&exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("c"), intern_values));
GPR_ASSERT(grpc_mdelem_eq(m1, m2));
GPR_ASSERT(!grpc_mdelem_eq(m3, m1));
GPR_ASSERT(grpc_slice_eq(GRPC_MDKEY(m3), GRPC_MDKEY(m1)));
GPR_ASSERT(!grpc_slice_eq(GRPC_MDVALUE(m3), GRPC_MDVALUE(m1)));
GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDKEY(m1), "a") == 0);
GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDVALUE(m1), "b") == 0);
GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDVALUE(m3), "c") == 0);
GRPC_MDELEM_UNREF(&exec_ctx, m1);
GRPC_MDELEM_UNREF(&exec_ctx, m2);
GRPC_MDELEM_UNREF(&exec_ctx, m3);
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
static void test_create_many_ephemeral_metadata(bool intern_keys,
bool intern_values) {
char buffer[GPR_LTOA_MIN_BUFSIZE];
long i;
gpr_log(
GPR_INFO,
"test_create_many_ephemeral_metadata: intern_keys=%d intern_values=%d",
intern_keys, intern_values);
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
/* add, and immediately delete a bunch of different elements */
for (i = 0; i < MANY; i++) {
gpr_ltoa(i, buffer);
GRPC_MDELEM_UNREF(
&exec_ctx,
grpc_mdelem_from_slices(
&exec_ctx,
maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_copied_string(buffer),
intern_values)));
}
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
static void test_create_many_persistant_metadata(void) {
char buffer[GPR_LTOA_MIN_BUFSIZE];
long i;
grpc_mdelem* created =
static_cast<grpc_mdelem*>(gpr_malloc(sizeof(grpc_mdelem) * MANY));
grpc_mdelem md;
gpr_log(GPR_INFO, "test_create_many_persistant_metadata");
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
/* add phase */
for (i = 0; i < MANY; i++) {
gpr_ltoa(i, buffer);
created[i] = grpc_mdelem_from_slices(
&exec_ctx, grpc_slice_intern(grpc_slice_from_static_string("a")),
grpc_slice_intern(grpc_slice_from_static_string(buffer)));
}
/* verify phase */
for (i = 0; i < MANY; i++) {
gpr_ltoa(i, buffer);
md = grpc_mdelem_from_slices(
&exec_ctx, grpc_slice_intern(grpc_slice_from_static_string("a")),
grpc_slice_intern(grpc_slice_from_static_string(buffer)));
GPR_ASSERT(grpc_mdelem_eq(md, created[i]));
GRPC_MDELEM_UNREF(&exec_ctx, md);
}
/* cleanup phase */
for (i = 0; i < MANY; i++) {
GRPC_MDELEM_UNREF(&exec_ctx, created[i]);
}
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
gpr_free(created);
}
static void test_spin_creating_the_same_thing(bool intern_keys,
bool intern_values) {
gpr_log(GPR_INFO,
"test_spin_creating_the_same_thing: intern_keys=%d intern_values=%d",
intern_keys, intern_values);
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_mdelem a, b, c;
GRPC_MDELEM_UNREF(
&exec_ctx,
a = grpc_mdelem_from_slices(
&exec_ctx,
maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values)));
GRPC_MDELEM_UNREF(
&exec_ctx,
b = grpc_mdelem_from_slices(
&exec_ctx,
maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values)));
GRPC_MDELEM_UNREF(
&exec_ctx,
c = grpc_mdelem_from_slices(
&exec_ctx,
maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values)));
if (intern_keys && intern_values) {
GPR_ASSERT(a.payload == b.payload);
GPR_ASSERT(a.payload == c.payload);
}
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
static void test_identity_laws(bool intern_keys, bool intern_values) {
gpr_log(GPR_INFO, "test_identity_laws: intern_keys=%d intern_values=%d",
intern_keys, intern_values);
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_mdelem a, b, c;
a = grpc_mdelem_from_slices(
&exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values));
b = grpc_mdelem_from_slices(
&exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values));
c = grpc_mdelem_from_slices(
&exec_ctx, maybe_intern(grpc_slice_from_static_string("a"), intern_keys),
maybe_intern(grpc_slice_from_static_string("b"), intern_values));
GPR_ASSERT(grpc_mdelem_eq(a, a));
GPR_ASSERT(grpc_mdelem_eq(b, b));
GPR_ASSERT(grpc_mdelem_eq(c, c));
GPR_ASSERT(grpc_mdelem_eq(a, b));
GPR_ASSERT(grpc_mdelem_eq(b, c));
GPR_ASSERT(grpc_mdelem_eq(a, c));
GPR_ASSERT(grpc_mdelem_eq(b, a));
GPR_ASSERT(grpc_mdelem_eq(c, b));
GPR_ASSERT(grpc_mdelem_eq(c, a));
if (intern_keys && intern_values) {
GPR_ASSERT(a.payload == b.payload);
GPR_ASSERT(a.payload == c.payload);
} else {
GPR_ASSERT(a.payload != b.payload);
GPR_ASSERT(a.payload != c.payload);
GPR_ASSERT(b.payload != c.payload);
}
GRPC_MDELEM_UNREF(&exec_ctx, a);
GRPC_MDELEM_UNREF(&exec_ctx, b);
GRPC_MDELEM_UNREF(&exec_ctx, c);
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
static void test_things_stick_around(void) {
size_t i, j;
char* buffer;
size_t nstrs = 1000;
grpc_slice* strs =
static_cast<grpc_slice*>(gpr_malloc(sizeof(grpc_slice) * nstrs));
size_t* shuf = static_cast<size_t*>(gpr_malloc(sizeof(size_t) * nstrs));
grpc_slice test;
gpr_log(GPR_INFO, "test_things_stick_around");
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
for (i = 0; i < nstrs; i++) {
gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%" PRIuPTR "x", i);
strs[i] = grpc_slice_intern(grpc_slice_from_static_string(buffer));
shuf[i] = i;
gpr_free(buffer);
}
for (i = 0; i < nstrs; i++) {
grpc_slice_ref_internal(strs[i]);
grpc_slice_unref_internal(&exec_ctx, strs[i]);
}
for (i = 0; i < nstrs; i++) {
size_t p = (size_t)rand() % nstrs;
size_t q = (size_t)rand() % nstrs;
size_t temp = shuf[p];
shuf[p] = shuf[q];
shuf[q] = temp;
}
for (i = 0; i < nstrs; i++) {
grpc_slice_unref_internal(&exec_ctx, strs[shuf[i]]);
for (j = i + 1; j < nstrs; j++) {
gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%" PRIuPTR "x",
shuf[j]);
test = grpc_slice_intern(grpc_slice_from_static_string(buffer));
GPR_ASSERT(grpc_slice_is_equivalent(test, strs[shuf[j]]));
grpc_slice_unref_internal(&exec_ctx, test);
gpr_free(buffer);
}
}
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
gpr_free(strs);
gpr_free(shuf);
}
static void test_user_data_works(void) {
int* ud1;
int* ud2;
grpc_mdelem md;
gpr_log(GPR_INFO, "test_user_data_works");
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
ud1 = static_cast<int*>(gpr_malloc(sizeof(int)));
*ud1 = 1;
ud2 = static_cast<int*>(gpr_malloc(sizeof(int)));
*ud2 = 2;
md = grpc_mdelem_from_slices(
&exec_ctx, grpc_slice_intern(grpc_slice_from_static_string("abc")),
grpc_slice_intern(grpc_slice_from_static_string("123")));
grpc_mdelem_set_user_data(md, gpr_free, ud1);
grpc_mdelem_set_user_data(md, gpr_free, ud2);
GPR_ASSERT(grpc_mdelem_get_user_data(md, gpr_free) == ud1);
GRPC_MDELEM_UNREF(&exec_ctx, md);
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
static void verify_ascii_header_size(grpc_exec_ctx* exec_ctx, const char* key,
const char* value, bool intern_key,
bool intern_value) {
grpc_mdelem elem = grpc_mdelem_from_slices(
exec_ctx, maybe_intern(grpc_slice_from_static_string(key), intern_key),
maybe_intern(grpc_slice_from_static_string(value), intern_value));
size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, false);
size_t expected_size = 32 + strlen(key) + strlen(value);
GPR_ASSERT(expected_size == elem_size);
GRPC_MDELEM_UNREF(exec_ctx, elem);
}
static void verify_binary_header_size(grpc_exec_ctx* exec_ctx, const char* key,
const uint8_t* value, size_t value_len,
bool intern_key, bool intern_value) {
grpc_mdelem elem = grpc_mdelem_from_slices(
exec_ctx, maybe_intern(grpc_slice_from_static_string(key), intern_key),
maybe_intern(grpc_slice_from_static_buffer(value, value_len),
intern_value));
GPR_ASSERT(grpc_is_binary_header(GRPC_MDKEY(elem)));
size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, false);
grpc_slice value_slice =
grpc_slice_from_copied_buffer((const char*)value, value_len);
grpc_slice base64_encoded = grpc_chttp2_base64_encode(value_slice);
size_t expected_size = 32 + strlen(key) + GRPC_SLICE_LENGTH(base64_encoded);
GPR_ASSERT(expected_size == elem_size);
grpc_slice_unref_internal(exec_ctx, value_slice);
grpc_slice_unref_internal(exec_ctx, base64_encoded);
GRPC_MDELEM_UNREF(exec_ctx, elem);
}
#define BUFFER_SIZE 64
static void test_mdelem_sizes_in_hpack(bool intern_key, bool intern_value) {
gpr_log(GPR_INFO, "test_mdelem_size: intern_key=%d intern_value=%d",
intern_key, intern_value);
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
uint8_t binary_value[BUFFER_SIZE] = {0};
for (uint8_t i = 0; i < BUFFER_SIZE; i++) {
binary_value[i] = i;
}
verify_ascii_header_size(&exec_ctx, "hello", "world", intern_key,
intern_value);
verify_ascii_header_size(&exec_ctx, "hello",
"worldxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", intern_key,
intern_value);
verify_ascii_header_size(&exec_ctx, ":scheme", "http", intern_key,
intern_value);
for (uint8_t i = 0; i < BUFFER_SIZE; i++) {
verify_binary_header_size(&exec_ctx, "hello-bin", binary_value, i,
intern_key, intern_value);
}
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
static void test_copied_static_metadata(bool dup_key, bool dup_value) {
gpr_log(GPR_INFO, "test_static_metadata: dup_key=%d dup_value=%d", dup_key,
dup_value);
grpc_init();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
for (size_t i = 0; i < GRPC_STATIC_MDELEM_COUNT; i++) {
grpc_mdelem p = GRPC_MAKE_MDELEM(&grpc_static_mdelem_table[i],
GRPC_MDELEM_STORAGE_STATIC);
grpc_mdelem q =
grpc_mdelem_from_slices(&exec_ctx, maybe_dup(GRPC_MDKEY(p), dup_key),
maybe_dup(GRPC_MDVALUE(p), dup_value));
GPR_ASSERT(grpc_mdelem_eq(p, q));
if (dup_key || dup_value) {
GPR_ASSERT(p.payload != q.payload);
} else {
GPR_ASSERT(p.payload == q.payload);
}
GRPC_MDELEM_UNREF(&exec_ctx, p);
GRPC_MDELEM_UNREF(&exec_ctx, q);
}
grpc_exec_ctx_finish(&exec_ctx);
grpc_shutdown();
}
int main(int argc, char** argv) {
grpc_test_init(argc, argv);
test_no_op();
for (int k = 0; k <= 1; k++) {
for (int v = 0; v <= 1; v++) {
test_create_metadata(k, v);
test_create_many_ephemeral_metadata(k, v);
test_identity_laws(k, v);
test_spin_creating_the_same_thing(k, v);
test_mdelem_sizes_in_hpack(k, v);
test_copied_static_metadata(k, v);
}
}
test_create_many_persistant_metadata();
test_things_stick_around();
test_user_data_works();
return 0;
}
|