binary_metadata.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <stdio.h>
  19. #include <string.h>
  20. #include <grpc/byte_buffer.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/time.h>
  24. #include "src/core/lib/config/core_configuration.h"
  25. #include "test/core/end2end/cq_verifier.h"
  26. #include "test/core/end2end/end2end_tests.h"
  27. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  28. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  29. const char* test_name,
  30. grpc_channel_args* client_args,
  31. grpc_channel_args* server_args) {
  32. grpc_end2end_test_fixture f;
  33. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  34. client_args =
  35. const_cast<grpc_channel_args*>(grpc_core::CoreConfiguration::Get()
  36. .channel_args_preconditioning()
  37. .PreconditionChannelArgs(client_args));
  38. server_args =
  39. const_cast<grpc_channel_args*>(grpc_core::CoreConfiguration::Get()
  40. .channel_args_preconditioning()
  41. .PreconditionChannelArgs(server_args));
  42. f = config.create_fixture(client_args, server_args);
  43. config.init_server(&f, server_args);
  44. config.init_client(&f, client_args);
  45. grpc_channel_args_destroy(client_args);
  46. grpc_channel_args_destroy(server_args);
  47. return f;
  48. }
  49. static gpr_timespec n_seconds_from_now(int n) {
  50. return grpc_timeout_seconds_to_deadline(n);
  51. }
  52. static gpr_timespec five_seconds_from_now(void) {
  53. return n_seconds_from_now(5);
  54. }
  55. static void drain_cq(grpc_completion_queue* cq) {
  56. grpc_event ev;
  57. do {
  58. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  59. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  60. }
  61. static void shutdown_server(grpc_end2end_test_fixture* f) {
  62. if (!f->server) return;
  63. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  64. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  65. grpc_timeout_seconds_to_deadline(5),
  66. nullptr)
  67. .type == GRPC_OP_COMPLETE);
  68. grpc_server_destroy(f->server);
  69. f->server = nullptr;
  70. }
  71. static void shutdown_client(grpc_end2end_test_fixture* f) {
  72. if (!f->client) return;
  73. grpc_channel_destroy(f->client);
  74. f->client = nullptr;
  75. }
  76. static void end_test(grpc_end2end_test_fixture* f) {
  77. shutdown_server(f);
  78. shutdown_client(f);
  79. grpc_completion_queue_shutdown(f->cq);
  80. drain_cq(f->cq);
  81. grpc_completion_queue_destroy(f->cq);
  82. grpc_completion_queue_destroy(f->shutdown_cq);
  83. }
  84. /* Request/response with metadata and payload.*/
  85. static void test_request_response_with_metadata_and_payload(
  86. grpc_end2end_test_config config) {
  87. grpc_call* c;
  88. grpc_call* s;
  89. grpc_slice request_payload_slice =
  90. grpc_slice_from_copied_string("hello world");
  91. grpc_slice response_payload_slice =
  92. grpc_slice_from_copied_string("hello you");
  93. grpc_byte_buffer* request_payload =
  94. grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  95. grpc_byte_buffer* response_payload =
  96. grpc_raw_byte_buffer_create(&response_payload_slice, 1);
  97. grpc_metadata meta_c[2] = {
  98. {grpc_slice_from_static_string("key1-bin"),
  99. grpc_slice_from_static_string(
  100. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc"),
  101. {{nullptr, nullptr, nullptr, nullptr}}},
  102. {grpc_slice_from_static_string("key2-bin"),
  103. grpc_slice_from_static_string(
  104. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d"),
  105. {{nullptr, nullptr, nullptr, nullptr}}}};
  106. grpc_metadata meta_s[2] = {
  107. {grpc_slice_from_static_string("key3-bin"),
  108. grpc_slice_from_static_string(
  109. "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee"),
  110. {{nullptr, nullptr, nullptr, nullptr}}},
  111. {grpc_slice_from_static_string("key4-bin"),
  112. grpc_slice_from_static_string(
  113. "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"),
  114. {{nullptr, nullptr, nullptr, nullptr}}}};
  115. grpc_end2end_test_fixture f =
  116. begin_test(config, "test_request_response_with_metadata_and_payload",
  117. nullptr, nullptr);
  118. cq_verifier* cqv = cq_verifier_create(f.cq);
  119. grpc_op ops[6];
  120. grpc_op* op;
  121. grpc_metadata_array initial_metadata_recv;
  122. grpc_metadata_array trailing_metadata_recv;
  123. grpc_metadata_array request_metadata_recv;
  124. grpc_byte_buffer* request_payload_recv = nullptr;
  125. grpc_byte_buffer* response_payload_recv = nullptr;
  126. grpc_call_details call_details;
  127. grpc_status_code status;
  128. grpc_call_error error;
  129. grpc_slice details;
  130. int was_cancelled = 2;
  131. gpr_timespec deadline = five_seconds_from_now();
  132. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  133. grpc_slice_from_static_string("/foo"), nullptr,
  134. deadline, nullptr);
  135. GPR_ASSERT(c);
  136. grpc_metadata_array_init(&initial_metadata_recv);
  137. grpc_metadata_array_init(&trailing_metadata_recv);
  138. grpc_metadata_array_init(&request_metadata_recv);
  139. grpc_call_details_init(&call_details);
  140. memset(ops, 0, sizeof(ops));
  141. op = ops;
  142. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  143. op->data.send_initial_metadata.count = 2;
  144. op->data.send_initial_metadata.metadata = meta_c;
  145. op->flags = 0;
  146. op->reserved = nullptr;
  147. op++;
  148. op->op = GRPC_OP_SEND_MESSAGE;
  149. op->data.send_message.send_message = request_payload;
  150. op->flags = 0;
  151. op->reserved = nullptr;
  152. op++;
  153. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  154. op->flags = 0;
  155. op->reserved = nullptr;
  156. op++;
  157. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  158. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  159. op->flags = 0;
  160. op->reserved = nullptr;
  161. op++;
  162. op->op = GRPC_OP_RECV_MESSAGE;
  163. op->data.recv_message.recv_message = &response_payload_recv;
  164. op->flags = 0;
  165. op->reserved = nullptr;
  166. op++;
  167. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  168. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  169. op->data.recv_status_on_client.status = &status;
  170. op->data.recv_status_on_client.status_details = &details;
  171. op->flags = 0;
  172. op->reserved = nullptr;
  173. op++;
  174. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  175. nullptr);
  176. GPR_ASSERT(GRPC_CALL_OK == error);
  177. error =
  178. grpc_server_request_call(f.server, &s, &call_details,
  179. &request_metadata_recv, f.cq, f.cq, tag(101));
  180. GPR_ASSERT(GRPC_CALL_OK == error);
  181. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  182. cq_verify(cqv);
  183. memset(ops, 0, sizeof(ops));
  184. op = ops;
  185. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  186. op->data.send_initial_metadata.count = 2;
  187. op->data.send_initial_metadata.metadata = meta_s;
  188. op->flags = 0;
  189. op->reserved = nullptr;
  190. op++;
  191. op->op = GRPC_OP_RECV_MESSAGE;
  192. op->data.recv_message.recv_message = &request_payload_recv;
  193. op->flags = 0;
  194. op->reserved = nullptr;
  195. op++;
  196. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  197. nullptr);
  198. GPR_ASSERT(GRPC_CALL_OK == error);
  199. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  200. cq_verify(cqv);
  201. memset(ops, 0, sizeof(ops));
  202. op = ops;
  203. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  204. op->data.recv_close_on_server.cancelled = &was_cancelled;
  205. op->flags = 0;
  206. op->reserved = nullptr;
  207. op++;
  208. op->op = GRPC_OP_SEND_MESSAGE;
  209. op->data.send_message.send_message = response_payload;
  210. op->flags = 0;
  211. op->reserved = nullptr;
  212. op++;
  213. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  214. op->data.send_status_from_server.trailing_metadata_count = 0;
  215. op->data.send_status_from_server.status = GRPC_STATUS_OK;
  216. grpc_slice status_string = grpc_slice_from_static_string(
  217. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12"
  218. "\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24"
  219. "\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36"
  220. "\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48"
  221. "\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a"
  222. "\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c"
  223. "\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e"
  224. "\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
  225. "\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2"
  226. "\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4"
  227. "\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6"
  228. "\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8"
  229. "\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea"
  230. "\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc"
  231. "\xfd\xfe\xff");
  232. op->data.send_status_from_server.status_details = &status_string;
  233. op->flags = 0;
  234. op->reserved = nullptr;
  235. op++;
  236. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
  237. nullptr);
  238. GPR_ASSERT(GRPC_CALL_OK == error);
  239. CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
  240. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  241. cq_verify(cqv);
  242. GPR_ASSERT(status == GRPC_STATUS_OK);
  243. GPR_ASSERT(
  244. 0 ==
  245. grpc_slice_str_cmp(
  246. details,
  247. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
  248. "\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
  249. "\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30"
  250. "\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40"
  251. "\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50"
  252. "\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60"
  253. "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70"
  254. "\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80"
  255. "\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
  256. "\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0"
  257. "\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0"
  258. "\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0"
  259. "\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0"
  260. "\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0"
  261. "\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0"
  262. "\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"));
  263. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  264. GPR_ASSERT(was_cancelled == 0);
  265. GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
  266. GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you"));
  267. GPR_ASSERT(contains_metadata(
  268. &request_metadata_recv, "key1-bin",
  269. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc"));
  270. GPR_ASSERT(contains_metadata(
  271. &request_metadata_recv, "key2-bin",
  272. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d"));
  273. GPR_ASSERT(contains_metadata(
  274. &initial_metadata_recv, "key3-bin",
  275. "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee"));
  276. GPR_ASSERT(contains_metadata(
  277. &initial_metadata_recv, "key4-bin",
  278. "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"));
  279. grpc_slice_unref(details);
  280. grpc_metadata_array_destroy(&initial_metadata_recv);
  281. grpc_metadata_array_destroy(&trailing_metadata_recv);
  282. grpc_metadata_array_destroy(&request_metadata_recv);
  283. grpc_call_details_destroy(&call_details);
  284. grpc_call_unref(c);
  285. grpc_call_unref(s);
  286. cq_verifier_destroy(cqv);
  287. grpc_byte_buffer_destroy(request_payload);
  288. grpc_byte_buffer_destroy(response_payload);
  289. grpc_byte_buffer_destroy(request_payload_recv);
  290. grpc_byte_buffer_destroy(response_payload_recv);
  291. end_test(&f);
  292. config.tear_down_data(&f);
  293. }
  294. void binary_metadata(grpc_end2end_test_config config) {
  295. test_request_response_with_metadata_and_payload(config);
  296. }
  297. void binary_metadata_pre_init(void) {}