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