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