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