• 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 <grpc/byte_buffer.h>
20 #include <grpc/credentials.h>
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_security.h>
23 #include <grpc/impl/propagation_bits.h>
24 #include <grpc/slice.h>
25 #include <grpc/status.h>
26 #include <grpc/support/port_platform.h>
27 #include <grpc/support/time.h>
28 #include <limits.h>
29 #include <string.h>
30 
31 #include <initializer_list>
32 #include <memory>
33 #include <string>
34 
35 #include "absl/log/check.h"
36 #include "absl/log/log.h"
37 #include "src/core/util/host_port.h"
38 #include "test/core/end2end/cq_verifier.h"
39 #include "test/core/test_util/port.h"
40 #include "test/core/test_util/test_config.h"
41 
42 struct test_state {
43   int is_client;
44   grpc_channel* chan;
45   grpc_call* call;
46   gpr_timespec deadline;
47   grpc_completion_queue* cq;
48   std::unique_ptr<grpc_core::CqVerifier> cqv;
49   grpc_op ops[6];
50   grpc_metadata_array initial_metadata_recv;
51   grpc_metadata_array trailing_metadata_recv;
52   grpc_status_code status;
53   grpc_slice details;
54   grpc_call* server_call;
55   grpc_server* server;
56   grpc_metadata_array server_initial_metadata_recv;
57   grpc_call_details call_details;
58 };
59 
60 static struct test_state g_state;
61 
prepare_test(int is_client)62 static void prepare_test(int is_client) {
63   int port = grpc_pick_unused_port_or_die();
64   grpc_op* op;
65   g_state.is_client = is_client;
66   grpc_metadata_array_init(&g_state.initial_metadata_recv);
67   grpc_metadata_array_init(&g_state.trailing_metadata_recv);
68   g_state.deadline = grpc_timeout_seconds_to_deadline(5);
69   g_state.cq = grpc_completion_queue_create_for_next(nullptr);
70   g_state.cqv = std::make_unique<grpc_core::CqVerifier>(g_state.cq);
71   g_state.details = grpc_empty_slice();
72   memset(g_state.ops, 0, sizeof(g_state.ops));
73 
74   if (is_client) {
75     // create a call, channel to a non existent server
76     grpc_channel_credentials* creds = grpc_insecure_credentials_create();
77     g_state.chan = grpc_channel_create("nonexistent:54321", creds, nullptr);
78     grpc_channel_credentials_release(creds);
79     grpc_slice host = grpc_slice_from_static_string("nonexistent");
80     g_state.call = grpc_channel_create_call(
81         g_state.chan, nullptr, GRPC_PROPAGATE_DEFAULTS, g_state.cq,
82         grpc_slice_from_static_string("/Foo"), &host, g_state.deadline,
83         nullptr);
84   } else {
85     g_state.server = grpc_server_create(nullptr, nullptr);
86     grpc_server_register_completion_queue(g_state.server, g_state.cq, nullptr);
87     std::string server_hostport = grpc_core::JoinHostPort("0.0.0.0", port);
88     grpc_server_credentials* server_creds =
89         grpc_insecure_server_credentials_create();
90     grpc_server_add_http2_port(g_state.server, server_hostport.c_str(),
91                                server_creds);
92     grpc_server_credentials_release(server_creds);
93     grpc_server_start(g_state.server);
94     server_hostport = grpc_core::JoinHostPort("localhost", port);
95     grpc_channel_credentials* creds = grpc_insecure_credentials_create();
96     g_state.chan = grpc_channel_create(server_hostport.c_str(), creds, nullptr);
97     grpc_channel_credentials_release(creds);
98     grpc_slice host = grpc_slice_from_static_string("bar");
99     g_state.call = grpc_channel_create_call(
100         g_state.chan, nullptr, GRPC_PROPAGATE_DEFAULTS, g_state.cq,
101         grpc_slice_from_static_string("/Foo"), &host, g_state.deadline,
102         nullptr);
103     grpc_metadata_array_init(&g_state.server_initial_metadata_recv);
104     grpc_call_details_init(&g_state.call_details);
105     op = g_state.ops;
106     op->op = GRPC_OP_SEND_INITIAL_METADATA;
107     op->data.send_initial_metadata.count = 0;
108     op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
109     op->reserved = nullptr;
110     op++;
111     CHECK_EQ(GRPC_CALL_OK,
112              grpc_call_start_batch(g_state.call, g_state.ops,
113                                    (size_t)(op - g_state.ops),
114                                    grpc_core::CqVerifier::tag(1), nullptr));
115     CHECK_EQ(GRPC_CALL_OK,
116              grpc_server_request_call(
117                  g_state.server, &g_state.server_call, &g_state.call_details,
118                  &g_state.server_initial_metadata_recv, g_state.cq, g_state.cq,
119                  grpc_core::CqVerifier::tag(101)));
120     g_state.cqv->Expect(grpc_core::CqVerifier::tag(101), true);
121     g_state.cqv->Expect(grpc_core::CqVerifier::tag(1), true);
122     g_state.cqv->Verify();
123   }
124 }
125 
cleanup_test()126 static void cleanup_test() {
127   grpc_call_unref(g_state.call);
128   grpc_channel_destroy(g_state.chan);
129   grpc_slice_unref(g_state.details);
130   grpc_metadata_array_destroy(&g_state.initial_metadata_recv);
131   grpc_metadata_array_destroy(&g_state.trailing_metadata_recv);
132 
133   if (!g_state.is_client) {
134     grpc_call_unref(g_state.server_call);
135     grpc_server_shutdown_and_notify(g_state.server, g_state.cq,
136                                     grpc_core::CqVerifier::tag(1000));
137     grpc_event ev;
138     do {
139       ev = grpc_completion_queue_next(
140           g_state.cq, grpc_timeout_seconds_to_deadline(5), nullptr);
141     } while (ev.type != GRPC_OP_COMPLETE ||
142              ev.tag != grpc_core::CqVerifier::tag(1000));
143     grpc_server_destroy(g_state.server);
144     grpc_call_details_destroy(&g_state.call_details);
145     grpc_metadata_array_destroy(&g_state.server_initial_metadata_recv);
146   }
147   grpc_completion_queue_shutdown(g_state.cq);
148   while (grpc_completion_queue_next(g_state.cq,
149                                     gpr_inf_future(GPR_CLOCK_REALTIME), nullptr)
150              .type != GRPC_QUEUE_SHUTDOWN) {
151   }
152   grpc_completion_queue_destroy(g_state.cq);
153 }
154 
test_non_null_reserved_on_start_batch()155 static void test_non_null_reserved_on_start_batch() {
156   LOG(INFO) << "test_non_null_reserved_on_start_batch";
157 
158   prepare_test(1);
159   CHECK(GRPC_CALL_ERROR ==
160         grpc_call_start_batch(g_state.call, nullptr, 0, nullptr,
161                               grpc_core::CqVerifier::tag(1)));
162   cleanup_test();
163 }
164 
test_non_null_reserved_on_op()165 static void test_non_null_reserved_on_op() {
166   LOG(INFO) << "test_non_null_reserved_on_op";
167 
168   grpc_op* op;
169   prepare_test(1);
170 
171   op = g_state.ops;
172   op->op = GRPC_OP_SEND_INITIAL_METADATA;
173   op->data.send_initial_metadata.count = 0;
174   op->flags = 0;
175   op->reserved = grpc_core::CqVerifier::tag(2);
176   op++;
177   CHECK(GRPC_CALL_ERROR == grpc_call_start_batch(g_state.call, g_state.ops,
178                                                  (size_t)(op - g_state.ops),
179                                                  grpc_core::CqVerifier::tag(1),
180                                                  nullptr));
181   cleanup_test();
182 }
183 
test_send_initial_metadata_more_than_once()184 static void test_send_initial_metadata_more_than_once() {
185   LOG(INFO) << "test_send_initial_metadata_more_than_once";
186 
187   grpc_op* op;
188   prepare_test(1);
189 
190   op = g_state.ops;
191   op->op = GRPC_OP_SEND_INITIAL_METADATA;
192   op->data.send_initial_metadata.count = 0;
193   op->flags = 0;
194   op->reserved = nullptr;
195   op++;
196   CHECK_EQ(GRPC_CALL_OK,
197            grpc_call_start_batch(g_state.call, g_state.ops,
198                                  (size_t)(op - g_state.ops),
199                                  grpc_core::CqVerifier::tag(1), nullptr));
200   g_state.cqv->Expect(grpc_core::CqVerifier::tag(1), false);
201   g_state.cqv->Verify();
202 
203   op = g_state.ops;
204   op->op = GRPC_OP_SEND_INITIAL_METADATA;
205   op->data.send_initial_metadata.count = 0;
206   op->flags = 0;
207   op->reserved = nullptr;
208   op++;
209   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
210         grpc_call_start_batch(g_state.call, g_state.ops,
211                               (size_t)(op - g_state.ops),
212                               grpc_core::CqVerifier::tag(1), nullptr));
213   cleanup_test();
214 }
215 
test_too_many_metadata()216 static void test_too_many_metadata() {
217   LOG(INFO) << "test_too_many_metadata";
218 
219   grpc_op* op;
220   prepare_test(1);
221 
222   op = g_state.ops;
223   op->op = GRPC_OP_SEND_INITIAL_METADATA;
224   op->data.send_initial_metadata.count = static_cast<size_t>(INT_MAX) + 1;
225   op->flags = 0;
226   op->reserved = nullptr;
227   op++;
228   CHECK(GRPC_CALL_ERROR_INVALID_METADATA ==
229         grpc_call_start_batch(g_state.call, g_state.ops,
230                               (size_t)(op - g_state.ops),
231                               grpc_core::CqVerifier::tag(1), nullptr));
232   cleanup_test();
233 }
234 
test_send_null_message()235 static void test_send_null_message() {
236   LOG(INFO) << "test_send_null_message";
237 
238   grpc_op* op;
239   prepare_test(1);
240 
241   op = g_state.ops;
242   op->op = GRPC_OP_SEND_INITIAL_METADATA;
243   op->data.send_initial_metadata.count = 0;
244   op->flags = 0;
245   op->reserved = nullptr;
246   op++;
247   op->op = GRPC_OP_SEND_MESSAGE;
248   op->data.send_message.send_message = nullptr;
249   op->flags = 0;
250   op->reserved = nullptr;
251   op++;
252   CHECK(GRPC_CALL_ERROR_INVALID_MESSAGE ==
253         grpc_call_start_batch(g_state.call, g_state.ops,
254                               (size_t)(op - g_state.ops),
255                               grpc_core::CqVerifier::tag(1), nullptr));
256   cleanup_test();
257 }
258 
test_send_messages_at_the_same_time()259 static void test_send_messages_at_the_same_time() {
260   LOG(INFO) << "test_send_messages_at_the_same_time";
261 
262   grpc_op* op;
263   grpc_slice request_payload_slice =
264       grpc_slice_from_copied_string("hello world");
265   grpc_byte_buffer* request_payload =
266       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
267   prepare_test(1);
268   op = g_state.ops;
269   op->op = GRPC_OP_SEND_INITIAL_METADATA;
270   op->data.send_initial_metadata.count = 0;
271   op->flags = 0;
272   op->reserved = nullptr;
273   op++;
274   op->op = GRPC_OP_SEND_MESSAGE;
275   op->data.send_message.send_message = request_payload;
276   op->flags = 0;
277   op->reserved = nullptr;
278   op++;
279   op->op = GRPC_OP_SEND_MESSAGE;
280   op->data.send_message.send_message =
281       static_cast<grpc_byte_buffer*>(grpc_core::CqVerifier::tag(2));
282   op->flags = 0;
283   op->reserved = nullptr;
284   op++;
285   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
286         grpc_call_start_batch(g_state.call, g_state.ops,
287                               (size_t)(op - g_state.ops),
288                               grpc_core::CqVerifier::tag(1), nullptr));
289   grpc_byte_buffer_destroy(request_payload);
290   cleanup_test();
291 }
292 
test_send_server_status_from_client()293 static void test_send_server_status_from_client() {
294   LOG(INFO) << "test_send_server_status_from_client";
295 
296   grpc_op* op;
297   prepare_test(1);
298 
299   op = g_state.ops;
300   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
301   op->data.send_status_from_server.trailing_metadata_count = 0;
302   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
303   grpc_slice status_details = grpc_slice_from_static_string("xyz");
304   op->data.send_status_from_server.status_details = &status_details;
305   op->flags = 0;
306   op->reserved = nullptr;
307   op++;
308   CHECK(GRPC_CALL_ERROR_NOT_ON_CLIENT ==
309         grpc_call_start_batch(g_state.call, g_state.ops,
310                               (size_t)(op - g_state.ops),
311                               grpc_core::CqVerifier::tag(1), nullptr));
312   cleanup_test();
313 }
314 
test_receive_initial_metadata_twice_at_client()315 static void test_receive_initial_metadata_twice_at_client() {
316   LOG(INFO) << "test_receive_initial_metadata_twice_at_client";
317 
318   grpc_op* op;
319   prepare_test(1);
320   op = g_state.ops;
321   op->op = GRPC_OP_RECV_INITIAL_METADATA;
322   op->data.recv_initial_metadata.recv_initial_metadata =
323       &g_state.initial_metadata_recv;
324   op->flags = 0;
325   op->reserved = nullptr;
326   op++;
327   CHECK_EQ(GRPC_CALL_OK,
328            grpc_call_start_batch(g_state.call, g_state.ops,
329                                  (size_t)(op - g_state.ops),
330                                  grpc_core::CqVerifier::tag(1), nullptr));
331   g_state.cqv->Expect(grpc_core::CqVerifier::tag(1), false);
332   g_state.cqv->Verify();
333   op = g_state.ops;
334   op->op = GRPC_OP_RECV_INITIAL_METADATA;
335   op->data.recv_initial_metadata.recv_initial_metadata =
336       &g_state.initial_metadata_recv;
337   op->flags = 0;
338   op->reserved = nullptr;
339   op++;
340   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
341         grpc_call_start_batch(g_state.call, g_state.ops,
342                               (size_t)(op - g_state.ops),
343                               grpc_core::CqVerifier::tag(1), nullptr));
344   cleanup_test();
345 }
346 
test_receive_message_with_invalid_flags()347 static void test_receive_message_with_invalid_flags() {
348   LOG(INFO) << "test_receive_message_with_invalid_flags";
349 
350   grpc_op* op;
351   grpc_byte_buffer* payload = nullptr;
352   prepare_test(1);
353   op = g_state.ops;
354   op->op = GRPC_OP_RECV_MESSAGE;
355   op->data.recv_message.recv_message = &payload;
356   op->flags = 1;
357   op->reserved = nullptr;
358   op++;
359   CHECK(GRPC_CALL_ERROR_INVALID_FLAGS ==
360         grpc_call_start_batch(g_state.call, g_state.ops,
361                               (size_t)(op - g_state.ops),
362                               grpc_core::CqVerifier::tag(1), nullptr));
363   cleanup_test();
364 }
365 
test_receive_two_messages_at_the_same_time()366 static void test_receive_two_messages_at_the_same_time() {
367   LOG(INFO) << "test_receive_two_messages_at_the_same_time";
368 
369   grpc_op* op;
370   grpc_byte_buffer* payload = nullptr;
371   prepare_test(1);
372   op = g_state.ops;
373   op->op = GRPC_OP_RECV_MESSAGE;
374   op->data.recv_message.recv_message = &payload;
375   op->flags = 0;
376   op->reserved = nullptr;
377   op++;
378   op->op = GRPC_OP_RECV_MESSAGE;
379   op->data.recv_message.recv_message = &payload;
380   op->flags = 0;
381   op->reserved = nullptr;
382   op++;
383   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
384         grpc_call_start_batch(g_state.call, g_state.ops,
385                               (size_t)(op - g_state.ops),
386                               grpc_core::CqVerifier::tag(1), nullptr));
387   cleanup_test();
388 }
389 
test_recv_close_on_server_from_client()390 static void test_recv_close_on_server_from_client() {
391   LOG(INFO) << "test_recv_close_on_server_from_client";
392 
393   grpc_op* op;
394   prepare_test(1);
395 
396   op = g_state.ops;
397   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
398   op->data.recv_close_on_server.cancelled = nullptr;
399   op->flags = 0;
400   op->reserved = nullptr;
401   op++;
402   CHECK(GRPC_CALL_ERROR_NOT_ON_CLIENT ==
403         grpc_call_start_batch(g_state.call, g_state.ops,
404                               (size_t)(op - g_state.ops),
405                               grpc_core::CqVerifier::tag(1), nullptr));
406   cleanup_test();
407 }
408 
test_recv_status_on_client_twice()409 static void test_recv_status_on_client_twice() {
410   LOG(INFO) << "test_recv_status_on_client_twice";
411 
412   grpc_op* op;
413   prepare_test(1);
414 
415   op = g_state.ops;
416   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
417   op->data.recv_status_on_client.trailing_metadata =
418       &g_state.trailing_metadata_recv;
419   op->data.recv_status_on_client.status = &g_state.status;
420   op->data.recv_status_on_client.status_details = &g_state.details;
421   op->flags = 0;
422   op->reserved = nullptr;
423   op++;
424   CHECK_EQ(GRPC_CALL_OK,
425            grpc_call_start_batch(g_state.call, g_state.ops,
426                                  (size_t)(op - g_state.ops),
427                                  grpc_core::CqVerifier::tag(1), nullptr));
428   g_state.cqv->Expect(grpc_core::CqVerifier::tag(1), true);
429   g_state.cqv->Verify();
430 
431   op = g_state.ops;
432   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
433   op->data.recv_status_on_client.trailing_metadata = nullptr;
434   op->data.recv_status_on_client.status = nullptr;
435   op->data.recv_status_on_client.status_details = nullptr;
436   op->flags = 0;
437   op->reserved = nullptr;
438   op++;
439   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
440         grpc_call_start_batch(g_state.call, g_state.ops,
441                               (size_t)(op - g_state.ops),
442                               grpc_core::CqVerifier::tag(1), nullptr));
443   cleanup_test();
444 }
445 
test_send_close_from_client_on_server()446 static void test_send_close_from_client_on_server() {
447   LOG(INFO) << "test_send_close_from_client_on_server";
448 
449   grpc_op* op;
450   prepare_test(0);
451 
452   op = g_state.ops;
453   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
454   op->flags = 0;
455   op->reserved = nullptr;
456   op++;
457   CHECK(GRPC_CALL_ERROR_NOT_ON_SERVER ==
458         grpc_call_start_batch(g_state.server_call, g_state.ops,
459                               (size_t)(op - g_state.ops),
460                               grpc_core::CqVerifier::tag(2), nullptr));
461   cleanup_test();
462 }
463 
test_recv_status_on_client_from_server()464 static void test_recv_status_on_client_from_server() {
465   LOG(INFO) << "test_recv_status_on_client_from_server";
466 
467   grpc_op* op;
468   prepare_test(0);
469 
470   op = g_state.ops;
471   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
472   op->data.recv_status_on_client.trailing_metadata =
473       &g_state.trailing_metadata_recv;
474   op->data.recv_status_on_client.status = &g_state.status;
475   op->data.recv_status_on_client.status_details = &g_state.details;
476   op->flags = 0;
477   op->reserved = nullptr;
478   op++;
479   CHECK(GRPC_CALL_ERROR_NOT_ON_SERVER ==
480         grpc_call_start_batch(g_state.server_call, g_state.ops,
481                               (size_t)(op - g_state.ops),
482                               grpc_core::CqVerifier::tag(2), nullptr));
483   cleanup_test();
484 }
485 
test_send_status_from_server_with_invalid_flags()486 static void test_send_status_from_server_with_invalid_flags() {
487   LOG(INFO) << "test_send_status_from_server_with_invalid_flags";
488 
489   grpc_op* op;
490   prepare_test(0);
491 
492   op = g_state.ops;
493   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
494   op->data.send_status_from_server.trailing_metadata_count = 0;
495   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
496   grpc_slice status_details = grpc_slice_from_static_string("xyz");
497   op->data.send_status_from_server.status_details = &status_details;
498   op->flags = 1;
499   op->reserved = nullptr;
500   op++;
501   CHECK(GRPC_CALL_ERROR_INVALID_FLAGS ==
502         grpc_call_start_batch(g_state.server_call, g_state.ops,
503                               (size_t)(op - g_state.ops),
504                               grpc_core::CqVerifier::tag(2), nullptr));
505   cleanup_test();
506 }
507 
test_too_many_trailing_metadata()508 static void test_too_many_trailing_metadata() {
509   LOG(INFO) << "test_too_many_trailing_metadata";
510 
511   grpc_op* op;
512   prepare_test(0);
513 
514   op = g_state.ops;
515   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
516   op->data.send_status_from_server.trailing_metadata_count =
517       static_cast<size_t>(INT_MAX) + 1;
518   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
519   grpc_slice status_details = grpc_slice_from_static_string("xyz");
520   op->data.send_status_from_server.status_details = &status_details;
521   op->flags = 0;
522   op->reserved = nullptr;
523   op++;
524   CHECK(GRPC_CALL_ERROR_INVALID_METADATA ==
525         grpc_call_start_batch(g_state.server_call, g_state.ops,
526                               (size_t)(op - g_state.ops),
527                               grpc_core::CqVerifier::tag(2), nullptr));
528   cleanup_test();
529 }
530 
test_send_server_status_twice()531 static void test_send_server_status_twice() {
532   LOG(INFO) << "test_send_server_status_twice";
533 
534   grpc_op* op;
535   prepare_test(0);
536 
537   op = g_state.ops;
538   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
539   op->data.send_status_from_server.trailing_metadata_count = 0;
540   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
541   grpc_slice status_details = grpc_slice_from_static_string("xyz");
542   op->data.send_status_from_server.status_details = &status_details;
543   op->flags = 0;
544   op->reserved = nullptr;
545   op++;
546   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
547   op->data.send_status_from_server.trailing_metadata_count = 0;
548   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
549   op->data.send_status_from_server.status_details = &status_details;
550   op->flags = 0;
551   op->reserved = nullptr;
552   op++;
553   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
554         grpc_call_start_batch(g_state.server_call, g_state.ops,
555                               (size_t)(op - g_state.ops),
556                               grpc_core::CqVerifier::tag(2), nullptr));
557   cleanup_test();
558 }
559 
test_recv_close_on_server_with_invalid_flags()560 static void test_recv_close_on_server_with_invalid_flags() {
561   LOG(INFO) << "test_recv_close_on_server_with_invalid_flags";
562 
563   grpc_op* op;
564   prepare_test(0);
565 
566   op = g_state.ops;
567   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
568   op->data.recv_close_on_server.cancelled = nullptr;
569   op->flags = 1;
570   op->reserved = nullptr;
571   op++;
572   CHECK(GRPC_CALL_ERROR_INVALID_FLAGS ==
573         grpc_call_start_batch(g_state.server_call, g_state.ops,
574                               (size_t)(op - g_state.ops),
575                               grpc_core::CqVerifier::tag(2), nullptr));
576   cleanup_test();
577 }
578 
test_recv_close_on_server_twice()579 static void test_recv_close_on_server_twice() {
580   LOG(INFO) << "test_recv_close_on_server_twice";
581 
582   grpc_op* op;
583   prepare_test(0);
584 
585   op = g_state.ops;
586   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
587   op->data.recv_close_on_server.cancelled = nullptr;
588   op->flags = 0;
589   op->reserved = nullptr;
590   op++;
591   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
592   op->data.recv_close_on_server.cancelled = nullptr;
593   op->flags = 0;
594   op->reserved = nullptr;
595   op++;
596   CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
597         grpc_call_start_batch(g_state.server_call, g_state.ops,
598                               (size_t)(op - g_state.ops),
599                               grpc_core::CqVerifier::tag(2), nullptr));
600   cleanup_test();
601 }
602 
test_invalid_initial_metadata_reserved_key()603 static void test_invalid_initial_metadata_reserved_key() {
604   LOG(INFO) << "test_invalid_initial_metadata_reserved_key";
605 
606   grpc_metadata metadata;
607   metadata.key = grpc_slice_from_static_string(":start_with_colon");
608   metadata.value = grpc_slice_from_static_string("value");
609 
610   grpc_op* op;
611   prepare_test(1);
612   op = g_state.ops;
613   op->op = GRPC_OP_SEND_INITIAL_METADATA;
614   op->data.send_initial_metadata.count = 1;
615   op->data.send_initial_metadata.metadata = &metadata;
616   op->flags = 0;
617   op->reserved = nullptr;
618   op++;
619   CHECK(GRPC_CALL_ERROR_INVALID_METADATA ==
620         grpc_call_start_batch(g_state.call, g_state.ops,
621                               (size_t)(op - g_state.ops),
622                               grpc_core::CqVerifier::tag(1), nullptr));
623   cleanup_test();
624 }
625 
test_multiple_ops_in_a_single_batch()626 static void test_multiple_ops_in_a_single_batch() {
627   LOG(INFO) << "test_multiple_ops_in_a_single_batch";
628 
629   grpc_op* op;
630   prepare_test(1);
631 
632   for (auto which :
633        {GRPC_OP_SEND_INITIAL_METADATA, GRPC_OP_RECV_INITIAL_METADATA,
634         GRPC_OP_SEND_MESSAGE, GRPC_OP_RECV_MESSAGE,
635         GRPC_OP_RECV_STATUS_ON_CLIENT, GRPC_OP_RECV_CLOSE_ON_SERVER,
636         GRPC_OP_SEND_STATUS_FROM_SERVER, GRPC_OP_RECV_CLOSE_ON_SERVER}) {
637     op = g_state.ops;
638     op->op = which;
639     op++;
640     op->op = which;
641     op++;
642     CHECK(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS ==
643           grpc_call_start_batch(g_state.call, g_state.ops,
644                                 (size_t)(op - g_state.ops),
645                                 grpc_core::CqVerifier::tag(1), nullptr));
646   }
647 
648   cleanup_test();
649 }
650 
main(int argc,char ** argv)651 int main(int argc, char** argv) {
652   grpc::testing::TestEnvironment env(&argc, argv);
653   grpc_init();
654   test_invalid_initial_metadata_reserved_key();
655   test_non_null_reserved_on_start_batch();
656   test_non_null_reserved_on_op();
657   test_send_initial_metadata_more_than_once();
658   test_too_many_metadata();
659   test_send_null_message();
660   test_send_messages_at_the_same_time();
661   test_send_server_status_from_client();
662   test_receive_initial_metadata_twice_at_client();
663   test_receive_message_with_invalid_flags();
664   test_receive_two_messages_at_the_same_time();
665   test_recv_close_on_server_from_client();
666   test_recv_status_on_client_twice();
667   test_send_close_from_client_on_server();
668   test_recv_status_on_client_from_server();
669   test_send_status_from_server_with_invalid_flags();
670   test_too_many_trailing_metadata();
671   test_send_server_status_twice();
672   test_recv_close_on_server_with_invalid_flags();
673   test_recv_close_on_server_twice();
674   test_multiple_ops_in_a_single_batch();
675   grpc_shutdown();
676 
677   return 0;
678 }
679