Debian Codegraph @

test/core/bad_client/tests/duplicate_header.cc

raw
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 <stdint.h>
20 #include <string.h>
21
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpc/status.h>
25 #include <grpc/support/log.h>
26
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
55 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
56
57 static void verifier(grpc_server* server, grpc_completion_queue* cq,
58 void* /*registered_method*/) {
59 grpc_call_error error;
60 grpc_call* s;
61 grpc_call_details call_details;
62 grpc_byte_buffer* request_payload_recv = nullptr;
63 grpc_op* op;
64 grpc_op ops[6];
65 grpc_core::CqVerifier cqv(cq);
66 grpc_metadata_array request_metadata_recv;
67 int was_cancelled = 2;
68
69 grpc_call_details_init(&call_details);
70 grpc_metadata_array_init(&request_metadata_recv);
71
72 error = grpc_server_request_call(server, &s, &call_details,
73 &request_metadata_recv, cq, cq, tag(101));
74 GPR_ASSERT(GRPC_CALL_OK == error);
75 cqv.Expect(tag(101), true);
76 cqv.Verify();
77
78 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
79 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo/bar"));
80
81 memset(ops, 0, sizeof(ops));
82 op = ops;
83 op->op = GRPC_OP_SEND_INITIAL_METADATA;
84 op->data.send_initial_metadata.count = 0;
85 op->flags = 0;
86 op->reserved = nullptr;
87 op++;
88 op->op = GRPC_OP_RECV_MESSAGE;
89 op->data.recv_message.recv_message = &request_payload_recv;
90 op->flags = 0;
91 op->reserved = nullptr;
92 op++;
93 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
94 nullptr);
95 GPR_ASSERT(GRPC_CALL_OK == error);
96
97 cqv.Expect(tag(102), grpc_core::CqVerifier::AnyStatus());
98 cqv.Verify();
99
100 memset(ops, 0, sizeof(ops));
101 op = ops;
102 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
103 op->data.recv_close_on_server.cancelled = &was_cancelled;
104 op->flags = 0;
105 op->reserved = nullptr;
106 op++;
107 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
108 op->data.send_status_from_server.trailing_metadata_count = 0;
109 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
110 grpc_slice status_details = grpc_slice_from_static_string("xyz");
111 op->data.send_status_from_server.status_details = &status_details;
112 op->flags = 0;
113 op->reserved = nullptr;
114 op++;
115 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
116 nullptr);
117 GPR_ASSERT(GRPC_CALL_OK == error);
118
119 cqv.Expect(tag(103), true);
120 cqv.Verify();
121
122 grpc_metadata_array_destroy(&request_metadata_recv);
123 grpc_call_details_destroy(&call_details);
124 grpc_call_unref(s);
125 }
126
127 int main(int argc, char** argv) {
128 grpc::testing::TestEnvironment env(&argc, argv);
129 grpc_init();
130
131 /* Verify that sending multiple headers doesn't segfault */
132 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
133 PFX_STR HEADER_STR HEADER_STR PAYLOAD_STR, 0);
134 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
135 PFX_STR HEADER_STR HEADER_STR HEADER_STR PAYLOAD_STR,
136 0);
137 grpc_shutdown();
138 return 0;
139 }