Craig Tiller | abd20f3 | 2015-12-13 11:48:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <string.h> |
| 35 | #include <stdio.h> |
| 36 | |
| 37 | #include <grpc/grpc.h> |
| 38 | #include <grpc/grpc_security.h> |
| 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/host_port.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/string_util.h> |
| 43 | #include <grpc/support/subprocess.h> |
| 44 | #include "src/core/support/string.h" |
| 45 | #include "test/core/util/port.h" |
| 46 | #include "test/core/end2end/cq_verifier.h" |
| 47 | #include "test/core/util/test_config.h" |
| 48 | |
| 49 | static void *tag(gpr_intptr t) { return (void *)t; } |
| 50 | |
| 51 | static void run_test(const char *target, size_t nops) { |
| 52 | grpc_channel_credentials *ssl_creds = |
| 53 | grpc_ssl_credentials_create(NULL, NULL, NULL); |
| 54 | grpc_channel *channel; |
| 55 | grpc_call *c; |
| 56 | |
| 57 | grpc_metadata_array initial_metadata_recv; |
| 58 | grpc_metadata_array trailing_metadata_recv; |
| 59 | char *details = NULL; |
| 60 | size_t details_capacity = 0; |
| 61 | grpc_status_code status; |
| 62 | grpc_call_error error; |
| 63 | gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5); |
| 64 | grpc_completion_queue *cq = grpc_completion_queue_create(NULL); |
| 65 | cq_verifier *cqv = cq_verifier_create(cq); |
| 66 | |
| 67 | grpc_op ops[6]; |
| 68 | grpc_op *op; |
| 69 | |
| 70 | grpc_arg ssl_name_override = {GRPC_ARG_STRING, |
| 71 | GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, |
| 72 | {"foo.test.google.fr"}}; |
| 73 | grpc_channel_args args; |
| 74 | |
| 75 | args.num_args = 1; |
| 76 | args.args = &ssl_name_override; |
| 77 | |
| 78 | grpc_metadata_array_init(&initial_metadata_recv); |
| 79 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 80 | |
| 81 | channel = grpc_secure_channel_create(ssl_creds, target, &args, NULL); |
| 82 | c = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, |
| 83 | "/foo", "foo.test.google.fr:1234", deadline, |
| 84 | NULL); |
| 85 | |
| 86 | op = ops; |
| 87 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 88 | op->data.send_initial_metadata.count = 0; |
| 89 | op->flags = 0; |
| 90 | op->reserved = NULL; |
| 91 | op++; |
| 92 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 93 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 94 | op->data.recv_status_on_client.status = &status; |
| 95 | op->data.recv_status_on_client.status_details = &details; |
| 96 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 97 | op->flags = 0; |
| 98 | op->reserved = NULL; |
| 99 | op++; |
| 100 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 101 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 102 | op->flags = 0; |
| 103 | op->reserved = NULL; |
| 104 | op++; |
| 105 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 106 | op->flags = 0; |
| 107 | op->reserved = NULL; |
| 108 | op++; |
| 109 | error = grpc_call_start_batch(c, ops, nops, tag(1), NULL); |
| 110 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 111 | |
| 112 | cq_expect_completion(cqv, tag(1), 1); |
| 113 | cq_verify(cqv); |
| 114 | |
| 115 | GPR_ASSERT(status != GRPC_STATUS_OK); |
| 116 | |
| 117 | grpc_call_destroy(c); |
| 118 | gpr_free(details); |
| 119 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 120 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 121 | |
| 122 | grpc_channel_destroy(channel); |
| 123 | grpc_completion_queue_destroy(cq); |
| 124 | cq_verifier_destroy(cqv); |
| 125 | } |
| 126 | |
| 127 | int main(int argc, char **argv) { |
| 128 | char *me = argv[0]; |
| 129 | char *lslash = strrchr(me, '/'); |
| 130 | char *lunder = strrchr(me, '_'); |
| 131 | char *tmp; |
| 132 | char root[1024]; |
| 133 | char test[64]; |
| 134 | int port = grpc_pick_unused_port_or_die(); |
| 135 | char *args[10]; |
| 136 | int status; |
| 137 | size_t i; |
| 138 | gpr_subprocess *svr; |
| 139 | /* figure out where we are */ |
| 140 | if (lslash) { |
| 141 | memcpy(root, me, (size_t)(lslash - me)); |
| 142 | root[lslash - me] = 0; |
| 143 | } else { |
| 144 | strcpy(root, "."); |
| 145 | } |
| 146 | /* figure out our test name */ |
| 147 | tmp = lunder - 1; |
| 148 | while (*tmp != '_') tmp--; |
| 149 | tmp++; |
Craig Tiller | 56369b4 | 2015-12-13 19:27:03 -0800 | [diff] [blame^] | 150 | memcpy(test, tmp, (size_t)(lunder - tmp)); |
Craig Tiller | abd20f3 | 2015-12-13 11:48:59 -0800 | [diff] [blame] | 151 | /* start the server */ |
| 152 | gpr_asprintf(&args[0], "%s/bad_ssl_%s_server%s", root, test, |
| 153 | gpr_subprocess_binary_extension()); |
| 154 | args[1] = "--bind"; |
| 155 | gpr_join_host_port(&args[2], "::", port); |
| 156 | svr = gpr_subprocess_create(4, (const char **)args); |
| 157 | gpr_free(args[0]); |
| 158 | |
| 159 | for (i = 3; i <= 4; i++) { |
| 160 | grpc_init(); |
| 161 | run_test(args[2], i); |
| 162 | grpc_shutdown(); |
| 163 | } |
| 164 | gpr_free(args[2]); |
| 165 | |
| 166 | gpr_subprocess_interrupt(svr); |
| 167 | status = gpr_subprocess_join(svr); |
| 168 | gpr_subprocess_destroy(svr); |
| 169 | return status; |
| 170 | } |