aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/handshake
diff options
context:
space:
mode:
Diffstat (limited to 'test/core/handshake')
-rw-r--r--test/core/handshake/BUILD39
-rw-r--r--test/core/handshake/client_ssl.cc (renamed from test/core/handshake/client_ssl.c)78
-rw-r--r--test/core/handshake/readahead_handshaker_server_ssl.cc103
-rw-r--r--test/core/handshake/server_ssl.cc57
-rw-r--r--test/core/handshake/server_ssl_common.cc (renamed from test/core/handshake/server_ssl.c)75
-rw-r--r--test/core/handshake/server_ssl_common.h36
6 files changed, 299 insertions, 89 deletions
diff --git a/test/core/handshake/BUILD b/test/core/handshake/BUILD
index 8e462cfc5b..a3276b9343 100644
--- a/test/core/handshake/BUILD
+++ b/test/core/handshake/BUILD
@@ -20,8 +20,8 @@ licenses(["notice"]) # Apache v2
grpc_cc_test(
name = "client_ssl",
- srcs = ["client_ssl.c"],
- language = "C",
+ srcs = ["client_ssl.cc"],
+ language = "C++",
data = [
"//src/core/tsi/test_creds:ca.pem",
"//src/core/tsi/test_creds:server1.key",
@@ -35,16 +35,47 @@ grpc_cc_test(
],
)
+grpc_cc_library(
+ name = "server_ssl_common",
+ hdrs = ["server_ssl_common.h"],
+ srcs = ["server_ssl_common.cc"],
+ deps = [
+ "//:gpr",
+ "//:grpc",
+ "//test/core/util:gpr_test_util",
+ "//test/core/util:grpc_test_util",
+ ],
+)
+
grpc_cc_test(
name = "server_ssl",
- srcs = ["server_ssl.c"],
- language = "C",
+ srcs = ["server_ssl.cc"],
+ language = "C++",
+ data = [
+ "//src/core/tsi/test_creds:ca.pem",
+ "//src/core/tsi/test_creds:server1.key",
+ "//src/core/tsi/test_creds:server1.pem",
+ ],
+ deps = [
+ ":server_ssl_common",
+ "//:gpr",
+ "//:grpc",
+ "//test/core/util:gpr_test_util",
+ "//test/core/util:grpc_test_util",
+ ],
+)
+
+grpc_cc_test(
+ name = "handshake_server_with_readahead_handshaker",
+ srcs = ["readahead_handshaker_server_ssl.cc"],
+ language = "C++",
data = [
"//src/core/tsi/test_creds:ca.pem",
"//src/core/tsi/test_creds:server1.key",
"//src/core/tsi/test_creds:server1.pem",
],
deps = [
+ ":server_ssl_common",
"//:gpr",
"//:grpc",
"//test/core/util:gpr_test_util",
diff --git a/test/core/handshake/client_ssl.c b/test/core/handshake/client_ssl.cc
index de660fe1c4..2b149a73b3 100644
--- a/test/core/handshake/client_ssl.c
+++ b/test/core/handshake/client_ssl.cc
@@ -46,13 +46,13 @@
// Arguments for TLS server thread.
typedef struct {
int socket;
- char *alpn_preferred;
+ char* alpn_preferred;
} server_args;
// Based on https://wiki.openssl.org/index.php/Simple_TLS_Server.
// Pick an arbitrary unused port and return it in *out_port. Return
// an fd>=0 on success.
-static int create_socket(int *out_port) {
+static int create_socket(int* out_port) {
int s;
struct sockaddr_in addr;
socklen_t addr_len;
@@ -68,7 +68,7 @@ static int create_socket(int *out_port) {
return -1;
}
- if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Unable to bind");
gpr_log(GPR_ERROR, "%s", "Unable to bind to any port");
close(s);
@@ -82,7 +82,7 @@ static int create_socket(int *out_port) {
}
addr_len = sizeof(addr);
- if (getsockname(s, (struct sockaddr *)&addr, &addr_len) != 0 ||
+ if (getsockname(s, (struct sockaddr*)&addr, &addr_len) != 0 ||
addr_len > sizeof(addr)) {
perror("getsockname");
gpr_log(GPR_ERROR, "%s", "Unable to get socket local address");
@@ -96,19 +96,19 @@ static int create_socket(int *out_port) {
// Server callback during ALPN negotiation. See man page for
// SSL_CTX_set_alpn_select_cb.
-static int alpn_select_cb(SSL *ssl, const uint8_t **out, uint8_t *out_len,
- const uint8_t *in, unsigned in_len, void *arg) {
- const uint8_t *alpn_preferred = (const uint8_t *)arg;
+static int alpn_select_cb(SSL* ssl, const uint8_t** out, uint8_t* out_len,
+ const uint8_t* in, unsigned in_len, void* arg) {
+ const uint8_t* alpn_preferred = (const uint8_t*)arg;
*out = alpn_preferred;
- *out_len = (uint8_t)strlen((char *)alpn_preferred);
+ *out_len = (uint8_t)strlen((char*)alpn_preferred);
// Validate that the ALPN list includes "h2" and "grpc-exp", that "grpc-exp"
// precedes "h2".
bool grpc_exp_seen = false;
bool h2_seen = false;
- const char *inp = (const char *)in;
- const char *in_end = inp + in_len;
+ const char* inp = (const char*)in;
+ const char* in_end = inp + in_len;
while (inp < in_end) {
const size_t length = (size_t)*inp++;
if (length == strlen("grpc-exp") && strncmp(inp, "grpc-exp", length) == 0) {
@@ -132,14 +132,14 @@ static int alpn_select_cb(SSL *ssl, const uint8_t **out, uint8_t *out_len,
// Minimal TLS server. This is largely based on the example at
// https://wiki.openssl.org/index.php/Simple_TLS_Server and the gRPC core
// internals in src/core/tsi/ssl_transport_security.c.
-static void server_thread(void *arg) {
- const server_args *args = (server_args *)arg;
+static void server_thread(void* arg) {
+ const server_args* args = (server_args*)arg;
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
- const SSL_METHOD *method = TLSv1_2_server_method();
- SSL_CTX *ctx = SSL_CTX_new(method);
+ const SSL_METHOD* method = TLSv1_2_server_method();
+ SSL_CTX* ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
@@ -158,7 +158,7 @@ static void server_thread(void *arg) {
// Set the cipher list to match the one expressed in
// src/core/tsi/ssl_transport_security.c.
- const char *cipher_list =
+ const char* cipher_list =
"ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-"
"SHA384:ECDHE-RSA-AES256-GCM-SHA384";
if (!SSL_CTX_set_cipher_list(ctx, cipher_list)) {
@@ -175,14 +175,14 @@ static void server_thread(void *arg) {
gpr_log(GPR_INFO, "Server listening");
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
- const int client = accept(sock, (struct sockaddr *)&addr, &len);
+ const int client = accept(sock, (struct sockaddr*)&addr, &len);
if (client < 0) {
perror("Unable to accept");
abort();
}
// Establish a SSL* and accept at SSL layer.
- SSL *ssl = SSL_new(ctx);
+ SSL* ssl = SSL_new(ctx);
GPR_ASSERT(ssl);
SSL_set_fd(ssl, client);
if (SSL_accept(ssl) <= 0) {
@@ -208,7 +208,7 @@ static void server_thread(void *arg) {
// establishes a TLS handshake via the core library to the server. The TLS
// server validates ALPN aspects of the handshake and supplies the protocol
// specified in the server_alpn_preferred argument to the client.
-static bool client_ssl_test(char *server_alpn_preferred) {
+static bool client_ssl_test(char* server_alpn_preferred) {
bool success = true;
grpc_init();
@@ -230,8 +230,7 @@ static bool client_ssl_test(char *server_alpn_preferred) {
gpr_thd_options thdopt = gpr_thd_options_default();
gpr_thd_id thdid;
gpr_thd_options_set_joinable(&thdopt);
- server_args args = {.socket = server_socket,
- .alpn_preferred = server_alpn_preferred};
+ server_args args = {server_socket, server_alpn_preferred};
GPR_ASSERT(gpr_thd_new(&thdid, server_thread, &args, &thdopt));
// Load key pair and establish client SSL credentials.
@@ -243,24 +242,25 @@ static bool client_ssl_test(char *server_alpn_preferred) {
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
- const char *ca_cert = (const char *)GRPC_SLICE_START_PTR(ca_slice);
- pem_key_cert_pair.private_key = (const char *)GRPC_SLICE_START_PTR(key_slice);
- pem_key_cert_pair.cert_chain = (const char *)GRPC_SLICE_START_PTR(cert_slice);
- grpc_channel_credentials *ssl_creds =
- grpc_ssl_credentials_create(ca_cert, &pem_key_cert_pair, NULL);
+ const char* ca_cert = (const char*)GRPC_SLICE_START_PTR(ca_slice);
+ pem_key_cert_pair.private_key = (const char*)GRPC_SLICE_START_PTR(key_slice);
+ pem_key_cert_pair.cert_chain = (const char*)GRPC_SLICE_START_PTR(cert_slice);
+ grpc_channel_credentials* ssl_creds =
+ grpc_ssl_credentials_create(ca_cert, &pem_key_cert_pair, nullptr);
// Establish a channel pointing at the TLS server. Since the gRPC runtime is
// lazy, this won't necessarily establish a connection yet.
- char *target;
+ char* target;
gpr_asprintf(&target, "127.0.0.1:%d", port);
- grpc_arg ssl_name_override = {GRPC_ARG_STRING,
- GRPC_SSL_TARGET_NAME_OVERRIDE_ARG,
- {"foo.test.google.fr"}};
+ grpc_arg ssl_name_override = {
+ GRPC_ARG_STRING,
+ const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
+ {const_cast<char*>("foo.test.google.fr")}};
grpc_channel_args grpc_args;
grpc_args.num_args = 1;
grpc_args.args = &ssl_name_override;
- grpc_channel *channel =
- grpc_secure_channel_create(ssl_creds, target, &grpc_args, NULL);
+ grpc_channel* channel =
+ grpc_secure_channel_create(ssl_creds, target, &grpc_args, nullptr);
GPR_ASSERT(channel);
gpr_free(target);
@@ -274,13 +274,13 @@ static bool client_ssl_test(char *server_alpn_preferred) {
// completed and we know that the client's ALPN list satisfied the server.
int retries = 10;
grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
- grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
+ grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
while (state != GRPC_CHANNEL_READY && retries-- > 0) {
grpc_channel_watch_connectivity_state(
- channel, state, grpc_timeout_seconds_to_deadline(3), cq, NULL);
+ channel, state, grpc_timeout_seconds_to_deadline(3), cq, nullptr);
gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(5);
- grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
+ grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
state =
grpc_channel_check_connectivity_state(channel, 0 /* try_to_connect */);
@@ -303,21 +303,21 @@ static bool client_ssl_test(char *server_alpn_preferred) {
return success;
}
-int main(int argc, char *argv[]) {
+int main(int argc, char* argv[]) {
// Handshake succeeeds when the server has grpc-exp as the ALPN preference.
- GPR_ASSERT(client_ssl_test("grpc-exp"));
+ GPR_ASSERT(client_ssl_test(const_cast<char*>("grpc-exp")));
// Handshake succeeeds when the server has h2 as the ALPN preference. This
// covers legacy gRPC servers which don't support grpc-exp.
- GPR_ASSERT(client_ssl_test("h2"));
+ GPR_ASSERT(client_ssl_test(const_cast<char*>("h2")));
// Handshake fails when the server uses a fake protocol as its ALPN
// preference. This validates the client is correctly validating ALPN returns
// and sanity checks the client_ssl_test.
- GPR_ASSERT(!client_ssl_test("foo"));
+ GPR_ASSERT(!client_ssl_test(const_cast<char*>("foo")));
return 0;
}
#else /* GRPC_POSIX_SOCKET */
-int main(int argc, char **argv) { return 1; }
+int main(int argc, char** argv) { return 1; }
#endif /* GRPC_POSIX_SOCKET */
diff --git a/test/core/handshake/readahead_handshaker_server_ssl.cc b/test/core/handshake/readahead_handshaker_server_ssl.cc
new file mode 100644
index 0000000000..2810082837
--- /dev/null
+++ b/test/core/handshake/readahead_handshaker_server_ssl.cc
@@ -0,0 +1,103 @@
+/*
+ *
+ * Copyright 2016 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <arpa/inet.h>
+#include <openssl/err.h>
+#include <openssl/ssl.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <grpc/grpc.h>
+#include <grpc/grpc_security.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/sync.h>
+#include <grpc/support/thd.h>
+#include "src/core/lib/iomgr/load_file.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+#include "src/core/lib/channel/handshaker_factory.h"
+#include "src/core/lib/channel/handshaker_registry.h"
+#include "src/core/lib/security/transport/security_handshaker.h"
+
+#include "test/core/handshake/server_ssl_common.h"
+
+/* The purpose of this test is to exercise the case when a
+ * grpc *security_handshaker* begins its handshake with data already
+ * in the read buffer of the handshaker arg. This scenario is created by
+ * adding a fake "readahead" handshaker at the beginning of the server's
+ * handshaker list, which just reads from the connection and then places
+ * read bytes into the read buffer of the handshake arg (to be passed down
+ * to the security_handshaker). This test is meant to protect code relying on
+ * this functionality that lives outside of this repo. */
+
+static void readahead_handshaker_destroy(grpc_exec_ctx* ctx,
+ grpc_handshaker* handshaker) {
+ gpr_free(handshaker);
+}
+
+static void readahead_handshaker_shutdown(grpc_exec_ctx* ctx,
+ grpc_handshaker* handshaker,
+ grpc_error* error) {}
+
+static void readahead_handshaker_do_handshake(
+ grpc_exec_ctx* ctx, grpc_handshaker* handshaker,
+ grpc_tcp_server_acceptor* acceptor, grpc_closure* on_handshake_done,
+ grpc_handshaker_args* args) {
+ grpc_endpoint_read(ctx, args->endpoint, args->read_buffer, on_handshake_done);
+}
+
+const grpc_handshaker_vtable readahead_handshaker_vtable = {
+ readahead_handshaker_destroy, readahead_handshaker_shutdown,
+ readahead_handshaker_do_handshake};
+
+static grpc_handshaker* readahead_handshaker_create(grpc_exec_ctx* ctx) {
+ grpc_handshaker* h = (grpc_handshaker*)gpr_zalloc(sizeof(grpc_handshaker));
+ grpc_handshaker_init(&readahead_handshaker_vtable, h);
+ return h;
+}
+
+static void readahead_handshaker_factory_add_handshakers(
+ grpc_exec_ctx* exec_ctx, grpc_handshaker_factory* hf,
+ const grpc_channel_args* args, grpc_handshake_manager* handshake_mgr) {
+ grpc_handshake_manager_add(handshake_mgr,
+ readahead_handshaker_create(exec_ctx));
+}
+
+static void readahead_handshaker_factory_destroy(
+ grpc_exec_ctx* exec_ctx, grpc_handshaker_factory* handshaker_factory) {}
+
+static const grpc_handshaker_factory_vtable
+ readahead_handshaker_factory_vtable = {
+ readahead_handshaker_factory_add_handshakers,
+ readahead_handshaker_factory_destroy};
+
+int main(int argc, char* argv[]) {
+ grpc_handshaker_factory readahead_handshaker_factory = {
+ &readahead_handshaker_factory_vtable};
+ grpc_init();
+ grpc_handshaker_factory_register(true /* at_start */, HANDSHAKER_SERVER,
+ &readahead_handshaker_factory);
+ const char* full_alpn_list[] = {"grpc-exp", "h2"};
+ GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
+ grpc_shutdown();
+ return 0;
+}
diff --git a/test/core/handshake/server_ssl.cc b/test/core/handshake/server_ssl.cc
new file mode 100644
index 0000000000..736d3e578e
--- /dev/null
+++ b/test/core/handshake/server_ssl.cc
@@ -0,0 +1,57 @@
+/*
+ *
+ * Copyright 2016 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <arpa/inet.h>
+#include <openssl/err.h>
+#include <openssl/ssl.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <grpc/grpc.h>
+#include <grpc/grpc_security.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/sync.h>
+#include <grpc/support/thd.h>
+#include "src/core/lib/iomgr/load_file.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+#include "test/core/handshake/server_ssl_common.h"
+
+int main(int argc, char* argv[]) {
+ // Handshake succeeeds when the client supplies the standard ALPN list.
+ const char* full_alpn_list[] = {"grpc-exp", "h2"};
+ GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
+ // Handshake succeeeds when the client supplies only h2 as the ALPN list. This
+ // covers legacy gRPC clients which don't support grpc-exp.
+ const char* h2_only_alpn_list[] = {"h2"};
+ GPR_ASSERT(server_ssl_test(h2_only_alpn_list, 1, "h2"));
+ // Handshake succeeds when the client supplies superfluous ALPN entries and
+ // also when h2 precedes gprc-exp.
+ const char* extra_alpn_list[] = {"foo", "h2", "bar", "grpc-exp"};
+ GPR_ASSERT(server_ssl_test(extra_alpn_list, 4, "h2"));
+ // Handshake fails when the client uses a fake protocol as its only ALPN
+ // preference. This validates the server is correctly validating ALPN
+ // and sanity checks the server_ssl_test.
+ const char* fake_alpn_list[] = {"foo"};
+ GPR_ASSERT(!server_ssl_test(fake_alpn_list, 1, "foo"));
+ return 0;
+}
diff --git a/test/core/handshake/server_ssl.c b/test/core/handshake/server_ssl_common.cc
index 85a8b4de41..599b2814e0 100644
--- a/test/core/handshake/server_ssl.c
+++ b/test/core/handshake/server_ssl_common.cc
@@ -16,6 +16,8 @@
*
*/
+#include "test/core/handshake/server_ssl_common.h"
+
#include <arpa/inet.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
@@ -55,7 +57,7 @@ static int create_socket(int port) {
return -1;
}
- if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Unable to connect");
return -1;
}
@@ -64,8 +66,8 @@ static int create_socket(int port) {
}
// Simple gRPC server. This listens until client_handshake_complete occurs.
-static void server_thread(void *arg) {
- const int port = *(int *)arg;
+static void server_thread(void* arg) {
+ const int port = *(int*)arg;
// Load key pair and establish server SSL credentials.
grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
@@ -76,22 +78,22 @@ static void server_thread(void *arg) {
grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
- const char *ca_cert = (const char *)GRPC_SLICE_START_PTR(ca_slice);
- pem_key_cert_pair.private_key = (const char *)GRPC_SLICE_START_PTR(key_slice);
- pem_key_cert_pair.cert_chain = (const char *)GRPC_SLICE_START_PTR(cert_slice);
- grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create(
- ca_cert, &pem_key_cert_pair, 1, 0, NULL);
+ const char* ca_cert = (const char*)GRPC_SLICE_START_PTR(ca_slice);
+ pem_key_cert_pair.private_key = (const char*)GRPC_SLICE_START_PTR(key_slice);
+ pem_key_cert_pair.cert_chain = (const char*)GRPC_SLICE_START_PTR(cert_slice);
+ grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
+ ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
// Start server listening on local port.
- char *addr;
+ char* addr;
gpr_asprintf(&addr, "127.0.0.1:%d", port);
- grpc_server *server = grpc_server_create(NULL, NULL);
+ grpc_server* server = grpc_server_create(nullptr, nullptr);
GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
free(addr);
- grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
+ grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
- grpc_server_register_completion_queue(server, cq, NULL);
+ grpc_server_register_completion_queue(server, cq, nullptr);
grpc_server_start(server);
// Wait a bounded number of time until client_handshake_complete is set,
@@ -99,16 +101,16 @@ static void server_thread(void *arg) {
int retries = 10;
while (!gpr_event_get(&client_handshake_complete) && retries-- > 0) {
const gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(1);
- grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
+ grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
}
gpr_log(GPR_INFO, "Shutting down server");
- grpc_server_shutdown_and_notify(server, cq, NULL);
+ grpc_server_shutdown_and_notify(server, cq, nullptr);
grpc_completion_queue_shutdown(cq);
const gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(5);
- grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL);
+ grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
grpc_server_destroy(server);
@@ -123,8 +125,8 @@ static void server_thread(void *arg) {
// TLS handshake via a minimal TLS client. The TLS client has configurable (via
// alpn_list) ALPN settings and can probe at the supported ALPN preferences
// using this (via alpn_expected).
-static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
- const char *alpn_expected) {
+bool server_ssl_test(const char* alpn_list[], unsigned int alpn_list_len,
+ const char* alpn_expected) {
bool success = true;
grpc_init();
@@ -140,8 +142,8 @@ static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
- const SSL_METHOD *method = TLSv1_2_client_method();
- SSL_CTX *ctx = SSL_CTX_new(method);
+ const SSL_METHOD* method = TLSv1_2_client_method();
+ SSL_CTX* ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
@@ -160,7 +162,7 @@ static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
// Set the cipher list to match the one expressed in
// src/core/tsi/ssl_transport_security.c.
- const char *cipher_list =
+ const char* cipher_list =
"ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-"
"SHA384:ECDHE-RSA-AES256-GCM-SHA384";
if (!SSL_CTX_set_cipher_list(ctx, cipher_list)) {
@@ -175,8 +177,9 @@ static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
for (unsigned int i = 0; i < alpn_list_len; ++i) {
alpn_protos_len += (unsigned int)strlen(alpn_list[i]);
}
- unsigned char *alpn_protos = gpr_malloc(alpn_protos_len);
- unsigned char *p = alpn_protos;
+ unsigned char* alpn_protos =
+ static_cast<unsigned char*>(gpr_malloc(alpn_protos_len));
+ unsigned char* p = alpn_protos;
for (unsigned int i = 0; i < alpn_list_len; ++i) {
const uint8_t len = (uint8_t)strlen(alpn_list[i]);
*p++ = len;
@@ -199,7 +202,7 @@ static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
gpr_log(GPR_INFO, "Connected to server on port %d", port);
// Establish a SSL* and connect at SSL layer.
- SSL *ssl = SSL_new(ctx);
+ SSL* ssl = SSL_new(ctx);
GPR_ASSERT(ssl);
SSL_set_fd(ssl, sock);
if (SSL_connect(ssl) <= 0) {
@@ -209,12 +212,12 @@ static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
} else {
gpr_log(GPR_INFO, "Handshake successful.");
// Validate ALPN preferred by server matches alpn_expected.
- const unsigned char *alpn_selected;
+ const unsigned char* alpn_selected;
unsigned int alpn_selected_len;
SSL_get0_alpn_selected(ssl, &alpn_selected, &alpn_selected_len);
if (strlen(alpn_expected) != alpn_selected_len ||
- strncmp((const char *)alpn_selected, alpn_expected,
- alpn_selected_len) != 0) {
+ strncmp((const char*)alpn_selected, alpn_expected, alpn_selected_len) !=
+ 0) {
gpr_log(GPR_ERROR, "Unexpected ALPN protocol preference");
success = false;
}
@@ -233,23 +236,3 @@ static bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len,
return success;
}
-
-int main(int argc, char *argv[]) {
- // Handshake succeeeds when the client supplies the standard ALPN list.
- const char *full_alpn_list[] = {"grpc-exp", "h2"};
- GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
- // Handshake succeeeds when the client supplies only h2 as the ALPN list. This
- // covers legacy gRPC clients which don't support grpc-exp.
- const char *h2_only_alpn_list[] = {"h2"};
- GPR_ASSERT(server_ssl_test(h2_only_alpn_list, 1, "h2"));
- // Handshake succeeds when the client supplies superfluous ALPN entries and
- // also when h2 precedes gprc-exp.
- const char *extra_alpn_list[] = {"foo", "h2", "bar", "grpc-exp"};
- GPR_ASSERT(server_ssl_test(extra_alpn_list, 4, "h2"));
- // Handshake fails when the client uses a fake protocol as its only ALPN
- // preference. This validates the server is correctly validating ALPN
- // and sanity checks the server_ssl_test.
- const char *fake_alpn_list[] = {"foo"};
- GPR_ASSERT(!server_ssl_test(fake_alpn_list, 1, "foo"));
- return 0;
-}
diff --git a/test/core/handshake/server_ssl_common.h b/test/core/handshake/server_ssl_common.h
new file mode 100644
index 0000000000..77865a408f
--- /dev/null
+++ b/test/core/handshake/server_ssl_common.h
@@ -0,0 +1,36 @@
+/*
+ *
+ * Copyright 2016 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_SERVER_SSL_COMMON_H
+#define GRPC_SERVER_SSL_COMMON_H
+
+#include <grpc/grpc.h>
+#include <grpc/grpc_security.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/sync.h>
+#include <grpc/support/thd.h>
+#include "src/core/lib/iomgr/load_file.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+bool server_ssl_test(const char* alpn_list[], unsigned int alpn_list_len,
+ const char* alpn_expected);
+
+#endif // GRPC_SERVER_SSL_COMMON_H