blob: 7fd7a00c81f81f995a96ff62904f15cf4f6d4eb4 [file] [log] [blame]
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001/*
2 *
Craig Tillera93a25f2016-01-28 13:55:49 -08003 * Copyright 2015-2016, Google Inc.
Craig Tillerba3c3cd2015-05-26 06:28:10 -07004 * 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
Craig Tilleradcb92d2016-03-28 10:14:05 -070036#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
Craig Tiller9533d042016-03-25 17:11:06 -070037#include "src/core/lib/channel/channel_stack.h"
38#include "src/core/lib/channel/http_server_filter.h"
39#include "src/core/lib/iomgr/endpoint_pair.h"
40#include "src/core/lib/support/string.h"
41#include "src/core/lib/surface/completion_queue.h"
42#include "src/core/lib/surface/server.h"
Craig Tillerba3c3cd2015-05-26 06:28:10 -070043
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 Tillera82950e2015-09-22 12:33:20 -070048typedef 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;
Craig Tillerac3220b2015-12-14 12:27:03 -080052 void *registered_method;
Craig Tiller4ebeb402015-05-26 13:31:52 +000053 gpr_event done_thd;
54 gpr_event done_write;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070055} thd_args;
56
Craig Tillera82950e2015-09-22 12:33:20 -070057static void thd_func(void *arg) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000058 thd_args *a = arg;
Craig Tillerac3220b2015-12-14 12:27:03 -080059 a->validator(a->server, a->cq, a->registered_method);
Craig Tillera82950e2015-09-22 12:33:20 -070060 gpr_event_set(&a->done_thd, (void *)1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070061}
62
Craig Tiller6c396862016-01-28 13:53:40 -080063static void done_write(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000064 thd_args *a = arg;
Craig Tillera82950e2015-09-22 12:33:20 -070065 gpr_event_set(&a->done_write, (void *)1);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070066}
67
Craig Tillerb2b42612015-11-20 12:02:17 -080068static void server_setup_transport(void *ts, grpc_transport *transport) {
Craig Tiller4ebeb402015-05-26 13:31:52 +000069 thd_args *a = ts;
Craig Tillerf5768a62015-09-22 10:54:34 -070070 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller178edfa2016-02-17 20:54:46 -080071 grpc_server_setup_transport(&exec_ctx, a->server, transport,
Craig Tillera82950e2015-09-22 12:33:20 -070072 grpc_server_get_channel_args(a->server));
73 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070074}
75
Craig Tillera82950e2015-09-22 12:33:20 -070076void grpc_run_bad_client_test(grpc_bad_client_server_side_validator validator,
77 const char *client_payload,
Craig Tiller7536af02015-12-22 13:49:30 -080078 size_t client_payload_length, uint32_t 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;
Craig Tillera82950e2015-09-22 12:33:20 -070084 gpr_slice slice =
85 gpr_slice_from_copied_buffer(client_payload, client_payload_length);
Craig Tillerb0298592015-08-27 07:38:01 -070086 gpr_slice_buffer outgoing;
Craig Tiller33825112015-09-18 07:44:19 -070087 grpc_closure done_write_closure;
Craig Tillerf5768a62015-09-22 10:54:34 -070088 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerba3c3cd2015-05-26 06:28:10 -070089
Craig Tillera82950e2015-09-22 12:33:20 -070090 hex = gpr_dump(client_payload, client_payload_length,
91 GPR_DUMP_HEX | GPR_DUMP_ASCII);
Craig Tiller3c26d092015-06-05 14:48:30 -070092
Craig Tiller4ebeb402015-05-26 13:31:52 +000093 /* Add a debug log */
Craig Tillera82950e2015-09-22 12:33:20 -070094 gpr_log(GPR_INFO, "TEST: %s", hex);
Craig Tillerba3c3cd2015-05-26 06:28:10 -070095
Craig Tillera82950e2015-09-22 12:33:20 -070096 gpr_free(hex);
Craig Tillere46f00a2015-06-30 14:27:27 -070097
Craig Tiller4ebeb402015-05-26 13:31:52 +000098 /* Init grpc */
Craig Tillera82950e2015-09-22 12:33:20 -070099 grpc_init();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700100
Craig Tiller4ebeb402015-05-26 13:31:52 +0000101 /* Create endpoints */
Craig Tillera82950e2015-09-22 12:33:20 -0700102 sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700103
104 /* Create server, completion events */
Craig Tiller178edfa2016-02-17 20:54:46 -0800105 a.server = grpc_server_create(NULL, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700106 a.cq = grpc_completion_queue_create(NULL);
107 gpr_event_init(&a.done_thd);
108 gpr_event_init(&a.done_write);
Craig Tiller4ebeb402015-05-26 13:31:52 +0000109 a.validator = validator;
Craig Tillera82950e2015-09-22 12:33:20 -0700110 grpc_server_register_completion_queue(a.server, a.cq, NULL);
Craig Tiller6cb69172015-12-14 13:35:53 -0800111 a.registered_method =
112 grpc_server_register_method(a.server, GRPC_BAD_CLIENT_REGISTERED_METHOD,
113 GRPC_BAD_CLIENT_REGISTERED_HOST);
Craig Tillera82950e2015-09-22 12:33:20 -0700114 grpc_server_start(a.server);
Craig Tillerb2b42612015-11-20 12:02:17 -0800115 transport = grpc_create_chttp2_transport(&exec_ctx, NULL, sfd.server, 0);
116 server_setup_transport(&a, transport);
Craig Tillera82950e2015-09-22 12:33:20 -0700117 grpc_chttp2_transport_start_reading(&exec_ctx, transport, NULL, 0);
118 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700119
120 /* Bind everything into the same pollset */
Craig Tillera82950e2015-09-22 12:33:20 -0700121 grpc_endpoint_add_to_pollset(&exec_ctx, sfd.client, grpc_cq_pollset(a.cq));
122 grpc_endpoint_add_to_pollset(&exec_ctx, sfd.server, grpc_cq_pollset(a.cq));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700123
Craig Tiller4ebeb402015-05-26 13:31:52 +0000124 /* Check a ground truth */
Craig Tillera82950e2015-09-22 12:33:20 -0700125 GPR_ASSERT(grpc_server_has_open_connections(a.server));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700126
Craig Tiller4ebeb402015-05-26 13:31:52 +0000127 /* Start validator */
Craig Tillera82950e2015-09-22 12:33:20 -0700128 gpr_thd_new(&id, thd_func, &a, NULL);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700129
Craig Tillera82950e2015-09-22 12:33:20 -0700130 gpr_slice_buffer_init(&outgoing);
131 gpr_slice_buffer_add(&outgoing, slice);
132 grpc_closure_init(&done_write_closure, done_write, &a);
Craig Tillerb0298592015-08-27 07:38:01 -0700133
Craig Tiller4ebeb402015-05-26 13:31:52 +0000134 /* Write data */
Craig Tillera82950e2015-09-22 12:33:20 -0700135 grpc_endpoint_write(&exec_ctx, sfd.client, &outgoing, &done_write_closure);
136 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700137
Craig Tiller4ebeb402015-05-26 13:31:52 +0000138 /* Await completion */
Craig Tillera82950e2015-09-22 12:33:20 -0700139 GPR_ASSERT(
140 gpr_event_wait(&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
Craig Tiller3c26d092015-06-05 14:48:30 -0700141
Craig Tillera82950e2015-09-22 12:33:20 -0700142 if (flags & GRPC_BAD_CLIENT_DISCONNECT) {
143 grpc_endpoint_shutdown(&exec_ctx, sfd.client);
144 grpc_endpoint_destroy(&exec_ctx, sfd.client);
145 grpc_exec_ctx_finish(&exec_ctx);
146 sfd.client = NULL;
147 }
Craig Tiller3c26d092015-06-05 14:48:30 -0700148
Craig Tillera82950e2015-09-22 12:33:20 -0700149 GPR_ASSERT(gpr_event_wait(&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700150
Craig Tiller4ebeb402015-05-26 13:31:52 +0000151 /* Shutdown */
Craig Tillera82950e2015-09-22 12:33:20 -0700152 if (sfd.client) {
153 grpc_endpoint_shutdown(&exec_ctx, sfd.client);
154 grpc_endpoint_destroy(&exec_ctx, sfd.client);
155 grpc_exec_ctx_finish(&exec_ctx);
156 }
157 grpc_server_shutdown_and_notify(a.server, a.cq, NULL);
Craig Tillerf40df232016-03-25 13:38:14 -0700158 GPR_ASSERT(grpc_completion_queue_pluck(
159 a.cq, NULL, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), NULL)
160 .type == GRPC_OP_COMPLETE);
Craig Tillera82950e2015-09-22 12:33:20 -0700161 grpc_server_destroy(a.server);
162 grpc_completion_queue_destroy(a.cq);
163 gpr_slice_buffer_destroy(&outgoing);
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700164
Craig Tillera82950e2015-09-22 12:33:20 -0700165 grpc_exec_ctx_finish(&exec_ctx);
166 grpc_shutdown();
Craig Tillerba3c3cd2015-05-26 06:28:10 -0700167}