diff options
Diffstat (limited to 'test/core')
37 files changed, 1379 insertions, 244 deletions
diff --git a/test/core/bad_client/gen_build_yaml.py b/test/core/bad_client/gen_build_yaml.py index fb86525b1a..32ab3f2137 100755 --- a/test/core/bad_client/gen_build_yaml.py +++ b/test/core/bad_client/gen_build_yaml.py @@ -83,6 +83,7 @@ def main(): 'secure': 'no', 'src': ['test/core/bad_client/tests/%s.c' % t], 'vs_proj_dir': 'test', + 'exclude_iomgrs': ['uv'], 'deps': [ 'bad_client_test', 'grpc_test_util_unsecure', diff --git a/test/core/client_channel/resolvers/dns_resolver_connectivity_test.c b/test/core/client_channel/resolvers/dns_resolver_connectivity_test.c index 07723c229d..ffa167a0e7 100644 --- a/test/core/client_channel/resolvers/dns_resolver_connectivity_test.c +++ b/test/core/client_channel/resolvers/dns_resolver_connectivity_test.c @@ -37,6 +37,7 @@ #include <grpc/support/alloc.h> #include "src/core/ext/client_channel/resolver_registry.h" +#include "src/core/lib/channel/channel_args.h" #include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/timer.h" #include "test/core/util/test_config.h" @@ -103,7 +104,7 @@ int main(int argc, char **argv) { grpc_resolver *resolver = create_resolver("dns:test"); - grpc_resolver_result *result = (grpc_resolver_result *)1; + grpc_channel_args *result = (grpc_channel_args *)1; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; gpr_event ev1; @@ -122,7 +123,7 @@ int main(int argc, char **argv) { GPR_ASSERT(wait_loop(30, &ev2)); GPR_ASSERT(result != NULL); - grpc_resolver_result_unref(&exec_ctx, result); + grpc_channel_args_destroy(result); GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test"); grpc_exec_ctx_finish(&exec_ctx); diff --git a/test/core/client_channel/resolvers/sockaddr_resolver_test.c b/test/core/client_channel/resolvers/sockaddr_resolver_test.c index c39052cd9d..ebf311ab83 100644 --- a/test/core/client_channel/resolvers/sockaddr_resolver_test.c +++ b/test/core/client_channel/resolvers/sockaddr_resolver_test.c @@ -38,21 +38,23 @@ #include <grpc/support/string_util.h> #include "src/core/ext/client_channel/resolver_registry.h" -#include "src/core/ext/client_channel/resolver_result.h" +#include "src/core/lib/channel/channel_args.h" #include "test/core/util/test_config.h" typedef struct on_resolution_arg { char *expected_server_name; - grpc_resolver_result *resolver_result; + grpc_channel_args *resolver_result; } on_resolution_arg; void on_resolution_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { on_resolution_arg *res = arg; - const char *server_name = - grpc_resolver_result_get_server_name(res->resolver_result); - GPR_ASSERT(strcmp(res->expected_server_name, server_name) == 0); - grpc_resolver_result_unref(exec_ctx, res->resolver_result); + const grpc_arg *channel_arg = + grpc_channel_args_find(res->resolver_result, GRPC_ARG_SERVER_NAME); + GPR_ASSERT(channel_arg != NULL); + GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); + GPR_ASSERT(strcmp(res->expected_server_name, channel_arg->value.string) == 0); + grpc_channel_args_destroy(res->resolver_result); } static void test_succeeds(grpc_resolver_factory *factory, const char *string) { diff --git a/test/core/client_channel/set_initial_connect_string_test.c b/test/core/client_channel/set_initial_connect_string_test.c index b7bd67567c..379f30fb9f 100644 --- a/test/core/client_channel/set_initial_connect_string_test.c +++ b/test/core/client_channel/set_initial_connect_string_test.c @@ -30,6 +30,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ + +/* With the addition of a libuv endpoint, sockaddr.h now includes uv.h when + using that endpoint. Because of various transitive includes in uv.h, + including windows.h on Windows, uv.h must be included before other system + headers. Therefore, sockaddr.h must always be included first */ +#include "src/core/lib/iomgr/sockaddr.h" + #include <string.h> #include <grpc/grpc.h> @@ -93,15 +100,14 @@ static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp, grpc_endpoint_read(exec_ctx, tcp, &state.temp_incoming_buffer, &on_read); } -static void set_magic_initial_string(struct sockaddr **addr, size_t *addr_len, +static void set_magic_initial_string(grpc_resolved_address **addr, gpr_slice *connect_string) { GPR_ASSERT(addr); - GPR_ASSERT(addr_len); + GPR_ASSERT((*addr)->len); *connect_string = gpr_slice_from_copied_string(magic_connect_string); } -static void reset_addr_and_set_magic_string(struct sockaddr **addr, - size_t *addr_len, +static void reset_addr_and_set_magic_string(grpc_resolved_address **addr, gpr_slice *connect_string) { struct sockaddr_in target; *connect_string = gpr_slice_from_copied_string(magic_connect_string); @@ -109,9 +115,9 @@ static void reset_addr_and_set_magic_string(struct sockaddr **addr, target.sin_family = AF_INET; target.sin_addr.s_addr = htonl(INADDR_LOOPBACK); target.sin_port = htons((uint16_t)server_port); - *addr_len = sizeof(target); - *addr = (struct sockaddr *)gpr_malloc(sizeof(target)); - memcpy(*addr, &target, sizeof(target)); + (*addr)->len = sizeof(target); + *addr = (grpc_resolved_address *)gpr_malloc(sizeof(grpc_resolved_address)); + memcpy((*addr)->addr, &target, sizeof(target)); } static gpr_timespec n_sec_deadline(int seconds) { diff --git a/test/core/end2end/bad_server_response_test.c b/test/core/end2end/bad_server_response_test.c index 5ed0eb64d2..9a3e1301f9 100644 --- a/test/core/end2end/bad_server_response_test.c +++ b/test/core/end2end/bad_server_response_test.c @@ -30,6 +30,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ + +/* With the addition of a libuv endpoint, sockaddr.h now includes uv.h when + using that endpoint. Because of various transitive includes in uv.h, + including windows.h on Windows, uv.h must be included before other system + headers. Therefore, sockaddr.h must always be included first */ +#include "src/core/lib/iomgr/sockaddr.h" + #include <string.h> #include <grpc/grpc.h> @@ -40,7 +47,6 @@ #include <grpc/support/thd.h> // #include "src/core/ext/transport/chttp2/transport/internal.h" -#include "src/core/lib/iomgr/sockaddr.h" #include "src/core/lib/support/string.h" #include "test/core/end2end/cq_verifier.h" #include "test/core/util/port.h" diff --git a/test/core/end2end/cq_verifier_internal.h b/test/core/end2end/cq_verifier_internal.h new file mode 100644 index 0000000000..1549608469 --- /dev/null +++ b/test/core/end2end/cq_verifier_internal.h @@ -0,0 +1,47 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_TEST_CORE_END2END_CQ_VERIFIER_INTERNAL_H +#define GRPC_TEST_CORE_END2END_CQ_VERIFIER_INTERNAL_H + +#include "test/core/end2end/cq_verifier.h" + +typedef struct expectation expectation; + +expectation *cq_verifier_get_first_expectation(cq_verifier *v); + +void cq_verifier_set_first_expectation(cq_verifier *v, expectation *e); + +grpc_event cq_verifier_next_event(cq_verifier *v, int timeout_seconds); + +#endif /* GRPC_TEST_CORE_END2END_CQ_VERIFIER_INTERNAL_H */ diff --git a/test/core/end2end/cq_verifier_native.c b/test/core/end2end/cq_verifier_native.c new file mode 100644 index 0000000000..b1fcb4beed --- /dev/null +++ b/test/core/end2end/cq_verifier_native.c @@ -0,0 +1,74 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* This check is for testing only. */ +#ifndef GRPC_UV + +#include "test/core/end2end/cq_verifier_internal.h" + +/* the verifier itself */ +struct cq_verifier { + /* bound completion queue */ + grpc_completion_queue *cq; + /* start of expectation list */ + expectation *first_expectation; + uv_timer_t timer; +}; + +cq_verifier *cq_verifier_create(grpc_completion_queue *cq) { + cq_verifier *v = gpr_malloc(sizeof(cq_verifier)); + v->cq = cq; + cq_verifier_set_first_expectation(v, NULL); + return v; +} + +void cq_verifier_destroy(cq_verifier *v) { + cq_verify(v); + gpr_free(v); +} + +expectation *cq_verifier_get_first_expectation(cq_verifier *v) { + return v->first_expectation; +} + +void cq_verifier_set_first_expectation(cq_verifier *v, expectation *e) { + v->first_expectation = e; +} + +grpc_event cq_verifier_next_event(cq_verifier *v, int timeout_seconds) { + const gpr_timespec deadline = + GRPC_TIMEOUT_SECONDS_TO_DEADLINE(timeout_seconds); + return grpc_completion_queue_next(v->cq, deadline, NULL); +} + +#endif /* GRPC_UV */ diff --git a/test/core/end2end/cq_verifier_uv.c b/test/core/end2end/cq_verifier_uv.c new file mode 100644 index 0000000000..2b5a2ca495 --- /dev/null +++ b/test/core/end2end/cq_verifier_uv.c @@ -0,0 +1,112 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <grpc/support/port_platform.h> + +#ifdef GRPC_UV + +#include <uv.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> + +#include "test/core/end2end/cq_verifier_internal.h" + +typedef enum timer_state { + TIMER_STARTED, + TIMER_TRIGGERED, + TIMER_CLOSED +} timer_state; + +/* the verifier itself */ +struct cq_verifier { + /* bound completion queue */ + grpc_completion_queue *cq; + /* start of expectation list */ + expectation *first_expectation; + uv_timer_t timer; +}; + +cq_verifier *cq_verifier_create(grpc_completion_queue *cq) { + cq_verifier *v = gpr_malloc(sizeof(cq_verifier)); + v->cq = cq; + v->first_expectation = NULL; + uv_timer_init(uv_default_loop(), &v->timer); + v->timer.data = (void *)TIMER_STARTED; + return v; +} + +static void timer_close_cb(uv_handle_t *handle) { + handle->data = (void *)TIMER_CLOSED; +} + +void cq_verifier_destroy(cq_verifier *v) { + cq_verify(v); + uv_close((uv_handle_t *)&v->timer, timer_close_cb); + while ((timer_state)v->timer.data != TIMER_CLOSED) { + uv_run(uv_default_loop(), UV_RUN_NOWAIT); + } + gpr_free(v); +} + +expectation *cq_verifier_get_first_expectation(cq_verifier *v) { + return v->first_expectation; +} + +void cq_verifier_set_first_expectation(cq_verifier *v, expectation *e) { + v->first_expectation = e; +} + +static void timer_run_cb(uv_timer_t *timer) { + timer->data = (void *)TIMER_TRIGGERED; +} + +grpc_event cq_verifier_next_event(cq_verifier *v, int timeout_seconds) { + uint64_t timeout_ms = + timeout_seconds < 0 ? 0 : (uint64_t)timeout_seconds * 1000; + grpc_event ev; + v->timer.data = (void *)TIMER_STARTED; + uv_timer_start(&v->timer, timer_run_cb, timeout_ms, 0); + ev = grpc_completion_queue_next(v->cq, gpr_inf_past(GPR_CLOCK_MONOTONIC), + NULL); + // Stop the loop if the timer goes off or we get a non-timeout event + while (((timer_state)v->timer.data != TIMER_TRIGGERED) && + ev.type == GRPC_QUEUE_TIMEOUT) { + uv_run(uv_default_loop(), UV_RUN_ONCE); + ev = grpc_completion_queue_next(v->cq, gpr_inf_past(GPR_CLOCK_MONOTONIC), + NULL); + } + return ev; +} + +#endif /* GRPC_UV */ diff --git a/test/core/end2end/dualstack_socket_test.c b/test/core/end2end/dualstack_socket_test.c index cb07ca535b..f427202a7b 100644 --- a/test/core/end2end/dualstack_socket_test.c +++ b/test/core/end2end/dualstack_socket_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include <string.h> #include <grpc/grpc.h> @@ -353,3 +358,9 @@ int main(int argc, char **argv) { return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/end2end/fake_resolver.c b/test/core/end2end/fake_resolver.c index 5669ce51e8..a2343cb709 100644 --- a/test/core/end2end/fake_resolver.c +++ b/test/core/end2end/fake_resolver.c @@ -42,13 +42,14 @@ #include <grpc/support/port_platform.h> #include <grpc/support/string_util.h> -#include "src/core/ext/client_channel/method_config.h" +#include "src/core/ext/client_channel/lb_policy_factory.h" #include "src/core/ext/client_channel/parse_address.h" #include "src/core/ext/client_channel/resolver_registry.h" #include "src/core/lib/channel/channel_args.h" #include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/unix_sockets_posix.h" #include "src/core/lib/support/string.h" +#include "src/core/lib/transport/method_config.h" // // fake_resolver @@ -59,7 +60,7 @@ typedef struct { grpc_resolver base; // passed-in parameters - char* target_name; // the path component of the uri passed in + grpc_channel_args* channel_args; grpc_lb_addresses* addresses; char* lb_policy_name; grpc_method_config_table* method_config_table; @@ -71,14 +72,14 @@ typedef struct { // pending next completion, or NULL grpc_closure* next_completion; // target result address for next completion - grpc_resolver_result** target_result; + grpc_channel_args** target_result; } fake_resolver; static void fake_resolver_destroy(grpc_exec_ctx* exec_ctx, grpc_resolver* gr) { fake_resolver* r = (fake_resolver*)gr; gpr_mu_destroy(&r->mu); - gpr_free(r->target_name); - grpc_lb_addresses_destroy(r->addresses, NULL /* user_data_destroy */); + grpc_channel_args_destroy(r->channel_args); + grpc_lb_addresses_destroy(r->addresses); gpr_free(r->lb_policy_name); grpc_method_config_table_unref(r->method_config_table); gpr_free(r); @@ -100,16 +101,21 @@ static void fake_resolver_maybe_finish_next_locked(grpc_exec_ctx* exec_ctx, fake_resolver* r) { if (r->next_completion != NULL && !r->published) { r->published = true; - grpc_channel_args* lb_policy_args = NULL; + grpc_arg new_args[3]; + size_t num_args = 0; + new_args[num_args++] = grpc_lb_addresses_create_channel_arg(r->addresses); if (r->method_config_table != NULL) { - const grpc_arg arg = + new_args[num_args++] = grpc_method_config_table_create_channel_arg(r->method_config_table); - lb_policy_args = grpc_channel_args_copy_and_add(NULL /* src */, &arg, 1); } - *r->target_result = grpc_resolver_result_create( - r->target_name, - grpc_lb_addresses_copy(r->addresses, NULL /* user_data_copy */), - r->lb_policy_name, lb_policy_args); + if (r->lb_policy_name != NULL) { + new_args[num_args].type = GRPC_ARG_STRING; + new_args[num_args].key = GRPC_ARG_LB_POLICY_NAME; + new_args[num_args].value.string = r->lb_policy_name; + ++num_args; + } + *r->target_result = + grpc_channel_args_copy_and_add(r->channel_args, new_args, num_args); grpc_exec_ctx_sched(exec_ctx, r->next_completion, GRPC_ERROR_NONE, NULL); r->next_completion = NULL; } @@ -125,7 +131,7 @@ static void fake_resolver_channel_saw_error(grpc_exec_ctx* exec_ctx, } static void fake_resolver_next(grpc_exec_ctx* exec_ctx, grpc_resolver* resolver, - grpc_resolver_result** target_result, + grpc_channel_args** target_result, grpc_closure* on_complete) { fake_resolver* r = (fake_resolver*)resolver; gpr_mu_lock(&r->mu); @@ -168,16 +174,14 @@ static grpc_resolver* fake_resolver_create(grpc_resolver_factory* factory, gpr_slice_buffer path_parts; gpr_slice_buffer_init(&path_parts); gpr_slice_split(path_slice, ",", &path_parts); - grpc_lb_addresses* addresses = grpc_lb_addresses_create(path_parts.count); + grpc_lb_addresses* addresses = + grpc_lb_addresses_create(path_parts.count, NULL /* user_data_vtable */); bool errors_found = false; for (size_t i = 0; i < addresses->num_addresses; i++) { grpc_uri ith_uri = *args->uri; char* part_str = gpr_dump_slice(path_parts.slices[i], GPR_DUMP_ASCII); ith_uri.path = part_str; - if (!parse_ipv4( - &ith_uri, - (struct sockaddr_storage*)(&addresses->addresses[i].address.addr), - &addresses->addresses[i].address.len)) { + if (!parse_ipv4(&ith_uri, &addresses->addresses[i].address)) { errors_found = true; } gpr_free(part_str); @@ -187,7 +191,7 @@ static grpc_resolver* fake_resolver_create(grpc_resolver_factory* factory, gpr_slice_buffer_destroy(&path_parts); gpr_slice_unref(path_slice); if (errors_found) { - grpc_lb_addresses_destroy(addresses, NULL /* user_data_destroy */); + grpc_lb_addresses_destroy(addresses); return NULL; } // Construct method config table. @@ -232,7 +236,12 @@ static grpc_resolver* fake_resolver_create(grpc_resolver_factory* factory, // Instantiate resolver. fake_resolver* r = gpr_malloc(sizeof(fake_resolver)); memset(r, 0, sizeof(*r)); - r->target_name = gpr_strdup(args->uri->path); + grpc_arg server_name_arg; + server_name_arg.type = GRPC_ARG_STRING; + server_name_arg.key = GRPC_ARG_SERVER_NAME; + server_name_arg.value.string = args->uri->path; + r->channel_args = + grpc_channel_args_copy_and_add(args->args, &server_name_arg, 1); r->addresses = addresses; r->lb_policy_name = gpr_strdup(grpc_uri_get_query_arg(args->uri, "lb_policy")); diff --git a/test/core/end2end/fixtures/h2_fd.c b/test/core/end2end/fixtures/h2_fd.c index 5a58fe34cd..2dfa54fb6b 100644 --- a/test/core/end2end/fixtures/h2_fd.c +++ b/test/core/end2end/fixtures/h2_fd.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "test/core/end2end/end2end_tests.h" #include <fcntl.h> @@ -129,3 +134,9 @@ int main(int argc, char **argv) { return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/end2end/fixtures/h2_full+pipe.c b/test/core/end2end/fixtures/h2_full+pipe.c index 5d4c44ce94..e0026a7940 100644 --- a/test/core/end2end/fixtures/h2_full+pipe.c +++ b/test/core/end2end/fixtures/h2_full+pipe.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test requires posix wakeup fds +#ifdef GRPC_POSIX_WAKEUP_FD + #include "test/core/end2end/end2end_tests.h" #include <string.h> @@ -121,3 +126,9 @@ int main(int argc, char **argv) { return 0; } + +#else /* GRPC_POSIX_WAKEUP_FD */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_WAKEUP_FD */ diff --git a/test/core/end2end/fixtures/h2_full+trace.c b/test/core/end2end/fixtures/h2_full+trace.c index 64aa53a5ae..eadc62d963 100644 --- a/test/core/end2end/fixtures/h2_full+trace.c +++ b/test/core/end2end/fixtures/h2_full+trace.c @@ -31,9 +31,14 @@ * */ +#include "src/core/lib/iomgr/port.h" + #include "test/core/end2end/end2end_tests.h" #include <string.h> +#ifdef GRPC_POSIX_SOCKET +#include <unistd.h> +#endif #include <grpc/support/alloc.h> #include <grpc/support/host_port.h> @@ -111,7 +116,7 @@ int main(int argc, char **argv) { code paths in trace.c to be taken */ gpr_setenv("GRPC_TRACE", "doesnt-exist,http,all"); -#ifdef GPR_POSIX_SOCKET +#ifdef GRPC_POSIX_SOCKET g_fixture_slowdown_factor = isatty(STDOUT_FILENO) ? 10.0 : 1.0; #else g_fixture_slowdown_factor = 10.0; diff --git a/test/core/end2end/fixtures/h2_sockpair+trace.c b/test/core/end2end/fixtures/h2_sockpair+trace.c index dd4376f046..15b045c33c 100644 --- a/test/core/end2end/fixtures/h2_sockpair+trace.c +++ b/test/core/end2end/fixtures/h2_sockpair+trace.c @@ -31,9 +31,14 @@ * */ +#include "src/core/lib/iomgr/port.h" + #include "test/core/end2end/end2end_tests.h" #include <string.h> +#ifdef GRPC_POSIX_SOCKET +#include <unistd.h> +#endif #include <grpc/support/alloc.h> #include <grpc/support/log.h> @@ -148,7 +153,7 @@ int main(int argc, char **argv) { /* force tracing on, with a value to force many code paths in trace.c to be taken */ gpr_setenv("GRPC_TRACE", "doesnt-exist,http,all"); -#ifdef GPR_POSIX_SOCKET +#ifdef GRPC_POSIX_SOCKET g_fixture_slowdown_factor = isatty(STDOUT_FILENO) ? 10.0 : 1.0; #else g_fixture_slowdown_factor = 10.0; diff --git a/test/core/end2end/fixtures/http_proxy.c b/test/core/end2end/fixtures/http_proxy.c index 22533b9694..b8f046c7df 100644 --- a/test/core/end2end/fixtures/http_proxy.c +++ b/test/core/end2end/fixtures/http_proxy.c @@ -33,6 +33,8 @@ #include "test/core/end2end/fixtures/http_proxy.h" +#include "src/core/lib/iomgr/sockaddr.h" + #include <string.h> #include <grpc/support/alloc.h> @@ -358,8 +360,7 @@ static void on_read_request_done(grpc_exec_ctx* exec_ctx, void* arg, gpr_now(GPR_CLOCK_MONOTONIC), gpr_time_from_seconds(10, GPR_TIMESPAN)); grpc_tcp_client_connect(exec_ctx, &conn->on_server_connect_done, &conn->server_endpoint, conn->pollset_set, - (struct sockaddr*)&resolved_addresses->addrs[0].addr, - resolved_addresses->addrs[0].len, deadline); + &resolved_addresses->addrs[0], deadline); grpc_resolved_addresses_destroy(resolved_addresses); } @@ -430,13 +431,13 @@ grpc_end2end_http_proxy* grpc_end2end_http_proxy_create() { grpc_tcp_server_create(NULL, proxy->channel_args, &proxy->server); GPR_ASSERT(error == GRPC_ERROR_NONE); // Bind to port. - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - grpc_sockaddr_set_port((struct sockaddr*)&addr, proxy_port); + grpc_resolved_address resolved_addr; + struct sockaddr_in* addr = (struct sockaddr_in*)resolved_addr.addr; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + addr->sin_family = AF_INET; + grpc_sockaddr_set_port(&resolved_addr, proxy_port); int port; - error = grpc_tcp_server_add_port(proxy->server, (struct sockaddr*)&addr, - sizeof(addr), &port); + error = grpc_tcp_server_add_port(proxy->server, &resolved_addr, &port); GPR_ASSERT(error == GRPC_ERROR_NONE); GPR_ASSERT(port == proxy_port); // Start server. diff --git a/test/core/end2end/fuzzers/api_fuzzer.c b/test/core/end2end/fuzzers/api_fuzzer.c index 96ea82d95e..4e19b76014 100644 --- a/test/core/end2end/fuzzers/api_fuzzer.c +++ b/test/core/end2end/fuzzers/api_fuzzer.c @@ -231,8 +231,8 @@ void my_resolve_address(grpc_exec_ctx *exec_ctx, const char *addr, // defined in tcp_client_posix.c extern void (*grpc_tcp_client_connect_impl)( grpc_exec_ctx *exec_ctx, grpc_closure *closure, grpc_endpoint **ep, - grpc_pollset_set *interested_parties, const struct sockaddr *addr, - size_t addr_len, gpr_timespec deadline); + grpc_pollset_set *interested_parties, const grpc_resolved_address *addr, + gpr_timespec deadline); static void sched_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure, grpc_endpoint **ep, gpr_timespec deadline); @@ -289,7 +289,7 @@ static void sched_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure, static void my_tcp_client_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure, grpc_endpoint **ep, grpc_pollset_set *interested_parties, - const struct sockaddr *addr, size_t addr_len, + const grpc_resolved_address *addr, gpr_timespec deadline) { sched_connect(exec_ctx, closure, ep, deadline); } diff --git a/test/core/end2end/gen_build_yaml.py b/test/core/end2end/gen_build_yaml.py index d3d6f5e9ce..0bbf10d475 100755 --- a/test/core/end2end/gen_build_yaml.py +++ b/test/core/end2end/gen_build_yaml.py @@ -39,19 +39,20 @@ import hashlib FixtureOptions = collections.namedtuple( 'FixtureOptions', - 'fullstack includes_proxy dns_resolver secure platforms ci_mac tracing exclude_configs') + 'fullstack includes_proxy dns_resolver secure platforms ci_mac tracing exclude_configs exclude_iomgrs') default_unsecure_fixture_options = FixtureOptions( - True, False, True, False, ['windows', 'linux', 'mac', 'posix'], True, False, []) + True, False, True, False, ['windows', 'linux', 'mac', 'posix'], True, False, [], []) socketpair_unsecure_fixture_options = default_unsecure_fixture_options._replace(fullstack=False, dns_resolver=False) default_secure_fixture_options = default_unsecure_fixture_options._replace(secure=True) -uds_fixture_options = default_unsecure_fixture_options._replace(dns_resolver=False, platforms=['linux', 'mac', 'posix']) +uds_fixture_options = default_unsecure_fixture_options._replace(dns_resolver=False, platforms=['linux', 'mac', 'posix'], exclude_iomgrs=['uv']) fd_unsecure_fixture_options = default_unsecure_fixture_options._replace( - dns_resolver=False, fullstack=False, platforms=['linux', 'mac', 'posix']) + dns_resolver=False, fullstack=False, platforms=['linux', 'mac', 'posix'], exclude_iomgrs=['uv']) # maps fixture name to whether it requires the security library END2END_FIXTURES = { 'h2_compress': default_unsecure_fixture_options, + 'h2_census': default_unsecure_fixture_options, 'h2_load_reporting': default_unsecure_fixture_options, 'h2_fakesec': default_secure_fixture_options._replace(ci_mac=False), @@ -59,28 +60,31 @@ END2END_FIXTURES = { 'h2_fd': fd_unsecure_fixture_options, 'h2_full': default_unsecure_fixture_options, 'h2_full+pipe': default_unsecure_fixture_options._replace( - platforms=['linux']), + platforms=['linux'], exclude_iomgrs=['uv']), 'h2_full+trace': default_unsecure_fixture_options._replace(tracing=True), - 'h2_http_proxy': default_unsecure_fixture_options._replace(ci_mac=False), - 'h2_oauth2': default_secure_fixture_options._replace(ci_mac=False), - 'h2_proxy': default_unsecure_fixture_options._replace(includes_proxy=True, - ci_mac=False), + 'h2_http_proxy': default_unsecure_fixture_options._replace( + ci_mac=False, exclude_iomgrs=['uv']), + 'h2_oauth2': default_secure_fixture_options._replace( + ci_mac=False, exclude_iomgrs=['uv']), + 'h2_proxy': default_unsecure_fixture_options._replace( + includes_proxy=True, ci_mac=False, exclude_iomgrs=['uv']), 'h2_sockpair_1byte': socketpair_unsecure_fixture_options._replace( - ci_mac=False, exclude_configs=['msan']), - 'h2_sockpair': socketpair_unsecure_fixture_options._replace(ci_mac=False), + ci_mac=False, exclude_configs=['msan'], exclude_iomgrs=['uv']), + 'h2_sockpair': socketpair_unsecure_fixture_options._replace( + ci_mac=False, exclude_iomgrs=['uv']), 'h2_sockpair+trace': socketpair_unsecure_fixture_options._replace( - ci_mac=False, tracing=True), + ci_mac=False, tracing=True, exclude_iomgrs=['uv']), 'h2_ssl': default_secure_fixture_options, 'h2_ssl_cert': default_secure_fixture_options, - 'h2_ssl_proxy': default_secure_fixture_options._replace(includes_proxy=True, - ci_mac=False), + 'h2_ssl_proxy': default_secure_fixture_options._replace( + includes_proxy=True, ci_mac=False, exclude_iomgrs=['uv']), 'h2_uds': uds_fixture_options, } TestOptions = collections.namedtuple( 'TestOptions', - 'needs_fullstack needs_dns proxyable secure traceable cpu_cost') -default_test_options = TestOptions(False, False, True, False, True, 1.0) + 'needs_fullstack needs_dns proxyable secure traceable cpu_cost exclude_iomgrs') +default_test_options = TestOptions(False, False, True, False, True, 1.0, []) connectivity_test_options = default_test_options._replace(needs_fullstack=True) LOWCPU = 0.1 @@ -97,8 +101,8 @@ END2END_TESTS = { 'cancel_in_a_vacuum': default_test_options._replace(cpu_cost=LOWCPU), 'cancel_with_status': default_test_options._replace(cpu_cost=LOWCPU), 'compressed_payload': default_test_options._replace(proxyable=False), - 'connectivity': connectivity_test_options._replace(proxyable=False, - cpu_cost=LOWCPU), + 'connectivity': connectivity_test_options._replace( + proxyable=False, cpu_cost=LOWCPU, exclude_iomgrs=['uv']), 'default_host': default_test_options._replace(needs_fullstack=True, needs_dns=True), 'disappearing_server': connectivity_test_options, @@ -247,6 +251,8 @@ def main(): 'name': '%s_test' % f, 'args': [t], 'exclude_configs': END2END_FIXTURES[f].exclude_configs, + 'exclude_iomgrs': list(set(END2END_FIXTURES[f].exclude_iomgrs) | + set(END2END_TESTS[t].exclude_iomgrs)), 'platforms': END2END_FIXTURES[f].platforms, 'ci_platforms': (END2END_FIXTURES[f].platforms if END2END_FIXTURES[f].ci_mac else without( @@ -262,6 +268,8 @@ def main(): 'name': '%s_nosec_test' % f, 'args': [t], 'exclude_configs': END2END_FIXTURES[f].exclude_configs, + 'exclude_iomgrs': list(set(END2END_FIXTURES[f].exclude_iomgrs) | + set(END2END_TESTS[t].exclude_iomgrs)), 'platforms': END2END_FIXTURES[f].platforms, 'ci_platforms': (END2END_FIXTURES[f].platforms if END2END_FIXTURES[f].ci_mac else without( diff --git a/test/core/handshake/client_ssl.c b/test/core/handshake/client_ssl.c new file mode 100644 index 0000000000..ee90f0b0bc --- /dev/null +++ b/test/core/handshake/client_ssl.c @@ -0,0 +1,311 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <arpa/inet.h> +#include <openssl/err.h> +#include <openssl/ssl.h> +#include <stdlib.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/thd.h> +#include "src/core/lib/iomgr/load_file.h" +#include "test/core/util/port.h" +#include "test/core/util/test_config.h" + +#define SSL_CERT_PATH "src/core/lib/tsi/test_creds/server1.pem" +#define SSL_KEY_PATH "src/core/lib/tsi/test_creds/server1.key" +#define SSL_CA_PATH "src/core/lib/tsi/test_creds/ca.pem" + +// Arguments for TLS server thread. +typedef struct { + int socket; + char *alpn_preferred; +} server_args; + +// From https://wiki.openssl.org/index.php/Simple_TLS_Server. +static int create_socket(int port) { + int s; + struct sockaddr_in addr; + + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t)port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) { + perror("Unable to create socket"); + return -1; + } + + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("Unable to bind"); + gpr_log(GPR_ERROR, "Unable to bind to %d", port); + close(s); + return -1; + } + + if (listen(s, 1) < 0) { + perror("Unable to listen"); + close(s); + return -1; + } + + return s; +} + +// 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; + + *out = 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; + for (int i = 0; i < (int)in_len; ++i) { + const size_t length = (size_t)*inp++; + if (length == strlen("grpc-exp") && strncmp(inp, "grpc-exp", length) == 0) { + grpc_exp_seen = true; + GPR_ASSERT(!h2_seen); + } + if (length == strlen("h2") && strncmp(inp, "h2", length) == 0) { + h2_seen = true; + GPR_ASSERT(grpc_exp_seen); + } + inp += length; + } + + GPR_ASSERT(grpc_exp_seen); + GPR_ASSERT(h2_seen); + + return SSL_TLSEXT_ERR_OK; +} + +// 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/lib/tsi/ssl_transport_security.c. +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); + if (!ctx) { + perror("Unable to create SSL context"); + ERR_print_errors_fp(stderr); + abort(); + } + + // Load key pair. + if (SSL_CTX_use_certificate_file(ctx, SSL_CERT_PATH, SSL_FILETYPE_PEM) < 0) { + ERR_print_errors_fp(stderr); + abort(); + } + if (SSL_CTX_use_PrivateKey_file(ctx, SSL_KEY_PATH, SSL_FILETYPE_PEM) < 0) { + ERR_print_errors_fp(stderr); + abort(); + } + + // Set the cipher list to match the one expressed in + // src/core/lib/tsi/ssl_transport_security.c. + 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)) { + ERR_print_errors_fp(stderr); + gpr_log(GPR_ERROR, "Couldn't set server cipher list."); + abort(); + } + + // Register the ALPN selection callback. + SSL_CTX_set_alpn_select_cb(ctx, alpn_select_cb, args->alpn_preferred); + + // bind/listen/accept at TCP layer. + const int sock = args->socket; + gpr_log(GPR_INFO, "Server listening"); + struct sockaddr_in addr; + socklen_t len = sizeof(addr); + 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); + GPR_ASSERT(ssl); + SSL_set_fd(ssl, client); + if (SSL_accept(ssl) <= 0) { + ERR_print_errors_fp(stderr); + gpr_log(GPR_ERROR, "Handshake failed."); + } else { + gpr_log(GPR_INFO, "Handshake successful."); + } + + // Wait until the client drops its connection. + char buf; + while (SSL_read(ssl, &buf, sizeof(buf)) > 0) + ; + + SSL_free(ssl); + close(client); + close(sock); + SSL_CTX_free(ctx); + EVP_cleanup(); +} + +// This test launches a minimal TLS server on a separate thread and then +// 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) { + bool success = true; + + grpc_init(); + + // Find a port we can bind to. Retries added to handle flakes in port server + // and port picking. + int port = -1; + int server_socket = -1; + int socket_retries = 10; + while (server_socket == -1 && socket_retries-- > 0) { + port = grpc_pick_unused_port_or_die(); + server_socket = create_socket(port); + if (server_socket == -1) { + sleep(1); + } + } + GPR_ASSERT(server_socket > 0); + + // Launch the TLS server thread. + 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}; + GPR_ASSERT(gpr_thd_new(&thdid, server_thread, &args, &thdopt)); + + // Load key pair and establish client SSL credentials. + grpc_ssl_pem_key_cert_pair pem_key_cert_pair; + gpr_slice ca_slice, cert_slice, key_slice; + GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", + grpc_load_file(SSL_CA_PATH, 1, &ca_slice))); + GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", + 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 *)GPR_SLICE_START_PTR(ca_slice); + pem_key_cert_pair.private_key = (const char *)GPR_SLICE_START_PTR(key_slice); + pem_key_cert_pair.cert_chain = (const char *)GPR_SLICE_START_PTR(cert_slice); + grpc_channel_credentials *ssl_creds = + grpc_ssl_credentials_create(ca_cert, &pem_key_cert_pair, NULL); + + // Establish a channel pointing at the TLS server. Since the gRPC runtime is + // lazy, this won't necessarily establish a connection yet. + 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_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); + GPR_ASSERT(channel); + gpr_free(target); + + // Initially the channel will be idle, the + // grpc_channel_check_connectivity_state triggers an attempt to connect. + GPR_ASSERT(grpc_channel_check_connectivity_state( + channel, 1 /* try_to_connect */) == GRPC_CHANNEL_IDLE); + + // Wait a bounded number of times for the channel to be ready. When the + // channel is ready, the initial TLS handshake will have successfully + // 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(NULL); + while (state != GRPC_CHANNEL_READY && retries-- > 0) { + grpc_channel_watch_connectivity_state( + channel, state, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), cq, NULL); + gpr_timespec cq_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5); + grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, NULL); + GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); + state = + grpc_channel_check_connectivity_state(channel, 0 /* try_to_connect */); + } + grpc_completion_queue_destroy(cq); + if (retries < 0) { + success = false; + } + + grpc_channel_destroy(channel); + grpc_channel_credentials_release(ssl_creds); + gpr_slice_unref(cert_slice); + gpr_slice_unref(key_slice); + gpr_slice_unref(ca_slice); + + gpr_thd_join(thdid); + + grpc_shutdown(); + + return success; +} + +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")); + // 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")); + // 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")); + return 0; +} diff --git a/test/core/handshake/server_ssl.c b/test/core/handshake/server_ssl.c new file mode 100644 index 0000000000..3e89f8e265 --- /dev/null +++ b/test/core/handshake/server_ssl.c @@ -0,0 +1,270 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#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" + +#define SSL_CERT_PATH "src/core/lib/tsi/test_creds/server1.pem" +#define SSL_KEY_PATH "src/core/lib/tsi/test_creds/server1.key" +#define SSL_CA_PATH "src/core/lib/tsi/test_creds/ca.pem" + +// Handshake completed signal to server thread. +static gpr_event client_handshake_complete; + +static int create_socket(int port) { + int s; + struct sockaddr_in addr; + + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t)port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) { + perror("Unable to create socket"); + return -1; + } + + if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("Unable to connect"); + return -1; + } + + return s; +} + +// Simple gRPC server. This listens until client_handshake_complete occurs. +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; + gpr_slice ca_slice, cert_slice, key_slice; + GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", + grpc_load_file(SSL_CA_PATH, 1, &ca_slice))); + GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", + 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 *)GPR_SLICE_START_PTR(ca_slice); + pem_key_cert_pair.private_key = (const char *)GPR_SLICE_START_PTR(key_slice); + pem_key_cert_pair.cert_chain = (const char *)GPR_SLICE_START_PTR(cert_slice); + grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create( + ca_cert, &pem_key_cert_pair, 1, 0, NULL); + + // Start server listening on local port. + char *addr; + gpr_asprintf(&addr, "127.0.0.1:%d", port); + grpc_server *server = grpc_server_create(NULL, NULL); + GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds)); + free(addr); + + grpc_completion_queue *cq = grpc_completion_queue_create(NULL); + + grpc_server_register_completion_queue(server, cq, NULL); + grpc_server_start(server); + + // Wait a bounded number of time until client_handshake_complete is set, + // sleeping between polls. + 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); + GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT); + } + + gpr_log(GPR_INFO, "Shutting down server"); + grpc_server_shutdown_and_notify(server, cq, NULL); + 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); + GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); + + grpc_server_destroy(server); + grpc_completion_queue_destroy(cq); + grpc_server_credentials_release(ssl_creds); + gpr_slice_unref(cert_slice); + gpr_slice_unref(key_slice); + gpr_slice_unref(ca_slice); +} + +// This test launches a gRPC server on a separate thread and then establishes a +// 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 success = true; + + grpc_init(); + int port = grpc_pick_unused_port_or_die(); + gpr_event_init(&client_handshake_complete); + + // Launch the gRPC server thread. + gpr_thd_options thdopt = gpr_thd_options_default(); + gpr_thd_id thdid; + gpr_thd_options_set_joinable(&thdopt); + GPR_ASSERT(gpr_thd_new(&thdid, server_thread, &port, &thdopt)); + + SSL_load_error_strings(); + OpenSSL_add_ssl_algorithms(); + + 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); + abort(); + } + + // Load key pair. + if (SSL_CTX_use_certificate_file(ctx, SSL_CERT_PATH, SSL_FILETYPE_PEM) < 0) { + ERR_print_errors_fp(stderr); + abort(); + } + if (SSL_CTX_use_PrivateKey_file(ctx, SSL_KEY_PATH, SSL_FILETYPE_PEM) < 0) { + ERR_print_errors_fp(stderr); + abort(); + } + + // Set the cipher list to match the one expressed in + // src/core/lib/tsi/ssl_transport_security.c. + 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)) { + ERR_print_errors_fp(stderr); + gpr_log(GPR_ERROR, "Couldn't set server cipher list."); + abort(); + } + + // Configure ALPN list the client will send to the server. This must match the + // wire format, see documentation for SSL_CTX_set_alpn_protos. + unsigned int alpn_protos_len = 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; + for (unsigned int i = 0; i < alpn_list_len; ++i) { + const uint8_t len = (uint8_t)strlen(alpn_list[i]); + *p++ = len; + memcpy(p, alpn_list[i], len); + p += len; + } + GPR_ASSERT(SSL_CTX_set_alpn_protos(ctx, alpn_protos, alpn_protos_len) == 0); + + // Try and connect to server. We allow a bounded number of retries as we might + // be racing with the server setup on its separate thread. + int retries = 10; + int sock = -1; + while (sock == -1 && retries-- > 0) { + sock = create_socket(port); + if (sock < 0) { + sleep(1); + } + } + GPR_ASSERT(sock > 0); + gpr_log(GPR_INFO, "Connected to server on port %d", port); + + // Establish a SSL* and connect at SSL layer. + SSL *ssl = SSL_new(ctx); + GPR_ASSERT(ssl); + SSL_set_fd(ssl, sock); + if (SSL_connect(ssl) <= 0) { + ERR_print_errors_fp(stderr); + gpr_log(GPR_ERROR, "Handshake failed."); + success = false; + } else { + gpr_log(GPR_INFO, "Handshake successful."); + // Validate ALPN preferred by server matches alpn_expected. + 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) { + gpr_log(GPR_ERROR, "Unexpected ALPN protocol preference"); + success = false; + } + } + gpr_event_set(&client_handshake_complete, &client_handshake_complete); + + SSL_free(ssl); + gpr_free(alpn_protos); + SSL_CTX_free(ctx); + EVP_cleanup(); + close(sock); + + gpr_thd_join(thdid); + + grpc_shutdown(); + + 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/iomgr/ev_epoll_linux_test.c b/test/core/iomgr/ev_epoll_linux_test.c index 2547dc9871..564b05d7f4 100644 --- a/test/core/iomgr/ev_epoll_linux_test.c +++ b/test/core/iomgr/ev_epoll_linux_test.c @@ -30,10 +30,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -#include <grpc/support/port_platform.h> +#include "src/core/lib/iomgr/port.h" /* This test only relevant on linux systems where epoll() is available */ -#ifdef GPR_LINUX_EPOLL +#ifdef GRPC_LINUX_EPOLL #include "src/core/lib/iomgr/ev_epoll_linux.h" #include "src/core/lib/iomgr/ev_posix.h" @@ -239,6 +239,6 @@ int main(int argc, char **argv) { grpc_iomgr_shutdown(); return 0; } -#else /* defined(GPR_LINUX_EPOLL) */ +#else /* defined(GRPC_LINUX_EPOLL) */ int main(int argc, char **argv) { return 0; } -#endif /* !defined(GPR_LINUX_EPOLL) */ +#endif /* !defined(GRPC_LINUX_EPOLL) */ diff --git a/test/core/iomgr/fd_posix_test.c b/test/core/iomgr/fd_posix_test.c index 62dc24d85a..6166699fe6 100644 --- a/test/core/iomgr/fd_posix_test.c +++ b/test/core/iomgr/fd_posix_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "src/core/lib/iomgr/ev_posix.h" #include <ctype.h> @@ -548,3 +553,9 @@ int main(int argc, char **argv) { grpc_iomgr_shutdown(); return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/iomgr/resolve_address_test.c b/test/core/iomgr/resolve_address_test.c index 4417d96043..2dd0d88b3f 100644 --- a/test/core/iomgr/resolve_address_test.c +++ b/test/core/iomgr/resolve_address_test.c @@ -36,6 +36,7 @@ #include <grpc/support/sync.h> #include <grpc/support/time.h> #include "src/core/lib/iomgr/executor.h" +#include "src/core/lib/iomgr/iomgr.h" #include "test/core/util/test_config.h" static gpr_timespec test_deadline(void) { diff --git a/test/core/iomgr/sockaddr_utils_test.c b/test/core/iomgr/sockaddr_utils_test.c index a330314443..8569c697fe 100644 --- a/test/core/iomgr/sockaddr_utils_test.c +++ b/test/core/iomgr/sockaddr_utils_test.c @@ -31,6 +31,11 @@ * */ +/* With the addition of a libuv endpoint, sockaddr.h now includes uv.h when + using that endpoint. Because of various transitive includes in uv.h, + including windows.h on Windows, uv.h must be included before other system + headers. Therefore, sockaddr.h must always be included first */ +#include "src/core/lib/iomgr/sockaddr.h" #include "src/core/lib/iomgr/sockaddr_utils.h" #include <errno.h> @@ -41,24 +46,28 @@ #include <grpc/support/port_platform.h> #include "test/core/util/test_config.h" -static struct sockaddr_in make_addr4(const uint8_t *data, size_t data_len) { - struct sockaddr_in addr4; - memset(&addr4, 0, sizeof(addr4)); - addr4.sin_family = AF_INET; - GPR_ASSERT(data_len == sizeof(addr4.sin_addr.s_addr)); - memcpy(&addr4.sin_addr.s_addr, data, data_len); - addr4.sin_port = htons(12345); - return addr4; +static grpc_resolved_address make_addr4(const uint8_t *data, size_t data_len) { + grpc_resolved_address resolved_addr4; + struct sockaddr_in *addr4 = (struct sockaddr_in *)resolved_addr4.addr; + memset(&resolved_addr4, 0, sizeof(resolved_addr4)); + addr4->sin_family = AF_INET; + GPR_ASSERT(data_len == sizeof(addr4->sin_addr.s_addr)); + memcpy(&addr4->sin_addr.s_addr, data, data_len); + addr4->sin_port = htons(12345); + resolved_addr4.len = sizeof(struct sockaddr_in); + return resolved_addr4; } -static struct sockaddr_in6 make_addr6(const uint8_t *data, size_t data_len) { - struct sockaddr_in6 addr6; - memset(&addr6, 0, sizeof(addr6)); - addr6.sin6_family = AF_INET6; - GPR_ASSERT(data_len == sizeof(addr6.sin6_addr.s6_addr)); - memcpy(&addr6.sin6_addr.s6_addr, data, data_len); - addr6.sin6_port = htons(12345); - return addr6; +static grpc_resolved_address make_addr6(const uint8_t *data, size_t data_len) { + grpc_resolved_address resolved_addr6; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)resolved_addr6.addr; + memset(&resolved_addr6, 0, sizeof(resolved_addr6)); + addr6->sin6_family = AF_INET6; + GPR_ASSERT(data_len == sizeof(addr6->sin6_addr.s6_addr)); + memcpy(&addr6->sin6_addr.s6_addr, data, data_len); + addr6->sin6_port = htons(12345); + resolved_addr6.len = sizeof(struct sockaddr_in6); + return resolved_addr6; } static const uint8_t kMapped[] = {0, 0, 0, 0, 0, 0, 0, 0, @@ -72,102 +81,96 @@ static const uint8_t kIPv6[] = {0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; static void test_sockaddr_is_v4mapped(void) { - struct sockaddr_in input4; - struct sockaddr_in6 input6; - struct sockaddr_in output4; - struct sockaddr_in expect4; + grpc_resolved_address input4; + grpc_resolved_address input6; + grpc_resolved_address output4; + grpc_resolved_address expect4; gpr_log(GPR_INFO, "%s", "test_sockaddr_is_v4mapped"); /* v4mapped input should succeed. */ input6 = make_addr6(kMapped, sizeof(kMapped)); - GPR_ASSERT(grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, NULL)); - GPR_ASSERT( - grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, &output4)); + GPR_ASSERT(grpc_sockaddr_is_v4mapped(&input6, NULL)); + GPR_ASSERT(grpc_sockaddr_is_v4mapped(&input6, &output4)); expect4 = make_addr4(kIPv4, sizeof(kIPv4)); GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0); /* Non-v4mapped input should fail. */ input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped)); - GPR_ASSERT( - !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, NULL)); - GPR_ASSERT( - !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, &output4)); + GPR_ASSERT(!grpc_sockaddr_is_v4mapped(&input6, NULL)); + GPR_ASSERT(!grpc_sockaddr_is_v4mapped(&input6, &output4)); /* Output is unchanged. */ GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0); /* Plain IPv4 input should also fail. */ input4 = make_addr4(kIPv4, sizeof(kIPv4)); - GPR_ASSERT( - !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input4, NULL)); + GPR_ASSERT(!grpc_sockaddr_is_v4mapped(&input4, NULL)); } static void test_sockaddr_to_v4mapped(void) { - struct sockaddr_in input4; - struct sockaddr_in6 input6; - struct sockaddr_in6 output6; - struct sockaddr_in6 expect6; + grpc_resolved_address input4; + grpc_resolved_address input6; + grpc_resolved_address output6; + grpc_resolved_address expect6; gpr_log(GPR_INFO, "%s", "test_sockaddr_to_v4mapped"); /* IPv4 input should succeed. */ input4 = make_addr4(kIPv4, sizeof(kIPv4)); - GPR_ASSERT( - grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input4, &output6)); + GPR_ASSERT(grpc_sockaddr_to_v4mapped(&input4, &output6)); expect6 = make_addr6(kMapped, sizeof(kMapped)); GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0); /* IPv6 input should fail. */ input6 = make_addr6(kIPv6, sizeof(kIPv6)); - GPR_ASSERT( - !grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input6, &output6)); + GPR_ASSERT(!grpc_sockaddr_to_v4mapped(&input6, &output6)); /* Output is unchanged. */ GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0); /* Already-v4mapped input should also fail. */ input6 = make_addr6(kMapped, sizeof(kMapped)); - GPR_ASSERT( - !grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input6, &output6)); + GPR_ASSERT(!grpc_sockaddr_to_v4mapped(&input6, &output6)); } static void test_sockaddr_is_wildcard(void) { - struct sockaddr_in wild4; - struct sockaddr_in6 wild6; - struct sockaddr_in6 wild_mapped; - struct sockaddr dummy; + grpc_resolved_address wild4; + grpc_resolved_address wild6; + grpc_resolved_address wild_mapped; + grpc_resolved_address dummy; + struct sockaddr_in *wild4_addr; + struct sockaddr_in6 *wild6_addr; + struct sockaddr_in6 *wild_mapped_addr; int port; gpr_log(GPR_INFO, "%s", "test_sockaddr_is_wildcard"); /* Generate wildcards. */ grpc_sockaddr_make_wildcards(555, &wild4, &wild6); - GPR_ASSERT( - grpc_sockaddr_to_v4mapped((const struct sockaddr *)&wild4, &wild_mapped)); + GPR_ASSERT(grpc_sockaddr_to_v4mapped(&wild4, &wild_mapped)); /* Test 0.0.0.0:555 */ port = -1; - GPR_ASSERT(grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild4, &port)); + GPR_ASSERT(grpc_sockaddr_is_wildcard(&wild4, &port)); GPR_ASSERT(port == 555); - memset(&wild4.sin_addr.s_addr, 0xbd, 1); - GPR_ASSERT( - !grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild4, &port)); + wild4_addr = (struct sockaddr_in *)&wild4.addr; + memset(&wild4_addr->sin_addr.s_addr, 0xbd, 1); + GPR_ASSERT(!grpc_sockaddr_is_wildcard(&wild4, &port)); /* Test [::]:555 */ port = -1; - GPR_ASSERT(grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild6, &port)); + GPR_ASSERT(grpc_sockaddr_is_wildcard(&wild6, &port)); GPR_ASSERT(port == 555); - memset(&wild6.sin6_addr.s6_addr, 0xbd, 1); - GPR_ASSERT( - !grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild6, &port)); + wild6_addr = (struct sockaddr_in6 *)&wild6.addr; + memset(&wild6_addr->sin6_addr.s6_addr, 0xbd, 1); + GPR_ASSERT(!grpc_sockaddr_is_wildcard(&wild6, &port)); /* Test [::ffff:0.0.0.0]:555 */ port = -1; - GPR_ASSERT( - grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild_mapped, &port)); + GPR_ASSERT(grpc_sockaddr_is_wildcard(&wild_mapped, &port)); GPR_ASSERT(port == 555); - memset(&wild_mapped.sin6_addr.s6_addr, 0xbd, 1); - GPR_ASSERT( - !grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild_mapped, &port)); + wild_mapped_addr = (struct sockaddr_in6 *)&wild_mapped.addr; + memset(&wild_mapped_addr->sin6_addr.s6_addr, 0xbd, 1); + GPR_ASSERT(!grpc_sockaddr_is_wildcard(&wild_mapped, &port)); /* Test AF_UNSPEC. */ port = -1; @@ -176,12 +179,12 @@ static void test_sockaddr_is_wildcard(void) { GPR_ASSERT(port == -1); } -static void expect_sockaddr_str(const char *expected, void *addr, - int normalize) { +static void expect_sockaddr_str(const char *expected, + grpc_resolved_address *addr, int normalize) { int result; char *str; gpr_log(GPR_INFO, " expect_sockaddr_str(%s)", expected); - result = grpc_sockaddr_to_string(&str, (struct sockaddr *)addr, normalize); + result = grpc_sockaddr_to_string(&str, addr, normalize); GPR_ASSERT(str != NULL); GPR_ASSERT(result >= 0); GPR_ASSERT((size_t)result == strlen(str)); @@ -189,19 +192,21 @@ static void expect_sockaddr_str(const char *expected, void *addr, gpr_free(str); } -static void expect_sockaddr_uri(const char *expected, void *addr) { +static void expect_sockaddr_uri(const char *expected, + grpc_resolved_address *addr) { char *str; gpr_log(GPR_INFO, " expect_sockaddr_uri(%s)", expected); - str = grpc_sockaddr_to_uri((struct sockaddr *)addr); + str = grpc_sockaddr_to_uri(addr); GPR_ASSERT(str != NULL); GPR_ASSERT(strcmp(expected, str) == 0); gpr_free(str); } static void test_sockaddr_to_string(void) { - struct sockaddr_in input4; - struct sockaddr_in6 input6; - struct sockaddr dummy; + grpc_resolved_address input4; + grpc_resolved_address input6; + grpc_resolved_address dummy; + struct sockaddr *dummy_addr; gpr_log(GPR_INFO, "%s", "test_sockaddr_to_string"); @@ -228,7 +233,8 @@ static void test_sockaddr_to_string(void) { expect_sockaddr_uri("ipv6:[::fffe:c000:263]:12345", &input6); memset(&dummy, 0, sizeof(dummy)); - dummy.sa_family = 123; + dummy_addr = (struct sockaddr *)dummy.addr; + dummy_addr->sa_family = 123; expect_sockaddr_str("(sockaddr family=123)", &dummy, 0); expect_sockaddr_str("(sockaddr family=123)", &dummy, 1); GPR_ASSERT(grpc_sockaddr_to_uri(&dummy) == NULL); @@ -237,24 +243,26 @@ static void test_sockaddr_to_string(void) { } static void test_sockaddr_set_get_port(void) { - struct sockaddr_in input4; - struct sockaddr_in6 input6; - struct sockaddr dummy; + grpc_resolved_address input4; + grpc_resolved_address input6; + grpc_resolved_address dummy; + struct sockaddr *dummy_addr; gpr_log(GPR_DEBUG, "test_sockaddr_set_get_port"); input4 = make_addr4(kIPv4, sizeof(kIPv4)); - GPR_ASSERT(grpc_sockaddr_get_port((struct sockaddr *)&input4) == 12345); - GPR_ASSERT(grpc_sockaddr_set_port((struct sockaddr *)&input4, 54321)); - GPR_ASSERT(grpc_sockaddr_get_port((struct sockaddr *)&input4) == 54321); + GPR_ASSERT(grpc_sockaddr_get_port(&input4) == 12345); + GPR_ASSERT(grpc_sockaddr_set_port(&input4, 54321)); + GPR_ASSERT(grpc_sockaddr_get_port(&input4) == 54321); input6 = make_addr6(kIPv6, sizeof(kIPv6)); - GPR_ASSERT(grpc_sockaddr_get_port((struct sockaddr *)&input6) == 12345); - GPR_ASSERT(grpc_sockaddr_set_port((struct sockaddr *)&input6, 54321)); - GPR_ASSERT(grpc_sockaddr_get_port((struct sockaddr *)&input6) == 54321); + GPR_ASSERT(grpc_sockaddr_get_port(&input6) == 12345); + GPR_ASSERT(grpc_sockaddr_set_port(&input6, 54321)); + GPR_ASSERT(grpc_sockaddr_get_port(&input6) == 54321); memset(&dummy, 0, sizeof(dummy)); - dummy.sa_family = 123; + dummy_addr = (struct sockaddr *)dummy.addr; + dummy_addr->sa_family = 123; GPR_ASSERT(grpc_sockaddr_get_port(&dummy) == 0); GPR_ASSERT(grpc_sockaddr_set_port(&dummy, 1234) == 0); } diff --git a/test/core/iomgr/socket_utils_test.c b/test/core/iomgr/socket_utils_test.c index 297531c44d..67bc914c15 100644 --- a/test/core/iomgr/socket_utils_test.c +++ b/test/core/iomgr/socket_utils_test.c @@ -31,7 +31,11 @@ * */ -#include <grpc/support/port_platform.h> +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "src/core/lib/iomgr/socket_utils_posix.h" #include <errno.h> @@ -68,3 +72,9 @@ int main(int argc, char **argv) { return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/iomgr/tcp_client_posix_test.c b/test/core/iomgr/tcp_client_posix_test.c index d0c1047423..6c6962ed7a 100644 --- a/test/core/iomgr/tcp_client_posix_test.c +++ b/test/core/iomgr/tcp_client_posix_test.c @@ -85,8 +85,8 @@ static void must_fail(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { } void test_succeeds(void) { - struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; int svr_fd; int r; int connections_complete_before; @@ -95,13 +95,15 @@ void test_succeeds(void) { gpr_log(GPR_DEBUG, "test_succeeds"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; /* create a dummy server */ svr_fd = socket(AF_INET, SOCK_STREAM, 0); GPR_ASSERT(svr_fd >= 0); - GPR_ASSERT(0 == bind(svr_fd, (struct sockaddr *)&addr, addr_len)); + GPR_ASSERT( + 0 == bind(svr_fd, (struct sockaddr *)addr, (socklen_t)resolved_addr.len)); GPR_ASSERT(0 == listen(svr_fd, 1)); gpr_mu_lock(g_mu); @@ -109,16 +111,17 @@ void test_succeeds(void) { gpr_mu_unlock(g_mu); /* connect to it */ - GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0); + GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)addr, + (socklen_t *)&resolved_addr.len) == 0); grpc_closure_init(&done, must_succeed, NULL); grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set, - (struct sockaddr *)&addr, addr_len, - gpr_inf_future(GPR_CLOCK_REALTIME)); + &resolved_addr, gpr_inf_future(GPR_CLOCK_REALTIME)); /* await the connection */ do { - addr_len = sizeof(addr); - r = accept(svr_fd, (struct sockaddr *)&addr, &addr_len); + resolved_addr.len = sizeof(addr); + r = accept(svr_fd, (struct sockaddr *)addr, + (socklen_t *)&resolved_addr.len); } while (r == -1 && errno == EINTR); GPR_ASSERT(r >= 0); close(r); @@ -143,16 +146,17 @@ void test_succeeds(void) { } void test_fails(void) { - struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; int connections_complete_before; grpc_closure done; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; gpr_log(GPR_DEBUG, "test_fails"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; gpr_mu_lock(g_mu); connections_complete_before = g_connections_complete; @@ -161,8 +165,7 @@ void test_fails(void) { /* connect to a broken address */ grpc_closure_init(&done, must_fail, NULL); grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set, - (struct sockaddr *)&addr, addr_len, - gpr_inf_future(GPR_CLOCK_REALTIME)); + &resolved_addr, gpr_inf_future(GPR_CLOCK_REALTIME)); gpr_mu_lock(g_mu); diff --git a/test/core/iomgr/tcp_posix_test.c b/test/core/iomgr/tcp_posix_test.c index 42614567ca..d998958744 100644 --- a/test/core/iomgr/tcp_posix_test.c +++ b/test/core/iomgr/tcp_posix_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "src/core/lib/iomgr/tcp_posix.h" #include <errno.h> @@ -544,3 +549,9 @@ int main(int argc, char **argv) { return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/iomgr/tcp_server_posix_test.c b/test/core/iomgr/tcp_server_posix_test.c index 5791f562e1..f839911a84 100644 --- a/test/core/iomgr/tcp_server_posix_test.c +++ b/test/core/iomgr/tcp_server_posix_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "src/core/lib/iomgr/tcp_server.h" #include <errno.h> @@ -46,6 +51,7 @@ #include <grpc/support/time.h> #include "src/core/lib/iomgr/iomgr.h" +#include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/sockaddr_utils.h" #include "test/core/util/port.h" #include "test/core/util/test_config.h" @@ -149,16 +155,18 @@ static void test_no_op_with_start(void) { static void test_no_op_with_port(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; grpc_tcp_server *s; GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); LOG_TEST("test_no_op_with_port"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; int port; - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), - &port) == GRPC_ERROR_NONE && + GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) == + GRPC_ERROR_NONE && port > 0); grpc_tcp_server_unref(&exec_ctx, s); @@ -167,16 +175,18 @@ static void test_no_op_with_port(void) { static void test_no_op_with_port_and_start(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; grpc_tcp_server *s; GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); LOG_TEST("test_no_op_with_port_and_start"); int port; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), - &port) == GRPC_ERROR_NONE && + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; + GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) == + GRPC_ERROR_NONE && port > 0); grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL); @@ -221,9 +231,11 @@ static void tcp_connect(grpc_exec_ctx *exec_ctx, const struct sockaddr *remote, the same port should be tested. */ static void test_connect(unsigned n) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_storage addr; - struct sockaddr_storage addr1; - socklen_t addr_len = sizeof(addr); + grpc_resolved_address resolved_addr; + grpc_resolved_address resolved_addr1; + struct sockaddr_storage *addr = (struct sockaddr_storage *)resolved_addr.addr; + struct sockaddr_storage *addr1 = + (struct sockaddr_storage *)resolved_addr1.addr; unsigned svr_fd_count; int svr_port; unsigned svr1_fd_count; @@ -235,19 +247,20 @@ static void test_connect(unsigned n) { server_weak_ref_init(&weak_ref); LOG_TEST("test_connect"); gpr_log(GPR_INFO, "clients=%d", n); - memset(&addr, 0, sizeof(addr)); - memset(&addr1, 0, sizeof(addr1)); - addr.ss_family = addr1.ss_family = AF_INET; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + memset(&resolved_addr1, 0, sizeof(resolved_addr1)); + resolved_addr.len = sizeof(struct sockaddr_storage); + resolved_addr1.len = sizeof(struct sockaddr_storage); + addr->ss_family = addr1->ss_family = AF_INET; GPR_ASSERT(GRPC_ERROR_NONE == - grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, addr_len, - &svr_port)); + grpc_tcp_server_add_port(s, &resolved_addr, &svr_port)); GPR_ASSERT(svr_port > 0); /* Cannot use wildcard (port==0), because add_port() will try to reuse the same port as a previous add_port(). */ svr1_port = grpc_pick_unused_port_or_die(); - grpc_sockaddr_set_port((struct sockaddr *)&addr1, svr1_port); - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr1, addr_len, - &svr_port) == GRPC_ERROR_NONE && + grpc_sockaddr_set_port(&resolved_addr1, svr1_port); + GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr1, &svr_port) == + GRPC_ERROR_NONE && svr_port == svr1_port); /* Bad port_index. */ @@ -268,16 +281,18 @@ static void test_connect(unsigned n) { int fd = grpc_tcp_server_port_fd(s, 0, i); GPR_ASSERT(fd >= 0); if (i == 0) { - GPR_ASSERT(getsockname(fd, (struct sockaddr *)&addr, &addr_len) == 0); - GPR_ASSERT(addr_len <= sizeof(addr)); + GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr, + (socklen_t *)&resolved_addr.len) == 0); + GPR_ASSERT(resolved_addr.len <= sizeof(*addr)); } } for (i = 0; i < svr1_fd_count; ++i) { int fd = grpc_tcp_server_port_fd(s, 1, i); GPR_ASSERT(fd >= 0); if (i == 0) { - GPR_ASSERT(getsockname(fd, (struct sockaddr *)&addr1, &addr_len) == 0); - GPR_ASSERT(addr_len <= sizeof(addr1)); + GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr1, + (socklen_t *)&resolved_addr1.len) == 0); + GPR_ASSERT(resolved_addr1.len <= sizeof(*addr1)); } } @@ -287,7 +302,8 @@ static void test_connect(unsigned n) { on_connect_result result; int svr_fd; on_connect_result_init(&result); - tcp_connect(&exec_ctx, (struct sockaddr *)&addr, addr_len, &result); + tcp_connect(&exec_ctx, (struct sockaddr *)addr, + (socklen_t)resolved_addr.len, &result); GPR_ASSERT(result.server_fd >= 0); svr_fd = result.server_fd; GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) == @@ -301,7 +317,8 @@ static void test_connect(unsigned n) { grpc_tcp_server_unref(&exec_ctx, result.server); on_connect_result_init(&result); - tcp_connect(&exec_ctx, (struct sockaddr *)&addr1, addr_len, &result); + tcp_connect(&exec_ctx, (struct sockaddr *)addr1, + (socklen_t)resolved_addr1.len, &result); GPR_ASSERT(result.server_fd >= 0); GPR_ASSERT(result.server_fd != svr_fd); GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) == @@ -350,3 +367,9 @@ int main(int argc, char **argv) { gpr_free(g_pollset); return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/iomgr/timer_heap_test.c b/test/core/iomgr/timer_heap_test.c index d1cb0047f2..410d972313 100644 --- a/test/core/iomgr/timer_heap_test.c +++ b/test/core/iomgr/timer_heap_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test only works with the generic timer implementation +#ifdef GRPC_TIMER_USE_GENERIC + #include "src/core/lib/iomgr/timer_heap.h" #include <stdlib.h> @@ -315,3 +320,9 @@ int main(int argc, char **argv) { return 0; } + +#else /* GRPC_TIMER_USE_GENERIC */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_TIMER_USE_GENERIC */ diff --git a/test/core/iomgr/udp_server_test.c b/test/core/iomgr/udp_server_test.c index 71d2fb5bd4..9bea229466 100644 --- a/test/core/iomgr/udp_server_test.c +++ b/test/core/iomgr/udp_server_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "src/core/lib/iomgr/udp_server.h" #include <netinet/in.h> @@ -98,14 +103,16 @@ static void test_no_op_with_start(void) { static void test_no_op_with_port(void) { g_number_of_orphan_calls = 0; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; grpc_udp_server *s = grpc_udp_server_create(); LOG_TEST("test_no_op_with_port"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - GPR_ASSERT(grpc_udp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), - on_read, on_fd_orphaned)); + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; + GPR_ASSERT( + grpc_udp_server_add_port(s, &resolved_addr, on_read, on_fd_orphaned)); grpc_udp_server_destroy(&exec_ctx, s, NULL); grpc_exec_ctx_finish(&exec_ctx); @@ -117,14 +124,16 @@ static void test_no_op_with_port(void) { static void test_no_op_with_port_and_start(void) { g_number_of_orphan_calls = 0; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; grpc_udp_server *s = grpc_udp_server_create(); LOG_TEST("test_no_op_with_port_and_start"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - GPR_ASSERT(grpc_udp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), - on_read, on_fd_orphaned)); + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; + GPR_ASSERT( + grpc_udp_server_add_port(s, &resolved_addr, on_read, on_fd_orphaned)); grpc_udp_server_start(&exec_ctx, s, NULL, 0, NULL); @@ -138,8 +147,8 @@ static void test_no_op_with_port_and_start(void) { static void test_receive(int number_of_clients) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); + grpc_resolved_address resolved_addr; + struct sockaddr_storage *addr = (struct sockaddr_storage *)resolved_addr.addr; int clifd, svrfd; grpc_udp_server *s = grpc_udp_server_create(); int i; @@ -152,15 +161,17 @@ static void test_receive(int number_of_clients) { g_number_of_bytes_read = 0; g_number_of_orphan_calls = 0; - memset(&addr, 0, sizeof(addr)); - addr.ss_family = AF_INET; - GPR_ASSERT(grpc_udp_server_add_port(s, (struct sockaddr *)&addr, addr_len, - on_read, on_fd_orphaned)); + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_storage); + addr->ss_family = AF_INET; + GPR_ASSERT( + grpc_udp_server_add_port(s, &resolved_addr, on_read, on_fd_orphaned)); svrfd = grpc_udp_server_get_fd(s, 0); GPR_ASSERT(svrfd >= 0); - GPR_ASSERT(getsockname(svrfd, (struct sockaddr *)&addr, &addr_len) == 0); - GPR_ASSERT(addr_len <= sizeof(addr)); + GPR_ASSERT(getsockname(svrfd, (struct sockaddr *)addr, + (socklen_t *)&resolved_addr.len) == 0); + GPR_ASSERT(resolved_addr.len <= sizeof(struct sockaddr_storage)); pollsets[0] = g_pollset; grpc_udp_server_start(&exec_ctx, s, pollsets, 1, NULL); @@ -172,9 +183,10 @@ static void test_receive(int number_of_clients) { number_of_reads_before = g_number_of_reads; /* Create a socket, send a packet to the UDP server. */ - clifd = socket(addr.ss_family, SOCK_DGRAM, 0); + clifd = socket(addr->ss_family, SOCK_DGRAM, 0); GPR_ASSERT(clifd >= 0); - GPR_ASSERT(connect(clifd, (struct sockaddr *)&addr, addr_len) == 0); + GPR_ASSERT(connect(clifd, (struct sockaddr *)addr, + (socklen_t)resolved_addr.len) == 0); GPR_ASSERT(5 == write(clifd, "hello", 5)); while (g_number_of_reads == number_of_reads_before && gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) > 0) { @@ -229,3 +241,9 @@ int main(int argc, char **argv) { grpc_iomgr_shutdown(); return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/iomgr/wakeup_fd_cv_test.c b/test/core/iomgr/wakeup_fd_cv_test.c index 952985ed7e..82452d2157 100644 --- a/test/core/iomgr/wakeup_fd_cv_test.c +++ b/test/core/iomgr/wakeup_fd_cv_test.c @@ -31,6 +31,10 @@ * */ +#include "src/core/lib/iomgr/port.h" + +#ifdef GRPC_POSIX_SOCKET + #include <pthread.h> #include <grpc/support/log.h> @@ -238,3 +242,9 @@ int main(int argc, char **argv) { grpc_iomgr_platform_shutdown(); return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index 2f8ffe4da6..b74baa33f1 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -1125,28 +1125,19 @@ static void test_metadata_plugin_failure(void) { } static void test_get_well_known_google_credentials_file_path(void) { -#ifdef GPR_POSIX_FILE char *path; - char *old_home = gpr_getenv("HOME"); - gpr_setenv("HOME", "/tmp"); + char *home = gpr_getenv("HOME"); path = grpc_get_well_known_google_credentials_file_path(); GPR_ASSERT(path != NULL); - GPR_ASSERT(0 == strcmp("/tmp/.config/" GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY - "/" GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE, - path)); gpr_free(path); #if defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV) unsetenv("HOME"); path = grpc_get_well_known_google_credentials_file_path(); GPR_ASSERT(path == NULL); -#endif /* GPR_POSIX_ENV || GPR_LINUX_ENV */ - gpr_setenv("HOME", old_home); - gpr_free(old_home); -#else /* GPR_POSIX_FILE */ - char *path = grpc_get_well_known_google_credentials_file_path(); - GPR_ASSERT(path != NULL); + gpr_setenv("HOME", home); gpr_free(path); -#endif +#endif /* GPR_POSIX_ENV || GPR_LINUX_ENV */ + gpr_free(home); } static void test_channel_creds_duplicate_without_call_creds(void) { diff --git a/test/core/surface/concurrent_connectivity_test.c b/test/core/surface/concurrent_connectivity_test.c index f7567f350d..f9f723baaa 100644 --- a/test/core/surface/concurrent_connectivity_test.c +++ b/test/core/surface/concurrent_connectivity_test.c @@ -31,6 +31,12 @@ * */ +/* With the addition of a libuv endpoint, sockaddr.h now includes uv.h when + using that endpoint. Because of various transitive includes in uv.h, + including windows.h on Windows, uv.h must be included before other system + headers. Therefore, sockaddr.h must always be included first */ +#include "src/core/lib/iomgr/sockaddr.h" + #include <memory.h> #include <stdio.h> @@ -42,6 +48,7 @@ #include "src/core/lib/iomgr/exec_ctx.h" #include "src/core/lib/iomgr/iomgr.h" +#include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/sockaddr_utils.h" #include "src/core/lib/iomgr/tcp_server.h" @@ -109,16 +116,15 @@ void bad_server_thread(void *vargs) { struct server_thread_args *args = (struct server_thread_args *)vargs; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); + grpc_resolved_address resolved_addr; + struct sockaddr_storage *addr = (struct sockaddr_storage *)resolved_addr.addr; int port; grpc_tcp_server *s; grpc_error *error = grpc_tcp_server_create(NULL, NULL, &s); GPR_ASSERT(error == GRPC_ERROR_NONE); - memset(&addr, 0, sizeof(addr)); - addr.ss_family = AF_INET; - error = - grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, addr_len, &port); + memset(&resolved_addr, 0, sizeof(resolved_addr)); + addr->ss_family = AF_INET; + error = grpc_tcp_server_add_port(s, &resolved_addr, &port); GPR_ASSERT(GRPC_LOG_IF_ERROR("grpc_tcp_server_add_port", error)); GPR_ASSERT(port > 0); gpr_asprintf(&args->addr, "localhost:%d", port); diff --git a/test/core/transport/chttp2/alpn_test.c b/test/core/transport/chttp2/alpn_test.c index 48064ec9b3..cde891cd5d 100644 --- a/test/core/transport/chttp2/alpn_test.c +++ b/test/core/transport/chttp2/alpn_test.c @@ -38,6 +38,7 @@ static void test_alpn_success(void) { GPR_ASSERT(grpc_chttp2_is_alpn_version_supported("h2", 2)); + GPR_ASSERT(grpc_chttp2_is_alpn_version_supported("grpc-exp", 8)); } static void test_alpn_failure(void) { @@ -45,9 +46,27 @@ static void test_alpn_failure(void) { GPR_ASSERT(!grpc_chttp2_is_alpn_version_supported("h1-15", 5)); } +// First index in ALPN supported version list of a given protocol. Returns a +// value one beyond the last valid element index if not found. +static size_t alpn_version_index(const char *version, size_t size) { + size_t i; + for (i = 0; i < grpc_chttp2_num_alpn_versions(); ++i) { + if (!strncmp(version, grpc_chttp2_get_alpn_version_index(i), size)) { + return i; + } + } + return i; +} + +static void test_alpn_grpc_before_h2(void) { + // grpc-exp is preferred over h2. + GPR_ASSERT(alpn_version_index("grpc-exp", 8) < alpn_version_index("h2", 2)); +} + int main(int argc, char **argv) { grpc_test_init(argc, argv); test_alpn_success(); test_alpn_failure(); + test_alpn_grpc_before_h2(); return 0; } diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c index 265e0acee1..60537b4946 100644 --- a/test/core/util/port_posix.c +++ b/test/core/util/port_posix.c @@ -31,9 +31,9 @@ * */ -#include <grpc/support/port_platform.h> +#include "src/core/lib/iomgr/port.h" #include "test/core/util/test_config.h" -#if defined(GPR_POSIX_SOCKET) && defined(GRPC_TEST_PICK_PORT) +#if defined(GRPC_POSIX_SOCKET) && defined(GRPC_TEST_PICK_PORT) #include "test/core/util/port.h" @@ -237,4 +237,4 @@ int grpc_pick_unused_port_or_die(void) { void grpc_recycle_unused_port(int port) { GPR_ASSERT(free_chosen_port(port)); } -#endif /* GPR_POSIX_SOCKET && GRPC_TEST_PICK_PORT */ +#endif /* GRPC_POSIX_SOCKET && GRPC_TEST_PICK_PORT */ diff --git a/test/core/util/port_uv.c b/test/core/util/port_uv.c new file mode 100644 index 0000000000..0c9c0d87d6 --- /dev/null +++ b/test/core/util/port_uv.c @@ -0,0 +1,117 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "src/core/lib/iomgr/port.h" +#include "test/core/util/test_config.h" +#if defined(GRPC_UV) && defined(GRPC_TEST_PICK_PORT) + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> + +#include "src/core/lib/support/env.h" +#include "test/core/util/port.h" +#include "test/core/util/port_server_client.h" + +// Almost everything in this file has been copied from port_posix.c + +static int *chosen_ports = NULL; +static size_t num_chosen_ports = 0; + +static int free_chosen_port(int port) { + size_t i; + int found = 0; + size_t found_at = 0; + char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); + /* Find the port and erase it from the list, then tell the server it can be + freed. */ + for (i = 0; i < num_chosen_ports; i++) { + if (chosen_ports[i] == port) { + GPR_ASSERT(found == 0); + found = 1; + found_at = i; + } + } + if (found) { + chosen_ports[found_at] = chosen_ports[num_chosen_ports - 1]; + num_chosen_ports--; + if (env) { + grpc_free_port_using_server(env, port); + } + } + gpr_free(env); + return found; +} + +static void free_chosen_ports(void) { + char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); + if (env != NULL) { + size_t i; + for (i = 0; i < num_chosen_ports; i++) { + grpc_free_port_using_server(env, chosen_ports[i]); + } + gpr_free(env); + } + + gpr_free(chosen_ports); +} + +static void chose_port(int port) { + if (chosen_ports == NULL) { + atexit(free_chosen_ports); + } + num_chosen_ports++; + chosen_ports = gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports); + chosen_ports[num_chosen_ports - 1] = port; +} + +int grpc_pick_unused_port(void) { + // Currently only works with the port server + char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); + GPR_ASSERT(env); + int port = grpc_pick_port_using_server(env); + gpr_free(env); + if (port != 0) { + chose_port(port); + } + return port; +} + +int grpc_pick_unused_port_or_die(void) { + int port = grpc_pick_unused_port(); + GPR_ASSERT(port > 0); + return port; +} + +void grpc_recycle_unused_port(int port) { GPR_ASSERT(free_chosen_port(port)); } + +#endif /* GRPC_UV && GRPC_TEST_PICK_PORT */ diff --git a/test/core/util/port_windows.c b/test/core/util/port_windows.c index 9023719675..0c50a46644 100644 --- a/test/core/util/port_windows.c +++ b/test/core/util/port_windows.c @@ -31,9 +31,11 @@ * */ -#include <grpc/support/port_platform.h> +#include "src/core/lib/iomgr/port.h" #include "test/core/util/test_config.h" -#if defined(GPR_WINSOCK_SOCKET) && defined(GRPC_TEST_PICK_PORT) +#if defined(GRPC_WINSOCK_SOCKET) && defined(GRPC_TEST_PICK_PORT) + +#include "src/core/lib/iomgr/sockaddr.h" #include "test/core/util/port.h" @@ -242,4 +244,4 @@ int grpc_pick_unused_port_or_die(void) { void grpc_recycle_unused_port(int port) { GPR_ASSERT(free_chosen_port(port)); } -#endif /* GPR_WINSOCK_SOCKET && GRPC_TEST_PICK_PORT */ +#endif /* GRPC_WINSOCK_SOCKET && GRPC_TEST_PICK_PORT */ diff --git a/test/core/util/test_tcp_server.c b/test/core/util/test_tcp_server.c index 8a0b3932d8..9ac97413d4 100644 --- a/test/core/util/test_tcp_server.c +++ b/test/core/util/test_tcp_server.c @@ -31,6 +31,8 @@ * */ +#include "src/core/lib/iomgr/sockaddr.h" + #include "test/core/util/test_tcp_server.h" #include <grpc/grpc.h> @@ -41,7 +43,7 @@ #include <grpc/support/time.h> #include <string.h> #include "src/core/lib/iomgr/endpoint.h" -#include "src/core/lib/iomgr/sockaddr.h" +#include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/tcp_server.h" #include "test/core/util/port.h" @@ -64,19 +66,20 @@ void test_tcp_server_init(test_tcp_server *server, } void test_tcp_server_start(test_tcp_server *server, int port) { - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; int port_added; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - addr.sin_family = AF_INET; - addr.sin_port = htons((uint16_t)port); - memset(&addr.sin_addr, 0, sizeof(addr.sin_addr)); + addr->sin_family = AF_INET; + addr->sin_port = htons((uint16_t)port); + memset(&addr->sin_addr, 0, sizeof(addr->sin_addr)); grpc_error *error = grpc_tcp_server_create(&server->shutdown_complete, NULL, &server->tcp_server); GPR_ASSERT(error == GRPC_ERROR_NONE); - error = grpc_tcp_server_add_port(server->tcp_server, &addr, sizeof(addr), - &port_added); + error = + grpc_tcp_server_add_port(server->tcp_server, &resolved_addr, &port_added); GPR_ASSERT(error == GRPC_ERROR_NONE); GPR_ASSERT(port_added == port); |