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 <string>
27 #include <vector>
28
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_join.h"
31
32 #include <grpc/byte_buffer.h>
33 #include <grpc/byte_buffer_reader.h>
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 #include <grpc/support/string_util.h>
37 #include <grpc/support/time.h>
38 #include "src/core/lib/compression/compression_internal.h"
39 #include "src/core/lib/compression/message_compress.h"
40 #include "src/core/lib/gpr/string.h"
41 #include "src/core/lib/surface/event_string.h"
42
43 #define ROOT_EXPECTATION 1000
44
45 /* a set of metadata we expect to find on an event */
46 typedef struct metadata {
47 size_t count;
48 size_t cap;
49 char** keys;
50 char** values;
51 } metadata;
52
53 /* details what we expect to find on a single event - and forms a linked
54 list to detail other expectations */
55 typedef struct expectation {
56 struct expectation* next;
57 const char* file;
58 int line;
59 grpc_completion_type type;
60 void* tag;
61 bool check_success;
62 int success;
63 } expectation;
64
65 /* the verifier itself */
66 struct cq_verifier {
67 /* bound completion queue */
68 grpc_completion_queue* cq;
69 /* start of expectation list */
70 expectation* first_expectation;
71 };
72
cq_verifier_create(grpc_completion_queue * cq)73 cq_verifier* cq_verifier_create(grpc_completion_queue* cq) {
74 cq_verifier* v = static_cast<cq_verifier*>(gpr_malloc(sizeof(cq_verifier)));
75 v->cq = cq;
76 v->first_expectation = nullptr;
77 return v;
78 }
79
cq_verifier_destroy(cq_verifier * v)80 void cq_verifier_destroy(cq_verifier* v) {
81 cq_verify(v);
82 gpr_free(v);
83 }
84
has_metadata(const grpc_metadata * md,size_t count,const char * key,const char * value)85 static int has_metadata(const grpc_metadata* md, size_t count, const char* key,
86 const char* value) {
87 size_t i;
88 for (i = 0; i < count; i++) {
89 if (0 == grpc_slice_str_cmp(md[i].key, key) &&
90 0 == grpc_slice_str_cmp(md[i].value, value)) {
91 return 1;
92 }
93 }
94 return 0;
95 }
96
contains_metadata(grpc_metadata_array * array,const char * key,const char * value)97 int contains_metadata(grpc_metadata_array* array, const char* key,
98 const char* value) {
99 return has_metadata(array->metadata, array->count, key, value);
100 }
101
has_metadata_slices(const grpc_metadata * md,size_t count,grpc_slice key,grpc_slice value)102 static int has_metadata_slices(const grpc_metadata* md, size_t count,
103 grpc_slice key, grpc_slice value) {
104 size_t i;
105 for (i = 0; i < count; i++) {
106 if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) {
107 return 1;
108 }
109 }
110 return 0;
111 }
112
contains_metadata_slices(grpc_metadata_array * array,grpc_slice key,grpc_slice value)113 int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key,
114 grpc_slice value) {
115 return has_metadata_slices(array->metadata, array->count, key, value);
116 }
117
merge_slices(grpc_slice * slices,size_t nslices)118 static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
119 size_t i;
120 size_t len = 0;
121 uint8_t* cursor;
122 grpc_slice out;
123
124 for (i = 0; i < nslices; i++) {
125 len += GRPC_SLICE_LENGTH(slices[i]);
126 }
127
128 out = grpc_slice_malloc(len);
129 cursor = GRPC_SLICE_START_PTR(out);
130
131 for (i = 0; i < nslices; i++) {
132 memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
133 GRPC_SLICE_LENGTH(slices[i]));
134 cursor += GRPC_SLICE_LENGTH(slices[i]);
135 }
136
137 return out;
138 }
139
raw_byte_buffer_eq_slice(grpc_byte_buffer * rbb,grpc_slice b)140 int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) {
141 grpc_slice a;
142 int ok;
143
144 if (!rbb) return 0;
145
146 a = merge_slices(rbb->data.raw.slice_buffer.slices,
147 rbb->data.raw.slice_buffer.count);
148 ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
149 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
150 GRPC_SLICE_LENGTH(a));
151 grpc_slice_unref(a);
152 grpc_slice_unref(b);
153 return ok;
154 }
155
byte_buffer_eq_slice(grpc_byte_buffer * bb,grpc_slice b)156 int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) {
157 if (bb->data.raw.compression > GRPC_COMPRESS_NONE) {
158 grpc_slice_buffer decompressed_buffer;
159 grpc_slice_buffer_init(&decompressed_buffer);
160 GPR_ASSERT(grpc_msg_decompress(
161 grpc_compression_algorithm_to_message_compression_algorithm(
162 bb->data.raw.compression),
163 &bb->data.raw.slice_buffer, &decompressed_buffer));
164 grpc_byte_buffer* rbb = grpc_raw_byte_buffer_create(
165 decompressed_buffer.slices, decompressed_buffer.count);
166 int ret_val = raw_byte_buffer_eq_slice(rbb, b);
167 grpc_byte_buffer_destroy(rbb);
168 grpc_slice_buffer_destroy(&decompressed_buffer);
169 return ret_val;
170 }
171 return raw_byte_buffer_eq_slice(bb, b);
172 }
173
byte_buffer_eq_string(grpc_byte_buffer * bb,const char * str)174 int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) {
175 return byte_buffer_eq_slice(bb, grpc_slice_from_copied_string(str));
176 }
177
is_probably_integer(void * p)178 static bool is_probably_integer(void* p) { return ((uintptr_t)p) < 1000000; }
179
180 namespace {
181
ExpectationString(const expectation & e)182 std::string ExpectationString(const expectation& e) {
183 std::string out;
184 if (is_probably_integer(e.tag)) {
185 out = absl::StrFormat("tag(%" PRIdPTR ") ", (intptr_t)e.tag);
186 } else {
187 out = absl::StrFormat("%p ", e.tag);
188 }
189 switch (e.type) {
190 case GRPC_OP_COMPLETE:
191 absl::StrAppendFormat(&out, "GRPC_OP_COMPLETE success=%d %s:%d",
192 e.success, e.file, e.line);
193 break;
194 case GRPC_QUEUE_TIMEOUT:
195 case GRPC_QUEUE_SHUTDOWN:
196 gpr_log(GPR_ERROR, "not implemented");
197 abort();
198 break;
199 }
200 return out;
201 }
202
ExpectationsString(const cq_verifier & v)203 std::string ExpectationsString(const cq_verifier& v) {
204 std::vector<std::string> expectations;
205 for (expectation* e = v.first_expectation; e != nullptr; e = e->next) {
206 expectations.push_back(ExpectationString(*e));
207 }
208 return absl::StrJoin(expectations, "\n");
209 }
210
211 } // namespace
212
fail_no_event_received(cq_verifier * v)213 static void fail_no_event_received(cq_verifier* v) {
214 gpr_log(GPR_ERROR, "no event received, but expected:%s",
215 ExpectationsString(*v).c_str());
216 abort();
217 }
218
verify_matches(expectation * e,grpc_event * ev)219 static void verify_matches(expectation* e, grpc_event* ev) {
220 GPR_ASSERT(e->type == ev->type);
221 switch (e->type) {
222 case GRPC_OP_COMPLETE:
223 if (e->check_success && e->success != ev->success) {
224 gpr_log(GPR_ERROR, "actual success does not match expected: %s",
225 ExpectationString(*e).c_str());
226 abort();
227 }
228 break;
229 case GRPC_QUEUE_SHUTDOWN:
230 gpr_log(GPR_ERROR, "premature queue shutdown");
231 abort();
232 break;
233 case GRPC_QUEUE_TIMEOUT:
234 gpr_log(GPR_ERROR, "not implemented");
235 abort();
236 break;
237 }
238 }
239
cq_verify(cq_verifier * v)240 void cq_verify(cq_verifier* v) {
241 const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
242 while (v->first_expectation != nullptr) {
243 grpc_event ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
244 if (ev.type == GRPC_QUEUE_TIMEOUT) {
245 fail_no_event_received(v);
246 break;
247 }
248 expectation* e;
249 expectation* prev = nullptr;
250 for (e = v->first_expectation; e != nullptr; e = e->next) {
251 if (e->tag == ev.tag) {
252 verify_matches(e, &ev);
253 if (e == v->first_expectation) v->first_expectation = e->next;
254 if (prev != nullptr) prev->next = e->next;
255 gpr_free(e);
256 break;
257 }
258 prev = e;
259 }
260 if (e == nullptr) {
261 gpr_log(GPR_ERROR, "cq returned unexpected event: %s",
262 grpc_event_string(&ev).c_str());
263 gpr_log(GPR_ERROR, "expected tags:\n%s", ExpectationsString(*v).c_str());
264 abort();
265 }
266 }
267 }
268
cq_verify_empty_timeout(cq_verifier * v,int timeout_sec)269 void cq_verify_empty_timeout(cq_verifier* v, int timeout_sec) {
270 gpr_timespec deadline =
271 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
272 gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN));
273 grpc_event ev;
274
275 GPR_ASSERT(v->first_expectation == nullptr &&
276 "expectation queue must be empty");
277
278 ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
279 if (ev.type != GRPC_QUEUE_TIMEOUT) {
280 gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s",
281 grpc_event_string(&ev).c_str());
282 abort();
283 }
284 }
285
cq_verify_empty(cq_verifier * v)286 void cq_verify_empty(cq_verifier* v) { cq_verify_empty_timeout(v, 1); }
287
add(cq_verifier * v,const char * file,int line,grpc_completion_type type,void * tag,bool check_success,bool success)288 static void add(cq_verifier* v, const char* file, int line,
289 grpc_completion_type type, void* tag, bool check_success,
290 bool success) {
291 expectation* e = static_cast<expectation*>(gpr_malloc(sizeof(expectation)));
292 e->type = type;
293 e->file = file;
294 e->line = line;
295 e->tag = tag;
296 e->check_success = check_success;
297 e->success = success;
298 e->next = v->first_expectation;
299 v->first_expectation = e;
300 }
301
cq_expect_completion(cq_verifier * v,const char * file,int line,void * tag,bool success)302 void cq_expect_completion(cq_verifier* v, const char* file, int line, void* tag,
303 bool success) {
304 add(v, file, line, GRPC_OP_COMPLETE, tag, true, success);
305 }
306
cq_expect_completion_any_status(cq_verifier * v,const char * file,int line,void * tag)307 void cq_expect_completion_any_status(cq_verifier* v, const char* file, int line,
308 void* tag) {
309 add(v, file, line, GRPC_OP_COMPLETE, tag, false, false);
310 }
311