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