| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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 <string> |
| 22 |
|
| 23 |
#include "absl/strings/str_format.h" |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/grpc_security.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
|
| 29 |
#include "src/core/lib/channel/channel_args.h" |
| 30 |
#include "src/core/lib/gprpp/host_port.h" |
| 31 |
#include "test/core/end2end/end2end_tests.h" |
| 32 |
#include "test/core/end2end/fixtures/http_proxy_fixture.h" |
| 33 |
#include "test/core/util/port.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
struct fullstack_fixture_data { |
| 37 |
~fullstack_fixture_data() { grpc_end2end_http_proxy_destroy(proxy); } |
| 38 |
std::string server_addr; |
| 39 |
grpc_end2end_http_proxy* proxy = nullptr; |
| 40 |
}; |
| 41 |
|
| 42 |
static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( |
| 43 |
const grpc_channel_args* client_args, |
| 44 |
const grpc_channel_args* /*server_args*/) { |
| 45 |
grpc_end2end_test_fixture f; |
| 46 |
memset(&f, 0, sizeof(f)); |
| 47 |
fullstack_fixture_data* ffd = new fullstack_fixture_data(); |
| 48 |
const int server_port = grpc_pick_unused_port_or_die(); |
| 49 |
ffd->server_addr = grpc_core::JoinHostPort("localhost", server_port); |
| 50 |
|
| 51 |
/* Passing client_args to proxy_create for the case of checking for proxy auth |
| 52 |
*/ |
| 53 |
ffd->proxy = grpc_end2end_http_proxy_create(client_args); |
| 54 |
|
| 55 |
f.fixture_data = ffd; |
| 56 |
f.cq = grpc_completion_queue_create_for_next(nullptr); |
| 57 |
|
| 58 |
return f; |
| 59 |
} |
| 60 |
|
| 61 |
void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, |
| 62 |
const grpc_channel_args* client_args) { |
| 63 |
fullstack_fixture_data* ffd = |
| 64 |
static_cast<fullstack_fixture_data*>(f->fixture_data); |
| 65 |
/* If testing for proxy auth, add credentials to proxy uri */ |
| 66 |
const char* proxy_auth_str = grpc_channel_args_find_string( |
| 67 |
client_args, GRPC_ARG_HTTP_PROXY_AUTH_CREDS); |
| 68 |
std::string proxy_uri; |
| 69 |
if (proxy_auth_str == nullptr) { |
| 70 |
proxy_uri = absl::StrFormat( |
| 71 |
"http://%s", grpc_end2end_http_proxy_get_proxy_name(ffd->proxy)); |
| 72 |
} else { |
| 73 |
proxy_uri = |
| 74 |
absl::StrFormat("http://%s@%s", proxy_auth_str, |
| 75 |
grpc_end2end_http_proxy_get_proxy_name(ffd->proxy)); |
| 76 |
} |
| 77 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 78 |
f->client = grpc_channel_create(ffd->server_addr.c_str(), creds, |
| 79 |
grpc_core::ChannelArgs::FromC(client_args) |
| 80 |
.Set(GRPC_ARG_HTTP_PROXY, proxy_uri) |
| 81 |
.ToC() |
| 82 |
.get()); |
| 83 |
grpc_channel_credentials_release(creds); |
| 84 |
GPR_ASSERT(f->client); |
| 85 |
} |
| 86 |
|
| 87 |
void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, |
| 88 |
const grpc_channel_args* server_args) { |
| 89 |
fullstack_fixture_data* ffd = |
| 90 |
static_cast<fullstack_fixture_data*>(f->fixture_data); |
| 91 |
if (f->server) { |
| 92 |
grpc_server_destroy(f->server); |
| 93 |
} |
| 94 |
f->server = grpc_server_create(server_args, nullptr); |
| 95 |
grpc_server_register_completion_queue(f->server, f->cq, nullptr); |
| 96 |
grpc_server_credentials* server_creds = |
| 97 |
grpc_insecure_server_credentials_create(); |
| 98 |
GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->server_addr.c_str(), |
| 99 |
server_creds)); |
| 100 |
grpc_server_credentials_release(server_creds); |
| 101 |
grpc_server_start(f->server); |
| 102 |
} |
| 103 |
|
| 104 |
void chttp2_tear_down_fullstack(grpc_end2end_test_fixture* f) { |
| 105 |
fullstack_fixture_data* ffd = |
| 106 |
static_cast<fullstack_fixture_data*>(f->fixture_data); |
| 107 |
delete ffd; |
| 108 |
} |
| 109 |
|
| 110 |
/* All test configurations */ |
| 111 |
static grpc_end2end_test_config configs[] = { |
| 112 |
{"chttp2/fullstack", |
| 113 |
FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | |
| 114 |
FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL | |
| 115 |
FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, |
| 116 |
nullptr, chttp2_create_fixture_fullstack, chttp2_init_client_fullstack, |
| 117 |
chttp2_init_server_fullstack, chttp2_tear_down_fullstack}, |
| 118 |
}; |
| 119 |
|
| 120 |
int main(int argc, char** argv) { |
| 121 |
size_t i; |
| 122 |
|
| 123 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 124 |
grpc_end2end_tests_pre_init(); |
| 125 |
grpc_init(); |
| 126 |
|
| 127 |
for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
| 128 |
grpc_end2end_tests(argc, argv, configs[i]); |
| 129 |
} |
| 130 |
|
| 131 |
grpc_shutdown(); |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |