blob: 0b59aa3185f87945c80ae38174fd6efced6df6d0 [file] [log] [blame]
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001/*
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"
Craig Tiller3c26d092015-06-05 14:48:30 -070041#include "src/core/support/string.h"
Craig Tillerba3c3cd2015-05-26 06:28:10 -070042#include "src/core/transport/chttp2_transport.h"
43
Craig Tillere46f00a2015-06-30 14:27:27 -070044#include <grpc/support/alloc.h>
Craig Tillerba3c3cd2015-05-26 06:28:10 -070045#include <grpc/support/sync.h>
46#include <grpc/support/thd.h>
47
Craig Tiller45724b32015-09-22 10:42:19 -070048typedef struct
49{
Craig Tiller4ebeb402015-05-26 13:31:52 +000050 grpc_server *server;
51 grpc_completion_queue *cq;
52 grpc_bad_client_server_side_validator validator;
53 gpr_event done_thd;
54 gpr_event done_write;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070055} thd_args;
56
Craig Tiller45724b32015-09-22 10:42:19 -070057static void
58thd_func (void *arg)
59{
Craig Tiller4ebeb402015-05-26 13:31:52 +000060 thd_args *a = arg;
Craig Tiller45724b32015-09-22 10:42:19 -070061 a->validator (a->server, a->cq);
62 gpr_event_set (&a->done_thd, (void *) 1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070063}
64
Craig Tiller45724b32015-09-22 10:42:19 -070065static void
Craig Tiller1be70cc2015-09-22 10:45:28 -070066done_write (grpc_exec_ctx * exec_ctx, void *arg, int success)
Craig Tiller45724b32015-09-22 10:42:19 -070067{
Craig Tiller4ebeb402015-05-26 13:31:52 +000068 thd_args *a = arg;
Craig Tiller45724b32015-09-22 10:42:19 -070069 gpr_event_set (&a->done_write, (void *) 1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070070}
71
Craig Tiller45724b32015-09-22 10:42:19 -070072static void
73server_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx)
74{
Craig Tiller4ebeb402015-05-26 13:31:52 +000075 thd_args *a = ts;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070076 static grpc_channel_filter const *extra_filters[] = {
Craig Tiller45724b32015-09-22 10:42:19 -070077 &grpc_http_server_filter
78 };
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070079 grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT;
Craig Tiller45724b32015-09-22 10:42:19 -070080 grpc_server_setup_transport (a->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (a->server), &closure_list);
81 grpc_closure_list_run (&closure_list);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070082}
83
Craig Tiller45724b32015-09-22 10:42:19 -070084void
85grpc_run_bad_client_test (grpc_bad_client_server_side_validator validator, const char *client_payload, size_t client_payload_length, gpr_uint32 flags)
86{
Craig Tiller4ebeb402015-05-26 13:31:52 +000087 grpc_endpoint_pair sfd;
88 thd_args a;
89 gpr_thd_id id;
Craig Tiller83b2f252015-06-05 14:50:00 -070090 char *hex;
Craig Tilleracf0f072015-06-29 08:24:16 -070091 grpc_transport *transport;
Craig Tiller45724b32015-09-22 10:42:19 -070092 grpc_mdctx *mdctx = grpc_mdctx_create ();
93 gpr_slice slice = gpr_slice_from_copied_buffer (client_payload, client_payload_length);
Craig Tillerb0298592015-08-27 07:38:01 -070094 gpr_slice_buffer outgoing;
Craig Tiller33825112015-09-18 07:44:19 -070095 grpc_closure done_write_closure;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070096 grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070097
Craig Tiller45724b32015-09-22 10:42:19 -070098 hex = gpr_dump (client_payload, client_payload_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
Craig Tiller3c26d092015-06-05 14:48:30 -070099
Craig Tiller4ebeb402015-05-26 13:31:52 +0000100 /* Add a debug log */
Craig Tiller45724b32015-09-22 10:42:19 -0700101 gpr_log (GPR_INFO, "TEST: %s", hex);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700102
Craig Tiller45724b32015-09-22 10:42:19 -0700103 gpr_free (hex);
Craig Tillere46f00a2015-06-30 14:27:27 -0700104
Craig Tiller4ebeb402015-05-26 13:31:52 +0000105 /* Init grpc */
Craig Tiller45724b32015-09-22 10:42:19 -0700106 grpc_init ();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700107
Craig Tiller4ebeb402015-05-26 13:31:52 +0000108 /* Create endpoints */
Craig Tiller45724b32015-09-22 10:42:19 -0700109 sfd = grpc_iomgr_create_endpoint_pair ("fixture", 65536);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700110
111 /* Create server, completion events */
Craig Tiller45724b32015-09-22 10:42:19 -0700112 a.server = grpc_server_create_from_filters (NULL, 0, NULL);
113 a.cq = grpc_completion_queue_create (NULL);
114 gpr_event_init (&a.done_thd);
115 gpr_event_init (&a.done_write);
Craig Tiller4ebeb402015-05-26 13:31:52 +0000116 a.validator = validator;
Craig Tiller45724b32015-09-22 10:42:19 -0700117 grpc_server_register_completion_queue (a.server, a.cq, NULL);
118 grpc_server_start (a.server);
119 transport = grpc_create_chttp2_transport (NULL, sfd.server, mdctx, 0, &closure_list);
120 server_setup_transport (&a, transport, mdctx);
121 grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list);
122 grpc_closure_list_run (&closure_list);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700123
124 /* Bind everything into the same pollset */
Craig Tiller45724b32015-09-22 10:42:19 -0700125 grpc_endpoint_add_to_pollset (sfd.client, grpc_cq_pollset (a.cq), &closure_list);
126 grpc_endpoint_add_to_pollset (sfd.server, grpc_cq_pollset (a.cq), &closure_list);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700127
Craig Tiller4ebeb402015-05-26 13:31:52 +0000128 /* Check a ground truth */
Craig Tiller45724b32015-09-22 10:42:19 -0700129 GPR_ASSERT (grpc_server_has_open_connections (a.server));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700130
Craig Tiller4ebeb402015-05-26 13:31:52 +0000131 /* Start validator */
Craig Tiller45724b32015-09-22 10:42:19 -0700132 gpr_thd_new (&id, thd_func, &a, NULL);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700133
Craig Tiller45724b32015-09-22 10:42:19 -0700134 gpr_slice_buffer_init (&outgoing);
135 gpr_slice_buffer_add (&outgoing, slice);
136 grpc_closure_init (&done_write_closure, done_write, &a);
Craig Tillerb0298592015-08-27 07:38:01 -0700137
Craig Tiller4ebeb402015-05-26 13:31:52 +0000138 /* Write data */
Craig Tiller45724b32015-09-22 10:42:19 -0700139 grpc_endpoint_write (sfd.client, &outgoing, &done_write_closure, &closure_list);
140 grpc_closure_list_run (&closure_list);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700141
Craig Tiller4ebeb402015-05-26 13:31:52 +0000142 /* Await completion */
Craig Tiller45724b32015-09-22 10:42:19 -0700143 GPR_ASSERT (gpr_event_wait (&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5)));
Craig Tiller3c26d092015-06-05 14:48:30 -0700144
Craig Tiller45724b32015-09-22 10:42:19 -0700145 if (flags & GRPC_BAD_CLIENT_DISCONNECT)
146 {
147 grpc_endpoint_shutdown (sfd.client, &closure_list);
148 grpc_endpoint_destroy (sfd.client, &closure_list);
149 grpc_closure_list_run (&closure_list);
150 sfd.client = NULL;
151 }
Craig Tiller3c26d092015-06-05 14:48:30 -0700152
Craig Tiller45724b32015-09-22 10:42:19 -0700153 GPR_ASSERT (gpr_event_wait (&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5)));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700154
Craig Tiller4ebeb402015-05-26 13:31:52 +0000155 /* Shutdown */
Craig Tiller45724b32015-09-22 10:42:19 -0700156 if (sfd.client)
157 {
158 grpc_endpoint_shutdown (sfd.client, &closure_list);
159 grpc_endpoint_destroy (sfd.client, &closure_list);
160 grpc_closure_list_run (&closure_list);
161 }
162 grpc_server_shutdown_and_notify (a.server, a.cq, NULL);
163 GPR_ASSERT (grpc_completion_queue_pluck (a.cq, NULL, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1), NULL).type == GRPC_OP_COMPLETE);
164 grpc_server_destroy (a.server);
165 grpc_completion_queue_destroy (a.cq);
166 gpr_slice_buffer_destroy (&outgoing);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700167
Craig Tiller45724b32015-09-22 10:42:19 -0700168 grpc_closure_list_run (&closure_list);
169 grpc_shutdown ();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700170}