| 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 <stdint.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/grpc_security.h> |
| 27 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 28 |
#include <grpc/slice.h> |
| 29 |
#include <grpc/status.h> |
| 30 |
#include <grpc/support/alloc.h> |
| 31 |
#include <grpc/support/log.h> |
| 32 |
#include <grpc/support/string_util.h> |
| 33 |
|
| 34 |
#include "src/core/lib/gprpp/global_config_generic.h" |
| 35 |
#include "src/core/lib/gprpp/host_port.h" |
| 36 |
#include "src/core/lib/security/security_connector/ssl_utils_config.h" |
| 37 |
#include "test/core/end2end/cq_verifier.h" |
| 38 |
#include "test/core/util/port.h" |
| 39 |
#include "test/core/util/subprocess.h" |
| 40 |
#include "test/core/util/test_config.h" |
| 41 |
|
| 42 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 43 |
|
| 44 |
static void run_test(const char* target, size_t nops) { |
| 45 |
grpc_channel_credentials* ssl_creds = |
| 46 |
grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr); |
| 47 |
grpc_channel* channel; |
| 48 |
grpc_call* c; |
| 49 |
|
| 50 |
grpc_metadata_array initial_metadata_recv; |
| 51 |
grpc_metadata_array trailing_metadata_recv; |
| 52 |
grpc_slice details; |
| 53 |
grpc_status_code status; |
| 54 |
grpc_call_error error; |
| 55 |
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5); |
| 56 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 57 |
grpc_core::CqVerifier cqv(cq); |
| 58 |
|
| 59 |
grpc_op ops[6]; |
| 60 |
grpc_op* op; |
| 61 |
|
| 62 |
grpc_arg ssl_name_override = { |
| 63 |
GRPC_ARG_STRING, |
| 64 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 65 |
{const_cast<char*>("foo.test.google.fr")}}; |
| 66 |
grpc_channel_args args; |
| 67 |
|
| 68 |
args.num_args = 1; |
| 69 |
args.args = &ssl_name_override; |
| 70 |
|
| 71 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 72 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 73 |
|
| 74 |
channel = grpc_channel_create(target, ssl_creds, &args); |
| 75 |
grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr:1234"); |
| 76 |
c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, |
| 77 |
grpc_slice_from_static_string("/foo"), &host, |
| 78 |
deadline, nullptr); |
| 79 |
|
| 80 |
memset(ops, 0, sizeof(ops)); |
| 81 |
op = ops; |
| 82 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 83 |
op->data.send_initial_metadata.count = 0; |
| 84 |
op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 85 |
op->reserved = nullptr; |
| 86 |
op++; |
| 87 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 88 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 89 |
op->data.recv_status_on_client.status = &status; |
| 90 |
op->data.recv_status_on_client.status_details = &details; |
| 91 |
op->flags = 0; |
| 92 |
op->reserved = nullptr; |
| 93 |
op++; |
| 94 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 95 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 96 |
op->flags = 0; |
| 97 |
op->reserved = nullptr; |
| 98 |
op++; |
| 99 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 100 |
op->flags = 0; |
| 101 |
op->reserved = nullptr; |
| 102 |
op++; |
| 103 |
error = grpc_call_start_batch(c, ops, nops, tag(1), nullptr); |
| 104 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 105 |
|
| 106 |
cqv.Expect(tag(1), true); |
| 107 |
cqv.Verify(); |
| 108 |
|
| 109 |
GPR_ASSERT(status != GRPC_STATUS_OK); |
| 110 |
|
| 111 |
grpc_call_unref(c); |
| 112 |
grpc_slice_unref(details); |
| 113 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 114 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 115 |
|
| 116 |
grpc_channel_destroy(channel); |
| 117 |
grpc_completion_queue_destroy(cq); |
| 118 |
grpc_channel_credentials_release(ssl_creds); |
| 119 |
} |
| 120 |
|
| 121 |
int main(int argc, char** argv) { |
| 122 |
char* me = argv[0]; |
| 123 |
char* lslash = strrchr(me, '/'); |
| 124 |
char* lunder = strrchr(me, '_'); |
| 125 |
char* tmp; |
| 126 |
char root[1024]; |
| 127 |
char test[64]; |
| 128 |
int port = grpc_pick_unused_port_or_die(); |
| 129 |
char* args[10]; |
| 130 |
int status; |
| 131 |
size_t i; |
| 132 |
gpr_subprocess* svr; |
| 133 |
/* figure out where we are */ |
| 134 |
if (lslash) { |
| 135 |
memcpy(root, me, static_cast<size_t>(lslash - me)); |
| 136 |
root[lslash - me] = 0; |
| 137 |
} else { |
| 138 |
strcpy(root, "."); |
| 139 |
} |
| 140 |
if (argc == 2) { |
| 141 |
GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, argv[1]); |
| 142 |
} |
| 143 |
/* figure out our test name */ |
| 144 |
tmp = lunder - 1; |
| 145 |
while (*tmp != '_') tmp--; |
| 146 |
tmp++; |
| 147 |
memcpy(test, tmp, static_cast<size_t>(lunder - tmp)); |
| 148 |
/* start the server */ |
| 149 |
gpr_asprintf(&args[0], "%s/bad_ssl_%s_server%s", root, test, |
| 150 |
gpr_subprocess_binary_extension()); |
| 151 |
args[1] = const_cast<char*>("--bind"); |
| 152 |
std::string joined = grpc_core::JoinHostPort("::", port); |
| 153 |
args[2] = const_cast<char*>(joined.c_str()); |
| 154 |
svr = gpr_subprocess_create(4, const_cast<const char**>(args)); |
| 155 |
gpr_free(args[0]); |
| 156 |
|
| 157 |
for (i = 3; i <= 4; i++) { |
| 158 |
grpc_init(); |
| 159 |
run_test(args[2], i); |
| 160 |
grpc_shutdown(); |
| 161 |
} |
| 162 |
|
| 163 |
gpr_subprocess_interrupt(svr); |
| 164 |
status = gpr_subprocess_join(svr); |
| 165 |
gpr_subprocess_destroy(svr); |
| 166 |
return status; |
| 167 |
} |