Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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/end2end/end2end_tests.h" |
| 35 | |
| 36 | #include <errno.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <string.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/socket.h> |
| 41 | #include <unistd.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <stdio.h> |
| 44 | |
| 45 | #include "src/core/channel/client_channel.h" |
| 46 | #include "src/core/channel/connected_channel.h" |
| 47 | #include "src/core/channel/http_filter.h" |
| 48 | #include "src/core/channel/http_server_filter.h" |
| 49 | #include "src/core/eventmanager/em.h" |
| 50 | #include "src/core/surface/channel.h" |
| 51 | #include "src/core/surface/client.h" |
| 52 | #include "src/core/surface/server.h" |
| 53 | #include "src/core/surface/surface_em.h" |
| 54 | #include "src/core/transport/chttp2_transport.h" |
| 55 | #include <grpc/support/alloc.h> |
| 56 | #include <grpc/support/log.h> |
| 57 | #include <grpc/support/sync.h> |
| 58 | #include <grpc/support/thd.h> |
| 59 | #include <grpc/support/useful.h> |
| 60 | #include "test/core/util/port.h" |
| 61 | #include "test/core/util/test_config.h" |
| 62 | |
| 63 | static void create_sockets(int sv[2]) { |
| 64 | int flags; |
| 65 | GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); |
| 66 | flags = fcntl(sv[0], F_GETFL, 0); |
| 67 | GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0); |
| 68 | flags = fcntl(sv[1], F_GETFL, 0); |
| 69 | GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0); |
| 70 | } |
| 71 | |
| 72 | /* chttp2 transport that is immediately available (used for testing |
| 73 | connected_channel without a client_channel */ |
| 74 | |
| 75 | static grpc_transport_setup_result server_setup_transport( |
| 76 | void *ts, grpc_transport *transport, grpc_mdctx *mdctx) { |
| 77 | grpc_end2end_test_fixture *f = ts; |
| 78 | static grpc_channel_filter const *extra_filters[] = {&grpc_http_server_filter, |
| 79 | &grpc_http_filter}; |
| 80 | return grpc_server_setup_transport(f->server, transport, extra_filters, |
| 81 | GPR_ARRAY_SIZE(extra_filters), mdctx); |
| 82 | } |
| 83 | |
| 84 | typedef struct { |
| 85 | grpc_end2end_test_fixture *f; |
| 86 | grpc_channel_args *client_args; |
| 87 | } sp_client_setup; |
| 88 | |
| 89 | static grpc_transport_setup_result client_setup_transport( |
| 90 | void *ts, grpc_transport *transport, grpc_mdctx *mdctx) { |
| 91 | sp_client_setup *cs = ts; |
| 92 | |
| 93 | const grpc_channel_filter *filters[] = {&grpc_client_surface_filter, |
| 94 | &grpc_connected_channel_filter}; |
| 95 | size_t nfilters = sizeof(filters) / sizeof(*filters); |
| 96 | grpc_channel *channel = grpc_channel_create_from_filters( |
| 97 | filters, nfilters, cs->client_args, mdctx, 1); |
| 98 | |
| 99 | cs->f->client = channel; |
| 100 | |
| 101 | return grpc_connected_channel_bind_transport( |
| 102 | grpc_channel_get_channel_stack(channel), transport); |
| 103 | } |
| 104 | |
| 105 | typedef struct socketpair_fixture_data { int sv[2]; } socketpair_fixture_data; |
| 106 | |
| 107 | static grpc_end2end_test_fixture chttp2_create_fixture_socketpair( |
| 108 | grpc_channel_args *client_args, grpc_channel_args *server_args) { |
| 109 | socketpair_fixture_data *sfd = gpr_malloc(sizeof(socketpair_fixture_data)); |
| 110 | |
| 111 | grpc_end2end_test_fixture f; |
| 112 | f.fixture_data = sfd; |
| 113 | f.client_cq = grpc_completion_queue_create(); |
| 114 | f.server_cq = grpc_completion_queue_create(); |
| 115 | f.server = grpc_server_create_from_filters(f.server_cq, NULL, 0, server_args); |
| 116 | f.client = NULL; |
| 117 | |
| 118 | create_sockets(sfd->sv); |
| 119 | |
| 120 | return f; |
| 121 | } |
| 122 | |
| 123 | static void chttp2_init_client_socketpair(grpc_end2end_test_fixture *f, |
| 124 | grpc_channel_args *client_args) { |
| 125 | socketpair_fixture_data *sfd = f->fixture_data; |
| 126 | grpc_endpoint *cli_tcp; |
| 127 | sp_client_setup cs; |
| 128 | cs.client_args = client_args; |
| 129 | cs.f = f; |
| 130 | cli_tcp = grpc_tcp_create_dbg(sfd->sv[0], grpc_surface_em(), 65536); |
| 131 | grpc_create_chttp2_transport(client_setup_transport, &cs, client_args, |
| 132 | cli_tcp, NULL, 0, grpc_mdctx_create(), 1); |
| 133 | GPR_ASSERT(f->client); |
| 134 | } |
| 135 | |
| 136 | static void chttp2_init_server_socketpair(grpc_end2end_test_fixture *f, |
| 137 | grpc_channel_args *server_args) { |
| 138 | socketpair_fixture_data *sfd = f->fixture_data; |
| 139 | grpc_endpoint *svr_tcp; |
| 140 | svr_tcp = grpc_tcp_create_dbg(sfd->sv[1], grpc_surface_em(), 65536); |
| 141 | grpc_create_chttp2_transport(server_setup_transport, f, server_args, svr_tcp, |
| 142 | NULL, 0, grpc_mdctx_create(), 0); |
| 143 | } |
| 144 | |
| 145 | static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture *f) { |
| 146 | gpr_free(f->fixture_data); |
| 147 | } |
| 148 | |
| 149 | /* All test configurations */ |
| 150 | static grpc_end2end_test_config configs[] = { |
| 151 | {"chttp2/socketpair", 0, chttp2_create_fixture_socketpair, |
| 152 | chttp2_init_client_socketpair, chttp2_init_server_socketpair, |
| 153 | chttp2_tear_down_socketpair}, |
| 154 | }; |
| 155 | |
| 156 | int main(int argc, char **argv) { |
| 157 | size_t i; |
| 158 | |
| 159 | grpc_test_init(argc, argv); |
| 160 | grpc_init(); |
| 161 | |
| 162 | for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
| 163 | grpc_end2end_tests(configs[i]); |
| 164 | } |
| 165 | |
| 166 | grpc_shutdown(); |
| 167 | |
| 168 | return 0; |
| 169 | } |