blob: 9aced1c4af56050772e4aef002a799030663158d [file] [log] [blame]
Craig Tillerabd20f32015-12-13 11:48:59 -08001/*
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
Craig Tillerabd20f32015-12-13 11:48:59 -080034#include <stdio.h>
Craig Tiller5b1e6592016-03-11 15:05:30 -080035#include <string.h>
Craig Tillerabd20f32015-12-13 11:48:59 -080036
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"
Craig Tillerabd20f32015-12-13 11:48:59 -080045#include "test/core/end2end/cq_verifier.h"
Craig Tiller5b1e6592016-03-11 15:05:30 -080046#include "test/core/util/port.h"
Craig Tillerabd20f32015-12-13 11:48:59 -080047#include "test/core/util/test_config.h"
48
Craig Tiller7536af02015-12-22 13:49:30 -080049static void *tag(intptr_t t) { return (void *)t; }
Craig Tillerabd20f32015-12-13 11:48:59 -080050
51static 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;
Craig Tiller5b1e6592016-03-11 15:05:30 -080089 op->flags = GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY;
Craig Tillerabd20f32015-12-13 11:48:59 -080090 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);
Craig Tillerff767732015-12-13 21:52:33 -0800125 grpc_channel_credentials_release(ssl_creds);
Craig Tillerabd20f32015-12-13 11:48:59 -0800126}
127
128int main(int argc, char **argv) {
129 char *me = argv[0];
130 char *lslash = strrchr(me, '/');
131 char *lunder = strrchr(me, '_');
132 char *tmp;
133 char root[1024];
134 char test[64];
135 int port = grpc_pick_unused_port_or_die();
136 char *args[10];
137 int status;
138 size_t i;
139 gpr_subprocess *svr;
140 /* figure out where we are */
141 if (lslash) {
142 memcpy(root, me, (size_t)(lslash - me));
143 root[lslash - me] = 0;
144 } else {
145 strcpy(root, ".");
146 }
147 /* figure out our test name */
148 tmp = lunder - 1;
149 while (*tmp != '_') tmp--;
150 tmp++;
Craig Tiller56369b42015-12-13 19:27:03 -0800151 memcpy(test, tmp, (size_t)(lunder - tmp));
Craig Tillerabd20f32015-12-13 11:48:59 -0800152 /* start the server */
153 gpr_asprintf(&args[0], "%s/bad_ssl_%s_server%s", root, test,
154 gpr_subprocess_binary_extension());
155 args[1] = "--bind";
156 gpr_join_host_port(&args[2], "::", port);
157 svr = gpr_subprocess_create(4, (const char **)args);
158 gpr_free(args[0]);
159
160 for (i = 3; i <= 4; i++) {
161 grpc_init();
162 run_test(args[2], i);
163 grpc_shutdown();
164 }
165 gpr_free(args[2]);
Craig Tillera3f298f2015-12-16 19:42:09 -0800166
Craig Tillerabd20f32015-12-13 11:48:59 -0800167 gpr_subprocess_interrupt(svr);
168 status = gpr_subprocess_join(svr);
169 gpr_subprocess_destroy(svr);
170 return status;
171}