1 /*
2 *
3 * Copyright 2016 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 <arpa/inet.h>
20 #include <openssl/err.h>
21 #include <openssl/ssl.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25
26 #include <grpc/grpc.h>
27 #include <grpc/grpc_security.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31 #include <grpc/support/sync.h>
32
33 #include "src/core/lib/iomgr/load_file.h"
34 #include "test/core/util/port.h"
35 #include "test/core/util/test_config.h"
36
37 #include "src/core/lib/channel/handshaker_factory.h"
38 #include "src/core/lib/channel/handshaker_registry.h"
39 #include "src/core/lib/security/transport/security_handshaker.h"
40
41 #include "test/core/handshake/server_ssl_common.h"
42
43 /* The purpose of this test is to exercise the case when a
44 * grpc *security_handshaker* begins its handshake with data already
45 * in the read buffer of the handshaker arg. This scenario is created by
46 * adding a fake "readahead" handshaker at the beginning of the server's
47 * handshaker list, which just reads from the connection and then places
48 * read bytes into the read buffer of the handshake arg (to be passed down
49 * to the security_handshaker). This test is meant to protect code relying on
50 * this functionality that lives outside of this repo. */
51
readahead_handshaker_destroy(grpc_handshaker * handshaker)52 static void readahead_handshaker_destroy(grpc_handshaker* handshaker) {
53 gpr_free(handshaker);
54 }
55
readahead_handshaker_shutdown(grpc_handshaker * handshaker,grpc_error * error)56 static void readahead_handshaker_shutdown(grpc_handshaker* handshaker,
57 grpc_error* error) {}
58
readahead_handshaker_do_handshake(grpc_handshaker * handshaker,grpc_tcp_server_acceptor * acceptor,grpc_closure * on_handshake_done,grpc_handshaker_args * args)59 static void readahead_handshaker_do_handshake(
60 grpc_handshaker* handshaker, grpc_tcp_server_acceptor* acceptor,
61 grpc_closure* on_handshake_done, grpc_handshaker_args* args) {
62 grpc_endpoint_read(args->endpoint, args->read_buffer, on_handshake_done);
63 }
64
65 const grpc_handshaker_vtable readahead_handshaker_vtable = {
66 readahead_handshaker_destroy, readahead_handshaker_shutdown,
67 readahead_handshaker_do_handshake, "read_ahead"};
68
readahead_handshaker_create()69 static grpc_handshaker* readahead_handshaker_create() {
70 grpc_handshaker* h =
71 static_cast<grpc_handshaker*>(gpr_zalloc(sizeof(grpc_handshaker)));
72 grpc_handshaker_init(&readahead_handshaker_vtable, h);
73 return h;
74 }
75
readahead_handshaker_factory_add_handshakers(grpc_handshaker_factory * hf,const grpc_channel_args * args,grpc_handshake_manager * handshake_mgr)76 static void readahead_handshaker_factory_add_handshakers(
77 grpc_handshaker_factory* hf, const grpc_channel_args* args,
78 grpc_handshake_manager* handshake_mgr) {
79 grpc_handshake_manager_add(handshake_mgr, readahead_handshaker_create());
80 }
81
readahead_handshaker_factory_destroy(grpc_handshaker_factory * handshaker_factory)82 static void readahead_handshaker_factory_destroy(
83 grpc_handshaker_factory* handshaker_factory) {}
84
85 static const grpc_handshaker_factory_vtable
86 readahead_handshaker_factory_vtable = {
87 readahead_handshaker_factory_add_handshakers,
88 readahead_handshaker_factory_destroy};
89
main(int argc,char * argv[])90 int main(int argc, char* argv[]) {
91 grpc_handshaker_factory readahead_handshaker_factory = {
92 &readahead_handshaker_factory_vtable};
93 grpc_init();
94 grpc_handshaker_factory_register(true /* at_start */, HANDSHAKER_SERVER,
95 &readahead_handshaker_factory);
96 const char* full_alpn_list[] = {"grpc-exp", "h2"};
97 GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
98 grpc_shutdown();
99 return 0;
100 }
101