| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include "absl/status/status.h" |
| 22 |
#include "absl/status/statusor.h" |
| 23 |
|
| 24 |
#include <grpc/grpc.h> |
| 25 |
#include <grpc/status.h> |
| 26 |
#include <grpc/support/alloc.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
|
| 29 |
#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h" |
| 30 |
#include "src/core/lib/channel/channel_args.h" |
| 31 |
#include "src/core/lib/channel/channel_args_preconditioning.h" |
| 32 |
#include "src/core/lib/channel/channelz.h" |
| 33 |
#include "src/core/lib/config/core_configuration.h" |
| 34 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 35 |
#include "src/core/lib/iomgr/endpoint.h" |
| 36 |
#include "src/core/lib/iomgr/endpoint_pair.h" |
| 37 |
#include "src/core/lib/iomgr/error.h" |
| 38 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 39 |
#include "src/core/lib/surface/channel.h" |
| 40 |
#include "src/core/lib/surface/channel_stack_type.h" |
| 41 |
#include "src/core/lib/surface/completion_queue.h" |
| 42 |
#include "src/core/lib/surface/server.h" |
| 43 |
#include "src/core/lib/transport/transport.h" |
| 44 |
#include "src/core/lib/transport/transport_fwd.h" |
| 45 |
#include "test/core/end2end/end2end_tests.h" |
| 46 |
#include "test/core/util/test_config.h" |
| 47 |
|
| 48 |
/* chttp2 transport that is immediately available (used for testing |
| 49 |
connected_channel without a client_channel) */ |
| 50 |
|
| 51 |
struct custom_fixture_data { |
| 52 |
grpc_endpoint_pair ep; |
| 53 |
}; |
| 54 |
|
| 55 |
static void server_setup_transport(void* ts, grpc_transport* transport) { |
| 56 |
grpc_end2end_test_fixture* f = static_cast<grpc_end2end_test_fixture*>(ts); |
| 57 |
grpc_core::ExecCtx exec_ctx; |
| 58 |
custom_fixture_data* fixture_data = |
| 59 |
static_cast<custom_fixture_data*>(f->fixture_data); |
| 60 |
grpc_endpoint_add_to_pollset(fixture_data->ep.server, grpc_cq_pollset(f->cq)); |
| 61 |
grpc_core::Server* core_server = grpc_core::Server::FromC(f->server); |
| 62 |
grpc_error_handle error = core_server->SetupTransport( |
| 63 |
transport, nullptr, core_server->channel_args(), nullptr); |
| 64 |
if (error == absl::OkStatus()) { |
| 65 |
grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr); |
| 66 |
} else { |
| 67 |
grpc_transport_destroy(transport); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
typedef struct { |
| 72 |
grpc_end2end_test_fixture* f; |
| 73 |
const grpc_channel_args* client_args; |
| 74 |
} sp_client_setup; |
| 75 |
|
| 76 |
static void client_setup_transport(void* ts, grpc_transport* transport) { |
| 77 |
sp_client_setup* cs = static_cast<sp_client_setup*>(ts); |
| 78 |
|
| 79 |
auto args = grpc_core::ChannelArgs::FromC(cs->client_args) |
| 80 |
.Set(GRPC_ARG_MINIMAL_STACK, true) |
| 81 |
.Set(GRPC_ARG_DEFAULT_AUTHORITY, "test-authority"); |
| 82 |
auto channel = grpc_core::Channel::Create( |
| 83 |
"socketpair-target", args, GRPC_CLIENT_DIRECT_CHANNEL, transport); |
| 84 |
if (channel.ok()) { |
| 85 |
cs->f->client = channel->release()->c_ptr(); |
| 86 |
grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr); |
| 87 |
} else { |
| 88 |
cs->f->client = grpc_lame_client_channel_create( |
| 89 |
nullptr, static_cast<grpc_status_code>(channel.status().code()), |
| 90 |
"lame channel"); |
| 91 |
grpc_transport_destroy(transport); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static grpc_end2end_test_fixture chttp2_create_fixture_socketpair( |
| 96 |
const grpc_channel_args* /*client_args*/, |
| 97 |
const grpc_channel_args* /*server_args*/) { |
| 98 |
custom_fixture_data* fixture_data = static_cast<custom_fixture_data*>( |
| 99 |
gpr_malloc(sizeof(custom_fixture_data))); |
| 100 |
grpc_end2end_test_fixture f; |
| 101 |
memset(&f, 0, sizeof(f)); |
| 102 |
f.fixture_data = fixture_data; |
| 103 |
f.cq = grpc_completion_queue_create_for_next(nullptr); |
| 104 |
fixture_data->ep = grpc_iomgr_create_endpoint_pair("fixture", nullptr); |
| 105 |
return f; |
| 106 |
} |
| 107 |
|
| 108 |
static void chttp2_init_client_socketpair( |
| 109 |
grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) { |
| 110 |
grpc_core::ExecCtx exec_ctx; |
| 111 |
auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data); |
| 112 |
grpc_transport* transport; |
| 113 |
sp_client_setup cs; |
| 114 |
auto final_client_args = grpc_core::CoreConfiguration::Get() |
| 115 |
.channel_args_preconditioning() |
| 116 |
.PreconditionChannelArgs(client_args) |
| 117 |
.Set(GRPC_ARG_MINIMAL_STACK, true); |
| 118 |
auto c_client_args = final_client_args.ToC(); |
| 119 |
cs.client_args = c_client_args.get(); |
| 120 |
cs.f = f; |
| 121 |
transport = grpc_create_chttp2_transport(final_client_args, |
| 122 |
fixture_data->ep.client, true); |
| 123 |
client_setup_transport(&cs, transport); |
| 124 |
GPR_ASSERT(f->client); |
| 125 |
} |
| 126 |
|
| 127 |
static void chttp2_init_server_socketpair( |
| 128 |
grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) { |
| 129 |
grpc_core::ExecCtx exec_ctx; |
| 130 |
auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data); |
| 131 |
grpc_transport* transport; |
| 132 |
GPR_ASSERT(!f->server); |
| 133 |
f->server = grpc_server_create(server_args, nullptr); |
| 134 |
grpc_server_register_completion_queue(f->server, f->cq, nullptr); |
| 135 |
grpc_server_start(f->server); |
| 136 |
auto final_server_args = grpc_core::CoreConfiguration::Get() |
| 137 |
.channel_args_preconditioning() |
| 138 |
.PreconditionChannelArgs(server_args) |
| 139 |
.Set(GRPC_ARG_MINIMAL_STACK, true); |
| 140 |
transport = grpc_create_chttp2_transport(final_server_args, |
| 141 |
fixture_data->ep.server, false); |
| 142 |
server_setup_transport(f, transport); |
| 143 |
} |
| 144 |
|
| 145 |
static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) { |
| 146 |
grpc_core::ExecCtx exec_ctx; |
| 147 |
gpr_free(f->fixture_data); |
| 148 |
} |
| 149 |
|
| 150 |
/* All test configurations */ |
| 151 |
static grpc_end2end_test_config configs[] = { |
| 152 |
{"chttp2/socketpair+minstack", |
| 153 |
FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER | |
| 154 |
FEATURE_MASK_DOES_NOT_SUPPORT_DEADLINES, |
| 155 |
nullptr, chttp2_create_fixture_socketpair, chttp2_init_client_socketpair, |
| 156 |
chttp2_init_server_socketpair, chttp2_tear_down_socketpair}, |
| 157 |
}; |
| 158 |
|
| 159 |
int main(int argc, char** argv) { |
| 160 |
size_t i; |
| 161 |
|
| 162 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 163 |
grpc_end2end_tests_pre_init(); |
| 164 |
grpc_init(); |
| 165 |
|
| 166 |
for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
| 167 |
grpc_end2end_tests(argc, argv, configs[i]); |
| 168 |
} |
| 169 |
|
| 170 |
grpc_shutdown(); |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |