blob: b9c0c98f69a190a6905af441e54133c757fad611 [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
48typedef struct {
Craig Tiller4ebeb402015-05-26 13:31:52 +000049 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;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070054} thd_args;
55
56static void thd_func(void *arg) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000057 thd_args *a = arg;
58 a->validator(a->server, a->cq);
59 gpr_event_set(&a->done_thd, (void *)1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070060}
61
Craig Tillerb0298592015-08-27 07:38:01 -070062static void done_write(void *arg, int success) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000063 thd_args *a = arg;
64 gpr_event_set(&a->done_write, (void *)1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070065}
66
Craig Tiller079a11b2015-06-30 10:07:15 -070067static void server_setup_transport(void *ts, grpc_transport *transport,
Craig Tiller47a708e2015-09-15 16:16:06 -070068 grpc_mdctx *mdctx,
69 grpc_workqueue *workqueue) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000070 thd_args *a = ts;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070071 static grpc_channel_filter const *extra_filters[] = {
72 &grpc_http_server_filter};
Craig Tilleracf0f072015-06-29 08:24:16 -070073 grpc_server_setup_transport(a->server, transport, extra_filters,
Craig Tiller47a708e2015-09-15 16:16:06 -070074 GPR_ARRAY_SIZE(extra_filters), mdctx, workqueue,
Craig Tiller079a11b2015-06-30 10:07:15 -070075 grpc_server_get_channel_args(a->server));
Craig Tillerba3c3cd2015-05-26 06:28:10 -070076}
77
Craig Tiller3c26d092015-06-05 14:48:30 -070078void grpc_run_bad_client_test(grpc_bad_client_server_side_validator validator,
79 const char *client_payload,
Craig Tiller83b2f252015-06-05 14:50:00 -070080 size_t client_payload_length, gpr_uint32 flags) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000081 grpc_endpoint_pair sfd;
82 thd_args a;
83 gpr_thd_id id;
Craig Tiller83b2f252015-06-05 14:50:00 -070084 char *hex;
Craig Tilleracf0f072015-06-29 08:24:16 -070085 grpc_transport *transport;
86 grpc_mdctx *mdctx = grpc_mdctx_create();
Craig Tiller4ebeb402015-05-26 13:31:52 +000087 gpr_slice slice =
88 gpr_slice_from_copied_buffer(client_payload, client_payload_length);
Craig Tillerb0298592015-08-27 07:38:01 -070089 gpr_slice_buffer outgoing;
Craig Tiller33825112015-09-18 07:44:19 -070090 grpc_closure done_write_closure;
Craig Tiller99b65f22015-09-17 08:52:59 -070091 grpc_workqueue *workqueue;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070092
Julien Boeufda13cd22015-06-29 19:25:32 +020093 hex = gpr_dump(client_payload, client_payload_length,
94 GPR_DUMP_HEX | GPR_DUMP_ASCII);
Craig Tiller3c26d092015-06-05 14:48:30 -070095
Craig Tiller4ebeb402015-05-26 13:31:52 +000096 /* Add a debug log */
Craig Tiller3c26d092015-06-05 14:48:30 -070097 gpr_log(GPR_INFO, "TEST: %s", hex);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070098
Craig Tillere46f00a2015-06-30 14:27:27 -070099 gpr_free(hex);
100
Craig Tiller4ebeb402015-05-26 13:31:52 +0000101 /* Init grpc */
102 grpc_init();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700103
Craig Tiller99b65f22015-09-17 08:52:59 -0700104 workqueue = grpc_workqueue_create();
105
Craig Tiller4ebeb402015-05-26 13:31:52 +0000106 /* Create endpoints */
Craig Tiller7d57ba12015-09-16 10:02:09 -0700107 sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536, workqueue);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700108
109 /* Create server, completion events */
Craig Tiller4ebeb402015-05-26 13:31:52 +0000110 a.server = grpc_server_create_from_filters(NULL, 0, NULL);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200111 a.cq = grpc_completion_queue_create(NULL);
Craig Tiller4ebeb402015-05-26 13:31:52 +0000112 gpr_event_init(&a.done_thd);
113 gpr_event_init(&a.done_write);
114 a.validator = validator;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200115 grpc_server_register_completion_queue(a.server, a.cq, NULL);
Craig Tiller4ebeb402015-05-26 13:31:52 +0000116 grpc_server_start(a.server);
Craig Tiller7d57ba12015-09-16 10:02:09 -0700117 transport =
118 grpc_create_chttp2_transport(NULL, sfd.server, mdctx, workqueue, 0);
119 server_setup_transport(&a, transport, mdctx, workqueue);
Craig Tiller1dd70262015-07-06 11:45:30 -0700120 grpc_chttp2_transport_start_reading(transport, NULL, 0);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700121
122 /* Bind everything into the same pollset */
Craig Tiller4ebeb402015-05-26 13:31:52 +0000123 grpc_endpoint_add_to_pollset(sfd.client, grpc_cq_pollset(a.cq));
124 grpc_endpoint_add_to_pollset(sfd.server, grpc_cq_pollset(a.cq));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700125
Craig Tiller4ebeb402015-05-26 13:31:52 +0000126 /* Check a ground truth */
127 GPR_ASSERT(grpc_server_has_open_connections(a.server));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700128
Craig Tiller4ebeb402015-05-26 13:31:52 +0000129 /* Start validator */
130 gpr_thd_new(&id, thd_func, &a, NULL);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700131
Craig Tillerb0298592015-08-27 07:38:01 -0700132 gpr_slice_buffer_init(&outgoing);
133 gpr_slice_buffer_add(&outgoing, slice);
Craig Tiller33825112015-09-18 07:44:19 -0700134 grpc_closure_init(&done_write_closure, done_write, &a);
Craig Tillerb0298592015-08-27 07:38:01 -0700135
Craig Tiller4ebeb402015-05-26 13:31:52 +0000136 /* Write data */
Craig Tillerb0298592015-08-27 07:38:01 -0700137 switch (grpc_endpoint_write(sfd.client, &outgoing, &done_write_closure)) {
138 case GRPC_ENDPOINT_DONE:
Craig Tiller4ebeb402015-05-26 13:31:52 +0000139 done_write(&a, 1);
140 break;
Craig Tillerb0298592015-08-27 07:38:01 -0700141 case GRPC_ENDPOINT_PENDING:
Craig Tiller4ebeb402015-05-26 13:31:52 +0000142 break;
Craig Tillerb0298592015-08-27 07:38:01 -0700143 case GRPC_ENDPOINT_ERROR:
Craig Tiller4ebeb402015-05-26 13:31:52 +0000144 done_write(&a, 0);
145 break;
146 }
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700147
Craig Tiller4ebeb402015-05-26 13:31:52 +0000148 /* Await completion */
149 GPR_ASSERT(
150 gpr_event_wait(&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
Craig Tiller3c26d092015-06-05 14:48:30 -0700151
152 if (flags & GRPC_BAD_CLIENT_DISCONNECT) {
Nicolas "Pixel" Noble2057a7a2015-09-04 03:47:25 +0200153 grpc_endpoint_shutdown(sfd.client);
Craig Tiller3c26d092015-06-05 14:48:30 -0700154 grpc_endpoint_destroy(sfd.client);
155 sfd.client = NULL;
156 }
157
Craig Tiller4ebeb402015-05-26 13:31:52 +0000158 GPR_ASSERT(gpr_event_wait(&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700159
Craig Tiller4ebeb402015-05-26 13:31:52 +0000160 /* Shutdown */
Craig Tiller3c26d092015-06-05 14:48:30 -0700161 if (sfd.client) {
Nicolas "Pixel" Noble2057a7a2015-09-04 03:47:25 +0200162 grpc_endpoint_shutdown(sfd.client);
Craig Tiller3c26d092015-06-05 14:48:30 -0700163 grpc_endpoint_destroy(sfd.client);
164 }
Craig Tillere511fdb2015-05-27 13:00:38 -0700165 grpc_server_shutdown_and_notify(a.server, a.cq, NULL);
Craig Tillerd6c98df2015-08-18 09:33:44 -0700166 GPR_ASSERT(grpc_completion_queue_pluck(
167 a.cq, NULL, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), NULL)
Craig Tillere511fdb2015-05-27 13:00:38 -0700168 .type == GRPC_OP_COMPLETE);
Craig Tiller4ebeb402015-05-26 13:31:52 +0000169 grpc_server_destroy(a.server);
170 grpc_completion_queue_destroy(a.cq);
Craig Tillerb0298592015-08-27 07:38:01 -0700171 gpr_slice_buffer_destroy(&outgoing);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700172
Craig Tiller3cd6a512015-09-16 16:15:47 -0700173 GRPC_WORKQUEUE_UNREF(workqueue, "destroy");
Craig Tiller4ebeb402015-05-26 13:31:52 +0000174 grpc_shutdown();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700175}