• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/alloc.h>
24 #include <grpc/support/string_util.h>
25 #include "src/core/lib/gpr/string.h"
26 #include "src/core/lib/surface/server.h"
27 #include "test/core/end2end/cq_verifier.h"
28 
29 // The large-metadata headers that we're adding for this test are not
30 // actually appended to this in a single string, since the string would
31 // be longer than the C99 string literal limit.  Instead, we dynamically
32 // construct it by adding the large headers one at a time.
33 
34 /* headers: generated from  large_metadata.headers in this directory */
35 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST         \
36   "\x00\x00\x00\x04\x01\x00\x00\x00\x00"                  \
37   "\x00"                                                  \
38   "5{\x01\x05\x00\x00\x00\x01"                            \
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\x15identity,deflate,gzip" \
47   "\x10\x02te\x08trailers"                                \
48   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
49 
50 // Each large-metadata header is constructed from these start and end
51 // strings, with a two-digit number in between.
52 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR "\x10\x0duser-header"
53 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR                   \
54   "~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
55   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
56 
57 // The size of each large-metadata header string.
58 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_SIZE                     \
59   ((sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR) - 1) + 2 + \
60    (sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR) - 1))
61 
62 // The number of headers we're adding and the total size of the client
63 // payload.
64 #define NUM_HEADERS 46
65 #define TOO_MUCH_METADATA_FROM_CLIENT_REQUEST_SIZE           \
66   ((sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST) - 1) + \
67    (NUM_HEADERS * PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_SIZE) + 1)
68 
69 #define PFX_TOO_MUCH_METADATA_FROM_SERVER_STR                                              \
70   "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" /* settings frame: sets                               \
71                                         MAX_HEADER_LIST_SIZE to 8K */                      \
72   "\x00\x00\x06\x04\x00\x00\x00\x00\x00\x00\x06\x00\x00\x20\x00" /* headers:               \
73                                                                     generated              \
74                                                                     from                   \
75                                                                     simple_request.headers \
76                                                                     in this                \
77                                                                     directory              \
78                                                                     */                     \
79   "\x00\x00\x00\x04\x01\x00\x00\x00\x00"                                                   \
80   "\x00\x00\xc9\x01\x04\x00\x00\x00\x01"                                                   \
81   "\x10\x05:path\x08/foo/bar"                                                              \
82   "\x10\x07:scheme\x04http"                                                                \
83   "\x10\x07:method\x04POST"                                                                \
84   "\x10\x0a:authority\x09localhost"                                                        \
85   "\x10\x0c"                                                                               \
86   "content-type\x10"                                                                       \
87   "application/grpc"                                                                       \
88   "\x10\x14grpc-accept-encoding\x15"                                                       \
89   "deflate,identity,gzip"                                                                  \
90   "\x10\x02te\x08trailers"                                                                 \
91   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
92 
tag(intptr_t t)93 static void* tag(intptr_t t) { return (void*)t; }
94 
server_verifier_sends_too_much_metadata(grpc_server * server,grpc_completion_queue * cq,void * registered_method)95 static void server_verifier_sends_too_much_metadata(grpc_server* server,
96                                                     grpc_completion_queue* cq,
97                                                     void* registered_method) {
98   grpc_call_error error;
99   grpc_call* s;
100   grpc_call_details call_details;
101   cq_verifier* cqv = cq_verifier_create(cq);
102   grpc_metadata_array request_metadata_recv;
103 
104   grpc_call_details_init(&call_details);
105   grpc_metadata_array_init(&request_metadata_recv);
106 
107   error = grpc_server_request_call(server, &s, &call_details,
108                                    &request_metadata_recv, cq, cq, tag(101));
109   GPR_ASSERT(GRPC_CALL_OK == error);
110   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
111   cq_verify(cqv);
112 
113   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
114   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo/bar"));
115 
116   const size_t metadata_value_size = 8 * 1024;
117   grpc_metadata meta;
118   meta.key = grpc_slice_from_static_string("key");
119   meta.value = grpc_slice_malloc(metadata_value_size);
120   memset(GRPC_SLICE_START_PTR(meta.value), 'a', metadata_value_size);
121 
122   grpc_op op;
123   memset(&op, 0, sizeof(op));
124   op.op = GRPC_OP_SEND_INITIAL_METADATA;
125   op.data.send_initial_metadata.count = 1;
126   op.data.send_initial_metadata.metadata = &meta;
127   op.flags = 0;
128   op.reserved = nullptr;
129   error = grpc_call_start_batch(s, &op, 1, tag(102), nullptr);
130   GPR_ASSERT(GRPC_CALL_OK == error);
131   CQ_EXPECT_COMPLETION(cqv, tag(102), 0);  // Operation fails.
132   cq_verify(cqv);
133 
134   grpc_slice_unref(meta.value);
135   grpc_metadata_array_destroy(&request_metadata_recv);
136   grpc_call_details_destroy(&call_details);
137   grpc_call_unref(s);
138   cq_verifier_destroy(cqv);
139 }
140 
main(int argc,char ** argv)141 int main(int argc, char** argv) {
142   int i;
143 
144   grpc_test_init(argc, argv);
145 
146   // Test sending more metadata than the server will accept.
147   gpr_strvec headers;
148   gpr_strvec_init(&headers);
149   for (i = 0; i < NUM_HEADERS; ++i) {
150     char* str;
151     gpr_asprintf(&str, "%s%02d%s",
152                  PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR, i,
153                  PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR);
154     gpr_strvec_add(&headers, str);
155   }
156   size_t headers_len;
157   const char* client_headers = gpr_strvec_flatten(&headers, &headers_len);
158   gpr_strvec_destroy(&headers);
159   char client_payload[TOO_MUCH_METADATA_FROM_CLIENT_REQUEST_SIZE] =
160       PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST;
161   memcpy(client_payload + sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST) - 1,
162          client_headers, headers_len);
163   grpc_bad_client_arg args[2];
164   args[0] = connection_preface_arg;
165   args[1].client_validator = rst_stream_client_validator;
166   args[1].client_payload = client_payload;
167   args[1].client_payload_length = sizeof(client_payload) - 1;
168 
169   grpc_run_bad_client_test(server_verifier_request_call, args, 2, 0);
170   gpr_free((void*)client_headers);
171 
172   // Test sending more metadata than the client will accept.
173   GRPC_RUN_BAD_CLIENT_TEST(server_verifier_sends_too_much_metadata,
174                            rst_stream_client_validator,
175                            PFX_TOO_MUCH_METADATA_FROM_SERVER_STR, 0);
176   return 0;
177 }
178