Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: third_party/grpc/test/core/census/context_test.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, 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 // Test census_context functions, including encoding/decoding
35
36 #include <grpc/census.h>
37 #include <grpc/support/log.h>
38 #include <grpc/support/time.h>
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include "test/core/util/test_config.h"
44
45 // A set of tags Used to create a basic context for testing. Note that
46 // replace_add_delete_test() relies on specific offsets into this array - if
47 // you add or delete entries, you will also need to change the test.
48 #define BASIC_TAG_COUNT 8
49 static census_tag basic_tags[BASIC_TAG_COUNT] = {
50 /* 0 */ {"key0", "tag value", 0},
51 /* 1 */ {"k1", "a", CENSUS_TAG_PROPAGATE},
52 /* 2 */ {"k2", "a longer tag value supercalifragilisticexpialiadocious",
53 CENSUS_TAG_STATS},
54 /* 3 */ {"key_three", "", 0},
55 /* 4 */ {"a_really_really_really_really_long_key_4", "random",
56 CENSUS_TAG_PROPAGATE | CENSUS_TAG_STATS},
57 /* 5 */ {"k5", "v5", CENSUS_TAG_PROPAGATE},
58 /* 6 */ {"k6", "v6", CENSUS_TAG_STATS},
59 /* 7 */ {"k7", "v7", CENSUS_TAG_PROPAGATE | CENSUS_TAG_STATS}};
60
61 // Set of tags used to modify the basic context. Note that
62 // replace_add_delete_test() relies on specific offsets into this array - if
63 // you add or delete entries, you will also need to change the test. Other
64 // tests that rely on specific instances have XXX_XXX_OFFSET definitions (also
65 // change the defines below if you add/delete entires).
66 #define MODIFY_TAG_COUNT 10
67 static census_tag modify_tags[MODIFY_TAG_COUNT] = {
68 #define REPLACE_VALUE_OFFSET 0
69 /* 0 */ {"key0", "replace key0", 0}, // replaces tag value only
70 #define ADD_TAG_OFFSET 1
71 /* 1 */ {"new_key", "xyzzy", CENSUS_TAG_STATS}, // new tag
72 #define DELETE_TAG_OFFSET 2
73 /* 2 */ {"k5", NULL, 0}, // should delete tag
74 /* 3 */ {"k5", NULL, 0}, // try deleting already-deleted tag
75 /* 4 */ {"non-existent", NULL, 0}, // delete non-existent tag
76 #define REPLACE_FLAG_OFFSET 5
77 /* 5 */ {"k1", "a", 0}, // change flags only
78 /* 6 */ {"k7", "bar", CENSUS_TAG_STATS}, // change flags and value
79 /* 7 */ {"k2", "", CENSUS_TAG_PROPAGATE}, // more value and flags change
80 /* 8 */ {"k5", "bar", 0}, // add back tag, with different value
81 /* 9 */ {"foo", "bar", CENSUS_TAG_PROPAGATE}, // another new tag
82 };
83
84 // Utility function to compare tags. Returns true if all fields match.
85 static bool compare_tag(const census_tag *t1, const census_tag *t2) {
86 return (strcmp(t1->key, t2->key) == 0 && strcmp(t1->value, t2->value) == 0 &&
87 t1->flags == t2->flags);
88 }
89
90 // Utility function to validate a tag exists in context.
91 static bool validate_tag(const census_context *context, const census_tag *tag) {
92 census_tag tag2;
93 if (census_context_get_tag(context, tag->key, &tag2) != 1) return false;
94 return compare_tag(tag, &tag2);
95 }
96
97 // Create an empty context.
98 static void empty_test(void) {
99 struct census_context *context = census_context_create(NULL, NULL, 0, NULL);
100 GPR_ASSERT(context != NULL);
101 const census_context_status *status = census_context_get_status(context);
102 census_context_status expected = {0, 0, 0, 0, 0, 0, 0};
103 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
104 census_context_destroy(context);
105 }
106
107 // Test create and iteration over basic context.
108 static void basic_test(void) {
109 const census_context_status *status;
110 struct census_context *context =
111 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, &status);
112 census_context_status expected = {4, 4, 0, 8, 0, 0, 0};
113 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
114 census_context_iterator it;
115 census_context_initialize_iterator(context, &it);
116 census_tag tag;
117 while (census_context_next_tag(&it, &tag)) {
118 // can't rely on tag return order: make sure it matches exactly one.
119 int matches = 0;
120 for (int i = 0; i < BASIC_TAG_COUNT; i++) {
121 if (compare_tag(&tag, &basic_tags[i])) matches++;
122 }
123 GPR_ASSERT(matches == 1);
124 }
125 census_context_destroy(context);
126 }
127
128 // Test census_context_get_tag().
129 static void lookup_by_key_test(void) {
130 struct census_context *context =
131 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
132 census_tag tag;
133 for (int i = 0; i < BASIC_TAG_COUNT; i++) {
134 GPR_ASSERT(census_context_get_tag(context, basic_tags[i].key, &tag) == 1);
135 GPR_ASSERT(compare_tag(&tag, &basic_tags[i]));
136 }
137 // non-existent keys
138 GPR_ASSERT(census_context_get_tag(context, "key", &tag) == 0);
139 GPR_ASSERT(census_context_get_tag(context, "key01", &tag) == 0);
140 GPR_ASSERT(census_context_get_tag(context, "k9", &tag) == 0);
141 GPR_ASSERT(census_context_get_tag(context, "random", &tag) == 0);
142 GPR_ASSERT(census_context_get_tag(context, "", &tag) == 0);
143 census_context_destroy(context);
144 }
145
146 // Try creating context with invalid entries.
147 static void invalid_test(void) {
148 char key[300];
149 memset(key, 'k', 299);
150 key[299] = 0;
151 char value[300];
152 memset(value, 'v', 299);
153 value[299] = 0;
154 census_tag tag = {key, value, 0};
155 // long keys, short value. Key lengths (including terminator) should be
156 // <= 255 (CENSUS_MAX_TAG_KV_LEN)
157 value[3] = 0;
158 GPR_ASSERT(strlen(value) == 3);
159 GPR_ASSERT(strlen(key) == 299);
160 const census_context_status *status;
161 struct census_context *context =
162 census_context_create(NULL, &tag, 1, &status);
163 census_context_status expected = {0, 0, 0, 0, 0, 1, 0};
164 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
165 census_context_destroy(context);
166 key[CENSUS_MAX_TAG_KV_LEN] = 0;
167 GPR_ASSERT(strlen(key) == CENSUS_MAX_TAG_KV_LEN);
168 context = census_context_create(NULL, &tag, 1, &status);
169 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
170 census_context_destroy(context);
171 key[CENSUS_MAX_TAG_KV_LEN - 1] = 0;
172 GPR_ASSERT(strlen(key) == CENSUS_MAX_TAG_KV_LEN - 1);
173 context = census_context_create(NULL, &tag, 1, &status);
174 census_context_status expected2 = {0, 1, 0, 1, 0, 0, 0};
175 GPR_ASSERT(memcmp(status, &expected2, sizeof(expected2)) == 0);
176 census_context_destroy(context);
177 // now try with long values
178 value[3] = 'v';
179 GPR_ASSERT(strlen(value) == 299);
180 context = census_context_create(NULL, &tag, 1, &status);
181 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
182 census_context_destroy(context);
183 value[CENSUS_MAX_TAG_KV_LEN] = 0;
184 GPR_ASSERT(strlen(value) == CENSUS_MAX_TAG_KV_LEN);
185 context = census_context_create(NULL, &tag, 1, &status);
186 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
187 census_context_destroy(context);
188 value[CENSUS_MAX_TAG_KV_LEN - 1] = 0;
189 GPR_ASSERT(strlen(value) == CENSUS_MAX_TAG_KV_LEN - 1);
190 context = census_context_create(NULL, &tag, 1, &status);
191 GPR_ASSERT(memcmp(status, &expected2, sizeof(expected2)) == 0);
192 census_context_destroy(context);
193 // 0 length key.
194 key[0] = 0;
195 GPR_ASSERT(strlen(key) == 0);
196 context = census_context_create(NULL, &tag, 1, &status);
197 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
198 census_context_destroy(context);
199 // invalid key character
200 key[0] = 31; // 32 (' ') is the first valid character value
201 key[1] = 0;
202 GPR_ASSERT(strlen(key) == 1);
203 context = census_context_create(NULL, &tag, 1, &status);
204 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
205 census_context_destroy(context);
206 // invalid value character
207 key[0] = ' ';
208 value[5] = 127; // 127 (DEL) is ('~' + 1)
209 value[8] = 0;
210 GPR_ASSERT(strlen(key) == 1);
211 GPR_ASSERT(strlen(value) == 8);
212 context = census_context_create(NULL, &tag, 1, &status);
213 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
214 census_context_destroy(context);
215 }
216
217 // Make a copy of a context
218 static void copy_test(void) {
219 struct census_context *context =
220 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
221 const census_context_status *status;
222 struct census_context *context2 =
223 census_context_create(context, NULL, 0, &status);
224 census_context_status expected = {4, 4, 0, 0, 0, 0, 0};
225 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
226 for (int i = 0; i < BASIC_TAG_COUNT; i++) {
227 census_tag tag;
228 GPR_ASSERT(census_context_get_tag(context2, basic_tags[i].key, &tag) == 1);
229 GPR_ASSERT(compare_tag(&tag, &basic_tags[i]));
230 }
231 census_context_destroy(context);
232 census_context_destroy(context2);
233 }
234
235 // replace a single tag value
236 static void replace_value_test(void) {
237 struct census_context *context =
238 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
239 const census_context_status *status;
240 struct census_context *context2 = census_context_create(
241 context, modify_tags + REPLACE_VALUE_OFFSET, 1, &status);
242 census_context_status expected = {4, 4, 0, 0, 1, 0, 0};
243 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
244 census_tag tag;
245 GPR_ASSERT(census_context_get_tag(
246 context2, modify_tags[REPLACE_VALUE_OFFSET].key, &tag) == 1);
247 GPR_ASSERT(compare_tag(&tag, &modify_tags[REPLACE_VALUE_OFFSET]));
248 census_context_destroy(context);
249 census_context_destroy(context2);
250 }
251
252 // replace a single tags flags
253 static void replace_flags_test(void) {
254 struct census_context *context =
255 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
256 const census_context_status *status;
257 struct census_context *context2 = census_context_create(
258 context, modify_tags + REPLACE_FLAG_OFFSET, 1, &status);
259 census_context_status expected = {3, 5, 0, 0, 1, 0, 0};
260 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
261 census_tag tag;
262 GPR_ASSERT(census_context_get_tag(
263 context2, modify_tags[REPLACE_FLAG_OFFSET].key, &tag) == 1);
264 GPR_ASSERT(compare_tag(&tag, &modify_tags[REPLACE_FLAG_OFFSET]));
265 census_context_destroy(context);
266 census_context_destroy(context2);
267 }
268
269 // delete a single tag.
270 static void delete_tag_test(void) {
271 struct census_context *context =
272 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
273 const census_context_status *status;
274 struct census_context *context2 = census_context_create(
275 context, modify_tags + DELETE_TAG_OFFSET, 1, &status);
276 census_context_status expected = {3, 4, 1, 0, 0, 0, 0};
277 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
278 census_tag tag;
279 GPR_ASSERT(census_context_get_tag(
280 context2, modify_tags[DELETE_TAG_OFFSET].key, &tag) == 0);
281 census_context_destroy(context);
282 census_context_destroy(context2);
283 }
284
285 // add a single new tag.
286 static void add_tag_test(void) {
287 struct census_context *context =
288 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
289 const census_context_status *status;
290 struct census_context *context2 =
291 census_context_create(context, modify_tags + ADD_TAG_OFFSET, 1, &status);
292 census_context_status expected = {4, 5, 0, 1, 0, 0, 0};
293 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
294 census_tag tag;
295 GPR_ASSERT(census_context_get_tag(context2, modify_tags[ADD_TAG_OFFSET].key,
296 &tag) == 1);
297 GPR_ASSERT(compare_tag(&tag, &modify_tags[ADD_TAG_OFFSET]));
298 census_context_destroy(context);
299 census_context_destroy(context2);
300 }
301
302 // test many changes at once.
303 static void replace_add_delete_test(void) {
304 struct census_context *context =
305 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
306 const census_context_status *status;
307 struct census_context *context2 =
308 census_context_create(context, modify_tags, MODIFY_TAG_COUNT, &status);
309 census_context_status expected = {3, 7, 1, 3, 4, 0, 0};
310 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
311 // validate context contents. Use specific indices into the two arrays
312 // holding tag values.
313 GPR_ASSERT(validate_tag(context2, &basic_tags[3]));
314 GPR_ASSERT(validate_tag(context2, &basic_tags[4]));
315 GPR_ASSERT(validate_tag(context2, &basic_tags[6]));
316 GPR_ASSERT(validate_tag(context2, &modify_tags[0]));
317 GPR_ASSERT(validate_tag(context2, &modify_tags[1]));
318 GPR_ASSERT(validate_tag(context2, &modify_tags[5]));
319 GPR_ASSERT(validate_tag(context2, &modify_tags[6]));
320 GPR_ASSERT(validate_tag(context2, &modify_tags[7]));
321 GPR_ASSERT(validate_tag(context2, &modify_tags[8]));
322 GPR_ASSERT(validate_tag(context2, &modify_tags[9]));
323 GPR_ASSERT(!validate_tag(context2, &basic_tags[0]));
324 GPR_ASSERT(!validate_tag(context2, &basic_tags[1]));
325 GPR_ASSERT(!validate_tag(context2, &basic_tags[2]));
326 GPR_ASSERT(!validate_tag(context2, &basic_tags[5]));
327 GPR_ASSERT(!validate_tag(context2, &basic_tags[7]));
328 census_context_destroy(context);
329 census_context_destroy(context2);
330 }
331
332 #define BUF_SIZE 200
333
334 // test encode/decode.
335 static void encode_decode_test(void) {
336 char buffer[BUF_SIZE];
337 struct census_context *context =
338 census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
339 // Test with too small a buffer
340 GPR_ASSERT(census_context_encode(context, buffer, 2) == 0);
341 // Test with sufficient buffer
342 size_t buf_used = census_context_encode(context, buffer, BUF_SIZE);
343 GPR_ASSERT(buf_used != 0);
344 census_context *context2 = census_context_decode(buffer, buf_used);
345 GPR_ASSERT(context2 != NULL);
346 const census_context_status *status = census_context_get_status(context2);
347 census_context_status expected = {4, 0, 0, 0, 0, 0, 0};
348 GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
349 for (int i = 0; i < BASIC_TAG_COUNT; i++) {
350 census_tag tag;
351 if (CENSUS_TAG_IS_PROPAGATED(basic_tags[i].flags)) {
352 GPR_ASSERT(census_context_get_tag(context2, basic_tags[i].key, &tag) ==
353 1);
354 GPR_ASSERT(compare_tag(&tag, &basic_tags[i]));
355 } else {
356 GPR_ASSERT(census_context_get_tag(context2, basic_tags[i].key, &tag) ==
357 0);
358 }
359 }
360 census_context_destroy(context2);
361 census_context_destroy(context);
362 }
363
364 int main(int argc, char *argv[]) {
365 grpc_test_init(argc, argv);
366 empty_test();
367 basic_test();
368 lookup_by_key_test();
369 invalid_test();
370 copy_test();
371 replace_value_test();
372 replace_flags_test();
373 delete_tag_test();
374 add_tag_test();
375 replace_add_delete_test();
376 encode_decode_test();
377 return 0;
378 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/core/bad_ssl/servers/cert.c ('k') | third_party/grpc/test/core/census/mlog_test.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698