• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
16 #include <grpc/support/alloc.h>
17 #include <stdint.h>
18 #include <string.h>
19 
20 #include <algorithm>
21 
22 #include "absl/log/check.h"
23 #include "src/core/server/server.h"
24 #include "test/core/bad_client/bad_client.h"
25 #include "test/core/test_util/test_config.h"
26 
27 #define PFX_STR                                                            \
28   "\x00\x00\x00\x04\x01\x00\x00\x00\x00"                                   \
29   "\x00\x00\xc9\x01\x04\x00\x00\x00\x01" /* headers: generated from        \
30                                             simple_request.headers in this \
31                                             directory */                   \
32   "\x10\x05:path\x08/foo/bar"                                              \
33   "\x10\x07:scheme\x04http"                                                \
34   "\x10\x07:method\x04POST"                                                \
35   "\x10\x0a:authority\x09localhost"                                        \
36   "\x10\x0c"                                                               \
37   "content-type\x10"                                                       \
38   "application/grpc"                                                       \
39   "\x10\x14grpc-accept-encoding\x15"                                       \
40   "deflate,identity,gzip"                                                  \
41   "\x10\x02te\x08trailers"                                                 \
42   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
43 
verifier(grpc_server * server,grpc_completion_queue * cq,void *)44 static void verifier(grpc_server* server, grpc_completion_queue* cq,
45                      void* /*registered_method*/) {
46   while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
47     CHECK(grpc_completion_queue_next(
48               cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
49               .type == GRPC_QUEUE_TIMEOUT);
50   }
51 }
52 
53 char* g_buffer;
54 size_t g_cap = 0;
55 size_t g_count = 0;
56 
addbuf(const void * data,size_t len)57 static void addbuf(const void* data, size_t len) {
58   if (g_count + len > g_cap) {
59     g_cap = std::max(g_count + len, g_cap * 2);
60     g_buffer = static_cast<char*>(gpr_realloc(g_buffer, g_cap));
61   }
62   memcpy(g_buffer + g_count, data, len);
63   g_count += len;
64 }
65 
main(int argc,char ** argv)66 int main(int argc, char** argv) {
67   int i, j;
68 #define MAX_FRAME_SIZE 16384
69 #define MESSAGES_PER_FRAME (MAX_FRAME_SIZE / 5)
70 #define FRAME_SIZE (MESSAGES_PER_FRAME * 5)
71 #define SEND_SIZE (4 * 1024 * 1024)
72 #define NUM_FRAMES (SEND_SIZE / FRAME_SIZE + 1)
73   grpc::testing::TestEnvironment env(&argc, argv);
74   grpc_init();
75 
76   addbuf(PFX_STR, sizeof(PFX_STR) - 1);
77   for (i = 0; i < NUM_FRAMES; i++) {
78     uint8_t hdr[9] = {static_cast<uint8_t>(FRAME_SIZE >> 16),
79                       static_cast<uint8_t>(FRAME_SIZE >> 8),
80                       static_cast<uint8_t>
81                           FRAME_SIZE,
82                       0,
83                       0,
84                       0,
85                       0,
86                       0,
87                       1};
88     addbuf(hdr, sizeof(hdr));
89     for (j = 0; j < MESSAGES_PER_FRAME; j++) {
90       uint8_t message[5] = {0, 0, 0, 0, 0};
91       addbuf(message, sizeof(message));
92     }
93   }
94   grpc_bad_client_arg bca[2];
95   bca[0] = connection_preface_arg;
96   bca[1] = {rst_stream_client_validator, nullptr, g_buffer, g_count};
97   grpc_run_bad_client_test(verifier, bca, 2, GRPC_BAD_CLIENT_LARGE_REQUEST);
98   gpr_free(g_buffer);
99   grpc_shutdown();
100 
101   return 0;
102 }
103