Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "test/core/bad_client/bad_client.h"
35 :
36 : #include "src/core/channel/channel_stack.h"
37 : #include "src/core/channel/http_server_filter.h"
38 : #include "src/core/iomgr/endpoint_pair.h"
39 : #include "src/core/surface/completion_queue.h"
40 : #include "src/core/surface/server.h"
41 : #include "src/core/support/string.h"
42 : #include "src/core/transport/chttp2_transport.h"
43 :
44 : #include <grpc/support/alloc.h>
45 : #include <grpc/support/sync.h>
46 : #include <grpc/support/thd.h>
47 :
48 : typedef struct {
49 : grpc_server *server;
50 : grpc_completion_queue *cq;
51 : grpc_bad_client_server_side_validator validator;
52 : gpr_event done_thd;
53 : gpr_event done_write;
54 : } thd_args;
55 :
56 44 : static void thd_func(void *arg) {
57 44 : thd_args *a = arg;
58 44 : a->validator(a->server, a->cq);
59 44 : gpr_event_set(&a->done_thd, (void *)1);
60 44 : }
61 :
62 44 : static void done_write(grpc_exec_ctx *exec_ctx, void *arg, int success) {
63 44 : thd_args *a = arg;
64 44 : gpr_event_set(&a->done_write, (void *)1);
65 44 : }
66 :
67 44 : static void server_setup_transport(void *ts, grpc_transport *transport,
68 : grpc_mdctx *mdctx) {
69 44 : thd_args *a = ts;
70 : static grpc_channel_filter const *extra_filters[] = {
71 : &grpc_http_server_filter};
72 44 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
73 44 : grpc_server_setup_transport(&exec_ctx, a->server, transport, extra_filters,
74 : GPR_ARRAY_SIZE(extra_filters), mdctx,
75 : grpc_server_get_channel_args(a->server));
76 44 : grpc_exec_ctx_finish(&exec_ctx);
77 44 : }
78 :
79 44 : void grpc_run_bad_client_test(grpc_bad_client_server_side_validator validator,
80 : const char *client_payload,
81 : size_t client_payload_length, gpr_uint32 flags) {
82 : grpc_endpoint_pair sfd;
83 : thd_args a;
84 : gpr_thd_id id;
85 : char *hex;
86 : grpc_transport *transport;
87 44 : grpc_mdctx *mdctx = grpc_mdctx_create();
88 44 : gpr_slice slice =
89 : gpr_slice_from_copied_buffer(client_payload, client_payload_length);
90 : gpr_slice_buffer outgoing;
91 : grpc_closure done_write_closure;
92 44 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
93 :
94 44 : hex = gpr_dump(client_payload, client_payload_length,
95 : GPR_DUMP_HEX | GPR_DUMP_ASCII);
96 :
97 : /* Add a debug log */
98 44 : gpr_log(GPR_INFO, "TEST: %s", hex);
99 :
100 44 : gpr_free(hex);
101 :
102 : /* Init grpc */
103 44 : grpc_init();
104 :
105 : /* Create endpoints */
106 44 : sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536);
107 :
108 : /* Create server, completion events */
109 44 : a.server = grpc_server_create_from_filters(NULL, 0, NULL);
110 44 : a.cq = grpc_completion_queue_create(NULL);
111 44 : gpr_event_init(&a.done_thd);
112 44 : gpr_event_init(&a.done_write);
113 44 : a.validator = validator;
114 44 : grpc_server_register_completion_queue(a.server, a.cq, NULL);
115 44 : grpc_server_start(a.server);
116 44 : transport =
117 44 : grpc_create_chttp2_transport(&exec_ctx, NULL, sfd.server, mdctx, 0);
118 44 : server_setup_transport(&a, transport, mdctx);
119 44 : grpc_chttp2_transport_start_reading(&exec_ctx, transport, NULL, 0);
120 44 : grpc_exec_ctx_finish(&exec_ctx);
121 :
122 : /* Bind everything into the same pollset */
123 44 : grpc_endpoint_add_to_pollset(&exec_ctx, sfd.client, grpc_cq_pollset(a.cq));
124 44 : grpc_endpoint_add_to_pollset(&exec_ctx, sfd.server, grpc_cq_pollset(a.cq));
125 :
126 : /* Check a ground truth */
127 44 : GPR_ASSERT(grpc_server_has_open_connections(a.server));
128 :
129 : /* Start validator */
130 44 : gpr_thd_new(&id, thd_func, &a, NULL);
131 :
132 44 : gpr_slice_buffer_init(&outgoing);
133 44 : gpr_slice_buffer_add(&outgoing, slice);
134 44 : grpc_closure_init(&done_write_closure, done_write, &a);
135 :
136 : /* Write data */
137 44 : grpc_endpoint_write(&exec_ctx, sfd.client, &outgoing, &done_write_closure);
138 44 : grpc_exec_ctx_finish(&exec_ctx);
139 :
140 : /* Await completion */
141 44 : GPR_ASSERT(
142 : gpr_event_wait(&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
143 :
144 44 : if (flags & GRPC_BAD_CLIENT_DISCONNECT) {
145 13 : grpc_endpoint_shutdown(&exec_ctx, sfd.client);
146 13 : grpc_endpoint_destroy(&exec_ctx, sfd.client);
147 13 : grpc_exec_ctx_finish(&exec_ctx);
148 13 : sfd.client = NULL;
149 : }
150 :
151 44 : GPR_ASSERT(gpr_event_wait(&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
152 :
153 : /* Shutdown */
154 44 : if (sfd.client) {
155 31 : grpc_endpoint_shutdown(&exec_ctx, sfd.client);
156 31 : grpc_endpoint_destroy(&exec_ctx, sfd.client);
157 31 : grpc_exec_ctx_finish(&exec_ctx);
158 : }
159 44 : grpc_server_shutdown_and_notify(a.server, a.cq, NULL);
160 44 : GPR_ASSERT(grpc_completion_queue_pluck(a.cq, NULL,
161 : GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1),
162 : NULL).type == GRPC_OP_COMPLETE);
163 44 : grpc_server_destroy(a.server);
164 44 : grpc_completion_queue_destroy(a.cq);
165 44 : gpr_slice_buffer_destroy(&outgoing);
166 :
167 44 : grpc_exec_ctx_finish(&exec_ctx);
168 44 : grpc_shutdown();
169 44 : }
|