blob: 4b889995a18c9a250ee191ab52e7786dfc222b73 [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
44#include <grpc/support/sync.h>
45#include <grpc/support/thd.h>
46
47typedef struct {
Craig Tiller4ebeb402015-05-26 13:31:52 +000048 grpc_server *server;
49 grpc_completion_queue *cq;
50 grpc_bad_client_server_side_validator validator;
51 gpr_event done_thd;
52 gpr_event done_write;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070053} thd_args;
54
55static void thd_func(void *arg) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000056 thd_args *a = arg;
57 a->validator(a->server, a->cq);
58 gpr_event_set(&a->done_thd, (void *)1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070059}
60
61static void done_write(void *arg, grpc_endpoint_cb_status status) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000062 thd_args *a = arg;
63 gpr_event_set(&a->done_write, (void *)1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070064}
65
Craig Tilleracf0f072015-06-29 08:24:16 -070066static void server_setup_transport(
Craig Tiller4ebeb402015-05-26 13:31:52 +000067 void *ts, grpc_transport *transport, grpc_mdctx *mdctx) {
68 thd_args *a = ts;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070069 static grpc_channel_filter const *extra_filters[] = {
70 &grpc_http_server_filter};
Craig Tilleracf0f072015-06-29 08:24:16 -070071 grpc_server_setup_transport(a->server, transport, extra_filters,
Julien Boeufff69cd12015-05-29 21:55:51 -070072 GPR_ARRAY_SIZE(extra_filters), mdctx,
73 grpc_server_get_channel_args(a->server));
Craig Tillerba3c3cd2015-05-26 06:28:10 -070074}
75
Craig Tiller3c26d092015-06-05 14:48:30 -070076void grpc_run_bad_client_test(grpc_bad_client_server_side_validator validator,
77 const char *client_payload,
Craig Tiller83b2f252015-06-05 14:50:00 -070078 size_t client_payload_length, gpr_uint32 flags) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000079 grpc_endpoint_pair sfd;
80 thd_args a;
81 gpr_thd_id id;
Craig Tiller83b2f252015-06-05 14:50:00 -070082 char *hex;
Craig Tilleracf0f072015-06-29 08:24:16 -070083 grpc_transport *transport;
84 grpc_mdctx *mdctx = grpc_mdctx_create();
Craig Tiller4ebeb402015-05-26 13:31:52 +000085 gpr_slice slice =
86 gpr_slice_from_copied_buffer(client_payload, client_payload_length);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070087
Craig Tiller83b2f252015-06-05 14:50:00 -070088 hex =
89 gpr_hexdump(client_payload, client_payload_length, GPR_HEXDUMP_PLAINTEXT);
Craig Tiller3c26d092015-06-05 14:48:30 -070090
Craig Tiller4ebeb402015-05-26 13:31:52 +000091 /* Add a debug log */
Craig Tiller3c26d092015-06-05 14:48:30 -070092 gpr_log(GPR_INFO, "TEST: %s", hex);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070093
Craig Tiller4ebeb402015-05-26 13:31:52 +000094 /* Init grpc */
95 grpc_init();
Craig Tillerba3c3cd2015-05-26 06:28:10 -070096
Craig Tiller4ebeb402015-05-26 13:31:52 +000097 /* Create endpoints */
Craig Tillerfa275a92015-06-01 13:55:54 -070098 sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070099
100 /* Create server, completion events */
Craig Tiller4ebeb402015-05-26 13:31:52 +0000101 a.server = grpc_server_create_from_filters(NULL, 0, NULL);
102 a.cq = grpc_completion_queue_create();
103 gpr_event_init(&a.done_thd);
104 gpr_event_init(&a.done_write);
105 a.validator = validator;
106 grpc_server_register_completion_queue(a.server, a.cq);
107 grpc_server_start(a.server);
Craig Tilleracf0f072015-06-29 08:24:16 -0700108 transport = grpc_create_chttp2_transport(NULL, sfd.server,
109 NULL, 0, mdctx, 0);
110 server_setup_transport(&a, transport, mdctx);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700111
112 /* Bind everything into the same pollset */
Craig Tiller4ebeb402015-05-26 13:31:52 +0000113 grpc_endpoint_add_to_pollset(sfd.client, grpc_cq_pollset(a.cq));
114 grpc_endpoint_add_to_pollset(sfd.server, grpc_cq_pollset(a.cq));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700115
Craig Tiller4ebeb402015-05-26 13:31:52 +0000116 /* Check a ground truth */
117 GPR_ASSERT(grpc_server_has_open_connections(a.server));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700118
Craig Tiller4ebeb402015-05-26 13:31:52 +0000119 /* Start validator */
120 gpr_thd_new(&id, thd_func, &a, NULL);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700121
Craig Tiller4ebeb402015-05-26 13:31:52 +0000122 /* Write data */
123 switch (grpc_endpoint_write(sfd.client, &slice, 1, done_write, &a)) {
124 case GRPC_ENDPOINT_WRITE_DONE:
125 done_write(&a, 1);
126 break;
127 case GRPC_ENDPOINT_WRITE_PENDING:
128 break;
129 case GRPC_ENDPOINT_WRITE_ERROR:
130 done_write(&a, 0);
131 break;
132 }
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700133
Craig Tiller4ebeb402015-05-26 13:31:52 +0000134 /* Await completion */
135 GPR_ASSERT(
136 gpr_event_wait(&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
Craig Tiller3c26d092015-06-05 14:48:30 -0700137
138 if (flags & GRPC_BAD_CLIENT_DISCONNECT) {
139 grpc_endpoint_destroy(sfd.client);
140 sfd.client = NULL;
141 }
142
Craig Tiller4ebeb402015-05-26 13:31:52 +0000143 GPR_ASSERT(gpr_event_wait(&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700144
Craig Tiller4ebeb402015-05-26 13:31:52 +0000145 /* Shutdown */
Craig Tiller3c26d092015-06-05 14:48:30 -0700146 if (sfd.client) {
147 grpc_endpoint_destroy(sfd.client);
148 }
Craig Tillere511fdb2015-05-27 13:00:38 -0700149 grpc_server_shutdown_and_notify(a.server, a.cq, NULL);
150 GPR_ASSERT(grpc_completion_queue_pluck(a.cq, NULL,
151 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1))
152 .type == GRPC_OP_COMPLETE);
Craig Tiller4ebeb402015-05-26 13:31:52 +0000153 grpc_server_destroy(a.server);
154 grpc_completion_queue_destroy(a.cq);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700155
Craig Tiller4ebeb402015-05-26 13:31:52 +0000156 grpc_shutdown();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700157}