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