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