blob: e6239bb7d54177a9d5db94a09c8b8f49028ac549 [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"
41#include "src/core/transport/chttp2_transport.h"
42
43#include <grpc/support/sync.h>
44#include <grpc/support/thd.h>
45
46typedef struct {
47 grpc_server *server;
48 grpc_completion_queue *cq;
49 grpc_bad_client_server_side_validator validator;
50 gpr_event done_thd;
51 gpr_event done_write;
52} thd_args;
53
54static void thd_func(void *arg) {
55 thd_args *a = arg;
56 a->validator(a->server, a->cq);
57 gpr_event_set(&a->done_thd, (void*)1);
58}
59
60static void done_write(void *arg, grpc_endpoint_cb_status status) {
61 thd_args *a = arg;
62 gpr_event_set(&a->done_write, (void*)1);
63}
64
65static grpc_transport_setup_result server_setup_transport(void *ts, grpc_transport *transport, grpc_mdctx *mdctx) {
66 thd_args *a = ts;
67 static grpc_channel_filter const *extra_filters[] = {
68 &grpc_http_server_filter};
69 return grpc_server_setup_transport(a->server, transport, extra_filters,
70 GPR_ARRAY_SIZE(extra_filters), mdctx);
71}
72
73void grpc_run_bad_client_test(const char *name, const char *client_payload,
74 size_t client_payload_length,
75 grpc_bad_client_server_side_validator validator
76 ) {
77 grpc_endpoint_pair sfd;
78 thd_args a;
79 gpr_thd_id id;
80 gpr_slice slice = gpr_slice_from_copied_buffer(client_payload, client_payload_length);
81
82 /* Add a debug log */
83 gpr_log(GPR_INFO, "TEST: %s", name);
84
85 /* Init grpc */
86 grpc_init();
87
88 /* Create endpoints */
89 sfd = grpc_iomgr_create_endpoint_pair(65536);
90
91 /* Create server, completion events */
92 a.server = grpc_server_create_from_filters(NULL, 0, NULL);
93 a.cq = grpc_completion_queue_create();
94 gpr_event_init(&a.done_thd);
95 gpr_event_init(&a.done_write);
96 a.validator = validator;
97 grpc_server_register_completion_queue(a.server, a.cq);
98 grpc_server_start(a.server);
99 grpc_create_chttp2_transport(server_setup_transport, &a, NULL, sfd.server, NULL, 0, grpc_mdctx_create(), 0);
100
101 /* Bind everything into the same pollset */
102 grpc_endpoint_add_to_pollset(sfd.client, grpc_cq_pollset(a.cq));
103 grpc_endpoint_add_to_pollset(sfd.server, grpc_cq_pollset(a.cq));
104
105 /* Check a ground truth */
106 GPR_ASSERT(grpc_server_has_open_connections(a.server));
107
108 /* Start validator */
109 gpr_thd_new(&id, thd_func, &a, NULL);
110
111 /* Write data */
112 switch (grpc_endpoint_write(sfd.client, &slice, 1, done_write, &a)) {
113 case GRPC_ENDPOINT_WRITE_DONE:
114 done_write(&a, 1);
115 break;
116 case GRPC_ENDPOINT_WRITE_PENDING:
117 break;
118 case GRPC_ENDPOINT_WRITE_ERROR:
119 done_write(&a, 0);
120 break;
121 }
122
123 /* Await completion */
124 GPR_ASSERT(gpr_event_wait(&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
125 GPR_ASSERT(gpr_event_wait(&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)));
126
127 /* Shutdown */
128 grpc_endpoint_destroy(sfd.client);
129 grpc_server_destroy(a.server);
130 grpc_completion_queue_destroy(a.cq);
131
132 grpc_shutdown();
133}