| 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 <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include <grpc/byte_buffer.h> |
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 27 |
#include <grpc/slice.h> |
| 28 |
#include <grpc/status.h> |
| 29 |
#include <grpc/support/alloc.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
|
| 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 |
f = config.create_fixture(client_args, server_args); |
| 45 |
config.init_server(&f, server_args); |
| 46 |
config.init_client(&f, client_args); |
| 47 |
return f; |
| 48 |
} |
| 49 |
|
| 50 |
static gpr_timespec n_seconds_from_now(int n) { |
| 51 |
return grpc_timeout_seconds_to_deadline(n); |
| 52 |
} |
| 53 |
|
| 54 |
static gpr_timespec five_seconds_from_now(void) { |
| 55 |
return n_seconds_from_now(5); |
| 56 |
} |
| 57 |
|
| 58 |
static void drain_cq(grpc_completion_queue* cq) { |
| 59 |
grpc_event ev; |
| 60 |
do { |
| 61 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 62 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 63 |
} |
| 64 |
|
| 65 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 66 |
if (!f->server) return; |
| 67 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 68 |
grpc_event ev; |
| 69 |
do { |
| 70 |
ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5), |
| 71 |
nullptr); |
| 72 |
} while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000)); |
| 73 |
grpc_server_destroy(f->server); |
| 74 |
f->server = nullptr; |
| 75 |
} |
| 76 |
|
| 77 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 78 |
if (!f->client) return; |
| 79 |
grpc_channel_destroy(f->client); |
| 80 |
f->client = nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 84 |
shutdown_server(f); |
| 85 |
shutdown_client(f); |
| 86 |
|
| 87 |
grpc_completion_queue_shutdown(f->cq); |
| 88 |
drain_cq(f->cq); |
| 89 |
grpc_completion_queue_destroy(f->cq); |
| 90 |
} |
| 91 |
|
| 92 |
/* Creates and returns a grpc_slice containing random alphanumeric characters. |
| 93 |
*/ |
| 94 |
static grpc_slice generate_random_slice() { |
| 95 |
size_t i; |
| 96 |
static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890"; |
| 97 |
char* output; |
| 98 |
const size_t output_size = 1024 * 1024; |
| 99 |
output = static_cast<char*>(gpr_malloc(output_size)); |
| 100 |
for (i = 0; i < output_size - 1; ++i) { |
| 101 |
output[i] = chars[rand() % static_cast<int>(sizeof(chars) - 1)]; |
| 102 |
} |
| 103 |
output[output_size - 1] = '\0'; |
| 104 |
grpc_slice out = grpc_slice_from_copied_string(output); |
| 105 |
gpr_free(output); |
| 106 |
return out; |
| 107 |
} |
| 108 |
|
| 109 |
static void request_response_with_payload(grpc_end2end_test_config /*config*/, |
| 110 |
grpc_end2end_test_fixture f) { |
| 111 |
/* Create large request and response bodies. These are big enough to require |
| 112 |
* multiple round trips to deliver to the peer, and their exact contents of |
| 113 |
* will be verified on completion. */ |
| 114 |
grpc_slice request_payload_slice = generate_random_slice(); |
| 115 |
grpc_slice response_payload_slice = generate_random_slice(); |
| 116 |
|
| 117 |
grpc_call* c; |
| 118 |
grpc_call* s; |
| 119 |
grpc_byte_buffer* request_payload = |
| 120 |
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 121 |
grpc_byte_buffer* response_payload = |
| 122 |
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
| 123 |
grpc_core::CqVerifier cqv(f.cq); |
| 124 |
grpc_op ops[6]; |
| 125 |
grpc_op* op; |
| 126 |
grpc_metadata_array initial_metadata_recv; |
| 127 |
grpc_metadata_array trailing_metadata_recv; |
| 128 |
grpc_metadata_array request_metadata_recv; |
| 129 |
grpc_byte_buffer* request_payload_recv = nullptr; |
| 130 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 131 |
grpc_call_details call_details; |
| 132 |
grpc_status_code status; |
| 133 |
grpc_call_error error; |
| 134 |
grpc_slice details; |
| 135 |
int was_cancelled = 2; |
| 136 |
|
| 137 |
gpr_timespec deadline = n_seconds_from_now(60); |
| 138 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 139 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 140 |
deadline, nullptr); |
| 141 |
GPR_ASSERT(c); |
| 142 |
|
| 143 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 144 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 145 |
grpc_metadata_array_init(&request_metadata_recv); |
| 146 |
grpc_call_details_init(&call_details); |
| 147 |
|
| 148 |
memset(ops, 0, sizeof(ops)); |
| 149 |
op = ops; |
| 150 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 151 |
op->data.send_initial_metadata.count = 0; |
| 152 |
op->flags = 0; |
| 153 |
op->reserved = nullptr; |
| 154 |
op++; |
| 155 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 156 |
op->data.send_message.send_message = request_payload; |
| 157 |
op->flags = 0; |
| 158 |
op->reserved = nullptr; |
| 159 |
op++; |
| 160 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 161 |
op->flags = 0; |
| 162 |
op->reserved = nullptr; |
| 163 |
op++; |
| 164 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 165 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 166 |
op->flags = 0; |
| 167 |
op->reserved = nullptr; |
| 168 |
op++; |
| 169 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 170 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 171 |
op->flags = 0; |
| 172 |
op->reserved = nullptr; |
| 173 |
op++; |
| 174 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 175 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 176 |
op->data.recv_status_on_client.status = &status; |
| 177 |
op->data.recv_status_on_client.status_details = &details; |
| 178 |
op->flags = 0; |
| 179 |
op->reserved = nullptr; |
| 180 |
op++; |
| 181 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 182 |
nullptr); |
| 183 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 184 |
|
| 185 |
error = |
| 186 |
grpc_server_request_call(f.server, &s, &call_details, |
| 187 |
&request_metadata_recv, f.cq, f.cq, tag(101)); |
| 188 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 189 |
cqv.Expect(tag(101), true); |
| 190 |
cqv.Verify(); |
| 191 |
|
| 192 |
memset(ops, 0, sizeof(ops)); |
| 193 |
op = ops; |
| 194 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 195 |
op->data.send_initial_metadata.count = 0; |
| 196 |
op->flags = 0; |
| 197 |
op->reserved = nullptr; |
| 198 |
op++; |
| 199 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 200 |
op->data.recv_message.recv_message = &request_payload_recv; |
| 201 |
op->flags = 0; |
| 202 |
op->reserved = nullptr; |
| 203 |
op++; |
| 204 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102), |
| 205 |
nullptr); |
| 206 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 207 |
|
| 208 |
cqv.Expect(tag(102), true); |
| 209 |
cqv.Verify(); |
| 210 |
|
| 211 |
memset(ops, 0, sizeof(ops)); |
| 212 |
op = ops; |
| 213 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 214 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 215 |
op->flags = 0; |
| 216 |
op->reserved = nullptr; |
| 217 |
op++; |
| 218 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 219 |
op->data.send_message.send_message = response_payload; |
| 220 |
op->flags = 0; |
| 221 |
op->reserved = nullptr; |
| 222 |
op++; |
| 223 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 224 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 225 |
op->data.send_status_from_server.status = GRPC_STATUS_OK; |
| 226 |
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 227 |
op->data.send_status_from_server.status_details = &status_details; |
| 228 |
op->flags = 0; |
| 229 |
op->reserved = nullptr; |
| 230 |
op++; |
| 231 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103), |
| 232 |
nullptr); |
| 233 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 234 |
|
| 235 |
cqv.Expect(tag(103), true); |
| 236 |
cqv.Expect(tag(1), true); |
| 237 |
cqv.Verify(); |
| 238 |
|
| 239 |
GPR_ASSERT(status == GRPC_STATUS_OK); |
| 240 |
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
| 241 |
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
| 242 |
GPR_ASSERT(was_cancelled == 0); |
| 243 |
GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); |
| 244 |
GPR_ASSERT( |
| 245 |
byte_buffer_eq_slice(response_payload_recv, response_payload_slice)); |
| 246 |
|
| 247 |
grpc_slice_unref(details); |
| 248 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 249 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 250 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 251 |
grpc_call_details_destroy(&call_details); |
| 252 |
|
| 253 |
grpc_call_unref(c); |
| 254 |
grpc_call_unref(s); |
| 255 |
|
| 256 |
grpc_byte_buffer_destroy(request_payload); |
| 257 |
grpc_byte_buffer_destroy(response_payload); |
| 258 |
grpc_byte_buffer_destroy(request_payload_recv); |
| 259 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 260 |
} |
| 261 |
|
| 262 |
/* Client sends a request with payload, server reads then returns a response |
| 263 |
payload and status. */ |
| 264 |
static void test_invoke_request_response_with_payload( |
| 265 |
grpc_end2end_test_config config) { |
| 266 |
grpc_end2end_test_fixture f = begin_test( |
| 267 |
config, "test_invoke_request_response_with_payload", nullptr, nullptr); |
| 268 |
request_response_with_payload(config, f); |
| 269 |
end_test(&f); |
| 270 |
config.tear_down_data(&f); |
| 271 |
} |
| 272 |
|
| 273 |
static void test_invoke_10_request_response_with_payload( |
| 274 |
grpc_end2end_test_config config) { |
| 275 |
int i; |
| 276 |
grpc_end2end_test_fixture f = begin_test( |
| 277 |
config, "test_invoke_10_request_response_with_payload", nullptr, nullptr); |
| 278 |
for (i = 0; i < 10; i++) { |
| 279 |
request_response_with_payload(config, f); |
| 280 |
} |
| 281 |
end_test(&f); |
| 282 |
config.tear_down_data(&f); |
| 283 |
} |
| 284 |
|
| 285 |
void payload(grpc_end2end_test_config config) { |
| 286 |
test_invoke_request_response_with_payload(config); |
| 287 |
test_invoke_10_request_response_with_payload(config); |
| 288 |
} |
| 289 |
|
| 290 |
void payload_pre_init(void) {} |