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