blob: a4f182e64350cef2166145c1d4848cc229eeaefd [file] [log] [blame]
Alexander Polcyn248c4f52017-11-13 16:31:27 -08001/*
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#include <grpc/support/thd.h>
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
Yash Tibrewal8cf14702017-12-06 09:47:54 -080052static void readahead_handshaker_destroy(grpc_handshaker* handshaker) {
Alexander Polcyn248c4f52017-11-13 16:31:27 -080053 gpr_free(handshaker);
54}
55
Yash Tibrewal8cf14702017-12-06 09:47:54 -080056static void readahead_handshaker_shutdown(grpc_handshaker* handshaker,
Alexander Polcyn248c4f52017-11-13 16:31:27 -080057 grpc_error* error) {}
58
59static void readahead_handshaker_do_handshake(
Yash Tibrewal8cf14702017-12-06 09:47:54 -080060 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);
Alexander Polcyn248c4f52017-11-13 16:31:27 -080063}
64
65const grpc_handshaker_vtable readahead_handshaker_vtable = {
66 readahead_handshaker_destroy, readahead_handshaker_shutdown,
67 readahead_handshaker_do_handshake};
68
Yash Tibrewal8cf14702017-12-06 09:47:54 -080069static grpc_handshaker* readahead_handshaker_create() {
Noah Eisenbe82e642018-02-09 09:16:55 -080070 grpc_handshaker* h = static_cast<grpc_handshaker*>(gpr_zalloc(sizeof(grpc_handshaker)));
Alexander Polcyn248c4f52017-11-13 16:31:27 -080071 grpc_handshaker_init(&readahead_handshaker_vtable, h);
72 return h;
73}
74
75static void readahead_handshaker_factory_add_handshakers(
Yash Tibrewal8cf14702017-12-06 09:47:54 -080076 grpc_handshaker_factory* hf, const grpc_channel_args* args,
77 grpc_handshake_manager* handshake_mgr) {
78 grpc_handshake_manager_add(handshake_mgr, readahead_handshaker_create());
Alexander Polcyn248c4f52017-11-13 16:31:27 -080079}
80
81static void readahead_handshaker_factory_destroy(
Yash Tibrewal8cf14702017-12-06 09:47:54 -080082 grpc_handshaker_factory* handshaker_factory) {}
Alexander Polcyn248c4f52017-11-13 16:31:27 -080083
84static const grpc_handshaker_factory_vtable
85 readahead_handshaker_factory_vtable = {
86 readahead_handshaker_factory_add_handshakers,
87 readahead_handshaker_factory_destroy};
88
89int main(int argc, char* argv[]) {
90 grpc_handshaker_factory readahead_handshaker_factory = {
91 &readahead_handshaker_factory_vtable};
92 grpc_init();
93 grpc_handshaker_factory_register(true /* at_start */, HANDSHAKER_SERVER,
94 &readahead_handshaker_factory);
95 const char* full_alpn_list[] = {"grpc-exp", "h2"};
96 GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
97 grpc_shutdown();
98 return 0;
99}