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