Debian Codegraph @

test/core/bad_client/tests/large_metadata.cc

raw
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 <string.h>
20
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24
25 #include "absl/strings/str_format.h"
26 #include "absl/strings/str_join.h"
27
28 #include <grpc/grpc.h>
29 #include <grpc/support/log.h>
30
31 #include "src/core/lib/surface/server.h"
32 #include "test/core/bad_client/bad_client.h"
33 #include "test/core/util/test_config.h"
34
35 // The large-metadata headers that we're adding for this test are not
36 // actually appended to this in a single string, since the string would
37 // be longer than the C99 string literal limit. Instead, we dynamically
38 // construct it by adding the large headers one at a time.
39
40 /* headers: generated from large_metadata.headers in this directory */
41 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST \
42 "\x00\x00\x00\x04\x01\x00\x00\x00\x00" \
43 "\x00" \
44 "5{\x01\x05\x00\x00\x00\x01" \
45 "\x10\x05:path\x08/foo/bar" \
46 "\x10\x07:scheme\x04http" \
47 "\x10\x07:method\x04POST" \
48 "\x10\x0a:authority\x09localhost" \
49 "\x10\x0c" \
50 "content-type\x10" \
51 "application/grpc" \
52 "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \
53 "\x10\x02te\x08trailers" \
54 "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
55
56 // Each large-metadata header is constructed from these start and end
57 // strings, with a two-digit number in between.
58 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR "\x10\x0duser-header"
59 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR \
60 "~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
61 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
62
63 // The size of each large-metadata header string.
64 #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_SIZE \
65 ((sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR) - 1) + 2 + \
66 (sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR) - 1))
67
68 // The number of headers we're adding and the total size of the client
69 // payload.
70 #define NUM_HEADERS 46
71 #define TOO_MUCH_METADATA_FROM_CLIENT_REQUEST_SIZE \
72 ((sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST) - 1) + \
73 (NUM_HEADERS * PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_SIZE) + 1)
74
75 static void verifier_fails(grpc_server* server, grpc_completion_queue* cq,
76 void* /*registered_method*/) {
77 while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
78 GPR_ASSERT(grpc_completion_queue_next(
79 cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
80 .type == GRPC_QUEUE_TIMEOUT);
81 }
82 }
83
84 int main(int argc, char** argv) {
85 int i;
86 grpc_init();
87 grpc::testing::TestEnvironment env(&argc, argv);
88
89 // Test sending more metadata than the server will accept.
90 std::vector<std::string> headers;
91 for (i = 0; i < NUM_HEADERS; ++i) {
92 headers.push_back(absl::StrFormat(
93 "%s%02d%s", PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR, i,
94 PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR));
95 }
96 std::string client_headers = absl::StrJoin(headers, "");
97 char client_payload[TOO_MUCH_METADATA_FROM_CLIENT_REQUEST_SIZE] =
98 PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST;
99 memcpy(client_payload + sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST) - 1,
100 client_headers.data(), client_headers.size());
101 grpc_bad_client_arg args[2];
102 args[0] = connection_preface_arg;
103 args[1].client_validator = rst_stream_client_validator;
104 args[1].client_payload = client_payload;
105 args[1].client_payload_length = sizeof(client_payload) - 1;
106
107 grpc_run_bad_client_test(verifier_fails, args, 2, 0);
108
109 grpc_shutdown();
110 return 0;
111 }