xref: /aosp_15_r20/external/grpc-grpc/test/core/bad_client/tests/head_of_line_blocking.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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/byte_buffer.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29 
30 #include "test/core/bad_client/bad_client.h"
31 #include "test/core/end2end/cq_verifier.h"
32 #include "test/core/util/test_config.h"
33 
34 static const char prefix[] =
35     "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
36     // settings frame
37     "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
38     // stream 1 headers: generated from server_registered_method.headers in this
39     // directory
40     "\x00\x00\xd0\x01\x04\x00\x00\x00\x01"
41     "\x10\x05:path\x0f/registered/bar"
42     "\x10\x07:scheme\x04http"
43     "\x10\x07:method\x04POST"
44     "\x10\x0a:authority\x09localhost"
45     "\x10\x0c"
46     "content-type\x10"
47     "application/grpc"
48     "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip"
49     "\x10\x02te\x08trailers"
50     "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
51     // data frame for stream 1: advertise a 10000 byte payload (that we won't
52     // fulfill)
53     "\x00\x00\x05\x00\x00\x00\x00\x00\x01"
54     "\x01\x00\x00\x27\x10"
55     // stream 3 headers: generated from server_registered_method.headers in this
56     // directory
57     "\x00\x00\xd0\x01\x04\x00\x00\x00\x03"
58     "\x10\x05:path\x0f/registered/bar"
59     "\x10\x07:scheme\x04http"
60     "\x10\x07:method\x04POST"
61     "\x10\x0a:authority\x09localhost"
62     "\x10\x0c"
63     "content-type\x10"
64     "application/grpc"
65     "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip"
66     "\x10\x02te\x08trailers"
67     "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
68     // data frame for stream 3: advertise a 10000 byte payload (that we will
69     // fulfill)
70     "\x00\x00\x05\x00\x00\x00\x00\x00\x03"
71     "\x01\x00\x00\x27\x10"
72     "";
73 
verifier(grpc_server * server,grpc_completion_queue * cq,void * registered_method)74 static void verifier(grpc_server* server, grpc_completion_queue* cq,
75                      void* registered_method) {
76   grpc_call_error error;
77   grpc_call* s;
78   grpc_core::CqVerifier cqv(cq);
79   grpc_metadata_array request_metadata_recv;
80   gpr_timespec deadline;
81   grpc_byte_buffer* payload = nullptr;
82 
83   grpc_metadata_array_init(&request_metadata_recv);
84 
85   error = grpc_server_request_registered_call(
86       server, registered_method, &s, &deadline, &request_metadata_recv,
87       &payload, cq, cq, grpc_core::CqVerifier::tag(101));
88   GPR_ASSERT(GRPC_CALL_OK == error);
89   cqv.Expect(grpc_core::CqVerifier::tag(101), true);
90   cqv.Verify();
91 
92   GPR_ASSERT(payload != nullptr);
93 
94   grpc_metadata_array_destroy(&request_metadata_recv);
95   grpc_call_unref(s);
96   grpc_byte_buffer_destroy(payload);
97 }
98 
99 char* g_buffer;
100 size_t g_cap = 0;
101 size_t g_count = 0;
102 
addbuf(const void * data,size_t len)103 static void addbuf(const void* data, size_t len) {
104   if (g_count + len > g_cap) {
105     g_cap = std::max(g_count + len, g_cap * 2);
106     g_buffer = static_cast<char*>(gpr_realloc(g_buffer, g_cap));
107   }
108   memcpy(g_buffer + g_count, data, len);
109   g_count += len;
110 }
111 
main(int argc,char ** argv)112 int main(int argc, char** argv) {
113   int i;
114   grpc::testing::TestEnvironment env(&argc, argv);
115   grpc_init();
116 
117 #define NUM_FRAMES 10
118 #define FRAME_SIZE 1000
119 
120   addbuf(prefix, sizeof(prefix) - 1);
121   for (i = 0; i < NUM_FRAMES; i++) {
122     uint8_t hdr[9] = {static_cast<uint8_t>(FRAME_SIZE >> 16),
123                       static_cast<uint8_t>(FRAME_SIZE >> 8),
124                       static_cast<uint8_t>(FRAME_SIZE),
125                       0,
126                       0,
127                       0,
128                       0,
129                       0,
130                       3};
131     uint8_t msg[FRAME_SIZE];
132     memset(msg, 'a', sizeof(msg));
133     addbuf(hdr, sizeof(hdr));
134     addbuf(msg, FRAME_SIZE);
135   }
136   grpc_bad_client_arg bca = {nullptr, nullptr, g_buffer, g_count};
137   grpc_run_bad_client_test(verifier, &bca, 1, 0);
138   gpr_free(g_buffer);
139   grpc_shutdown();
140 
141   return 0;
142 }
143