• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "test/core/end2end/cq_verifier.h"
20 
21 #include <inttypes.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <grpc/byte_buffer.h>
27 #include <grpc/byte_buffer_reader.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31 #include <grpc/support/time.h>
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/surface/event_string.h"
34 
35 #define ROOT_EXPECTATION 1000
36 
37 /* a set of metadata we expect to find on an event */
38 typedef struct metadata {
39   size_t count;
40   size_t cap;
41   char** keys;
42   char** values;
43 } metadata;
44 
45 /* details what we expect to find on a single event - and forms a linked
46    list to detail other expectations */
47 typedef struct expectation {
48   struct expectation* next;
49   const char* file;
50   int line;
51   grpc_completion_type type;
52   void* tag;
53   int success;
54 } expectation;
55 
56 /* the verifier itself */
57 struct cq_verifier {
58   /* bound completion queue */
59   grpc_completion_queue* cq;
60   /* start of expectation list */
61   expectation* first_expectation;
62 };
63 
cq_verifier_create(grpc_completion_queue * cq)64 cq_verifier* cq_verifier_create(grpc_completion_queue* cq) {
65   cq_verifier* v = static_cast<cq_verifier*>(gpr_malloc(sizeof(cq_verifier)));
66   v->cq = cq;
67   v->first_expectation = nullptr;
68   return v;
69 }
70 
cq_verifier_destroy(cq_verifier * v)71 void cq_verifier_destroy(cq_verifier* v) {
72   cq_verify(v);
73   gpr_free(v);
74 }
75 
has_metadata(const grpc_metadata * md,size_t count,const char * key,const char * value)76 static int has_metadata(const grpc_metadata* md, size_t count, const char* key,
77                         const char* value) {
78   size_t i;
79   for (i = 0; i < count; i++) {
80     if (0 == grpc_slice_str_cmp(md[i].key, key) &&
81         0 == grpc_slice_str_cmp(md[i].value, value)) {
82       return 1;
83     }
84   }
85   return 0;
86 }
87 
contains_metadata(grpc_metadata_array * array,const char * key,const char * value)88 int contains_metadata(grpc_metadata_array* array, const char* key,
89                       const char* value) {
90   return has_metadata(array->metadata, array->count, key, value);
91 }
92 
has_metadata_slices(const grpc_metadata * md,size_t count,grpc_slice key,grpc_slice value)93 static int has_metadata_slices(const grpc_metadata* md, size_t count,
94                                grpc_slice key, grpc_slice value) {
95   size_t i;
96   for (i = 0; i < count; i++) {
97     if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) {
98       return 1;
99     }
100   }
101   return 0;
102 }
103 
contains_metadata_slices(grpc_metadata_array * array,grpc_slice key,grpc_slice value)104 int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key,
105                              grpc_slice value) {
106   return has_metadata_slices(array->metadata, array->count, key, value);
107 }
108 
merge_slices(grpc_slice * slices,size_t nslices)109 static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
110   size_t i;
111   size_t len = 0;
112   uint8_t* cursor;
113   grpc_slice out;
114 
115   for (i = 0; i < nslices; i++) {
116     len += GRPC_SLICE_LENGTH(slices[i]);
117   }
118 
119   out = grpc_slice_malloc(len);
120   cursor = GRPC_SLICE_START_PTR(out);
121 
122   for (i = 0; i < nslices; i++) {
123     memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
124            GRPC_SLICE_LENGTH(slices[i]));
125     cursor += GRPC_SLICE_LENGTH(slices[i]);
126   }
127 
128   return out;
129 }
130 
raw_byte_buffer_eq_slice(grpc_byte_buffer * rbb,grpc_slice b)131 int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) {
132   grpc_slice a;
133   int ok;
134 
135   if (!rbb) return 0;
136 
137   a = merge_slices(rbb->data.raw.slice_buffer.slices,
138                    rbb->data.raw.slice_buffer.count);
139   ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
140        0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
141                    GRPC_SLICE_LENGTH(a));
142   grpc_slice_unref(a);
143   grpc_slice_unref(b);
144   return ok;
145 }
146 
byte_buffer_eq_slice(grpc_byte_buffer * bb,grpc_slice b)147 int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) {
148   grpc_byte_buffer_reader reader;
149   grpc_byte_buffer* rbb;
150   int res;
151 
152   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) &&
153              "Couldn't init byte buffer reader");
154   rbb = grpc_raw_byte_buffer_from_reader(&reader);
155   res = raw_byte_buffer_eq_slice(rbb, b);
156   grpc_byte_buffer_reader_destroy(&reader);
157   grpc_byte_buffer_destroy(rbb);
158 
159   return res;
160 }
161 
byte_buffer_eq_string(grpc_byte_buffer * bb,const char * str)162 int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) {
163   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, grpc_slice_from_copied_string(str));
171   grpc_byte_buffer_reader_destroy(&reader);
172   grpc_byte_buffer_destroy(rbb);
173 
174   return res;
175 }
176 
is_probably_integer(void * p)177 static bool is_probably_integer(void* p) { return ((uintptr_t)p) < 1000000; }
178 
expectation_to_strvec(gpr_strvec * buf,expectation * e)179 static void expectation_to_strvec(gpr_strvec* buf, expectation* e) {
180   char* tmp;
181 
182   if (is_probably_integer(e->tag)) {
183     gpr_asprintf(&tmp, "tag(%" PRIdPTR ") ", (intptr_t)e->tag);
184   } else {
185     gpr_asprintf(&tmp, "%p ", e->tag);
186   }
187   gpr_strvec_add(buf, tmp);
188 
189   switch (e->type) {
190     case GRPC_OP_COMPLETE:
191       gpr_asprintf(&tmp, "GRPC_OP_COMPLETE success=%d %s:%d", e->success,
192                    e->file, e->line);
193       gpr_strvec_add(buf, tmp);
194       break;
195     case GRPC_QUEUE_TIMEOUT:
196     case GRPC_QUEUE_SHUTDOWN:
197       gpr_log(GPR_ERROR, "not implemented");
198       abort();
199       break;
200   }
201 }
202 
expectations_to_strvec(gpr_strvec * buf,cq_verifier * v)203 static void expectations_to_strvec(gpr_strvec* buf, cq_verifier* v) {
204   expectation* e;
205 
206   for (e = v->first_expectation; e != nullptr; e = e->next) {
207     expectation_to_strvec(buf, e);
208     gpr_strvec_add(buf, gpr_strdup("\n"));
209   }
210 }
211 
fail_no_event_received(cq_verifier * v)212 static void fail_no_event_received(cq_verifier* v) {
213   gpr_strvec buf;
214   char* msg;
215   gpr_strvec_init(&buf);
216   gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n"));
217   expectations_to_strvec(&buf, v);
218   msg = gpr_strvec_flatten(&buf, nullptr);
219   gpr_log(GPR_ERROR, "%s", msg);
220   gpr_strvec_destroy(&buf);
221   gpr_free(msg);
222   abort();
223 }
224 
verify_matches(expectation * e,grpc_event * ev)225 static void verify_matches(expectation* e, grpc_event* ev) {
226   GPR_ASSERT(e->type == ev->type);
227   switch (e->type) {
228     case GRPC_OP_COMPLETE:
229       if (e->success != ev->success) {
230         gpr_strvec expected;
231         gpr_strvec_init(&expected);
232         expectation_to_strvec(&expected, e);
233         char* s = gpr_strvec_flatten(&expected, nullptr);
234         gpr_strvec_destroy(&expected);
235         gpr_log(GPR_ERROR, "actual success does not match expected: %s", s);
236         gpr_free(s);
237         abort();
238       }
239       break;
240     case GRPC_QUEUE_SHUTDOWN:
241       gpr_log(GPR_ERROR, "premature queue shutdown");
242       abort();
243       break;
244     case GRPC_QUEUE_TIMEOUT:
245       gpr_log(GPR_ERROR, "not implemented");
246       abort();
247       break;
248   }
249 }
250 
cq_verify(cq_verifier * v)251 void cq_verify(cq_verifier* v) {
252   const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
253   while (v->first_expectation != nullptr) {
254     grpc_event ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
255     if (ev.type == GRPC_QUEUE_TIMEOUT) {
256       fail_no_event_received(v);
257       break;
258     }
259     expectation* e;
260     expectation* prev = nullptr;
261     for (e = v->first_expectation; e != nullptr; e = e->next) {
262       if (e->tag == ev.tag) {
263         verify_matches(e, &ev);
264         if (e == v->first_expectation) v->first_expectation = e->next;
265         if (prev != nullptr) prev->next = e->next;
266         gpr_free(e);
267         break;
268       }
269       prev = e;
270     }
271     if (e == nullptr) {
272       char* s = grpc_event_string(&ev);
273       gpr_log(GPR_ERROR, "cq returned unexpected event: %s", s);
274       gpr_free(s);
275       gpr_strvec expectations;
276       gpr_strvec_init(&expectations);
277       expectations_to_strvec(&expectations, v);
278       s = gpr_strvec_flatten(&expectations, nullptr);
279       gpr_strvec_destroy(&expectations);
280       gpr_log(GPR_ERROR, "expected tags:\n%s", s);
281       gpr_free(s);
282       abort();
283     }
284   }
285 }
286 
cq_verify_empty_timeout(cq_verifier * v,int timeout_sec)287 void cq_verify_empty_timeout(cq_verifier* v, int timeout_sec) {
288   gpr_timespec deadline =
289       gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
290                    gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN));
291   grpc_event ev;
292 
293   GPR_ASSERT(v->first_expectation == nullptr &&
294              "expectation queue must be empty");
295 
296   ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
297   if (ev.type != GRPC_QUEUE_TIMEOUT) {
298     char* s = grpc_event_string(&ev);
299     gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s", s);
300     gpr_free(s);
301     abort();
302   }
303 }
304 
cq_verify_empty(cq_verifier * v)305 void cq_verify_empty(cq_verifier* v) { cq_verify_empty_timeout(v, 1); }
306 
add(cq_verifier * v,const char * file,int line,grpc_completion_type type,void * tag,bool success)307 static void add(cq_verifier* v, const char* file, int line,
308                 grpc_completion_type type, void* tag, bool success) {
309   expectation* e = static_cast<expectation*>(gpr_malloc(sizeof(expectation)));
310   e->type = type;
311   e->file = file;
312   e->line = line;
313   e->tag = tag;
314   e->success = success;
315   e->next = v->first_expectation;
316   v->first_expectation = e;
317 }
318 
cq_expect_completion(cq_verifier * v,const char * file,int line,void * tag,bool success)319 void cq_expect_completion(cq_verifier* v, const char* file, int line, void* tag,
320                           bool success) {
321   add(v, file, line, GRPC_OP_COMPLETE, tag, success);
322 }
323