blob: 0fafb0c8c9979fe4294a11c28222d49f1b1c8512 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080034#include "test/core/end2end/cq_verifier.h"
35
36#include <stdarg.h>
37#include <stdio.h>
38#include <string.h>
39
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/byte_buffer.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070041#include <grpc/byte_buffer_reader.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include <grpc/support/alloc.h>
43#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070044#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045#include <grpc/support/time.h>
46#include <grpc/support/useful.h>
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/support/string.h"
48#include "src/core/lib/surface/event_string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
Craig Tiller64be9f72015-05-04 14:53:51 -070050#define ROOT_EXPECTATION 1000
51
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052/* a set of metadata we expect to find on an event */
53typedef struct metadata {
54 size_t count;
55 size_t cap;
Craig Tillere005d222015-01-21 15:02:20 -080056 char **keys;
57 char **values;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058} metadata;
59
60/* details what we expect to find on a single event - and forms a linked
61 list to detail other expectations */
62typedef struct expectation {
63 struct expectation *next;
Mark D. Roth7187ab92016-08-24 13:49:22 -070064 const char *file;
65 int line;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066 grpc_completion_type type;
67 void *tag;
Craig Tiller64be9f72015-05-04 14:53:51 -070068 int success;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069} expectation;
70
71/* the verifier itself */
72struct cq_verifier {
73 /* bound completion queue */
74 grpc_completion_queue *cq;
Mark D. Roth37c1c8f2016-08-18 07:05:11 -070075 /* start of expectation list */
Mark D. Roth5f3cb4d2016-08-15 14:50:11 -070076 expectation *first_expectation;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077};
78
79cq_verifier *cq_verifier_create(grpc_completion_queue *cq) {
Craig Tiller4d16f4d2017-05-10 08:21:08 -070080 cq_verifier *v = (cq_verifier *)gpr_malloc(sizeof(cq_verifier));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 v->cq = cq;
Mark D. Rothcf501a72016-08-02 14:04:52 -070082 v->first_expectation = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 return v;
84}
85
86void cq_verifier_destroy(cq_verifier *v) {
87 cq_verify(v);
88 gpr_free(v);
89}
90
91static int has_metadata(const grpc_metadata *md, size_t count, const char *key,
92 const char *value) {
93 size_t i;
94 for (i = 0; i < count; i++) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080095 if (0 == grpc_slice_str_cmp(md[i].key, key) &&
96 0 == grpc_slice_str_cmp(md[i].value, value)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097 return 1;
98 }
99 }
100 return 0;
101}
102
Craig Tiller8ad8a412015-02-25 08:36:40 -0800103int contains_metadata(grpc_metadata_array *array, const char *key,
104 const char *value) {
Craig Tiller16c39672015-02-05 12:57:08 -0800105 return has_metadata(array->metadata, array->count, key, value);
106}
107
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800108static int has_metadata_slices(const grpc_metadata *md, size_t count,
109 grpc_slice key, grpc_slice value) {
110 size_t i;
111 for (i = 0; i < count; i++) {
112 if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) {
113 return 1;
114 }
115 }
116 return 0;
117}
118
119int contains_metadata_slices(grpc_metadata_array *array, grpc_slice key,
120 grpc_slice value) {
121 return has_metadata_slices(array->metadata, array->count, key, value);
122}
123
Craig Tillerd41a4a72016-10-26 16:16:06 -0700124static grpc_slice merge_slices(grpc_slice *slices, size_t nslices) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125 size_t i;
126 size_t len = 0;
Craig Tiller7536af02015-12-22 13:49:30 -0800127 uint8_t *cursor;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700128 grpc_slice out;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129
130 for (i = 0; i < nslices; i++) {
Craig Tiller618e67d2016-10-26 21:08:10 -0700131 len += GRPC_SLICE_LENGTH(slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132 }
133
Craig Tillerd41a4a72016-10-26 16:16:06 -0700134 out = grpc_slice_malloc(len);
Craig Tiller618e67d2016-10-26 21:08:10 -0700135 cursor = GRPC_SLICE_START_PTR(out);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136
137 for (i = 0; i < nslices; i++) {
Craig Tiller28b72422016-10-26 21:15:29 -0700138 memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
139 GRPC_SLICE_LENGTH(slices[i]));
Craig Tiller618e67d2016-10-26 21:08:10 -0700140 cursor += GRPC_SLICE_LENGTH(slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 }
142
143 return out;
144}
145
Craig Tillerd41a4a72016-10-26 16:16:06 -0700146int raw_byte_buffer_eq_slice(grpc_byte_buffer *rbb, grpc_slice b) {
147 grpc_slice a;
Craig Tillerc2599882015-05-19 15:47:19 -0700148 int ok;
149
Robbie Shaded5d851b2016-07-15 17:16:00 -0400150 if (!rbb) return 0;
Craig Tillerc2599882015-05-19 15:47:19 -0700151
Robbie Shaded5d851b2016-07-15 17:16:00 -0400152 a = merge_slices(rbb->data.raw.slice_buffer.slices,
153 rbb->data.raw.slice_buffer.count);
Craig Tiller618e67d2016-10-26 21:08:10 -0700154 ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
155 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
156 GRPC_SLICE_LENGTH(a));
Craig Tillerd41a4a72016-10-26 16:16:06 -0700157 grpc_slice_unref(a);
158 grpc_slice_unref(b);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159 return ok;
160}
161
Craig Tillerd41a4a72016-10-26 16:16:06 -0700162int byte_buffer_eq_slice(grpc_byte_buffer *bb, grpc_slice b) {
Robbie Shaded5d851b2016-07-15 17:16:00 -0400163 grpc_byte_buffer_reader reader;
164 grpc_byte_buffer *rbb;
165 int res;
166
167 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) &&
168 "Couldn't init byte buffer reader");
169 rbb = grpc_raw_byte_buffer_from_reader(&reader);
170 res = raw_byte_buffer_eq_slice(rbb, b);
171 grpc_byte_buffer_reader_destroy(&reader);
172 grpc_byte_buffer_destroy(rbb);
173
174 return res;
175}
176
Craig Tiller6a60cba2015-02-04 13:00:17 -0800177int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str) {
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700178 grpc_byte_buffer_reader reader;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700179 grpc_byte_buffer *rbb;
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700180 int res;
181
David Garcia Quintas6721d4f2016-06-30 17:17:23 -0700182 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) &&
183 "Couldn't init byte buffer reader");
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700184 rbb = grpc_raw_byte_buffer_from_reader(&reader);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700185 res = raw_byte_buffer_eq_slice(rbb, grpc_slice_from_copied_string(str));
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700186 grpc_byte_buffer_reader_destroy(&reader);
187 grpc_byte_buffer_destroy(rbb);
188
189 return res;
Craig Tiller6a60cba2015-02-04 13:00:17 -0800190}
191
Craig Tiller33857822015-01-23 15:39:40 -0800192static void expectation_to_strvec(gpr_strvec *buf, expectation *e) {
Craig Tiller33857822015-01-23 15:39:40 -0800193 char *tmp;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194
Craig Tiller9dcb58c2015-05-29 22:06:35 -0700195 gpr_asprintf(&tmp, "%p ", e->tag);
196 gpr_strvec_add(buf, tmp);
197
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 switch (e->type) {
Craig Tillerfb189f82015-02-03 12:07:07 -0800199 case GRPC_OP_COMPLETE:
Mark D. Roth13ded3f2017-05-02 10:43:48 -0700200 gpr_asprintf(&tmp, "GRPC_OP_COMPLETE success=%d %s:%d", e->success,
Mark D. Roth661408c2016-08-26 13:45:01 -0700201 e->file, e->line);
Craig Tillercce17ac2015-01-20 09:29:28 -0800202 gpr_strvec_add(buf, tmp);
Craig Tiller33857822015-01-23 15:39:40 -0800203 break;
Craig Tiller64be9f72015-05-04 14:53:51 -0700204 case GRPC_QUEUE_TIMEOUT:
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205 case GRPC_QUEUE_SHUTDOWN:
206 gpr_log(GPR_ERROR, "not implemented");
207 abort();
208 break;
209 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210}
211
Craig Tiller33857822015-01-23 15:39:40 -0800212static void expectations_to_strvec(gpr_strvec *buf, cq_verifier *v) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 expectation *e;
214
Mark D. Rothcf501a72016-08-02 14:04:52 -0700215 for (e = v->first_expectation; e != NULL; e = e->next) {
Craig Tiller33857822015-01-23 15:39:40 -0800216 expectation_to_strvec(buf, e);
217 gpr_strvec_add(buf, gpr_strdup("\n"));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219}
220
221static void fail_no_event_received(cq_verifier *v) {
Craig Tiller33857822015-01-23 15:39:40 -0800222 gpr_strvec buf;
223 char *msg;
224 gpr_strvec_init(&buf);
225 gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n"));
226 expectations_to_strvec(&buf, v);
227 msg = gpr_strvec_flatten(&buf, NULL);
228 gpr_log(GPR_ERROR, "%s", msg);
229 gpr_strvec_destroy(&buf);
230 gpr_free(msg);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231 abort();
232}
233
Mark D. Roth13ded3f2017-05-02 10:43:48 -0700234static void verify_matches(expectation *e, grpc_event *ev) {
235 GPR_ASSERT(e->type == ev->type);
236 switch (e->type) {
237 case GRPC_OP_COMPLETE:
238 if (e->success != ev->success) {
239 gpr_strvec expected;
240 gpr_strvec_init(&expected);
241 expectation_to_strvec(&expected, e);
242 char *s = gpr_strvec_flatten(&expected, NULL);
243 gpr_strvec_destroy(&expected);
244 gpr_log(GPR_ERROR, "actual success does not match expected: %s", s);
245 gpr_free(s);
246 abort();
247 }
248 break;
249 case GRPC_QUEUE_SHUTDOWN:
250 gpr_log(GPR_ERROR, "premature queue shutdown");
251 abort();
252 break;
253 case GRPC_QUEUE_TIMEOUT:
254 gpr_log(GPR_ERROR, "not implemented");
255 abort();
256 break;
257 }
258}
259
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800260void cq_verify(cq_verifier *v) {
Robbie Shadeca7effc2017-01-17 09:14:29 -0500261 const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
Mark D. Rothcf501a72016-08-02 14:04:52 -0700262 while (v->first_expectation != NULL) {
Mark D. Roth7187ab92016-08-24 13:49:22 -0700263 grpc_event ev = grpc_completion_queue_next(v->cq, deadline, NULL);
Craig Tiller64be9f72015-05-04 14:53:51 -0700264 if (ev.type == GRPC_QUEUE_TIMEOUT) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800265 fail_no_event_received(v);
Craig Tiller64be9f72015-05-04 14:53:51 -0700266 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800267 }
Mark D. Roth7187ab92016-08-24 13:49:22 -0700268 expectation *e;
Mark D. Roth5f3cb4d2016-08-15 14:50:11 -0700269 expectation *prev = NULL;
Mark D. Rothcf501a72016-08-02 14:04:52 -0700270 for (e = v->first_expectation; e != NULL; e = e->next) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700271 if (e->tag == ev.tag) {
272 verify_matches(e, &ev);
Mark D. Roth5f3cb4d2016-08-15 14:50:11 -0700273 if (e == v->first_expectation) v->first_expectation = e->next;
274 if (prev != NULL) prev->next = e->next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800275 gpr_free(e);
276 break;
277 }
Mark D. Rothcf501a72016-08-02 14:04:52 -0700278 prev = e;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279 }
Mark D. Rothcf501a72016-08-02 14:04:52 -0700280 if (e == NULL) {
Mark D. Roth7187ab92016-08-24 13:49:22 -0700281 char *s = grpc_event_string(&ev);
Mark D. Rothcf501a72016-08-02 14:04:52 -0700282 gpr_log(GPR_ERROR, "cq returned unexpected event: %s", s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800283 gpr_free(s);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700284 gpr_strvec expectations;
285 gpr_strvec_init(&expectations);
286 expectations_to_strvec(&expectations, v);
287 s = gpr_strvec_flatten(&expectations, NULL);
288 gpr_strvec_destroy(&expectations);
289 gpr_log(GPR_ERROR, "expected tags:\n%s", s);
Craig Tiller715342e2015-01-23 15:42:33 -0800290 gpr_free(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291 abort();
292 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800293 }
294}
295
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700296void cq_verify_empty_timeout(cq_verifier *v, int timeout_sec) {
297 gpr_timespec deadline =
298 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
299 gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN));
Craig Tiller64be9f72015-05-04 14:53:51 -0700300 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800301
Mark D. Rothcf501a72016-08-02 14:04:52 -0700302 GPR_ASSERT(v->first_expectation == NULL && "expectation queue must be empty");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200304 ev = grpc_completion_queue_next(v->cq, deadline, NULL);
Craig Tiller64be9f72015-05-04 14:53:51 -0700305 if (ev.type != GRPC_QUEUE_TIMEOUT) {
306 char *s = grpc_event_string(&ev);
ctiller58393c22015-01-07 14:03:30 -0800307 gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s", s);
308 gpr_free(s);
309 abort();
310 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800311}
312
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700313void cq_verify_empty(cq_verifier *v) { cq_verify_empty_timeout(v, 1); }
314
Mark D. Roth7187ab92016-08-24 13:49:22 -0700315static void add(cq_verifier *v, const char *file, int line,
316 grpc_completion_type type, void *tag, bool success) {
Craig Tiller4d16f4d2017-05-10 08:21:08 -0700317 expectation *e = (expectation *)gpr_malloc(sizeof(expectation));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800318 e->type = type;
Mark D. Roth7187ab92016-08-24 13:49:22 -0700319 e->file = file;
320 e->line = line;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321 e->tag = tag;
Mark D. Rothcf501a72016-08-02 14:04:52 -0700322 e->success = success;
Mark D. Roth37c1c8f2016-08-18 07:05:11 -0700323 e->next = v->first_expectation;
324 v->first_expectation = e;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325}
326
Mark D. Roth661408c2016-08-26 13:45:01 -0700327void cq_expect_completion(cq_verifier *v, const char *file, int line, void *tag,
328 bool success) {
Mark D. Roth7187ab92016-08-24 13:49:22 -0700329 add(v, file, line, GRPC_OP_COMPLETE, tag, success);
Craig Tiller190d3602015-02-18 09:23:38 -0800330}