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