| 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 |
|
| 21 |
#include <grpc/byte_buffer.h> |
| 22 |
#include <grpc/grpc.h> |
| 23 |
#include <grpc/support/log.h> |
| 24 |
|
| 25 |
#include "src/core/lib/surface/server.h" |
| 26 |
#include "test/core/bad_client/bad_client.h" |
| 27 |
#include "test/core/end2end/cq_verifier.h" |
| 28 |
#include "test/core/util/test_config.h" |
| 29 |
|
| 30 |
#define PFX_STR \ |
| 31 |
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \ |
| 32 |
"\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \ |
| 33 |
"\x00\x00\xd0\x01\x04\x00\x00\x00\x01" \ |
| 34 |
"\x10\x05:path\x0f/registered/bar" \ |
| 35 |
"\x10\x07:scheme\x04http" \ |
| 36 |
"\x10\x07:method\x04POST" \ |
| 37 |
"\x10\x0a:authority\x09localhost" \ |
| 38 |
"\x10\x0c" \ |
| 39 |
"content-type\x10" \ |
| 40 |
"application/grpc" \ |
| 41 |
"\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \ |
| 42 |
"\x10\x02te\x08trailers" \ |
| 43 |
"\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" |
| 44 |
|
| 45 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 46 |
|
| 47 |
static void verifier_succeeds(grpc_server* server, grpc_completion_queue* cq, |
| 48 |
void* registered_method) { |
| 49 |
grpc_call_error error; |
| 50 |
grpc_call* s; |
| 51 |
grpc_core::CqVerifier cqv(cq); |
| 52 |
grpc_metadata_array request_metadata_recv; |
| 53 |
gpr_timespec deadline; |
| 54 |
grpc_byte_buffer* payload = nullptr; |
| 55 |
|
| 56 |
grpc_metadata_array_init(&request_metadata_recv); |
| 57 |
|
| 58 |
error = grpc_server_request_registered_call(server, registered_method, &s, |
| 59 |
&deadline, &request_metadata_recv, |
| 60 |
&payload, cq, cq, tag(101)); |
| 61 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 62 |
cqv.Expect(tag(101), true); |
| 63 |
cqv.Verify(); |
| 64 |
|
| 65 |
GPR_ASSERT(payload != nullptr); |
| 66 |
|
| 67 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 68 |
grpc_call_unref(s); |
| 69 |
grpc_byte_buffer_destroy(payload); |
| 70 |
} |
| 71 |
|
| 72 |
static void verifier_fails(grpc_server* server, grpc_completion_queue* cq, |
| 73 |
void* /*registered_method*/) { |
| 74 |
while (grpc_core::Server::FromC(server)->HasOpenConnections()) { |
| 75 |
GPR_ASSERT(grpc_completion_queue_next( |
| 76 |
cq, grpc_timeout_milliseconds_to_deadline(20), nullptr) |
| 77 |
.type == GRPC_QUEUE_TIMEOUT); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
int main(int argc, char** argv) { |
| 82 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 83 |
grpc_init(); |
| 84 |
|
| 85 |
/* body generated with |
| 86 |
* tools/codegen/core/gen_server_registered_method_bad_client_test_body.py */ |
| 87 |
GRPC_RUN_BAD_CLIENT_TEST(verifier_fails, nullptr, |
| 88 |
PFX_STR "\x00\x00\x00\x00\x00\x00\x00\x00\x01", |
| 89 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 90 |
GRPC_RUN_BAD_CLIENT_TEST(verifier_fails, nullptr, |
| 91 |
PFX_STR "\x00\x00\x01\x00\x00\x00\x00\x00\x01\x00", |
| 92 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 93 |
GRPC_RUN_BAD_CLIENT_TEST(verifier_fails, nullptr, |
| 94 |
PFX_STR |
| 95 |
"\x00\x00\x02\x00\x00\x00\x00\x00\x01\x00\x00", |
| 96 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 97 |
GRPC_RUN_BAD_CLIENT_TEST(verifier_fails, nullptr, |
| 98 |
PFX_STR |
| 99 |
"\x00\x00\x03\x00\x00\x00\x00\x00\x01\x00\x00\x00", |
| 100 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 101 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 102 |
verifier_fails, nullptr, |
| 103 |
PFX_STR "\x00\x00\x04\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00", |
| 104 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 105 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 106 |
verifier_succeeds, nullptr, |
| 107 |
PFX_STR "\x00\x00\x05\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00", 0); |
| 108 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 109 |
verifier_fails, nullptr, |
| 110 |
PFX_STR "\x00\x00\x05\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01", |
| 111 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 112 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 113 |
verifier_succeeds, nullptr, |
| 114 |
PFX_STR "\x00\x00\x06\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00", |
| 115 |
0); |
| 116 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 117 |
verifier_fails, nullptr, |
| 118 |
PFX_STR "\x00\x00\x05\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x02", |
| 119 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 120 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 121 |
verifier_fails, nullptr, |
| 122 |
PFX_STR "\x00\x00\x06\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x02\x00", |
| 123 |
GRPC_BAD_CLIENT_DISCONNECT); |
| 124 |
GRPC_RUN_BAD_CLIENT_TEST( |
| 125 |
verifier_succeeds, nullptr, |
| 126 |
PFX_STR |
| 127 |
"\x00\x00\x07\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x02\x00\x00", |
| 128 |
0); |
| 129 |
|
| 130 |
grpc_shutdown(); |
| 131 |
return 0; |
| 132 |
} |