| 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 <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/gprpp/host_port.h" |
| 29 |
#include "src/core/lib/security/credentials/fake/fake_credentials.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 |
struct fullstack_secure_fixture_data { |
| 35 |
std::string localaddr; |
| 36 |
}; |
| 37 |
|
| 38 |
static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( |
| 39 |
const grpc_channel_args* /*client_args*/, |
| 40 |
const grpc_channel_args* /*server_args*/) { |
| 41 |
grpc_end2end_test_fixture f; |
| 42 |
int port = grpc_pick_unused_port_or_die(); |
| 43 |
fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); |
| 44 |
memset(&f, 0, sizeof(f)); |
| 45 |
ffd->localaddr = grpc_core::JoinHostPort("localhost", port); |
| 46 |
|
| 47 |
f.fixture_data = ffd; |
| 48 |
f.cq = grpc_completion_queue_create_for_next(nullptr); |
| 49 |
|
| 50 |
return f; |
| 51 |
} |
| 52 |
|
| 53 |
static void process_auth_failure(void* state, grpc_auth_context* /*ctx*/, |
| 54 |
const grpc_metadata* /*md*/, |
| 55 |
size_t /*md_count*/, |
| 56 |
grpc_process_auth_metadata_done_cb cb, |
| 57 |
void* user_data) { |
| 58 |
GPR_ASSERT(state == nullptr); |
| 59 |
cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr); |
| 60 |
} |
| 61 |
|
| 62 |
static void chttp2_init_client_secure_fullstack( |
| 63 |
grpc_end2end_test_fixture* f, const grpc_channel_args* client_args, |
| 64 |
grpc_channel_credentials* creds) { |
| 65 |
fullstack_secure_fixture_data* ffd = |
| 66 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 67 |
f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args); |
| 68 |
GPR_ASSERT(f->client != nullptr); |
| 69 |
grpc_channel_credentials_release(creds); |
| 70 |
} |
| 71 |
|
| 72 |
static void chttp2_init_server_secure_fullstack( |
| 73 |
grpc_end2end_test_fixture* f, const grpc_channel_args* server_args, |
| 74 |
grpc_server_credentials* server_creds) { |
| 75 |
fullstack_secure_fixture_data* ffd = |
| 76 |
static_cast<fullstack_secure_fixture_data*>(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 |
GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(), |
| 83 |
server_creds)); |
| 84 |
grpc_server_credentials_release(server_creds); |
| 85 |
grpc_server_start(f->server); |
| 86 |
} |
| 87 |
|
| 88 |
void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) { |
| 89 |
fullstack_secure_fixture_data* ffd = |
| 90 |
static_cast<fullstack_secure_fixture_data*>(f->fixture_data); |
| 91 |
delete ffd; |
| 92 |
} |
| 93 |
|
| 94 |
static void chttp2_init_client_fake_secure_fullstack( |
| 95 |
grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) { |
| 96 |
grpc_channel_credentials* fake_ts_creds = |
| 97 |
grpc_fake_transport_security_credentials_create(); |
| 98 |
chttp2_init_client_secure_fullstack(f, client_args, fake_ts_creds); |
| 99 |
} |
| 100 |
|
| 101 |
static int fail_server_auth_check(const grpc_channel_args* server_args) { |
| 102 |
size_t i; |
| 103 |
if (server_args == nullptr) return 0; |
| 104 |
for (i = 0; i < server_args->num_args; i++) { |
| 105 |
if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == |
| 106 |
0) { |
| 107 |
return 1; |
| 108 |
} |
| 109 |
} |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static void chttp2_init_server_fake_secure_fullstack( |
| 114 |
grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) { |
| 115 |
grpc_server_credentials* fake_ts_creds = |
| 116 |
grpc_fake_transport_security_server_credentials_create(); |
| 117 |
if (fail_server_auth_check(server_args)) { |
| 118 |
grpc_auth_metadata_processor processor = {process_auth_failure, nullptr, |
| 119 |
nullptr}; |
| 120 |
grpc_server_credentials_set_auth_metadata_processor(fake_ts_creds, |
| 121 |
processor); |
| 122 |
} |
| 123 |
chttp2_init_server_secure_fullstack(f, server_args, fake_ts_creds); |
| 124 |
} |
| 125 |
|
| 126 |
/* All test configurations */ |
| 127 |
|
| 128 |
static grpc_end2end_test_config configs[] = { |
| 129 |
{"chttp2/fake_secure_fullstack", |
| 130 |
FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | |
| 131 |
FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL | |
| 132 |
FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER | |
| 133 |
FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS_LEVEL_INSECURE, |
| 134 |
nullptr, chttp2_create_fixture_secure_fullstack, |
| 135 |
chttp2_init_client_fake_secure_fullstack, |
| 136 |
chttp2_init_server_fake_secure_fullstack, |
| 137 |
chttp2_tear_down_secure_fullstack}, |
| 138 |
}; |
| 139 |
|
| 140 |
int main(int argc, char** argv) { |
| 141 |
size_t i; |
| 142 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 143 |
grpc_end2end_tests_pre_init(); |
| 144 |
grpc_init(); |
| 145 |
|
| 146 |
for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { |
| 147 |
grpc_end2end_tests(argc, argv, configs[i]); |
| 148 |
} |
| 149 |
|
| 150 |
grpc_shutdown(); |
| 151 |
|
| 152 |
return 0; |
| 153 |
} |