| 1 |
// |
| 2 |
// |
| 3 |
// Copyright 2020 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 <grpc/grpc.h> |
| 24 |
#include <grpc/grpc_security.h> |
| 25 |
#include <grpc/status.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
#include "src/core/lib/channel/channel_args.h" |
| 29 |
#include "src/core/lib/gprpp/host_port.h" |
| 30 |
#include "test/core/end2end/end2end_tests.h" |
| 31 |
#include "test/core/util/port.h" |
| 32 |
#include "test/core/util/test_config.h" |
| 33 |
|
| 34 |
namespace { |
| 35 |
|
| 36 |
struct Chttp2InsecureFullstackFixtureData { |
| 37 |
std::string localaddr; |
| 38 |
}; |
| 39 |
|
| 40 |
grpc_end2end_test_fixture Chttp2CreateFixtureInsecureFullstack( |
| 41 |
const grpc_channel_args* /*client_args*/, |
| 42 |
const grpc_channel_args* /*server_args*/) { |
| 43 |
grpc_end2end_test_fixture f; |
| 44 |
int port = grpc_pick_unused_port_or_die(); |
| 45 |
Chttp2InsecureFullstackFixtureData* ffd = |
| 46 |
new Chttp2InsecureFullstackFixtureData(); |
| 47 |
memset(&f, 0, sizeof(f)); |
| 48 |
ffd->localaddr = grpc_core::JoinHostPort("localhost", port); |
| 49 |
f.fixture_data = ffd; |
| 50 |
f.cq = grpc_completion_queue_create_for_next(nullptr); |
| 51 |
|
| 52 |
return f; |
| 53 |
} |
| 54 |
|
| 55 |
void Chttp2InitClientInsecureFullstack(grpc_end2end_test_fixture* f, |
| 56 |
const grpc_channel_args* client_args) { |
| 57 |
Chttp2InsecureFullstackFixtureData* ffd = |
| 58 |
static_cast<Chttp2InsecureFullstackFixtureData*>(f->fixture_data); |
| 59 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 60 |
f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args); |
| 61 |
grpc_channel_credentials_release(creds); |
| 62 |
GPR_ASSERT(f->client); |
| 63 |
} |
| 64 |
|
| 65 |
void ProcessAuthFailure(void* state, grpc_auth_context* /*ctx*/, |
| 66 |
const grpc_metadata* /*md*/, size_t /*md_count*/, |
| 67 |
grpc_process_auth_metadata_done_cb cb, |
| 68 |
void* user_data) { |
| 69 |
GPR_ASSERT(state == nullptr); |
| 70 |
cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr); |
| 71 |
} |
| 72 |
|
| 73 |
void Chttp2InitServerInsecureFullstack(grpc_end2end_test_fixture* f, |
| 74 |
const grpc_channel_args* server_args) { |
| 75 |
Chttp2InsecureFullstackFixtureData* ffd = |
| 76 |
static_cast<Chttp2InsecureFullstackFixtureData*>(f->fixture_data); |
| 77 |
if (f->server) { |
| 78 |
grpc_server_destroy(f->server); |
| 79 |
} |
| 80 |
f->server = grpc_server_create(server_args, nullptr); |
| 81 |
grpc_server_register_completion_queue(f->server, f->cq, nullptr); |
| 82 |
grpc_server_credentials* server_creds = |
| 83 |
grpc_insecure_server_credentials_create(); |
| 84 |
if (grpc_channel_args_find(server_args, FAIL_AUTH_CHECK_SERVER_ARG_NAME) != |
| 85 |
nullptr) { |
| 86 |
grpc_auth_metadata_processor processor = {ProcessAuthFailure, nullptr, |
| 87 |
nullptr}; |
| 88 |
grpc_server_credentials_set_auth_metadata_processor(server_creds, |
| 89 |
processor); |
| 90 |
} |
| 91 |
GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(), |
| 92 |
server_creds)); |
| 93 |
grpc_server_credentials_release(server_creds); |
| 94 |
grpc_server_start(f->server); |
| 95 |
} |
| 96 |
|
| 97 |
void Chttp2TearDownInsecureFullstack(grpc_end2end_test_fixture* f) { |
| 98 |
Chttp2InsecureFullstackFixtureData* ffd = |
| 99 |
static_cast<Chttp2InsecureFullstackFixtureData*>(f->fixture_data); |
| 100 |
delete ffd; |
| 101 |
} |
| 102 |
|
| 103 |
/* All test configurations */ |
| 104 |
grpc_end2end_test_config configs[] = { |
| 105 |
{"chttp2/insecure_fullstack", |
| 106 |
FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | |
| 107 |
FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL | |
| 108 |
FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER | |
| 109 |
FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS_LEVEL_INSECURE, |
| 110 |
nullptr, Chttp2CreateFixtureInsecureFullstack, |
| 111 |
Chttp2InitClientInsecureFullstack, Chttp2InitServerInsecureFullstack, |
| 112 |
Chttp2TearDownInsecureFullstack}, |
| 113 |
}; |
| 114 |
|
| 115 |
} // namespace |
| 116 |
|
| 117 |
int main(int argc, char** argv) { |
| 118 |
size_t i; |
| 119 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 120 |
grpc_end2end_tests_pre_init(); |
| 121 |
grpc_init(); |
| 122 |
for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
| 123 |
grpc_end2end_tests(argc, argv, configs[i]); |
| 124 |
} |
| 125 |
grpc_shutdown(); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |