diff options
author | vjpai <vpai@google.com> | 2015-06-01 09:41:09 -0700 |
---|---|---|
committer | vjpai <vpai@google.com> | 2015-06-01 09:41:09 -0700 |
commit | 1795985322eb8b4e689c5e175d9d6937839f817c (patch) | |
tree | d3ce282d12ea0920ca94a9da9509ec6704f4dfa4 /test | |
parent | 675140ca1d394f564f2156add8a1a18a2dc132b7 (diff) | |
parent | 7f61193cdeacfb21f4e99ac6b8e2c0da33a643e8 (diff) |
Merge branch 'master' into poisson
Diffstat (limited to 'test')
-rw-r--r-- | test/compiler/python_plugin_test.py | 2 | ||||
-rw-r--r-- | test/core/bad_client/bad_client.c | 3 | ||||
-rw-r--r-- | test/core/end2end/fixtures/chttp2_fullstack_with_poll.c | 118 | ||||
-rw-r--r-- | test/core/end2end/fixtures/chttp2_simple_ssl_fullstack_with_poll.c | 168 | ||||
-rw-r--r-- | test/core/end2end/fixtures/chttp2_socket_pair.c | 3 | ||||
-rw-r--r-- | test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c | 3 | ||||
-rw-r--r-- | test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c | 3 | ||||
-rwxr-xr-x | test/core/end2end/gen_build_json.py | 2 | ||||
-rw-r--r-- | test/core/end2end/tests/request_response_with_payload_and_call_creds.c | 23 | ||||
-rw-r--r-- | test/core/security/auth_context_test.c | 143 | ||||
-rw-r--r-- | test/core/security/credentials_test.c | 163 | ||||
-rw-r--r-- | test/core/support/cmdline_test.c | 23 |
12 files changed, 596 insertions, 58 deletions
diff --git a/test/compiler/python_plugin_test.py b/test/compiler/python_plugin_test.py index ad3bebac30..367effdb1a 100644 --- a/test/compiler/python_plugin_test.py +++ b/test/compiler/python_plugin_test.py @@ -520,4 +520,4 @@ class PythonPluginTest(unittest.TestCase): if __name__ == '__main__': os.chdir(os.path.dirname(sys.argv[0])) - unittest.main() + unittest.main(verbosity=2) diff --git a/test/core/bad_client/bad_client.c b/test/core/bad_client/bad_client.c index c3725b5909..9d1c4ce2d7 100644 --- a/test/core/bad_client/bad_client.c +++ b/test/core/bad_client/bad_client.c @@ -68,7 +68,8 @@ static grpc_transport_setup_result server_setup_transport( static grpc_channel_filter const *extra_filters[] = { &grpc_http_server_filter}; return grpc_server_setup_transport(a->server, transport, extra_filters, - GPR_ARRAY_SIZE(extra_filters), mdctx); + GPR_ARRAY_SIZE(extra_filters), mdctx, + grpc_server_get_channel_args(a->server)); } void grpc_run_bad_client_test(const char *name, const char *client_payload, diff --git a/test/core/end2end/fixtures/chttp2_fullstack_with_poll.c b/test/core/end2end/fixtures/chttp2_fullstack_with_poll.c new file mode 100644 index 0000000000..f92b40efeb --- /dev/null +++ b/test/core/end2end/fixtures/chttp2_fullstack_with_poll.c @@ -0,0 +1,118 @@ +/* + * + * 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 "test/core/end2end/end2end_tests.h" + +#include <string.h> + +#include "src/core/channel/client_channel.h" +#include "src/core/channel/connected_channel.h" +#include "src/core/channel/http_server_filter.h" +#include "src/core/surface/channel.h" +#include "src/core/surface/client.h" +#include "src/core/surface/server.h" +#include "src/core/transport/chttp2_transport.h" +#include <grpc/support/alloc.h> +#include <grpc/support/host_port.h> +#include <grpc/support/log.h> +#include <grpc/support/sync.h> +#include <grpc/support/thd.h> +#include <grpc/support/useful.h> +#include "test/core/util/port.h" +#include "test/core/util/test_config.h" + +typedef struct fullstack_fixture_data { + char *localaddr; +} fullstack_fixture_data; + +static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( + grpc_channel_args *client_args, grpc_channel_args *server_args) { + grpc_end2end_test_fixture f; + int port = grpc_pick_unused_port_or_die(); + fullstack_fixture_data *ffd = gpr_malloc(sizeof(fullstack_fixture_data)); + memset(&f, 0, sizeof(f)); + + gpr_join_host_port(&ffd->localaddr, "localhost", port); + + f.fixture_data = ffd; + f.client_cq = grpc_completion_queue_create(); + f.server_cq = grpc_completion_queue_create(); + + return f; +} + +void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f, + grpc_channel_args *client_args) { + fullstack_fixture_data *ffd = f->fixture_data; + f->client = grpc_channel_create(ffd->localaddr, client_args); +} + +void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f, + grpc_channel_args *server_args) { + fullstack_fixture_data *ffd = f->fixture_data; + if (f->server) { + grpc_server_destroy(f->server); + } + f->server = grpc_server_create(server_args); + grpc_server_register_completion_queue(f->server, f->server_cq); + GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr)); + grpc_server_start(f->server); +} + +void chttp2_tear_down_fullstack(grpc_end2end_test_fixture *f) { + fullstack_fixture_data *ffd = f->fixture_data; + gpr_free(ffd->localaddr); + gpr_free(ffd); +} + +/* All test configurations */ +static grpc_end2end_test_config configs[] = { + {"chttp2/fullstack", FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION, + chttp2_create_fixture_fullstack, chttp2_init_client_fullstack, + chttp2_init_server_fullstack, chttp2_tear_down_fullstack}, +}; + +int main(int argc, char **argv) { + size_t i; + + grpc_test_init(argc, argv); + grpc_init(); + + for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { + grpc_end2end_tests(configs[i]); + } + + grpc_shutdown(); + + return 0; +} diff --git a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack_with_poll.c b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack_with_poll.c new file mode 100644 index 0000000000..a5865d37c8 --- /dev/null +++ b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack_with_poll.c @@ -0,0 +1,168 @@ +/* + * + * 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 "test/core/end2end/end2end_tests.h" + +#include <stdio.h> +#include <string.h> + +#include "src/core/channel/channel_args.h" +#include "src/core/security/credentials.h" +#include "src/core/support/env.h" +#include "src/core/support/file.h" +#include "src/core/support/string.h" +#include <grpc/support/alloc.h> +#include <grpc/support/host_port.h> +#include <grpc/support/log.h> +#include "test/core/util/test_config.h" +#include "test/core/util/port.h" +#include "test/core/end2end/data/ssl_test_data.h" + +typedef struct fullstack_secure_fixture_data { + char *localaddr; +} fullstack_secure_fixture_data; + +static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( + grpc_channel_args *client_args, grpc_channel_args *server_args) { + grpc_end2end_test_fixture f; + int port = grpc_pick_unused_port_or_die(); + fullstack_secure_fixture_data *ffd = + gpr_malloc(sizeof(fullstack_secure_fixture_data)); + memset(&f, 0, sizeof(f)); + + gpr_join_host_port(&ffd->localaddr, "localhost", port); + + f.fixture_data = ffd; + f.client_cq = grpc_completion_queue_create(); + f.server_cq = grpc_completion_queue_create(); + + return f; +} + +static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f, + grpc_channel_args *client_args, + grpc_credentials *creds) { + fullstack_secure_fixture_data *ffd = f->fixture_data; + f->client = grpc_secure_channel_create(creds, ffd->localaddr, client_args); + GPR_ASSERT(f->client != NULL); + grpc_credentials_release(creds); +} + +static void chttp2_init_server_secure_fullstack( + grpc_end2end_test_fixture *f, grpc_channel_args *server_args, + grpc_server_credentials *server_creds) { + fullstack_secure_fixture_data *ffd = f->fixture_data; + if (f->server) { + grpc_server_destroy(f->server); + } + f->server = grpc_server_create(server_args); + grpc_server_register_completion_queue(f->server, f->server_cq); + GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr, + server_creds)); + grpc_server_credentials_release(server_creds); + grpc_server_start(f->server); +} + +void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) { + fullstack_secure_fixture_data *ffd = f->fixture_data; + gpr_free(ffd->localaddr); + gpr_free(ffd); +} + +static void chttp2_init_client_simple_ssl_secure_fullstack( + grpc_end2end_test_fixture *f, grpc_channel_args *client_args) { + grpc_credentials *ssl_creds = grpc_ssl_credentials_create(NULL, NULL); + grpc_arg ssl_name_override = {GRPC_ARG_STRING, + GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, + {"foo.test.google.fr"}}; + grpc_channel_args *new_client_args = + grpc_channel_args_copy_and_add(client_args, &ssl_name_override); + chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); + grpc_channel_args_destroy(new_client_args); +} + +static void chttp2_init_server_simple_ssl_secure_fullstack( + grpc_end2end_test_fixture *f, grpc_channel_args *server_args) { + grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key, + test_server1_cert}; + grpc_server_credentials *ssl_creds = + grpc_ssl_server_credentials_create(NULL, &pem_cert_key_pair, 1); + chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); +} + +/* All test configurations */ + +static grpc_end2end_test_config configs[] = { + {"chttp2/simple_ssl_fullstack", + FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | + FEATURE_MASK_SUPPORTS_HOSTNAME_VERIFICATION | + FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS, + chttp2_create_fixture_secure_fullstack, + chttp2_init_client_simple_ssl_secure_fullstack, + chttp2_init_server_simple_ssl_secure_fullstack, + chttp2_tear_down_secure_fullstack}, +}; + +int main(int argc, char **argv) { + size_t i; + FILE *roots_file; + size_t roots_size = strlen(test_root_cert); + char *roots_filename; + + grpc_platform_become_multipoller = grpc_poll_become_multipoller; + + grpc_test_init(argc, argv); + + /* Set the SSL roots env var. */ + roots_file = gpr_tmpfile("chttp2_simple_ssl_with_poll_fullstack_test", + &roots_filename); + GPR_ASSERT(roots_filename != NULL); + GPR_ASSERT(roots_file != NULL); + GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size); + fclose(roots_file); + gpr_setenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR, roots_filename); + + grpc_init(); + + for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { + grpc_end2end_tests(configs[i]); + } + + grpc_shutdown(); + + /* Cleanup. */ + remove(roots_filename); + gpr_free(roots_filename); + + return 0; +} diff --git a/test/core/end2end/fixtures/chttp2_socket_pair.c b/test/core/end2end/fixtures/chttp2_socket_pair.c index 43ebf7eed5..e0221d0452 100644 --- a/test/core/end2end/fixtures/chttp2_socket_pair.c +++ b/test/core/end2end/fixtures/chttp2_socket_pair.c @@ -62,7 +62,8 @@ static grpc_transport_setup_result server_setup_transport( static grpc_channel_filter const *extra_filters[] = { &grpc_http_server_filter}; return grpc_server_setup_transport(f->server, transport, extra_filters, - GPR_ARRAY_SIZE(extra_filters), mdctx); + GPR_ARRAY_SIZE(extra_filters), mdctx, + grpc_server_get_channel_args(f->server)); } typedef struct { diff --git a/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c b/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c index 385d5a4e81..37b5529d7f 100644 --- a/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c +++ b/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c @@ -62,7 +62,8 @@ static grpc_transport_setup_result server_setup_transport( static grpc_channel_filter const *extra_filters[] = { &grpc_http_server_filter}; return grpc_server_setup_transport(f->server, transport, extra_filters, - GPR_ARRAY_SIZE(extra_filters), mdctx); + GPR_ARRAY_SIZE(extra_filters), mdctx, + grpc_server_get_channel_args(f->server)); } typedef struct { diff --git a/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c b/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c index 1f53e581ab..b15a18f1a7 100644 --- a/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c +++ b/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c @@ -63,7 +63,8 @@ static grpc_transport_setup_result server_setup_transport( static grpc_channel_filter const *extra_filters[] = { &grpc_http_server_filter}; return grpc_server_setup_transport(f->server, transport, extra_filters, - GPR_ARRAY_SIZE(extra_filters), mdctx); + GPR_ARRAY_SIZE(extra_filters), mdctx, + grpc_server_get_channel_args(f->server)); } typedef struct { diff --git a/test/core/end2end/gen_build_json.py b/test/core/end2end/gen_build_json.py index 960ec51b5d..fb13c7840c 100755 --- a/test/core/end2end/gen_build_json.py +++ b/test/core/end2end/gen_build_json.py @@ -44,8 +44,10 @@ default_secure_fixture_options = FixtureOptions(True, ['windows', 'posix']) END2END_FIXTURES = { 'chttp2_fake_security': default_secure_fixture_options, 'chttp2_fullstack': default_unsecure_fixture_options, + 'chttp2_fullstack_with_poll': FixtureOptions(False, ['posix']), 'chttp2_fullstack_uds_posix': FixtureOptions(False, ['posix']), 'chttp2_simple_ssl_fullstack': default_secure_fixture_options, + 'chttp2_simple_ssl_fullstack_with_poll': FixtureOptions(True, ['posix']), 'chttp2_simple_ssl_with_oauth2_fullstack': default_secure_fixture_options, 'chttp2_socket_pair': default_unsecure_fixture_options, 'chttp2_socket_pair_one_byte_at_a_time': default_unsecure_fixture_options, diff --git a/test/core/end2end/tests/request_response_with_payload_and_call_creds.c b/test/core/end2end/tests/request_response_with_payload_and_call_creds.c index df1e9d7671..08443f19a5 100644 --- a/test/core/end2end/tests/request_response_with_payload_and_call_creds.c +++ b/test/core/end2end/tests/request_response_with_payload_and_call_creds.c @@ -111,6 +111,23 @@ static void end_test(grpc_end2end_test_fixture *f) { grpc_completion_queue_destroy(f->client_cq); } +static void print_auth_context(int is_client, const grpc_auth_context *ctx) { + const grpc_auth_property *p; + grpc_auth_property_iterator it; + gpr_log(GPR_INFO, "%s peer:", is_client ? "client" : "server"); + gpr_log(GPR_INFO, "\tauthenticated: %s", + grpc_auth_context_peer_is_authenticated(ctx) ? "YES" : "NO"); + it = grpc_auth_context_peer_identity(ctx); + while ((p = grpc_auth_property_iterator_next(&it)) != NULL) { + gpr_log(GPR_INFO, "\t\t%s: %s", p->name, p->value); + } + gpr_log(GPR_INFO, "\tall properties:"); + it = grpc_auth_context_property_iterator(ctx); + while ((p = grpc_auth_property_iterator_next(&it)) != NULL) { + gpr_log(GPR_INFO, "\t\t%s: %s", p->name, p->value); + } +} + static void test_call_creds_failure(grpc_end2end_test_config config) { grpc_call *c; grpc_credentials *creds = NULL; @@ -160,6 +177,7 @@ static void request_response_with_payload_and_call_creds( size_t details_capacity = 0; int was_cancelled = 2; grpc_credentials *creds = NULL; + const grpc_auth_context *s_auth_context = NULL; c = grpc_channel_create_call(f.client, f.client_cq, "/foo", "foo.test.google.fr", deadline); @@ -214,10 +232,13 @@ static void request_response_with_payload_and_call_creds( GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s, &call_details, &request_metadata_recv, - f.server_cq, f.server_cq, + f.server_cq, f.server_cq, tag(101))); cq_expect_completion(v_server, tag(101), 1); cq_verify(v_server); + s_auth_context = grpc_call_auth_context(s); + GPR_ASSERT(s_auth_context != NULL); + print_auth_context(0, s_auth_context); /* Cannot set creds on the server call object. */ GPR_ASSERT(grpc_call_set_credentials(s, NULL) != GRPC_CALL_OK); diff --git a/test/core/security/auth_context_test.c b/test/core/security/auth_context_test.c new file mode 100644 index 0000000000..2fb0c01f6f --- /dev/null +++ b/test/core/security/auth_context_test.c @@ -0,0 +1,143 @@ +/* + * + * 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<string.h> + +#include "src/core/security/security_context.h" +#include "src/core/support/string.h" +#include "test/core/util/test_config.h" + +#include <grpc/support/log.h> + +static void test_empty_context(void) { + grpc_auth_context *ctx = grpc_auth_context_create(NULL, 0); + grpc_auth_property_iterator it; + + gpr_log(GPR_INFO, "test_empty_context"); + GPR_ASSERT(ctx != NULL); + GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL); + it = grpc_auth_context_peer_identity(ctx); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + it = grpc_auth_context_property_iterator(ctx); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + it = grpc_auth_context_find_properties_by_name(ctx, "foo"); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + grpc_auth_context_unref(ctx); +} + +static void test_simple_context(void) { + grpc_auth_context *ctx = grpc_auth_context_create(NULL, 3); + grpc_auth_property_iterator it; + size_t i; + + gpr_log(GPR_INFO, "test_simple_context"); + GPR_ASSERT(ctx != NULL); + GPR_ASSERT(ctx->property_count == 3); + ctx->properties[0] = grpc_auth_property_init_from_cstring("name", "chapi"); + ctx->properties[1] = grpc_auth_property_init_from_cstring("name", "chapo"); + ctx->properties[2] = grpc_auth_property_init_from_cstring("foo", "bar"); + ctx->peer_identity_property_name = ctx->properties[0].name; + + GPR_ASSERT( + strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0); + it = grpc_auth_context_property_iterator(ctx); + for (i = 0; i < ctx->property_count; i++) { + const grpc_auth_property *p = grpc_auth_property_iterator_next(&it); + GPR_ASSERT(p == &ctx->properties[i]); + } + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + + it = grpc_auth_context_find_properties_by_name(ctx, "foo"); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[2]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + + it = grpc_auth_context_peer_identity(ctx); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[0]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[1]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + + grpc_auth_context_unref(ctx); +} + +static void test_chained_context(void) { + grpc_auth_context *chained = grpc_auth_context_create(NULL, 2); + grpc_auth_context *ctx = grpc_auth_context_create(chained, 3); + grpc_auth_property_iterator it; + size_t i; + + gpr_log(GPR_INFO, "test_chained_context"); + grpc_auth_context_unref(chained); + chained->properties[0] = + grpc_auth_property_init_from_cstring("name", "padapo"); + chained->properties[1] = grpc_auth_property_init_from_cstring("foo", "baz"); + ctx->properties[0] = grpc_auth_property_init_from_cstring("name", "chapi"); + ctx->properties[1] = grpc_auth_property_init_from_cstring("name", "chapo"); + ctx->properties[2] = grpc_auth_property_init_from_cstring("foo", "bar"); + ctx->peer_identity_property_name = ctx->properties[0].name; + + GPR_ASSERT( + strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0); + it = grpc_auth_context_property_iterator(ctx); + for (i = 0; i < ctx->property_count; i++) { + const grpc_auth_property *p = grpc_auth_property_iterator_next(&it); + GPR_ASSERT(p == &ctx->properties[i]); + } + for (i = 0; i < chained->property_count; i++) { + const grpc_auth_property *p = grpc_auth_property_iterator_next(&it); + GPR_ASSERT(p == &chained->properties[i]); + } + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + + it = grpc_auth_context_find_properties_by_name(ctx, "foo"); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[2]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &chained->properties[1]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + + it = grpc_auth_context_peer_identity(ctx); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[0]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[1]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &chained->properties[0]); + GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); + + grpc_auth_context_unref(ctx); +} + + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_empty_context(); + test_simple_context(); + test_chained_context(); + return 0; +} + diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index 1b657e3d89..9a77f88e73 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -135,51 +135,113 @@ static grpc_httpcli_response http_response(int status, const char *body) { return response; } +static void test_empty_md_store(void) { + grpc_credentials_md_store *store = grpc_credentials_md_store_create(0); + GPR_ASSERT(store->num_entries == 0); + GPR_ASSERT(store->allocated == 0); + grpc_credentials_md_store_unref(store); +} + +static void test_ref_unref_empty_md_store(void) { + grpc_credentials_md_store *store = grpc_credentials_md_store_create(0); + grpc_credentials_md_store_ref(store); + grpc_credentials_md_store_ref(store); + GPR_ASSERT(store->num_entries == 0); + GPR_ASSERT(store->allocated == 0); + grpc_credentials_md_store_unref(store); + grpc_credentials_md_store_unref(store); + grpc_credentials_md_store_unref(store); +} + +static void test_add_to_empty_md_store(void) { + grpc_credentials_md_store *store = grpc_credentials_md_store_create(0); + const char *key_str = "hello"; + const char *value_str = "there blah blah blah blah blah blah blah"; + gpr_slice key = gpr_slice_from_copied_string(key_str); + gpr_slice value = gpr_slice_from_copied_string(value_str); + grpc_credentials_md_store_add(store, key, value); + GPR_ASSERT(store->num_entries == 1); + GPR_ASSERT(gpr_slice_cmp(key, store->entries[0].key) == 0); + GPR_ASSERT(gpr_slice_cmp(value, store->entries[0].value) == 0); + gpr_slice_unref(key); + gpr_slice_unref(value); + grpc_credentials_md_store_unref(store); +} + +static void test_add_cstrings_to_empty_md_store(void) { + grpc_credentials_md_store *store = grpc_credentials_md_store_create(0); + const char *key_str = "hello"; + const char *value_str = "there blah blah blah blah blah blah blah"; + grpc_credentials_md_store_add_cstrings(store, key_str, value_str); + GPR_ASSERT(store->num_entries == 1); + GPR_ASSERT(gpr_slice_str_cmp(store->entries[0].key, key_str) == 0); + GPR_ASSERT(gpr_slice_str_cmp(store->entries[0].value, value_str) == 0); + grpc_credentials_md_store_unref(store); +} + +static void test_empty_preallocated_md_store(void) { + grpc_credentials_md_store *store = grpc_credentials_md_store_create(4); + GPR_ASSERT(store->num_entries == 0); + GPR_ASSERT(store->allocated == 4); + GPR_ASSERT(store->entries != NULL); + grpc_credentials_md_store_unref(store); +} + +static void test_add_abunch_to_md_store(void) { + grpc_credentials_md_store *store = grpc_credentials_md_store_create(4); + size_t num_entries = 1000; + const char *key_str = "hello"; + const char *value_str = "there blah blah blah blah blah blah blah"; + size_t i; + for (i = 0; i < num_entries; i++) { + grpc_credentials_md_store_add_cstrings(store, key_str, value_str); + } + for (i = 0; i < num_entries; i++) { + GPR_ASSERT(gpr_slice_str_cmp(store->entries[i].key, key_str) == 0); + GPR_ASSERT(gpr_slice_str_cmp(store->entries[i].value, value_str) == 0); + } + grpc_credentials_md_store_unref(store); +} + static void test_oauth2_token_fetcher_creds_parsing_ok(void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(200, valid_oauth2_json_response); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_OK); GPR_ASSERT(token_lifetime.tv_sec == 3599); GPR_ASSERT(token_lifetime.tv_nsec == 0); - GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(token_elem->key), - "Authorization") == 0); - GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(token_elem->value), - "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == 0); - grpc_mdelem_unref(token_elem); - grpc_mdctx_unref(ctx); + GPR_ASSERT(token_md->num_entries == 1); + GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].key, "Authorization") == 0); + GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].value, + "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == + 0); + grpc_credentials_md_store_unref(token_md); } static void test_oauth2_token_fetcher_creds_parsing_bad_http_status(void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(401, valid_oauth2_json_response); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); - grpc_mdctx_unref(ctx); } static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(200, ""); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); - grpc_mdctx_unref(ctx); } static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(200, @@ -187,28 +249,24 @@ static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) { " \"expires_in\":3599, " " \"token_type\":\"Bearer\""); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); - grpc_mdctx_unref(ctx); } static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(200, "{" " \"expires_in\":3599, " " \"token_type\":\"Bearer\"}"); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); - grpc_mdctx_unref(ctx); } static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(200, @@ -216,35 +274,32 @@ static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) { " \"expires_in\":3599, " "}"); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); - grpc_mdctx_unref(ctx); } static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime( void) { - grpc_mdctx *ctx = grpc_mdctx_create(); - grpc_mdelem *token_elem = NULL; + grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; grpc_httpcli_response response = http_response(200, "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," " \"token_type\":\"Bearer\"}"); GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, ctx, &token_elem, &token_lifetime) == + &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); - grpc_mdctx_unref(ctx); } -static void check_metadata(expected_md *expected, grpc_mdelem **md_elems, +static void check_metadata(expected_md *expected, grpc_credentials_md *md_elems, size_t num_md) { size_t i; for (i = 0; i < num_md; i++) { size_t j; for (j = 0; j < num_md; j++) { - if (0 == gpr_slice_str_cmp(md_elems[j]->key->slice, expected[i].key)) { - GPR_ASSERT(0 == gpr_slice_str_cmp(md_elems[j]->value->slice, - expected[i].value)); + if (0 == gpr_slice_str_cmp(md_elems[j].key, expected[i].key)) { + GPR_ASSERT(gpr_slice_str_cmp(md_elems[j].value, expected[i].value) == + 0); break; } } @@ -255,7 +310,7 @@ static void check_metadata(expected_md *expected, grpc_mdelem **md_elems, } } -static void check_iam_metadata(void *user_data, grpc_mdelem **md_elems, +static void check_iam_metadata(void *user_data, grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { grpc_credentials *c = (grpc_credentials *)user_data; expected_md emd[] = { @@ -277,7 +332,7 @@ static void test_iam_creds(void) { } static void check_ssl_oauth2_composite_metadata( - void *user_data, grpc_mdelem **md_elems, size_t num_md, + void *user_data, grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { grpc_credentials *c = (grpc_credentials *)user_data; expected_md emd[] = { @@ -327,7 +382,7 @@ void test_ssl_fake_transport_security_composite_creds_failure(void) { } static void check_ssl_oauth2_iam_composite_metadata( - void *user_data, grpc_mdelem **md_elems, size_t num_md, + void *user_data, grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { grpc_credentials *c = (grpc_credentials *)user_data; expected_md emd[] = { @@ -374,20 +429,20 @@ static void test_ssl_oauth2_iam_composite_creds(void) { } static void on_oauth2_creds_get_metadata_success( - void *user_data, grpc_mdelem **md_elems, size_t num_md, + void *user_data, grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { GPR_ASSERT(status == GRPC_CREDENTIALS_OK); GPR_ASSERT(num_md == 1); - GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->key), - "Authorization") == 0); - GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->value), - "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == 0); + GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].key, "Authorization") == 0); + GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].value, + "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == + 0); GPR_ASSERT(user_data != NULL); GPR_ASSERT(strcmp((const char *)user_data, test_user_data) == 0); } static void on_oauth2_creds_get_metadata_failure( - void *user_data, grpc_mdelem **md_elems, size_t num_md, + void *user_data, grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR); GPR_ASSERT(num_md == 0); @@ -719,24 +774,22 @@ static void test_service_account_creds_signing_failure(void) { } static void on_jwt_creds_get_metadata_success(void *user_data, - grpc_mdelem **md_elems, + grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { char *expected_md_value; gpr_asprintf(&expected_md_value, "Bearer %s", test_signed_jwt); GPR_ASSERT(status == GRPC_CREDENTIALS_OK); GPR_ASSERT(num_md == 1); - GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->key), - "Authorization") == 0); - GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->value), - expected_md_value) == 0); + GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].key, "Authorization") == 0); + GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].value, expected_md_value) == 0); GPR_ASSERT(user_data != NULL); GPR_ASSERT(strcmp((const char *)user_data, test_user_data) == 0); gpr_free(expected_md_value); } static void on_jwt_creds_get_metadata_failure(void *user_data, - grpc_mdelem **md_elems, + grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR); @@ -796,6 +849,12 @@ static void test_jwt_creds_signing_failure(void) { int main(int argc, char **argv) { grpc_test_init(argc, argv); + test_empty_md_store(); + test_ref_unref_empty_md_store(); + test_add_to_empty_md_store(); + test_add_cstrings_to_empty_md_store(); + test_empty_preallocated_md_store(); + test_add_abunch_to_md_store(); test_oauth2_token_fetcher_creds_parsing_ok(); test_oauth2_token_fetcher_creds_parsing_bad_http_status(); test_oauth2_token_fetcher_creds_parsing_empty_http_body(); diff --git a/test/core/support/cmdline_test.c b/test/core/support/cmdline_test.c index a7767ace5e..26153b192c 100644 --- a/test/core/support/cmdline_test.c +++ b/test/core/support/cmdline_test.c @@ -35,6 +35,7 @@ #include <string.h> +#include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/useful.h> #include "test/core/util/test_config.h" @@ -272,6 +273,27 @@ static void test_many(void) { gpr_cmdline_destroy(cl); } +static void test_usage(void) { + gpr_cmdline *cl; + char *usage; + + char *str = NULL; + int x = 0; + int flag = 2; + + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_string(cl, "str", NULL, &str); + gpr_cmdline_add_int(cl, "x", NULL, &x); + gpr_cmdline_add_flag(cl, "flag", NULL, &flag); + + usage = gpr_cmdline_usage_string(cl, "test"); + GPR_ASSERT(0 == strcmp(usage, + "Usage: test [--str=string] [--x=int] [--flag|--no-flag]\n")); + gpr_free(usage); + + gpr_cmdline_destroy(cl); +} + int main(int argc, char **argv) { grpc_test_init(argc, argv); test_simple_int(); @@ -289,5 +311,6 @@ int main(int argc, char **argv) { test_flag_val_true(); test_flag_val_false(); test_many(); + test_usage(); return 0; } |