1 /*
2 *
3 * Copyright 2018 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 <grpc/grpc.h>
24
25 #include "src/core/lib/surface/server.h"
26 #include "test/core/end2end/cq_verifier.h"
27
28 #define PFX_STR \
29 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
30 "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */
31
32 #define HEADER_STR \
33 "\x00\x00\xc9\x01\x04\x00\x00\x00\x01" /* headers: generated from \
34 simple_request.headers in this \
35 directory */ \
36 "\x10\x05:path\x08/foo/bar" \
37 "\x10\x07:scheme\x04http" \
38 "\x10\x07:method\x04POST" \
39 "\x10\x0a:authority\x09localhost" \
40 "\x10\x0c" \
41 "content-type\x10" \
42 "application/grpc" \
43 "\x10\x14grpc-accept-encoding\x15" \
44 "deflate,identity,gzip" \
45 "\x10\x02te\x08trailers" \
46 "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
47
48 #define PAYLOAD_STR \
49 "\x00\x00\x20\x00\x00\x00\x00\x00\x01" \
50 "\x00\x00\x00\x00"
51
tag(intptr_t t)52 static void* tag(intptr_t t) { return (void*)t; }
53
verifier(grpc_server * server,grpc_completion_queue * cq,void * registered_method)54 static void verifier(grpc_server* server, grpc_completion_queue* cq,
55 void* registered_method) {
56 grpc_call_error error;
57 grpc_call* s;
58 grpc_call_details call_details;
59 grpc_byte_buffer* request_payload_recv = nullptr;
60 grpc_op* op;
61 grpc_op ops[6];
62 cq_verifier* cqv = cq_verifier_create(cq);
63 grpc_metadata_array request_metadata_recv;
64 int was_cancelled = 2;
65
66 grpc_call_details_init(&call_details);
67 grpc_metadata_array_init(&request_metadata_recv);
68
69 error = grpc_server_request_call(server, &s, &call_details,
70 &request_metadata_recv, cq, cq, tag(101));
71 GPR_ASSERT(GRPC_CALL_OK == error);
72 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
73 cq_verify(cqv);
74
75 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
76 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo/bar"));
77
78 memset(ops, 0, sizeof(ops));
79 op = ops;
80 op->op = GRPC_OP_SEND_INITIAL_METADATA;
81 op->data.send_initial_metadata.count = 0;
82 op->flags = 0;
83 op->reserved = nullptr;
84 op++;
85 op->op = GRPC_OP_RECV_MESSAGE;
86 op->data.recv_message.recv_message = &request_payload_recv;
87 op->flags = 0;
88 op->reserved = nullptr;
89 op++;
90 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
91 nullptr);
92 GPR_ASSERT(GRPC_CALL_OK == error);
93
94 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
95 cq_verify(cqv);
96
97 memset(ops, 0, sizeof(ops));
98 op = ops;
99 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
100 op->data.recv_close_on_server.cancelled = &was_cancelled;
101 op->flags = 0;
102 op->reserved = nullptr;
103 op++;
104 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
105 op->data.send_status_from_server.trailing_metadata_count = 0;
106 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
107 grpc_slice status_details = grpc_slice_from_static_string("xyz");
108 op->data.send_status_from_server.status_details = &status_details;
109 op->flags = 0;
110 op->reserved = nullptr;
111 op++;
112 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
113 nullptr);
114 GPR_ASSERT(GRPC_CALL_OK == error);
115
116 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
117
118 grpc_metadata_array_destroy(&request_metadata_recv);
119 grpc_call_details_destroy(&call_details);
120 grpc_call_unref(s);
121 cq_verifier_destroy(cqv);
122 }
123
main(int argc,char ** argv)124 int main(int argc, char** argv) {
125 grpc_test_init(argc, argv);
126 grpc_init();
127
128 /* Verify that sending multiple headers doesn't segfault */
129 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
130 PFX_STR HEADER_STR HEADER_STR PAYLOAD_STR, 0);
131 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
132 PFX_STR HEADER_STR HEADER_STR HEADER_STR PAYLOAD_STR,
133 0);
134 grpc_shutdown();
135 return 0;
136 }
137