| 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 <algorithm> |
| 23 |
|
| 24 |
#include <grpc/byte_buffer.h> |
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/support/alloc.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
|
| 29 |
#include "test/core/bad_client/bad_client.h" |
| 30 |
#include "test/core/end2end/cq_verifier.h" |
| 31 |
#include "test/core/util/test_config.h" |
| 32 |
|
| 33 |
static const char prefix[] = |
| 34 |
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
| 35 |
// settings frame |
| 36 |
"\x00\x00\x00\x04\x00\x00\x00\x00\x00" |
| 37 |
// stream 1 headers: generated from server_registered_method.headers in this |
| 38 |
// directory |
| 39 |
"\x00\x00\xd0\x01\x04\x00\x00\x00\x01" |
| 40 |
"\x10\x05:path\x0f/registered/bar" |
| 41 |
"\x10\x07:scheme\x04http" |
| 42 |
"\x10\x07:method\x04POST" |
| 43 |
"\x10\x0a:authority\x09localhost" |
| 44 |
"\x10\x0c" |
| 45 |
"content-type\x10" |
| 46 |
"application/grpc" |
| 47 |
"\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" |
| 48 |
"\x10\x02te\x08trailers" |
| 49 |
"\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" |
| 50 |
// data frame for stream 1: advertise a 10000 byte payload (that we won't |
| 51 |
// fulfill) |
| 52 |
"\x00\x00\x05\x00\x00\x00\x00\x00\x01" |
| 53 |
"\x01\x00\x00\x27\x10" |
| 54 |
// stream 3 headers: generated from server_registered_method.headers in this |
| 55 |
// directory |
| 56 |
"\x00\x00\xd0\x01\x04\x00\x00\x00\x03" |
| 57 |
"\x10\x05:path\x0f/registered/bar" |
| 58 |
"\x10\x07:scheme\x04http" |
| 59 |
"\x10\x07:method\x04POST" |
| 60 |
"\x10\x0a:authority\x09localhost" |
| 61 |
"\x10\x0c" |
| 62 |
"content-type\x10" |
| 63 |
"application/grpc" |
| 64 |
"\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" |
| 65 |
"\x10\x02te\x08trailers" |
| 66 |
"\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" |
| 67 |
// data frame for stream 3: advertise a 10000 byte payload (that we will |
| 68 |
// fulfill) |
| 69 |
"\x00\x00\x05\x00\x00\x00\x00\x00\x03" |
| 70 |
"\x01\x00\x00\x27\x10" |
| 71 |
""; |
| 72 |
|
| 73 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 74 |
|
| 75 |
static void verifier(grpc_server* server, grpc_completion_queue* cq, |
| 76 |
void* registered_method) { |
| 77 |
grpc_call_error error; |
| 78 |
grpc_call* s; |
| 79 |
grpc_core::CqVerifier cqv(cq); |
| 80 |
grpc_metadata_array request_metadata_recv; |
| 81 |
gpr_timespec deadline; |
| 82 |
grpc_byte_buffer* payload = nullptr; |
| 83 |
|
| 84 |
grpc_metadata_array_init(&request_metadata_recv); |
| 85 |
|
| 86 |
error = grpc_server_request_registered_call(server, registered_method, &s, |
| 87 |
&deadline, &request_metadata_recv, |
| 88 |
&payload, cq, cq, tag(101)); |
| 89 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 90 |
cqv.Expect(tag(101), true); |
| 91 |
cqv.Verify(); |
| 92 |
|
| 93 |
GPR_ASSERT(payload != nullptr); |
| 94 |
|
| 95 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 96 |
grpc_call_unref(s); |
| 97 |
grpc_byte_buffer_destroy(payload); |
| 98 |
} |
| 99 |
|
| 100 |
char* g_buffer; |
| 101 |
size_t g_cap = 0; |
| 102 |
size_t g_count = 0; |
| 103 |
|
| 104 |
static void addbuf(const void* data, size_t len) { |
| 105 |
if (g_count + len > g_cap) { |
| 106 |
g_cap = std::max(g_count + len, g_cap * 2); |
| 107 |
g_buffer = static_cast<char*>(gpr_realloc(g_buffer, g_cap)); |
| 108 |
} |
| 109 |
memcpy(g_buffer + g_count, data, len); |
| 110 |
g_count += len; |
| 111 |
} |
| 112 |
|
| 113 |
int main(int argc, char** argv) { |
| 114 |
int i; |
| 115 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 116 |
grpc_init(); |
| 117 |
|
| 118 |
#define NUM_FRAMES 10 |
| 119 |
#define FRAME_SIZE 1000 |
| 120 |
|
| 121 |
addbuf(prefix, sizeof(prefix) - 1); |
| 122 |
for (i = 0; i < NUM_FRAMES; i++) { |
| 123 |
uint8_t hdr[9] = {static_cast<uint8_t>(FRAME_SIZE >> 16), |
| 124 |
static_cast<uint8_t>(FRAME_SIZE >> 8), |
| 125 |
static_cast<uint8_t>(FRAME_SIZE), |
| 126 |
0, |
| 127 |
0, |
| 128 |
0, |
| 129 |
0, |
| 130 |
0, |
| 131 |
3}; |
| 132 |
uint8_t msg[FRAME_SIZE]; |
| 133 |
memset(msg, 'a', sizeof(msg)); |
| 134 |
addbuf(hdr, sizeof(hdr)); |
| 135 |
addbuf(msg, FRAME_SIZE); |
| 136 |
} |
| 137 |
grpc_bad_client_arg bca = {nullptr, nullptr, g_buffer, g_count}; |
| 138 |
grpc_run_bad_client_test(verifier, &bca, 1, 0); |
| 139 |
gpr_free(g_buffer); |
| 140 |
grpc_shutdown(); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |