invalid_call_argument_test.cc 23 KB

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