1 // Copyright 2023 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdint.h>
16 #include <string.h>
17
18 #include <algorithm>
19
20 #include <grpc/grpc.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23
24 #include "src/core/lib/surface/server.h"
25 #include "test/core/bad_client/bad_client.h"
26 #include "test/core/util/test_config.h"
27
28 #define PFX_STR \
29 "\x00\x00\x00\x04\x01\x00\x00\x00\x00" \
30 "\x00\x00\xc9\x01\x04\x00\x00\x00\x01" /* headers: generated from \
31 simple_request.headers in this \
32 directory */ \
33 "\x10\x05:path\x08/foo/bar" \
34 "\x10\x07:scheme\x04http" \
35 "\x10\x07:method\x04POST" \
36 "\x10\x0a:authority\x09localhost" \
37 "\x10\x0c" \
38 "content-type\x10" \
39 "application/grpc" \
40 "\x10\x14grpc-accept-encoding\x15" \
41 "deflate,identity,gzip" \
42 "\x10\x02te\x08trailers" \
43 "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
44
verifier(grpc_server * server,grpc_completion_queue * cq,void *)45 static void verifier(grpc_server* server, grpc_completion_queue* cq,
46 void* /*registered_method*/) {
47 while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
48 GPR_ASSERT(grpc_completion_queue_next(
49 cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
50 .type == GRPC_QUEUE_TIMEOUT);
51 }
52 }
53
54 char* g_buffer;
55 size_t g_cap = 0;
56 size_t g_count = 0;
57
addbuf(const void * data,size_t len)58 static void addbuf(const void* data, size_t len) {
59 if (g_count + len > g_cap) {
60 g_cap = std::max(g_count + len, g_cap * 2);
61 g_buffer = static_cast<char*>(gpr_realloc(g_buffer, g_cap));
62 }
63 memcpy(g_buffer + g_count, data, len);
64 g_count += len;
65 }
66
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68 int i, j;
69 #define MAX_FRAME_SIZE 16384
70 #define MESSAGES_PER_FRAME (MAX_FRAME_SIZE / 5)
71 #define FRAME_SIZE (MESSAGES_PER_FRAME * 5)
72 #define SEND_SIZE (4 * 1024 * 1024)
73 #define NUM_FRAMES (SEND_SIZE / FRAME_SIZE + 1)
74 grpc::testing::TestEnvironment env(&argc, argv);
75 grpc_init();
76
77 addbuf(PFX_STR, sizeof(PFX_STR) - 1);
78 for (i = 0; i < NUM_FRAMES; i++) {
79 uint8_t hdr[9] = {static_cast<uint8_t>(FRAME_SIZE >> 16),
80 static_cast<uint8_t>(FRAME_SIZE >> 8),
81 static_cast<uint8_t>
82 FRAME_SIZE,
83 0,
84 0,
85 0,
86 0,
87 0,
88 1};
89 addbuf(hdr, sizeof(hdr));
90 for (j = 0; j < MESSAGES_PER_FRAME; j++) {
91 uint8_t message[5] = {0, 0, 0, 0, 0};
92 addbuf(message, sizeof(message));
93 }
94 }
95 grpc_bad_client_arg bca[2];
96 bca[0] = connection_preface_arg;
97 bca[1] = {rst_stream_client_validator, nullptr, g_buffer, g_count};
98 grpc_run_bad_client_test(verifier, bca, 2, GRPC_BAD_CLIENT_LARGE_REQUEST);
99 gpr_free(g_buffer);
100 grpc_shutdown();
101
102 return 0;
103 }
104