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