blob: 30e311391387ea61a815bae85269f419e654a719 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tillerb7f3f6e2016-03-25 17:13:09 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/transport/metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36#include <stdio.h>
37
Craig Tillerebdef9d2015-11-19 17:09:49 -080038#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070041#include <grpc/support/string_util.h>
Craig Tillerebdef9d2015-11-19 17:09:49 -080042
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/support/string.h"
44#include "src/core/lib/transport/chttp2/bin_encoder.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045#include "test/core/util/test_config.h"
46
Craig Tiller35696192015-05-24 15:00:37 -070047#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
49/* a large number */
Craig Tillerefad8fa2015-02-18 13:59:46 -080050#define MANY 10000
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051
Craig Tiller32946d32015-01-15 11:37:30 -080052static void test_no_op(void) {
Craig Tiller35696192015-05-24 15:00:37 -070053 LOG_TEST("test_no_op");
Craig Tillerb2b42612015-11-20 12:02:17 -080054 grpc_init();
55 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056}
57
Craig Tiller32946d32015-01-15 11:37:30 -080058static void test_create_string(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059 grpc_mdstr *s1, *s2, *s3;
60
Craig Tiller35696192015-05-24 15:00:37 -070061 LOG_TEST("test_create_string");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Craig Tillerb2b42612015-11-20 12:02:17 -080063 grpc_init();
64 s1 = grpc_mdstr_from_string("hello");
65 s2 = grpc_mdstr_from_string("hello");
66 s3 = grpc_mdstr_from_string("very much not hello");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 GPR_ASSERT(s1 == s2);
68 GPR_ASSERT(s3 != s1);
69 GPR_ASSERT(gpr_slice_str_cmp(s1->slice, "hello") == 0);
70 GPR_ASSERT(gpr_slice_str_cmp(s3->slice, "very much not hello") == 0);
Craig Tiller1a65a232015-07-06 10:22:32 -070071 GRPC_MDSTR_UNREF(s1);
72 GRPC_MDSTR_UNREF(s2);
Craig Tiller1a65a232015-07-06 10:22:32 -070073 GRPC_MDSTR_UNREF(s3);
Craig Tillerb2b42612015-11-20 12:02:17 -080074 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075}
76
Craig Tiller32946d32015-01-15 11:37:30 -080077static void test_create_metadata(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 grpc_mdelem *m1, *m2, *m3;
79
Craig Tiller35696192015-05-24 15:00:37 -070080 LOG_TEST("test_create_metadata");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081
Craig Tillerb2b42612015-11-20 12:02:17 -080082 grpc_init();
83 m1 = grpc_mdelem_from_strings("a", "b");
84 m2 = grpc_mdelem_from_strings("a", "b");
85 m3 = grpc_mdelem_from_strings("a", "c");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 GPR_ASSERT(m1 == m2);
87 GPR_ASSERT(m3 != m1);
88 GPR_ASSERT(m3->key == m1->key);
89 GPR_ASSERT(m3->value != m1->value);
90 GPR_ASSERT(gpr_slice_str_cmp(m1->key->slice, "a") == 0);
91 GPR_ASSERT(gpr_slice_str_cmp(m1->value->slice, "b") == 0);
92 GPR_ASSERT(gpr_slice_str_cmp(m3->value->slice, "c") == 0);
Craig Tiller1a65a232015-07-06 10:22:32 -070093 GRPC_MDELEM_UNREF(m1);
94 GRPC_MDELEM_UNREF(m2);
95 GRPC_MDELEM_UNREF(m3);
Craig Tillerb2b42612015-11-20 12:02:17 -080096 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097}
98
Craig Tiller32946d32015-01-15 11:37:30 -080099static void test_create_many_ephemeral_metadata(void) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800100 char buffer[GPR_LTOA_MIN_BUFSIZE];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 long i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102
Craig Tiller35696192015-05-24 15:00:37 -0700103 LOG_TEST("test_create_many_ephemeral_metadata");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104
Craig Tillerb2b42612015-11-20 12:02:17 -0800105 grpc_init();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 /* add, and immediately delete a bunch of different elements */
107 for (i = 0; i < MANY; i++) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800108 gpr_ltoa(i, buffer);
Craig Tillerb2b42612015-11-20 12:02:17 -0800109 GRPC_MDELEM_UNREF(grpc_mdelem_from_strings("a", buffer));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800111 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112}
113
Craig Tiller32946d32015-01-15 11:37:30 -0800114static void test_create_many_persistant_metadata(void) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800115 char buffer[GPR_LTOA_MIN_BUFSIZE];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116 long i;
117 grpc_mdelem **created = gpr_malloc(sizeof(grpc_mdelem *) * MANY);
118 grpc_mdelem *md;
119
Craig Tiller35696192015-05-24 15:00:37 -0700120 LOG_TEST("test_create_many_persistant_metadata");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
Craig Tillerb2b42612015-11-20 12:02:17 -0800122 grpc_init();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123 /* add phase */
124 for (i = 0; i < MANY; i++) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800125 gpr_ltoa(i, buffer);
Craig Tillerb2b42612015-11-20 12:02:17 -0800126 created[i] = grpc_mdelem_from_strings("a", buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127 }
128 /* verify phase */
129 for (i = 0; i < MANY; i++) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800130 gpr_ltoa(i, buffer);
Craig Tillerb2b42612015-11-20 12:02:17 -0800131 md = grpc_mdelem_from_strings("a", buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132 GPR_ASSERT(md == created[i]);
Craig Tiller1a65a232015-07-06 10:22:32 -0700133 GRPC_MDELEM_UNREF(md);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134 }
135 /* cleanup phase */
136 for (i = 0; i < MANY; i++) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700137 GRPC_MDELEM_UNREF(created[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138 }
Craig Tillerb2b42612015-11-20 12:02:17 -0800139 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140
141 gpr_free(created);
142}
143
Craig Tiller32946d32015-01-15 11:37:30 -0800144static void test_spin_creating_the_same_thing(void) {
Craig Tiller35696192015-05-24 15:00:37 -0700145 LOG_TEST("test_spin_creating_the_same_thing");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146
Craig Tillerb2b42612015-11-20 12:02:17 -0800147 grpc_init();
148 GRPC_MDELEM_UNREF(grpc_mdelem_from_strings("a", "b"));
149 GRPC_MDELEM_UNREF(grpc_mdelem_from_strings("a", "b"));
150 GRPC_MDELEM_UNREF(grpc_mdelem_from_strings("a", "b"));
151 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152}
153
Craig Tiller32946d32015-01-15 11:37:30 -0800154static void test_things_stick_around(void) {
Craig Tiller32ca48c2015-09-10 11:47:15 -0700155 size_t i, j;
Craig Tiller8ddf3122015-01-23 15:25:40 -0800156 char *buffer;
Craig Tiller32ca48c2015-09-10 11:47:15 -0700157 size_t nstrs = 1000;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 grpc_mdstr **strs = gpr_malloc(sizeof(grpc_mdstr *) * nstrs);
Craig Tiller32ca48c2015-09-10 11:47:15 -0700159 size_t *shuf = gpr_malloc(sizeof(size_t) * nstrs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 grpc_mdstr *test;
161
Craig Tiller35696192015-05-24 15:00:37 -0700162 LOG_TEST("test_things_stick_around");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800163
Craig Tillerb2b42612015-11-20 12:02:17 -0800164 grpc_init();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165
166 for (i = 0; i < nstrs; i++) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800167 gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", i);
Craig Tillerb2b42612015-11-20 12:02:17 -0800168 strs[i] = grpc_mdstr_from_string(buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 shuf[i] = i;
Craig Tiller8ddf3122015-01-23 15:25:40 -0800170 gpr_free(buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800171 }
172
173 for (i = 0; i < nstrs; i++) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700174 GRPC_MDSTR_REF(strs[i]);
175 GRPC_MDSTR_UNREF(strs[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176 }
177
178 for (i = 0; i < nstrs; i++) {
Craig Tiller32ca48c2015-09-10 11:47:15 -0700179 size_t p = (size_t)rand() % nstrs;
180 size_t q = (size_t)rand() % nstrs;
181 size_t temp = shuf[p];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182 shuf[p] = shuf[q];
183 shuf[q] = temp;
184 }
185
186 for (i = 0; i < nstrs; i++) {
Craig Tiller1a65a232015-07-06 10:22:32 -0700187 GRPC_MDSTR_UNREF(strs[shuf[i]]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 for (j = i + 1; j < nstrs; j++) {
Craig Tiller8ddf3122015-01-23 15:25:40 -0800189 gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", shuf[j]);
Craig Tillerb2b42612015-11-20 12:02:17 -0800190 test = grpc_mdstr_from_string(buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800191 GPR_ASSERT(test == strs[shuf[j]]);
Craig Tiller1a65a232015-07-06 10:22:32 -0700192 GRPC_MDSTR_UNREF(test);
Craig Tiller8ddf3122015-01-23 15:25:40 -0800193 gpr_free(buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 }
195 }
196
Craig Tillerb2b42612015-11-20 12:02:17 -0800197 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 gpr_free(strs);
199 gpr_free(shuf);
200}
201
Craig Tiller32946d32015-01-15 11:37:30 -0800202static void test_slices_work(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203 /* ensure no memory leaks when switching representation from mdstr to slice */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204 grpc_mdstr *str;
205 gpr_slice slice;
206
Craig Tiller35696192015-05-24 15:00:37 -0700207 LOG_TEST("test_slices_work");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208
Craig Tillerb2b42612015-11-20 12:02:17 -0800209 grpc_init();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210
211 str = grpc_mdstr_from_string(
Craig Tillerb2b42612015-11-20 12:02:17 -0800212 "123456789012345678901234567890123456789012345678901234567890");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 slice = gpr_slice_ref(str->slice);
Craig Tiller1a65a232015-07-06 10:22:32 -0700214 GRPC_MDSTR_UNREF(str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215 gpr_slice_unref(slice);
216
217 str = grpc_mdstr_from_string(
Craig Tillerb2b42612015-11-20 12:02:17 -0800218 "123456789012345678901234567890123456789012345678901234567890");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219 slice = gpr_slice_ref(str->slice);
220 gpr_slice_unref(slice);
Craig Tiller1a65a232015-07-06 10:22:32 -0700221 GRPC_MDSTR_UNREF(str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222
Craig Tillerb2b42612015-11-20 12:02:17 -0800223 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224}
225
Craig Tiller32946d32015-01-15 11:37:30 -0800226static void test_base64_and_huffman_works(void) {
ctiller430c4992014-12-11 09:15:41 -0800227 grpc_mdstr *str;
228 gpr_slice slice1;
229 gpr_slice slice2;
230
Craig Tiller35696192015-05-24 15:00:37 -0700231 LOG_TEST("test_base64_and_huffman_works");
ctiller430c4992014-12-11 09:15:41 -0800232
Craig Tillerb2b42612015-11-20 12:02:17 -0800233 grpc_init();
234 str = grpc_mdstr_from_string("abcdefg");
ctiller430c4992014-12-11 09:15:41 -0800235 slice1 = grpc_mdstr_as_base64_encoded_and_huffman_compressed(str);
236 slice2 = grpc_chttp2_base64_encode_and_huffman_compress(str->slice);
237 GPR_ASSERT(0 == gpr_slice_cmp(slice1, slice2));
238
239 gpr_slice_unref(slice2);
Craig Tiller1a65a232015-07-06 10:22:32 -0700240 GRPC_MDSTR_UNREF(str);
Craig Tillerb2b42612015-11-20 12:02:17 -0800241 grpc_shutdown();
ctiller430c4992014-12-11 09:15:41 -0800242}
243
Craig Tillerd52fbb82015-12-09 14:23:12 -0800244static void test_user_data_works(void) {
245 int *ud1;
246 int *ud2;
247 grpc_mdelem *md;
248 LOG_TEST("test_user_data_works");
249
250 grpc_init();
251 ud1 = gpr_malloc(sizeof(int));
252 *ud1 = 1;
253 ud2 = gpr_malloc(sizeof(int));
254 *ud2 = 2;
255 md = grpc_mdelem_from_strings("abc", "123");
256 grpc_mdelem_set_user_data(md, gpr_free, ud1);
257 grpc_mdelem_set_user_data(md, gpr_free, ud2);
258 GPR_ASSERT(grpc_mdelem_get_user_data(md, gpr_free) == ud1);
259 GRPC_MDELEM_UNREF(md);
260 grpc_shutdown();
261}
262
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800263int main(int argc, char **argv) {
264 grpc_test_init(argc, argv);
265 test_no_op();
266 test_create_string();
267 test_create_metadata();
268 test_create_many_ephemeral_metadata();
269 test_create_many_persistant_metadata();
270 test_spin_creating_the_same_thing();
271 test_things_stick_around();
272 test_slices_work();
ctiller430c4992014-12-11 09:15:41 -0800273 test_base64_and_huffman_works();
Craig Tillerd52fbb82015-12-09 14:23:12 -0800274 test_user_data_works();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800275 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800276}