Debian Codegraph @

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