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 <stdio.h>
23 #include <string.h>
24
25 #include <algorithm>
26 #include <limits>
27 #include <string>
28 #include <utility>
29 #include <vector>
30
31 #include "absl/strings/escaping.h"
32 #include "absl/strings/str_cat.h"
33 #include "absl/strings/str_format.h"
34 #include "absl/strings/str_join.h"
35 #include "absl/strings/string_view.h"
36 #include "gtest/gtest.h"
37
38 #include <grpc/byte_buffer.h>
39 #include <grpc/compression.h>
40 #include <grpc/grpc.h>
41 #include <grpc/slice.h>
42 #include <grpc/slice_buffer.h>
43 #include <grpc/support/log.h>
44 #include <grpc/support/time.h>
45
46 #include "src/core/lib/compression/message_compress.h"
47 #include "src/core/lib/gprpp/crash.h"
48 #include "src/core/lib/gprpp/debug_location.h"
49 #include "src/core/lib/gprpp/match.h"
50 #include "src/core/lib/surface/event_string.h"
51 #include "test/core/util/test_config.h"
52
53 // a set of metadata we expect to find on an event
54 typedef struct metadata {
55 size_t count;
56 size_t cap;
57 char** keys;
58 char** values;
59 } metadata;
60
has_metadata(const grpc_metadata * md,size_t count,const char * key,const char * value)61 static int has_metadata(const grpc_metadata* md, size_t count, const char* key,
62 const char* value) {
63 size_t i;
64 for (i = 0; i < count; i++) {
65 if (0 == grpc_slice_str_cmp(md[i].key, key) &&
66 0 == grpc_slice_str_cmp(md[i].value, value)) {
67 return 1;
68 }
69 }
70 return 0;
71 }
72
contains_metadata(grpc_metadata_array * array,const char * key,const char * value)73 int contains_metadata(grpc_metadata_array* array, const char* key,
74 const char* value) {
75 return has_metadata(array->metadata, array->count, key, value);
76 }
77
has_metadata_slices(const grpc_metadata * md,size_t count,grpc_slice key,grpc_slice value)78 static int has_metadata_slices(const grpc_metadata* md, size_t count,
79 grpc_slice key, grpc_slice value) {
80 size_t i;
81 for (i = 0; i < count; i++) {
82 if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) {
83 return 1;
84 }
85 }
86 return 0;
87 }
88
contains_metadata_slices(grpc_metadata_array * array,grpc_slice key,grpc_slice value)89 int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key,
90 grpc_slice value) {
91 return has_metadata_slices(array->metadata, array->count, key, value);
92 }
93
merge_slices(grpc_slice * slices,size_t nslices)94 static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
95 size_t i;
96 size_t len = 0;
97 uint8_t* cursor;
98 grpc_slice out;
99
100 for (i = 0; i < nslices; i++) {
101 len += GRPC_SLICE_LENGTH(slices[i]);
102 }
103
104 out = grpc_slice_malloc(len);
105 cursor = GRPC_SLICE_START_PTR(out);
106
107 for (i = 0; i < nslices; i++) {
108 memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
109 GRPC_SLICE_LENGTH(slices[i]));
110 cursor += GRPC_SLICE_LENGTH(slices[i]);
111 }
112
113 return out;
114 }
115
raw_byte_buffer_eq_slice(grpc_byte_buffer * rbb,grpc_slice b)116 int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) {
117 grpc_slice a;
118 int ok;
119
120 if (!rbb) return 0;
121
122 a = merge_slices(rbb->data.raw.slice_buffer.slices,
123 rbb->data.raw.slice_buffer.count);
124 ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
125 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
126 GRPC_SLICE_LENGTH(a));
127 if (!ok) {
128 gpr_log(GPR_ERROR,
129 "SLICE MISMATCH: left_length=%" PRIuPTR " right_length=%" PRIuPTR,
130 GRPC_SLICE_LENGTH(a), GRPC_SLICE_LENGTH(b));
131 std::string out;
132 const char* a_str = reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(a));
133 const char* b_str = reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(b));
134 for (size_t i = 0; i < std::max(GRPC_SLICE_LENGTH(a), GRPC_SLICE_LENGTH(b));
135 i++) {
136 if (i >= GRPC_SLICE_LENGTH(a)) {
137 absl::StrAppend(&out, "\u001b[36m", // cyan
138 absl::CEscape(absl::string_view(&b_str[i], 1)),
139 "\u001b[0m");
140 } else if (i >= GRPC_SLICE_LENGTH(b)) {
141 absl::StrAppend(&out, "\u001b[35m", // magenta
142 absl::CEscape(absl::string_view(&a_str[i], 1)),
143 "\u001b[0m");
144 } else if (a_str[i] == b_str[i]) {
145 absl::StrAppend(&out, absl::CEscape(absl::string_view(&a_str[i], 1)));
146 } else {
147 absl::StrAppend(&out, "\u001b[31m", // red
148 absl::CEscape(absl::string_view(&a_str[i], 1)),
149 "\u001b[33m", // yellow
150 absl::CEscape(absl::string_view(&b_str[i], 1)),
151 "\u001b[0m");
152 }
153 gpr_log(GPR_ERROR, "%s", out.c_str());
154 }
155 }
156 grpc_slice_unref(a);
157 grpc_slice_unref(b);
158 return ok;
159 }
160
byte_buffer_eq_slice(grpc_byte_buffer * bb,grpc_slice b)161 int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) {
162 if (bb == nullptr) return 0;
163 if (bb->data.raw.compression > GRPC_COMPRESS_NONE) {
164 grpc_slice_buffer decompressed_buffer;
165 grpc_slice_buffer_init(&decompressed_buffer);
166 GPR_ASSERT(grpc_msg_decompress(bb->data.raw.compression,
167 &bb->data.raw.slice_buffer,
168 &decompressed_buffer));
169 grpc_byte_buffer* rbb = grpc_raw_byte_buffer_create(
170 decompressed_buffer.slices, decompressed_buffer.count);
171 int ret_val = raw_byte_buffer_eq_slice(rbb, b);
172 grpc_byte_buffer_destroy(rbb);
173 grpc_slice_buffer_destroy(&decompressed_buffer);
174 return ret_val;
175 }
176 return raw_byte_buffer_eq_slice(bb, b);
177 }
178
byte_buffer_eq_string(grpc_byte_buffer * bb,const char * str)179 int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) {
180 return byte_buffer_eq_slice(bb, grpc_slice_from_copied_string(str));
181 }
182
183 namespace {
IsProbablyInteger(void * p)184 bool IsProbablyInteger(void* p) {
185 return reinterpret_cast<uintptr_t>(p) < 1000000 ||
186 (reinterpret_cast<uintptr_t>(p) >
187 std::numeric_limits<uintptr_t>::max() - 10);
188 }
189
TagStr(void * tag)190 std::string TagStr(void* tag) {
191 if (IsProbablyInteger(tag)) {
192 return absl::StrFormat("tag(%" PRIdPTR ")",
193 reinterpret_cast<intptr_t>(tag));
194 } else {
195 return absl::StrFormat("%p", tag);
196 }
197 }
198 } // namespace
199
200 namespace grpc_core {
201
CqVerifier(grpc_completion_queue * cq,absl::AnyInvocable<void (Failure)const> fail,absl::AnyInvocable<void (grpc_event_engine::experimental::EventEngine::Duration)const> step_fn)202 CqVerifier::CqVerifier(
203 grpc_completion_queue* cq, absl::AnyInvocable<void(Failure) const> fail,
204 absl::AnyInvocable<
205 void(grpc_event_engine::experimental::EventEngine::Duration) const>
206 step_fn)
207 : cq_(cq), fail_(std::move(fail)), step_fn_(std::move(step_fn)) {}
208
~CqVerifier()209 CqVerifier::~CqVerifier() { Verify(); }
210
ToString() const211 std::string CqVerifier::Expectation::ToString() const {
212 return absl::StrCat(
213 location.file(), ":", location.line(), ": ", TagStr(tag), " ",
214 Match(
215 result,
216 [](bool success) {
217 return absl::StrCat("success=", success ? "true" : "false");
218 },
219 [](Maybe) { return std::string("maybe"); },
220 [](AnyStatus) { return std::string("any success value"); },
221 [](const PerformAction&) {
222 return std::string("perform some action");
223 },
224 [](const MaybePerformAction&) {
225 return std::string("maybe perform action");
226 }));
227 }
228
ToShortString() const229 std::string CqVerifier::Expectation::ToShortString() const {
230 return absl::StrCat(
231 TagStr(tag),
232 Match(
233 result,
234 [](bool success) -> std::string {
235 if (!success) return "-❌";
236 return "-✅";
237 },
238 [](Maybe) { return std::string("-❓"); },
239 [](AnyStatus) { return std::string("-"); },
240 [](const PerformAction&) { return std::string("-"); },
241 [](const MaybePerformAction&) { return std::string("-❓"); }));
242 }
243
ToStrings() const244 std::vector<std::string> CqVerifier::ToStrings() const {
245 std::vector<std::string> expectations;
246 expectations.reserve(expectations_.size());
247 for (const auto& e : expectations_) {
248 expectations.push_back(e.ToString());
249 }
250 return expectations;
251 }
252
ToString() const253 std::string CqVerifier::ToString() const {
254 return absl::StrJoin(ToStrings(), "\n");
255 }
256
ToShortStrings() const257 std::vector<std::string> CqVerifier::ToShortStrings() const {
258 std::vector<std::string> expectations;
259 expectations.reserve(expectations_.size());
260 for (const auto& e : expectations_) {
261 expectations.push_back(e.ToShortString());
262 }
263 return expectations;
264 }
265
ToShortString() const266 std::string CqVerifier::ToShortString() const {
267 return absl::StrJoin(ToShortStrings(), " ");
268 }
269
FailNoEventReceived(const SourceLocation & location) const270 void CqVerifier::FailNoEventReceived(const SourceLocation& location) const {
271 fail_(Failure{location, "No event received", ToStrings(), {}});
272 }
273
FailUnexpectedEvent(grpc_event * ev,const SourceLocation & location) const274 void CqVerifier::FailUnexpectedEvent(grpc_event* ev,
275 const SourceLocation& location) const {
276 std::vector<std::string> message_details;
277 if (ev->type == GRPC_OP_COMPLETE && ev->success) {
278 auto successful_state_strings = successful_state_strings_.find(ev->tag);
279 if (successful_state_strings != successful_state_strings_.end()) {
280 for (SuccessfulStateString* sss : successful_state_strings->second) {
281 message_details.push_back(sss->GetSuccessfulStateString());
282 }
283 }
284 }
285 fail_(Failure{location,
286 absl::StrCat("Unexpected event: ", grpc_event_string(ev)),
287 ToStrings(), std::move(message_details)});
288 }
289
290 namespace {
CrashMessage(const CqVerifier::Failure & failure)291 std::string CrashMessage(const CqVerifier::Failure& failure) {
292 std::string message = failure.message;
293 if (!failure.message_details.empty()) {
294 absl::StrAppend(&message, "\nwith:");
295 for (const auto& detail : failure.message_details) {
296 absl::StrAppend(&message, "\n ", detail);
297 }
298 }
299 absl::StrAppend(&message, "\nchecked @ ", failure.location.file(), ":",
300 failure.location.line());
301 if (!failure.expected.empty()) {
302 absl::StrAppend(&message, "\nexpected:\n");
303 for (const auto& line : failure.expected) {
304 absl::StrAppend(&message, " ", line, "\n");
305 }
306 } else {
307 absl::StrAppend(&message, "\nexpected nothing");
308 }
309 return message;
310 }
311 } // namespace
312
FailUsingGprCrashWithStdio(const Failure & failure)313 void CqVerifier::FailUsingGprCrashWithStdio(const Failure& failure) {
314 CrashWithStdio(CrashMessage(failure));
315 }
316
FailUsingGprCrash(const Failure & failure)317 void CqVerifier::FailUsingGprCrash(const Failure& failure) {
318 Crash(CrashMessage(failure));
319 }
320
FailUsingGtestFail(const Failure & failure)321 void CqVerifier::FailUsingGtestFail(const Failure& failure) {
322 std::string message = absl::StrCat(" ", failure.message);
323 if (!failure.expected.empty()) {
324 absl::StrAppend(&message, "\n expected:\n");
325 for (const auto& line : failure.expected) {
326 absl::StrAppend(&message, " ", line, "\n");
327 }
328 } else {
329 absl::StrAppend(&message, "\n expected nothing");
330 }
331 ADD_FAILURE_AT(failure.location.file(), failure.location.line()) << message;
332 }
333
334 namespace {
IsMaybe(const CqVerifier::ExpectedResult & r)335 bool IsMaybe(const CqVerifier::ExpectedResult& r) {
336 return Match(
337 r, [](bool) { return false; }, [](CqVerifier::Maybe) { return true; },
338 [](CqVerifier::AnyStatus) { return false; },
339 [](const CqVerifier::PerformAction&) { return false; },
340 [](const CqVerifier::MaybePerformAction&) { return true; });
341 }
342 } // namespace
343
Step(gpr_timespec deadline)344 grpc_event CqVerifier::Step(gpr_timespec deadline) {
345 if (step_fn_ != nullptr) {
346 while (true) {
347 grpc_event r = grpc_completion_queue_next(
348 cq_, gpr_inf_past(deadline.clock_type), nullptr);
349 if (r.type != GRPC_QUEUE_TIMEOUT) return r;
350 auto now = gpr_now(deadline.clock_type);
351 if (gpr_time_cmp(deadline, now) < 0) break;
352 step_fn_(Timestamp::FromTimespecRoundDown(deadline) - Timestamp::Now());
353 }
354 return grpc_event{GRPC_QUEUE_TIMEOUT, 0, nullptr};
355 }
356 return grpc_completion_queue_next(cq_, deadline, nullptr);
357 }
358
Verify(Duration timeout,SourceLocation location)359 void CqVerifier::Verify(Duration timeout, SourceLocation location) {
360 if (expectations_.empty()) return;
361 bool must_log = true;
362 const gpr_timespec deadline =
363 grpc_timeout_milliseconds_to_deadline(timeout.millis());
364 while (!expectations_.empty()) {
365 must_log = std::exchange(added_expectations_, false) || must_log;
366 if (log_verifications_ && must_log) {
367 gpr_log(GPR_ERROR, "Verify %s for %s", ToShortString().c_str(),
368 timeout.ToString().c_str());
369 }
370 must_log = false;
371 grpc_event ev = Step(deadline);
372 if (ev.type == GRPC_QUEUE_TIMEOUT) break;
373 if (ev.type != GRPC_OP_COMPLETE) {
374 FailUnexpectedEvent(&ev, location);
375 }
376 bool found = false;
377 for (auto it = expectations_.begin(); it != expectations_.end(); ++it) {
378 if (it->tag != ev.tag) continue;
379 auto expectation = std::move(*it);
380 expectations_.erase(it);
381 const bool expected = Match(
382 expectation.result,
383 [ev](bool success) { return ev.success == success; },
384 [ev](Maybe m) {
385 if (m.seen != nullptr) *m.seen = true;
386 return ev.success != 0;
387 },
388 [ev](AnyStatus a) {
389 if (a.result != nullptr) *a.result = ev.success;
390 return true;
391 },
392 [ev](const PerformAction& action) {
393 action.action(ev.success);
394 return true;
395 },
396 [ev](const MaybePerformAction& action) {
397 action.action(ev.success);
398 return true;
399 });
400 if (!expected) {
401 FailUnexpectedEvent(&ev, location);
402 }
403 found = true;
404 break;
405 }
406 if (!found) FailUnexpectedEvent(&ev, location);
407 if (AllMaybes()) break;
408 }
409 expectations_.erase(
410 std::remove_if(expectations_.begin(), expectations_.end(),
411 [](const Expectation& e) { return IsMaybe(e.result); }),
412 expectations_.end());
413 if (!expectations_.empty()) FailNoEventReceived(location);
414 }
415
AllMaybes() const416 bool CqVerifier::AllMaybes() const {
417 for (const auto& e : expectations_) {
418 if (!IsMaybe(e.result)) return false;
419 }
420 return true;
421 }
422
VerifyEmpty(Duration timeout,SourceLocation location)423 void CqVerifier::VerifyEmpty(Duration timeout, SourceLocation location) {
424 if (log_verifications_) {
425 gpr_log(GPR_ERROR, "Verify empty completion queue for %s",
426 timeout.ToString().c_str());
427 }
428 const gpr_timespec deadline =
429 gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), timeout.as_timespec());
430 GPR_ASSERT(expectations_.empty());
431 grpc_event ev = Step(deadline);
432 if (ev.type != GRPC_QUEUE_TIMEOUT) {
433 FailUnexpectedEvent(&ev, location);
434 }
435 }
436
Expect(void * tag,ExpectedResult result,SourceLocation location)437 void CqVerifier::Expect(void* tag, ExpectedResult result,
438 SourceLocation location) {
439 added_expectations_ = true;
440 expectations_.push_back(Expectation{location, tag, std::move(result)});
441 }
442
AddSuccessfulStateString(void * tag,SuccessfulStateString * successful_state_string)443 void CqVerifier::AddSuccessfulStateString(
444 void* tag, SuccessfulStateString* successful_state_string) {
445 successful_state_strings_[tag].push_back(successful_state_string);
446 }
447
ClearSuccessfulStateStrings(void * tag)448 void CqVerifier::ClearSuccessfulStateStrings(void* tag) {
449 successful_state_strings_.erase(tag);
450 }
451
452 } // namespace grpc_core
453