diff options
author | Craig Tiller <ctiller@google.com> | 2015-09-22 10:42:19 -0700 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2015-09-22 10:42:19 -0700 |
commit | 45724b35e411fef7c5da66a74c78428c11d56843 (patch) | |
tree | 9264034aca675c89444e02f72ef58e67d7043604 /test/core | |
parent | 298751c1195523ef6228595043b583c3a6270e08 (diff) |
indent pass to get logical source lines on one physical line
Diffstat (limited to 'test/core')
165 files changed, 17339 insertions, 15423 deletions
diff --git a/test/core/bad_client/bad_client.c b/test/core/bad_client/bad_client.c index 7605904411..7661aec656 100644 --- a/test/core/bad_client/bad_client.c +++ b/test/core/bad_client/bad_client.c @@ -45,7 +45,8 @@ #include <grpc/support/sync.h> #include <grpc/support/thd.h> -typedef struct { +typedef struct +{ grpc_server *server; grpc_completion_queue *cq; grpc_bad_client_server_side_validator validator; @@ -53,121 +54,117 @@ typedef struct { gpr_event done_write; } thd_args; -static void thd_func(void *arg) { +static void +thd_func (void *arg) +{ thd_args *a = arg; - a->validator(a->server, a->cq); - gpr_event_set(&a->done_thd, (void *)1); + a->validator (a->server, a->cq); + gpr_event_set (&a->done_thd, (void *) 1); } -static void done_write(void *arg, int success, - grpc_closure_list *closure_list) { +static void +done_write (void *arg, int success, grpc_closure_list * closure_list) +{ thd_args *a = arg; - gpr_event_set(&a->done_write, (void *)1); + gpr_event_set (&a->done_write, (void *) 1); } -static void server_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx) { +static void +server_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx) +{ thd_args *a = ts; static grpc_channel_filter const *extra_filters[] = { - &grpc_http_server_filter}; + &grpc_http_server_filter + }; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_server_setup_transport( - a->server, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters), mdctx, - grpc_server_get_channel_args(a->server), &closure_list); - grpc_closure_list_run(&closure_list); + grpc_server_setup_transport (a->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (a->server), &closure_list); + grpc_closure_list_run (&closure_list); } -void grpc_run_bad_client_test(grpc_bad_client_server_side_validator validator, - const char *client_payload, - size_t client_payload_length, gpr_uint32 flags) { +void +grpc_run_bad_client_test (grpc_bad_client_server_side_validator validator, const char *client_payload, size_t client_payload_length, gpr_uint32 flags) +{ grpc_endpoint_pair sfd; thd_args a; gpr_thd_id id; char *hex; grpc_transport *transport; - grpc_mdctx *mdctx = grpc_mdctx_create(); - gpr_slice slice = - gpr_slice_from_copied_buffer(client_payload, client_payload_length); + grpc_mdctx *mdctx = grpc_mdctx_create (); + gpr_slice slice = gpr_slice_from_copied_buffer (client_payload, client_payload_length); gpr_slice_buffer outgoing; grpc_closure done_write_closure; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - hex = gpr_dump(client_payload, client_payload_length, - GPR_DUMP_HEX | GPR_DUMP_ASCII); + hex = gpr_dump (client_payload, client_payload_length, GPR_DUMP_HEX | GPR_DUMP_ASCII); /* Add a debug log */ - gpr_log(GPR_INFO, "TEST: %s", hex); + gpr_log (GPR_INFO, "TEST: %s", hex); - gpr_free(hex); + gpr_free (hex); /* Init grpc */ - grpc_init(); + grpc_init (); /* Create endpoints */ - sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536); + sfd = grpc_iomgr_create_endpoint_pair ("fixture", 65536); /* Create server, completion events */ - a.server = grpc_server_create_from_filters(NULL, 0, NULL); - a.cq = grpc_completion_queue_create(NULL); - gpr_event_init(&a.done_thd); - gpr_event_init(&a.done_write); + a.server = grpc_server_create_from_filters (NULL, 0, NULL); + a.cq = grpc_completion_queue_create (NULL); + gpr_event_init (&a.done_thd); + gpr_event_init (&a.done_write); a.validator = validator; - grpc_server_register_completion_queue(a.server, a.cq, NULL); - grpc_server_start(a.server); - transport = - grpc_create_chttp2_transport(NULL, sfd.server, mdctx, 0, &closure_list); - server_setup_transport(&a, transport, mdctx); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_server_register_completion_queue (a.server, a.cq, NULL); + grpc_server_start (a.server); + transport = grpc_create_chttp2_transport (NULL, sfd.server, mdctx, 0, &closure_list); + server_setup_transport (&a, transport, mdctx); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); /* Bind everything into the same pollset */ - grpc_endpoint_add_to_pollset(sfd.client, grpc_cq_pollset(a.cq), - &closure_list); - grpc_endpoint_add_to_pollset(sfd.server, grpc_cq_pollset(a.cq), - &closure_list); + grpc_endpoint_add_to_pollset (sfd.client, grpc_cq_pollset (a.cq), &closure_list); + grpc_endpoint_add_to_pollset (sfd.server, grpc_cq_pollset (a.cq), &closure_list); /* Check a ground truth */ - GPR_ASSERT(grpc_server_has_open_connections(a.server)); + GPR_ASSERT (grpc_server_has_open_connections (a.server)); /* Start validator */ - gpr_thd_new(&id, thd_func, &a, NULL); + gpr_thd_new (&id, thd_func, &a, NULL); - gpr_slice_buffer_init(&outgoing); - gpr_slice_buffer_add(&outgoing, slice); - grpc_closure_init(&done_write_closure, done_write, &a); + gpr_slice_buffer_init (&outgoing); + gpr_slice_buffer_add (&outgoing, slice); + grpc_closure_init (&done_write_closure, done_write, &a); /* Write data */ - grpc_endpoint_write(sfd.client, &outgoing, &done_write_closure, - &closure_list); - grpc_closure_list_run(&closure_list); + grpc_endpoint_write (sfd.client, &outgoing, &done_write_closure, &closure_list); + grpc_closure_list_run (&closure_list); /* Await completion */ - GPR_ASSERT( - gpr_event_wait(&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5))); + GPR_ASSERT (gpr_event_wait (&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5))); - if (flags & GRPC_BAD_CLIENT_DISCONNECT) { - grpc_endpoint_shutdown(sfd.client, &closure_list); - grpc_endpoint_destroy(sfd.client, &closure_list); - grpc_closure_list_run(&closure_list); - sfd.client = NULL; - } + if (flags & GRPC_BAD_CLIENT_DISCONNECT) + { + grpc_endpoint_shutdown (sfd.client, &closure_list); + grpc_endpoint_destroy (sfd.client, &closure_list); + grpc_closure_list_run (&closure_list); + sfd.client = NULL; + } - GPR_ASSERT(gpr_event_wait(&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5))); + GPR_ASSERT (gpr_event_wait (&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5))); /* Shutdown */ - if (sfd.client) { - grpc_endpoint_shutdown(sfd.client, &closure_list); - grpc_endpoint_destroy(sfd.client, &closure_list); - grpc_closure_list_run(&closure_list); - } - grpc_server_shutdown_and_notify(a.server, a.cq, NULL); - GPR_ASSERT(grpc_completion_queue_pluck( - a.cq, NULL, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(a.server); - grpc_completion_queue_destroy(a.cq); - gpr_slice_buffer_destroy(&outgoing); - - grpc_closure_list_run(&closure_list); - grpc_shutdown(); + if (sfd.client) + { + grpc_endpoint_shutdown (sfd.client, &closure_list); + grpc_endpoint_destroy (sfd.client, &closure_list); + grpc_closure_list_run (&closure_list); + } + grpc_server_shutdown_and_notify (a.server, a.cq, NULL); + GPR_ASSERT (grpc_completion_queue_pluck (a.cq, NULL, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (a.server); + grpc_completion_queue_destroy (a.cq); + gpr_slice_buffer_destroy (&outgoing); + + grpc_closure_list_run (&closure_list); + grpc_shutdown (); } diff --git a/test/core/bad_client/bad_client.h b/test/core/bad_client/bad_client.h index 01beda60ee..437e884f6a 100644 --- a/test/core/bad_client/bad_client.h +++ b/test/core/bad_client/bad_client.h @@ -37,8 +37,7 @@ #include <grpc/grpc.h> #include "test/core/util/test_config.h" -typedef void (*grpc_bad_client_server_side_validator)( - grpc_server *server, grpc_completion_queue *cq); +typedef void (*grpc_bad_client_server_side_validator) (grpc_server * server, grpc_completion_queue * cq); #define GRPC_BAD_CLIENT_DISCONNECT 1 @@ -47,9 +46,7 @@ typedef void (*grpc_bad_client_server_side_validator)( Create a server, and send client_payload to it as bytes from a client. Execute validator in a separate thread to assert that the bytes are handled as expected. */ -void grpc_run_bad_client_test(grpc_bad_client_server_side_validator validator, - const char *client_payload, - size_t client_payload_length, gpr_uint32 flags); +void grpc_run_bad_client_test (grpc_bad_client_server_side_validator validator, const char *client_payload, size_t client_payload_length, gpr_uint32 flags); #define GRPC_RUN_BAD_CLIENT_TEST(validator, payload, flags) \ grpc_run_bad_client_test(validator, payload, sizeof(payload) - 1, flags) diff --git a/test/core/bad_client/tests/connection_prefix.c b/test/core/bad_client/tests/connection_prefix.c index ec85211605..0e51e9a267 100644 --- a/test/core/bad_client/tests/connection_prefix.c +++ b/test/core/bad_client/tests/connection_prefix.c @@ -34,40 +34,43 @@ #include "test/core/bad_client/bad_client.h" #include "src/core/surface/server.h" -static void verifier(grpc_server *server, grpc_completion_queue *cq) { - while (grpc_server_has_open_connections(server)) { - GPR_ASSERT(grpc_completion_queue_next( - cq, GRPC_TIMEOUT_MILLIS_TO_DEADLINE(20), NULL) - .type == GRPC_QUEUE_TIMEOUT); - } +static void +verifier (grpc_server * server, grpc_completion_queue * cq) +{ + while (grpc_server_has_open_connections (server)) + { + GPR_ASSERT (grpc_completion_queue_next (cq, GRPC_TIMEOUT_MILLIS_TO_DEADLINE (20), NULL).type == GRPC_QUEUE_TIMEOUT); + } } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRIX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI *X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTPX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0X", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\rX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\nX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\rX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\r\nX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\r\nSX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\r\nSMX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\r\nSM\rX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\r\nSM\r\nX", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, "PRI * HTTP/2.0\r\n\r\nSM\r\n\rX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRIX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI *X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTPX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0X", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\rX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\nX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\rX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\r\nX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\r\nSX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\r\nSMX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\r\nSM\rX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\r\nSM\r\nX", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, "PRI * HTTP/2.0\r\n\r\nSM\r\n\rX", 0); return 0; } diff --git a/test/core/bad_client/tests/initial_settings_frame.c b/test/core/bad_client/tests/initial_settings_frame.c index 261fecdaf2..3db6d83369 100644 --- a/test/core/bad_client/tests/initial_settings_frame.c +++ b/test/core/bad_client/tests/initial_settings_frame.c @@ -36,60 +36,43 @@ #define PFX_STR "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" -static void verifier(grpc_server *server, grpc_completion_queue *cq) { - while (grpc_server_has_open_connections(server)) { - GPR_ASSERT(grpc_completion_queue_next( - cq, GRPC_TIMEOUT_MILLIS_TO_DEADLINE(20), NULL) - .type == GRPC_QUEUE_TIMEOUT); - } +static void +verifier (grpc_server * server, grpc_completion_queue * cq) +{ + while (grpc_server_has_open_connections (server)) + { + GPR_ASSERT (grpc_completion_queue_next (cq, GRPC_TIMEOUT_MILLIS_TO_DEADLINE (20), NULL).type == GRPC_QUEUE_TIMEOUT); + } } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); /* various partial prefixes */ - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x06", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x06", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x06", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04\x00", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04\x01", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04\xff", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04\x00\x00", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04\x00\x00\x00", - GRPC_BAD_CLIENT_DISCONNECT); - GRPC_RUN_BAD_CLIENT_TEST(verifier, PFX_STR "\x00\x00\x00\x04\x00\x00\x00\x00", - GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x06", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x06", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x06", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x00", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x01", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\xff", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x00\x00", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x00\x00\x00", GRPC_BAD_CLIENT_DISCONNECT); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x00\x00\x00\x00", GRPC_BAD_CLIENT_DISCONNECT); /* must not send frames with stream id != 0 */ - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x00\x04\x00\x00\x00\x00\x01", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x00\x04\x00\x40\x00\x00\x00", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x00\x00\x00\x00\x01", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x00\x04\x00\x40\x00\x00\x00", 0); /* settings frame must be a multiple of six bytes long */ - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x01\x04\x00\x00\x00\x00\x00", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x02\x04\x00\x00\x00\x00\x00", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x03\x04\x00\x00\x00\x00\x00", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x04\x04\x00\x00\x00\x00\x00", 0); - GRPC_RUN_BAD_CLIENT_TEST(verifier, - PFX_STR "\x00\x00\x05\x04\x00\x00\x00\x00\x00", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x01\x04\x00\x00\x00\x00\x00", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x02\x04\x00\x00\x00\x00\x00", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x03\x04\x00\x00\x00\x00\x00", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x04\x04\x00\x00\x00\x00\x00", 0); + GRPC_RUN_BAD_CLIENT_TEST (verifier, PFX_STR "\x00\x00\x05\x04\x00\x00\x00\x00\x00", 0); return 0; } diff --git a/test/core/channel/channel_args_test.c b/test/core/channel/channel_args_test.c index 0b74dee41e..207196ebdb 100644 --- a/test/core/channel/channel_args_test.c +++ b/test/core/channel/channel_args_test.c @@ -40,7 +40,9 @@ #include "test/core/util/test_config.h" -static void test_create(void) { +static void +test_create (void) +{ grpc_arg arg_int; grpc_arg arg_string; grpc_arg to_add[2]; @@ -56,87 +58,94 @@ static void test_create(void) { to_add[0] = arg_int; to_add[1] = arg_string; - ch_args = grpc_channel_args_copy_and_add(NULL, to_add, 2); + ch_args = grpc_channel_args_copy_and_add (NULL, to_add, 2); - GPR_ASSERT(ch_args->num_args == 2); - GPR_ASSERT(strcmp(ch_args->args[0].key, arg_int.key) == 0); - GPR_ASSERT(ch_args->args[0].type == arg_int.type); - GPR_ASSERT(ch_args->args[0].value.integer == arg_int.value.integer); + GPR_ASSERT (ch_args->num_args == 2); + GPR_ASSERT (strcmp (ch_args->args[0].key, arg_int.key) == 0); + GPR_ASSERT (ch_args->args[0].type == arg_int.type); + GPR_ASSERT (ch_args->args[0].value.integer == arg_int.value.integer); - GPR_ASSERT(strcmp(ch_args->args[1].key, arg_string.key) == 0); - GPR_ASSERT(ch_args->args[1].type == arg_string.type); - GPR_ASSERT(strcmp(ch_args->args[1].value.string, arg_string.value.string) == - 0); + GPR_ASSERT (strcmp (ch_args->args[1].key, arg_string.key) == 0); + GPR_ASSERT (ch_args->args[1].type == arg_string.type); + GPR_ASSERT (strcmp (ch_args->args[1].value.string, arg_string.value.string) == 0); - grpc_channel_args_destroy(ch_args); + grpc_channel_args_destroy (ch_args); } -static void test_set_compression_algorithm(void) { +static void +test_set_compression_algorithm (void) +{ grpc_channel_args *ch_args; - ch_args = - grpc_channel_args_set_compression_algorithm(NULL, GRPC_COMPRESS_GZIP); - GPR_ASSERT(ch_args->num_args == 1); - GPR_ASSERT(strcmp(ch_args->args[0].key, GRPC_COMPRESSION_ALGORITHM_ARG) == 0); - GPR_ASSERT(ch_args->args[0].type == GRPC_ARG_INTEGER); + ch_args = grpc_channel_args_set_compression_algorithm (NULL, GRPC_COMPRESS_GZIP); + GPR_ASSERT (ch_args->num_args == 1); + GPR_ASSERT (strcmp (ch_args->args[0].key, GRPC_COMPRESSION_ALGORITHM_ARG) == 0); + GPR_ASSERT (ch_args->args[0].type == GRPC_ARG_INTEGER); - grpc_channel_args_destroy(ch_args); + grpc_channel_args_destroy (ch_args); } -static void test_compression_algorithm_states(void) { +static void +test_compression_algorithm_states (void) +{ grpc_channel_args *ch_args, *ch_args_wo_gzip, *ch_args_wo_gzip_deflate; unsigned states_bitset; size_t i; - ch_args = grpc_channel_args_copy_and_add(NULL, NULL, 0); + ch_args = grpc_channel_args_copy_and_add (NULL, NULL, 0); /* by default, all enabled */ - states_bitset = - (unsigned)grpc_channel_args_compression_algorithm_get_states(ch_args); + states_bitset = (unsigned) grpc_channel_args_compression_algorithm_get_states (ch_args); - for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { - GPR_ASSERT(GPR_BITGET(states_bitset, i)); - } + for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) + { + GPR_ASSERT (GPR_BITGET (states_bitset, i)); + } /* disable gzip and deflate */ - ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state( - &ch_args, GRPC_COMPRESS_GZIP, 0); - GPR_ASSERT(ch_args == ch_args_wo_gzip); - ch_args_wo_gzip_deflate = grpc_channel_args_compression_algorithm_set_state( - &ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0); - GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate); - - states_bitset = (unsigned)grpc_channel_args_compression_algorithm_get_states( - ch_args_wo_gzip_deflate); - for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { - if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE) { - GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0); - } else { - GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0); + ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state (&ch_args, GRPC_COMPRESS_GZIP, 0); + GPR_ASSERT (ch_args == ch_args_wo_gzip); + ch_args_wo_gzip_deflate = grpc_channel_args_compression_algorithm_set_state (&ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0); + GPR_ASSERT (ch_args_wo_gzip == ch_args_wo_gzip_deflate); + + states_bitset = (unsigned) grpc_channel_args_compression_algorithm_get_states (ch_args_wo_gzip_deflate); + for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) + { + if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE) + { + GPR_ASSERT (GPR_BITGET (states_bitset, i) == 0); + } + else + { + GPR_ASSERT (GPR_BITGET (states_bitset, i) != 0); + } } - } /* re-enabled gzip only */ - ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state( - &ch_args_wo_gzip_deflate, GRPC_COMPRESS_GZIP, 1); - GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate); - - states_bitset = (unsigned)grpc_channel_args_compression_algorithm_get_states( - ch_args_wo_gzip); - for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { - if (i == GRPC_COMPRESS_DEFLATE) { - GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0); - } else { - GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0); + ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state (&ch_args_wo_gzip_deflate, GRPC_COMPRESS_GZIP, 1); + GPR_ASSERT (ch_args_wo_gzip == ch_args_wo_gzip_deflate); + + states_bitset = (unsigned) grpc_channel_args_compression_algorithm_get_states (ch_args_wo_gzip); + for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) + { + if (i == GRPC_COMPRESS_DEFLATE) + { + GPR_ASSERT (GPR_BITGET (states_bitset, i) == 0); + } + else + { + GPR_ASSERT (GPR_BITGET (states_bitset, i) != 0); + } } - } - grpc_channel_args_destroy(ch_args); + grpc_channel_args_destroy (ch_args); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_create(); - test_set_compression_algorithm(); - test_compression_algorithm_states(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_create (); + test_set_compression_algorithm (); + test_compression_algorithm_states (); return 0; } diff --git a/test/core/channel/channel_stack_test.c b/test/core/channel/channel_stack_test.c index 72b48e208d..af5c705810 100644 --- a/test/core/channel/channel_stack_test.c +++ b/test/core/channel/channel_stack_test.c @@ -41,56 +41,63 @@ #include "test/core/util/test_config.h" -static void channel_init_func(grpc_channel_element *elem, grpc_channel *master, - const grpc_channel_args *args, - grpc_mdctx *metadata_context, int is_first, - int is_last, grpc_closure_list *closure_list) { - GPR_ASSERT(args->num_args == 1); - GPR_ASSERT(args->args[0].type == GRPC_ARG_INTEGER); - GPR_ASSERT(0 == strcmp(args->args[0].key, "test_key")); - GPR_ASSERT(args->args[0].value.integer == 42); - GPR_ASSERT(is_first); - GPR_ASSERT(is_last); - *(int *)(elem->channel_data) = 0; +static void +channel_init_func (grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, int is_first, int is_last, grpc_closure_list * closure_list) +{ + GPR_ASSERT (args->num_args == 1); + GPR_ASSERT (args->args[0].type == GRPC_ARG_INTEGER); + GPR_ASSERT (0 == strcmp (args->args[0].key, "test_key")); + GPR_ASSERT (args->args[0].value.integer == 42); + GPR_ASSERT (is_first); + GPR_ASSERT (is_last); + *(int *) (elem->channel_data) = 0; } -static void call_init_func(grpc_call_element *elem, - const void *server_transport_data, - grpc_transport_stream_op *initial_op, - grpc_closure_list *closure_list) { - ++*(int *)(elem->channel_data); - *(int *)(elem->call_data) = 0; +static void +call_init_func (grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op, grpc_closure_list * closure_list) +{ + ++*(int *) (elem->channel_data); + *(int *) (elem->call_data) = 0; } -static void channel_destroy_func(grpc_channel_element *elem, - grpc_closure_list *closure_list) {} +static void +channel_destroy_func (grpc_channel_element * elem, grpc_closure_list * closure_list) +{ +} -static void call_destroy_func(grpc_call_element *elem, - grpc_closure_list *closure_list) { - ++*(int *)(elem->channel_data); +static void +call_destroy_func (grpc_call_element * elem, grpc_closure_list * closure_list) +{ + ++*(int *) (elem->channel_data); } -static void call_func(grpc_call_element *elem, grpc_transport_stream_op *op, - grpc_closure_list *closure_list) { - ++*(int *)(elem->call_data); +static void +call_func (grpc_call_element * elem, grpc_transport_stream_op * op, grpc_closure_list * closure_list) +{ + ++*(int *) (elem->call_data); } -static void channel_func(grpc_channel_element *elem, grpc_transport_op *op, - grpc_closure_list *closure_list) { - ++*(int *)(elem->channel_data); +static void +channel_func (grpc_channel_element * elem, grpc_transport_op * op, grpc_closure_list * closure_list) +{ + ++*(int *) (elem->channel_data); } -static char *get_peer(grpc_call_element *elem, - grpc_closure_list *closure_list) { - return gpr_strdup("peer"); +static char * +get_peer (grpc_call_element * elem, grpc_closure_list * closure_list) +{ + return gpr_strdup ("peer"); } -static void test_create_channel_stack(void) { - const grpc_channel_filter filter = {call_func, channel_func, - sizeof(int), call_init_func, - call_destroy_func, sizeof(int), - channel_init_func, channel_destroy_func, - get_peer, "some_test_filter"}; +static void +test_create_channel_stack (void) +{ + const grpc_channel_filter filter = { call_func, channel_func, + sizeof (int), call_init_func, + call_destroy_func, sizeof (int), + channel_init_func, channel_destroy_func, + get_peer, "some_test_filter" + }; const grpc_channel_filter *filters = &filter; grpc_channel_stack *channel_stack; grpc_call_stack *call_stack; @@ -103,7 +110,7 @@ static void test_create_channel_stack(void) { int *call_data; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - metadata_context = grpc_mdctx_create(); + metadata_context = grpc_mdctx_create (); arg.type = GRPC_ARG_INTEGER; arg.key = "test_key"; @@ -112,38 +119,39 @@ static void test_create_channel_stack(void) { chan_args.num_args = 1; chan_args.args = &arg; - channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1)); - grpc_channel_stack_init(&filters, 1, NULL, &chan_args, metadata_context, - channel_stack, &closure_list); - GPR_ASSERT(channel_stack->count == 1); - channel_elem = grpc_channel_stack_element(channel_stack, 0); - channel_data = (int *)channel_elem->channel_data; - GPR_ASSERT(*channel_data == 0); - - call_stack = gpr_malloc(channel_stack->call_stack_size); - grpc_call_stack_init(channel_stack, NULL, NULL, call_stack, &closure_list); - GPR_ASSERT(call_stack->count == 1); - call_elem = grpc_call_stack_element(call_stack, 0); - GPR_ASSERT(call_elem->filter == channel_elem->filter); - GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data); - call_data = (int *)call_elem->call_data; - GPR_ASSERT(*call_data == 0); - GPR_ASSERT(*channel_data == 1); - - grpc_call_stack_destroy(call_stack, &closure_list); - gpr_free(call_stack); - GPR_ASSERT(*channel_data == 2); - - grpc_channel_stack_destroy(channel_stack, &closure_list); - gpr_free(channel_stack); - - grpc_mdctx_unref(metadata_context); - - GPR_ASSERT(grpc_closure_list_empty(closure_list)); + channel_stack = gpr_malloc (grpc_channel_stack_size (&filters, 1)); + grpc_channel_stack_init (&filters, 1, NULL, &chan_args, metadata_context, channel_stack, &closure_list); + GPR_ASSERT (channel_stack->count == 1); + channel_elem = grpc_channel_stack_element (channel_stack, 0); + channel_data = (int *) channel_elem->channel_data; + GPR_ASSERT (*channel_data == 0); + + call_stack = gpr_malloc (channel_stack->call_stack_size); + grpc_call_stack_init (channel_stack, NULL, NULL, call_stack, &closure_list); + GPR_ASSERT (call_stack->count == 1); + call_elem = grpc_call_stack_element (call_stack, 0); + GPR_ASSERT (call_elem->filter == channel_elem->filter); + GPR_ASSERT (call_elem->channel_data == channel_elem->channel_data); + call_data = (int *) call_elem->call_data; + GPR_ASSERT (*call_data == 0); + GPR_ASSERT (*channel_data == 1); + + grpc_call_stack_destroy (call_stack, &closure_list); + gpr_free (call_stack); + GPR_ASSERT (*channel_data == 2); + + grpc_channel_stack_destroy (channel_stack, &closure_list); + gpr_free (channel_stack); + + grpc_mdctx_unref (metadata_context); + + GPR_ASSERT (grpc_closure_list_empty (closure_list)); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_create_channel_stack(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_create_channel_stack (); return 0; } diff --git a/test/core/client_config/lb_policies_test.c b/test/core/client_config/lb_policies_test.c index 411e96598a..931bdef017 100644 --- a/test/core/client_config/lb_policies_test.c +++ b/test/core/client_config/lb_policies_test.c @@ -50,7 +50,8 @@ #include "test/core/util/port.h" #include "test/core/end2end/cq_verifier.h" -typedef struct servers_fixture { +typedef struct servers_fixture +{ size_t num_servers; grpc_server **servers; grpc_call **server_calls; @@ -59,10 +60,10 @@ typedef struct servers_fixture { grpc_metadata_array *request_metadata_recv; } servers_fixture; -typedef void (*verifier_fn)(const servers_fixture *, grpc_channel *, - const int *, const size_t); +typedef void (*verifier_fn) (const servers_fixture *, grpc_channel *, const int *, const size_t); -typedef struct test_spec { +typedef struct test_spec +{ size_t num_iters; size_t num_servers; @@ -75,145 +76,170 @@ typedef struct test_spec { } test_spec; -static void test_spec_reset(test_spec *spec) { +static void +test_spec_reset (test_spec * spec) +{ size_t i, j; - for (i = 0; i < spec->num_iters; i++) { - for (j = 0; j < spec->num_servers; j++) { - spec->kill_at[i][j] = 0; - spec->revive_at[i][j] = 0; + for (i = 0; i < spec->num_iters; i++) + { + for (j = 0; j < spec->num_servers; j++) + { + spec->kill_at[i][j] = 0; + spec->revive_at[i][j] = 0; + } } - } } -static test_spec *test_spec_create(size_t num_iters, size_t num_servers) { +static test_spec * +test_spec_create (size_t num_iters, size_t num_servers) +{ test_spec *spec; size_t i; - spec = gpr_malloc(sizeof(test_spec)); + spec = gpr_malloc (sizeof (test_spec)); spec->num_iters = num_iters; spec->num_servers = num_servers; - spec->kill_at = gpr_malloc(sizeof(int *) * num_iters); - spec->revive_at = gpr_malloc(sizeof(int *) * num_iters); - for (i = 0; i < num_iters; i++) { - spec->kill_at[i] = gpr_malloc(sizeof(int) * num_servers); - spec->revive_at[i] = gpr_malloc(sizeof(int) * num_servers); - } - - test_spec_reset(spec); + spec->kill_at = gpr_malloc (sizeof (int *) * num_iters); + spec->revive_at = gpr_malloc (sizeof (int *) * num_iters); + for (i = 0; i < num_iters; i++) + { + spec->kill_at[i] = gpr_malloc (sizeof (int) * num_servers); + spec->revive_at[i] = gpr_malloc (sizeof (int) * num_servers); + } + + test_spec_reset (spec); return spec; } -static void test_spec_destroy(test_spec *spec) { +static void +test_spec_destroy (test_spec * spec) +{ size_t i; - for (i = 0; i < spec->num_iters; i++) { - gpr_free(spec->kill_at[i]); - gpr_free(spec->revive_at[i]); - } + for (i = 0; i < spec->num_iters; i++) + { + gpr_free (spec->kill_at[i]); + gpr_free (spec->revive_at[i]); + } - gpr_free(spec->kill_at); - gpr_free(spec->revive_at); + gpr_free (spec->kill_at); + gpr_free (spec->revive_at); - gpr_free(spec); + gpr_free (spec); } -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, n_seconds_time(5), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, n_seconds_time (5), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void kill_server(const servers_fixture *f, size_t i) { - gpr_log(GPR_INFO, "KILLING SERVER %d", i); - GPR_ASSERT(f->servers[i] != NULL); - grpc_server_shutdown_and_notify(f->servers[i], f->cq, tag(10000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(10000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->servers[i]); +static void +kill_server (const servers_fixture * f, size_t i) +{ + gpr_log (GPR_INFO, "KILLING SERVER %d", i); + GPR_ASSERT (f->servers[i] != NULL); + grpc_server_shutdown_and_notify (f->servers[i], f->cq, tag (10000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (10000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->servers[i]); f->servers[i] = NULL; } -static void revive_server(const servers_fixture *f, size_t i) { +static void +revive_server (const servers_fixture * f, size_t i) +{ int got_port; - gpr_log(GPR_INFO, "RAISE AGAIN SERVER %d", i); - GPR_ASSERT(f->servers[i] == NULL); - f->servers[i] = grpc_server_create(NULL, NULL); - grpc_server_register_completion_queue(f->servers[i], f->cq, NULL); - GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port( - f->servers[i], f->servers_hostports[i])) > 0); - grpc_server_start(f->servers[i]); + gpr_log (GPR_INFO, "RAISE AGAIN SERVER %d", i); + GPR_ASSERT (f->servers[i] == NULL); + f->servers[i] = grpc_server_create (NULL, NULL); + grpc_server_register_completion_queue (f->servers[i], f->cq, NULL); + GPR_ASSERT ((got_port = grpc_server_add_insecure_http2_port (f->servers[i], f->servers_hostports[i])) > 0); + grpc_server_start (f->servers[i]); } -static servers_fixture *setup_servers(const char *server_host, - const size_t num_servers) { - servers_fixture *f = gpr_malloc(sizeof(servers_fixture)); +static servers_fixture * +setup_servers (const char *server_host, const size_t num_servers) +{ + servers_fixture *f = gpr_malloc (sizeof (servers_fixture)); int *ports; int got_port; size_t i; f->num_servers = num_servers; - f->server_calls = gpr_malloc(sizeof(grpc_call *) * num_servers); - f->request_metadata_recv = - gpr_malloc(sizeof(grpc_metadata_array) * num_servers); + f->server_calls = gpr_malloc (sizeof (grpc_call *) * num_servers); + f->request_metadata_recv = gpr_malloc (sizeof (grpc_metadata_array) * num_servers); /* Create servers. */ - ports = gpr_malloc(sizeof(int *) * num_servers); - f->servers = gpr_malloc(sizeof(grpc_server *) * num_servers); - f->servers_hostports = gpr_malloc(sizeof(char *) * num_servers); - f->cq = grpc_completion_queue_create(NULL); - for (i = 0; i < num_servers; i++) { - ports[i] = grpc_pick_unused_port_or_die(); - - gpr_join_host_port(&f->servers_hostports[i], server_host, ports[i]); - - f->servers[i] = grpc_server_create(NULL, NULL); - grpc_server_register_completion_queue(f->servers[i], f->cq, NULL); - GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port( - f->servers[i], f->servers_hostports[i])) > 0); - GPR_ASSERT(ports[i] == got_port); - grpc_server_start(f->servers[i]); - } - gpr_free(ports); + ports = gpr_malloc (sizeof (int *) * num_servers); + f->servers = gpr_malloc (sizeof (grpc_server *) * num_servers); + f->servers_hostports = gpr_malloc (sizeof (char *) * num_servers); + f->cq = grpc_completion_queue_create (NULL); + for (i = 0; i < num_servers; i++) + { + ports[i] = grpc_pick_unused_port_or_die (); + + gpr_join_host_port (&f->servers_hostports[i], server_host, ports[i]); + + f->servers[i] = grpc_server_create (NULL, NULL); + grpc_server_register_completion_queue (f->servers[i], f->cq, NULL); + GPR_ASSERT ((got_port = grpc_server_add_insecure_http2_port (f->servers[i], f->servers_hostports[i])) > 0); + GPR_ASSERT (ports[i] == got_port); + grpc_server_start (f->servers[i]); + } + gpr_free (ports); return f; } -static void teardown_servers(servers_fixture *f) { +static void +teardown_servers (servers_fixture * f) +{ size_t i; /* Destroy server. */ - for (i = 0; i < f->num_servers; i++) { - if (f->servers[i] == NULL) continue; - grpc_server_shutdown_and_notify(f->servers[i], f->cq, tag(10000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(10000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->servers[i]); - } - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); - - gpr_free(f->servers); - - for (i = 0; i < f->num_servers; i++) { - gpr_free(f->servers_hostports[i]); - } - - gpr_free(f->servers_hostports); - gpr_free(f->request_metadata_recv); - gpr_free(f->server_calls); - gpr_free(f); + for (i = 0; i < f->num_servers; i++) + { + if (f->servers[i] == NULL) + continue; + grpc_server_shutdown_and_notify (f->servers[i], f->cq, tag (10000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (10000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->servers[i]); + } + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); + + gpr_free (f->servers); + + for (i = 0; i < f->num_servers; i++) + { + gpr_free (f->servers_hostports[i]); + } + + gpr_free (f->servers_hostports); + gpr_free (f->request_metadata_recv); + gpr_free (f->server_calls); + gpr_free (f); } /** Returns connection sequence (server indices), which must be freed */ -int *perform_request(servers_fixture *f, grpc_channel *client, - const test_spec *spec) { +int * +perform_request (servers_fixture * f, grpc_channel * client, const test_spec * spec) +{ grpc_call *c; int s_idx; int *s_valid; @@ -232,157 +258,160 @@ int *perform_request(servers_fixture *f, grpc_channel *client, grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; - s_valid = gpr_malloc(sizeof(int) * f->num_servers); - call_details = gpr_malloc(sizeof(grpc_call_details) * f->num_servers); - connection_sequence = gpr_malloc(sizeof(int) * spec->num_iters); + s_valid = gpr_malloc (sizeof (int) * f->num_servers); + call_details = gpr_malloc (sizeof (grpc_call_details) * f->num_servers); + connection_sequence = gpr_malloc (sizeof (int) * spec->num_iters); /* Send a trivial request. */ - deadline = n_seconds_time(60); - - for (iter_num = 0; iter_num < spec->num_iters; iter_num++) { - cq_verifier *cqv = cq_verifier_create(f->cq); - details = NULL; - details_capacity = 0; - was_cancelled = 2; - - for (i = 0; i < f->num_servers; i++) { - if (spec->kill_at[iter_num][i] != 0) { - kill_server(f, i); - } else if (spec->revive_at[iter_num][i] != 0) { - /* killing takes precedence */ - revive_server(f, i); - } - } - - connection_sequence[iter_num] = -1; - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); + deadline = n_seconds_time (60); + + for (iter_num = 0; iter_num < spec->num_iters; iter_num++) + { + cq_verifier *cqv = cq_verifier_create (f->cq); + details = NULL; + details_capacity = 0; + was_cancelled = 2; + + for (i = 0; i < f->num_servers; i++) + { + if (spec->kill_at[iter_num][i] != 0) + { + kill_server (f, i); + } + else if (spec->revive_at[iter_num][i] != 0) + { + /* killing takes precedence */ + revive_server (f, i); + } + } + + connection_sequence[iter_num] = -1; + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + + for (i = 0; i < f->num_servers; i++) + { + grpc_call_details_init (&call_details[i]); + } + memset (s_valid, 0, f->num_servers * sizeof (int)); + + c = grpc_channel_create_call (client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - for (i = 0; i < f->num_servers; i++) { - grpc_call_details_init(&call_details[i]); - } - memset(s_valid, 0, f->num_servers * sizeof(int)); - - c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); - - op = ops; - op->op = GRPC_OP_SEND_INITIAL_METADATA; - op->data.send_initial_metadata.count = 0; - op->flags = 0; - op->reserved = NULL; - op++; - op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; - op->flags = 0; - op->reserved = NULL; - op++; - op->op = GRPC_OP_RECV_INITIAL_METADATA; - op->data.recv_initial_metadata = &initial_metadata_recv; - op->flags = 0; - op->reserved = NULL; - op++; - op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; - op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; - op->data.recv_status_on_client.status = &status; - op->data.recv_status_on_client.status_details = &details; - op->data.recv_status_on_client.status_details_capacity = &details_capacity; - op->flags = 0; - op->reserved = NULL; - op++; - GPR_ASSERT(GRPC_CALL_OK == - grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL)); - - /* "listen" on all servers */ - for (i = 0; i < f->num_servers; i++) { - grpc_metadata_array_init(&f->request_metadata_recv[i]); - if (f->servers[i] != NULL) { - GPR_ASSERT(GRPC_CALL_OK == - grpc_server_request_call(f->servers[i], &f->server_calls[i], - &call_details[i], - &f->request_metadata_recv[i], f->cq, - f->cq, tag(1000 + (int)i))); - } - } - - s_idx = -1; - while ((ev = grpc_completion_queue_next( - f->cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), NULL)) - .type != GRPC_QUEUE_TIMEOUT) { - read_tag = ((int)(gpr_intptr)ev.tag); - gpr_log(GPR_DEBUG, "EVENT: success:%d, type:%d, tag:%d iter:%d", - ev.success, ev.type, read_tag, iter_num); - if (ev.success && read_tag >= 1000) { - GPR_ASSERT(s_idx == -1); /* only one server must reply */ - /* only server notifications for non-shutdown events */ - s_idx = read_tag - 1000; - s_valid[s_idx] = 1; - connection_sequence[iter_num] = s_idx; - } - } - - if (s_idx >= 0) { op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = 0; op->reserved = NULL; op++; - op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; - op->data.send_status_from_server.trailing_metadata_count = 0; - op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; - op->data.send_status_from_server.status_details = "xyz"; + op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; op->flags = 0; op->reserved = NULL; op++; - op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; - op->data.recv_close_on_server.cancelled = &was_cancelled; + op->op = GRPC_OP_RECV_INITIAL_METADATA; + op->data.recv_initial_metadata = &initial_metadata_recv; op->flags = 0; op->reserved = NULL; op++; - GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(f->server_calls[s_idx], - ops, (size_t)(op - ops), - tag(102), NULL)); - - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); - - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details[s_idx].method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details[s_idx].host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 1); - } - - for (i = 0; i < f->num_servers; i++) { - if (s_valid[i] != 0) { - grpc_call_destroy(f->server_calls[i]); - } - grpc_metadata_array_destroy(&f->request_metadata_recv[i]); - } - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - - cq_verifier_destroy(cqv); - - grpc_call_destroy(c); - - for (i = 0; i < f->num_servers; i++) { - grpc_call_details_destroy(&call_details[i]); + op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; + op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; + op->data.recv_status_on_client.status = &status; + op->data.recv_status_on_client.status_details = &details; + op->data.recv_status_on_client.status_details_capacity = &details_capacity; + op->flags = 0; + op->reserved = NULL; + op++; + GPR_ASSERT (GRPC_CALL_OK == grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL)); + + /* "listen" on all servers */ + for (i = 0; i < f->num_servers; i++) + { + grpc_metadata_array_init (&f->request_metadata_recv[i]); + if (f->servers[i] != NULL) + { + GPR_ASSERT (GRPC_CALL_OK == grpc_server_request_call (f->servers[i], &f->server_calls[i], &call_details[i], &f->request_metadata_recv[i], f->cq, f->cq, tag (1000 + (int) i))); + } + } + + s_idx = -1; + while ((ev = grpc_completion_queue_next (f->cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1), NULL)).type != GRPC_QUEUE_TIMEOUT) + { + read_tag = ((int) (gpr_intptr) ev.tag); + gpr_log (GPR_DEBUG, "EVENT: success:%d, type:%d, tag:%d iter:%d", ev.success, ev.type, read_tag, iter_num); + if (ev.success && read_tag >= 1000) + { + GPR_ASSERT (s_idx == -1); /* only one server must reply */ + /* only server notifications for non-shutdown events */ + s_idx = read_tag - 1000; + s_valid[s_idx] = 1; + connection_sequence[iter_num] = s_idx; + } + } + + if (s_idx >= 0) + { + op = ops; + op->op = GRPC_OP_SEND_INITIAL_METADATA; + op->data.send_initial_metadata.count = 0; + op->flags = 0; + op->reserved = NULL; + op++; + op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; + op->data.send_status_from_server.trailing_metadata_count = 0; + op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; + op->data.send_status_from_server.status_details = "xyz"; + op->flags = 0; + op->reserved = NULL; + op++; + op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; + op->data.recv_close_on_server.cancelled = &was_cancelled; + op->flags = 0; + op->reserved = NULL; + op++; + GPR_ASSERT (GRPC_CALL_OK == grpc_call_start_batch (f->server_calls[s_idx], ops, (size_t) (op - ops), tag (102), NULL)); + + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); + + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details[s_idx].method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details[s_idx].host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 1); + } + + for (i = 0; i < f->num_servers; i++) + { + if (s_valid[i] != 0) + { + grpc_call_destroy (f->server_calls[i]); + } + grpc_metadata_array_destroy (&f->request_metadata_recv[i]); + } + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + + cq_verifier_destroy (cqv); + + grpc_call_destroy (c); + + for (i = 0; i < f->num_servers; i++) + { + grpc_call_details_destroy (&call_details[i]); + } + gpr_free (details); } - gpr_free(details); - } - gpr_free(call_details); - gpr_free(s_valid); + gpr_free (call_details); + gpr_free (s_valid); return connection_sequence; } -static void assert_channel_connectivity( - grpc_channel *ch, size_t num_accepted_conn_states, - grpc_connectivity_state accepted_conn_state, ...) { +static void +assert_channel_connectivity (grpc_channel * ch, size_t num_accepted_conn_states, grpc_connectivity_state accepted_conn_state, ...) +{ size_t i; grpc_channel_stack *client_stack; grpc_channel_element *client_channel_filter; @@ -390,316 +419,306 @@ static void assert_channel_connectivity( grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; va_list ap; - client_stack = grpc_channel_get_channel_stack(ch); - client_channel_filter = grpc_channel_stack_last_element(client_stack); - - actual_conn_state = grpc_client_channel_check_connectivity_state( - client_channel_filter, 0 /* don't try to connect */, &closure_list); - grpc_closure_list_run(&closure_list); - va_start(ap, accepted_conn_state); - for (i = 0; i < num_accepted_conn_states; i++) { - if (actual_conn_state == accepted_conn_state) { - break; - } - accepted_conn_state = va_arg(ap, grpc_connectivity_state); - } - va_end(ap); - if (i == num_accepted_conn_states) { - char **accepted_strs = - gpr_malloc(sizeof(char *) * num_accepted_conn_states); - char *accepted_str_joined; - va_start(ap, accepted_conn_state); - for (i = 0; i < num_accepted_conn_states; i++) { - GPR_ASSERT(gpr_asprintf(&accepted_strs[i], "%d", accepted_conn_state) > - 0); - accepted_conn_state = va_arg(ap, grpc_connectivity_state); + client_stack = grpc_channel_get_channel_stack (ch); + client_channel_filter = grpc_channel_stack_last_element (client_stack); + + actual_conn_state = grpc_client_channel_check_connectivity_state (client_channel_filter, 0 /* don't try to connect */ , &closure_list); + grpc_closure_list_run (&closure_list); + va_start (ap, accepted_conn_state); + for (i = 0; i < num_accepted_conn_states; i++) + { + if (actual_conn_state == accepted_conn_state) + { + break; + } + accepted_conn_state = va_arg (ap, grpc_connectivity_state); } - va_end(ap); - accepted_str_joined = gpr_strjoin_sep((const char **)accepted_strs, - num_accepted_conn_states, ", ", NULL); - gpr_log( - GPR_ERROR, - "Channel connectivity assertion failed: expected <one of [%s]>, got %d", - accepted_str_joined, actual_conn_state); - - for (i = 0; i < num_accepted_conn_states; i++) { - gpr_free(accepted_strs[i]); + va_end (ap); + if (i == num_accepted_conn_states) + { + char **accepted_strs = gpr_malloc (sizeof (char *) * num_accepted_conn_states); + char *accepted_str_joined; + va_start (ap, accepted_conn_state); + for (i = 0; i < num_accepted_conn_states; i++) + { + GPR_ASSERT (gpr_asprintf (&accepted_strs[i], "%d", accepted_conn_state) > 0); + accepted_conn_state = va_arg (ap, grpc_connectivity_state); + } + va_end (ap); + accepted_str_joined = gpr_strjoin_sep ((const char **) accepted_strs, num_accepted_conn_states, ", ", NULL); + gpr_log (GPR_ERROR, "Channel connectivity assertion failed: expected <one of [%s]>, got %d", accepted_str_joined, actual_conn_state); + + for (i = 0; i < num_accepted_conn_states; i++) + { + gpr_free (accepted_strs[i]); + } + gpr_free (accepted_strs); + gpr_free (accepted_str_joined); + abort (); } - gpr_free(accepted_strs); - gpr_free(accepted_str_joined); - abort(); - } } -void run_spec(const test_spec *spec) { +void +run_spec (const test_spec * spec) +{ grpc_channel *client; char *client_hostport; char *servers_hostports_str; int *actual_connection_sequence; - servers_fixture *f = setup_servers("127.0.0.1", spec->num_servers); + servers_fixture *f = setup_servers ("127.0.0.1", spec->num_servers); /* Create client. */ - servers_hostports_str = gpr_strjoin_sep((const char **)f->servers_hostports, - f->num_servers, ",", NULL); - gpr_asprintf(&client_hostport, "ipv4:%s?lb_policy=round_robin", - servers_hostports_str); - client = grpc_insecure_channel_create(client_hostport, NULL, NULL); + servers_hostports_str = gpr_strjoin_sep ((const char **) f->servers_hostports, f->num_servers, ",", NULL); + gpr_asprintf (&client_hostport, "ipv4:%s?lb_policy=round_robin", servers_hostports_str); + client = grpc_insecure_channel_create (client_hostport, NULL, NULL); - gpr_log(GPR_INFO, "Testing '%s' with servers=%s client=%s", spec->description, - servers_hostports_str, client_hostport); + gpr_log (GPR_INFO, "Testing '%s' with servers=%s client=%s", spec->description, servers_hostports_str, client_hostport); - actual_connection_sequence = perform_request(f, client, spec); + actual_connection_sequence = perform_request (f, client, spec); - spec->verifier(f, client, actual_connection_sequence, spec->num_iters); + spec->verifier (f, client, actual_connection_sequence, spec->num_iters); - gpr_free(client_hostport); - gpr_free(servers_hostports_str); - gpr_free(actual_connection_sequence); + gpr_free (client_hostport); + gpr_free (servers_hostports_str); + gpr_free (actual_connection_sequence); - grpc_channel_destroy(client); - teardown_servers(f); + grpc_channel_destroy (client); + teardown_servers (f); } -static void print_failed_expectations(const int *expected_connection_sequence, - const int *actual_connection_sequence, - const size_t expected_seq_length, - const size_t num_iters) { +static void +print_failed_expectations (const int *expected_connection_sequence, const int *actual_connection_sequence, const size_t expected_seq_length, const size_t num_iters) +{ size_t i; - for (i = 0; i < num_iters; i++) { - gpr_log(GPR_ERROR, "FAILURE: Iter, expected, actual:%d (%d, %d)", i, - expected_connection_sequence[i % expected_seq_length], - actual_connection_sequence[i]); - } + for (i = 0; i < num_iters; i++) + { + gpr_log (GPR_ERROR, "FAILURE: Iter, expected, actual:%d (%d, %d)", i, expected_connection_sequence[i % expected_seq_length], actual_connection_sequence[i]); + } } -static void verify_vanilla_round_robin(const servers_fixture *f, - grpc_channel *client, - const int *actual_connection_sequence, - const size_t num_iters) { +static void +verify_vanilla_round_robin (const servers_fixture * f, grpc_channel * client, const int *actual_connection_sequence, const size_t num_iters) +{ int *expected_connection_sequence; size_t i; const size_t expected_seq_length = f->num_servers; /* verify conn. seq. expectation */ /* get the first sequence of "num_servers" elements */ - expected_connection_sequence = gpr_malloc(sizeof(int) * expected_seq_length); - memcpy(expected_connection_sequence, actual_connection_sequence, - sizeof(int) * expected_seq_length); - - for (i = 0; i < num_iters; i++) { - const int actual = actual_connection_sequence[i]; - const int expected = expected_connection_sequence[i % expected_seq_length]; - if (actual != expected) { - gpr_log(GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, - actual, i); - print_failed_expectations(expected_connection_sequence, - actual_connection_sequence, expected_seq_length, - num_iters); - abort(); + expected_connection_sequence = gpr_malloc (sizeof (int) * expected_seq_length); + memcpy (expected_connection_sequence, actual_connection_sequence, sizeof (int) * expected_seq_length); + + for (i = 0; i < num_iters; i++) + { + const int actual = actual_connection_sequence[i]; + const int expected = expected_connection_sequence[i % expected_seq_length]; + if (actual != expected) + { + gpr_log (GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, actual, i); + print_failed_expectations (expected_connection_sequence, actual_connection_sequence, expected_seq_length, num_iters); + abort (); + } } - } - assert_channel_connectivity(client, 1, GRPC_CHANNEL_READY); + assert_channel_connectivity (client, 1, GRPC_CHANNEL_READY); - gpr_free(expected_connection_sequence); + gpr_free (expected_connection_sequence); } /* At the start of the second iteration, all but the first and last servers (as * given in "f") are killed */ -static void verify_vanishing_floor_round_robin( - const servers_fixture *f, grpc_channel *client, - const int *actual_connection_sequence, const size_t num_iters) { +static void +verify_vanishing_floor_round_robin (const servers_fixture * f, grpc_channel * client, const int *actual_connection_sequence, const size_t num_iters) +{ int *expected_connection_sequence; const size_t expected_seq_length = 2; size_t i; /* verify conn. seq. expectation */ /* copy the first full sequence (without -1s) */ - expected_connection_sequence = gpr_malloc(sizeof(int) * expected_seq_length); - memcpy(expected_connection_sequence, actual_connection_sequence + 2, - expected_seq_length * sizeof(int)); + expected_connection_sequence = gpr_malloc (sizeof (int) * expected_seq_length); + memcpy (expected_connection_sequence, actual_connection_sequence + 2, expected_seq_length * sizeof (int)); /* first three elements of the sequence should be [<1st>, -1] */ - if (actual_connection_sequence[0] != expected_connection_sequence[0]) { - gpr_log(GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", - expected_connection_sequence[0], actual_connection_sequence[0], 0); - print_failed_expectations(expected_connection_sequence, - actual_connection_sequence, expected_seq_length, - 1u); - abort(); - } - - GPR_ASSERT(actual_connection_sequence[1] == -1); - - for (i = 2; i < num_iters; i++) { - const int actual = actual_connection_sequence[i]; - const int expected = expected_connection_sequence[i % expected_seq_length]; - if (actual != expected) { - gpr_log(GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, - actual, i); - print_failed_expectations(expected_connection_sequence, - actual_connection_sequence, expected_seq_length, - num_iters); - abort(); + if (actual_connection_sequence[0] != expected_connection_sequence[0]) + { + gpr_log (GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected_connection_sequence[0], actual_connection_sequence[0], 0); + print_failed_expectations (expected_connection_sequence, actual_connection_sequence, expected_seq_length, 1u); + abort (); + } + + GPR_ASSERT (actual_connection_sequence[1] == -1); + + for (i = 2; i < num_iters; i++) + { + const int actual = actual_connection_sequence[i]; + const int expected = expected_connection_sequence[i % expected_seq_length]; + if (actual != expected) + { + gpr_log (GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, actual, i); + print_failed_expectations (expected_connection_sequence, actual_connection_sequence, expected_seq_length, num_iters); + abort (); + } } - } - gpr_free(expected_connection_sequence); + gpr_free (expected_connection_sequence); } -static void verify_total_carnage_round_robin( - const servers_fixture *f, grpc_channel *client, - const int *actual_connection_sequence, const size_t num_iters) { +static void +verify_total_carnage_round_robin (const servers_fixture * f, grpc_channel * client, const int *actual_connection_sequence, const size_t num_iters) +{ size_t i; - for (i = 0; i < num_iters; i++) { - const int actual = actual_connection_sequence[i]; - const int expected = -1; - if (actual != expected) { - gpr_log(GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, - actual, i); - abort(); + for (i = 0; i < num_iters; i++) + { + const int actual = actual_connection_sequence[i]; + const int expected = -1; + if (actual != expected) + { + gpr_log (GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, actual, i); + abort (); + } } - } /* even though we know all the servers are dead, the client is still trying * retrying, believing it's in a transient failure situation */ - assert_channel_connectivity(client, 2, GRPC_CHANNEL_TRANSIENT_FAILURE, - GRPC_CHANNEL_CONNECTING); + assert_channel_connectivity (client, 2, GRPC_CHANNEL_TRANSIENT_FAILURE, GRPC_CHANNEL_CONNECTING); } -static void verify_partial_carnage_round_robin( - const servers_fixture *f, grpc_channel *client, - const int *actual_connection_sequence, const size_t num_iters) { +static void +verify_partial_carnage_round_robin (const servers_fixture * f, grpc_channel * client, const int *actual_connection_sequence, const size_t num_iters) +{ int *expected_connection_sequence; size_t i; const size_t expected_seq_length = f->num_servers; /* verify conn. seq. expectation */ /* get the first sequence of "num_servers" elements */ - expected_connection_sequence = gpr_malloc(sizeof(int) * expected_seq_length); - memcpy(expected_connection_sequence, actual_connection_sequence, - sizeof(int) * expected_seq_length); - - for (i = 0; i < num_iters / 2; i++) { - const int actual = actual_connection_sequence[i]; - const int expected = expected_connection_sequence[i % expected_seq_length]; - if (actual != expected) { - gpr_log(GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, - actual, i); - print_failed_expectations(expected_connection_sequence, - actual_connection_sequence, expected_seq_length, - num_iters); - abort(); + expected_connection_sequence = gpr_malloc (sizeof (int) * expected_seq_length); + memcpy (expected_connection_sequence, actual_connection_sequence, sizeof (int) * expected_seq_length); + + for (i = 0; i < num_iters / 2; i++) + { + const int actual = actual_connection_sequence[i]; + const int expected = expected_connection_sequence[i % expected_seq_length]; + if (actual != expected) + { + gpr_log (GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, actual, i); + print_failed_expectations (expected_connection_sequence, actual_connection_sequence, expected_seq_length, num_iters); + abort (); + } } - } /* second half of the iterations go without response */ - for (; i < num_iters; i++) { - GPR_ASSERT(actual_connection_sequence[i] == -1); - } + for (; i < num_iters; i++) + { + GPR_ASSERT (actual_connection_sequence[i] == -1); + } /* even though we know all the servers are dead, the client is still trying * retrying, believing it's in a transient failure situation */ - assert_channel_connectivity(client, 2, GRPC_CHANNEL_TRANSIENT_FAILURE, - GRPC_CHANNEL_CONNECTING); - gpr_free(expected_connection_sequence); + assert_channel_connectivity (client, 2, GRPC_CHANNEL_TRANSIENT_FAILURE, GRPC_CHANNEL_CONNECTING); + gpr_free (expected_connection_sequence); } -static void verify_rebirth_round_robin(const servers_fixture *f, - grpc_channel *client, - const int *actual_connection_sequence, - const size_t num_iters) { +static void +verify_rebirth_round_robin (const servers_fixture * f, grpc_channel * client, const int *actual_connection_sequence, const size_t num_iters) +{ int *expected_connection_sequence; size_t i; const size_t expected_seq_length = f->num_servers; /* verify conn. seq. expectation */ /* get the first sequence of "num_servers" elements */ - expected_connection_sequence = gpr_malloc(sizeof(int) * expected_seq_length); - memcpy(expected_connection_sequence, actual_connection_sequence + 4, - sizeof(int) * expected_seq_length); + expected_connection_sequence = gpr_malloc (sizeof (int) * expected_seq_length); + memcpy (expected_connection_sequence, actual_connection_sequence + 4, sizeof (int) * expected_seq_length); /* first iteration succeeds */ - GPR_ASSERT(actual_connection_sequence[0] != -1); + GPR_ASSERT (actual_connection_sequence[0] != -1); /* back up on the third (or maybe fourth) iteration */ i = 3; - if (actual_connection_sequence[i] == -1) { - i = 4; - } - for (; i < num_iters; i++) { - const int actual = actual_connection_sequence[i]; - const int expected = expected_connection_sequence[i % expected_seq_length]; - if (actual != expected) { - gpr_log(GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, - actual, i); - print_failed_expectations(expected_connection_sequence, - actual_connection_sequence, expected_seq_length, - num_iters); - abort(); + if (actual_connection_sequence[i] == -1) + { + i = 4; + } + for (; i < num_iters; i++) + { + const int actual = actual_connection_sequence[i]; + const int expected = expected_connection_sequence[i % expected_seq_length]; + if (actual != expected) + { + gpr_log (GPR_ERROR, "FAILURE: expected %d, actual %d at iter %d", expected, actual, i); + print_failed_expectations (expected_connection_sequence, actual_connection_sequence, expected_seq_length, num_iters); + abort (); + } } - } /* things are fine once the servers are brought back up */ - assert_channel_connectivity(client, 1, GRPC_CHANNEL_READY); - gpr_free(expected_connection_sequence); + assert_channel_connectivity (client, 1, GRPC_CHANNEL_READY); + gpr_free (expected_connection_sequence); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ test_spec *spec; size_t i; const size_t NUM_ITERS = 10; const size_t NUM_SERVERS = 4; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); /* everything is fine, all servers stay up the whole time and life's peachy */ - spec = test_spec_create(NUM_ITERS, NUM_SERVERS); + spec = test_spec_create (NUM_ITERS, NUM_SERVERS); spec->verifier = verify_vanilla_round_robin; spec->description = "test_all_server_up"; - run_spec(spec); + run_spec (spec); /* Kill all servers first thing in the morning */ - test_spec_reset(spec); + test_spec_reset (spec); spec->verifier = verify_total_carnage_round_robin; spec->description = "test_kill_all_server"; - for (i = 0; i < NUM_SERVERS; i++) { - spec->kill_at[0][i] = 1; - } - run_spec(spec); + for (i = 0; i < NUM_SERVERS; i++) + { + spec->kill_at[0][i] = 1; + } + run_spec (spec); /* at the start of the 2nd iteration, kill all but the first and last servers. * This should knock down the server bound to be selected next */ - test_spec_reset(spec); + test_spec_reset (spec); spec->verifier = verify_vanishing_floor_round_robin; spec->description = "test_kill_all_server_at_2nd_iteration"; - for (i = 1; i < NUM_SERVERS - 1; i++) { - spec->kill_at[1][i] = 1; - } - run_spec(spec); + for (i = 1; i < NUM_SERVERS - 1; i++) + { + spec->kill_at[1][i] = 1; + } + run_spec (spec); /* Midway, kill all servers. */ - test_spec_reset(spec); + test_spec_reset (spec); spec->verifier = verify_partial_carnage_round_robin; spec->description = "test_kill_all_server_midway"; - for (i = 0; i < NUM_SERVERS; i++) { - spec->kill_at[spec->num_iters / 2][i] = 1; - } - run_spec(spec); + for (i = 0; i < NUM_SERVERS; i++) + { + spec->kill_at[spec->num_iters / 2][i] = 1; + } + run_spec (spec); /* After first iteration, kill all servers. On the third one, bring them all * back up. */ - test_spec_reset(spec); + test_spec_reset (spec); spec->verifier = verify_rebirth_round_robin; spec->description = "test_kill_all_server_after_1st_resurrect_at_3rd"; - for (i = 0; i < NUM_SERVERS; i++) { - spec->kill_at[1][i] = 1; - spec->revive_at[3][i] = 1; - } - run_spec(spec); + for (i = 0; i < NUM_SERVERS; i++) + { + spec->kill_at[1][i] = 1; + spec->revive_at[3][i] = 1; + } + run_spec (spec); - test_spec_destroy(spec); + test_spec_destroy (spec); - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/client_config/uri_parser_test.c b/test/core/client_config/uri_parser_test.c index df12d6b4cb..3f91a3242e 100644 --- a/test/core/client_config/uri_parser_test.c +++ b/test/core/client_config/uri_parser_test.c @@ -39,48 +39,48 @@ #include "test/core/util/test_config.h" -static void test_succeeds(const char *uri_text, const char *scheme, - const char *authority, const char *path, - const char *query, const char *fragment) { - grpc_uri *uri = grpc_uri_parse(uri_text, 0); - GPR_ASSERT(uri); - GPR_ASSERT(0 == strcmp(scheme, uri->scheme)); - GPR_ASSERT(0 == strcmp(authority, uri->authority)); - GPR_ASSERT(0 == strcmp(path, uri->path)); - GPR_ASSERT(0 == strcmp(query, uri->query)); - GPR_ASSERT(0 == strcmp(fragment, uri->fragment)); - grpc_uri_destroy(uri); +static void +test_succeeds (const char *uri_text, const char *scheme, const char *authority, const char *path, const char *query, const char *fragment) +{ + grpc_uri *uri = grpc_uri_parse (uri_text, 0); + GPR_ASSERT (uri); + GPR_ASSERT (0 == strcmp (scheme, uri->scheme)); + GPR_ASSERT (0 == strcmp (authority, uri->authority)); + GPR_ASSERT (0 == strcmp (path, uri->path)); + GPR_ASSERT (0 == strcmp (query, uri->query)); + GPR_ASSERT (0 == strcmp (fragment, uri->fragment)); + grpc_uri_destroy (uri); } -static void test_fails(const char *uri_text) { - GPR_ASSERT(NULL == grpc_uri_parse(uri_text, 0)); +static void +test_fails (const char *uri_text) +{ + GPR_ASSERT (NULL == grpc_uri_parse (uri_text, 0)); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_succeeds("http://www.google.com", "http", "www.google.com", "", "", ""); - test_succeeds("dns:///foo", "dns", "", "/foo", "", ""); - test_succeeds("http://www.google.com:90", "http", "www.google.com:90", "", "", - ""); - test_succeeds("a192.4-df:foo.coom", "a192.4-df", "", "foo.coom", "", ""); - test_succeeds("a+b:foo.coom", "a+b", "", "foo.coom", "", ""); - test_succeeds("zookeeper://127.0.0.1:2181/foo/bar", "zookeeper", - "127.0.0.1:2181", "/foo/bar", "", ""); - test_succeeds("http://www.google.com?yay-i'm-using-queries", "http", - "www.google.com", "", "yay-i'm-using-queries", ""); - test_succeeds("dns:foo.com#fragment-all-the-things", "dns", "", "foo.com", "", - "fragment-all-the-things"); - test_succeeds("http:?legit", "http", "", "", "legit", ""); - test_succeeds("unix:#this-is-ok-too", "unix", "", "", "", "this-is-ok-too"); - test_succeeds("http:?legit#twice", "http", "", "", "legit", "twice"); - test_succeeds("http://foo?bar#lol?", "http", "foo", "", "bar", "lol?"); - test_succeeds("http://foo?bar#lol?/", "http", "foo", "", "bar", "lol?/"); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_succeeds ("http://www.google.com", "http", "www.google.com", "", "", ""); + test_succeeds ("dns:///foo", "dns", "", "/foo", "", ""); + test_succeeds ("http://www.google.com:90", "http", "www.google.com:90", "", "", ""); + test_succeeds ("a192.4-df:foo.coom", "a192.4-df", "", "foo.coom", "", ""); + test_succeeds ("a+b:foo.coom", "a+b", "", "foo.coom", "", ""); + test_succeeds ("zookeeper://127.0.0.1:2181/foo/bar", "zookeeper", "127.0.0.1:2181", "/foo/bar", "", ""); + test_succeeds ("http://www.google.com?yay-i'm-using-queries", "http", "www.google.com", "", "yay-i'm-using-queries", ""); + test_succeeds ("dns:foo.com#fragment-all-the-things", "dns", "", "foo.com", "", "fragment-all-the-things"); + test_succeeds ("http:?legit", "http", "", "", "legit", ""); + test_succeeds ("unix:#this-is-ok-too", "unix", "", "", "", "this-is-ok-too"); + test_succeeds ("http:?legit#twice", "http", "", "", "legit", "twice"); + test_succeeds ("http://foo?bar#lol?", "http", "foo", "", "bar", "lol?"); + test_succeeds ("http://foo?bar#lol?/", "http", "foo", "", "bar", "lol?/"); - test_fails("xyz"); - test_fails("http:?dangling-pct-%0"); - test_fails("http://foo?[bar]"); - test_fails("http://foo?x[bar]"); - test_fails("http://foo?bar#lol#"); + test_fails ("xyz"); + test_fails ("http:?dangling-pct-%0"); + test_fails ("http://foo?[bar]"); + test_fails ("http://foo?x[bar]"); + test_fails ("http://foo?bar#lol#"); return 0; } diff --git a/test/core/compression/compression_test.c b/test/core/compression/compression_test.c index 4df0acae37..40b115efb6 100644 --- a/test/core/compression/compression_test.c +++ b/test/core/compression/compression_test.c @@ -40,38 +40,43 @@ #include "test/core/util/test_config.h" -static void test_compression_algorithm_parse(void) { +static void +test_compression_algorithm_parse (void) +{ size_t i; - const char* valid_names[] = {"identity", "gzip", "deflate"}; + const char *valid_names[] = { "identity", "gzip", "deflate" }; const grpc_compression_algorithm valid_algorithms[] = { - GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE}; - const char* invalid_names[] = {"gzip2", "foo", "", "2gzip"}; + GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE + }; + const char *invalid_names[] = { "gzip2", "foo", "", "2gzip" }; - gpr_log(GPR_DEBUG, "test_compression_algorithm_parse"); + gpr_log (GPR_DEBUG, "test_compression_algorithm_parse"); - for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) { - const char* valid_name = valid_names[i]; - grpc_compression_algorithm algorithm; - int success; - success = grpc_compression_algorithm_parse(valid_name, strlen(valid_name), - &algorithm); - GPR_ASSERT(success != 0); - GPR_ASSERT(algorithm == valid_algorithms[i]); - } + for (i = 0; i < GPR_ARRAY_SIZE (valid_names); i++) + { + const char *valid_name = valid_names[i]; + grpc_compression_algorithm algorithm; + int success; + success = grpc_compression_algorithm_parse (valid_name, strlen (valid_name), &algorithm); + GPR_ASSERT (success != 0); + GPR_ASSERT (algorithm == valid_algorithms[i]); + } - for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) { - const char* invalid_name = invalid_names[i]; - grpc_compression_algorithm algorithm; - int success; - success = grpc_compression_algorithm_parse( - invalid_name, strlen(invalid_name), &algorithm); - GPR_ASSERT(success == 0); - /* the value of "algorithm" is undefined upon failure */ - } + for (i = 0; i < GPR_ARRAY_SIZE (invalid_names); i++) + { + const char *invalid_name = invalid_names[i]; + grpc_compression_algorithm algorithm; + int success; + success = grpc_compression_algorithm_parse (invalid_name, strlen (invalid_name), &algorithm); + GPR_ASSERT (success == 0); + /* the value of "algorithm" is undefined upon failure */ + } } -int main(int argc, char** argv) { - test_compression_algorithm_parse(); +int +main (int argc, char **argv) +{ + test_compression_algorithm_parse (); return 0; } diff --git a/test/core/compression/message_compress_test.c b/test/core/compression/message_compress_test.c index 98da6a1eaa..43978b97f4 100644 --- a/test/core/compression/message_compress_test.c +++ b/test/core/compression/message_compress_test.c @@ -42,19 +42,19 @@ #include <grpc/support/useful.h> #include "test/core/util/slice_splitter.h" -typedef enum { ONE_A = 0, ONE_KB_A, ONE_MB_A, TEST_VALUE_COUNT } test_value; +typedef enum +{ ONE_A = 0, ONE_KB_A, ONE_MB_A, TEST_VALUE_COUNT } test_value; -typedef enum { +typedef enum +{ SHOULD_NOT_COMPRESS, SHOULD_COMPRESS, MAYBE_COMPRESSES } compressability; -static void assert_passthrough(gpr_slice value, - grpc_compression_algorithm algorithm, - grpc_slice_split_mode uncompressed_split_mode, - grpc_slice_split_mode compressed_split_mode, - compressability compress_result_check) { +static void +assert_passthrough (gpr_slice value, grpc_compression_algorithm algorithm, grpc_slice_split_mode uncompressed_split_mode, grpc_slice_split_mode compressed_split_mode, compressability compress_result_check) +{ gpr_slice_buffer input; gpr_slice_buffer compressed_raw; gpr_slice_buffer compressed; @@ -63,132 +63,145 @@ static void assert_passthrough(gpr_slice value, int was_compressed; char *algorithm_name; - GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algorithm_name) != 0); - gpr_log(GPR_INFO, - "assert_passthrough: value_length=%d value_hash=0x%08x " - "algorithm='%s' uncompressed_split='%s' compressed_split='%s'", - GPR_SLICE_LENGTH(value), gpr_murmur_hash3(GPR_SLICE_START_PTR(value), - GPR_SLICE_LENGTH(value), 0), - algorithm_name, grpc_slice_split_mode_name(uncompressed_split_mode), - grpc_slice_split_mode_name(compressed_split_mode)); + GPR_ASSERT (grpc_compression_algorithm_name (algorithm, &algorithm_name) != 0); + gpr_log (GPR_INFO, "assert_passthrough: value_length=%d value_hash=0x%08x " "algorithm='%s' uncompressed_split='%s' compressed_split='%s'", GPR_SLICE_LENGTH (value), gpr_murmur_hash3 (GPR_SLICE_START_PTR (value), GPR_SLICE_LENGTH (value), 0), algorithm_name, grpc_slice_split_mode_name (uncompressed_split_mode), grpc_slice_split_mode_name (compressed_split_mode)); - gpr_slice_buffer_init(&input); - gpr_slice_buffer_init(&compressed_raw); - gpr_slice_buffer_init(&compressed); - gpr_slice_buffer_init(&output); + gpr_slice_buffer_init (&input); + gpr_slice_buffer_init (&compressed_raw); + gpr_slice_buffer_init (&compressed); + gpr_slice_buffer_init (&output); - grpc_split_slices_to_buffer(uncompressed_split_mode, &value, 1, &input); + grpc_split_slices_to_buffer (uncompressed_split_mode, &value, 1, &input); - was_compressed = grpc_msg_compress(algorithm, &input, &compressed_raw); - GPR_ASSERT(input.count > 0); + was_compressed = grpc_msg_compress (algorithm, &input, &compressed_raw); + GPR_ASSERT (input.count > 0); - switch (compress_result_check) { + switch (compress_result_check) + { case SHOULD_NOT_COMPRESS: - GPR_ASSERT(was_compressed == 0); + GPR_ASSERT (was_compressed == 0); break; case SHOULD_COMPRESS: - GPR_ASSERT(was_compressed == 1); + GPR_ASSERT (was_compressed == 1); break; case MAYBE_COMPRESSES: /* no check */ break; - } + } - grpc_split_slice_buffer(compressed_split_mode, &compressed_raw, &compressed); + grpc_split_slice_buffer (compressed_split_mode, &compressed_raw, &compressed); - GPR_ASSERT(grpc_msg_decompress( - was_compressed ? algorithm : GRPC_COMPRESS_NONE, &compressed, &output)); + GPR_ASSERT (grpc_msg_decompress (was_compressed ? algorithm : GRPC_COMPRESS_NONE, &compressed, &output)); - final = grpc_slice_merge(output.slices, output.count); - GPR_ASSERT(0 == gpr_slice_cmp(value, final)); + final = grpc_slice_merge (output.slices, output.count); + GPR_ASSERT (0 == gpr_slice_cmp (value, final)); - gpr_slice_buffer_destroy(&input); - gpr_slice_buffer_destroy(&compressed); - gpr_slice_buffer_destroy(&compressed_raw); - gpr_slice_buffer_destroy(&output); - gpr_slice_unref(final); + gpr_slice_buffer_destroy (&input); + gpr_slice_buffer_destroy (&compressed); + gpr_slice_buffer_destroy (&compressed_raw); + gpr_slice_buffer_destroy (&output); + gpr_slice_unref (final); } -static gpr_slice repeated(char c, size_t length) { - gpr_slice out = gpr_slice_malloc(length); - memset(GPR_SLICE_START_PTR(out), c, length); +static gpr_slice +repeated (char c, size_t length) +{ + gpr_slice out = gpr_slice_malloc (length); + memset (GPR_SLICE_START_PTR (out), c, length); return out; } -static compressability get_compressability( - test_value id, grpc_compression_algorithm algorithm) { - if (algorithm == GRPC_COMPRESS_NONE) return SHOULD_NOT_COMPRESS; - switch (id) { +static compressability +get_compressability (test_value id, grpc_compression_algorithm algorithm) +{ + if (algorithm == GRPC_COMPRESS_NONE) + return SHOULD_NOT_COMPRESS; + switch (id) + { case ONE_A: return SHOULD_NOT_COMPRESS; case ONE_KB_A: case ONE_MB_A: return SHOULD_COMPRESS; case TEST_VALUE_COUNT: - abort(); + abort (); break; - } + } return MAYBE_COMPRESSES; } -static gpr_slice create_test_value(test_value id) { - switch (id) { +static gpr_slice +create_test_value (test_value id) +{ + switch (id) + { case ONE_A: - return gpr_slice_from_copied_string("a"); + return gpr_slice_from_copied_string ("a"); case ONE_KB_A: - return repeated('a', 1024); + return repeated ('a', 1024); case ONE_MB_A: - return repeated('a', 1024 * 1024); + return repeated ('a', 1024 * 1024); case TEST_VALUE_COUNT: - abort(); + abort (); break; - } - return gpr_slice_from_copied_string("bad value"); + } + return gpr_slice_from_copied_string ("bad value"); } -static void test_bad_data(void) { +static void +test_bad_data (void) +{ gpr_slice_buffer input; gpr_slice_buffer output; grpc_compression_algorithm i; - gpr_slice_buffer_init(&input); - gpr_slice_buffer_init(&output); - gpr_slice_buffer_add(&input, gpr_slice_from_copied_string( - "this is not valid compressed input")); + gpr_slice_buffer_init (&input); + gpr_slice_buffer_init (&output); + gpr_slice_buffer_add (&input, gpr_slice_from_copied_string ("this is not valid compressed input")); - for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { - if (i == GRPC_COMPRESS_NONE) continue; - GPR_ASSERT(0 == grpc_msg_decompress(i, &input, &output)); - GPR_ASSERT(0 == output.count); - } + for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) + { + if (i == GRPC_COMPRESS_NONE) + continue; + GPR_ASSERT (0 == grpc_msg_decompress (i, &input, &output)); + GPR_ASSERT (0 == output.count); + } - gpr_slice_buffer_destroy(&input); - gpr_slice_buffer_destroy(&output); + gpr_slice_buffer_destroy (&input); + gpr_slice_buffer_destroy (&output); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ unsigned i, j, k, m; grpc_slice_split_mode uncompressed_split_modes[] = { - GRPC_SLICE_SPLIT_IDENTITY, GRPC_SLICE_SPLIT_ONE_BYTE}; - grpc_slice_split_mode compressed_split_modes[] = {GRPC_SLICE_SPLIT_MERGE_ALL, - GRPC_SLICE_SPLIT_IDENTITY, - GRPC_SLICE_SPLIT_ONE_BYTE}; - - grpc_test_init(argc, argv); - - for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { - for (j = 0; j < GPR_ARRAY_SIZE(uncompressed_split_modes); j++) { - for (k = 0; k < GPR_ARRAY_SIZE(compressed_split_modes); k++) { - for (m = 0; m < TEST_VALUE_COUNT; m++) { - gpr_slice slice = create_test_value(m); - assert_passthrough(slice, i, j, k, get_compressability(m, i)); - gpr_slice_unref(slice); - } - } + GRPC_SLICE_SPLIT_IDENTITY, GRPC_SLICE_SPLIT_ONE_BYTE + }; + grpc_slice_split_mode compressed_split_modes[] = { GRPC_SLICE_SPLIT_MERGE_ALL, + GRPC_SLICE_SPLIT_IDENTITY, + GRPC_SLICE_SPLIT_ONE_BYTE + }; + + grpc_test_init (argc, argv); + + for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) + { + for (j = 0; j < GPR_ARRAY_SIZE (uncompressed_split_modes); j++) + { + for (k = 0; k < GPR_ARRAY_SIZE (compressed_split_modes); k++) + { + for (m = 0; m < TEST_VALUE_COUNT; m++) + { + gpr_slice slice = create_test_value (m); + assert_passthrough (slice, i, j, k, get_compressability (m, i)); + gpr_slice_unref (slice); + } + } + } } - } - test_bad_data(); + test_bad_data (); return 0; } diff --git a/test/core/end2end/cq_verifier.c b/test/core/end2end/cq_verifier.c index 922de268f4..3feb3977b3 100644 --- a/test/core/end2end/cq_verifier.c +++ b/test/core/end2end/cq_verifier.c @@ -50,7 +50,8 @@ #define ROOT_EXPECTATION 1000 /* a set of metadata we expect to find on an event */ -typedef struct metadata { +typedef struct metadata +{ size_t count; size_t cap; char **keys; @@ -59,7 +60,8 @@ typedef struct metadata { /* details what we expect to find on a single event - and forms a linked list to detail other expectations */ -typedef struct expectation { +typedef struct expectation +{ struct expectation *next; struct expectation *prev; grpc_completion_type type; @@ -68,15 +70,18 @@ typedef struct expectation { } expectation; /* the verifier itself */ -struct cq_verifier { +struct cq_verifier +{ /* bound completion queue */ grpc_completion_queue *cq; /* the root/sentinal expectation */ expectation expect; }; -cq_verifier *cq_verifier_create(grpc_completion_queue *cq) { - cq_verifier *v = gpr_malloc(sizeof(cq_verifier)); +cq_verifier * +cq_verifier_create (grpc_completion_queue * cq) +{ + cq_verifier *v = gpr_malloc (sizeof (cq_verifier)); v->expect.type = ROOT_EXPECTATION; v->expect.tag = NULL; v->expect.next = &v->expect; @@ -85,197 +90,231 @@ cq_verifier *cq_verifier_create(grpc_completion_queue *cq) { return v; } -void cq_verifier_destroy(cq_verifier *v) { - cq_verify(v); - gpr_free(v); +void +cq_verifier_destroy (cq_verifier * v) +{ + cq_verify (v); + gpr_free (v); } -static int has_metadata(const grpc_metadata *md, size_t count, const char *key, - const char *value) { +static int +has_metadata (const grpc_metadata * md, size_t count, const char *key, const char *value) +{ size_t i; - for (i = 0; i < count; i++) { - if (0 == strcmp(key, md[i].key) && strlen(value) == md[i].value_length && - 0 == memcmp(md[i].value, value, md[i].value_length)) { - return 1; + for (i = 0; i < count; i++) + { + if (0 == strcmp (key, md[i].key) && strlen (value) == md[i].value_length && 0 == memcmp (md[i].value, value, md[i].value_length)) + { + return 1; + } } - } return 0; } -int contains_metadata(grpc_metadata_array *array, const char *key, - const char *value) { - return has_metadata(array->metadata, array->count, key, value); +int +contains_metadata (grpc_metadata_array * array, const char *key, const char *value) +{ + return has_metadata (array->metadata, array->count, key, value); } -static gpr_slice merge_slices(gpr_slice *slices, size_t nslices) { +static gpr_slice +merge_slices (gpr_slice * slices, size_t nslices) +{ size_t i; size_t len = 0; gpr_uint8 *cursor; gpr_slice out; - for (i = 0; i < nslices; i++) { - len += GPR_SLICE_LENGTH(slices[i]); - } + for (i = 0; i < nslices; i++) + { + len += GPR_SLICE_LENGTH (slices[i]); + } - out = gpr_slice_malloc(len); - cursor = GPR_SLICE_START_PTR(out); + out = gpr_slice_malloc (len); + cursor = GPR_SLICE_START_PTR (out); - for (i = 0; i < nslices; i++) { - memcpy(cursor, GPR_SLICE_START_PTR(slices[i]), GPR_SLICE_LENGTH(slices[i])); - cursor += GPR_SLICE_LENGTH(slices[i]); - } + for (i = 0; i < nslices; i++) + { + memcpy (cursor, GPR_SLICE_START_PTR (slices[i]), GPR_SLICE_LENGTH (slices[i])); + cursor += GPR_SLICE_LENGTH (slices[i]); + } return out; } -static int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { +static int +byte_buffer_eq_slice (grpc_byte_buffer * bb, gpr_slice b) +{ gpr_slice a; int ok; - if (!bb) return 0; + if (!bb) + return 0; - a = merge_slices(bb->data.raw.slice_buffer.slices, - bb->data.raw.slice_buffer.count); - ok = GPR_SLICE_LENGTH(a) == GPR_SLICE_LENGTH(b) && - 0 == memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b), - GPR_SLICE_LENGTH(a)); - gpr_slice_unref(a); - gpr_slice_unref(b); + a = merge_slices (bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count); + ok = GPR_SLICE_LENGTH (a) == GPR_SLICE_LENGTH (b) && 0 == memcmp (GPR_SLICE_START_PTR (a), GPR_SLICE_START_PTR (b), GPR_SLICE_LENGTH (a)); + gpr_slice_unref (a); + gpr_slice_unref (b); return ok; } -int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str) { +int +byte_buffer_eq_string (grpc_byte_buffer * bb, const char *str) +{ grpc_byte_buffer_reader reader; grpc_byte_buffer *rbb; int res; - grpc_byte_buffer_reader_init(&reader, bb); - rbb = grpc_raw_byte_buffer_from_reader(&reader); - res = byte_buffer_eq_slice(rbb, gpr_slice_from_copied_string(str)); - grpc_byte_buffer_reader_destroy(&reader); - grpc_byte_buffer_destroy(rbb); + grpc_byte_buffer_reader_init (&reader, bb); + rbb = grpc_raw_byte_buffer_from_reader (&reader); + res = byte_buffer_eq_slice (rbb, gpr_slice_from_copied_string (str)); + grpc_byte_buffer_reader_destroy (&reader); + grpc_byte_buffer_destroy (rbb); return res; } -static void verify_matches(expectation *e, grpc_event *ev) { - GPR_ASSERT(e->type == ev->type); - switch (e->type) { +static void +verify_matches (expectation * e, grpc_event * ev) +{ + GPR_ASSERT (e->type == ev->type); + switch (e->type) + { case GRPC_QUEUE_SHUTDOWN: - gpr_log(GPR_ERROR, "premature queue shutdown"); - abort(); + gpr_log (GPR_ERROR, "premature queue shutdown"); + abort (); break; case GRPC_OP_COMPLETE: - GPR_ASSERT(e->success == ev->success); + GPR_ASSERT (e->success == ev->success); break; case GRPC_QUEUE_TIMEOUT: - gpr_log(GPR_ERROR, "not implemented"); - abort(); + gpr_log (GPR_ERROR, "not implemented"); + abort (); break; - } + } } -static void expectation_to_strvec(gpr_strvec *buf, expectation *e) { +static void +expectation_to_strvec (gpr_strvec * buf, expectation * e) +{ char *tmp; - gpr_asprintf(&tmp, "%p ", e->tag); - gpr_strvec_add(buf, tmp); + gpr_asprintf (&tmp, "%p ", e->tag); + gpr_strvec_add (buf, tmp); - switch (e->type) { + switch (e->type) + { case GRPC_OP_COMPLETE: - gpr_asprintf(&tmp, "GRPC_OP_COMPLETE result=%d", e->success); - gpr_strvec_add(buf, tmp); + gpr_asprintf (&tmp, "GRPC_OP_COMPLETE result=%d", e->success); + gpr_strvec_add (buf, tmp); break; case GRPC_QUEUE_TIMEOUT: case GRPC_QUEUE_SHUTDOWN: - gpr_log(GPR_ERROR, "not implemented"); - abort(); + gpr_log (GPR_ERROR, "not implemented"); + abort (); break; - } + } } -static void expectations_to_strvec(gpr_strvec *buf, cq_verifier *v) { +static void +expectations_to_strvec (gpr_strvec * buf, cq_verifier * v) +{ expectation *e; - for (e = v->expect.next; e != &v->expect; e = e->next) { - expectation_to_strvec(buf, e); - gpr_strvec_add(buf, gpr_strdup("\n")); - } + for (e = v->expect.next; e != &v->expect; e = e->next) + { + expectation_to_strvec (buf, e); + gpr_strvec_add (buf, gpr_strdup ("\n")); + } } -static void fail_no_event_received(cq_verifier *v) { +static void +fail_no_event_received (cq_verifier * v) +{ gpr_strvec buf; char *msg; - gpr_strvec_init(&buf); - gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n")); - expectations_to_strvec(&buf, v); - msg = gpr_strvec_flatten(&buf, NULL); - gpr_log(GPR_ERROR, "%s", msg); - gpr_strvec_destroy(&buf); - gpr_free(msg); - abort(); + gpr_strvec_init (&buf); + gpr_strvec_add (&buf, gpr_strdup ("no event received, but expected:\n")); + expectations_to_strvec (&buf, v); + msg = gpr_strvec_flatten (&buf, NULL); + gpr_log (GPR_ERROR, "%s", msg); + gpr_strvec_destroy (&buf); + gpr_free (msg); + abort (); } -void cq_verify(cq_verifier *v) { - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); +void +cq_verify (cq_verifier * v) +{ + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10); grpc_event ev; expectation *e; char *s; gpr_strvec have_tags; - gpr_strvec_init(&have_tags); - - while (v->expect.next != &v->expect) { - ev = grpc_completion_queue_next(v->cq, deadline, NULL); - if (ev.type == GRPC_QUEUE_TIMEOUT) { - fail_no_event_received(v); - break; + gpr_strvec_init (&have_tags); + + while (v->expect.next != &v->expect) + { + ev = grpc_completion_queue_next (v->cq, deadline, NULL); + if (ev.type == GRPC_QUEUE_TIMEOUT) + { + fail_no_event_received (v); + break; + } + + for (e = v->expect.next; e != &v->expect; e = e->next) + { + gpr_asprintf (&s, " %p", e->tag); + gpr_strvec_add (&have_tags, s); + if (e->tag == ev.tag) + { + verify_matches (e, &ev); + e->next->prev = e->prev; + e->prev->next = e->next; + gpr_free (e); + break; + } + } + if (e == &v->expect) + { + s = grpc_event_string (&ev); + gpr_log (GPR_ERROR, "event not found: %s", s); + gpr_free (s); + s = gpr_strvec_flatten (&have_tags, NULL); + gpr_log (GPR_ERROR, "have tags:%s", s); + gpr_free (s); + gpr_strvec_destroy (&have_tags); + abort (); + } } - for (e = v->expect.next; e != &v->expect; e = e->next) { - gpr_asprintf(&s, " %p", e->tag); - gpr_strvec_add(&have_tags, s); - if (e->tag == ev.tag) { - verify_matches(e, &ev); - e->next->prev = e->prev; - e->prev->next = e->next; - gpr_free(e); - break; - } - } - if (e == &v->expect) { - s = grpc_event_string(&ev); - gpr_log(GPR_ERROR, "event not found: %s", s); - gpr_free(s); - s = gpr_strvec_flatten(&have_tags, NULL); - gpr_log(GPR_ERROR, "have tags:%s", s); - gpr_free(s); - gpr_strvec_destroy(&have_tags); - abort(); - } - } - - gpr_strvec_destroy(&have_tags); + gpr_strvec_destroy (&have_tags); } -void cq_verify_empty(cq_verifier *v) { - gpr_timespec deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(1, GPR_TIMESPAN)); +void +cq_verify_empty (cq_verifier * v) +{ + gpr_timespec deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), + gpr_time_from_seconds (1, GPR_TIMESPAN)); grpc_event ev; - GPR_ASSERT(v->expect.next == &v->expect && "expectation queue must be empty"); + GPR_ASSERT (v->expect.next == &v->expect && "expectation queue must be empty"); - ev = grpc_completion_queue_next(v->cq, deadline, NULL); - if (ev.type != GRPC_QUEUE_TIMEOUT) { - char *s = grpc_event_string(&ev); - gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s", s); - gpr_free(s); - abort(); - } + ev = grpc_completion_queue_next (v->cq, deadline, NULL); + if (ev.type != GRPC_QUEUE_TIMEOUT) + { + char *s = grpc_event_string (&ev); + gpr_log (GPR_ERROR, "unexpected event (expected nothing): %s", s); + gpr_free (s); + abort (); + } } -static expectation *add(cq_verifier *v, grpc_completion_type type, void *tag) { - expectation *e = gpr_malloc(sizeof(expectation)); +static expectation * +add (cq_verifier * v, grpc_completion_type type, void *tag) +{ + expectation *e = gpr_malloc (sizeof (expectation)); e->type = type; e->tag = tag; e->next = &v->expect; @@ -284,6 +323,8 @@ static expectation *add(cq_verifier *v, grpc_completion_type type, void *tag) { return e; } -void cq_expect_completion(cq_verifier *v, void *tag, int success) { - add(v, GRPC_OP_COMPLETE, tag)->success = success; +void +cq_expect_completion (cq_verifier * v, void *tag, int success) +{ + add (v, GRPC_OP_COMPLETE, tag)->success = success; } diff --git a/test/core/end2end/cq_verifier.h b/test/core/end2end/cq_verifier.h index b3e07c45a5..4447acb3ee 100644 --- a/test/core/end2end/cq_verifier.h +++ b/test/core/end2end/cq_verifier.h @@ -43,24 +43,23 @@ typedef struct cq_verifier cq_verifier; /* construct/destroy a cq_verifier */ -cq_verifier *cq_verifier_create(grpc_completion_queue *cq); -void cq_verifier_destroy(cq_verifier *v); +cq_verifier *cq_verifier_create (grpc_completion_queue * cq); +void cq_verifier_destroy (cq_verifier * v); /* ensure all expected events (and only those events) are present on the bound completion queue */ -void cq_verify(cq_verifier *v); +void cq_verify (cq_verifier * v); /* ensure that the completion queue is empty */ -void cq_verify_empty(cq_verifier *v); +void cq_verify_empty (cq_verifier * v); /* Various expectation matchers Any functions taking ... expect a NULL terminated list of key/value pairs (each pair using two parameter slots) of metadata that MUST be present in the event. */ -void cq_expect_completion(cq_verifier *v, void *tag, int success); +void cq_expect_completion (cq_verifier * v, void *tag, int success); -int byte_buffer_eq_string(grpc_byte_buffer *byte_buffer, const char *string); -int contains_metadata(grpc_metadata_array *array, const char *key, - const char *value); +int byte_buffer_eq_string (grpc_byte_buffer * byte_buffer, const char *string); +int contains_metadata (grpc_metadata_array * array, const char *key, const char *value); #endif /* GRPC_TEST_CORE_END2END_CQ_VERIFIER_H */ diff --git a/test/core/end2end/data/server1_cert.c b/test/core/end2end/data/server1_cert.c index d31f2e2d0e..a303b27282 100644 --- a/test/core/end2end/data/server1_cert.c +++ b/test/core/end2end/data/server1_cert.c @@ -32,84 +32,85 @@ */ const char test_server1_cert[] = { - 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, - 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, - 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x6d, 0x7a, 0x43, 0x43, - 0x41, 0x67, 0x53, 0x67, 0x41, 0x77, 0x49, 0x42, 0x41, 0x67, 0x49, 0x42, - 0x41, 0x7a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, 0x47, - 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x55, 0x46, 0x41, 0x44, 0x42, 0x57, - 0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, - 0x45, 0x77, 0x4a, 0x42, 0x56, 0x54, 0x45, 0x54, 0x0a, 0x4d, 0x42, 0x45, - 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x4b, 0x55, 0x32, 0x39, - 0x74, 0x5a, 0x53, 0x31, 0x54, 0x64, 0x47, 0x46, 0x30, 0x5a, 0x54, 0x45, - 0x68, 0x4d, 0x42, 0x38, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x67, 0x77, - 0x59, 0x53, 0x57, 0x35, 0x30, 0x5a, 0x58, 0x4a, 0x75, 0x5a, 0x58, 0x51, - 0x67, 0x56, 0x32, 0x6c, 0x6b, 0x5a, 0x32, 0x6c, 0x30, 0x63, 0x79, 0x42, - 0x51, 0x0a, 0x64, 0x48, 0x6b, 0x67, 0x54, 0x48, 0x52, 0x6b, 0x4d, 0x51, - 0x38, 0x77, 0x44, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x44, 0x41, - 0x5a, 0x30, 0x5a, 0x58, 0x4e, 0x30, 0x59, 0x32, 0x45, 0x77, 0x48, 0x68, - 0x63, 0x4e, 0x4d, 0x54, 0x51, 0x77, 0x4e, 0x7a, 0x49, 0x79, 0x4d, 0x44, - 0x59, 0x77, 0x4d, 0x44, 0x55, 0x33, 0x57, 0x68, 0x63, 0x4e, 0x4d, 0x6a, - 0x51, 0x77, 0x4e, 0x7a, 0x45, 0x35, 0x0a, 0x4d, 0x44, 0x59, 0x77, 0x4d, - 0x44, 0x55, 0x33, 0x57, 0x6a, 0x42, 0x6b, 0x4d, 0x51, 0x73, 0x77, 0x43, - 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x56, 0x55, - 0x7a, 0x45, 0x52, 0x4d, 0x41, 0x38, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, - 0x42, 0x4d, 0x49, 0x53, 0x57, 0x78, 0x73, 0x61, 0x57, 0x35, 0x76, 0x61, - 0x58, 0x4d, 0x78, 0x45, 0x44, 0x41, 0x4f, 0x42, 0x67, 0x4e, 0x56, 0x0a, - 0x42, 0x41, 0x63, 0x54, 0x42, 0x30, 0x4e, 0x6f, 0x61, 0x57, 0x4e, 0x68, - 0x5a, 0x32, 0x38, 0x78, 0x46, 0x44, 0x41, 0x53, 0x42, 0x67, 0x4e, 0x56, - 0x42, 0x41, 0x6f, 0x54, 0x43, 0x30, 0x64, 0x76, 0x62, 0x32, 0x64, 0x73, - 0x5a, 0x53, 0x42, 0x4a, 0x62, 0x6d, 0x4d, 0x75, 0x4d, 0x52, 0x6f, 0x77, - 0x47, 0x41, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x46, 0x42, 0x45, 0x71, - 0x4c, 0x6e, 0x52, 0x6c, 0x0a, 0x63, 0x33, 0x51, 0x75, 0x5a, 0x32, 0x39, - 0x76, 0x5a, 0x32, 0x78, 0x6c, 0x4c, 0x6d, 0x4e, 0x76, 0x62, 0x54, 0x43, - 0x42, 0x6e, 0x7a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, - 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x45, 0x46, 0x41, 0x41, 0x4f, - 0x42, 0x6a, 0x51, 0x41, 0x77, 0x67, 0x59, 0x6b, 0x43, 0x67, 0x59, 0x45, - 0x41, 0x34, 0x63, 0x4d, 0x56, 0x4a, 0x79, 0x67, 0x73, 0x0a, 0x4a, 0x55, - 0x6d, 0x6c, 0x67, 0x4d, 0x4d, 0x7a, 0x67, 0x64, 0x69, 0x30, 0x68, 0x31, - 0x58, 0x6f, 0x43, 0x52, 0x37, 0x2b, 0x77, 0x77, 0x31, 0x70, 0x6f, 0x70, - 0x30, 0x34, 0x4f, 0x4d, 0x4d, 0x79, 0x79, 0x37, 0x48, 0x2f, 0x69, 0x30, - 0x50, 0x4a, 0x32, 0x57, 0x36, 0x59, 0x33, 0x35, 0x2b, 0x62, 0x34, 0x43, - 0x4d, 0x38, 0x51, 0x72, 0x6b, 0x59, 0x65, 0x45, 0x61, 0x66, 0x55, 0x47, - 0x44, 0x4f, 0x0a, 0x52, 0x59, 0x58, 0x36, 0x79, 0x56, 0x2f, 0x63, 0x48, - 0x47, 0x47, 0x73, 0x44, 0x2f, 0x78, 0x30, 0x32, 0x79, 0x65, 0x36, 0x65, - 0x79, 0x31, 0x55, 0x44, 0x74, 0x6b, 0x47, 0x41, 0x44, 0x2f, 0x6d, 0x70, - 0x44, 0x45, 0x78, 0x38, 0x59, 0x43, 0x72, 0x6a, 0x41, 0x63, 0x31, 0x56, - 0x66, 0x76, 0x74, 0x38, 0x46, 0x6b, 0x36, 0x43, 0x6e, 0x31, 0x57, 0x56, - 0x49, 0x78, 0x56, 0x2f, 0x4a, 0x33, 0x30, 0x0a, 0x33, 0x78, 0x6a, 0x42, - 0x73, 0x46, 0x67, 0x42, 0x79, 0x51, 0x35, 0x35, 0x52, 0x42, 0x70, 0x31, - 0x4f, 0x4c, 0x5a, 0x66, 0x56, 0x4c, 0x6f, 0x36, 0x41, 0x6c, 0x65, 0x42, - 0x44, 0x53, 0x62, 0x63, 0x78, 0x61, 0x45, 0x43, 0x41, 0x77, 0x45, 0x41, - 0x41, 0x61, 0x4e, 0x72, 0x4d, 0x47, 0x6b, 0x77, 0x43, 0x51, 0x59, 0x44, - 0x56, 0x52, 0x30, 0x54, 0x42, 0x41, 0x49, 0x77, 0x41, 0x44, 0x41, 0x4c, - 0x0a, 0x42, 0x67, 0x4e, 0x56, 0x48, 0x51, 0x38, 0x45, 0x42, 0x41, 0x4d, - 0x43, 0x42, 0x65, 0x41, 0x77, 0x54, 0x77, 0x59, 0x44, 0x56, 0x52, 0x30, - 0x52, 0x42, 0x45, 0x67, 0x77, 0x52, 0x6f, 0x49, 0x51, 0x4b, 0x69, 0x35, - 0x30, 0x5a, 0x58, 0x4e, 0x30, 0x4c, 0x6d, 0x64, 0x76, 0x62, 0x32, 0x64, - 0x73, 0x5a, 0x53, 0x35, 0x6d, 0x63, 0x6f, 0x49, 0x59, 0x64, 0x32, 0x46, - 0x30, 0x5a, 0x58, 0x4a, 0x36, 0x0a, 0x62, 0x32, 0x39, 0x70, 0x4c, 0x6e, - 0x52, 0x6c, 0x63, 0x33, 0x51, 0x75, 0x5a, 0x32, 0x39, 0x76, 0x5a, 0x32, - 0x78, 0x6c, 0x4c, 0x6d, 0x4a, 0x6c, 0x67, 0x68, 0x49, 0x71, 0x4c, 0x6e, - 0x52, 0x6c, 0x63, 0x33, 0x51, 0x75, 0x65, 0x57, 0x39, 0x31, 0x64, 0x48, - 0x56, 0x69, 0x5a, 0x53, 0x35, 0x6a, 0x62, 0x32, 0x32, 0x48, 0x42, 0x4d, - 0x43, 0x6f, 0x41, 0x51, 0x4d, 0x77, 0x44, 0x51, 0x59, 0x4a, 0x0a, 0x4b, - 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, 0x41, 0x51, 0x45, 0x46, 0x42, - 0x51, 0x41, 0x44, 0x67, 0x59, 0x45, 0x41, 0x4d, 0x32, 0x49, 0x69, 0x30, - 0x4c, 0x67, 0x54, 0x47, 0x62, 0x4a, 0x31, 0x6a, 0x34, 0x6f, 0x71, 0x58, - 0x39, 0x62, 0x78, 0x56, 0x63, 0x78, 0x6d, 0x2b, 0x2f, 0x52, 0x35, 0x59, - 0x66, 0x38, 0x6f, 0x69, 0x30, 0x61, 0x5a, 0x71, 0x54, 0x4a, 0x6c, 0x6e, - 0x4c, 0x59, 0x53, 0x0a, 0x77, 0x58, 0x63, 0x42, 0x79, 0x6b, 0x78, 0x54, - 0x78, 0x31, 0x38, 0x31, 0x73, 0x37, 0x57, 0x79, 0x66, 0x4a, 0x34, 0x39, - 0x57, 0x77, 0x72, 0x59, 0x58, 0x6f, 0x37, 0x38, 0x7a, 0x54, 0x44, 0x41, - 0x6e, 0x66, 0x31, 0x6d, 0x61, 0x30, 0x66, 0x50, 0x71, 0x33, 0x65, 0x34, - 0x6d, 0x70, 0x73, 0x70, 0x76, 0x79, 0x6e, 0x64, 0x4c, 0x68, 0x31, 0x61, - 0x2b, 0x4f, 0x61, 0x72, 0x48, 0x61, 0x31, 0x65, 0x0a, 0x61, 0x54, 0x30, - 0x44, 0x49, 0x49, 0x59, 0x6b, 0x37, 0x71, 0x65, 0x45, 0x61, 0x31, 0x59, - 0x63, 0x56, 0x6c, 0x6a, 0x78, 0x32, 0x4b, 0x79, 0x4c, 0x64, 0x30, 0x72, - 0x31, 0x42, 0x42, 0x41, 0x66, 0x72, 0x77, 0x79, 0x47, 0x61, 0x45, 0x50, - 0x56, 0x65, 0x4a, 0x51, 0x56, 0x59, 0x57, 0x61, 0x4f, 0x4a, 0x52, 0x55, - 0x32, 0x77, 0x65, 0x2f, 0x4b, 0x44, 0x34, 0x6f, 0x6a, 0x66, 0x39, 0x73, - 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x43, - 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, - 0x2d, 0x2d, 0x2d, 0x0a, 0x00}; + 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, + 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, + 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x6d, 0x7a, 0x43, 0x43, + 0x41, 0x67, 0x53, 0x67, 0x41, 0x77, 0x49, 0x42, 0x41, 0x67, 0x49, 0x42, + 0x41, 0x7a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, 0x47, + 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x55, 0x46, 0x41, 0x44, 0x42, 0x57, + 0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, + 0x45, 0x77, 0x4a, 0x42, 0x56, 0x54, 0x45, 0x54, 0x0a, 0x4d, 0x42, 0x45, + 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x4b, 0x55, 0x32, 0x39, + 0x74, 0x5a, 0x53, 0x31, 0x54, 0x64, 0x47, 0x46, 0x30, 0x5a, 0x54, 0x45, + 0x68, 0x4d, 0x42, 0x38, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x67, 0x77, + 0x59, 0x53, 0x57, 0x35, 0x30, 0x5a, 0x58, 0x4a, 0x75, 0x5a, 0x58, 0x51, + 0x67, 0x56, 0x32, 0x6c, 0x6b, 0x5a, 0x32, 0x6c, 0x30, 0x63, 0x79, 0x42, + 0x51, 0x0a, 0x64, 0x48, 0x6b, 0x67, 0x54, 0x48, 0x52, 0x6b, 0x4d, 0x51, + 0x38, 0x77, 0x44, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x44, 0x41, + 0x5a, 0x30, 0x5a, 0x58, 0x4e, 0x30, 0x59, 0x32, 0x45, 0x77, 0x48, 0x68, + 0x63, 0x4e, 0x4d, 0x54, 0x51, 0x77, 0x4e, 0x7a, 0x49, 0x79, 0x4d, 0x44, + 0x59, 0x77, 0x4d, 0x44, 0x55, 0x33, 0x57, 0x68, 0x63, 0x4e, 0x4d, 0x6a, + 0x51, 0x77, 0x4e, 0x7a, 0x45, 0x35, 0x0a, 0x4d, 0x44, 0x59, 0x77, 0x4d, + 0x44, 0x55, 0x33, 0x57, 0x6a, 0x42, 0x6b, 0x4d, 0x51, 0x73, 0x77, 0x43, + 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x56, 0x55, + 0x7a, 0x45, 0x52, 0x4d, 0x41, 0x38, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, + 0x42, 0x4d, 0x49, 0x53, 0x57, 0x78, 0x73, 0x61, 0x57, 0x35, 0x76, 0x61, + 0x58, 0x4d, 0x78, 0x45, 0x44, 0x41, 0x4f, 0x42, 0x67, 0x4e, 0x56, 0x0a, + 0x42, 0x41, 0x63, 0x54, 0x42, 0x30, 0x4e, 0x6f, 0x61, 0x57, 0x4e, 0x68, + 0x5a, 0x32, 0x38, 0x78, 0x46, 0x44, 0x41, 0x53, 0x42, 0x67, 0x4e, 0x56, + 0x42, 0x41, 0x6f, 0x54, 0x43, 0x30, 0x64, 0x76, 0x62, 0x32, 0x64, 0x73, + 0x5a, 0x53, 0x42, 0x4a, 0x62, 0x6d, 0x4d, 0x75, 0x4d, 0x52, 0x6f, 0x77, + 0x47, 0x41, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x46, 0x42, 0x45, 0x71, + 0x4c, 0x6e, 0x52, 0x6c, 0x0a, 0x63, 0x33, 0x51, 0x75, 0x5a, 0x32, 0x39, + 0x76, 0x5a, 0x32, 0x78, 0x6c, 0x4c, 0x6d, 0x4e, 0x76, 0x62, 0x54, 0x43, + 0x42, 0x6e, 0x7a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, + 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x45, 0x46, 0x41, 0x41, 0x4f, + 0x42, 0x6a, 0x51, 0x41, 0x77, 0x67, 0x59, 0x6b, 0x43, 0x67, 0x59, 0x45, + 0x41, 0x34, 0x63, 0x4d, 0x56, 0x4a, 0x79, 0x67, 0x73, 0x0a, 0x4a, 0x55, + 0x6d, 0x6c, 0x67, 0x4d, 0x4d, 0x7a, 0x67, 0x64, 0x69, 0x30, 0x68, 0x31, + 0x58, 0x6f, 0x43, 0x52, 0x37, 0x2b, 0x77, 0x77, 0x31, 0x70, 0x6f, 0x70, + 0x30, 0x34, 0x4f, 0x4d, 0x4d, 0x79, 0x79, 0x37, 0x48, 0x2f, 0x69, 0x30, + 0x50, 0x4a, 0x32, 0x57, 0x36, 0x59, 0x33, 0x35, 0x2b, 0x62, 0x34, 0x43, + 0x4d, 0x38, 0x51, 0x72, 0x6b, 0x59, 0x65, 0x45, 0x61, 0x66, 0x55, 0x47, + 0x44, 0x4f, 0x0a, 0x52, 0x59, 0x58, 0x36, 0x79, 0x56, 0x2f, 0x63, 0x48, + 0x47, 0x47, 0x73, 0x44, 0x2f, 0x78, 0x30, 0x32, 0x79, 0x65, 0x36, 0x65, + 0x79, 0x31, 0x55, 0x44, 0x74, 0x6b, 0x47, 0x41, 0x44, 0x2f, 0x6d, 0x70, + 0x44, 0x45, 0x78, 0x38, 0x59, 0x43, 0x72, 0x6a, 0x41, 0x63, 0x31, 0x56, + 0x66, 0x76, 0x74, 0x38, 0x46, 0x6b, 0x36, 0x43, 0x6e, 0x31, 0x57, 0x56, + 0x49, 0x78, 0x56, 0x2f, 0x4a, 0x33, 0x30, 0x0a, 0x33, 0x78, 0x6a, 0x42, + 0x73, 0x46, 0x67, 0x42, 0x79, 0x51, 0x35, 0x35, 0x52, 0x42, 0x70, 0x31, + 0x4f, 0x4c, 0x5a, 0x66, 0x56, 0x4c, 0x6f, 0x36, 0x41, 0x6c, 0x65, 0x42, + 0x44, 0x53, 0x62, 0x63, 0x78, 0x61, 0x45, 0x43, 0x41, 0x77, 0x45, 0x41, + 0x41, 0x61, 0x4e, 0x72, 0x4d, 0x47, 0x6b, 0x77, 0x43, 0x51, 0x59, 0x44, + 0x56, 0x52, 0x30, 0x54, 0x42, 0x41, 0x49, 0x77, 0x41, 0x44, 0x41, 0x4c, + 0x0a, 0x42, 0x67, 0x4e, 0x56, 0x48, 0x51, 0x38, 0x45, 0x42, 0x41, 0x4d, + 0x43, 0x42, 0x65, 0x41, 0x77, 0x54, 0x77, 0x59, 0x44, 0x56, 0x52, 0x30, + 0x52, 0x42, 0x45, 0x67, 0x77, 0x52, 0x6f, 0x49, 0x51, 0x4b, 0x69, 0x35, + 0x30, 0x5a, 0x58, 0x4e, 0x30, 0x4c, 0x6d, 0x64, 0x76, 0x62, 0x32, 0x64, + 0x73, 0x5a, 0x53, 0x35, 0x6d, 0x63, 0x6f, 0x49, 0x59, 0x64, 0x32, 0x46, + 0x30, 0x5a, 0x58, 0x4a, 0x36, 0x0a, 0x62, 0x32, 0x39, 0x70, 0x4c, 0x6e, + 0x52, 0x6c, 0x63, 0x33, 0x51, 0x75, 0x5a, 0x32, 0x39, 0x76, 0x5a, 0x32, + 0x78, 0x6c, 0x4c, 0x6d, 0x4a, 0x6c, 0x67, 0x68, 0x49, 0x71, 0x4c, 0x6e, + 0x52, 0x6c, 0x63, 0x33, 0x51, 0x75, 0x65, 0x57, 0x39, 0x31, 0x64, 0x48, + 0x56, 0x69, 0x5a, 0x53, 0x35, 0x6a, 0x62, 0x32, 0x32, 0x48, 0x42, 0x4d, + 0x43, 0x6f, 0x41, 0x51, 0x4d, 0x77, 0x44, 0x51, 0x59, 0x4a, 0x0a, 0x4b, + 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, 0x41, 0x51, 0x45, 0x46, 0x42, + 0x51, 0x41, 0x44, 0x67, 0x59, 0x45, 0x41, 0x4d, 0x32, 0x49, 0x69, 0x30, + 0x4c, 0x67, 0x54, 0x47, 0x62, 0x4a, 0x31, 0x6a, 0x34, 0x6f, 0x71, 0x58, + 0x39, 0x62, 0x78, 0x56, 0x63, 0x78, 0x6d, 0x2b, 0x2f, 0x52, 0x35, 0x59, + 0x66, 0x38, 0x6f, 0x69, 0x30, 0x61, 0x5a, 0x71, 0x54, 0x4a, 0x6c, 0x6e, + 0x4c, 0x59, 0x53, 0x0a, 0x77, 0x58, 0x63, 0x42, 0x79, 0x6b, 0x78, 0x54, + 0x78, 0x31, 0x38, 0x31, 0x73, 0x37, 0x57, 0x79, 0x66, 0x4a, 0x34, 0x39, + 0x57, 0x77, 0x72, 0x59, 0x58, 0x6f, 0x37, 0x38, 0x7a, 0x54, 0x44, 0x41, + 0x6e, 0x66, 0x31, 0x6d, 0x61, 0x30, 0x66, 0x50, 0x71, 0x33, 0x65, 0x34, + 0x6d, 0x70, 0x73, 0x70, 0x76, 0x79, 0x6e, 0x64, 0x4c, 0x68, 0x31, 0x61, + 0x2b, 0x4f, 0x61, 0x72, 0x48, 0x61, 0x31, 0x65, 0x0a, 0x61, 0x54, 0x30, + 0x44, 0x49, 0x49, 0x59, 0x6b, 0x37, 0x71, 0x65, 0x45, 0x61, 0x31, 0x59, + 0x63, 0x56, 0x6c, 0x6a, 0x78, 0x32, 0x4b, 0x79, 0x4c, 0x64, 0x30, 0x72, + 0x31, 0x42, 0x42, 0x41, 0x66, 0x72, 0x77, 0x79, 0x47, 0x61, 0x45, 0x50, + 0x56, 0x65, 0x4a, 0x51, 0x56, 0x59, 0x57, 0x61, 0x4f, 0x4a, 0x52, 0x55, + 0x32, 0x77, 0x65, 0x2f, 0x4b, 0x44, 0x34, 0x6f, 0x6a, 0x66, 0x39, 0x73, + 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x43, + 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, + 0x2d, 0x2d, 0x2d, 0x0a, 0x00 +}; diff --git a/test/core/end2end/data/server1_key.c b/test/core/end2end/data/server1_key.c index d089660bfd..78353eed6d 100644 --- a/test/core/end2end/data/server1_key.c +++ b/test/core/end2end/data/server1_key.c @@ -32,77 +32,78 @@ */ const char test_server1_key[] = { - 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, - 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, - 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, - 0x57, 0x77, 0x49, 0x42, 0x41, 0x41, 0x4b, 0x42, 0x67, 0x51, 0x44, 0x68, - 0x77, 0x78, 0x55, 0x6e, 0x4b, 0x43, 0x77, 0x6c, 0x53, 0x61, 0x57, 0x41, - 0x77, 0x7a, 0x4f, 0x42, 0x32, 0x4c, 0x53, 0x48, 0x56, 0x65, 0x67, 0x4a, - 0x48, 0x76, 0x37, 0x44, 0x44, 0x57, 0x6d, 0x69, 0x6e, 0x54, 0x67, 0x34, - 0x77, 0x7a, 0x4c, 0x4c, 0x73, 0x66, 0x2b, 0x4c, 0x51, 0x38, 0x6e, 0x5a, - 0x0a, 0x62, 0x70, 0x6a, 0x66, 0x6e, 0x35, 0x76, 0x67, 0x49, 0x7a, 0x78, - 0x43, 0x75, 0x52, 0x68, 0x34, 0x52, 0x70, 0x39, 0x51, 0x59, 0x4d, 0x35, - 0x46, 0x68, 0x66, 0x72, 0x4a, 0x58, 0x39, 0x77, 0x63, 0x59, 0x61, 0x77, - 0x50, 0x2f, 0x48, 0x54, 0x62, 0x4a, 0x37, 0x70, 0x37, 0x4c, 0x56, 0x51, - 0x4f, 0x32, 0x51, 0x59, 0x41, 0x50, 0x2b, 0x61, 0x6b, 0x4d, 0x54, 0x48, - 0x78, 0x67, 0x4b, 0x75, 0x4d, 0x0a, 0x42, 0x7a, 0x56, 0x56, 0x2b, 0x2b, - 0x33, 0x77, 0x57, 0x54, 0x6f, 0x4b, 0x66, 0x56, 0x5a, 0x55, 0x6a, 0x46, - 0x58, 0x38, 0x6e, 0x66, 0x54, 0x66, 0x47, 0x4d, 0x47, 0x77, 0x57, 0x41, - 0x48, 0x4a, 0x44, 0x6e, 0x6c, 0x45, 0x47, 0x6e, 0x55, 0x34, 0x74, 0x6c, - 0x39, 0x55, 0x75, 0x6a, 0x6f, 0x43, 0x56, 0x34, 0x45, 0x4e, 0x4a, 0x74, - 0x7a, 0x46, 0x6f, 0x51, 0x49, 0x44, 0x41, 0x51, 0x41, 0x42, 0x0a, 0x41, - 0x6f, 0x47, 0x41, 0x4a, 0x2b, 0x36, 0x68, 0x70, 0x7a, 0x4e, 0x72, 0x32, - 0x34, 0x79, 0x54, 0x51, 0x5a, 0x74, 0x46, 0x57, 0x51, 0x70, 0x44, 0x70, - 0x45, 0x79, 0x46, 0x70, 0x6c, 0x64, 0x64, 0x4b, 0x4a, 0x4d, 0x4f, 0x78, - 0x44, 0x79, 0x61, 0x33, 0x53, 0x39, 0x70, 0x70, 0x4b, 0x33, 0x76, 0x54, - 0x57, 0x72, 0x49, 0x49, 0x54, 0x56, 0x32, 0x78, 0x4e, 0x63, 0x75, 0x63, - 0x77, 0x37, 0x49, 0x0a, 0x63, 0x65, 0x54, 0x62, 0x64, 0x79, 0x72, 0x47, - 0x73, 0x79, 0x6a, 0x73, 0x55, 0x30, 0x2f, 0x48, 0x64, 0x43, 0x63, 0x49, - 0x66, 0x39, 0x79, 0x6d, 0x32, 0x6a, 0x66, 0x6d, 0x47, 0x4c, 0x55, 0x77, - 0x6d, 0x79, 0x68, 0x6c, 0x74, 0x4b, 0x56, 0x77, 0x30, 0x51, 0x59, 0x63, - 0x46, 0x42, 0x30, 0x58, 0x4c, 0x6b, 0x63, 0x30, 0x6e, 0x49, 0x35, 0x59, - 0x76, 0x45, 0x59, 0x6f, 0x65, 0x56, 0x44, 0x67, 0x0a, 0x6f, 0x6d, 0x5a, - 0x49, 0x58, 0x6e, 0x31, 0x45, 0x33, 0x45, 0x57, 0x2b, 0x73, 0x53, 0x49, - 0x57, 0x53, 0x62, 0x6b, 0x4d, 0x75, 0x39, 0x62, 0x59, 0x32, 0x6b, 0x73, - 0x74, 0x4b, 0x58, 0x52, 0x32, 0x55, 0x5a, 0x6d, 0x4d, 0x67, 0x57, 0x44, - 0x74, 0x6d, 0x42, 0x45, 0x50, 0x4d, 0x61, 0x45, 0x43, 0x51, 0x51, 0x44, - 0x36, 0x79, 0x54, 0x34, 0x54, 0x41, 0x5a, 0x4d, 0x35, 0x68, 0x47, 0x42, - 0x62, 0x0a, 0x63, 0x69, 0x42, 0x4b, 0x67, 0x4d, 0x55, 0x50, 0x36, 0x50, - 0x77, 0x4f, 0x68, 0x50, 0x68, 0x4f, 0x4d, 0x50, 0x49, 0x76, 0x69, 0x6a, - 0x4f, 0x35, 0x30, 0x41, 0x69, 0x75, 0x36, 0x69, 0x75, 0x43, 0x56, 0x38, - 0x38, 0x6c, 0x31, 0x51, 0x49, 0x79, 0x33, 0x38, 0x67, 0x57, 0x56, 0x68, - 0x78, 0x6a, 0x4e, 0x72, 0x71, 0x36, 0x50, 0x33, 0x34, 0x36, 0x6a, 0x34, - 0x49, 0x42, 0x67, 0x2b, 0x6b, 0x42, 0x0a, 0x39, 0x61, 0x6c, 0x77, 0x70, - 0x43, 0x4f, 0x44, 0x41, 0x6b, 0x45, 0x41, 0x35, 0x6e, 0x53, 0x6e, 0x6d, - 0x39, 0x6b, 0x36, 0x79, 0x6b, 0x59, 0x65, 0x51, 0x57, 0x4e, 0x53, 0x30, - 0x66, 0x4e, 0x57, 0x69, 0x52, 0x69, 0x6e, 0x43, 0x64, 0x6c, 0x32, 0x33, - 0x41, 0x37, 0x75, 0x73, 0x44, 0x47, 0x53, 0x75, 0x4b, 0x4b, 0x6c, 0x6d, - 0x30, 0x31, 0x39, 0x69, 0x6f, 0x6d, 0x4a, 0x2f, 0x52, 0x67, 0x64, 0x0a, - 0x4d, 0x4b, 0x44, 0x4f, 0x70, 0x30, 0x71, 0x2f, 0x32, 0x4f, 0x6f, 0x73, - 0x74, 0x62, 0x74, 0x65, 0x4f, 0x57, 0x4d, 0x32, 0x4d, 0x52, 0x46, 0x66, - 0x34, 0x6a, 0x4d, 0x48, 0x33, 0x77, 0x79, 0x56, 0x43, 0x77, 0x4a, 0x41, - 0x66, 0x41, 0x64, 0x6a, 0x4a, 0x38, 0x73, 0x7a, 0x6f, 0x4e, 0x4b, 0x54, - 0x52, 0x53, 0x61, 0x67, 0x53, 0x62, 0x68, 0x39, 0x76, 0x57, 0x79, 0x67, - 0x6e, 0x42, 0x32, 0x76, 0x0a, 0x49, 0x42, 0x79, 0x63, 0x36, 0x6c, 0x34, - 0x54, 0x54, 0x75, 0x5a, 0x51, 0x4a, 0x52, 0x47, 0x7a, 0x43, 0x76, 0x65, - 0x61, 0x66, 0x7a, 0x39, 0x6c, 0x6f, 0x76, 0x75, 0x42, 0x33, 0x57, 0x6f, - 0x68, 0x43, 0x41, 0x42, 0x64, 0x51, 0x52, 0x64, 0x39, 0x75, 0x6b, 0x43, - 0x58, 0x4c, 0x32, 0x43, 0x70, 0x73, 0x45, 0x70, 0x71, 0x7a, 0x6b, 0x61, - 0x66, 0x4f, 0x51, 0x4a, 0x41, 0x4a, 0x55, 0x6a, 0x63, 0x0a, 0x55, 0x53, - 0x65, 0x64, 0x44, 0x6c, 0x71, 0x33, 0x7a, 0x47, 0x5a, 0x77, 0x59, 0x4d, - 0x31, 0x59, 0x77, 0x38, 0x64, 0x38, 0x52, 0x75, 0x69, 0x72, 0x42, 0x55, - 0x46, 0x5a, 0x4e, 0x71, 0x4a, 0x65, 0x6c, 0x59, 0x61, 0x69, 0x2b, 0x6e, - 0x52, 0x59, 0x43, 0x6c, 0x44, 0x6b, 0x52, 0x56, 0x46, 0x67, 0x62, 0x35, - 0x79, 0x6b, 0x73, 0x6f, 0x59, 0x79, 0x63, 0x62, 0x71, 0x35, 0x54, 0x78, - 0x47, 0x6f, 0x0a, 0x56, 0x65, 0x71, 0x4b, 0x4f, 0x76, 0x67, 0x50, 0x70, - 0x6a, 0x34, 0x52, 0x57, 0x50, 0x48, 0x6c, 0x4c, 0x77, 0x4a, 0x41, 0x47, - 0x55, 0x4d, 0x6b, 0x33, 0x62, 0x71, 0x54, 0x39, 0x31, 0x78, 0x42, 0x55, - 0x43, 0x6e, 0x4c, 0x52, 0x73, 0x2f, 0x76, 0x66, 0x6f, 0x43, 0x70, 0x48, - 0x70, 0x67, 0x36, 0x65, 0x79, 0x77, 0x51, 0x54, 0x42, 0x44, 0x41, 0x56, - 0x36, 0x78, 0x6b, 0x79, 0x7a, 0x34, 0x61, 0x0a, 0x52, 0x48, 0x33, 0x49, - 0x37, 0x2f, 0x2b, 0x79, 0x6a, 0x33, 0x5a, 0x78, 0x52, 0x32, 0x4a, 0x6f, - 0x57, 0x48, 0x67, 0x55, 0x77, 0x5a, 0x37, 0x6c, 0x5a, 0x6b, 0x31, 0x56, - 0x6e, 0x68, 0x66, 0x66, 0x46, 0x79, 0x65, 0x37, 0x53, 0x42, 0x58, 0x79, - 0x61, 0x67, 0x3d, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, - 0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, - 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00}; + 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, + 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, + 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, + 0x57, 0x77, 0x49, 0x42, 0x41, 0x41, 0x4b, 0x42, 0x67, 0x51, 0x44, 0x68, + 0x77, 0x78, 0x55, 0x6e, 0x4b, 0x43, 0x77, 0x6c, 0x53, 0x61, 0x57, 0x41, + 0x77, 0x7a, 0x4f, 0x42, 0x32, 0x4c, 0x53, 0x48, 0x56, 0x65, 0x67, 0x4a, + 0x48, 0x76, 0x37, 0x44, 0x44, 0x57, 0x6d, 0x69, 0x6e, 0x54, 0x67, 0x34, + 0x77, 0x7a, 0x4c, 0x4c, 0x73, 0x66, 0x2b, 0x4c, 0x51, 0x38, 0x6e, 0x5a, + 0x0a, 0x62, 0x70, 0x6a, 0x66, 0x6e, 0x35, 0x76, 0x67, 0x49, 0x7a, 0x78, + 0x43, 0x75, 0x52, 0x68, 0x34, 0x52, 0x70, 0x39, 0x51, 0x59, 0x4d, 0x35, + 0x46, 0x68, 0x66, 0x72, 0x4a, 0x58, 0x39, 0x77, 0x63, 0x59, 0x61, 0x77, + 0x50, 0x2f, 0x48, 0x54, 0x62, 0x4a, 0x37, 0x70, 0x37, 0x4c, 0x56, 0x51, + 0x4f, 0x32, 0x51, 0x59, 0x41, 0x50, 0x2b, 0x61, 0x6b, 0x4d, 0x54, 0x48, + 0x78, 0x67, 0x4b, 0x75, 0x4d, 0x0a, 0x42, 0x7a, 0x56, 0x56, 0x2b, 0x2b, + 0x33, 0x77, 0x57, 0x54, 0x6f, 0x4b, 0x66, 0x56, 0x5a, 0x55, 0x6a, 0x46, + 0x58, 0x38, 0x6e, 0x66, 0x54, 0x66, 0x47, 0x4d, 0x47, 0x77, 0x57, 0x41, + 0x48, 0x4a, 0x44, 0x6e, 0x6c, 0x45, 0x47, 0x6e, 0x55, 0x34, 0x74, 0x6c, + 0x39, 0x55, 0x75, 0x6a, 0x6f, 0x43, 0x56, 0x34, 0x45, 0x4e, 0x4a, 0x74, + 0x7a, 0x46, 0x6f, 0x51, 0x49, 0x44, 0x41, 0x51, 0x41, 0x42, 0x0a, 0x41, + 0x6f, 0x47, 0x41, 0x4a, 0x2b, 0x36, 0x68, 0x70, 0x7a, 0x4e, 0x72, 0x32, + 0x34, 0x79, 0x54, 0x51, 0x5a, 0x74, 0x46, 0x57, 0x51, 0x70, 0x44, 0x70, + 0x45, 0x79, 0x46, 0x70, 0x6c, 0x64, 0x64, 0x4b, 0x4a, 0x4d, 0x4f, 0x78, + 0x44, 0x79, 0x61, 0x33, 0x53, 0x39, 0x70, 0x70, 0x4b, 0x33, 0x76, 0x54, + 0x57, 0x72, 0x49, 0x49, 0x54, 0x56, 0x32, 0x78, 0x4e, 0x63, 0x75, 0x63, + 0x77, 0x37, 0x49, 0x0a, 0x63, 0x65, 0x54, 0x62, 0x64, 0x79, 0x72, 0x47, + 0x73, 0x79, 0x6a, 0x73, 0x55, 0x30, 0x2f, 0x48, 0x64, 0x43, 0x63, 0x49, + 0x66, 0x39, 0x79, 0x6d, 0x32, 0x6a, 0x66, 0x6d, 0x47, 0x4c, 0x55, 0x77, + 0x6d, 0x79, 0x68, 0x6c, 0x74, 0x4b, 0x56, 0x77, 0x30, 0x51, 0x59, 0x63, + 0x46, 0x42, 0x30, 0x58, 0x4c, 0x6b, 0x63, 0x30, 0x6e, 0x49, 0x35, 0x59, + 0x76, 0x45, 0x59, 0x6f, 0x65, 0x56, 0x44, 0x67, 0x0a, 0x6f, 0x6d, 0x5a, + 0x49, 0x58, 0x6e, 0x31, 0x45, 0x33, 0x45, 0x57, 0x2b, 0x73, 0x53, 0x49, + 0x57, 0x53, 0x62, 0x6b, 0x4d, 0x75, 0x39, 0x62, 0x59, 0x32, 0x6b, 0x73, + 0x74, 0x4b, 0x58, 0x52, 0x32, 0x55, 0x5a, 0x6d, 0x4d, 0x67, 0x57, 0x44, + 0x74, 0x6d, 0x42, 0x45, 0x50, 0x4d, 0x61, 0x45, 0x43, 0x51, 0x51, 0x44, + 0x36, 0x79, 0x54, 0x34, 0x54, 0x41, 0x5a, 0x4d, 0x35, 0x68, 0x47, 0x42, + 0x62, 0x0a, 0x63, 0x69, 0x42, 0x4b, 0x67, 0x4d, 0x55, 0x50, 0x36, 0x50, + 0x77, 0x4f, 0x68, 0x50, 0x68, 0x4f, 0x4d, 0x50, 0x49, 0x76, 0x69, 0x6a, + 0x4f, 0x35, 0x30, 0x41, 0x69, 0x75, 0x36, 0x69, 0x75, 0x43, 0x56, 0x38, + 0x38, 0x6c, 0x31, 0x51, 0x49, 0x79, 0x33, 0x38, 0x67, 0x57, 0x56, 0x68, + 0x78, 0x6a, 0x4e, 0x72, 0x71, 0x36, 0x50, 0x33, 0x34, 0x36, 0x6a, 0x34, + 0x49, 0x42, 0x67, 0x2b, 0x6b, 0x42, 0x0a, 0x39, 0x61, 0x6c, 0x77, 0x70, + 0x43, 0x4f, 0x44, 0x41, 0x6b, 0x45, 0x41, 0x35, 0x6e, 0x53, 0x6e, 0x6d, + 0x39, 0x6b, 0x36, 0x79, 0x6b, 0x59, 0x65, 0x51, 0x57, 0x4e, 0x53, 0x30, + 0x66, 0x4e, 0x57, 0x69, 0x52, 0x69, 0x6e, 0x43, 0x64, 0x6c, 0x32, 0x33, + 0x41, 0x37, 0x75, 0x73, 0x44, 0x47, 0x53, 0x75, 0x4b, 0x4b, 0x6c, 0x6d, + 0x30, 0x31, 0x39, 0x69, 0x6f, 0x6d, 0x4a, 0x2f, 0x52, 0x67, 0x64, 0x0a, + 0x4d, 0x4b, 0x44, 0x4f, 0x70, 0x30, 0x71, 0x2f, 0x32, 0x4f, 0x6f, 0x73, + 0x74, 0x62, 0x74, 0x65, 0x4f, 0x57, 0x4d, 0x32, 0x4d, 0x52, 0x46, 0x66, + 0x34, 0x6a, 0x4d, 0x48, 0x33, 0x77, 0x79, 0x56, 0x43, 0x77, 0x4a, 0x41, + 0x66, 0x41, 0x64, 0x6a, 0x4a, 0x38, 0x73, 0x7a, 0x6f, 0x4e, 0x4b, 0x54, + 0x52, 0x53, 0x61, 0x67, 0x53, 0x62, 0x68, 0x39, 0x76, 0x57, 0x79, 0x67, + 0x6e, 0x42, 0x32, 0x76, 0x0a, 0x49, 0x42, 0x79, 0x63, 0x36, 0x6c, 0x34, + 0x54, 0x54, 0x75, 0x5a, 0x51, 0x4a, 0x52, 0x47, 0x7a, 0x43, 0x76, 0x65, + 0x61, 0x66, 0x7a, 0x39, 0x6c, 0x6f, 0x76, 0x75, 0x42, 0x33, 0x57, 0x6f, + 0x68, 0x43, 0x41, 0x42, 0x64, 0x51, 0x52, 0x64, 0x39, 0x75, 0x6b, 0x43, + 0x58, 0x4c, 0x32, 0x43, 0x70, 0x73, 0x45, 0x70, 0x71, 0x7a, 0x6b, 0x61, + 0x66, 0x4f, 0x51, 0x4a, 0x41, 0x4a, 0x55, 0x6a, 0x63, 0x0a, 0x55, 0x53, + 0x65, 0x64, 0x44, 0x6c, 0x71, 0x33, 0x7a, 0x47, 0x5a, 0x77, 0x59, 0x4d, + 0x31, 0x59, 0x77, 0x38, 0x64, 0x38, 0x52, 0x75, 0x69, 0x72, 0x42, 0x55, + 0x46, 0x5a, 0x4e, 0x71, 0x4a, 0x65, 0x6c, 0x59, 0x61, 0x69, 0x2b, 0x6e, + 0x52, 0x59, 0x43, 0x6c, 0x44, 0x6b, 0x52, 0x56, 0x46, 0x67, 0x62, 0x35, + 0x79, 0x6b, 0x73, 0x6f, 0x59, 0x79, 0x63, 0x62, 0x71, 0x35, 0x54, 0x78, + 0x47, 0x6f, 0x0a, 0x56, 0x65, 0x71, 0x4b, 0x4f, 0x76, 0x67, 0x50, 0x70, + 0x6a, 0x34, 0x52, 0x57, 0x50, 0x48, 0x6c, 0x4c, 0x77, 0x4a, 0x41, 0x47, + 0x55, 0x4d, 0x6b, 0x33, 0x62, 0x71, 0x54, 0x39, 0x31, 0x78, 0x42, 0x55, + 0x43, 0x6e, 0x4c, 0x52, 0x73, 0x2f, 0x76, 0x66, 0x6f, 0x43, 0x70, 0x48, + 0x70, 0x67, 0x36, 0x65, 0x79, 0x77, 0x51, 0x54, 0x42, 0x44, 0x41, 0x56, + 0x36, 0x78, 0x6b, 0x79, 0x7a, 0x34, 0x61, 0x0a, 0x52, 0x48, 0x33, 0x49, + 0x37, 0x2f, 0x2b, 0x79, 0x6a, 0x33, 0x5a, 0x78, 0x52, 0x32, 0x4a, 0x6f, + 0x57, 0x48, 0x67, 0x55, 0x77, 0x5a, 0x37, 0x6c, 0x5a, 0x6b, 0x31, 0x56, + 0x6e, 0x68, 0x66, 0x66, 0x46, 0x79, 0x65, 0x37, 0x53, 0x42, 0x58, 0x79, + 0x61, 0x67, 0x3d, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, + 0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, + 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00 +}; diff --git a/test/core/end2end/data/test_root_cert.c b/test/core/end2end/data/test_root_cert.c index 58d9805017..f8e69e61b2 100644 --- a/test/core/end2end/data/test_root_cert.c +++ b/test/core/end2end/data/test_root_cert.c @@ -32,70 +32,71 @@ */ const char test_root_cert[] = { - 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, - 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, - 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x49, 0x7a, 0x43, 0x43, - 0x41, 0x59, 0x77, 0x43, 0x43, 0x51, 0x43, 0x46, 0x54, 0x62, 0x46, 0x37, - 0x58, 0x4e, 0x53, 0x76, 0x76, 0x6a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71, - 0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x55, 0x46, - 0x41, 0x44, 0x42, 0x57, 0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, - 0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x42, 0x0a, 0x56, 0x54, 0x45, - 0x54, 0x4d, 0x42, 0x45, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, - 0x4b, 0x55, 0x32, 0x39, 0x74, 0x5a, 0x53, 0x31, 0x54, 0x64, 0x47, 0x46, - 0x30, 0x5a, 0x54, 0x45, 0x68, 0x4d, 0x42, 0x38, 0x47, 0x41, 0x31, 0x55, - 0x45, 0x43, 0x67, 0x77, 0x59, 0x53, 0x57, 0x35, 0x30, 0x5a, 0x58, 0x4a, - 0x75, 0x5a, 0x58, 0x51, 0x67, 0x56, 0x32, 0x6c, 0x6b, 0x5a, 0x32, 0x6c, - 0x30, 0x0a, 0x63, 0x79, 0x42, 0x51, 0x64, 0x48, 0x6b, 0x67, 0x54, 0x48, - 0x52, 0x6b, 0x4d, 0x51, 0x38, 0x77, 0x44, 0x51, 0x59, 0x44, 0x56, 0x51, - 0x51, 0x44, 0x44, 0x41, 0x5a, 0x30, 0x5a, 0x58, 0x4e, 0x30, 0x59, 0x32, - 0x45, 0x77, 0x48, 0x68, 0x63, 0x4e, 0x4d, 0x54, 0x51, 0x77, 0x4e, 0x7a, - 0x45, 0x33, 0x4d, 0x6a, 0x4d, 0x78, 0x4e, 0x7a, 0x55, 0x78, 0x57, 0x68, - 0x63, 0x4e, 0x4d, 0x6a, 0x51, 0x77, 0x0a, 0x4e, 0x7a, 0x45, 0x30, 0x4d, - 0x6a, 0x4d, 0x78, 0x4e, 0x7a, 0x55, 0x78, 0x57, 0x6a, 0x42, 0x57, 0x4d, - 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x45, - 0x77, 0x4a, 0x42, 0x56, 0x54, 0x45, 0x54, 0x4d, 0x42, 0x45, 0x47, 0x41, - 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x4b, 0x55, 0x32, 0x39, 0x74, 0x5a, - 0x53, 0x31, 0x54, 0x64, 0x47, 0x46, 0x30, 0x5a, 0x54, 0x45, 0x68, 0x0a, - 0x4d, 0x42, 0x38, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x67, 0x77, 0x59, - 0x53, 0x57, 0x35, 0x30, 0x5a, 0x58, 0x4a, 0x75, 0x5a, 0x58, 0x51, 0x67, - 0x56, 0x32, 0x6c, 0x6b, 0x5a, 0x32, 0x6c, 0x30, 0x63, 0x79, 0x42, 0x51, - 0x64, 0x48, 0x6b, 0x67, 0x54, 0x48, 0x52, 0x6b, 0x4d, 0x51, 0x38, 0x77, - 0x44, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x44, 0x41, 0x5a, 0x30, - 0x5a, 0x58, 0x4e, 0x30, 0x0a, 0x59, 0x32, 0x45, 0x77, 0x67, 0x5a, 0x38, - 0x77, 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, - 0x4e, 0x41, 0x51, 0x45, 0x42, 0x42, 0x51, 0x41, 0x44, 0x67, 0x59, 0x30, - 0x41, 0x4d, 0x49, 0x47, 0x4a, 0x41, 0x6f, 0x47, 0x42, 0x41, 0x4d, 0x42, - 0x41, 0x33, 0x77, 0x56, 0x65, 0x54, 0x47, 0x48, 0x5a, 0x52, 0x31, 0x52, - 0x79, 0x65, 0x2f, 0x69, 0x2b, 0x4a, 0x38, 0x61, 0x32, 0x0a, 0x63, 0x75, - 0x35, 0x67, 0x58, 0x77, 0x46, 0x56, 0x36, 0x54, 0x6e, 0x4f, 0x62, 0x7a, - 0x47, 0x4d, 0x37, 0x62, 0x4c, 0x46, 0x43, 0x4f, 0x35, 0x69, 0x39, 0x76, - 0x34, 0x6d, 0x4c, 0x6f, 0x34, 0x69, 0x46, 0x7a, 0x50, 0x73, 0x48, 0x6d, - 0x57, 0x44, 0x55, 0x78, 0x4b, 0x53, 0x33, 0x59, 0x38, 0x69, 0x58, 0x62, - 0x75, 0x30, 0x65, 0x59, 0x42, 0x6c, 0x4c, 0x6f, 0x4e, 0x59, 0x30, 0x6c, - 0x53, 0x76, 0x0a, 0x78, 0x44, 0x78, 0x33, 0x33, 0x4f, 0x2b, 0x44, 0x75, - 0x77, 0x4d, 0x6d, 0x56, 0x4e, 0x2b, 0x44, 0x7a, 0x53, 0x44, 0x2b, 0x45, - 0x6f, 0x64, 0x39, 0x7a, 0x66, 0x76, 0x77, 0x4f, 0x57, 0x48, 0x73, 0x61, - 0x7a, 0x59, 0x43, 0x5a, 0x54, 0x32, 0x50, 0x68, 0x4e, 0x78, 0x6e, 0x56, - 0x57, 0x49, 0x75, 0x4a, 0x58, 0x56, 0x69, 0x59, 0x34, 0x4a, 0x41, 0x48, - 0x55, 0x47, 0x6f, 0x64, 0x6a, 0x78, 0x2b, 0x0a, 0x51, 0x41, 0x69, 0x36, - 0x79, 0x43, 0x41, 0x75, 0x72, 0x55, 0x5a, 0x47, 0x76, 0x59, 0x58, 0x47, - 0x67, 0x5a, 0x53, 0x42, 0x41, 0x67, 0x4d, 0x42, 0x41, 0x41, 0x45, 0x77, - 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, - 0x41, 0x51, 0x45, 0x46, 0x42, 0x51, 0x41, 0x44, 0x67, 0x59, 0x45, 0x41, - 0x51, 0x6f, 0x51, 0x56, 0x44, 0x38, 0x62, 0x77, 0x64, 0x74, 0x57, 0x4a, - 0x0a, 0x41, 0x6e, 0x69, 0x47, 0x42, 0x77, 0x63, 0x43, 0x66, 0x71, 0x59, - 0x79, 0x48, 0x2b, 0x2f, 0x4b, 0x70, 0x41, 0x31, 0x30, 0x41, 0x63, 0x65, - 0x62, 0x4a, 0x56, 0x56, 0x54, 0x79, 0x59, 0x62, 0x59, 0x76, 0x49, 0x39, - 0x51, 0x38, 0x64, 0x36, 0x52, 0x53, 0x56, 0x75, 0x34, 0x50, 0x5a, 0x79, - 0x39, 0x4f, 0x41, 0x4c, 0x48, 0x52, 0x2f, 0x51, 0x72, 0x57, 0x42, 0x64, - 0x59, 0x54, 0x41, 0x79, 0x7a, 0x0a, 0x66, 0x4e, 0x41, 0x6d, 0x63, 0x32, - 0x63, 0x6d, 0x64, 0x6b, 0x53, 0x52, 0x4a, 0x7a, 0x6a, 0x68, 0x49, 0x61, - 0x4f, 0x73, 0x74, 0x6e, 0x51, 0x79, 0x31, 0x4a, 0x2b, 0x46, 0x6b, 0x30, - 0x54, 0x39, 0x58, 0x79, 0x76, 0x51, 0x74, 0x71, 0x34, 0x39, 0x39, 0x79, - 0x46, 0x62, 0x71, 0x39, 0x78, 0x6f, 0x67, 0x55, 0x56, 0x6c, 0x45, 0x47, - 0x48, 0x36, 0x32, 0x78, 0x50, 0x36, 0x76, 0x48, 0x30, 0x59, 0x0a, 0x35, - 0x75, 0x6b, 0x4b, 0x2f, 0x2f, 0x64, 0x43, 0x50, 0x41, 0x7a, 0x41, 0x31, - 0x31, 0x59, 0x75, 0x58, 0x32, 0x72, 0x6e, 0x65, 0x78, 0x30, 0x4a, 0x68, - 0x75, 0x54, 0x51, 0x66, 0x63, 0x49, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, - 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, - 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00}; + 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, + 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, + 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x49, 0x7a, 0x43, 0x43, + 0x41, 0x59, 0x77, 0x43, 0x43, 0x51, 0x43, 0x46, 0x54, 0x62, 0x46, 0x37, + 0x58, 0x4e, 0x53, 0x76, 0x76, 0x6a, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71, + 0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x55, 0x46, + 0x41, 0x44, 0x42, 0x57, 0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, + 0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x42, 0x0a, 0x56, 0x54, 0x45, + 0x54, 0x4d, 0x42, 0x45, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, + 0x4b, 0x55, 0x32, 0x39, 0x74, 0x5a, 0x53, 0x31, 0x54, 0x64, 0x47, 0x46, + 0x30, 0x5a, 0x54, 0x45, 0x68, 0x4d, 0x42, 0x38, 0x47, 0x41, 0x31, 0x55, + 0x45, 0x43, 0x67, 0x77, 0x59, 0x53, 0x57, 0x35, 0x30, 0x5a, 0x58, 0x4a, + 0x75, 0x5a, 0x58, 0x51, 0x67, 0x56, 0x32, 0x6c, 0x6b, 0x5a, 0x32, 0x6c, + 0x30, 0x0a, 0x63, 0x79, 0x42, 0x51, 0x64, 0x48, 0x6b, 0x67, 0x54, 0x48, + 0x52, 0x6b, 0x4d, 0x51, 0x38, 0x77, 0x44, 0x51, 0x59, 0x44, 0x56, 0x51, + 0x51, 0x44, 0x44, 0x41, 0x5a, 0x30, 0x5a, 0x58, 0x4e, 0x30, 0x59, 0x32, + 0x45, 0x77, 0x48, 0x68, 0x63, 0x4e, 0x4d, 0x54, 0x51, 0x77, 0x4e, 0x7a, + 0x45, 0x33, 0x4d, 0x6a, 0x4d, 0x78, 0x4e, 0x7a, 0x55, 0x78, 0x57, 0x68, + 0x63, 0x4e, 0x4d, 0x6a, 0x51, 0x77, 0x0a, 0x4e, 0x7a, 0x45, 0x30, 0x4d, + 0x6a, 0x4d, 0x78, 0x4e, 0x7a, 0x55, 0x78, 0x57, 0x6a, 0x42, 0x57, 0x4d, + 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x45, + 0x77, 0x4a, 0x42, 0x56, 0x54, 0x45, 0x54, 0x4d, 0x42, 0x45, 0x47, 0x41, + 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x4b, 0x55, 0x32, 0x39, 0x74, 0x5a, + 0x53, 0x31, 0x54, 0x64, 0x47, 0x46, 0x30, 0x5a, 0x54, 0x45, 0x68, 0x0a, + 0x4d, 0x42, 0x38, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x67, 0x77, 0x59, + 0x53, 0x57, 0x35, 0x30, 0x5a, 0x58, 0x4a, 0x75, 0x5a, 0x58, 0x51, 0x67, + 0x56, 0x32, 0x6c, 0x6b, 0x5a, 0x32, 0x6c, 0x30, 0x63, 0x79, 0x42, 0x51, + 0x64, 0x48, 0x6b, 0x67, 0x54, 0x48, 0x52, 0x6b, 0x4d, 0x51, 0x38, 0x77, + 0x44, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x44, 0x41, 0x5a, 0x30, + 0x5a, 0x58, 0x4e, 0x30, 0x0a, 0x59, 0x32, 0x45, 0x77, 0x67, 0x5a, 0x38, + 0x77, 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, + 0x4e, 0x41, 0x51, 0x45, 0x42, 0x42, 0x51, 0x41, 0x44, 0x67, 0x59, 0x30, + 0x41, 0x4d, 0x49, 0x47, 0x4a, 0x41, 0x6f, 0x47, 0x42, 0x41, 0x4d, 0x42, + 0x41, 0x33, 0x77, 0x56, 0x65, 0x54, 0x47, 0x48, 0x5a, 0x52, 0x31, 0x52, + 0x79, 0x65, 0x2f, 0x69, 0x2b, 0x4a, 0x38, 0x61, 0x32, 0x0a, 0x63, 0x75, + 0x35, 0x67, 0x58, 0x77, 0x46, 0x56, 0x36, 0x54, 0x6e, 0x4f, 0x62, 0x7a, + 0x47, 0x4d, 0x37, 0x62, 0x4c, 0x46, 0x43, 0x4f, 0x35, 0x69, 0x39, 0x76, + 0x34, 0x6d, 0x4c, 0x6f, 0x34, 0x69, 0x46, 0x7a, 0x50, 0x73, 0x48, 0x6d, + 0x57, 0x44, 0x55, 0x78, 0x4b, 0x53, 0x33, 0x59, 0x38, 0x69, 0x58, 0x62, + 0x75, 0x30, 0x65, 0x59, 0x42, 0x6c, 0x4c, 0x6f, 0x4e, 0x59, 0x30, 0x6c, + 0x53, 0x76, 0x0a, 0x78, 0x44, 0x78, 0x33, 0x33, 0x4f, 0x2b, 0x44, 0x75, + 0x77, 0x4d, 0x6d, 0x56, 0x4e, 0x2b, 0x44, 0x7a, 0x53, 0x44, 0x2b, 0x45, + 0x6f, 0x64, 0x39, 0x7a, 0x66, 0x76, 0x77, 0x4f, 0x57, 0x48, 0x73, 0x61, + 0x7a, 0x59, 0x43, 0x5a, 0x54, 0x32, 0x50, 0x68, 0x4e, 0x78, 0x6e, 0x56, + 0x57, 0x49, 0x75, 0x4a, 0x58, 0x56, 0x69, 0x59, 0x34, 0x4a, 0x41, 0x48, + 0x55, 0x47, 0x6f, 0x64, 0x6a, 0x78, 0x2b, 0x0a, 0x51, 0x41, 0x69, 0x36, + 0x79, 0x43, 0x41, 0x75, 0x72, 0x55, 0x5a, 0x47, 0x76, 0x59, 0x58, 0x47, + 0x67, 0x5a, 0x53, 0x42, 0x41, 0x67, 0x4d, 0x42, 0x41, 0x41, 0x45, 0x77, + 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, + 0x41, 0x51, 0x45, 0x46, 0x42, 0x51, 0x41, 0x44, 0x67, 0x59, 0x45, 0x41, + 0x51, 0x6f, 0x51, 0x56, 0x44, 0x38, 0x62, 0x77, 0x64, 0x74, 0x57, 0x4a, + 0x0a, 0x41, 0x6e, 0x69, 0x47, 0x42, 0x77, 0x63, 0x43, 0x66, 0x71, 0x59, + 0x79, 0x48, 0x2b, 0x2f, 0x4b, 0x70, 0x41, 0x31, 0x30, 0x41, 0x63, 0x65, + 0x62, 0x4a, 0x56, 0x56, 0x54, 0x79, 0x59, 0x62, 0x59, 0x76, 0x49, 0x39, + 0x51, 0x38, 0x64, 0x36, 0x52, 0x53, 0x56, 0x75, 0x34, 0x50, 0x5a, 0x79, + 0x39, 0x4f, 0x41, 0x4c, 0x48, 0x52, 0x2f, 0x51, 0x72, 0x57, 0x42, 0x64, + 0x59, 0x54, 0x41, 0x79, 0x7a, 0x0a, 0x66, 0x4e, 0x41, 0x6d, 0x63, 0x32, + 0x63, 0x6d, 0x64, 0x6b, 0x53, 0x52, 0x4a, 0x7a, 0x6a, 0x68, 0x49, 0x61, + 0x4f, 0x73, 0x74, 0x6e, 0x51, 0x79, 0x31, 0x4a, 0x2b, 0x46, 0x6b, 0x30, + 0x54, 0x39, 0x58, 0x79, 0x76, 0x51, 0x74, 0x71, 0x34, 0x39, 0x39, 0x79, + 0x46, 0x62, 0x71, 0x39, 0x78, 0x6f, 0x67, 0x55, 0x56, 0x6c, 0x45, 0x47, + 0x48, 0x36, 0x32, 0x78, 0x50, 0x36, 0x76, 0x48, 0x30, 0x59, 0x0a, 0x35, + 0x75, 0x6b, 0x4b, 0x2f, 0x2f, 0x64, 0x43, 0x50, 0x41, 0x7a, 0x41, 0x31, + 0x31, 0x59, 0x75, 0x58, 0x32, 0x72, 0x6e, 0x65, 0x78, 0x30, 0x4a, 0x68, + 0x75, 0x54, 0x51, 0x66, 0x63, 0x49, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, + 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, + 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00 +}; diff --git a/test/core/end2end/dualstack_socket_test.c b/test/core/end2end/dualstack_socket_test.c index ec98cf39b4..5da851bbcd 100644 --- a/test/core/end2end/dualstack_socket_test.c +++ b/test/core/end2end/dualstack_socket_test.c @@ -49,22 +49,37 @@ /* This test exercises IPv4, IPv6, and dualstack sockets in various ways. */ -static void *tag(gpr_intptr i) { return (void *)i; } +static void * +tag (gpr_intptr i) +{ + return (void *) i; +} -static gpr_timespec ms_from_now(int ms) { - return GRPC_TIMEOUT_MILLIS_TO_DEADLINE(ms); +static gpr_timespec +ms_from_now (int ms) +{ + return GRPC_TIMEOUT_MILLIS_TO_DEADLINE (ms); } -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, ms_from_now(5000), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, ms_from_now (5000), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); +} + +static void +do_nothing (void *ignored) +{ } -static void do_nothing(void *ignored) {} -void test_connect(const char *server_host, const char *client_host, int port, - int expect_ok) { +void +test_connect (const char *server_host, const char *client_host, int port, int expect_ok) +{ char *client_hostport; char *server_hostport; grpc_channel *client; @@ -88,81 +103,88 @@ void test_connect(const char *server_host, const char *client_host, int port, grpc_call_details call_details; char *peer; - if (port == 0) { - port = grpc_pick_unused_port_or_die(); - } + if (port == 0) + { + port = grpc_pick_unused_port_or_die (); + } - gpr_join_host_port(&server_hostport, server_host, port); + gpr_join_host_port (&server_hostport, server_host, port); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); /* Create server. */ - cq = grpc_completion_queue_create(NULL); - server = grpc_server_create(NULL, NULL); - grpc_server_register_completion_queue(server, cq, NULL); - GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port( - server, server_hostport)) > 0); - if (port == 0) { - port = got_port; - } else { - GPR_ASSERT(port == got_port); - } - grpc_server_start(server); - cqv = cq_verifier_create(cq); + cq = grpc_completion_queue_create (NULL); + server = grpc_server_create (NULL, NULL); + grpc_server_register_completion_queue (server, cq, NULL); + GPR_ASSERT ((got_port = grpc_server_add_insecure_http2_port (server, server_hostport)) > 0); + if (port == 0) + { + port = got_port; + } + else + { + GPR_ASSERT (port == got_port); + } + grpc_server_start (server); + cqv = cq_verifier_create (cq); /* Create client. */ - if (client_host[0] == 'i') { - /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */ - size_t i; - gpr_slice uri_slice; - gpr_slice_buffer uri_parts; - char **hosts_with_port; - - uri_slice = - gpr_slice_new((char *)client_host, strlen(client_host), do_nothing); - gpr_slice_buffer_init(&uri_parts); - gpr_slice_split(uri_slice, ",", &uri_parts); - hosts_with_port = gpr_malloc(sizeof(char *) * uri_parts.count); - for (i = 0; i < uri_parts.count; i++) { - char *uri_part_str = gpr_dump_slice(uri_parts.slices[i], GPR_DUMP_ASCII); - gpr_asprintf(&hosts_with_port[i], "%s:%d", uri_part_str, port); - gpr_free(uri_part_str); + if (client_host[0] == 'i') + { + /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */ + size_t i; + gpr_slice uri_slice; + gpr_slice_buffer uri_parts; + char **hosts_with_port; + + uri_slice = gpr_slice_new ((char *) client_host, strlen (client_host), do_nothing); + gpr_slice_buffer_init (&uri_parts); + gpr_slice_split (uri_slice, ",", &uri_parts); + hosts_with_port = gpr_malloc (sizeof (char *) * uri_parts.count); + for (i = 0; i < uri_parts.count; i++) + { + char *uri_part_str = gpr_dump_slice (uri_parts.slices[i], GPR_DUMP_ASCII); + gpr_asprintf (&hosts_with_port[i], "%s:%d", uri_part_str, port); + gpr_free (uri_part_str); + } + client_hostport = gpr_strjoin_sep ((const char **) hosts_with_port, uri_parts.count, ",", NULL); + for (i = 0; i < uri_parts.count; i++) + { + gpr_free (hosts_with_port[i]); + } + gpr_free (hosts_with_port); + gpr_slice_buffer_destroy (&uri_parts); + gpr_slice_unref (uri_slice); + } + else + { + gpr_join_host_port (&client_hostport, client_host, port); + } + client = grpc_insecure_channel_create (client_hostport, NULL, NULL); + + gpr_log (GPR_INFO, "Testing with server=%s client=%s (expecting %s)", server_hostport, client_hostport, expect_ok ? "success" : "failure"); + + gpr_free (client_hostport); + gpr_free (server_hostport); + + if (expect_ok) + { + /* Normal deadline, shouldn't be reached. */ + deadline = ms_from_now (60000); } - client_hostport = gpr_strjoin_sep((const char **)hosts_with_port, - uri_parts.count, ",", NULL); - for (i = 0; i < uri_parts.count; i++) { - gpr_free(hosts_with_port[i]); + else + { + /* Give up faster when failure is expected. + BUG: Setting this to 1000 reveals a memory leak (b/18608927). */ + deadline = ms_from_now (1500); } - gpr_free(hosts_with_port); - gpr_slice_buffer_destroy(&uri_parts); - gpr_slice_unref(uri_slice); - } else { - gpr_join_host_port(&client_hostport, client_host, port); - } - client = grpc_insecure_channel_create(client_hostport, NULL, NULL); - - gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)", - server_hostport, client_hostport, expect_ok ? "success" : "failure"); - - gpr_free(client_hostport); - gpr_free(server_hostport); - - if (expect_ok) { - /* Normal deadline, shouldn't be reached. */ - deadline = ms_from_now(60000); - } else { - /* Give up faster when failure is expected. - BUG: Setting this to 1000 reveals a memory leak (b/18608927). */ - deadline = ms_from_now(1500); - } /* Send a trivial request. */ - c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (client, NULL, GRPC_PROPAGATE_DEFAULTS, cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -187,159 +209,164 @@ void test_connect(const char *server_host, const char *client_host, int port, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - if (expect_ok) { - /* Check for a successful request. */ - error = grpc_server_request_call(server, &s, &call_details, - &request_metadata_recv, cq, cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); - - op = ops; - op->op = GRPC_OP_SEND_INITIAL_METADATA; - op->data.send_initial_metadata.count = 0; - op->flags = 0; - op++; - op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; - op->data.send_status_from_server.trailing_metadata_count = 0; - op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; - op->data.send_status_from_server.status_details = "xyz"; - op->flags = 0; - op++; - op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; - op->data.recv_close_on_server.cancelled = &was_cancelled; - op->flags = 0; - op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); - - peer = grpc_call_get_peer(c); - gpr_log(GPR_DEBUG, "got peer: '%s'", peer); - gpr_free(peer); - - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 1); - - grpc_call_destroy(s); - } else { - /* Check for a failed connection. */ - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); - - GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED); - } - - grpc_call_destroy(c); - - cq_verifier_destroy(cqv); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + if (expect_ok) + { + /* Check for a successful request. */ + error = grpc_server_request_call (server, &s, &call_details, &request_metadata_recv, cq, cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); + + op = ops; + op->op = GRPC_OP_SEND_INITIAL_METADATA; + op->data.send_initial_metadata.count = 0; + op->flags = 0; + op++; + op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; + op->data.send_status_from_server.trailing_metadata_count = 0; + op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; + op->data.send_status_from_server.status_details = "xyz"; + op->flags = 0; + op++; + op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; + op->data.recv_close_on_server.cancelled = &was_cancelled; + op->flags = 0; + op++; + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); + + peer = grpc_call_get_peer (c); + gpr_log (GPR_DEBUG, "got peer: '%s'", peer); + gpr_free (peer); + + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 1); + + grpc_call_destroy (s); + } + else + { + /* Check for a failed connection. */ + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); + + GPR_ASSERT (status == GRPC_STATUS_DEADLINE_EXCEEDED); + } + + grpc_call_destroy (c); + + cq_verifier_destroy (cqv); /* Destroy client. */ - grpc_channel_destroy(client); + grpc_channel_destroy (client); /* Destroy server. */ - grpc_server_shutdown_and_notify(server, cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(server); - grpc_completion_queue_shutdown(cq); - drain_cq(cq); - grpc_completion_queue_destroy(cq); - - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - - grpc_call_details_destroy(&call_details); - gpr_free(details); + grpc_server_shutdown_and_notify (server, cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (server); + grpc_completion_queue_shutdown (cq); + drain_cq (cq); + grpc_completion_queue_destroy (cq); + + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + + grpc_call_details_destroy (&call_details); + gpr_free (details); } -int external_dns_works(const char *host) { - grpc_resolved_addresses *res = grpc_blocking_resolve_address(host, "80"); - if (res != NULL) { - grpc_resolved_addresses_destroy(res); - return 1; - } +int +external_dns_works (const char *host) +{ + grpc_resolved_addresses *res = grpc_blocking_resolve_address (host, "80"); + if (res != NULL) + { + grpc_resolved_addresses_destroy (res); + return 1; + } return 0; } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ int do_ipv6 = 1; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - if (!grpc_ipv6_loopback_available()) { - gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests."); - do_ipv6 = 0; - } - - /* For coverage, test with and without dualstack sockets. */ - for (grpc_forbid_dualstack_sockets_for_testing = 0; - grpc_forbid_dualstack_sockets_for_testing <= 1; - grpc_forbid_dualstack_sockets_for_testing++) { - /* :: and 0.0.0.0 are handled identically. */ - test_connect("::", "127.0.0.1", 0, 1); - test_connect("::", "::ffff:127.0.0.1", 0, 1); - test_connect("::", "ipv4:127.0.0.1", 0, 1); - test_connect("::", "ipv6:[::ffff:127.0.0.1]", 0, 1); - test_connect("::", "localhost", 0, 1); - test_connect("0.0.0.0", "127.0.0.1", 0, 1); - test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1); - test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1); - test_connect("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1); - test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1); - test_connect("0.0.0.0", "localhost", 0, 1); - if (do_ipv6) { - test_connect("::", "::1", 0, 1); - test_connect("0.0.0.0", "::1", 0, 1); - test_connect("::", "ipv6:[::1]", 0, 1); - test_connect("0.0.0.0", "ipv6:[::1]", 0, 1); + if (!grpc_ipv6_loopback_available ()) + { + gpr_log (GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests."); + do_ipv6 = 0; } - /* These only work when the families agree. */ - test_connect("127.0.0.1", "127.0.0.1", 0, 1); - test_connect("127.0.0.1", "ipv4:127.0.0.1", 0, 1); - if (do_ipv6) { - test_connect("::1", "::1", 0, 1); - test_connect("::1", "127.0.0.1", 0, 0); - test_connect("127.0.0.1", "::1", 0, 0); - test_connect("::1", "ipv6:[::1]", 0, 1); - test_connect("::1", "ipv4:127.0.0.1", 0, 0); - test_connect("127.0.0.1", "ipv6:[::1]", 0, 0); - } - - if (!external_dns_works("loopback46.unittest.grpc.io")) { - gpr_log(GPR_INFO, "Skipping tests that depend on *.unittest.grpc.io."); - } else { - test_connect("loopback46.unittest.grpc.io", "loopback4.unittest.grpc.io", - 0, 1); - test_connect("loopback4.unittest.grpc.io", "loopback46.unittest.grpc.io", - 0, 1); - if (do_ipv6) { - test_connect("loopback46.unittest.grpc.io", - "loopback6.unittest.grpc.io", 0, 1); - test_connect("loopback6.unittest.grpc.io", - "loopback46.unittest.grpc.io", 0, 1); - test_connect("loopback4.unittest.grpc.io", "loopback6.unittest.grpc.io", - 0, 0); - test_connect("loopback6.unittest.grpc.io", "loopback4.unittest.grpc.io", - 0, 0); - } + /* For coverage, test with and without dualstack sockets. */ + for (grpc_forbid_dualstack_sockets_for_testing = 0; grpc_forbid_dualstack_sockets_for_testing <= 1; grpc_forbid_dualstack_sockets_for_testing++) + { + /* :: and 0.0.0.0 are handled identically. */ + test_connect ("::", "127.0.0.1", 0, 1); + test_connect ("::", "::ffff:127.0.0.1", 0, 1); + test_connect ("::", "ipv4:127.0.0.1", 0, 1); + test_connect ("::", "ipv6:[::ffff:127.0.0.1]", 0, 1); + test_connect ("::", "localhost", 0, 1); + test_connect ("0.0.0.0", "127.0.0.1", 0, 1); + test_connect ("0.0.0.0", "::ffff:127.0.0.1", 0, 1); + test_connect ("0.0.0.0", "ipv4:127.0.0.1", 0, 1); + test_connect ("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1); + test_connect ("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1); + test_connect ("0.0.0.0", "localhost", 0, 1); + if (do_ipv6) + { + test_connect ("::", "::1", 0, 1); + test_connect ("0.0.0.0", "::1", 0, 1); + test_connect ("::", "ipv6:[::1]", 0, 1); + test_connect ("0.0.0.0", "ipv6:[::1]", 0, 1); + } + + /* These only work when the families agree. */ + test_connect ("127.0.0.1", "127.0.0.1", 0, 1); + test_connect ("127.0.0.1", "ipv4:127.0.0.1", 0, 1); + if (do_ipv6) + { + test_connect ("::1", "::1", 0, 1); + test_connect ("::1", "127.0.0.1", 0, 0); + test_connect ("127.0.0.1", "::1", 0, 0); + test_connect ("::1", "ipv6:[::1]", 0, 1); + test_connect ("::1", "ipv4:127.0.0.1", 0, 0); + test_connect ("127.0.0.1", "ipv6:[::1]", 0, 0); + } + + if (!external_dns_works ("loopback46.unittest.grpc.io")) + { + gpr_log (GPR_INFO, "Skipping tests that depend on *.unittest.grpc.io."); + } + else + { + test_connect ("loopback46.unittest.grpc.io", "loopback4.unittest.grpc.io", 0, 1); + test_connect ("loopback4.unittest.grpc.io", "loopback46.unittest.grpc.io", 0, 1); + if (do_ipv6) + { + test_connect ("loopback46.unittest.grpc.io", "loopback6.unittest.grpc.io", 0, 1); + test_connect ("loopback6.unittest.grpc.io", "loopback46.unittest.grpc.io", 0, 1); + test_connect ("loopback4.unittest.grpc.io", "loopback6.unittest.grpc.io", 0, 0); + test_connect ("loopback6.unittest.grpc.io", "loopback4.unittest.grpc.io", 0, 0); + } + } } - } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/end2end_tests.h b/test/core/end2end/end2end_tests.h index 3f1665613c..4f653ec868 100644 --- a/test/core/end2end/end2end_tests.h +++ b/test/core/end2end/end2end_tests.h @@ -45,25 +45,24 @@ typedef struct grpc_end2end_test_config grpc_end2end_test_config; #define FAIL_AUTH_CHECK_SERVER_ARG_NAME "fail_auth_check" -struct grpc_end2end_test_fixture { +struct grpc_end2end_test_fixture +{ grpc_completion_queue *cq; grpc_server *server; grpc_channel *client; void *fixture_data; }; -struct grpc_end2end_test_config { +struct grpc_end2end_test_config +{ const char *name; gpr_uint32 feature_mask; - grpc_end2end_test_fixture (*create_fixture)(grpc_channel_args *client_args, - grpc_channel_args *server_args); - void (*init_client)(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args); - void (*init_server)(grpc_end2end_test_fixture *f, - grpc_channel_args *server_args); - void (*tear_down_data)(grpc_end2end_test_fixture *f); + grpc_end2end_test_fixture (*create_fixture) (grpc_channel_args * client_args, grpc_channel_args * server_args); + void (*init_client) (grpc_end2end_test_fixture * f, grpc_channel_args * client_args); + void (*init_server) (grpc_end2end_test_fixture * f, grpc_channel_args * server_args); + void (*tear_down_data) (grpc_end2end_test_fixture * f); }; -void grpc_end2end_tests(grpc_end2end_test_config config); +void grpc_end2end_tests (grpc_end2end_test_config config); #endif /* GRPC_TEST_CORE_END2END_END2END_TESTS_H */ diff --git a/test/core/end2end/fixtures/h2_compress.c b/test/core/end2end/fixtures/h2_compress.c index 6e7a1faf55..7117777bfa 100644 --- a/test/core/end2end/fixtures/h2_compress.c +++ b/test/core/end2end/fixtures/h2_compress.c @@ -51,86 +51,94 @@ #include "test/core/util/port.h" #include "test/core/util/test_config.h" -typedef struct fullstack_compression_fixture_data { +typedef struct fullstack_compression_fixture_data +{ char *localaddr; grpc_channel_args *client_args_compression; grpc_channel_args *server_args_compression; } fullstack_compression_fixture_data; -static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_compression( - grpc_channel_args *client_args, grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +chttp2_create_fixture_fullstack_compression (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - int port = grpc_pick_unused_port_or_die(); - fullstack_compression_fixture_data *ffd = - gpr_malloc(sizeof(fullstack_compression_fixture_data)); - memset(ffd, 0, sizeof(fullstack_compression_fixture_data)); + int port = grpc_pick_unused_port_or_die (); + fullstack_compression_fixture_data *ffd = gpr_malloc (sizeof (fullstack_compression_fixture_data)); + memset (ffd, 0, sizeof (fullstack_compression_fixture_data)); - gpr_join_host_port(&ffd->localaddr, "localhost", port); + gpr_join_host_port (&ffd->localaddr, "localhost", port); - memset(&f, 0, sizeof(f)); + memset (&f, 0, sizeof (f)); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -void chttp2_init_client_fullstack_compression(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +void +chttp2_init_client_fullstack_compression (grpc_end2end_test_fixture * f, grpc_channel_args * client_args) +{ fullstack_compression_fixture_data *ffd = f->fixture_data; - if (ffd->client_args_compression != NULL) { - grpc_channel_args_destroy(ffd->client_args_compression); - } - ffd->client_args_compression = grpc_channel_args_set_compression_algorithm( - client_args, GRPC_COMPRESS_GZIP); - f->client = grpc_insecure_channel_create(ffd->localaddr, - ffd->client_args_compression, NULL); + if (ffd->client_args_compression != NULL) + { + grpc_channel_args_destroy (ffd->client_args_compression); + } + ffd->client_args_compression = grpc_channel_args_set_compression_algorithm (client_args, GRPC_COMPRESS_GZIP); + f->client = grpc_insecure_channel_create (ffd->localaddr, ffd->client_args_compression, NULL); } -void chttp2_init_server_fullstack_compression(grpc_end2end_test_fixture *f, - grpc_channel_args *server_args) { +void +chttp2_init_server_fullstack_compression (grpc_end2end_test_fixture * f, grpc_channel_args * server_args) +{ fullstack_compression_fixture_data *ffd = f->fixture_data; - if (ffd->server_args_compression != NULL) { - grpc_channel_args_destroy(ffd->server_args_compression); - } - ffd->server_args_compression = grpc_channel_args_set_compression_algorithm( - server_args, GRPC_COMPRESS_GZIP); - if (f->server) { - grpc_server_destroy(f->server); - } - f->server = grpc_server_create(ffd->server_args_compression, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr)); - grpc_server_start(f->server); + if (ffd->server_args_compression != NULL) + { + grpc_channel_args_destroy (ffd->server_args_compression); + } + ffd->server_args_compression = grpc_channel_args_set_compression_algorithm (server_args, GRPC_COMPRESS_GZIP); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (ffd->server_args_compression, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (f->server, ffd->localaddr)); + grpc_server_start (f->server); } -void chttp2_tear_down_fullstack_compression(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_fullstack_compression (grpc_end2end_test_fixture * f) +{ fullstack_compression_fixture_data *ffd = f->fixture_data; - grpc_channel_args_destroy(ffd->client_args_compression); - grpc_channel_args_destroy(ffd->server_args_compression); - gpr_free(ffd->localaddr); - gpr_free(ffd); + grpc_channel_args_destroy (ffd->client_args_compression); + grpc_channel_args_destroy (ffd->server_args_compression); + gpr_free (ffd->localaddr); + gpr_free (ffd); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/fullstack_compression", FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION, - chttp2_create_fixture_fullstack_compression, - chttp2_init_client_fullstack_compression, - chttp2_init_server_fullstack_compression, - chttp2_tear_down_fullstack_compression}, + {"chttp2/fullstack_compression", FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION, + chttp2_create_fixture_fullstack_compression, + chttp2_init_client_fullstack_compression, + chttp2_init_server_fullstack_compression, + chttp2_tear_down_fullstack_compression}, }; -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_fakesec.c b/test/core/end2end/fixtures/h2_fakesec.c index 3e64cc08e8..c663f40ede 100644 --- a/test/core/end2end/fixtures/h2_fakesec.c +++ b/test/core/end2end/fixtures/h2_fakesec.c @@ -45,119 +45,126 @@ #include "test/core/util/port.h" #include "test/core/end2end/data/ssl_test_data.h" -typedef struct fullstack_secure_fixture_data { +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) { +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)); + 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); + memset (&f, 0, sizeof (f)); + gpr_join_host_port (&ffd->localaddr, "localhost", port); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -static void process_auth_failure(void *state, grpc_auth_context *ctx, - const grpc_metadata *md, size_t md_count, - grpc_process_auth_metadata_done_cb cb, - void *user_data) { - GPR_ASSERT(state == NULL); - cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); +static void +process_auth_failure (void *state, grpc_auth_context * ctx, const grpc_metadata * md, size_t md_count, grpc_process_auth_metadata_done_cb cb, void *user_data) +{ + GPR_ASSERT (state == NULL); + cb (user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); } -static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args, - grpc_credentials *creds) { +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, NULL); - GPR_ASSERT(f->client != NULL); - grpc_credentials_release(creds); + f->client = grpc_secure_channel_create (creds, ffd->localaddr, client_args, NULL); + 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) { +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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - 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); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + 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) { +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); + gpr_free (ffd->localaddr); + gpr_free (ffd); } -static void chttp2_init_client_fake_secure_fullstack( - grpc_end2end_test_fixture *f, grpc_channel_args *client_args) { - grpc_credentials *fake_ts_creds = - grpc_fake_transport_security_credentials_create(); - chttp2_init_client_secure_fullstack(f, client_args, fake_ts_creds); +static void +chttp2_init_client_fake_secure_fullstack (grpc_end2end_test_fixture * f, grpc_channel_args * client_args) +{ + grpc_credentials *fake_ts_creds = grpc_fake_transport_security_credentials_create (); + chttp2_init_client_secure_fullstack (f, client_args, fake_ts_creds); } -static int fail_server_auth_check(grpc_channel_args *server_args) { +static int +fail_server_auth_check (grpc_channel_args * server_args) +{ size_t i; - if (server_args == NULL) return 0; - for (i = 0; i < server_args->num_args; i++) { - if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == - 0) { - return 1; + if (server_args == NULL) + return 0; + for (i = 0; i < server_args->num_args; i++) + { + if (strcmp (server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) + { + return 1; + } } - } return 0; } -static void chttp2_init_server_fake_secure_fullstack( - grpc_end2end_test_fixture *f, grpc_channel_args *server_args) { - grpc_server_credentials *fake_ts_creds = - grpc_fake_transport_security_server_credentials_create(); - if (fail_server_auth_check(server_args)) { - grpc_auth_metadata_processor processor = {process_auth_failure, NULL, NULL}; - grpc_server_credentials_set_auth_metadata_processor(fake_ts_creds, - processor); - } - chttp2_init_server_secure_fullstack(f, server_args, fake_ts_creds); +static void +chttp2_init_server_fake_secure_fullstack (grpc_end2end_test_fixture * f, grpc_channel_args * server_args) +{ + grpc_server_credentials *fake_ts_creds = grpc_fake_transport_security_server_credentials_create (); + if (fail_server_auth_check (server_args)) + { + grpc_auth_metadata_processor processor = { process_auth_failure, NULL, NULL }; + grpc_server_credentials_set_auth_metadata_processor (fake_ts_creds, processor); + } + chttp2_init_server_secure_fullstack (f, server_args, fake_ts_creds); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/fake_secure_fullstack", - FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | - FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS, - chttp2_create_fixture_secure_fullstack, - chttp2_init_client_fake_secure_fullstack, - chttp2_init_server_fake_secure_fullstack, - chttp2_tear_down_secure_fullstack}, + {"chttp2/fake_secure_fullstack", + FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS, + chttp2_create_fixture_secure_fullstack, + chttp2_init_client_fake_secure_fullstack, + chttp2_init_server_fake_secure_fullstack, + chttp2_tear_down_secure_fullstack}, }; -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - grpc_init(); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_full+poll.c b/test/core/end2end/fixtures/h2_full+poll.c index 48feefc931..92f49c0bc6 100644 --- a/test/core/end2end/fixtures/h2_full+poll.c +++ b/test/core/end2end/fixtures/h2_full+poll.c @@ -50,69 +50,79 @@ #include "test/core/util/port.h" #include "test/core/util/test_config.h" -typedef struct fullstack_fixture_data { +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) { +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)); + 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); + gpr_join_host_port (&ffd->localaddr, "localhost", port); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +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_insecure_channel_create(ffd->localaddr, client_args, NULL); + f->client = grpc_insecure_channel_create (ffd->localaddr, client_args, NULL); } -void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *server_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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr)); - grpc_server_start(f->server); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (f->server, ffd->localaddr)); + grpc_server_start (f->server); } -void chttp2_tear_down_fullstack(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_fullstack (grpc_end2end_test_fixture * f) +{ fullstack_fixture_data *ffd = f->fixture_data; - gpr_free(ffd->localaddr); - gpr_free(ffd); + 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}, + {"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) { +int +main (int argc, char **argv) +{ size_t i; grpc_platform_become_multipoller = grpc_poll_become_multipoller; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_full.c b/test/core/end2end/fixtures/h2_full.c index 0170dcf0e5..16e9359500 100644 --- a/test/core/end2end/fixtures/h2_full.c +++ b/test/core/end2end/fixtures/h2_full.c @@ -50,68 +50,78 @@ #include "test/core/util/port.h" #include "test/core/util/test_config.h" -typedef struct fullstack_fixture_data { +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) { +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)); + 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); + gpr_join_host_port (&ffd->localaddr, "localhost", port); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +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_insecure_channel_create(ffd->localaddr, client_args, NULL); - GPR_ASSERT(f->client); + f->client = grpc_insecure_channel_create (ffd->localaddr, client_args, NULL); + GPR_ASSERT (f->client); } -void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *server_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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr)); - grpc_server_start(f->server); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (f->server, ffd->localaddr)); + grpc_server_start (f->server); } -void chttp2_tear_down_fullstack(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_fullstack (grpc_end2end_test_fixture * f) +{ fullstack_fixture_data *ffd = f->fixture_data; - gpr_free(ffd->localaddr); - gpr_free(ffd); + 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}, + {"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) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_oauth2.c b/test/core/end2end/fixtures/h2_oauth2.c index b4b2e02684..a328eb1ed5 100644 --- a/test/core/end2end/fixtures/h2_oauth2.c +++ b/test/core/end2end/fixtures/h2_oauth2.c @@ -50,191 +50,201 @@ static const char oauth2_md[] = "Bearer aaslkfjs424535asdf"; static const char *client_identity_property_name = "smurf_name"; static const char *client_identity = "Brainy Smurf"; -typedef struct fullstack_secure_fixture_data { +typedef struct fullstack_secure_fixture_data +{ char *localaddr; } fullstack_secure_fixture_data; -static const grpc_metadata *find_metadata(const grpc_metadata *md, - size_t md_count, const char *key, - const char *value) { +static const grpc_metadata * +find_metadata (const grpc_metadata * md, size_t md_count, const char *key, const char *value) +{ size_t i; - for (i = 0; i < md_count; i++) { - if (strcmp(key, md[i].key) == 0 && strlen(value) == md[i].value_length && - memcmp(md[i].value, value, md[i].value_length) == 0) { - return &md[i]; + for (i = 0; i < md_count; i++) + { + if (strcmp (key, md[i].key) == 0 && strlen (value) == md[i].value_length && memcmp (md[i].value, value, md[i].value_length) == 0) + { + return &md[i]; + } } - } return NULL; } -typedef struct { size_t pseudo_refcount; } test_processor_state; +typedef struct +{ + size_t pseudo_refcount; +} test_processor_state; -static void process_oauth2_success(void *state, grpc_auth_context *ctx, - const grpc_metadata *md, size_t md_count, - grpc_process_auth_metadata_done_cb cb, - void *user_data) { - const grpc_metadata *oauth2 = - find_metadata(md, md_count, "Authorization", oauth2_md); +static void +process_oauth2_success (void *state, grpc_auth_context * ctx, const grpc_metadata * md, size_t md_count, grpc_process_auth_metadata_done_cb cb, void *user_data) +{ + const grpc_metadata *oauth2 = find_metadata (md, md_count, "Authorization", oauth2_md); test_processor_state *s; - GPR_ASSERT(state != NULL); - s = (test_processor_state *)state; - GPR_ASSERT(s->pseudo_refcount == 1); - GPR_ASSERT(oauth2 != NULL); - grpc_auth_context_add_cstring_property(ctx, client_identity_property_name, - client_identity); - GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name( - ctx, client_identity_property_name) == 1); - cb(user_data, oauth2, 1, NULL, 0, GRPC_STATUS_OK, NULL); + GPR_ASSERT (state != NULL); + s = (test_processor_state *) state; + GPR_ASSERT (s->pseudo_refcount == 1); + GPR_ASSERT (oauth2 != NULL); + grpc_auth_context_add_cstring_property (ctx, client_identity_property_name, client_identity); + GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, client_identity_property_name) == 1); + cb (user_data, oauth2, 1, NULL, 0, GRPC_STATUS_OK, NULL); } -static void process_oauth2_failure(void *state, grpc_auth_context *ctx, - const grpc_metadata *md, size_t md_count, - grpc_process_auth_metadata_done_cb cb, - void *user_data) { - const grpc_metadata *oauth2 = - find_metadata(md, md_count, "Authorization", oauth2_md); +static void +process_oauth2_failure (void *state, grpc_auth_context * ctx, const grpc_metadata * md, size_t md_count, grpc_process_auth_metadata_done_cb cb, void *user_data) +{ + const grpc_metadata *oauth2 = find_metadata (md, md_count, "Authorization", oauth2_md); test_processor_state *s; - GPR_ASSERT(state != NULL); - s = (test_processor_state *)state; - GPR_ASSERT(s->pseudo_refcount == 1); - GPR_ASSERT(oauth2 != NULL); - cb(user_data, oauth2, 1, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); + GPR_ASSERT (state != NULL); + s = (test_processor_state *) state; + GPR_ASSERT (s->pseudo_refcount == 1); + GPR_ASSERT (oauth2 != NULL); + cb (user_data, oauth2, 1, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); } -static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( - grpc_channel_args *client_args, grpc_channel_args *server_args) { +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)); + 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); + gpr_join_host_port (&ffd->localaddr, "localhost", port); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args, - grpc_credentials *creds) { +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, NULL); - GPR_ASSERT(f->client != NULL); - grpc_credentials_release(creds); + f->client = grpc_secure_channel_create (creds, ffd->localaddr, client_args, NULL); + 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) { +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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - 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); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + 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) { +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); + gpr_free (ffd->localaddr); + gpr_free (ffd); } -static void chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack( - grpc_end2end_test_fixture *f, grpc_channel_args *client_args) { - grpc_credentials *ssl_creds = - grpc_ssl_credentials_create(test_root_cert, NULL, NULL); - grpc_credentials *oauth2_creds = - grpc_md_only_test_credentials_create("Authorization", oauth2_md, 1); - grpc_credentials *ssl_oauth2_creds = - grpc_composite_credentials_create(ssl_creds, oauth2_creds, 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, 1); - chttp2_init_client_secure_fullstack(f, new_client_args, ssl_oauth2_creds); - grpc_channel_args_destroy(new_client_args); - grpc_credentials_release(ssl_creds); - grpc_credentials_release(oauth2_creds); +static void +chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack (grpc_end2end_test_fixture * f, grpc_channel_args * client_args) +{ + grpc_credentials *ssl_creds = grpc_ssl_credentials_create (test_root_cert, NULL, NULL); + grpc_credentials *oauth2_creds = grpc_md_only_test_credentials_create ("Authorization", oauth2_md, 1); + grpc_credentials *ssl_oauth2_creds = grpc_composite_credentials_create (ssl_creds, oauth2_creds, 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, 1); + chttp2_init_client_secure_fullstack (f, new_client_args, ssl_oauth2_creds); + grpc_channel_args_destroy (new_client_args); + grpc_credentials_release (ssl_creds); + grpc_credentials_release (oauth2_creds); } -static int fail_server_auth_check(grpc_channel_args *server_args) { +static int +fail_server_auth_check (grpc_channel_args * server_args) +{ size_t i; - if (server_args == NULL) return 0; - for (i = 0; i < server_args->num_args; i++) { - if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == - 0) { - return 1; + if (server_args == NULL) + return 0; + for (i = 0; i < server_args->num_args; i++) + { + if (strcmp (server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) + { + return 1; + } } - } return 0; } -static void processor_destroy(void *state) { - test_processor_state *s = (test_processor_state *)state; - GPR_ASSERT((s->pseudo_refcount--) == 1); - gpr_free(s); +static void +processor_destroy (void *state) +{ + test_processor_state *s = (test_processor_state *) state; + GPR_ASSERT ((s->pseudo_refcount--) == 1); + gpr_free (s); } -static grpc_auth_metadata_processor test_processor_create(int failing) { - test_processor_state *s = gpr_malloc(sizeof(*s)); +static grpc_auth_metadata_processor +test_processor_create (int failing) +{ + test_processor_state *s = gpr_malloc (sizeof (*s)); grpc_auth_metadata_processor result; s->pseudo_refcount = 1; result.state = s; result.destroy = processor_destroy; - if (failing) { - result.process = process_oauth2_failure; - } else { - result.process = process_oauth2_success; - } + if (failing) + { + result.process = process_oauth2_failure; + } + else + { + result.process = process_oauth2_success; + } return result; } -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_key_cert_pair = {test_server1_key, - test_server1_cert}; - grpc_server_credentials *ssl_creds = - grpc_ssl_server_credentials_create(NULL, &pem_key_cert_pair, 1, 0, NULL); - grpc_server_credentials_set_auth_metadata_processor( - ssl_creds, test_processor_create(fail_server_auth_check(server_args))); - chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); +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_key_cert_pair = { test_server1_key, + test_server1_cert + }; + grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create (NULL, &pem_key_cert_pair, 1, 0, NULL); + grpc_server_credentials_set_auth_metadata_processor (ssl_creds, test_processor_create (fail_server_auth_check (server_args))); + chttp2_init_server_secure_fullstack (f, server_args, ssl_creds); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/simple_ssl_with_oauth2_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_with_oauth2_secure_fullstack, - chttp2_init_server_simple_ssl_secure_fullstack, - chttp2_tear_down_secure_fullstack}, + {"chttp2/simple_ssl_with_oauth2_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_with_oauth2_secure_fullstack, + chttp2_init_server_simple_ssl_secure_fullstack, + chttp2_tear_down_secure_fullstack}, }; -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - grpc_init(); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_proxy.c b/test/core/end2end/fixtures/h2_proxy.c index 44083e2676..479db966ca 100644 --- a/test/core/end2end/fixtures/h2_proxy.c +++ b/test/core/end2end/fixtures/h2_proxy.c @@ -51,82 +51,95 @@ #include "test/core/util/port.h" #include "test/core/util/test_config.h" -typedef struct fullstack_fixture_data { +typedef struct fullstack_fixture_data +{ grpc_end2end_proxy *proxy; } fullstack_fixture_data; -static grpc_server *create_proxy_server(const char *port) { - grpc_server *s = grpc_server_create(NULL, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(s, port)); +static grpc_server * +create_proxy_server (const char *port) +{ + grpc_server *s = grpc_server_create (NULL, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (s, port)); return s; } -static grpc_channel *create_proxy_client(const char *target) { - return grpc_insecure_channel_create(target, NULL, NULL); +static grpc_channel * +create_proxy_client (const char *target) +{ + return grpc_insecure_channel_create (target, NULL, NULL); } -static const grpc_end2end_proxy_def proxy_def = {create_proxy_server, - create_proxy_client}; +static const grpc_end2end_proxy_def proxy_def = { create_proxy_server, + create_proxy_client +}; -static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( - grpc_channel_args *client_args, grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +chttp2_create_fixture_fullstack (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - fullstack_fixture_data *ffd = gpr_malloc(sizeof(fullstack_fixture_data)); - memset(&f, 0, sizeof(f)); + fullstack_fixture_data *ffd = gpr_malloc (sizeof (fullstack_fixture_data)); + memset (&f, 0, sizeof (f)); - ffd->proxy = grpc_end2end_proxy_create(&proxy_def); + ffd->proxy = grpc_end2end_proxy_create (&proxy_def); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +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_insecure_channel_create( - grpc_end2end_proxy_get_client_target(ffd->proxy), client_args, NULL); - GPR_ASSERT(f->client); + f->client = grpc_insecure_channel_create (grpc_end2end_proxy_get_client_target (ffd->proxy), client_args, NULL); + GPR_ASSERT (f->client); } -void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *server_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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port( - f->server, grpc_end2end_proxy_get_server_port(ffd->proxy))); - grpc_server_start(f->server); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (f->server, grpc_end2end_proxy_get_server_port (ffd->proxy))); + grpc_server_start (f->server); } -void chttp2_tear_down_fullstack(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_fullstack (grpc_end2end_test_fixture * f) +{ fullstack_fixture_data *ffd = f->fixture_data; - grpc_end2end_proxy_destroy(ffd->proxy); - gpr_free(ffd); + grpc_end2end_proxy_destroy (ffd->proxy); + gpr_free (ffd); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/fullstack+proxy", FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION, - chttp2_create_fixture_fullstack, chttp2_init_client_fullstack, - chttp2_init_server_fullstack, chttp2_tear_down_fullstack}, + {"chttp2/fullstack+proxy", 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) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_sockpair+trace.c b/test/core/end2end/fixtures/h2_sockpair+trace.c index b79c483703..85fa111ad4 100644 --- a/test/core/end2end/fixtures/h2_sockpair+trace.c +++ b/test/core/end2end/fixtures/h2_sockpair+trace.c @@ -57,127 +57,133 @@ /* chttp2 transport that is immediately available (used for testing connected_channel without a client_channel */ -static void server_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx) { +static void +server_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx) +{ grpc_end2end_test_fixture *f = ts; static grpc_channel_filter const *extra_filters[] = { - &grpc_http_server_filter}; + &grpc_http_server_filter + }; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_server_setup_transport( - f->server, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters), mdctx, - grpc_server_get_channel_args(f->server), &closure_list); - grpc_closure_list_run(&closure_list); + grpc_server_setup_transport (f->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (f->server), &closure_list); + grpc_closure_list_run (&closure_list); } -typedef struct { +typedef struct +{ grpc_end2end_test_fixture *f; grpc_channel_args *client_args; } sp_client_setup; -static void client_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx, - grpc_closure_list *closure_list) { +static void +client_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx, grpc_closure_list * closure_list) +{ sp_client_setup *cs = ts; - const grpc_channel_filter *filters[] = {&grpc_http_client_filter, - &grpc_compress_filter, - &grpc_connected_channel_filter}; - size_t nfilters = sizeof(filters) / sizeof(*filters); - grpc_channel *channel = - grpc_channel_create_from_filters("socketpair-target", filters, nfilters, - cs->client_args, mdctx, 1, closure_list); + const grpc_channel_filter *filters[] = { &grpc_http_client_filter, + &grpc_compress_filter, + &grpc_connected_channel_filter + }; + size_t nfilters = sizeof (filters) / sizeof (*filters); + grpc_channel *channel = grpc_channel_create_from_filters ("socketpair-target", filters, nfilters, + cs->client_args, mdctx, 1, closure_list); cs->f->client = channel; - grpc_connected_channel_bind_transport(grpc_channel_get_channel_stack(channel), - transport); + grpc_connected_channel_bind_transport (grpc_channel_get_channel_stack (channel), transport); } -static grpc_end2end_test_fixture chttp2_create_fixture_socketpair( - grpc_channel_args *client_args, grpc_channel_args *server_args) { - grpc_endpoint_pair *sfd = gpr_malloc(sizeof(grpc_endpoint_pair)); +static grpc_end2end_test_fixture +chttp2_create_fixture_socketpair (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ + grpc_endpoint_pair *sfd = gpr_malloc (sizeof (grpc_endpoint_pair)); grpc_end2end_test_fixture f; - memset(&f, 0, sizeof(f)); + memset (&f, 0, sizeof (f)); f.fixture_data = sfd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); - *sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536); + *sfd = grpc_iomgr_create_endpoint_pair ("fixture", 65536); return f; } -static void chttp2_init_client_socketpair(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +static void +chttp2_init_client_socketpair (grpc_end2end_test_fixture * f, grpc_channel_args * client_args) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_pair *sfd = f->fixture_data; grpc_transport *transport; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); sp_client_setup cs; cs.client_args = client_args; cs.f = f; - transport = grpc_create_chttp2_transport(client_args, sfd->client, mdctx, 1, - &closure_list); - client_setup_transport(&cs, transport, mdctx, &closure_list); - GPR_ASSERT(f->client); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + transport = grpc_create_chttp2_transport (client_args, sfd->client, mdctx, 1, &closure_list); + client_setup_transport (&cs, transport, mdctx, &closure_list); + GPR_ASSERT (f->client); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); } -static void chttp2_init_server_socketpair(grpc_end2end_test_fixture *f, - grpc_channel_args *server_args) { +static void +chttp2_init_server_socketpair (grpc_end2end_test_fixture * f, grpc_channel_args * server_args) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_pair *sfd = f->fixture_data; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); grpc_transport *transport; - GPR_ASSERT(!f->server); - f->server = grpc_server_create_from_filters(NULL, 0, server_args); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - grpc_server_start(f->server); - transport = grpc_create_chttp2_transport(server_args, sfd->server, mdctx, 0, - &closure_list); - server_setup_transport(f, transport, mdctx); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + GPR_ASSERT (!f->server); + f->server = grpc_server_create_from_filters (NULL, 0, server_args); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + grpc_server_start (f->server); + transport = grpc_create_chttp2_transport (server_args, sfd->server, mdctx, 0, &closure_list); + server_setup_transport (f, transport, mdctx); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); } -static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture *f) { - gpr_free(f->fixture_data); +static void +chttp2_tear_down_socketpair (grpc_end2end_test_fixture * f) +{ + gpr_free (f->fixture_data); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/socketpair", 0, chttp2_create_fixture_socketpair, - chttp2_init_client_socketpair, chttp2_init_server_socketpair, - chttp2_tear_down_socketpair}, + {"chttp2/socketpair", 0, chttp2_create_fixture_socketpair, + chttp2_init_client_socketpair, chttp2_init_server_socketpair, + chttp2_tear_down_socketpair}, }; -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; /* 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"); + gpr_setenv ("GRPC_TRACE", "doesnt-exist,http,all"); #ifdef GPR_POSIX_SOCKET - g_fixture_slowdown_factor = isatty(STDOUT_FILENO) ? 10.0 : 1.0; + g_fixture_slowdown_factor = isatty (STDOUT_FILENO) ? 10.0 : 1.0; #else g_fixture_slowdown_factor = 10.0; #endif - grpc_test_init(argc, argv); - grpc_init(); - grpc_closure_list_run(&closure_list); + grpc_test_init (argc, argv); + grpc_init (); + grpc_closure_list_run (&closure_list); - GPR_ASSERT(0 == grpc_tracer_set_enabled("also-doesnt-exist", 0)); - GPR_ASSERT(1 == grpc_tracer_set_enabled("http", 1)); - GPR_ASSERT(1 == grpc_tracer_set_enabled("all", 1)); + GPR_ASSERT (0 == grpc_tracer_set_enabled ("also-doesnt-exist", 0)); + GPR_ASSERT (1 == grpc_tracer_set_enabled ("http", 1)); + GPR_ASSERT (1 == grpc_tracer_set_enabled ("all", 1)); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_sockpair.c b/test/core/end2end/fixtures/h2_sockpair.c index e927a0e0d8..48faad97d3 100644 --- a/test/core/end2end/fixtures/h2_sockpair.c +++ b/test/core/end2end/fixtures/h2_sockpair.c @@ -56,112 +56,118 @@ /* chttp2 transport that is immediately available (used for testing connected_channel without a client_channel */ -static void server_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx) { +static void +server_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx) +{ grpc_end2end_test_fixture *f = ts; static grpc_channel_filter const *extra_filters[] = { - &grpc_http_server_filter}; + &grpc_http_server_filter + }; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_server_setup_transport( - f->server, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters), mdctx, - grpc_server_get_channel_args(f->server), &closure_list); - grpc_closure_list_run(&closure_list); + grpc_server_setup_transport (f->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (f->server), &closure_list); + grpc_closure_list_run (&closure_list); } -typedef struct { +typedef struct +{ grpc_end2end_test_fixture *f; grpc_channel_args *client_args; } sp_client_setup; -static void client_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx, - grpc_closure_list *closure_list) { +static void +client_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx, grpc_closure_list * closure_list) +{ sp_client_setup *cs = ts; - const grpc_channel_filter *filters[] = {&grpc_http_client_filter, - &grpc_compress_filter, - &grpc_connected_channel_filter}; - size_t nfilters = sizeof(filters) / sizeof(*filters); - grpc_channel *channel = - grpc_channel_create_from_filters("socketpair-target", filters, nfilters, - cs->client_args, mdctx, 1, closure_list); + const grpc_channel_filter *filters[] = { &grpc_http_client_filter, + &grpc_compress_filter, + &grpc_connected_channel_filter + }; + size_t nfilters = sizeof (filters) / sizeof (*filters); + grpc_channel *channel = grpc_channel_create_from_filters ("socketpair-target", filters, nfilters, + cs->client_args, mdctx, 1, closure_list); cs->f->client = channel; - grpc_connected_channel_bind_transport(grpc_channel_get_channel_stack(channel), - transport); + grpc_connected_channel_bind_transport (grpc_channel_get_channel_stack (channel), transport); } -static grpc_end2end_test_fixture chttp2_create_fixture_socketpair( - grpc_channel_args *client_args, grpc_channel_args *server_args) { - grpc_endpoint_pair *sfd = gpr_malloc(sizeof(grpc_endpoint_pair)); +static grpc_end2end_test_fixture +chttp2_create_fixture_socketpair (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ + grpc_endpoint_pair *sfd = gpr_malloc (sizeof (grpc_endpoint_pair)); grpc_end2end_test_fixture f; - memset(&f, 0, sizeof(f)); + memset (&f, 0, sizeof (f)); f.fixture_data = sfd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); - *sfd = grpc_iomgr_create_endpoint_pair("fixture", 65536); + *sfd = grpc_iomgr_create_endpoint_pair ("fixture", 65536); return f; } -static void chttp2_init_client_socketpair(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +static void +chttp2_init_client_socketpair (grpc_end2end_test_fixture * f, grpc_channel_args * client_args) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_pair *sfd = f->fixture_data; grpc_transport *transport; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); sp_client_setup cs; cs.client_args = client_args; cs.f = f; - transport = grpc_create_chttp2_transport(client_args, sfd->client, mdctx, 1, - &closure_list); - client_setup_transport(&cs, transport, mdctx, &closure_list); - GPR_ASSERT(f->client); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + transport = grpc_create_chttp2_transport (client_args, sfd->client, mdctx, 1, &closure_list); + client_setup_transport (&cs, transport, mdctx, &closure_list); + GPR_ASSERT (f->client); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); } -static void chttp2_init_server_socketpair(grpc_end2end_test_fixture *f, - grpc_channel_args *server_args) { +static void +chttp2_init_server_socketpair (grpc_end2end_test_fixture * f, grpc_channel_args * server_args) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_pair *sfd = f->fixture_data; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); grpc_transport *transport; - GPR_ASSERT(!f->server); - f->server = grpc_server_create_from_filters(NULL, 0, server_args); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - grpc_server_start(f->server); - transport = grpc_create_chttp2_transport(server_args, sfd->server, mdctx, 0, - &closure_list); - server_setup_transport(f, transport, mdctx); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + GPR_ASSERT (!f->server); + f->server = grpc_server_create_from_filters (NULL, 0, server_args); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + grpc_server_start (f->server); + transport = grpc_create_chttp2_transport (server_args, sfd->server, mdctx, 0, &closure_list); + server_setup_transport (f, transport, mdctx); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); } -static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture *f) { - gpr_free(f->fixture_data); +static void +chttp2_tear_down_socketpair (grpc_end2end_test_fixture * f) +{ + gpr_free (f->fixture_data); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/socketpair", 0, chttp2_create_fixture_socketpair, - chttp2_init_client_socketpair, chttp2_init_server_socketpair, - chttp2_tear_down_socketpair}, + {"chttp2/socketpair", 0, chttp2_create_fixture_socketpair, + chttp2_init_client_socketpair, chttp2_init_server_socketpair, + chttp2_tear_down_socketpair}, }; -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_sockpair_1byte.c b/test/core/end2end/fixtures/h2_sockpair_1byte.c index 8a9196d3b6..0e11a944af 100644 --- a/test/core/end2end/fixtures/h2_sockpair_1byte.c +++ b/test/core/end2end/fixtures/h2_sockpair_1byte.c @@ -56,112 +56,118 @@ /* chttp2 transport that is immediately available (used for testing connected_channel without a client_channel */ -static void server_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx) { +static void +server_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx) +{ grpc_end2end_test_fixture *f = ts; static grpc_channel_filter const *extra_filters[] = { - &grpc_http_server_filter}; + &grpc_http_server_filter + }; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_server_setup_transport( - f->server, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters), mdctx, - grpc_server_get_channel_args(f->server), &closure_list); - grpc_closure_list_run(&closure_list); + grpc_server_setup_transport (f->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (f->server), &closure_list); + grpc_closure_list_run (&closure_list); } -typedef struct { +typedef struct +{ grpc_end2end_test_fixture *f; grpc_channel_args *client_args; } sp_client_setup; -static void client_setup_transport(void *ts, grpc_transport *transport, - grpc_mdctx *mdctx, - grpc_closure_list *closure_list) { +static void +client_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx, grpc_closure_list * closure_list) +{ sp_client_setup *cs = ts; - const grpc_channel_filter *filters[] = {&grpc_http_client_filter, - &grpc_compress_filter, - &grpc_connected_channel_filter}; - size_t nfilters = sizeof(filters) / sizeof(*filters); - grpc_channel *channel = - grpc_channel_create_from_filters("socketpair-target", filters, nfilters, - cs->client_args, mdctx, 1, closure_list); + const grpc_channel_filter *filters[] = { &grpc_http_client_filter, + &grpc_compress_filter, + &grpc_connected_channel_filter + }; + size_t nfilters = sizeof (filters) / sizeof (*filters); + grpc_channel *channel = grpc_channel_create_from_filters ("socketpair-target", filters, nfilters, + cs->client_args, mdctx, 1, closure_list); cs->f->client = channel; - grpc_connected_channel_bind_transport(grpc_channel_get_channel_stack(channel), - transport); + grpc_connected_channel_bind_transport (grpc_channel_get_channel_stack (channel), transport); } -static grpc_end2end_test_fixture chttp2_create_fixture_socketpair( - grpc_channel_args *client_args, grpc_channel_args *server_args) { - grpc_endpoint_pair *sfd = gpr_malloc(sizeof(grpc_endpoint_pair)); +static grpc_end2end_test_fixture +chttp2_create_fixture_socketpair (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ + grpc_endpoint_pair *sfd = gpr_malloc (sizeof (grpc_endpoint_pair)); grpc_end2end_test_fixture f; - memset(&f, 0, sizeof(f)); + memset (&f, 0, sizeof (f)); f.fixture_data = sfd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); - *sfd = grpc_iomgr_create_endpoint_pair("fixture", 1); + *sfd = grpc_iomgr_create_endpoint_pair ("fixture", 1); return f; } -static void chttp2_init_client_socketpair(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +static void +chttp2_init_client_socketpair (grpc_end2end_test_fixture * f, grpc_channel_args * client_args) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_pair *sfd = f->fixture_data; grpc_transport *transport; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); sp_client_setup cs; cs.client_args = client_args; cs.f = f; - transport = grpc_create_chttp2_transport(client_args, sfd->client, mdctx, 1, - &closure_list); - client_setup_transport(&cs, transport, mdctx, &closure_list); - GPR_ASSERT(f->client); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + transport = grpc_create_chttp2_transport (client_args, sfd->client, mdctx, 1, &closure_list); + client_setup_transport (&cs, transport, mdctx, &closure_list); + GPR_ASSERT (f->client); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); } -static void chttp2_init_server_socketpair(grpc_end2end_test_fixture *f, - grpc_channel_args *server_args) { +static void +chttp2_init_server_socketpair (grpc_end2end_test_fixture * f, grpc_channel_args * server_args) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_pair *sfd = f->fixture_data; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); grpc_transport *transport; - GPR_ASSERT(!f->server); - f->server = grpc_server_create_from_filters(NULL, 0, server_args); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - grpc_server_start(f->server); - transport = grpc_create_chttp2_transport(server_args, sfd->server, mdctx, 0, - &closure_list); - server_setup_transport(f, transport, mdctx); - grpc_chttp2_transport_start_reading(transport, NULL, 0, &closure_list); - grpc_closure_list_run(&closure_list); + GPR_ASSERT (!f->server); + f->server = grpc_server_create_from_filters (NULL, 0, server_args); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + grpc_server_start (f->server); + transport = grpc_create_chttp2_transport (server_args, sfd->server, mdctx, 0, &closure_list); + server_setup_transport (f, transport, mdctx); + grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list); + grpc_closure_list_run (&closure_list); } -static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture *f) { - gpr_free(f->fixture_data); +static void +chttp2_tear_down_socketpair (grpc_end2end_test_fixture * f) +{ + gpr_free (f->fixture_data); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/socketpair_one_byte_at_a_time", 0, - chttp2_create_fixture_socketpair, chttp2_init_client_socketpair, - chttp2_init_server_socketpair, chttp2_tear_down_socketpair}, + {"chttp2/socketpair_one_byte_at_a_time", 0, + chttp2_create_fixture_socketpair, chttp2_init_client_socketpair, + chttp2_init_server_socketpair, chttp2_tear_down_socketpair}, }; -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_ssl+poll.c b/test/core/end2end/fixtures/h2_ssl+poll.c index 2c605d1471..55c12a37ca 100644 --- a/test/core/end2end/fixtures/h2_ssl+poll.c +++ b/test/core/end2end/fixtures/h2_ssl+poll.c @@ -48,145 +48,153 @@ #include "test/core/util/port.h" #include "test/core/end2end/data/ssl_test_data.h" -typedef struct fullstack_secure_fixture_data { +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) { +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)); + 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); + gpr_join_host_port (&ffd->localaddr, "localhost", port); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -static void process_auth_failure(void *state, grpc_auth_context *ctx, - const grpc_metadata *md, size_t md_count, - grpc_process_auth_metadata_done_cb cb, - void *user_data) { - GPR_ASSERT(state == NULL); - cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); +static void +process_auth_failure (void *state, grpc_auth_context * ctx, const grpc_metadata * md, size_t md_count, grpc_process_auth_metadata_done_cb cb, void *user_data) +{ + GPR_ASSERT (state == NULL); + cb (user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); } -static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args, - grpc_credentials *creds) { +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, NULL); - GPR_ASSERT(f->client != NULL); - grpc_credentials_release(creds); + f->client = grpc_secure_channel_create (creds, ffd->localaddr, client_args, NULL); + 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) { +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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - 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); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + 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) { +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); + 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, 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, 1); - chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); - grpc_channel_args_destroy(new_client_args); +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, 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, 1); + chttp2_init_client_secure_fullstack (f, new_client_args, ssl_creds); + grpc_channel_args_destroy (new_client_args); } -static int fail_server_auth_check(grpc_channel_args *server_args) { +static int +fail_server_auth_check (grpc_channel_args * server_args) +{ size_t i; - if (server_args == NULL) return 0; - for (i = 0; i < server_args->num_args; i++) { - if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == - 0) { - return 1; + if (server_args == NULL) + return 0; + for (i = 0; i < server_args->num_args; i++) + { + if (strcmp (server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) + { + return 1; + } } - } return 0; } -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, 0, NULL); - if (fail_server_auth_check(server_args)) { - grpc_auth_metadata_processor processor = {process_auth_failure, NULL, NULL}; - grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor); - } - chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); +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, 0, NULL); + if (fail_server_auth_check (server_args)) + { + grpc_auth_metadata_processor processor = { process_auth_failure, NULL, NULL }; + grpc_server_credentials_set_auth_metadata_processor (ssl_creds, processor); + } + 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}, + {"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) { +int +main (int argc, char **argv) +{ size_t i; FILE *roots_file; - size_t roots_size = strlen(test_root_cert); + size_t roots_size = strlen (test_root_cert); char *roots_filename; grpc_platform_become_multipoller = grpc_poll_become_multipoller; - grpc_test_init(argc, argv); + 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]); - } + 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(); + grpc_shutdown (); /* Cleanup. */ - remove(roots_filename); - gpr_free(roots_filename); + remove (roots_filename); + gpr_free (roots_filename); return 0; } diff --git a/test/core/end2end/fixtures/h2_ssl.c b/test/core/end2end/fixtures/h2_ssl.c index 9193a09b17..7752ff2703 100644 --- a/test/core/end2end/fixtures/h2_ssl.c +++ b/test/core/end2end/fixtures/h2_ssl.c @@ -48,142 +48,151 @@ #include "test/core/util/port.h" #include "test/core/end2end/data/ssl_test_data.h" -typedef struct fullstack_secure_fixture_data { +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) { +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)); + 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); + gpr_join_host_port (&ffd->localaddr, "localhost", port); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -static void process_auth_failure(void *state, grpc_auth_context *ctx, - const grpc_metadata *md, size_t md_count, - grpc_process_auth_metadata_done_cb cb, - void *user_data) { - GPR_ASSERT(state == NULL); - cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); +static void +process_auth_failure (void *state, grpc_auth_context * ctx, const grpc_metadata * md, size_t md_count, grpc_process_auth_metadata_done_cb cb, void *user_data) +{ + GPR_ASSERT (state == NULL); + cb (user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); } -static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args, - grpc_credentials *creds) { +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, NULL); - GPR_ASSERT(f->client != NULL); - grpc_credentials_release(creds); + f->client = grpc_secure_channel_create (creds, ffd->localaddr, client_args, NULL); + 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) { +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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - 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); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + 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) { +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); + 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, 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, 1); - chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); - grpc_channel_args_destroy(new_client_args); +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, 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, 1); + chttp2_init_client_secure_fullstack (f, new_client_args, ssl_creds); + grpc_channel_args_destroy (new_client_args); } -static int fail_server_auth_check(grpc_channel_args *server_args) { +static int +fail_server_auth_check (grpc_channel_args * server_args) +{ size_t i; - if (server_args == NULL) return 0; - for (i = 0; i < server_args->num_args; i++) { - if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == - 0) { - return 1; + if (server_args == NULL) + return 0; + for (i = 0; i < server_args->num_args; i++) + { + if (strcmp (server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) + { + return 1; + } } - } return 0; } -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, 0, NULL); - if (fail_server_auth_check(server_args)) { - grpc_auth_metadata_processor processor = {process_auth_failure, NULL, NULL}; - grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor); - } - chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); +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, 0, NULL); + if (fail_server_auth_check (server_args)) + { + grpc_auth_metadata_processor processor = { process_auth_failure, NULL, NULL }; + grpc_server_credentials_set_auth_metadata_processor (ssl_creds, processor); + } + 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}, + {"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) { +int +main (int argc, char **argv) +{ size_t i; FILE *roots_file; - size_t roots_size = strlen(test_root_cert); + size_t roots_size = strlen (test_root_cert); char *roots_filename; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); /* Set the SSL roots env var. */ - roots_file = gpr_tmpfile("chttp2_simple_ssl_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]); - } + roots_file = gpr_tmpfile ("chttp2_simple_ssl_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(); + grpc_shutdown (); /* Cleanup. */ - remove(roots_filename); - gpr_free(roots_filename); + remove (roots_filename); + gpr_free (roots_filename); return 0; } diff --git a/test/core/end2end/fixtures/h2_ssl_proxy.c b/test/core/end2end/fixtures/h2_ssl_proxy.c index 8133a69a0c..ff82a0f291 100644 --- a/test/core/end2end/fixtures/h2_ssl_proxy.c +++ b/test/core/end2end/fixtures/h2_ssl_proxy.c @@ -49,170 +49,184 @@ #include "test/core/util/test_config.h" #include "test/core/util/port.h" -typedef struct fullstack_secure_fixture_data { +typedef struct fullstack_secure_fixture_data +{ grpc_end2end_proxy *proxy; } fullstack_secure_fixture_data; -static grpc_server *create_proxy_server(const char *port) { - grpc_server *s = grpc_server_create(NULL, NULL); - 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, 0, NULL); - GPR_ASSERT(grpc_server_add_secure_http2_port(s, port, ssl_creds)); - grpc_server_credentials_release(ssl_creds); +static grpc_server * +create_proxy_server (const char *port) +{ + grpc_server *s = grpc_server_create (NULL, NULL); + 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, 0, NULL); + GPR_ASSERT (grpc_server_add_secure_http2_port (s, port, ssl_creds)); + grpc_server_credentials_release (ssl_creds); return s; } -static grpc_channel *create_proxy_client(const char *target) { +static grpc_channel * +create_proxy_client (const char *target) +{ grpc_channel *channel; - grpc_credentials *ssl_creds = grpc_ssl_credentials_create(NULL, NULL, NULL); - grpc_arg ssl_name_override = {GRPC_ARG_STRING, - GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, - {"foo.test.google.fr"}}; + grpc_credentials *ssl_creds = grpc_ssl_credentials_create (NULL, NULL, NULL); + grpc_arg ssl_name_override = { GRPC_ARG_STRING, + GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, + {"foo.test.google.fr"} + }; grpc_channel_args client_args; client_args.num_args = 1; client_args.args = &ssl_name_override; - channel = grpc_secure_channel_create(ssl_creds, target, &client_args, NULL); - grpc_credentials_release(ssl_creds); + channel = grpc_secure_channel_create (ssl_creds, target, &client_args, NULL); + grpc_credentials_release (ssl_creds); return channel; } -static const grpc_end2end_proxy_def proxy_def = {create_proxy_server, - create_proxy_client}; +static const grpc_end2end_proxy_def proxy_def = { create_proxy_server, + create_proxy_client +}; -static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( - grpc_channel_args *client_args, grpc_channel_args *server_args) { +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; - fullstack_secure_fixture_data *ffd = - gpr_malloc(sizeof(fullstack_secure_fixture_data)); - memset(&f, 0, sizeof(f)); + fullstack_secure_fixture_data *ffd = gpr_malloc (sizeof (fullstack_secure_fixture_data)); + memset (&f, 0, sizeof (f)); - ffd->proxy = grpc_end2end_proxy_create(&proxy_def); + ffd->proxy = grpc_end2end_proxy_create (&proxy_def); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -static void process_auth_failure(void *state, grpc_auth_context *ctx, - const grpc_metadata *md, size_t md_count, - grpc_process_auth_metadata_done_cb cb, - void *user_data) { - GPR_ASSERT(state == NULL); - cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); +static void +process_auth_failure (void *state, grpc_auth_context * ctx, const grpc_metadata * md, size_t md_count, grpc_process_auth_metadata_done_cb cb, void *user_data) +{ + GPR_ASSERT (state == NULL); + cb (user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL); } -static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args, - grpc_credentials *creds) { +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, grpc_end2end_proxy_get_client_target(ffd->proxy), client_args, - NULL); - GPR_ASSERT(f->client != NULL); - grpc_credentials_release(creds); + f->client = grpc_secure_channel_create (creds, grpc_end2end_proxy_get_client_target (ffd->proxy), client_args, NULL); + 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) { +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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_secure_http2_port( - f->server, grpc_end2end_proxy_get_server_port(ffd->proxy), server_creds)); - grpc_server_credentials_release(server_creds); - grpc_server_start(f->server); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_secure_http2_port (f->server, grpc_end2end_proxy_get_server_port (ffd->proxy), server_creds)); + grpc_server_credentials_release (server_creds); + grpc_server_start (f->server); } -void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_secure_fullstack (grpc_end2end_test_fixture * f) +{ fullstack_secure_fixture_data *ffd = f->fixture_data; - grpc_end2end_proxy_destroy(ffd->proxy); - gpr_free(ffd); + grpc_end2end_proxy_destroy (ffd->proxy); + 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, 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, 1); - chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); - grpc_channel_args_destroy(new_client_args); +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, 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, 1); + chttp2_init_client_secure_fullstack (f, new_client_args, ssl_creds); + grpc_channel_args_destroy (new_client_args); } -static int fail_server_auth_check(grpc_channel_args *server_args) { +static int +fail_server_auth_check (grpc_channel_args * server_args) +{ size_t i; - if (server_args == NULL) return 0; - for (i = 0; i < server_args->num_args; i++) { - if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == - 0) { - return 1; + if (server_args == NULL) + return 0; + for (i = 0; i < server_args->num_args; i++) + { + if (strcmp (server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) + { + return 1; + } } - } return 0; } -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, 0, NULL); - if (fail_server_auth_check(server_args)) { - grpc_auth_metadata_processor processor = {process_auth_failure, NULL, NULL}; - grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor); - } - chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); +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, 0, NULL); + if (fail_server_auth_check (server_args)) + { + grpc_auth_metadata_processor processor = { process_auth_failure, NULL, NULL }; + grpc_server_credentials_set_auth_metadata_processor (ssl_creds, processor); + } + 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}, + {"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) { +int +main (int argc, char **argv) +{ size_t i; FILE *roots_file; - size_t roots_size = strlen(test_root_cert); + size_t roots_size = strlen (test_root_cert); char *roots_filename; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); /* Set the SSL roots env var. */ - roots_file = gpr_tmpfile("chttp2_simple_ssl_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]); - } + roots_file = gpr_tmpfile ("chttp2_simple_ssl_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(); + grpc_shutdown (); /* Cleanup. */ - remove(roots_filename); - gpr_free(roots_filename); + remove (roots_filename); + gpr_free (roots_filename); return 0; } diff --git a/test/core/end2end/fixtures/h2_uds+poll.c b/test/core/end2end/fixtures/h2_uds+poll.c index 05b9ea0949..b2981fe0bc 100644 --- a/test/core/end2end/fixtures/h2_uds+poll.c +++ b/test/core/end2end/fixtures/h2_uds+poll.c @@ -54,71 +54,80 @@ #include "test/core/util/port.h" #include "test/core/util/test_config.h" -typedef struct fullstack_fixture_data { +typedef struct fullstack_fixture_data +{ char *localaddr; } fullstack_fixture_data; static int unique = 1; -static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( - grpc_channel_args *client_args, grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +chttp2_create_fixture_fullstack (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - fullstack_fixture_data *ffd = gpr_malloc(sizeof(fullstack_fixture_data)); - memset(&f, 0, sizeof(f)); + fullstack_fixture_data *ffd = gpr_malloc (sizeof (fullstack_fixture_data)); + memset (&f, 0, sizeof (f)); - gpr_asprintf(&ffd->localaddr, "unix:/tmp/grpc_fullstack_test.%d.%d", getpid(), - unique++); + gpr_asprintf (&ffd->localaddr, "unix:/tmp/grpc_fullstack_test.%d.%d", getpid (), unique++); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +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_insecure_channel_create(ffd->localaddr, client_args, NULL); + f->client = grpc_insecure_channel_create (ffd->localaddr, client_args, NULL); } -void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *server_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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr)); - grpc_server_start(f->server); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (f->server, ffd->localaddr)); + grpc_server_start (f->server); } -void chttp2_tear_down_fullstack(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_fullstack (grpc_end2end_test_fixture * f) +{ fullstack_fixture_data *ffd = f->fixture_data; - gpr_free(ffd->localaddr); - gpr_free(ffd); + gpr_free (ffd->localaddr); + gpr_free (ffd); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/fullstack_uds", FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION, - chttp2_create_fixture_fullstack, chttp2_init_client_fullstack, - chttp2_init_server_fullstack, chttp2_tear_down_fullstack}, + {"chttp2/fullstack_uds", 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) { +int +main (int argc, char **argv) +{ size_t i; grpc_platform_become_multipoller = grpc_poll_become_multipoller; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/h2_uds.c b/test/core/end2end/fixtures/h2_uds.c index 59d39bbb0f..6b664d3aec 100644 --- a/test/core/end2end/fixtures/h2_uds.c +++ b/test/core/end2end/fixtures/h2_uds.c @@ -54,69 +54,78 @@ #include "test/core/util/port.h" #include "test/core/util/test_config.h" -typedef struct fullstack_fixture_data { +typedef struct fullstack_fixture_data +{ char *localaddr; } fullstack_fixture_data; static int unique = 1; -static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( - grpc_channel_args *client_args, grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +chttp2_create_fixture_fullstack (grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - fullstack_fixture_data *ffd = gpr_malloc(sizeof(fullstack_fixture_data)); - memset(&f, 0, sizeof(f)); + fullstack_fixture_data *ffd = gpr_malloc (sizeof (fullstack_fixture_data)); + memset (&f, 0, sizeof (f)); - gpr_asprintf(&ffd->localaddr, "unix:/tmp/grpc_fullstack_test.%d.%d", getpid(), - unique++); + gpr_asprintf (&ffd->localaddr, "unix:/tmp/grpc_fullstack_test.%d.%d", getpid (), unique++); f.fixture_data = ffd; - f.cq = grpc_completion_queue_create(NULL); + f.cq = grpc_completion_queue_create (NULL); return f; } -void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *client_args) { +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_insecure_channel_create(ffd->localaddr, client_args, NULL); + f->client = grpc_insecure_channel_create (ffd->localaddr, client_args, NULL); } -void chttp2_init_server_fullstack(grpc_end2end_test_fixture *f, - grpc_channel_args *server_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, NULL); - grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr)); - grpc_server_start(f->server); + if (f->server) + { + grpc_server_destroy (f->server); + } + f->server = grpc_server_create (server_args, NULL); + grpc_server_register_completion_queue (f->server, f->cq, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (f->server, ffd->localaddr)); + grpc_server_start (f->server); } -void chttp2_tear_down_fullstack(grpc_end2end_test_fixture *f) { +void +chttp2_tear_down_fullstack (grpc_end2end_test_fixture * f) +{ fullstack_fixture_data *ffd = f->fixture_data; - gpr_free(ffd->localaddr); - gpr_free(ffd); + gpr_free (ffd->localaddr); + gpr_free (ffd); } /* All test configurations */ static grpc_end2end_test_config configs[] = { - {"chttp2/fullstack_uds", FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION, - chttp2_create_fixture_fullstack, chttp2_init_client_fullstack, - chttp2_init_server_fullstack, chttp2_tear_down_fullstack}, + {"chttp2/fullstack_uds", 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) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { - grpc_end2end_tests(configs[i]); - } + for (i = 0; i < sizeof (configs) / sizeof (*configs); i++) + { + grpc_end2end_tests (configs[i]); + } - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/fixtures/proxy.c b/test/core/end2end/fixtures/proxy.c index 434e75dd15..14a55fb852 100644 --- a/test/core/end2end/fixtures/proxy.c +++ b/test/core/end2end/fixtures/proxy.c @@ -44,7 +44,8 @@ #include "test/core/util/port.h" -struct grpc_end2end_proxy { +struct grpc_end2end_proxy +{ gpr_thd_id thd; char *proxy_port; char *server_port; @@ -60,12 +61,14 @@ struct grpc_end2end_proxy { grpc_metadata_array new_call_metadata; }; -typedef struct { - void (*func)(void *arg, int success); +typedef struct +{ + void (*func) (void *arg, int success); void *arg; } closure; -typedef struct { +typedef struct +{ gpr_refcount refs; grpc_end2end_proxy *proxy; @@ -86,351 +89,383 @@ typedef struct { int c2p_server_cancelled; } proxy_call; -static void thread_main(void *arg); -static void request_call(grpc_end2end_proxy *proxy); +static void thread_main (void *arg); +static void request_call (grpc_end2end_proxy * proxy); -grpc_end2end_proxy *grpc_end2end_proxy_create( - const grpc_end2end_proxy_def *def) { - gpr_thd_options opt = gpr_thd_options_default(); - int proxy_port = grpc_pick_unused_port_or_die(); - int server_port = grpc_pick_unused_port_or_die(); +grpc_end2end_proxy * +grpc_end2end_proxy_create (const grpc_end2end_proxy_def * def) +{ + gpr_thd_options opt = gpr_thd_options_default (); + int proxy_port = grpc_pick_unused_port_or_die (); + int server_port = grpc_pick_unused_port_or_die (); - grpc_end2end_proxy *proxy = gpr_malloc(sizeof(*proxy)); - memset(proxy, 0, sizeof(*proxy)); + grpc_end2end_proxy *proxy = gpr_malloc (sizeof (*proxy)); + memset (proxy, 0, sizeof (*proxy)); - gpr_join_host_port(&proxy->proxy_port, "localhost", proxy_port); - gpr_join_host_port(&proxy->server_port, "localhost", server_port); + gpr_join_host_port (&proxy->proxy_port, "localhost", proxy_port); + gpr_join_host_port (&proxy->server_port, "localhost", server_port); - gpr_log(GPR_DEBUG, "PROXY ADDR:%s BACKEND:%s", proxy->proxy_port, - proxy->server_port); + gpr_log (GPR_DEBUG, "PROXY ADDR:%s BACKEND:%s", proxy->proxy_port, proxy->server_port); - proxy->cq = grpc_completion_queue_create(NULL); - proxy->server = def->create_server(proxy->proxy_port); - proxy->client = def->create_client(proxy->server_port); + proxy->cq = grpc_completion_queue_create (NULL); + proxy->server = def->create_server (proxy->proxy_port); + proxy->client = def->create_client (proxy->server_port); - grpc_server_register_completion_queue(proxy->server, proxy->cq, NULL); - grpc_server_start(proxy->server); + grpc_server_register_completion_queue (proxy->server, proxy->cq, NULL); + grpc_server_start (proxy->server); - gpr_thd_options_set_joinable(&opt); - GPR_ASSERT(gpr_thd_new(&proxy->thd, thread_main, proxy, &opt)); + gpr_thd_options_set_joinable (&opt); + GPR_ASSERT (gpr_thd_new (&proxy->thd, thread_main, proxy, &opt)); - request_call(proxy); + request_call (proxy); return proxy; } -static closure *new_closure(void (*func)(void *arg, int success), void *arg) { - closure *cl = gpr_malloc(sizeof(*cl)); +static closure * +new_closure (void (*func) (void *arg, int success), void *arg) +{ + closure *cl = gpr_malloc (sizeof (*cl)); cl->func = func; cl->arg = arg; return cl; } -static void shutdown_complete(void *arg, int success) { +static void +shutdown_complete (void *arg, int success) +{ grpc_end2end_proxy *proxy = arg; proxy->shutdown = 1; - grpc_completion_queue_shutdown(proxy->cq); + grpc_completion_queue_shutdown (proxy->cq); } -void grpc_end2end_proxy_destroy(grpc_end2end_proxy *proxy) { - grpc_server_shutdown_and_notify(proxy->server, proxy->cq, - new_closure(shutdown_complete, proxy)); - gpr_thd_join(proxy->thd); - gpr_free(proxy->proxy_port); - gpr_free(proxy->server_port); - grpc_server_destroy(proxy->server); - grpc_channel_destroy(proxy->client); - grpc_completion_queue_destroy(proxy->cq); - grpc_call_details_destroy(&proxy->new_call_details); - gpr_free(proxy); +void +grpc_end2end_proxy_destroy (grpc_end2end_proxy * proxy) +{ + grpc_server_shutdown_and_notify (proxy->server, proxy->cq, new_closure (shutdown_complete, proxy)); + gpr_thd_join (proxy->thd); + gpr_free (proxy->proxy_port); + gpr_free (proxy->server_port); + grpc_server_destroy (proxy->server); + grpc_channel_destroy (proxy->client); + grpc_completion_queue_destroy (proxy->cq); + grpc_call_details_destroy (&proxy->new_call_details); + gpr_free (proxy); } -static void unrefpc(proxy_call *pc, const char *reason) { - if (gpr_unref(&pc->refs)) { - grpc_call_destroy(pc->c2p); - grpc_call_destroy(pc->p2s); - grpc_metadata_array_destroy(&pc->c2p_initial_metadata); - grpc_metadata_array_destroy(&pc->p2s_initial_metadata); - grpc_metadata_array_destroy(&pc->p2s_trailing_metadata); - gpr_free(pc->p2s_status_details); - gpr_free(pc); - } +static void +unrefpc (proxy_call * pc, const char *reason) +{ + if (gpr_unref (&pc->refs)) + { + grpc_call_destroy (pc->c2p); + grpc_call_destroy (pc->p2s); + grpc_metadata_array_destroy (&pc->c2p_initial_metadata); + grpc_metadata_array_destroy (&pc->p2s_initial_metadata); + grpc_metadata_array_destroy (&pc->p2s_trailing_metadata); + gpr_free (pc->p2s_status_details); + gpr_free (pc); + } } -static void refpc(proxy_call *pc, const char *reason) { gpr_ref(&pc->refs); } +static void +refpc (proxy_call * pc, const char *reason) +{ + gpr_ref (&pc->refs); +} -static void on_c2p_sent_initial_metadata(void *arg, int success) { +static void +on_c2p_sent_initial_metadata (void *arg, int success) +{ proxy_call *pc = arg; - unrefpc(pc, "on_c2p_sent_initial_metadata"); + unrefpc (pc, "on_c2p_sent_initial_metadata"); } -static void on_p2s_recv_initial_metadata(void *arg, int success) { +static void +on_p2s_recv_initial_metadata (void *arg, int success) +{ proxy_call *pc = arg; grpc_op op; grpc_call_error err; - if (!pc->proxy->shutdown) { - op.op = GRPC_OP_SEND_INITIAL_METADATA; - op.flags = 0; - op.reserved = NULL; - op.data.send_initial_metadata.count = pc->p2s_initial_metadata.count; - op.data.send_initial_metadata.metadata = pc->p2s_initial_metadata.metadata; - refpc(pc, "on_c2p_sent_initial_metadata"); - err = grpc_call_start_batch( - pc->c2p, &op, 1, new_closure(on_c2p_sent_initial_metadata, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - } - - unrefpc(pc, "on_p2s_recv_initial_metadata"); + if (!pc->proxy->shutdown) + { + op.op = GRPC_OP_SEND_INITIAL_METADATA; + op.flags = 0; + op.reserved = NULL; + op.data.send_initial_metadata.count = pc->p2s_initial_metadata.count; + op.data.send_initial_metadata.metadata = pc->p2s_initial_metadata.metadata; + refpc (pc, "on_c2p_sent_initial_metadata"); + err = grpc_call_start_batch (pc->c2p, &op, 1, new_closure (on_c2p_sent_initial_metadata, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } + + unrefpc (pc, "on_p2s_recv_initial_metadata"); } -static void on_p2s_sent_initial_metadata(void *arg, int success) { +static void +on_p2s_sent_initial_metadata (void *arg, int success) +{ proxy_call *pc = arg; - unrefpc(pc, "on_p2s_sent_initial_metadata"); + unrefpc (pc, "on_p2s_sent_initial_metadata"); } -static void on_c2p_recv_msg(void *arg, int success); +static void on_c2p_recv_msg (void *arg, int success); -static void on_p2s_sent_message(void *arg, int success) { +static void +on_p2s_sent_message (void *arg, int success) +{ proxy_call *pc = arg; grpc_op op; grpc_call_error err; - grpc_byte_buffer_destroy(pc->c2p_msg); - if (!pc->proxy->shutdown && success) { - op.op = GRPC_OP_RECV_MESSAGE; - op.flags = 0; - op.reserved = NULL; - op.data.recv_message = &pc->c2p_msg; - refpc(pc, "on_c2p_recv_msg"); - err = grpc_call_start_batch(pc->c2p, &op, 1, - new_closure(on_c2p_recv_msg, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - } - - unrefpc(pc, "on_p2s_sent_message"); + grpc_byte_buffer_destroy (pc->c2p_msg); + if (!pc->proxy->shutdown && success) + { + op.op = GRPC_OP_RECV_MESSAGE; + op.flags = 0; + op.reserved = NULL; + op.data.recv_message = &pc->c2p_msg; + refpc (pc, "on_c2p_recv_msg"); + err = grpc_call_start_batch (pc->c2p, &op, 1, new_closure (on_c2p_recv_msg, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } + + unrefpc (pc, "on_p2s_sent_message"); } -static void on_p2s_sent_close(void *arg, int success) { +static void +on_p2s_sent_close (void *arg, int success) +{ proxy_call *pc = arg; - unrefpc(pc, "on_p2s_sent_close"); + unrefpc (pc, "on_p2s_sent_close"); } -static void on_c2p_recv_msg(void *arg, int success) { +static void +on_c2p_recv_msg (void *arg, int success) +{ proxy_call *pc = arg; grpc_op op; grpc_call_error err; - if (!pc->proxy->shutdown && success) { - if (pc->c2p_msg != NULL) { - op.op = GRPC_OP_SEND_MESSAGE; - op.flags = 0; - op.reserved = NULL; - op.data.send_message = pc->c2p_msg; - refpc(pc, "on_p2s_sent_message"); - err = grpc_call_start_batch(pc->p2s, &op, 1, - new_closure(on_p2s_sent_message, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - } else { - op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; - op.flags = 0; - op.reserved = NULL; - refpc(pc, "on_p2s_sent_close"); - err = grpc_call_start_batch(pc->p2s, &op, 1, - new_closure(on_p2s_sent_close, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); + if (!pc->proxy->shutdown && success) + { + if (pc->c2p_msg != NULL) + { + op.op = GRPC_OP_SEND_MESSAGE; + op.flags = 0; + op.reserved = NULL; + op.data.send_message = pc->c2p_msg; + refpc (pc, "on_p2s_sent_message"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_sent_message, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } + else + { + op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; + op.flags = 0; + op.reserved = NULL; + refpc (pc, "on_p2s_sent_close"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_sent_close, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } } - } - unrefpc(pc, "on_c2p_recv_msg"); + unrefpc (pc, "on_c2p_recv_msg"); } -static void on_p2s_recv_msg(void *arg, int success); +static void on_p2s_recv_msg (void *arg, int success); -static void on_c2p_sent_message(void *arg, int success) { +static void +on_c2p_sent_message (void *arg, int success) +{ proxy_call *pc = arg; grpc_op op; grpc_call_error err; - grpc_byte_buffer_destroy(pc->p2s_msg); - if (!pc->proxy->shutdown && success) { - op.op = GRPC_OP_RECV_MESSAGE; - op.flags = 0; - op.reserved = NULL; - op.data.recv_message = &pc->p2s_msg; - refpc(pc, "on_p2s_recv_msg"); - err = grpc_call_start_batch(pc->p2s, &op, 1, - new_closure(on_p2s_recv_msg, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - } - - unrefpc(pc, "on_c2p_sent_message"); + grpc_byte_buffer_destroy (pc->p2s_msg); + if (!pc->proxy->shutdown && success) + { + op.op = GRPC_OP_RECV_MESSAGE; + op.flags = 0; + op.reserved = NULL; + op.data.recv_message = &pc->p2s_msg; + refpc (pc, "on_p2s_recv_msg"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_recv_msg, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } + + unrefpc (pc, "on_c2p_sent_message"); } -static void on_p2s_recv_msg(void *arg, int success) { +static void +on_p2s_recv_msg (void *arg, int success) +{ proxy_call *pc = arg; grpc_op op; grpc_call_error err; - if (!pc->proxy->shutdown && success && pc->p2s_msg) { - op.op = GRPC_OP_SEND_MESSAGE; - op.flags = 0; - op.reserved = NULL; - op.data.send_message = pc->p2s_msg; - refpc(pc, "on_c2p_sent_message"); - err = grpc_call_start_batch(pc->c2p, &op, 1, - new_closure(on_c2p_sent_message, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - } - unrefpc(pc, "on_p2s_recv_msg"); + if (!pc->proxy->shutdown && success && pc->p2s_msg) + { + op.op = GRPC_OP_SEND_MESSAGE; + op.flags = 0; + op.reserved = NULL; + op.data.send_message = pc->p2s_msg; + refpc (pc, "on_c2p_sent_message"); + err = grpc_call_start_batch (pc->c2p, &op, 1, new_closure (on_c2p_sent_message, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } + unrefpc (pc, "on_p2s_recv_msg"); } -static void on_c2p_sent_status(void *arg, int success) { +static void +on_c2p_sent_status (void *arg, int success) +{ proxy_call *pc = arg; - unrefpc(pc, "on_c2p_sent_status"); + unrefpc (pc, "on_c2p_sent_status"); } -static void on_p2s_status(void *arg, int success) { +static void +on_p2s_status (void *arg, int success) +{ proxy_call *pc = arg; grpc_op op; grpc_call_error err; - if (!pc->proxy->shutdown) { - GPR_ASSERT(success); - op.op = GRPC_OP_SEND_STATUS_FROM_SERVER; - op.flags = 0; - op.reserved = NULL; - op.data.send_status_from_server.trailing_metadata_count = - pc->p2s_trailing_metadata.count; - op.data.send_status_from_server.trailing_metadata = - pc->p2s_trailing_metadata.metadata; - op.data.send_status_from_server.status = pc->p2s_status; - op.data.send_status_from_server.status_details = pc->p2s_status_details; - refpc(pc, "on_c2p_sent_status"); - err = grpc_call_start_batch(pc->c2p, &op, 1, - new_closure(on_c2p_sent_status, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - } - - unrefpc(pc, "on_p2s_status"); + if (!pc->proxy->shutdown) + { + GPR_ASSERT (success); + op.op = GRPC_OP_SEND_STATUS_FROM_SERVER; + op.flags = 0; + op.reserved = NULL; + op.data.send_status_from_server.trailing_metadata_count = pc->p2s_trailing_metadata.count; + op.data.send_status_from_server.trailing_metadata = pc->p2s_trailing_metadata.metadata; + op.data.send_status_from_server.status = pc->p2s_status; + op.data.send_status_from_server.status_details = pc->p2s_status_details; + refpc (pc, "on_c2p_sent_status"); + err = grpc_call_start_batch (pc->c2p, &op, 1, new_closure (on_c2p_sent_status, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + } + + unrefpc (pc, "on_p2s_status"); } -static void on_c2p_closed(void *arg, int success) { +static void +on_c2p_closed (void *arg, int success) +{ proxy_call *pc = arg; - unrefpc(pc, "on_c2p_closed"); + unrefpc (pc, "on_c2p_closed"); } -static void on_new_call(void *arg, int success) { +static void +on_new_call (void *arg, int success) +{ grpc_end2end_proxy *proxy = arg; grpc_call_error err; - if (success) { - grpc_op op; - proxy_call *pc = gpr_malloc(sizeof(*pc)); - memset(pc, 0, sizeof(*pc)); - pc->proxy = proxy; - GPR_SWAP(grpc_metadata_array, pc->c2p_initial_metadata, - proxy->new_call_metadata); - pc->c2p = proxy->new_call; - pc->p2s = grpc_channel_create_call( - proxy->client, pc->c2p, GRPC_PROPAGATE_DEFAULTS, proxy->cq, - proxy->new_call_details.method, proxy->new_call_details.host, - proxy->new_call_details.deadline, NULL); - gpr_ref_init(&pc->refs, 1); - - op.flags = 0; - op.reserved = NULL; - - op.op = GRPC_OP_RECV_INITIAL_METADATA; - op.data.recv_initial_metadata = &pc->p2s_initial_metadata; - refpc(pc, "on_p2s_recv_initial_metadata"); - err = grpc_call_start_batch( - pc->p2s, &op, 1, new_closure(on_p2s_recv_initial_metadata, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - - op.op = GRPC_OP_SEND_INITIAL_METADATA; - op.data.send_initial_metadata.count = pc->c2p_initial_metadata.count; - op.data.send_initial_metadata.metadata = pc->c2p_initial_metadata.metadata; - refpc(pc, "on_p2s_sent_initial_metadata"); - err = grpc_call_start_batch( - pc->p2s, &op, 1, new_closure(on_p2s_sent_initial_metadata, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - - op.op = GRPC_OP_RECV_MESSAGE; - op.data.recv_message = &pc->c2p_msg; - refpc(pc, "on_c2p_recv_msg"); - err = grpc_call_start_batch(pc->c2p, &op, 1, - new_closure(on_c2p_recv_msg, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - - op.op = GRPC_OP_RECV_MESSAGE; - op.data.recv_message = &pc->p2s_msg; - refpc(pc, "on_p2s_recv_msg"); - err = grpc_call_start_batch(pc->p2s, &op, 1, - new_closure(on_p2s_recv_msg, pc), NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - - op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; - op.data.recv_status_on_client.trailing_metadata = - &pc->p2s_trailing_metadata; - op.data.recv_status_on_client.status = &pc->p2s_status; - op.data.recv_status_on_client.status_details = &pc->p2s_status_details; - op.data.recv_status_on_client.status_details_capacity = - &pc->p2s_status_details_capacity; - refpc(pc, "on_p2s_status"); - err = grpc_call_start_batch(pc->p2s, &op, 1, new_closure(on_p2s_status, pc), - NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - - op.op = GRPC_OP_RECV_CLOSE_ON_SERVER; - op.data.recv_close_on_server.cancelled = &pc->c2p_server_cancelled; - refpc(pc, "on_c2p_closed"); - err = grpc_call_start_batch(pc->c2p, &op, 1, new_closure(on_c2p_closed, pc), - NULL); - GPR_ASSERT(err == GRPC_CALL_OK); - - request_call(proxy); - - unrefpc(pc, "init"); - } else { - GPR_ASSERT(proxy->new_call == NULL); - } + if (success) + { + grpc_op op; + proxy_call *pc = gpr_malloc (sizeof (*pc)); + memset (pc, 0, sizeof (*pc)); + pc->proxy = proxy; + GPR_SWAP (grpc_metadata_array, pc->c2p_initial_metadata, proxy->new_call_metadata); + pc->c2p = proxy->new_call; + pc->p2s = grpc_channel_create_call (proxy->client, pc->c2p, GRPC_PROPAGATE_DEFAULTS, proxy->cq, proxy->new_call_details.method, proxy->new_call_details.host, proxy->new_call_details.deadline, NULL); + gpr_ref_init (&pc->refs, 1); + + op.flags = 0; + op.reserved = NULL; + + op.op = GRPC_OP_RECV_INITIAL_METADATA; + op.data.recv_initial_metadata = &pc->p2s_initial_metadata; + refpc (pc, "on_p2s_recv_initial_metadata"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_recv_initial_metadata, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + + op.op = GRPC_OP_SEND_INITIAL_METADATA; + op.data.send_initial_metadata.count = pc->c2p_initial_metadata.count; + op.data.send_initial_metadata.metadata = pc->c2p_initial_metadata.metadata; + refpc (pc, "on_p2s_sent_initial_metadata"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_sent_initial_metadata, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + + op.op = GRPC_OP_RECV_MESSAGE; + op.data.recv_message = &pc->c2p_msg; + refpc (pc, "on_c2p_recv_msg"); + err = grpc_call_start_batch (pc->c2p, &op, 1, new_closure (on_c2p_recv_msg, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + + op.op = GRPC_OP_RECV_MESSAGE; + op.data.recv_message = &pc->p2s_msg; + refpc (pc, "on_p2s_recv_msg"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_recv_msg, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + + op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; + op.data.recv_status_on_client.trailing_metadata = &pc->p2s_trailing_metadata; + op.data.recv_status_on_client.status = &pc->p2s_status; + op.data.recv_status_on_client.status_details = &pc->p2s_status_details; + op.data.recv_status_on_client.status_details_capacity = &pc->p2s_status_details_capacity; + refpc (pc, "on_p2s_status"); + err = grpc_call_start_batch (pc->p2s, &op, 1, new_closure (on_p2s_status, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + + op.op = GRPC_OP_RECV_CLOSE_ON_SERVER; + op.data.recv_close_on_server.cancelled = &pc->c2p_server_cancelled; + refpc (pc, "on_c2p_closed"); + err = grpc_call_start_batch (pc->c2p, &op, 1, new_closure (on_c2p_closed, pc), NULL); + GPR_ASSERT (err == GRPC_CALL_OK); + + request_call (proxy); + + unrefpc (pc, "init"); + } + else + { + GPR_ASSERT (proxy->new_call == NULL); + } } -static void request_call(grpc_end2end_proxy *proxy) { +static void +request_call (grpc_end2end_proxy * proxy) +{ proxy->new_call = NULL; - GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( - proxy->server, &proxy->new_call, - &proxy->new_call_details, - &proxy->new_call_metadata, proxy->cq, - proxy->cq, new_closure(on_new_call, proxy))); + GPR_ASSERT (GRPC_CALL_OK == grpc_server_request_call (proxy->server, &proxy->new_call, &proxy->new_call_details, &proxy->new_call_metadata, proxy->cq, proxy->cq, new_closure (on_new_call, proxy))); } -static void thread_main(void *arg) { +static void +thread_main (void *arg) +{ grpc_end2end_proxy *proxy = arg; closure *cl; - for (;;) { - grpc_event ev = grpc_completion_queue_next( - proxy->cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL); - switch (ev.type) { - case GRPC_QUEUE_TIMEOUT: - gpr_log(GPR_ERROR, "Should never reach here"); - abort(); - case GRPC_QUEUE_SHUTDOWN: - return; - case GRPC_OP_COMPLETE: - cl = ev.tag; - cl->func(cl->arg, ev.success); - gpr_free(cl); - break; + for (;;) + { + grpc_event ev = grpc_completion_queue_next (proxy->cq, gpr_inf_future (GPR_CLOCK_MONOTONIC), NULL); + switch (ev.type) + { + case GRPC_QUEUE_TIMEOUT: + gpr_log (GPR_ERROR, "Should never reach here"); + abort (); + case GRPC_QUEUE_SHUTDOWN: + return; + case GRPC_OP_COMPLETE: + cl = ev.tag; + cl->func (cl->arg, ev.success); + gpr_free (cl); + break; + } } - } } -const char *grpc_end2end_proxy_get_client_target(grpc_end2end_proxy *proxy) { +const char * +grpc_end2end_proxy_get_client_target (grpc_end2end_proxy * proxy) +{ return proxy->proxy_port; } -const char *grpc_end2end_proxy_get_server_port(grpc_end2end_proxy *proxy) { +const char * +grpc_end2end_proxy_get_server_port (grpc_end2end_proxy * proxy) +{ return proxy->server_port; } diff --git a/test/core/end2end/fixtures/proxy.h b/test/core/end2end/fixtures/proxy.h index c1cf01d39a..616fa1e10e 100644 --- a/test/core/end2end/fixtures/proxy.h +++ b/test/core/end2end/fixtures/proxy.h @@ -40,16 +40,16 @@ typedef struct grpc_end2end_proxy grpc_end2end_proxy; -typedef struct grpc_end2end_proxy_def { - grpc_server *(*create_server)(const char *port); - grpc_channel *(*create_client)(const char *target); +typedef struct grpc_end2end_proxy_def +{ + grpc_server *(*create_server) (const char *port); + grpc_channel *(*create_client) (const char *target); } grpc_end2end_proxy_def; -grpc_end2end_proxy *grpc_end2end_proxy_create( - const grpc_end2end_proxy_def *def); -void grpc_end2end_proxy_destroy(grpc_end2end_proxy *proxy); +grpc_end2end_proxy *grpc_end2end_proxy_create (const grpc_end2end_proxy_def * def); +void grpc_end2end_proxy_destroy (grpc_end2end_proxy * proxy); -const char *grpc_end2end_proxy_get_client_target(grpc_end2end_proxy *proxy); -const char *grpc_end2end_proxy_get_server_port(grpc_end2end_proxy *proxy); +const char *grpc_end2end_proxy_get_client_target (grpc_end2end_proxy * proxy); +const char *grpc_end2end_proxy_get_server_port (grpc_end2end_proxy * proxy); #endif /* GRPC_TEST_CORE_END2END_FIXTURES_PROXY_H */ diff --git a/test/core/end2end/multiple_server_queues_test.c b/test/core/end2end/multiple_server_queues_test.c index 5e2eaf4ae9..4d68a2d40d 100644 --- a/test/core/end2end/multiple_server_queues_test.c +++ b/test/core/end2end/multiple_server_queues_test.c @@ -34,30 +34,31 @@ #include <grpc/grpc.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_completion_queue *cq1; grpc_completion_queue *cq2; grpc_server *server; - grpc_test_init(argc, argv); - grpc_init(); - cq1 = grpc_completion_queue_create(NULL); - cq2 = grpc_completion_queue_create(NULL); - server = grpc_server_create(NULL, NULL); - grpc_server_register_completion_queue(server, cq1, NULL); - grpc_server_add_insecure_http2_port(server, "[::]:0"); - grpc_server_register_completion_queue(server, cq2, NULL); - grpc_server_start(server); - grpc_server_shutdown_and_notify(server, cq2, NULL); - grpc_completion_queue_next(cq2, gpr_inf_future(GPR_CLOCK_REALTIME), - NULL); /* cue queue hang */ - grpc_completion_queue_shutdown(cq1); - grpc_completion_queue_shutdown(cq2); - grpc_completion_queue_next(cq1, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - grpc_completion_queue_next(cq2, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - grpc_server_destroy(server); - grpc_completion_queue_destroy(cq1); - grpc_completion_queue_destroy(cq2); - grpc_shutdown(); + grpc_test_init (argc, argv); + grpc_init (); + cq1 = grpc_completion_queue_create (NULL); + cq2 = grpc_completion_queue_create (NULL); + server = grpc_server_create (NULL, NULL); + grpc_server_register_completion_queue (server, cq1, NULL); + grpc_server_add_insecure_http2_port (server, "[::]:0"); + grpc_server_register_completion_queue (server, cq2, NULL); + grpc_server_start (server); + grpc_server_shutdown_and_notify (server, cq2, NULL); + grpc_completion_queue_next (cq2, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); /* cue queue hang */ + grpc_completion_queue_shutdown (cq1); + grpc_completion_queue_shutdown (cq2); + grpc_completion_queue_next (cq1, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + grpc_completion_queue_next (cq2, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + grpc_server_destroy (server); + grpc_completion_queue_destroy (cq1); + grpc_completion_queue_destroy (cq2); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/no_server_test.c b/test/core/end2end/no_server_test.c index c391003141..45642dca2b 100644 --- a/test/core/end2end/no_server_test.c +++ b/test/core/end2end/no_server_test.c @@ -37,12 +37,18 @@ #include "test/core/end2end/cq_verifier.h" #include "test/core/util/test_config.h" -static void *tag(gpr_intptr i) { return (void *)i; } +static void * +tag (gpr_intptr i) +{ + return (void *) i; +} -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_channel *chan; grpc_call *call; - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2); + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (2); grpc_completion_queue *cq; cq_verifier *cqv; grpc_op ops[6]; @@ -52,18 +58,17 @@ int main(int argc, char **argv) { char *details = NULL; size_t details_capacity = 0; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - grpc_metadata_array_init(&trailing_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); - cq = grpc_completion_queue_create(NULL); - cqv = cq_verifier_create(cq); + cq = grpc_completion_queue_create (NULL); + cqv = cq_verifier_create (cq); /* create a call, channel to a non existant server */ - chan = grpc_insecure_channel_create("nonexistant:54321", NULL, NULL); - call = grpc_channel_create_call(chan, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - "/Foo", "nonexistant", deadline, NULL); + chan = grpc_insecure_channel_create ("nonexistant:54321", NULL, NULL); + call = grpc_channel_create_call (chan, NULL, GRPC_PROPAGATE_DEFAULTS, cq, "/Foo", "nonexistant", deadline, NULL); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -79,28 +84,25 @@ int main(int argc, char **argv) { op->flags = 0; op->reserved = NULL; op++; - GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch( - call, ops, (size_t)(op - ops), tag(1), NULL)); + GPR_ASSERT (GRPC_CALL_OK == grpc_call_start_batch (call, ops, (size_t) (op - ops), tag (1), NULL)); /* verify that all tags get completed */ - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED); + GPR_ASSERT (status == GRPC_STATUS_DEADLINE_EXCEEDED); - grpc_completion_queue_shutdown(cq); - while ( - grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL) - .type != GRPC_QUEUE_SHUTDOWN) + grpc_completion_queue_shutdown (cq); + while (grpc_completion_queue_next (cq, gpr_inf_future (GPR_CLOCK_REALTIME), NULL).type != GRPC_QUEUE_SHUTDOWN) ; - grpc_completion_queue_destroy(cq); - grpc_call_destroy(call); - grpc_channel_destroy(chan); - cq_verifier_destroy(cqv); + grpc_completion_queue_destroy (cq); + grpc_call_destroy (call); + grpc_channel_destroy (chan); + cq_verifier_destroy (cqv); - gpr_free(details); - grpc_metadata_array_destroy(&trailing_metadata_recv); + gpr_free (details); + grpc_metadata_array_destroy (&trailing_metadata_recv); - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/end2end/tests/bad_hostname.c b/test/core/end2end/tests/bad_hostname.c index 93e2df1e43..901b4b2210 100644 --- a/test/core/end2end/tests/bad_hostname.c +++ b/test/core/end2end/tests/bad_hostname.c @@ -45,64 +45,86 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f) { +static void +simple_request_body (grpc_end2end_test_fixture f) +{ grpc_call *c; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -114,14 +136,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { char *details = NULL; size_t details_capacity = 0; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "slartibartfast.local", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "slartibartfast.local", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -146,36 +167,41 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_INVALID_ARGUMENT); + GPR_ASSERT (status == GRPC_STATUS_INVALID_ARGUMENT); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_simple_request(grpc_end2end_test_config config) { +static void +test_invoke_simple_request (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - f = begin_test(config, "test_invoke_simple_request", NULL, NULL); - simple_request_body(f); - end_test(&f); - config.tear_down_data(&f); + f = begin_test (config, "test_invoke_simple_request", NULL, NULL); + simple_request_body (f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - if (config.feature_mask & FEATURE_MASK_SUPPORTS_HOSTNAME_VERIFICATION) { - test_invoke_simple_request(config); - } +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + if (config.feature_mask & FEATURE_MASK_SUPPORTS_HOSTNAME_VERIFICATION) + { + test_invoke_simple_request (config); + } } diff --git a/test/core/end2end/tests/binary_metadata.c b/test/core/end2end/tests/binary_metadata.c index 21e0bbcc49..2b32077847 100644 --- a/test/core/end2end/tests/binary_metadata.c +++ b/test/core/end2end/tests/binary_metadata.c @@ -43,97 +43,117 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Request/response with metadata and payload.*/ -static void test_request_response_with_metadata_and_payload( - grpc_end2end_test_config config) { +static void +test_request_response_with_metadata_and_payload (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); grpc_metadata meta_c[2] = { - {"key1-bin", - "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", - 13, - 0, - {{NULL, NULL, NULL, NULL}}}, - {"key2-bin", - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d", - 14, - 0, - {{NULL, NULL, NULL, NULL}}}}; + {"key1-bin", + "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", + 13, + 0, + {{NULL, NULL, NULL, NULL}}}, + {"key2-bin", + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d", + 14, + 0, + {{NULL, NULL, NULL, NULL}}} + }; grpc_metadata meta_s[2] = { - {"key3-bin", - "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", - 15, - 0, - {{NULL, NULL, NULL, NULL}}}, - {"key4-bin", - "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", - 16, - 0, - {{NULL, NULL, NULL, NULL}}}}; - grpc_end2end_test_fixture f = begin_test( - config, "test_request_response_with_metadata_and_payload", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + {"key3-bin", + "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", + 15, + 0, + {{NULL, NULL, NULL, NULL}}}, + {"key4-bin", + "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", + 16, + 0, + {{NULL, NULL, NULL, NULL}}} + }; + grpc_end2end_test_fixture f = begin_test (config, "test_request_response_with_metadata_and_payload", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -148,14 +168,13 @@ static void test_request_response_with_metadata_and_payload( size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -191,15 +210,13 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -213,11 +230,11 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -237,53 +254,47 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); - GPR_ASSERT(contains_metadata( - &request_metadata_recv, "key1-bin", - "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc")); - GPR_ASSERT(contains_metadata( - &request_metadata_recv, "key2-bin", - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d")); - GPR_ASSERT(contains_metadata( - &initial_metadata_recv, "key3-bin", - "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee")); - GPR_ASSERT(contains_metadata( - &initial_metadata_recv, "key4-bin", - "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); + GPR_ASSERT (byte_buffer_eq_string (response_payload_recv, "hello you")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key1-bin", "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key2-bin", "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d")); + GPR_ASSERT (contains_metadata (&initial_metadata_recv, "key3-bin", "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee")); + GPR_ASSERT (contains_metadata (&initial_metadata_recv, "key4-bin", "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_request_response_with_metadata_and_payload(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_request_response_with_metadata_and_payload (config); } diff --git a/test/core/end2end/tests/call_creds.c b/test/core/end2end/tests/call_creds.c index 4e905aafc7..a8ec505ffb 100644 --- a/test/core/end2end/tests/call_creds.c +++ b/test/core/end2end/tests/call_creds.c @@ -51,120 +51,147 @@ static const char iam_selector[] = "selector"; static const char overridden_iam_token[] = "overridden_token"; static const char overridden_iam_selector[] = "overridden_selector"; -typedef enum { NONE, OVERRIDE, DESTROY } override_mode; +typedef enum +{ NONE, OVERRIDE, DESTROY } override_mode; -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - int fail_server_auth_check) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, int fail_server_auth_check) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(NULL, NULL); - config.init_client(&f, NULL); - if (fail_server_auth_check) { - grpc_arg fail_auth_arg = { - GRPC_ARG_STRING, FAIL_AUTH_CHECK_SERVER_ARG_NAME, {NULL}}; - grpc_channel_args args; - args.num_args = 1; - args.args = &fail_auth_arg; - config.init_server(&f, &args); - } else { - config.init_server(&f, NULL); - } + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (NULL, NULL); + config.init_client (&f, NULL); + if (fail_server_auth_check) + { + grpc_arg fail_auth_arg = { + GRPC_ARG_STRING, FAIL_AUTH_CHECK_SERVER_ARG_NAME, {NULL} + }; + grpc_channel_args args; + args.num_args = 1; + args.args = &fail_auth_arg; + config.init_server (&f, &args); + } + else + { + config.init_server (&f, NULL); + } return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void print_auth_context(int is_client, const grpc_auth_context *ctx) { +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); - } + 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) { +static void +test_call_creds_failure (grpc_end2end_test_config config) +{ grpc_call *c; grpc_credentials *creds = NULL; - grpc_end2end_test_fixture f = - begin_test(config, "test_call_creds_failure", 0); - gpr_timespec deadline = five_seconds_time(); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + grpc_end2end_test_fixture f = begin_test (config, "test_call_creds_failure", 0); + gpr_timespec deadline = five_seconds_time (); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); /* Try with credentials unfit to be set on a call (channel creds). */ - creds = grpc_fake_transport_security_credentials_create(); - GPR_ASSERT(grpc_call_set_credentials(c, creds) != GRPC_CALL_OK); - grpc_credentials_release(creds); + creds = grpc_fake_transport_security_credentials_create (); + GPR_ASSERT (grpc_call_set_credentials (c, creds) != GRPC_CALL_OK); + grpc_credentials_release (creds); - grpc_call_destroy(c); + grpc_call_destroy (c); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -static void request_response_with_payload_and_call_creds( - const char *test_name, grpc_end2end_test_config config, - override_mode mode) { +static void +request_response_with_payload_and_call_creds (const char *test_name, grpc_end2end_test_config config, override_mode mode) +{ grpc_call *c; grpc_call *s; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); grpc_end2end_test_fixture f; cq_verifier *cqv; grpc_op ops[6]; @@ -184,35 +211,34 @@ static void request_response_with_payload_and_call_creds( grpc_auth_context *s_auth_context = NULL; grpc_auth_context *c_auth_context = NULL; - f = begin_test(config, test_name, 0); - cqv = cq_verifier_create(f.cq); + f = begin_test (config, test_name, 0); + cqv = cq_verifier_create (f.cq); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); - creds = grpc_google_iam_credentials_create(iam_token, iam_selector, NULL); - GPR_ASSERT(creds != NULL); - GPR_ASSERT(grpc_call_set_credentials(c, creds) == GRPC_CALL_OK); - switch (mode) { + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); + creds = grpc_google_iam_credentials_create (iam_token, iam_selector, NULL); + GPR_ASSERT (creds != NULL); + GPR_ASSERT (grpc_call_set_credentials (c, creds) == GRPC_CALL_OK); + switch (mode) + { case NONE: break; case OVERRIDE: - grpc_credentials_release(creds); - creds = grpc_google_iam_credentials_create(overridden_iam_token, - overridden_iam_selector, NULL); - GPR_ASSERT(creds != NULL); - GPR_ASSERT(grpc_call_set_credentials(c, creds) == GRPC_CALL_OK); + grpc_credentials_release (creds); + creds = grpc_google_iam_credentials_create (overridden_iam_token, overridden_iam_selector, NULL); + GPR_ASSERT (creds != NULL); + GPR_ASSERT (grpc_call_set_credentials (c, creds) == GRPC_CALL_OK); break; case DESTROY: - GPR_ASSERT(grpc_call_set_credentials(c, NULL) == GRPC_CALL_OK); + GPR_ASSERT (grpc_call_set_credentials (c, NULL) == GRPC_CALL_OK); break; - } - grpc_credentials_release(creds); + } + grpc_credentials_release (creds); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -247,27 +273,25 @@ static void request_response_with_payload_and_call_creds( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); - s_auth_context = grpc_call_auth_context(s); - GPR_ASSERT(s_auth_context != NULL); - print_auth_context(0, s_auth_context); - grpc_auth_context_release(s_auth_context); - - c_auth_context = grpc_call_auth_context(c); - GPR_ASSERT(c_auth_context != NULL); - print_auth_context(1, c_auth_context); - grpc_auth_context_release(c_auth_context); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); + s_auth_context = grpc_call_auth_context (s); + GPR_ASSERT (s_auth_context != NULL); + print_auth_context (0, s_auth_context); + grpc_auth_context_release (s_auth_context); + + c_auth_context = grpc_call_auth_context (c); + GPR_ASSERT (c_auth_context != NULL); + print_auth_context (1, c_auth_context); + grpc_auth_context_release (c_auth_context); /* Cannot set creds on the server call object. */ - GPR_ASSERT(grpc_call_set_credentials(s, NULL) != GRPC_CALL_OK); + GPR_ASSERT (grpc_call_set_credentials (s, NULL) != GRPC_CALL_OK); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -280,11 +304,11 @@ static void request_response_with_payload_and_call_creds( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -304,101 +328,85 @@ static void request_response_with_payload_and_call_creds( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); - - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); - - switch (mode) { + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); + + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); + GPR_ASSERT (byte_buffer_eq_string (response_payload_recv, "hello you")); + + switch (mode) + { case NONE: - GPR_ASSERT(contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, - iam_token)); - GPR_ASSERT(contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, - iam_selector)); + GPR_ASSERT (contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, iam_token)); + GPR_ASSERT (contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, iam_selector)); break; case OVERRIDE: - GPR_ASSERT(contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, - overridden_iam_token)); - GPR_ASSERT(contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, - overridden_iam_selector)); + GPR_ASSERT (contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, overridden_iam_token)); + GPR_ASSERT (contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, overridden_iam_selector)); break; case DESTROY: - GPR_ASSERT(!contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, - iam_token)); - GPR_ASSERT(!contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, - iam_selector)); - GPR_ASSERT(!contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, - overridden_iam_token)); - GPR_ASSERT(!contains_metadata(&request_metadata_recv, - GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, - overridden_iam_selector)); + GPR_ASSERT (!contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, iam_token)); + GPR_ASSERT (!contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, iam_selector)); + GPR_ASSERT (!contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, overridden_iam_token)); + GPR_ASSERT (!contains_metadata (&request_metadata_recv, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, overridden_iam_selector)); break; - } + } - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -static void test_request_response_with_payload_and_call_creds( - grpc_end2end_test_config config) { - request_response_with_payload_and_call_creds( - "test_request_response_with_payload_and_call_creds", config, NONE); +static void +test_request_response_with_payload_and_call_creds (grpc_end2end_test_config config) +{ + request_response_with_payload_and_call_creds ("test_request_response_with_payload_and_call_creds", config, NONE); } -static void test_request_response_with_payload_and_overridden_call_creds( - grpc_end2end_test_config config) { - request_response_with_payload_and_call_creds( - "test_request_response_with_payload_and_overridden_call_creds", config, - OVERRIDE); +static void +test_request_response_with_payload_and_overridden_call_creds (grpc_end2end_test_config config) +{ + request_response_with_payload_and_call_creds ("test_request_response_with_payload_and_overridden_call_creds", config, OVERRIDE); } -static void test_request_response_with_payload_and_deleted_call_creds( - grpc_end2end_test_config config) { - request_response_with_payload_and_call_creds( - "test_request_response_with_payload_and_deleted_call_creds", config, - DESTROY); +static void +test_request_response_with_payload_and_deleted_call_creds (grpc_end2end_test_config config) +{ + request_response_with_payload_and_call_creds ("test_request_response_with_payload_and_deleted_call_creds", config, DESTROY); } -static void test_request_with_server_rejecting_client_creds( - grpc_end2end_test_config config) { +static void +test_request_with_server_rejecting_client_creds (grpc_end2end_test_config config) +{ grpc_op ops[6]; grpc_op *op; grpc_call *c; grpc_end2end_test_fixture f; - gpr_timespec deadline = five_seconds_time(); + gpr_timespec deadline = five_seconds_time (); cq_verifier *cqv; grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; @@ -409,27 +417,25 @@ static void test_request_with_server_rejecting_client_creds( char *details = NULL; size_t details_capacity = 0; grpc_byte_buffer *response_payload_recv = NULL; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); grpc_credentials *creds; - f = begin_test(config, "test_request_with_server_rejecting_client_creds", 1); - cqv = cq_verifier_create(f.cq); + f = begin_test (config, "test_request_with_server_rejecting_client_creds", 1); + cqv = cq_verifier_create (f.cq); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - creds = grpc_google_iam_credentials_create(iam_token, iam_selector, NULL); - GPR_ASSERT(creds != NULL); - GPR_ASSERT(grpc_call_set_credentials(c, creds) == GRPC_CALL_OK); - grpc_credentials_release(creds); + creds = grpc_google_iam_credentials_create (iam_token, iam_selector, NULL); + GPR_ASSERT (creds != NULL); + GPR_ASSERT (grpc_call_set_credentials (c, creds) == GRPC_CALL_OK); + grpc_credentials_release (creds); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -464,36 +470,39 @@ static void test_request_with_server_rejecting_client_creds( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(error == GRPC_CALL_OK); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (error == GRPC_CALL_OK); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNAUTHENTICATED); + GPR_ASSERT (status == GRPC_STATUS_UNAUTHENTICATED); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload_recv); - gpr_free(details); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload_recv); + gpr_free (details); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); - end_test(&f); - config.tear_down_data(&f); + cq_verifier_destroy (cqv); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - if (config.feature_mask & FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS) { - test_call_creds_failure(config); - test_request_response_with_payload_and_call_creds(config); - test_request_response_with_payload_and_overridden_call_creds(config); - test_request_response_with_payload_and_deleted_call_creds(config); - test_request_with_server_rejecting_client_creds(config); - } +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + if (config.feature_mask & FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS) + { + test_call_creds_failure (config); + test_request_response_with_payload_and_call_creds (config); + test_request_response_with_payload_and_overridden_call_creds (config); + test_request_response_with_payload_and_deleted_call_creds (config); + test_request_with_server_rejecting_client_creds (config); + } } diff --git a/test/core/end2end/tests/cancel_after_accept.c b/test/core/end2end/tests/cancel_after_accept.c index 10e62275ab..bbef223229 100644 --- a/test/core/end2end/tests/cancel_after_accept.c +++ b/test/core/end2end/tests/cancel_after_accept.c @@ -44,71 +44,91 @@ #include "test/core/end2end/cq_verifier.h" #include "test/core/end2end/tests/cancel_test_helpers.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Cancel after accept, no payload */ -static void test_cancel_after_accept(grpc_end2end_test_config config, - cancellation_mode mode) { +static void +test_cancel_after_accept (grpc_end2end_test_config config, cancellation_mode mode) +{ grpc_op ops[6]; grpc_op *op; grpc_call *c; grpc_call *s; - grpc_end2end_test_fixture f = - begin_test(config, "cancel_after_accept", NULL, NULL); - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_end2end_test_fixture f = begin_test (config, "cancel_after_accept", NULL, NULL); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; @@ -119,22 +139,19 @@ static void test_cancel_after_accept(grpc_end2end_test_config config, size_t details_capacity = 0; grpc_byte_buffer *request_payload_recv = NULL; grpc_byte_buffer *response_payload_recv = NULL; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -165,14 +182,13 @@ static void test_cancel_after_accept(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(2)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(2), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (2)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (2), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_MESSAGE; @@ -195,41 +211,44 @@ static void test_cancel_after_accept(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(3), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (3), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, NULL)); + GPR_ASSERT (GRPC_CALL_OK == mode.initiate_cancel (c, NULL)); - cq_expect_completion(cqv, tag(3), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (3), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == mode.expect_status || status == GRPC_STATUS_INTERNAL); + GPR_ASSERT (was_cancelled == 1); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); - gpr_free(details); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); + gpr_free (details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); - end_test(&f); - config.tear_down_data(&f); + cq_verifier_destroy (cqv); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { - test_cancel_after_accept(config, cancellation_modes[i]); - } + for (i = 0; i < GPR_ARRAY_SIZE (cancellation_modes); i++) + { + test_cancel_after_accept (config, cancellation_modes[i]); + } } diff --git a/test/core/end2end/tests/cancel_after_client_done.c b/test/core/end2end/tests/cancel_after_client_done.c index 4fed5be5f7..b02d40c250 100644 --- a/test/core/end2end/tests/cancel_after_client_done.c +++ b/test/core/end2end/tests/cancel_after_client_done.c @@ -44,71 +44,91 @@ #include "test/core/end2end/cq_verifier.h" #include "test/core/end2end/tests/cancel_test_helpers.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Cancel after accept with a writes closed, no payload */ -static void test_cancel_after_accept_and_writes_closed( - grpc_end2end_test_config config, cancellation_mode mode) { +static void +test_cancel_after_accept_and_writes_closed (grpc_end2end_test_config config, cancellation_mode mode) +{ grpc_op ops[6]; grpc_op *op; grpc_call *c; grpc_call *s; - grpc_end2end_test_fixture f = begin_test( - config, "test_cancel_after_accept_and_writes_closed", NULL, NULL); - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_end2end_test_fixture f = begin_test (config, "test_cancel_after_accept_and_writes_closed", NULL, NULL); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; @@ -119,22 +139,19 @@ static void test_cancel_after_accept_and_writes_closed( size_t details_capacity = 0; grpc_byte_buffer *request_payload_recv = NULL; grpc_byte_buffer *response_payload_recv = NULL; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -169,14 +186,13 @@ static void test_cancel_after_accept_and_writes_closed( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(2)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(2), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (2)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (2), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_MESSAGE; @@ -199,41 +215,44 @@ static void test_cancel_after_accept_and_writes_closed( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(3), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (3), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, NULL)); + GPR_ASSERT (GRPC_CALL_OK == mode.initiate_cancel (c, NULL)); - cq_expect_completion(cqv, tag(3), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (3), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == mode.expect_status || status == GRPC_STATUS_INTERNAL); + GPR_ASSERT (was_cancelled == 1); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); - gpr_free(details); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); + gpr_free (details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); - end_test(&f); - config.tear_down_data(&f); + cq_verifier_destroy (cqv); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { - test_cancel_after_accept_and_writes_closed(config, cancellation_modes[i]); - } + for (i = 0; i < GPR_ARRAY_SIZE (cancellation_modes); i++) + { + test_cancel_after_accept_and_writes_closed (config, cancellation_modes[i]); + } } diff --git a/test/core/end2end/tests/cancel_after_invoke.c b/test/core/end2end/tests/cancel_after_invoke.c index 40595e4c7b..4f83a3ea7c 100644 --- a/test/core/end2end/tests/cancel_after_invoke.c +++ b/test/core/end2end/tests/cancel_after_invoke.c @@ -44,71 +44,90 @@ #include "test/core/end2end/cq_verifier.h" #include "test/core/end2end/tests/cancel_test_helpers.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - cancellation_mode mode, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, cancellation_mode mode, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s/%s", test_name, config.name, mode.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s/%s", test_name, config.name, mode.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Cancel after invoke, no payload */ -static void test_cancel_after_invoke(grpc_end2end_test_config config, - cancellation_mode mode, size_t test_ops) { +static void +test_cancel_after_invoke (grpc_end2end_test_config config, cancellation_mode mode, size_t test_ops) +{ grpc_op ops[6]; grpc_op *op; grpc_call *c; - grpc_end2end_test_fixture f = - begin_test(config, "test_cancel_after_invoke", mode, NULL, NULL); - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_end2end_test_fixture f = begin_test (config, "test_cancel_after_invoke", mode, NULL, NULL); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; @@ -118,18 +137,16 @@ static void test_cancel_after_invoke(grpc_end2end_test_config config, char *details = NULL; size_t details_capacity = 0; grpc_byte_buffer *response_payload_recv = NULL; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -164,38 +181,42 @@ static void test_cancel_after_invoke(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, test_ops, tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, test_ops, tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, NULL)); + GPR_ASSERT (GRPC_CALL_OK == mode.initiate_cancel (c, NULL)); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL); + GPR_ASSERT (status == mode.expect_status || status == GRPC_STATUS_INTERNAL); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload_recv); - gpr_free(details); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload_recv); + gpr_free (details); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); - end_test(&f); - config.tear_down_data(&f); + cq_verifier_destroy (cqv); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ unsigned i, j; - for (j = 2; j < 6; j++) { - for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { - test_cancel_after_invoke(config, cancellation_modes[i], j); + for (j = 2; j < 6; j++) + { + for (i = 0; i < GPR_ARRAY_SIZE (cancellation_modes); i++) + { + test_cancel_after_invoke (config, cancellation_modes[i], j); + } } - } } diff --git a/test/core/end2end/tests/cancel_before_invoke.c b/test/core/end2end/tests/cancel_before_invoke.c index c049e0c2d6..065ed43ebd 100644 --- a/test/core/end2end/tests/cancel_before_invoke.c +++ b/test/core/end2end/tests/cancel_before_invoke.c @@ -43,70 +43,90 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Cancel before invoke */ -static void test_cancel_before_invoke(grpc_end2end_test_config config, - size_t test_ops) { +static void +test_cancel_before_invoke (grpc_end2end_test_config config, size_t test_ops) +{ grpc_op ops[6]; grpc_op *op; grpc_call *c; - grpc_end2end_test_fixture f = - begin_test(config, "cancel_before_invoke", NULL, NULL); - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_end2end_test_fixture f = begin_test (config, "cancel_before_invoke", NULL, NULL); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; @@ -116,20 +136,18 @@ static void test_cancel_before_invoke(grpc_end2end_test_config config, char *details = NULL; size_t details_capacity = 0; grpc_byte_buffer *response_payload_recv = NULL; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - GPR_ASSERT(GRPC_CALL_OK == grpc_call_cancel(c, NULL)); + GPR_ASSERT (GRPC_CALL_OK == grpc_call_cancel (c, NULL)); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -164,33 +182,36 @@ static void test_cancel_before_invoke(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, test_ops, tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, test_ops, tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_CANCELLED); + GPR_ASSERT (status == GRPC_STATUS_CANCELLED); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload_recv); - gpr_free(details); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload_recv); + gpr_free (details); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); - end_test(&f); - config.tear_down_data(&f); + cq_verifier_destroy (cqv); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ size_t i; - for (i = 1; i <= 6; i++) { - test_cancel_before_invoke(config, i); - } + for (i = 1; i <= 6; i++) + { + test_cancel_before_invoke (config, i); + } } diff --git a/test/core/end2end/tests/cancel_in_a_vacuum.c b/test/core/end2end/tests/cancel_in_a_vacuum.c index 6c63d7c0ad..512bceb6e5 100644 --- a/test/core/end2end/tests/cancel_in_a_vacuum.c +++ b/test/core/end2end/tests/cancel_in_a_vacuum.c @@ -44,86 +44,108 @@ #include "test/core/end2end/cq_verifier.h" #include "test/core/end2end/tests/cancel_test_helpers.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Cancel and do nothing */ -static void test_cancel_in_a_vacuum(grpc_end2end_test_config config, - cancellation_mode mode) { +static void +test_cancel_in_a_vacuum (grpc_end2end_test_config config, cancellation_mode mode) +{ grpc_call *c; - grpc_end2end_test_fixture f = - begin_test(config, "test_cancel_in_a_vacuum", NULL, NULL); - gpr_timespec deadline = five_seconds_time(); - cq_verifier *v_client = cq_verifier_create(f.cq); + grpc_end2end_test_fixture f = begin_test (config, "test_cancel_in_a_vacuum", NULL, NULL); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *v_client = cq_verifier_create (f.cq); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, NULL)); + GPR_ASSERT (GRPC_CALL_OK == mode.initiate_cancel (c, NULL)); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(v_client); - end_test(&f); - config.tear_down_data(&f); + cq_verifier_destroy (v_client); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { - test_cancel_in_a_vacuum(config, cancellation_modes[i]); - } + for (i = 0; i < GPR_ARRAY_SIZE (cancellation_modes); i++) + { + test_cancel_in_a_vacuum (config, cancellation_modes[i]); + } } diff --git a/test/core/end2end/tests/cancel_test_helpers.h b/test/core/end2end/tests/cancel_test_helpers.h index f8fafae597..20bcfc86aa 100644 --- a/test/core/end2end/tests/cancel_test_helpers.h +++ b/test/core/end2end/tests/cancel_test_helpers.h @@ -34,22 +34,25 @@ #ifndef GRPC_TEST_CORE_END2END_TESTS_CANCEL_TEST_HELPERS_H #define GRPC_TEST_CORE_END2END_TESTS_CANCEL_TEST_HELPERS_H -typedef struct { +typedef struct +{ const char *name; - grpc_call_error (*initiate_cancel)(grpc_call *call, void *reserved); + grpc_call_error (*initiate_cancel) (grpc_call * call, void *reserved); grpc_status_code expect_status; const char *expect_details; } cancellation_mode; -static grpc_call_error wait_for_deadline(grpc_call *call, void *reserved) { - (void)reserved; +static grpc_call_error +wait_for_deadline (grpc_call * call, void *reserved) +{ + (void) reserved; return GRPC_CALL_OK; } static const cancellation_mode cancellation_modes[] = { - {"cancel", grpc_call_cancel, GRPC_STATUS_CANCELLED, "Cancelled"}, - {"deadline", wait_for_deadline, GRPC_STATUS_DEADLINE_EXCEEDED, - "Deadline Exceeded"}, + {"cancel", grpc_call_cancel, GRPC_STATUS_CANCELLED, "Cancelled"}, + {"deadline", wait_for_deadline, GRPC_STATUS_DEADLINE_EXCEEDED, + "Deadline Exceeded"}, }; #endif /* GRPC_TEST_CORE_END2END_TESTS_CANCEL_TEST_HELPERS_H */ diff --git a/test/core/end2end/tests/census_simple_request.c b/test/core/end2end/tests/census_simple_request.c index adf855ca60..71fd9e0e49 100644 --- a/test/core/end2end/tests/census_simple_request.c +++ b/test/core/end2end/tests/census_simple_request.c @@ -45,61 +45,78 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, n_seconds_time(5), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, n_seconds_time (5), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_body(grpc_end2end_test_fixture f) { +static void +test_body (grpc_end2end_test_fixture f) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = n_seconds_time(5); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = n_seconds_time (5); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -112,15 +129,13 @@ static void test_body(grpc_end2end_test_fixture f) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -145,15 +160,13 @@ static void test_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -173,34 +186,34 @@ static void test_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_request_with_census( - grpc_end2end_test_config config, const char *name, - void (*body)(grpc_end2end_test_fixture f)) { +static void +test_invoke_request_with_census (grpc_end2end_test_config config, const char *name, void (*body) (grpc_end2end_test_fixture f)) +{ char *fullname; grpc_end2end_test_fixture f; grpc_arg client_arg, server_arg; @@ -219,14 +232,16 @@ static void test_invoke_request_with_census( server_args.num_args = 1; server_args.args = &server_arg; - gpr_asprintf(&fullname, "%s/%s", "test_invoke_request_with_census", name); - f = begin_test(config, fullname, &client_args, &server_args); - body(f); - end_test(&f); - config.tear_down_data(&f); - gpr_free(fullname); + gpr_asprintf (&fullname, "%s/%s", "test_invoke_request_with_census", name); + f = begin_test (config, fullname, &client_args, &server_args); + body (f); + end_test (&f); + config.tear_down_data (&f); + gpr_free (fullname); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_request_with_census(config, "census_simple_request", test_body); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_request_with_census (config, "census_simple_request", test_body); } diff --git a/test/core/end2end/tests/channel_connectivity.c b/test/core/end2end/tests/channel_connectivity.c index 0b7a8a664b..54e719e545 100644 --- a/test/core/end2end/tests/channel_connectivity.c +++ b/test/core/end2end/tests/channel_connectivity.c @@ -38,101 +38,95 @@ #include "test/core/end2end/cq_verifier.h" -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static void test_connectivity(grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL); +static void +test_connectivity (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = config.create_fixture (NULL, NULL); grpc_connectivity_state state; - cq_verifier *cqv = cq_verifier_create(f.cq); + cq_verifier *cqv = cq_verifier_create (f.cq); - config.init_client(&f, NULL); + config.init_client (&f, NULL); /* channels should start life in IDLE, and stay there */ - GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) == - GRPC_CHANNEL_IDLE); - gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(100)); - GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) == - GRPC_CHANNEL_IDLE); + GPR_ASSERT (grpc_channel_check_connectivity_state (f.client, 0) == GRPC_CHANNEL_IDLE); + gpr_sleep_until (GRPC_TIMEOUT_MILLIS_TO_DEADLINE (100)); + GPR_ASSERT (grpc_channel_check_connectivity_state (f.client, 0) == GRPC_CHANNEL_IDLE); /* start watching for a change */ - grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_IDLE, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), - f.cq, tag(1)); + grpc_channel_watch_connectivity_state (f.client, GRPC_CHANNEL_IDLE, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (3), f.cq, tag (1)); /* nothing should happen */ - cq_verify_empty(cqv); + cq_verify_empty (cqv); /* check that we're still in idle, and start connecting */ - GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) == - GRPC_CHANNEL_IDLE); + GPR_ASSERT (grpc_channel_check_connectivity_state (f.client, 1) == GRPC_CHANNEL_IDLE); /* and now the watch should trigger */ - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); - state = grpc_channel_check_connectivity_state(f.client, 0); - GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || - state == GRPC_CHANNEL_CONNECTING); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); + state = grpc_channel_check_connectivity_state (f.client, 0); + GPR_ASSERT (state == GRPC_CHANNEL_TRANSIENT_FAILURE || state == GRPC_CHANNEL_CONNECTING); /* quickly followed by a transition to TRANSIENT_FAILURE */ - grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_CONNECTING, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), - f.cq, tag(2)); - cq_expect_completion(cqv, tag(2), 1); - cq_verify(cqv); - state = grpc_channel_check_connectivity_state(f.client, 0); - GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || - state == GRPC_CHANNEL_CONNECTING); + grpc_channel_watch_connectivity_state (f.client, GRPC_CHANNEL_CONNECTING, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (3), f.cq, tag (2)); + cq_expect_completion (cqv, tag (2), 1); + cq_verify (cqv); + state = grpc_channel_check_connectivity_state (f.client, 0); + GPR_ASSERT (state == GRPC_CHANNEL_TRANSIENT_FAILURE || state == GRPC_CHANNEL_CONNECTING); - gpr_log(GPR_DEBUG, "*** STARTING SERVER ***"); + gpr_log (GPR_DEBUG, "*** STARTING SERVER ***"); /* now let's bring up a server to connect to */ - config.init_server(&f, NULL); + config.init_server (&f, NULL); - gpr_log(GPR_DEBUG, "*** STARTED SERVER ***"); + gpr_log (GPR_DEBUG, "*** STARTED SERVER ***"); /* we'll go through some set of transitions (some might be missed), until READY is reached */ - while (state != GRPC_CHANNEL_READY) { - grpc_channel_watch_connectivity_state( - f.client, state, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), f.cq, tag(3)); - cq_expect_completion(cqv, tag(3), 1); - cq_verify(cqv); - state = grpc_channel_check_connectivity_state(f.client, 0); - GPR_ASSERT(state == GRPC_CHANNEL_READY || - state == GRPC_CHANNEL_CONNECTING || - state == GRPC_CHANNEL_TRANSIENT_FAILURE); - } + while (state != GRPC_CHANNEL_READY) + { + grpc_channel_watch_connectivity_state (f.client, state, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (3), f.cq, tag (3)); + cq_expect_completion (cqv, tag (3), 1); + cq_verify (cqv); + state = grpc_channel_check_connectivity_state (f.client, 0); + GPR_ASSERT (state == GRPC_CHANNEL_READY || state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_TRANSIENT_FAILURE); + } /* bring down the server again */ /* we should go immediately to TRANSIENT_FAILURE */ - gpr_log(GPR_DEBUG, "*** SHUTTING DOWN SERVER ***"); + gpr_log (GPR_DEBUG, "*** SHUTTING DOWN SERVER ***"); - grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_READY, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), - f.cq, tag(4)); + grpc_channel_watch_connectivity_state (f.client, GRPC_CHANNEL_READY, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (3), f.cq, tag (4)); - grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); + grpc_server_shutdown_and_notify (f.server, f.cq, tag (0xdead)); - cq_expect_completion(cqv, tag(4), 1); - cq_expect_completion(cqv, tag(0xdead), 1); - cq_verify(cqv); - state = grpc_channel_check_connectivity_state(f.client, 0); - GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || - state == GRPC_CHANNEL_CONNECTING); + cq_expect_completion (cqv, tag (4), 1); + cq_expect_completion (cqv, tag (0xdead), 1); + cq_verify (cqv); + state = grpc_channel_check_connectivity_state (f.client, 0); + GPR_ASSERT (state == GRPC_CHANNEL_TRANSIENT_FAILURE || state == GRPC_CHANNEL_CONNECTING); /* cleanup server */ - grpc_server_destroy(f.server); + grpc_server_destroy (f.server); - gpr_log(GPR_DEBUG, "*** SHUTDOWN SERVER ***"); + gpr_log (GPR_DEBUG, "*** SHUTDOWN SERVER ***"); - grpc_channel_destroy(f.client); - grpc_completion_queue_shutdown(f.cq); - grpc_completion_queue_destroy(f.cq); - config.tear_down_data(&f); + grpc_channel_destroy (f.client); + grpc_completion_queue_shutdown (f.cq); + grpc_completion_queue_destroy (f.cq); + config.tear_down_data (&f); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); - test_connectivity(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + GPR_ASSERT (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); + test_connectivity (config); } diff --git a/test/core/end2end/tests/compressed_payload.c b/test/core/end2end/tests/compressed_payload.c index c50eaba8b2..e7c0768d22 100644 --- a/test/core/end2end/tests/compressed_payload.c +++ b/test/core/end2end/tests/compressed_payload.c @@ -48,71 +48,88 @@ #include "src/core/channel/compress_filter.h" #include "src/core/surface/call.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void request_with_payload_template( - grpc_end2end_test_config config, const char *test_name, - gpr_uint32 send_flags_bitmask, - grpc_compression_algorithm requested_compression_algorithm, - grpc_compression_algorithm expected_compression_algorithm, - grpc_metadata *client_metadata) { +static void +request_with_payload_template (grpc_end2end_test_config config, const char *test_name, gpr_uint32 send_flags_bitmask, grpc_compression_algorithm requested_compression_algorithm, grpc_compression_algorithm expected_compression_algorithm, grpc_metadata * client_metadata) +{ grpc_call *c; grpc_call *s; gpr_slice request_payload_slice; grpc_byte_buffer *request_payload; - gpr_timespec deadline = five_seconds_time(); + gpr_timespec deadline = five_seconds_time (); grpc_channel_args *client_args; grpc_channel_args *server_args; grpc_end2end_test_fixture f; @@ -131,36 +148,36 @@ static void request_with_payload_template( cq_verifier *cqv; char str[1024]; - memset(str, 'x', 1023); + memset (str, 'x', 1023); str[1023] = '\0'; - request_payload_slice = gpr_slice_from_copied_string(str); - request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1); + request_payload_slice = gpr_slice_from_copied_string (str); + request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); - client_args = grpc_channel_args_set_compression_algorithm( - NULL, requested_compression_algorithm); - server_args = grpc_channel_args_set_compression_algorithm( - NULL, requested_compression_algorithm); + client_args = grpc_channel_args_set_compression_algorithm (NULL, requested_compression_algorithm); + server_args = grpc_channel_args_set_compression_algorithm (NULL, requested_compression_algorithm); - f = begin_test(config, test_name, client_args, server_args); - cqv = cq_verifier_create(f.cq); + f = begin_test (config, test_name, client_args, server_args); + cqv = cq_verifier_create (f.cq); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; - if (client_metadata != NULL) { - op->data.send_initial_metadata.count = 1; - op->data.send_initial_metadata.metadata = client_metadata; - } else { - op->data.send_initial_metadata.count = 0; - } + if (client_metadata != NULL) + { + op->data.send_initial_metadata.count = 1; + op->data.send_initial_metadata.metadata = client_metadata; + } + else + { + op->data.send_initial_metadata.count = 0; + } op->flags = 0; op->reserved = NULL; op++; @@ -186,23 +203,18 @@ static void request_with_payload_template( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); - - GPR_ASSERT(GPR_BITCOUNT(grpc_call_get_encodings_accepted_by_peer(s)) == 3); - GPR_ASSERT(GPR_BITGET(grpc_call_get_encodings_accepted_by_peer(s), - GRPC_COMPRESS_NONE) != 0); - GPR_ASSERT(GPR_BITGET(grpc_call_get_encodings_accepted_by_peer(s), - GRPC_COMPRESS_DEFLATE) != 0); - GPR_ASSERT(GPR_BITGET(grpc_call_get_encodings_accepted_by_peer(s), - GRPC_COMPRESS_GZIP) != 0); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); + + GPR_ASSERT (GPR_BITCOUNT (grpc_call_get_encodings_accepted_by_peer (s)) == 3); + GPR_ASSERT (GPR_BITGET (grpc_call_get_encodings_accepted_by_peer (s), GRPC_COMPRESS_NONE) != 0); + GPR_ASSERT (GPR_BITGET (grpc_call_get_encodings_accepted_by_peer (s), GRPC_COMPRESS_DEFLATE) != 0); + GPR_ASSERT (GPR_BITGET (grpc_call_get_encodings_accepted_by_peer (s), GRPC_COMPRESS_GZIP) != 0); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -215,11 +227,11 @@ static void request_with_payload_template( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -234,104 +246,95 @@ static void request_with_payload_template( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); - GPR_ASSERT(request_payload_recv->type == GRPC_BB_RAW); - GPR_ASSERT(request_payload_recv->data.raw.compression == - expected_compression_algorithm); + GPR_ASSERT (request_payload_recv->type == GRPC_BB_RAW); + GPR_ASSERT (request_payload_recv->data.raw.compression == expected_compression_algorithm); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, str)); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, str)); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - gpr_slice_unref(request_payload_slice); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(request_payload_recv); + gpr_slice_unref (request_payload_slice); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (request_payload_recv); - grpc_channel_args_destroy(client_args); - grpc_channel_args_destroy(server_args); + grpc_channel_args_destroy (client_args); + grpc_channel_args_destroy (server_args); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -static void test_invoke_request_with_exceptionally_uncompressed_payload( - grpc_end2end_test_config config) { - request_with_payload_template( - config, "test_invoke_request_with_exceptionally_uncompressed_payload", - GRPC_WRITE_NO_COMPRESS, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_NONE, NULL); +static void +test_invoke_request_with_exceptionally_uncompressed_payload (grpc_end2end_test_config config) +{ + request_with_payload_template (config, "test_invoke_request_with_exceptionally_uncompressed_payload", GRPC_WRITE_NO_COMPRESS, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_NONE, NULL); } -static void test_invoke_request_with_uncompressed_payload( - grpc_end2end_test_config config) { - request_with_payload_template( - config, "test_invoke_request_with_uncompressed_payload", 0, - GRPC_COMPRESS_NONE, GRPC_COMPRESS_NONE, NULL); +static void +test_invoke_request_with_uncompressed_payload (grpc_end2end_test_config config) +{ + request_with_payload_template (config, "test_invoke_request_with_uncompressed_payload", 0, GRPC_COMPRESS_NONE, GRPC_COMPRESS_NONE, NULL); } -static void test_invoke_request_with_compressed_payload( - grpc_end2end_test_config config) { - request_with_payload_template( - config, "test_invoke_request_with_compressed_payload", 0, - GRPC_COMPRESS_GZIP, GRPC_COMPRESS_GZIP, NULL); +static void +test_invoke_request_with_compressed_payload (grpc_end2end_test_config config) +{ + request_with_payload_template (config, "test_invoke_request_with_compressed_payload", 0, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_GZIP, NULL); } -static void test_invoke_request_with_compressed_payload_md_override( - grpc_end2end_test_config config) { +static void +test_invoke_request_with_compressed_payload_md_override (grpc_end2end_test_config config) +{ grpc_metadata gzip_compression_override; grpc_metadata none_compression_override; gzip_compression_override.key = GRPC_COMPRESS_REQUEST_ALGORITHM_KEY; gzip_compression_override.value = "gzip"; gzip_compression_override.value_length = 4; - memset(&gzip_compression_override.internal_data, 0, - sizeof(gzip_compression_override.internal_data)); + memset (&gzip_compression_override.internal_data, 0, sizeof (gzip_compression_override.internal_data)); none_compression_override.key = GRPC_COMPRESS_REQUEST_ALGORITHM_KEY; none_compression_override.value = "identity"; none_compression_override.value_length = 4; - memset(&none_compression_override.internal_data, 0, - sizeof(none_compression_override.internal_data)); + memset (&none_compression_override.internal_data, 0, sizeof (none_compression_override.internal_data)); /* Channel default NONE (aka IDENTITY), call override to GZIP */ - request_with_payload_template( - config, "test_invoke_request_with_compressed_payload_md_override_1", 0, - GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, &gzip_compression_override); + request_with_payload_template (config, "test_invoke_request_with_compressed_payload_md_override_1", 0, GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, &gzip_compression_override); /* Channel default DEFLATE, call override to GZIP */ - request_with_payload_template( - config, "test_invoke_request_with_compressed_payload_md_override_2", 0, - GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP, &gzip_compression_override); + request_with_payload_template (config, "test_invoke_request_with_compressed_payload_md_override_2", 0, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP, &gzip_compression_override); /* Channel default DEFLATE, call override to NONE (aka IDENTITY) */ - request_with_payload_template( - config, "test_invoke_request_with_compressed_payload_md_override_3", 0, - GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_NONE, &none_compression_override); + request_with_payload_template (config, "test_invoke_request_with_compressed_payload_md_override_3", 0, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_NONE, &none_compression_override); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_request_with_exceptionally_uncompressed_payload(config); - test_invoke_request_with_uncompressed_payload(config); - test_invoke_request_with_compressed_payload(config); - test_invoke_request_with_compressed_payload_md_override(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_request_with_exceptionally_uncompressed_payload (config); + test_invoke_request_with_uncompressed_payload (config); + test_invoke_request_with_compressed_payload (config); + test_invoke_request_with_compressed_payload_md_override (config); } diff --git a/test/core/end2end/tests/default_host.c b/test/core/end2end/tests/default_host.c index 7b62505a9a..2bffa8d692 100644 --- a/test/core/end2end/tests/default_host.c +++ b/test/core/end2end/tests/default_host.c @@ -45,65 +45,87 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f) { +static void +simple_request_body (grpc_end2end_test_fixture f) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -117,19 +139,18 @@ static void simple_request_body(grpc_end2end_test_fixture f) { int was_cancelled = 2; char *peer; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", NULL, deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", NULL, deadline, NULL); + GPR_ASSERT (c); - peer = grpc_call_get_peer(c); - GPR_ASSERT(peer != NULL); - gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); - gpr_free(peer); + peer = grpc_call_get_peer (c); + GPR_ASSERT (peer != NULL); + gpr_log (GPR_DEBUG, "client_peer_before_call=%s", peer); + gpr_free (peer); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -154,24 +175,22 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(error == GRPC_CALL_OK); - - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(error == GRPC_CALL_OK); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); - - peer = grpc_call_get_peer(s); - GPR_ASSERT(peer != NULL); - gpr_log(GPR_DEBUG, "server_peer=%s", peer); - gpr_free(peer); - peer = grpc_call_get_peer(c); - GPR_ASSERT(peer != NULL); - gpr_log(GPR_DEBUG, "client_peer=%s", peer); - gpr_free(peer); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (error == GRPC_CALL_OK); + + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (error == GRPC_CALL_OK); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); + + peer = grpc_call_get_peer (s); + GPR_ASSERT (peer != NULL); + gpr_log (GPR_DEBUG, "server_peer=%s", peer); + gpr_free (peer); + peer = grpc_call_get_peer (c); + GPR_ASSERT (peer != NULL); + gpr_log (GPR_DEBUG, "client_peer=%s", peer); + gpr_free (peer); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -191,44 +210,48 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(error == GRPC_CALL_OK); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (error == GRPC_CALL_OK); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strncmp(call_details.host, "localhost", 9)); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strncmp (call_details.host, "localhost", 9)); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_simple_request(grpc_end2end_test_config config) { +static void +test_invoke_simple_request (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - f = begin_test(config, "test_invoke_simple_request", NULL, NULL); - simple_request_body(f); - end_test(&f); - config.tear_down_data(&f); + f = begin_test (config, "test_invoke_simple_request", NULL, NULL); + simple_request_body (f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ if ((config.feature_mask & FEATURE_MASK_SUPPORTS_HOSTNAME_VERIFICATION) != 0) return; if ((config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) == 0) return; - test_invoke_simple_request(config); + test_invoke_simple_request (config); } diff --git a/test/core/end2end/tests/disappearing_server.c b/test/core/end2end/tests/disappearing_server.c index de3ea16f66..e1758706bc 100644 --- a/test/core/end2end/tests/disappearing_server.c +++ b/test/core/end2end/tests/disappearing_server.c @@ -43,49 +43,73 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void do_request_and_shutdown_server(grpc_end2end_test_fixture *f, - cq_verifier *cqv) { +static void +do_request_and_shutdown_server (grpc_end2end_test_fixture * f, cq_verifier * cqv) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); + gpr_timespec deadline = five_seconds_time (); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -98,15 +122,13 @@ static void do_request_and_shutdown_server(grpc_end2end_test_fixture *f, size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f->client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f->client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -131,19 +153,17 @@ static void do_request_and_shutdown_server(grpc_end2end_test_fixture *f, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f->server, &s, &call_details, - &request_metadata_recv, f->cq, f->cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f->server, &s, &call_details, &request_metadata_recv, f->cq, f->cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); /* should be able to shut down the server early - and still complete the request */ - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -163,53 +183,57 @@ static void do_request_and_shutdown_server(grpc_end2end_test_fixture *f, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_expect_completion(cqv, tag(1000), 1); - cq_verify(cqv); - - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); - - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); - - grpc_call_destroy(c); - grpc_call_destroy(s); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_expect_completion (cqv, tag (1000), 1); + cq_verify (cqv); + + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); + + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); + + grpc_call_destroy (c); + grpc_call_destroy (s); } -static void disappearing_server_test(grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); +static void +disappearing_server_test (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = config.create_fixture (NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); - gpr_log(GPR_INFO, "%s/%s", "disappearing_server_test", config.name); + gpr_log (GPR_INFO, "%s/%s", "disappearing_server_test", config.name); - config.init_client(&f, NULL); - config.init_server(&f, NULL); + config.init_client (&f, NULL); + config.init_server (&f, NULL); - do_request_and_shutdown_server(&f, cqv); + do_request_and_shutdown_server (&f, cqv); /* now destroy and recreate the server */ - config.init_server(&f, NULL); + config.init_server (&f, NULL); - do_request_and_shutdown_server(&f, cqv); + do_request_and_shutdown_server (&f, cqv); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); - disappearing_server_test(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + GPR_ASSERT (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); + disappearing_server_test (config); } diff --git a/test/core/end2end/tests/empty_batch.c b/test/core/end2end/tests/empty_batch.c index c93d236a6a..a73bdc7b3f 100644 --- a/test/core/end2end/tests/empty_batch.c +++ b/test/core/end2end/tests/empty_batch.c @@ -45,90 +45,115 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void empty_batch_body(grpc_end2end_test_fixture f) { +static void +empty_batch_body (grpc_end2end_test_fixture f) +{ grpc_call *c; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_call_error error; grpc_op *op = NULL; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - error = grpc_call_start_batch(c, op, 0, tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + error = grpc_call_start_batch (c, op, 0, tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_empty_body(grpc_end2end_test_config config) { +static void +test_invoke_empty_body (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - f = begin_test(config, "test_invoke_empty_body", NULL, NULL); - empty_batch_body(f); - end_test(&f); - config.tear_down_data(&f); + f = begin_test (config, "test_invoke_empty_body", NULL, NULL); + empty_batch_body (f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_empty_body(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_empty_body (config); } diff --git a/test/core/end2end/tests/graceful_server_shutdown.c b/test/core/end2end/tests/graceful_server_shutdown.c index df420a9846..b3e30964ae 100644 --- a/test/core/end2end/tests/graceful_server_shutdown.c +++ b/test/core/end2end/tests/graceful_server_shutdown.c @@ -43,64 +43,86 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_early_server_shutdown_finishes_inflight_calls( - grpc_end2end_test_config config) { +static void +test_early_server_shutdown_finishes_inflight_calls (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - grpc_end2end_test_fixture f = begin_test( - config, "test_early_server_shutdown_finishes_inflight_calls", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + grpc_end2end_test_fixture f = begin_test (config, "test_early_server_shutdown_finishes_inflight_calls", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -113,14 +135,13 @@ static void test_early_server_shutdown_finishes_inflight_calls( size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -146,19 +167,17 @@ static void test_early_server_shutdown_finishes_inflight_calls( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); /* shutdown and destroy the server */ - grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); - cq_verify_empty(cqv); + grpc_server_shutdown_and_notify (f.server, f.cq, tag (0xdead)); + cq_verify_empty (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -178,35 +197,37 @@ static void test_early_server_shutdown_finishes_inflight_calls( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(0xdead), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (0xdead), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - grpc_call_destroy(s); + grpc_call_destroy (s); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_early_server_shutdown_finishes_inflight_calls(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_early_server_shutdown_finishes_inflight_calls (config); } diff --git a/test/core/end2end/tests/high_initial_seqno.c b/test/core/end2end/tests/high_initial_seqno.c index 44d6a60c10..4d9d09cd80 100644 --- a/test/core/end2end/tests/high_initial_seqno.c +++ b/test/core/end2end/tests/high_initial_seqno.c @@ -45,65 +45,87 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f) { +static void +simple_request_body (grpc_end2end_test_fixture f) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -116,15 +138,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -149,15 +169,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -177,33 +195,34 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_10_simple_requests(grpc_end2end_test_config config, - int initial_sequence_number) { +static void +test_invoke_10_simple_requests (grpc_end2end_test_config config, int initial_sequence_number) +{ int i; grpc_end2end_test_fixture f; grpc_arg client_arg; @@ -216,18 +235,22 @@ static void test_invoke_10_simple_requests(grpc_end2end_test_config config, client_args.num_args = 1; client_args.args = &client_arg; - f = begin_test(config, "test_invoke_10_simple_requests", &client_args, NULL); - for (i = 0; i < 10; i++) { - simple_request_body(f); - gpr_log(GPR_INFO, "Passed simple request %d", i); - } - end_test(&f); - config.tear_down_data(&f); + f = begin_test (config, "test_invoke_10_simple_requests", &client_args, NULL); + for (i = 0; i < 10; i++) + { + simple_request_body (f); + gpr_log (GPR_INFO, "Passed simple request %d", i); + } + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_10_simple_requests(config, 16777213); - if (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) { - test_invoke_10_simple_requests(config, 2147483645); - } +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_10_simple_requests (config, 16777213); + if (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) + { + test_invoke_10_simple_requests (config, 2147483645); + } } diff --git a/test/core/end2end/tests/invoke_large_request.c b/test/core/end2end/tests/invoke_large_request.c index f79d146da1..da19222c53 100644 --- a/test/core/end2end/tests/invoke_large_request.c +++ b/test/core/end2end/tests/invoke_large_request.c @@ -43,78 +43,95 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, n_seconds_time(5), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, n_seconds_time (5), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static gpr_slice large_slice(void) { - gpr_slice slice = gpr_slice_malloc(1000000); - memset(GPR_SLICE_START_PTR(slice), 'x', GPR_SLICE_LENGTH(slice)); +static gpr_slice +large_slice (void) +{ + gpr_slice slice = gpr_slice_malloc (1000000); + memset (GPR_SLICE_START_PTR (slice), 'x', GPR_SLICE_LENGTH (slice)); return slice; } -static void test_invoke_large_request(grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = - begin_test(config, "test_invoke_large_request", NULL, NULL); +static void +test_invoke_large_request (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_large_request", NULL, NULL); - gpr_slice request_payload_slice = large_slice(); - gpr_slice response_payload_slice = large_slice(); + gpr_slice request_payload_slice = large_slice (); + gpr_slice response_payload_slice = large_slice (); grpc_call *c; grpc_call *s; - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); - gpr_timespec deadline = n_seconds_time(30); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + gpr_timespec deadline = n_seconds_time (30); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -129,14 +146,13 @@ static void test_invoke_large_request(grpc_end2end_test_config config) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -171,15 +187,13 @@ static void test_invoke_large_request(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -192,11 +206,11 @@ static void test_invoke_large_request(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -216,41 +230,43 @@ static void test_invoke_large_request(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); - gpr_slice_unref(request_payload_slice); - gpr_slice_unref(response_payload_slice); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); + gpr_slice_unref (request_payload_slice); + gpr_slice_unref (response_payload_slice); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_large_request(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_large_request (config); } diff --git a/test/core/end2end/tests/large_metadata.c b/test/core/end2end/tests/large_metadata.c index 87529639a7..ff7605ebe9 100644 --- a/test/core/end2end/tests/large_metadata.c +++ b/test/core/end2end/tests/large_metadata.c @@ -43,72 +43,92 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Request with a large amount of metadata.*/ -static void test_request_with_large_metadata(grpc_end2end_test_config config) { +static void +test_request_with_large_metadata (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); grpc_metadata meta; - grpc_end2end_test_fixture f = - begin_test(config, "test_request_with_large_metadata", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_end2end_test_fixture f = begin_test (config, "test_request_with_large_metadata", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -123,20 +143,19 @@ static void test_request_with_large_metadata(grpc_end2end_test_config config) { int was_cancelled = 2; const size_t large_size = 64 * 1024; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); meta.key = "key"; - meta.value = gpr_malloc(large_size + 1); - memset((char *)meta.value, 'a', large_size); - ((char *)meta.value)[large_size] = 0; + meta.value = gpr_malloc (large_size + 1); + memset ((char *) meta.value, 'a', large_size); + ((char *) meta.value)[large_size] = 0; meta.value_length = large_size; - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -167,15 +186,13 @@ static void test_request_with_large_metadata(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -188,11 +205,11 @@ static void test_request_with_large_metadata(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -207,41 +224,43 @@ static void test_request_with_large_metadata(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(contains_metadata(&request_metadata_recv, "key", meta.value)); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key", meta.value)); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(request_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (request_payload_recv); - gpr_free((char *)meta.value); + gpr_free ((char *) meta.value); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_request_with_large_metadata(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_request_with_large_metadata (config); } diff --git a/test/core/end2end/tests/max_concurrent_streams.c b/test/core/end2end/tests/max_concurrent_streams.c index 87eb4dd451..1d4ba32ee0 100644 --- a/test/core/end2end/tests/max_concurrent_streams.c +++ b/test/core/end2end/tests/max_concurrent_streams.c @@ -43,65 +43,87 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f) { +static void +simple_request_body (grpc_end2end_test_fixture f) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -114,15 +136,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -147,15 +167,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -175,32 +193,34 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_max_concurrent_streams(grpc_end2end_test_config config) { +static void +test_max_concurrent_streams (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; grpc_arg server_arg; grpc_channel_args server_args; @@ -238,37 +258,31 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { server_args.num_args = 1; server_args.args = &server_arg; - f = begin_test(config, "test_max_concurrent_streams", NULL, &server_args); - cqv = cq_verifier_create(f.cq); + f = begin_test (config, "test_max_concurrent_streams", NULL, &server_args); + cqv = cq_verifier_create (f.cq); - grpc_metadata_array_init(&request_metadata_recv); - grpc_metadata_array_init(&initial_metadata_recv1); - grpc_metadata_array_init(&trailing_metadata_recv1); - grpc_metadata_array_init(&initial_metadata_recv2); - grpc_metadata_array_init(&trailing_metadata_recv2); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&request_metadata_recv); + grpc_metadata_array_init (&initial_metadata_recv1); + grpc_metadata_array_init (&trailing_metadata_recv1); + grpc_metadata_array_init (&initial_metadata_recv2); + grpc_metadata_array_init (&trailing_metadata_recv2); + grpc_call_details_init (&call_details); /* perform a ping-pong to ensure that settings have had a chance to round trip */ - simple_request_body(f); + simple_request_body (f); /* perform another one to make sure that the one stream case still works */ - simple_request_body(f); + simple_request_body (f); /* start two requests - ensuring that the second is not accepted until the first completes */ - deadline = n_seconds_time(1000); - c1 = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/alpha", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c1); - c2 = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/beta", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c2); - - GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( - f.server, &s1, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101))); + deadline = n_seconds_time (1000); + c1 = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/alpha", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c1); + c2 = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/beta", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c2); + + GPR_ASSERT (GRPC_CALL_OK == grpc_server_request_call (f.server, &s1, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101))); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -280,8 +294,8 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c1, ops, (size_t)(op - ops), tag(301), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c1, ops, (size_t) (op - ops), tag (301), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -297,8 +311,8 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c1, ops, (size_t)(op - ops), tag(302), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c1, ops, (size_t) (op - ops), tag (302), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -310,8 +324,8 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c2, ops, (size_t)(op - ops), tag(401), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c2, ops, (size_t) (op - ops), tag (401), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); op = ops; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; @@ -327,32 +341,35 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c2, ops, (size_t)(op - ops), tag(402), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c2, ops, (size_t) (op - ops), tag (402), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); got_client_start = 0; got_server_start = 0; live_call = -1; - while (!got_client_start || !got_server_start) { - ev = grpc_completion_queue_next(f.cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), - NULL); - GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); - GPR_ASSERT(ev.success); - if (ev.tag == tag(101)) { - GPR_ASSERT(!got_server_start); - got_server_start = 1; - } else { - GPR_ASSERT(!got_client_start); - GPR_ASSERT(ev.tag == tag(301) || ev.tag == tag(401)); - /* The /alpha or /beta calls started above could be invoked (but NOT - * both); - * check this here */ - /* We'll get tag 303 or 403, we want 300, 400 */ - live_call = ((int)(gpr_intptr)ev.tag) - 1; - got_client_start = 1; + while (!got_client_start || !got_server_start) + { + ev = grpc_completion_queue_next (f.cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (3), NULL); + GPR_ASSERT (ev.type == GRPC_OP_COMPLETE); + GPR_ASSERT (ev.success); + if (ev.tag == tag (101)) + { + GPR_ASSERT (!got_server_start); + got_server_start = 1; + } + else + { + GPR_ASSERT (!got_client_start); + GPR_ASSERT (ev.tag == tag (301) || ev.tag == tag (401)); + /* The /alpha or /beta calls started above could be invoked (but NOT + * both); + * check this here */ + /* We'll get tag 303 or 403, we want 300, 400 */ + live_call = ((int) (gpr_intptr) ev.tag) - 1; + got_client_start = 1; + } } - } - GPR_ASSERT(live_call == 300 || live_call == 400); + GPR_ASSERT (live_call == 300 || live_call == 400); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -372,21 +389,19 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s1, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s1, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(live_call + 2), 1); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (live_call + 2), 1); /* first request is finished, we should be able to start the second */ live_call = (live_call == 300) ? 400 : 300; - cq_expect_completion(cqv, tag(live_call + 1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (live_call + 1), 1); + cq_verify (cqv); - GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( - f.server, &s2, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(201))); - cq_expect_completion(cqv, tag(201), 1); - cq_verify(cqv); + GPR_ASSERT (GRPC_CALL_OK == grpc_server_request_call (f.server, &s2, &call_details, &request_metadata_recv, f.cq, f.cq, tag (201))); + cq_expect_completion (cqv, tag (201), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -406,33 +421,35 @@ static void test_max_concurrent_streams(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s2, ops, (size_t)(op - ops), tag(202), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - cq_expect_completion(cqv, tag(live_call + 2), 1); - cq_expect_completion(cqv, tag(202), 1); - cq_verify(cqv); - - cq_verifier_destroy(cqv); - - grpc_call_destroy(c1); - grpc_call_destroy(s1); - grpc_call_destroy(c2); - grpc_call_destroy(s2); - - gpr_free(details1); - gpr_free(details2); - grpc_metadata_array_destroy(&initial_metadata_recv1); - grpc_metadata_array_destroy(&trailing_metadata_recv1); - grpc_metadata_array_destroy(&initial_metadata_recv2); - grpc_metadata_array_destroy(&trailing_metadata_recv2); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); - - end_test(&f); - config.tear_down_data(&f); + error = grpc_call_start_batch (s2, ops, (size_t) (op - ops), tag (202), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + cq_expect_completion (cqv, tag (live_call + 2), 1); + cq_expect_completion (cqv, tag (202), 1); + cq_verify (cqv); + + cq_verifier_destroy (cqv); + + grpc_call_destroy (c1); + grpc_call_destroy (s1); + grpc_call_destroy (c2); + grpc_call_destroy (s2); + + gpr_free (details1); + gpr_free (details2); + grpc_metadata_array_destroy (&initial_metadata_recv1); + grpc_metadata_array_destroy (&trailing_metadata_recv1); + grpc_metadata_array_destroy (&initial_metadata_recv2); + grpc_metadata_array_destroy (&trailing_metadata_recv2); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); + + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_max_concurrent_streams(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_max_concurrent_streams (config); } diff --git a/test/core/end2end/tests/max_message_length.c b/test/core/end2end/tests/max_message_length.c index 782b9d0f26..e1949692ca 100644 --- a/test/core/end2end/tests/max_message_length.c +++ b/test/core/end2end/tests/max_message_length.c @@ -43,61 +43,83 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_max_message_length(grpc_end2end_test_config config) { +static void +test_max_message_length (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; grpc_arg server_arg; grpc_channel_args server_args; @@ -106,9 +128,8 @@ static void test_max_message_length(grpc_end2end_test_config config) { cq_verifier *cqv; grpc_op ops[6]; grpc_op *op; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; @@ -126,18 +147,16 @@ static void test_max_message_length(grpc_end2end_test_config config) { server_args.num_args = 1; server_args.args = &server_arg; - f = begin_test(config, "test_max_message_length", NULL, &server_args); - cqv = cq_verifier_create(f.cq); + f = begin_test (config, "test_max_message_length", NULL, &server_args); + cqv = cq_verifier_create (f.cq); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", - gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -167,15 +186,13 @@ static void test_max_message_length(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -183,34 +200,36 @@ static void test_max_message_length(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status != GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status != GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); - grpc_byte_buffer_destroy(request_payload); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); + grpc_byte_buffer_destroy (request_payload); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_max_message_length(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_max_message_length (config); } diff --git a/test/core/end2end/tests/metadata.c b/test/core/end2end/tests/metadata.c index d0604cc6b6..b1aefa77c6 100644 --- a/test/core/end2end/tests/metadata.c +++ b/test/core/end2end/tests/metadata.c @@ -43,81 +43,101 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Request/response with metadata and payload.*/ -static void test_request_response_with_metadata_and_payload( - grpc_end2end_test_config config) { +static void +test_request_response_with_metadata_and_payload (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); grpc_metadata meta_c[2] = { - {"key1", "val1", 4, 0, {{NULL, NULL, NULL, NULL}}}, - {"key2", "val2", 4, 0, {{NULL, NULL, NULL, NULL}}}}; + {"key1", "val1", 4, 0, {{NULL, NULL, NULL, NULL}}}, + {"key2", "val2", 4, 0, {{NULL, NULL, NULL, NULL}}} + }; grpc_metadata meta_s[2] = { - {"KeY3", "val3", 4, 0, {{NULL, NULL, NULL, NULL}}}, - {"KeY4", "val4", 4, 0, {{NULL, NULL, NULL, NULL}}}}; - grpc_end2end_test_fixture f = begin_test( - config, "test_request_response_with_metadata_and_payload", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + {"KeY3", "val3", 4, 0, {{NULL, NULL, NULL, NULL}}}, + {"KeY4", "val4", 4, 0, {{NULL, NULL, NULL, NULL}}} + }; + grpc_end2end_test_fixture f = begin_test (config, "test_request_response_with_metadata_and_payload", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -132,14 +152,13 @@ static void test_request_response_with_metadata_and_payload( size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -175,15 +194,13 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -197,11 +214,11 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -221,45 +238,47 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); - GPR_ASSERT(contains_metadata(&request_metadata_recv, "key1", "val1")); - GPR_ASSERT(contains_metadata(&request_metadata_recv, "key2", "val2")); - GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key3", "val3")); - GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key4", "val4")); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); + GPR_ASSERT (byte_buffer_eq_string (response_payload_recv, "hello you")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key1", "val1")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key2", "val2")); + GPR_ASSERT (contains_metadata (&initial_metadata_recv, "key3", "val3")); + GPR_ASSERT (contains_metadata (&initial_metadata_recv, "key4", "val4")); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_request_response_with_metadata_and_payload(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_request_response_with_metadata_and_payload (config); } diff --git a/test/core/end2end/tests/no_op.c b/test/core/end2end/tests/no_op.c index 157d0d5349..0f15963a4a 100644 --- a/test/core/end2end/tests/no_op.c +++ b/test/core/end2end/tests/no_op.c @@ -43,64 +43,90 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_no_op(grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = begin_test(config, "no-op", NULL, NULL); - end_test(&f); - config.tear_down_data(&f); +static void +test_no_op (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = begin_test (config, "no-op", NULL, NULL); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { test_no_op(config); } +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_no_op (config); +} diff --git a/test/core/end2end/tests/payload.c b/test/core/end2end/tests/payload.c index 0b303268fc..1674974135 100644 --- a/test/core/end2end/tests/payload.c +++ b/test/core/end2end/tests/payload.c @@ -43,71 +43,91 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void request_response_with_payload(grpc_end2end_test_fixture f) { - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); +static void +request_response_with_payload (grpc_end2end_test_fixture f) +{ + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); grpc_call *c; grpc_call *s; - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -122,14 +142,13 @@ static void request_response_with_payload(grpc_end2end_test_fixture f) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -164,15 +183,13 @@ static void request_response_with_payload(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -185,11 +202,11 @@ static void request_response_with_payload(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -209,62 +226,65 @@ static void request_response_with_payload(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); + GPR_ASSERT (byte_buffer_eq_string (response_payload_recv, "hello you")); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); } /* Client sends a request with payload, server reads then returns a response payload and status. */ -static void test_invoke_request_response_with_payload( - grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = begin_test( - config, "test_invoke_request_response_with_payload", NULL, NULL); - request_response_with_payload(f); - end_test(&f); - config.tear_down_data(&f); +static void +test_invoke_request_response_with_payload (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_request_response_with_payload", NULL, NULL); + request_response_with_payload (f); + end_test (&f); + config.tear_down_data (&f); } -static void test_invoke_10_request_response_with_payload( - grpc_end2end_test_config config) { +static void +test_invoke_10_request_response_with_payload (grpc_end2end_test_config config) +{ int i; - grpc_end2end_test_fixture f = begin_test( - config, "test_invoke_10_request_response_with_payload", NULL, NULL); - for (i = 0; i < 10; i++) { - request_response_with_payload(f); - } - end_test(&f); - config.tear_down_data(&f); + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_10_request_response_with_payload", NULL, NULL); + for (i = 0; i < 10; i++) + { + request_response_with_payload (f); + } + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_request_response_with_payload(config); - test_invoke_10_request_response_with_payload(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_request_response_with_payload (config); + test_invoke_10_request_response_with_payload (config); } diff --git a/test/core/end2end/tests/ping_pong_streaming.c b/test/core/end2end/tests/ping_pong_streaming.c index 39682d8bc5..c3431dd03d 100644 --- a/test/core/end2end/tests/ping_pong_streaming.c +++ b/test/core/end2end/tests/ping_pong_streaming.c @@ -43,69 +43,89 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Client pings and server pongs. Repeat messages rounds before finishing. */ -static void test_pingpong_streaming(grpc_end2end_test_config config, - int messages) { - grpc_end2end_test_fixture f = - begin_test(config, "test_pingpong_streaming", NULL, NULL); +static void +test_pingpong_streaming (grpc_end2end_test_config config, int messages) +{ + grpc_end2end_test_fixture f = begin_test (config, "test_pingpong_streaming", NULL, NULL); grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -122,18 +142,16 @@ static void test_pingpong_streaming(grpc_end2end_test_config config, grpc_byte_buffer *response_payload; grpc_byte_buffer *response_payload_recv; int i; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -154,15 +172,13 @@ static void test_pingpong_streaming(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(100)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(100), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (100)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (100), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -175,66 +191,67 @@ static void test_pingpong_streaming(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(101), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - for (i = 0; i < messages; i++) { - request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1); - response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1); - - op = ops; - op->op = GRPC_OP_SEND_MESSAGE; - op->data.send_message = request_payload; - op->flags = 0; - op->reserved = NULL; - op++; - op->op = GRPC_OP_RECV_MESSAGE; - op->data.recv_message = &response_payload_recv; - op->flags = 0; - op->reserved = NULL; - op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - op = ops; - op->op = GRPC_OP_RECV_MESSAGE; - op->data.recv_message = &request_payload_recv; - op->flags = 0; - op->reserved = NULL; - op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); - - op = ops; - op->op = GRPC_OP_SEND_MESSAGE; - op->data.send_message = response_payload; - op->flags = 0; - op->reserved = NULL; - op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(2), 1); - cq_verify(cqv); - - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); - } - - gpr_slice_unref(request_payload_slice); - gpr_slice_unref(response_payload_slice); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (101), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + for (i = 0; i < messages; i++) + { + request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + + op = ops; + op->op = GRPC_OP_SEND_MESSAGE; + op->data.send_message = request_payload; + op->flags = 0; + op->reserved = NULL; + op++; + op->op = GRPC_OP_RECV_MESSAGE; + op->data.recv_message = &response_payload_recv; + op->flags = 0; + op->reserved = NULL; + op++; + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (2), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + op = ops; + op->op = GRPC_OP_RECV_MESSAGE; + op->data.recv_message = &request_payload_recv; + op->flags = 0; + op->reserved = NULL; + op++; + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); + + op = ops; + op->op = GRPC_OP_SEND_MESSAGE; + op->data.send_message = response_payload; + op->flags = 0; + op->reserved = NULL; + op++; + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (2), 1); + cq_verify (cqv); + + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); + } + + gpr_slice_unref (request_payload_slice); + gpr_slice_unref (response_payload_slice); op = ops; op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (3), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); op = ops; op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; @@ -244,34 +261,37 @@ static void test_pingpong_streaming(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (104), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(1), 1); - cq_expect_completion(cqv, tag(3), 1); - cq_expect_completion(cqv, tag(101), 1); - cq_expect_completion(cqv, tag(104), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_expect_completion (cqv, tag (3), 1); + cq_expect_completion (cqv, tag (101), 1); + cq_expect_completion (cqv, tag (104), 1); + cq_verify (cqv); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); - gpr_free(details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); + gpr_free (details); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ int i; - for (i = 1; i < 10; i++) { - test_pingpong_streaming(config, i); - } + for (i = 1; i < 10; i++) + { + test_pingpong_streaming (config, i); + } } diff --git a/test/core/end2end/tests/registered_call.c b/test/core/end2end/tests/registered_call.c index b3156e0248..b30498a990 100644 --- a/test/core/end2end/tests/registered_call.c +++ b/test/core/end2end/tests/registered_call.c @@ -45,65 +45,87 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f, void *rc) { +static void +simple_request_body (grpc_end2end_test_fixture f, void *rc) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -116,14 +138,13 @@ static void simple_request_body(grpc_end2end_test_fixture f, void *rc) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_registered_call( - f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, rc, deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_registered_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, rc, deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -148,15 +169,13 @@ static void simple_request_body(grpc_end2end_test_fixture f, void *rc) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -176,58 +195,63 @@ static void simple_request_body(grpc_end2end_test_fixture f, void *rc) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_simple_request(grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = - begin_test(config, "test_invoke_simple_request", NULL, NULL); - void *rc = grpc_channel_register_call(f.client, "/foo", - "foo.test.google.fr:1234", NULL); +static void +test_invoke_simple_request (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_simple_request", NULL, NULL); + void *rc = grpc_channel_register_call (f.client, "/foo", + "foo.test.google.fr:1234", NULL); - simple_request_body(f, rc); - end_test(&f); - config.tear_down_data(&f); + simple_request_body (f, rc); + end_test (&f); + config.tear_down_data (&f); } -static void test_invoke_10_simple_requests(grpc_end2end_test_config config) { +static void +test_invoke_10_simple_requests (grpc_end2end_test_config config) +{ int i; - grpc_end2end_test_fixture f = - begin_test(config, "test_invoke_10_simple_requests", NULL, NULL); - void *rc = grpc_channel_register_call(f.client, "/foo", - "foo.test.google.fr:1234", NULL); - - for (i = 0; i < 10; i++) { - simple_request_body(f, rc); - gpr_log(GPR_INFO, "Passed simple request %d", i); - } - end_test(&f); - config.tear_down_data(&f); + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_10_simple_requests", NULL, NULL); + void *rc = grpc_channel_register_call (f.client, "/foo", + "foo.test.google.fr:1234", NULL); + + for (i = 0; i < 10; i++) + { + simple_request_body (f, rc); + gpr_log (GPR_INFO, "Passed simple request %d", i); + } + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_simple_request(config); - test_invoke_10_simple_requests(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_simple_request (config); + test_invoke_10_simple_requests (config); } diff --git a/test/core/end2end/tests/request_with_flags.c b/test/core/end2end/tests/request_with_flags.c index b9cdf5168c..49474bb14f 100644 --- a/test/core/end2end/tests/request_with_flags.c +++ b/test/core/end2end/tests/request_with_flags.c @@ -44,71 +44,89 @@ #include "src/core/transport/stream_op.h" #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_invoke_request_with_flags( - grpc_end2end_test_config config, gpr_uint32 *flags_for_op, - grpc_call_error call_start_batch_expected_result) { +static void +test_invoke_request_with_flags (grpc_end2end_test_config config, gpr_uint32 * flags_for_op, grpc_call_error call_start_batch_expected_result) +{ grpc_call *c; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - gpr_timespec deadline = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(10); - grpc_end2end_test_fixture f = - begin_test(config, "test_invoke_request_with_flags", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + gpr_timespec deadline = GRPC_TIMEOUT_MILLIS_TO_DEADLINE (10); + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_request_with_flags", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -122,14 +140,13 @@ static void test_invoke_request_with_flags( size_t details_capacity = 0; grpc_call_error expectation; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -160,57 +177,63 @@ static void test_invoke_request_with_flags( op->reserved = NULL; op++; expectation = call_start_batch_expected_result; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(expectation == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (expectation == error); - if (expectation == GRPC_CALL_OK) { - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); - } + if (expectation == GRPC_CALL_OK) + { + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); + } - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); + grpc_call_destroy (c); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(request_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (request_payload_recv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ size_t i; gpr_uint32 flags_for_op[GRPC_OP_RECV_CLOSE_ON_SERVER + 1]; { /* check that all grpc_op_types fail when their flag value is set to an * invalid value */ - int indices[] = {GRPC_OP_SEND_INITIAL_METADATA, GRPC_OP_SEND_MESSAGE, - GRPC_OP_SEND_CLOSE_FROM_CLIENT, - GRPC_OP_RECV_INITIAL_METADATA, - GRPC_OP_RECV_STATUS_ON_CLIENT}; - for (i = 0; i < GPR_ARRAY_SIZE(indices); ++i) { - memset(flags_for_op, 0, sizeof(flags_for_op)); - flags_for_op[indices[i]] = 0xDEADBEEF; - test_invoke_request_with_flags(config, flags_for_op, - GRPC_CALL_ERROR_INVALID_FLAGS); - } + int indices[] = { GRPC_OP_SEND_INITIAL_METADATA, GRPC_OP_SEND_MESSAGE, + GRPC_OP_SEND_CLOSE_FROM_CLIENT, + GRPC_OP_RECV_INITIAL_METADATA, + GRPC_OP_RECV_STATUS_ON_CLIENT + }; + for (i = 0; i < GPR_ARRAY_SIZE (indices); ++i) + { + memset (flags_for_op, 0, sizeof (flags_for_op)); + flags_for_op[indices[i]] = 0xDEADBEEF; + test_invoke_request_with_flags (config, flags_for_op, GRPC_CALL_ERROR_INVALID_FLAGS); + } } { /* check valid operation with allowed flags for GRPC_OP_SEND_BUFFER */ - gpr_uint32 flags[] = {GRPC_WRITE_BUFFER_HINT, GRPC_WRITE_NO_COMPRESS, - GRPC_WRITE_INTERNAL_COMPRESS}; - for (i = 0; i < GPR_ARRAY_SIZE(flags); ++i) { - memset(flags_for_op, 0, sizeof(flags_for_op)); - flags_for_op[GRPC_OP_SEND_MESSAGE] = flags[i]; - test_invoke_request_with_flags(config, flags_for_op, GRPC_CALL_OK); - } + gpr_uint32 flags[] = { GRPC_WRITE_BUFFER_HINT, GRPC_WRITE_NO_COMPRESS, + GRPC_WRITE_INTERNAL_COMPRESS + }; + for (i = 0; i < GPR_ARRAY_SIZE (flags); ++i) + { + memset (flags_for_op, 0, sizeof (flags_for_op)); + flags_for_op[GRPC_OP_SEND_MESSAGE] = flags[i]; + test_invoke_request_with_flags (config, flags_for_op, GRPC_CALL_OK); + } } } diff --git a/test/core/end2end/tests/request_with_payload.c b/test/core/end2end/tests/request_with_payload.c index a323b43ab0..bbfbae8140 100644 --- a/test/core/end2end/tests/request_with_payload.c +++ b/test/core/end2end/tests/request_with_payload.c @@ -43,71 +43,91 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Client sends a request with payload, server reads then returns status. */ -static void test_invoke_request_with_payload(grpc_end2end_test_config config) { +static void +test_invoke_request_with_payload (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); - grpc_end2end_test_fixture f = - begin_test(config, "test_invoke_request_with_payload", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_request_with_payload", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -121,14 +141,13 @@ static void test_invoke_request_with_payload(grpc_end2end_test_config config) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -158,14 +177,12 @@ static void test_invoke_request_with_payload(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( - f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101))); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + GPR_ASSERT (GRPC_CALL_OK == grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101))); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -178,11 +195,11 @@ static void test_invoke_request_with_payload(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -197,38 +214,40 @@ static void test_invoke_request_with_payload(grpc_end2end_test_config config) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 0); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(request_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (request_payload_recv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_request_with_payload(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_request_with_payload (config); } diff --git a/test/core/end2end/tests/server_finishes_request.c b/test/core/end2end/tests/server_finishes_request.c index bdc18a5e38..14830735a2 100644 --- a/test/core/end2end/tests/server_finishes_request.c +++ b/test/core/end2end/tests/server_finishes_request.c @@ -45,65 +45,87 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f) { +static void +simple_request_body (grpc_end2end_test_fixture f) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -116,15 +138,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -145,15 +165,13 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -173,40 +191,44 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_simple_request(grpc_end2end_test_config config) { +static void +test_invoke_simple_request (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - f = begin_test(config, "test_invoke_simple_request", NULL, NULL); - simple_request_body(f); - end_test(&f); - config.tear_down_data(&f); + f = begin_test (config, "test_invoke_simple_request", NULL, NULL); + simple_request_body (f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_invoke_simple_request(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_invoke_simple_request (config); } diff --git a/test/core/end2end/tests/shutdown_finishes_calls.c b/test/core/end2end/tests/shutdown_finishes_calls.c index ad7def09a9..a9d8535e31 100644 --- a/test/core/end2end/tests/shutdown_finishes_calls.c +++ b/test/core/end2end/tests/shutdown_finishes_calls.c @@ -43,57 +43,76 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_early_server_shutdown_finishes_inflight_calls( - grpc_end2end_test_config config) { +static void +test_early_server_shutdown_finishes_inflight_calls (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - grpc_end2end_test_fixture f = begin_test( - config, "test_early_server_shutdown_finishes_inflight_calls", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + grpc_end2end_test_fixture f = begin_test (config, "test_early_server_shutdown_finishes_inflight_calls", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -106,14 +125,13 @@ static void test_early_server_shutdown_finishes_inflight_calls( size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -139,15 +157,13 @@ static void test_early_server_shutdown_finishes_inflight_calls( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -155,40 +171,42 @@ static void test_early_server_shutdown_finishes_inflight_calls( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); /* shutdown and destroy the server */ - grpc_server_shutdown_and_notify(f.server, f.cq, tag(1000)); - grpc_server_cancel_all_calls(f.server); + grpc_server_shutdown_and_notify (f.server, f.cq, tag (1000)); + grpc_server_cancel_all_calls (f.server); - cq_expect_completion(cqv, tag(1000), 1); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1000), 1); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - grpc_server_destroy(f.server); + grpc_server_destroy (f.server); - GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNAVAILABLE); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_early_server_shutdown_finishes_inflight_calls(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_early_server_shutdown_finishes_inflight_calls (config); } diff --git a/test/core/end2end/tests/shutdown_finishes_tags.c b/test/core/end2end/tests/shutdown_finishes_tags.c index 9b678a1754..9569d42424 100644 --- a/test/core/end2end/tests/shutdown_finishes_tags.c +++ b/test/core/end2end/tests/shutdown_finishes_tags.c @@ -43,79 +43,98 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void test_early_server_shutdown_finishes_tags( - grpc_end2end_test_config config) { - grpc_end2end_test_fixture f = begin_test( - config, "test_early_server_shutdown_finishes_tags", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); - grpc_call *s = (void *)1; +static void +test_early_server_shutdown_finishes_tags (grpc_end2end_test_config config) +{ + grpc_end2end_test_fixture f = begin_test (config, "test_early_server_shutdown_finishes_tags", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); + grpc_call *s = (void *) 1; grpc_call_details call_details; grpc_metadata_array request_metadata_recv; - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); /* upon shutdown, the server should finish all requested calls indicating no new call */ - GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( - f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101))); - grpc_server_shutdown_and_notify(f.server, f.cq, tag(1000)); - cq_expect_completion(cqv, tag(101), 0); - cq_expect_completion(cqv, tag(1000), 1); - cq_verify(cqv); - GPR_ASSERT(s == NULL); - - grpc_server_destroy(f.server); - - end_test(&f); - config.tear_down_data(&f); - cq_verifier_destroy(cqv); + GPR_ASSERT (GRPC_CALL_OK == grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101))); + grpc_server_shutdown_and_notify (f.server, f.cq, tag (1000)); + cq_expect_completion (cqv, tag (101), 0); + cq_expect_completion (cqv, tag (1000), 1); + cq_verify (cqv); + GPR_ASSERT (s == NULL); + + grpc_server_destroy (f.server); + + end_test (&f); + config.tear_down_data (&f); + cq_verifier_destroy (cqv); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_early_server_shutdown_finishes_tags(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_early_server_shutdown_finishes_tags (config); } diff --git a/test/core/end2end/tests/simple_delayed_request.c b/test/core/end2end/tests/simple_delayed_request.c index 14e0ac8f66..ea21827b3e 100644 --- a/test/core/end2end/tests/simple_delayed_request.c +++ b/test/core/end2end/tests/simple_delayed_request.c @@ -43,57 +43,76 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_delayed_request_body(grpc_end2end_test_config config, - grpc_end2end_test_fixture *f, - grpc_channel_args *client_args, - grpc_channel_args *server_args, - long delay_us) { +static void +simple_delayed_request_body (grpc_end2end_test_config config, grpc_end2end_test_fixture * f, grpc_channel_args * client_args, grpc_channel_args * server_args, long delay_us) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f->cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f->cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -106,16 +125,15 @@ static void simple_delayed_request_body(grpc_end2end_test_config config, size_t details_capacity = 0; int was_cancelled = 2; - config.init_client(f, client_args); + config.init_client (f, client_args); - c = grpc_channel_create_call(f->client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f->client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -140,17 +158,15 @@ static void simple_delayed_request_body(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - config.init_server(f, server_args); + config.init_server (f, server_args); - error = - grpc_server_request_call(f->server, &s, &call_details, - &request_metadata_recv, f->cq, f->cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f->server, &s, &call_details, &request_metadata_recv, f->cq, f->cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -170,54 +186,60 @@ static void simple_delayed_request_body(grpc_end2end_test_config config, op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_simple_delayed_request_short(grpc_end2end_test_config config) { +static void +test_simple_delayed_request_short (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", "test_simple_delayed_request_short", config.name); - f = config.create_fixture(NULL, NULL); - simple_delayed_request_body(config, &f, NULL, NULL, 100000); - end_test(&f); - config.tear_down_data(&f); + gpr_log (GPR_INFO, "%s/%s", "test_simple_delayed_request_short", config.name); + f = config.create_fixture (NULL, NULL); + simple_delayed_request_body (config, &f, NULL, NULL, 100000); + end_test (&f); + config.tear_down_data (&f); } -static void test_simple_delayed_request_long(grpc_end2end_test_config config) { +static void +test_simple_delayed_request_long (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", "test_simple_delayed_request_long", config.name); - f = config.create_fixture(NULL, NULL); + gpr_log (GPR_INFO, "%s/%s", "test_simple_delayed_request_long", config.name); + f = config.create_fixture (NULL, NULL); /* This timeout should be longer than a single retry */ - simple_delayed_request_body(config, &f, NULL, NULL, 1500000); - end_test(&f); - config.tear_down_data(&f); + simple_delayed_request_body (config, &f, NULL, NULL, 1500000); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); - test_simple_delayed_request_short(config); - test_simple_delayed_request_long(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + GPR_ASSERT (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); + test_simple_delayed_request_short (config); + test_simple_delayed_request_long (config); } diff --git a/test/core/end2end/tests/simple_request.c b/test/core/end2end/tests/simple_request.c index a874640837..76f0091298 100644 --- a/test/core/end2end/tests/simple_request.c +++ b/test/core/end2end/tests/simple_request.c @@ -45,65 +45,87 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } -static void simple_request_body(grpc_end2end_test_fixture f) { +static void +simple_request_body (grpc_end2end_test_fixture f) +{ grpc_call *c; grpc_call *s; - gpr_timespec deadline = five_seconds_time(); - cq_verifier *cqv = cq_verifier_create(f.cq); + gpr_timespec deadline = five_seconds_time (); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -117,20 +139,18 @@ static void simple_request_body(grpc_end2end_test_fixture f) { int was_cancelled = 2; char *peer; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr:1234", deadline, - NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr:1234", deadline, NULL); + GPR_ASSERT (c); - peer = grpc_call_get_peer(c); - GPR_ASSERT(peer != NULL); - gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); - gpr_free(peer); + peer = grpc_call_get_peer (c); + GPR_ASSERT (peer != NULL); + gpr_log (GPR_DEBUG, "client_peer_before_call=%s", peer); + gpr_free (peer); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -155,24 +175,22 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); - - peer = grpc_call_get_peer(s); - GPR_ASSERT(peer != NULL); - gpr_log(GPR_DEBUG, "server_peer=%s", peer); - gpr_free(peer); - peer = grpc_call_get_peer(c); - GPR_ASSERT(peer != NULL); - gpr_log(GPR_DEBUG, "client_peer=%s", peer); - gpr_free(peer); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); + + peer = grpc_call_get_peer (s); + GPR_ASSERT (peer != NULL); + gpr_log (GPR_DEBUG, "server_peer=%s", peer); + gpr_free (peer); + peer = grpc_call_get_peer (c); + GPR_ASSERT (peer != NULL); + gpr_log (GPR_DEBUG, "client_peer=%s", peer); + gpr_free (peer); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -192,56 +210,63 @@ static void simple_request_body(grpc_end2end_test_fixture f) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); - GPR_ASSERT(was_cancelled == 1); + GPR_ASSERT (status == GRPC_STATUS_UNIMPLEMENTED); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr:1234")); + GPR_ASSERT (was_cancelled == 1); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); } -static void test_invoke_simple_request(grpc_end2end_test_config config) { +static void +test_invoke_simple_request (grpc_end2end_test_config config) +{ grpc_end2end_test_fixture f; - f = begin_test(config, "test_invoke_simple_request", NULL, NULL); - simple_request_body(f); - end_test(&f); - config.tear_down_data(&f); + f = begin_test (config, "test_invoke_simple_request", NULL, NULL); + simple_request_body (f); + end_test (&f); + config.tear_down_data (&f); } -static void test_invoke_10_simple_requests(grpc_end2end_test_config config) { +static void +test_invoke_10_simple_requests (grpc_end2end_test_config config) +{ int i; - grpc_end2end_test_fixture f = - begin_test(config, "test_invoke_10_simple_requests", NULL, NULL); - for (i = 0; i < 10; i++) { - simple_request_body(f); - gpr_log(GPR_INFO, "Passed simple request %d", i); - } - end_test(&f); - config.tear_down_data(&f); + grpc_end2end_test_fixture f = begin_test (config, "test_invoke_10_simple_requests", NULL, NULL); + for (i = 0; i < 10; i++) + { + simple_request_body (f); + gpr_log (GPR_INFO, "Passed simple request %d", i); + } + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ int i; - for (i = 0; i < 10; i++) { - test_invoke_simple_request(config); - } - test_invoke_10_simple_requests(config); + for (i = 0; i < 10; i++) + { + test_invoke_simple_request (config); + } + test_invoke_10_simple_requests (config); } diff --git a/test/core/end2end/tests/trailing_metadata.c b/test/core/end2end/tests/trailing_metadata.c index c040b9fac1..28bcfadc8a 100644 --- a/test/core/end2end/tests/trailing_metadata.c +++ b/test/core/end2end/tests/trailing_metadata.c @@ -43,84 +43,105 @@ #include <grpc/support/useful.h> #include "test/core/end2end/cq_verifier.h" -enum { TIMEOUT = 200000 }; +enum +{ TIMEOUT = 200000 }; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, - const char *test_name, - grpc_channel_args *client_args, - grpc_channel_args *server_args) { +static grpc_end2end_test_fixture +begin_test (grpc_end2end_test_config config, const char *test_name, grpc_channel_args * client_args, grpc_channel_args * server_args) +{ grpc_end2end_test_fixture f; - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - f = config.create_fixture(client_args, server_args); - config.init_client(&f, client_args); - config.init_server(&f, server_args); + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + f = config.create_fixture (client_args, server_args); + config.init_client (&f, client_args); + config.init_server (&f, server_args); return f; } -static gpr_timespec n_seconds_time(int n) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); +static gpr_timespec +n_seconds_time (int n) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (n); } -static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } +static gpr_timespec +five_seconds_time (void) +{ + return n_seconds_time (5); +} -static void drain_cq(grpc_completion_queue *cq) { +static void +drain_cq (grpc_completion_queue * cq) +{ grpc_event ev; - do { - ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); - } while (ev.type != GRPC_QUEUE_SHUTDOWN); + do + { + ev = grpc_completion_queue_next (cq, five_seconds_time (), NULL); + } + while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void shutdown_server(grpc_end2end_test_fixture *f) { - if (!f->server) return; - grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_server_destroy(f->server); +static void +shutdown_server (grpc_end2end_test_fixture * f) +{ + if (!f->server) + return; + grpc_server_shutdown_and_notify (f->server, f->cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (f->cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_server_destroy (f->server); f->server = NULL; } -static void shutdown_client(grpc_end2end_test_fixture *f) { - if (!f->client) return; - grpc_channel_destroy(f->client); +static void +shutdown_client (grpc_end2end_test_fixture * f) +{ + if (!f->client) + return; + grpc_channel_destroy (f->client); f->client = NULL; } -static void end_test(grpc_end2end_test_fixture *f) { - shutdown_server(f); - shutdown_client(f); +static void +end_test (grpc_end2end_test_fixture * f) +{ + shutdown_server (f); + shutdown_client (f); - grpc_completion_queue_shutdown(f->cq); - drain_cq(f->cq); - grpc_completion_queue_destroy(f->cq); + grpc_completion_queue_shutdown (f->cq); + drain_cq (f->cq); + grpc_completion_queue_destroy (f->cq); } /* Request/response with metadata and payload.*/ -static void test_request_response_with_metadata_and_payload( - grpc_end2end_test_config config) { +static void +test_request_response_with_metadata_and_payload (grpc_end2end_test_config config) +{ grpc_call *c; grpc_call *s; - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); - grpc_byte_buffer *response_payload = - grpc_raw_byte_buffer_create(&response_payload_slice, 1); - gpr_timespec deadline = five_seconds_time(); + gpr_slice request_payload_slice = gpr_slice_from_copied_string ("hello world"); + gpr_slice response_payload_slice = gpr_slice_from_copied_string ("hello you"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create (&request_payload_slice, 1); + grpc_byte_buffer *response_payload = grpc_raw_byte_buffer_create (&response_payload_slice, 1); + gpr_timespec deadline = five_seconds_time (); grpc_metadata meta_c[2] = { - {"key1", "val1", 4, 0, {{NULL, NULL, NULL, NULL}}}, - {"key2", "val2", 4, 0, {{NULL, NULL, NULL, NULL}}}}; + {"key1", "val1", 4, 0, {{NULL, NULL, NULL, NULL}}}, + {"key2", "val2", 4, 0, {{NULL, NULL, NULL, NULL}}} + }; grpc_metadata meta_s[2] = { - {"key3", "val3", 4, 0, {{NULL, NULL, NULL, NULL}}}, - {"key4", "val4", 4, 0, {{NULL, NULL, NULL, NULL}}}}; + {"key3", "val3", 4, 0, {{NULL, NULL, NULL, NULL}}}, + {"key4", "val4", 4, 0, {{NULL, NULL, NULL, NULL}}} + }; grpc_metadata meta_t[2] = { - {"key5", "val5", 4, 0, {{NULL, NULL, NULL, NULL}}}, - {"key6", "val6", 4, 0, {{NULL, NULL, NULL, NULL}}}}; - grpc_end2end_test_fixture f = begin_test( - config, "test_request_response_with_metadata_and_payload", NULL, NULL); - cq_verifier *cqv = cq_verifier_create(f.cq); + {"key5", "val5", 4, 0, {{NULL, NULL, NULL, NULL}}}, + {"key6", "val6", 4, 0, {{NULL, NULL, NULL, NULL}}} + }; + grpc_end2end_test_fixture f = begin_test (config, "test_request_response_with_metadata_and_payload", NULL, NULL); + cq_verifier *cqv = cq_verifier_create (f.cq); grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; @@ -135,14 +156,13 @@ static void test_request_response_with_metadata_and_payload( size_t details_capacity = 0; int was_cancelled = 2; - c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, - "/foo", "foo.test.google.fr", deadline, NULL); - GPR_ASSERT(c); + c = grpc_channel_create_call (f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo", "foo.test.google.fr", deadline, NULL); + GPR_ASSERT (c); - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); - grpc_metadata_array_init(&request_metadata_recv); - grpc_call_details_init(&call_details); + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); + grpc_metadata_array_init (&request_metadata_recv); + grpc_call_details_init (&call_details); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -178,15 +198,13 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (c, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - error = - grpc_server_request_call(f.server, &s, &call_details, - &request_metadata_recv, f.cq, f.cq, tag(101)); - GPR_ASSERT(GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(101), 1); - cq_verify(cqv); + error = grpc_server_request_call (f.server, &s, &call_details, &request_metadata_recv, f.cq, f.cq, tag (101)); + GPR_ASSERT (GRPC_CALL_OK == error); + cq_expect_completion (cqv, tag (101), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -200,11 +218,11 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (102), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(102), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (102), 1); + cq_verify (cqv); op = ops; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; @@ -225,46 +243,48 @@ static void test_request_response_with_metadata_and_payload( op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (s, ops, (size_t) (op - ops), tag (103), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); - cq_expect_completion(cqv, tag(103), 1); - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (103), 1); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - GPR_ASSERT(status == GRPC_STATUS_OK); - GPR_ASSERT(0 == strcmp(details, "xyz")); - GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); - GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); - GPR_ASSERT(contains_metadata(&request_metadata_recv, "key1", "val1")); - GPR_ASSERT(contains_metadata(&request_metadata_recv, "key2", "val2")); - GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key3", "val3")); - GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key4", "val4")); - GPR_ASSERT(contains_metadata(&trailing_metadata_recv, "key5", "val5")); - GPR_ASSERT(contains_metadata(&trailing_metadata_recv, "key6", "val6")); + GPR_ASSERT (status == GRPC_STATUS_OK); + GPR_ASSERT (0 == strcmp (details, "xyz")); + GPR_ASSERT (0 == strcmp (call_details.method, "/foo")); + GPR_ASSERT (0 == strcmp (call_details.host, "foo.test.google.fr")); + GPR_ASSERT (byte_buffer_eq_string (request_payload_recv, "hello world")); + GPR_ASSERT (byte_buffer_eq_string (response_payload_recv, "hello you")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key1", "val1")); + GPR_ASSERT (contains_metadata (&request_metadata_recv, "key2", "val2")); + GPR_ASSERT (contains_metadata (&initial_metadata_recv, "key3", "val3")); + GPR_ASSERT (contains_metadata (&initial_metadata_recv, "key4", "val4")); + GPR_ASSERT (contains_metadata (&trailing_metadata_recv, "key5", "val5")); + GPR_ASSERT (contains_metadata (&trailing_metadata_recv, "key6", "val6")); - gpr_free(details); - grpc_metadata_array_destroy(&initial_metadata_recv); - grpc_metadata_array_destroy(&trailing_metadata_recv); - grpc_metadata_array_destroy(&request_metadata_recv); - grpc_call_details_destroy(&call_details); + gpr_free (details); + grpc_metadata_array_destroy (&initial_metadata_recv); + grpc_metadata_array_destroy (&trailing_metadata_recv); + grpc_metadata_array_destroy (&request_metadata_recv); + grpc_call_details_destroy (&call_details); - grpc_call_destroy(c); - grpc_call_destroy(s); + grpc_call_destroy (c); + grpc_call_destroy (s); - cq_verifier_destroy(cqv); + cq_verifier_destroy (cqv); - grpc_byte_buffer_destroy(request_payload); - grpc_byte_buffer_destroy(response_payload); - grpc_byte_buffer_destroy(request_payload_recv); - grpc_byte_buffer_destroy(response_payload_recv); + grpc_byte_buffer_destroy (request_payload); + grpc_byte_buffer_destroy (response_payload); + grpc_byte_buffer_destroy (request_payload_recv); + grpc_byte_buffer_destroy (response_payload_recv); - end_test(&f); - config.tear_down_data(&f); + end_test (&f); + config.tear_down_data (&f); } -void grpc_end2end_tests(grpc_end2end_test_config config) { - test_request_response_with_metadata_and_payload(config); +void +grpc_end2end_tests (grpc_end2end_test_config config) +{ + test_request_response_with_metadata_and_payload (config); } diff --git a/test/core/fling/client.c b/test/core/fling/client.c index a53411c2f5..29d8153006 100644 --- a/test/core/fling/client.c +++ b/test/core/fling/client.c @@ -60,9 +60,11 @@ static char *details = NULL; static size_t details_capacity = 0; static grpc_op *op; -static void init_ping_pong_request(void) { - grpc_metadata_array_init(&initial_metadata_recv); - grpc_metadata_array_init(&trailing_metadata_recv); +static void +init_ping_pong_request (void) +{ + grpc_metadata_array_init (&initial_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); op = ops; @@ -88,31 +90,29 @@ static void init_ping_pong_request(void) { op++; } -static void step_ping_pong_request(void) { - call = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - "/Reflector/reflectUnary", "localhost", - gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops, - (size_t)(op - ops), - (void *)1, NULL)); - grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - grpc_call_destroy(call); - grpc_byte_buffer_destroy(response_payload_recv); +static void +step_ping_pong_request (void) +{ + call = grpc_channel_create_call (channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, "/Reflector/reflectUnary", "localhost", gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (GRPC_CALL_OK == grpc_call_start_batch (call, ops, (size_t) (op - ops), (void *) 1, NULL)); + grpc_completion_queue_next (cq, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + grpc_call_destroy (call); + grpc_byte_buffer_destroy (response_payload_recv); call = NULL; } -static void init_ping_pong_stream(void) { +static void +init_ping_pong_stream (void) +{ grpc_call_error error; - call = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - "/Reflector/reflectStream", "localhost", - gpr_inf_future(GPR_CLOCK_REALTIME), NULL); + call = grpc_channel_create_call (channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, "/Reflector/reflectStream", "localhost", gpr_inf_future (GPR_CLOCK_REALTIME), NULL); stream_init_op.op = GRPC_OP_SEND_INITIAL_METADATA; stream_init_op.data.send_initial_metadata.count = 0; - error = grpc_call_start_batch(call, &stream_init_op, 1, (void *)1, NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); + error = grpc_call_start_batch (call, &stream_init_op, 1, (void *) 1, NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + grpc_completion_queue_next (cq, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); - grpc_metadata_array_init(&initial_metadata_recv); + grpc_metadata_array_init (&initial_metadata_recv); stream_step_ops[0].op = GRPC_OP_SEND_MESSAGE; stream_step_ops[0].data.send_message = the_buffer; @@ -120,32 +120,39 @@ static void init_ping_pong_stream(void) { stream_step_ops[1].data.recv_message = &response_payload_recv; } -static void step_ping_pong_stream(void) { +static void +step_ping_pong_stream (void) +{ grpc_call_error error; - error = grpc_call_start_batch(call, stream_step_ops, 2, (void *)1, NULL); - GPR_ASSERT(GRPC_CALL_OK == error); - grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - grpc_byte_buffer_destroy(response_payload_recv); + error = grpc_call_start_batch (call, stream_step_ops, 2, (void *) 1, NULL); + GPR_ASSERT (GRPC_CALL_OK == error); + grpc_completion_queue_next (cq, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + grpc_byte_buffer_destroy (response_payload_recv); } -static double now(void) { - gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME); - return 1e9 * (double)tv.tv_sec + tv.tv_nsec; +static double +now (void) +{ + gpr_timespec tv = gpr_now (GPR_CLOCK_REALTIME); + return 1e9 * (double) tv.tv_sec + tv.tv_nsec; } -typedef struct { +typedef struct +{ const char *name; - void (*init)(); - void (*do_one_step)(); + void (*init) (); + void (*do_one_step) (); } scenario; static const scenario scenarios[] = { - {"ping-pong-request", init_ping_pong_request, step_ping_pong_request}, - {"ping-pong-stream", init_ping_pong_stream, step_ping_pong_stream}, + {"ping-pong-request", init_ping_pong_request, step_ping_pong_request}, + {"ping-pong-stream", init_ping_pong_stream, step_ping_pong_stream}, }; -int main(int argc, char **argv) { - gpr_slice slice = gpr_slice_from_copied_string("x"); +int +main (int argc, char **argv) +{ + gpr_slice slice = gpr_slice_from_copied_string ("x"); double start, stop; unsigned i; @@ -157,79 +164,82 @@ int main(int argc, char **argv) { gpr_cmdline *cl; grpc_event event; char *scenario_name = "ping-pong-request"; - scenario sc = {NULL, NULL, NULL}; + scenario sc = { NULL, NULL, NULL }; - GPR_ASSERT(argc >= 1); + GPR_ASSERT (argc >= 1); fake_argv[0] = argv[0]; - grpc_test_init(1, fake_argv); - - grpc_init(); - - cl = gpr_cmdline_create("fling client"); - gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send", - &payload_size); - gpr_cmdline_add_string(cl, "target", "Target host:port", &target); - gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure); - gpr_cmdline_add_string(cl, "scenario", "Scenario", &scenario_name); - gpr_cmdline_parse(cl, argc, argv); - gpr_cmdline_destroy(cl); - - for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) { - if (0 == strcmp(scenarios[i].name, scenario_name)) { - sc = scenarios[i]; + grpc_test_init (1, fake_argv); + + grpc_init (); + + cl = gpr_cmdline_create ("fling client"); + gpr_cmdline_add_int (cl, "payload_size", "Size of the payload to send", &payload_size); + gpr_cmdline_add_string (cl, "target", "Target host:port", &target); + gpr_cmdline_add_flag (cl, "secure", "Run with security?", &secure); + gpr_cmdline_add_string (cl, "scenario", "Scenario", &scenario_name); + gpr_cmdline_parse (cl, argc, argv); + gpr_cmdline_destroy (cl); + + for (i = 0; i < GPR_ARRAY_SIZE (scenarios); i++) + { + if (0 == strcmp (scenarios[i].name, scenario_name)) + { + sc = scenarios[i]; + } } - } - if (!sc.name) { - fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name); - for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) { - fprintf(stderr, " %s", scenarios[i].name); + if (!sc.name) + { + fprintf (stderr, "unsupported scenario '%s'. Valid are:", scenario_name); + for (i = 0; i < GPR_ARRAY_SIZE (scenarios); i++) + { + fprintf (stderr, " %s", scenarios[i].name); + } + return 1; } - return 1; - } - - channel = grpc_insecure_channel_create(target, NULL, NULL); - cq = grpc_completion_queue_create(NULL); - the_buffer = grpc_raw_byte_buffer_create(&slice, (size_t)payload_size); - histogram = gpr_histogram_create(0.01, 60e9); - - sc.init(); - - for (i = 0; i < 1000; i++) { - sc.do_one_step(); - } - - gpr_log(GPR_INFO, "start profiling"); - grpc_profiler_start("client.prof"); - for (i = 0; i < 100000; i++) { - start = now(); - sc.do_one_step(); - stop = now(); - gpr_histogram_add(histogram, stop - start); - } - grpc_profiler_stop(); - - if (call) { - grpc_call_destroy(call); - } - - grpc_channel_destroy(channel); - grpc_completion_queue_shutdown(cq); - do { - event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), - NULL); - } while (event.type != GRPC_QUEUE_SHUTDOWN); - grpc_completion_queue_destroy(cq); - grpc_byte_buffer_destroy(the_buffer); - gpr_slice_unref(slice); - - gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", - gpr_histogram_percentile(histogram, 50), - gpr_histogram_percentile(histogram, 95), - gpr_histogram_percentile(histogram, 99), - gpr_histogram_percentile(histogram, 99.9)); - gpr_histogram_destroy(histogram); - - grpc_shutdown(); + + channel = grpc_insecure_channel_create (target, NULL, NULL); + cq = grpc_completion_queue_create (NULL); + the_buffer = grpc_raw_byte_buffer_create (&slice, (size_t) payload_size); + histogram = gpr_histogram_create (0.01, 60e9); + + sc.init (); + + for (i = 0; i < 1000; i++) + { + sc.do_one_step (); + } + + gpr_log (GPR_INFO, "start profiling"); + grpc_profiler_start ("client.prof"); + for (i = 0; i < 100000; i++) + { + start = now (); + sc.do_one_step (); + stop = now (); + gpr_histogram_add (histogram, stop - start); + } + grpc_profiler_stop (); + + if (call) + { + grpc_call_destroy (call); + } + + grpc_channel_destroy (channel); + grpc_completion_queue_shutdown (cq); + do + { + event = grpc_completion_queue_next (cq, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + } + while (event.type != GRPC_QUEUE_SHUTDOWN); + grpc_completion_queue_destroy (cq); + grpc_byte_buffer_destroy (the_buffer); + gpr_slice_unref (slice); + + gpr_log (GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", gpr_histogram_percentile (histogram, 50), gpr_histogram_percentile (histogram, 95), gpr_histogram_percentile (histogram, 99), gpr_histogram_percentile (histogram, 99.9)); + gpr_histogram_destroy (histogram); + + grpc_shutdown (); return 0; } diff --git a/test/core/fling/fling_stream_test.c b/test/core/fling/fling_stream_test.c index 78a73372aa..0bda19f5e2 100644 --- a/test/core/fling/fling_stream_test.c +++ b/test/core/fling/fling_stream_test.c @@ -50,64 +50,77 @@ #include <grpc/support/string_util.h> #include "test/core/util/port.h" -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ char *me = argv[0]; - char *lslash = strrchr(me, '/'); + char *lslash = strrchr (me, '/'); char root[1024]; - int port = grpc_pick_unused_port_or_die(); + int port = grpc_pick_unused_port_or_die (); char *args[10]; int status; pid_t svr, cli; /* seed rng with pid, so we don't end up with the same random numbers as a concurrently running test binary */ - srand((unsigned)getpid()); + srand ((unsigned) getpid ()); /* figure out where we are */ - if (lslash) { - memcpy(root, me, (size_t)(lslash - me)); - root[lslash - me] = 0; - } else { - strcpy(root, "."); - } + if (lslash) + { + memcpy (root, me, (size_t) (lslash - me)); + root[lslash - me] = 0; + } + else + { + strcpy (root, "."); + } /* start the server */ - svr = fork(); - if (svr == 0) { - gpr_asprintf(&args[0], "%s/fling_server", root); - args[1] = "--bind"; - gpr_join_host_port(&args[2], "::", port); - args[3] = "--no-secure"; - args[4] = 0; - execv(args[0], args); + svr = fork (); + if (svr == 0) + { + gpr_asprintf (&args[0], "%s/fling_server", root); + args[1] = "--bind"; + gpr_join_host_port (&args[2], "::", port); + args[3] = "--no-secure"; + args[4] = 0; + execv (args[0], args); - gpr_free(args[0]); - gpr_free(args[2]); - return 1; - } + gpr_free (args[0]); + gpr_free (args[2]); + return 1; + } /* wait a little */ - sleep(2); + sleep (2); /* start the client */ - cli = fork(); - if (cli == 0) { - gpr_asprintf(&args[0], "%s/fling_client", root); - args[1] = "--target"; - gpr_join_host_port(&args[2], "127.0.0.1", port); - args[3] = "--scenario=ping-pong-stream"; - args[4] = "--no-secure"; - args[5] = 0; - execv(args[0], args); + cli = fork (); + if (cli == 0) + { + gpr_asprintf (&args[0], "%s/fling_client", root); + args[1] = "--target"; + gpr_join_host_port (&args[2], "127.0.0.1", port); + args[3] = "--scenario=ping-pong-stream"; + args[4] = "--no-secure"; + args[5] = 0; + execv (args[0], args); - gpr_free(args[0]); - gpr_free(args[2]); - return 1; - } + gpr_free (args[0]); + gpr_free (args[2]); + return 1; + } /* wait for completion */ - printf("waiting for client\n"); - if (waitpid(cli, &status, 0) == -1) return 2; - if (!WIFEXITED(status)) return 4; - if (WEXITSTATUS(status)) return WEXITSTATUS(status); - printf("waiting for server\n"); - kill(svr, SIGINT); - if (waitpid(svr, &status, 0) == -1) return 2; - if (!WIFEXITED(status)) return 4; - if (WEXITSTATUS(status)) return WEXITSTATUS(status); + printf ("waiting for client\n"); + if (waitpid (cli, &status, 0) == -1) + return 2; + if (!WIFEXITED (status)) + return 4; + if (WEXITSTATUS (status)) + return WEXITSTATUS (status); + printf ("waiting for server\n"); + kill (svr, SIGINT); + if (waitpid (svr, &status, 0) == -1) + return 2; + if (!WIFEXITED (status)) + return 4; + if (WEXITSTATUS (status)) + return WEXITSTATUS (status); return 0; } diff --git a/test/core/fling/fling_test.c b/test/core/fling/fling_test.c index cf43ecfd2d..a4b789f6ca 100644 --- a/test/core/fling/fling_test.c +++ b/test/core/fling/fling_test.c @@ -41,54 +41,58 @@ #include "src/core/support/string.h" #include "test/core/util/port.h" -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ char *me = argv[0]; - char *lslash = strrchr(me, '/'); + char *lslash = strrchr (me, '/'); char root[1024]; - int port = grpc_pick_unused_port_or_die(); + int port = grpc_pick_unused_port_or_die (); char *args[10]; int status; gpr_subprocess *svr, *cli; /* figure out where we are */ - if (lslash) { - memcpy(root, me, (size_t)(lslash - me)); - root[lslash - me] = 0; - } else { - strcpy(root, "."); - } + if (lslash) + { + memcpy (root, me, (size_t) (lslash - me)); + root[lslash - me] = 0; + } + else + { + strcpy (root, "."); + } /* start the server */ - gpr_asprintf(&args[0], "%s/fling_server%s", root, - gpr_subprocess_binary_extension()); + gpr_asprintf (&args[0], "%s/fling_server%s", root, gpr_subprocess_binary_extension ()); args[1] = "--bind"; - gpr_join_host_port(&args[2], "::", port); + gpr_join_host_port (&args[2], "::", port); args[3] = "--no-secure"; - svr = gpr_subprocess_create(4, (const char **)args); - gpr_free(args[0]); - gpr_free(args[2]); + svr = gpr_subprocess_create (4, (const char **) args); + gpr_free (args[0]); + gpr_free (args[2]); /* start the client */ - gpr_asprintf(&args[0], "%s/fling_client%s", root, - gpr_subprocess_binary_extension()); + gpr_asprintf (&args[0], "%s/fling_client%s", root, gpr_subprocess_binary_extension ()); args[1] = "--target"; - gpr_join_host_port(&args[2], "127.0.0.1", port); + gpr_join_host_port (&args[2], "127.0.0.1", port); args[3] = "--scenario=ping-pong-request"; args[4] = "--no-secure"; args[5] = 0; - cli = gpr_subprocess_create(6, (const char **)args); - gpr_free(args[0]); - gpr_free(args[2]); + cli = gpr_subprocess_create (6, (const char **) args); + gpr_free (args[0]); + gpr_free (args[2]); /* wait for completion */ - printf("waiting for client\n"); - if ((status = gpr_subprocess_join(cli))) { - gpr_subprocess_destroy(cli); - gpr_subprocess_destroy(svr); - return status; - } - gpr_subprocess_destroy(cli); + printf ("waiting for client\n"); + if ((status = gpr_subprocess_join (cli))) + { + gpr_subprocess_destroy (cli); + gpr_subprocess_destroy (svr); + return status; + } + gpr_subprocess_destroy (cli); - gpr_subprocess_interrupt(svr); - status = gpr_subprocess_join(svr); - gpr_subprocess_destroy(svr); + gpr_subprocess_interrupt (svr); + status = gpr_subprocess_join (svr); + gpr_subprocess_destroy (svr); return status; } diff --git a/test/core/fling/server.c b/test/core/fling/server.c index 5aace03520..75c0edbd5a 100644 --- a/test/core/fling/server.c +++ b/test/core/fling/server.c @@ -72,9 +72,14 @@ static int was_cancelled = 2; static grpc_op unary_ops[6]; static int got_sigint = 0; -static void *tag(gpr_intptr t) { return (void *)t; } +static void * +tag (gpr_intptr t) +{ + return (void *) t; +} -typedef enum { +typedef enum +{ FLING_SERVER_NEW_REQUEST = 1, FLING_SERVER_READ_FOR_UNARY, FLING_SERVER_BATCH_OPS_FOR_UNARY, @@ -84,22 +89,26 @@ typedef enum { FLING_SERVER_SEND_STATUS_FOR_STREAMING } fling_server_tags; -typedef struct { +typedef struct +{ gpr_refcount pending_ops; gpr_uint32 flags; } call_state; -static void request_call(void) { - grpc_metadata_array_init(&request_metadata_recv); - grpc_server_request_call(server, &call, &call_details, &request_metadata_recv, - cq, cq, tag(FLING_SERVER_NEW_REQUEST)); +static void +request_call (void) +{ + grpc_metadata_array_init (&request_metadata_recv); + grpc_server_request_call (server, &call, &call_details, &request_metadata_recv, cq, cq, tag (FLING_SERVER_NEW_REQUEST)); } -static void handle_unary_method(void) { +static void +handle_unary_method (void) +{ grpc_op *op; grpc_call_error error; - grpc_metadata_array_init(&initial_metadata_send); + grpc_metadata_array_init (&initial_metadata_send); op = unary_ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -109,9 +118,10 @@ static void handle_unary_method(void) { op->data.recv_message = &terminal_buffer; op++; op->op = GRPC_OP_SEND_MESSAGE; - if (payload_buffer == NULL) { - gpr_log(GPR_INFO, "NULL payload buffer !!!"); - } + if (payload_buffer == NULL) + { + gpr_log (GPR_INFO, "NULL payload buffer !!!"); + } op->data.send_message = payload_buffer; op++; op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; @@ -123,47 +133,55 @@ static void handle_unary_method(void) { op->data.recv_close_on_server.cancelled = &was_cancelled; op++; - error = grpc_call_start_batch(call, unary_ops, (size_t)(op - unary_ops), - tag(FLING_SERVER_BATCH_OPS_FOR_UNARY), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (call, unary_ops, (size_t) (op - unary_ops), tag (FLING_SERVER_BATCH_OPS_FOR_UNARY), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); } -static void send_initial_metadata(void) { +static void +send_initial_metadata (void) +{ grpc_call_error error; - void *tagarg = tag(FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING); - grpc_metadata_array_init(&initial_metadata_send); + void *tagarg = tag (FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING); + grpc_metadata_array_init (&initial_metadata_send); metadata_send_op.op = GRPC_OP_SEND_INITIAL_METADATA; metadata_send_op.data.send_initial_metadata.count = 0; - error = grpc_call_start_batch(call, &metadata_send_op, 1, tagarg, NULL); + error = grpc_call_start_batch (call, &metadata_send_op, 1, tagarg, NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + GPR_ASSERT (GRPC_CALL_OK == error); } -static void start_read_op(int t) { +static void +start_read_op (int t) +{ grpc_call_error error; /* Starting read at server */ read_op.op = GRPC_OP_RECV_MESSAGE; read_op.data.recv_message = &payload_buffer; - error = grpc_call_start_batch(call, &read_op, 1, tag(t), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (call, &read_op, 1, tag (t), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); } -static void start_write_op(void) { +static void +start_write_op (void) +{ grpc_call_error error; - void *tagarg = tag(FLING_SERVER_WRITE_FOR_STREAMING); + void *tagarg = tag (FLING_SERVER_WRITE_FOR_STREAMING); /* Starting write at server */ write_op.op = GRPC_OP_SEND_MESSAGE; - if (payload_buffer == NULL) { - gpr_log(GPR_INFO, "NULL payload buffer !!!"); - } + if (payload_buffer == NULL) + { + gpr_log (GPR_INFO, "NULL payload buffer !!!"); + } write_op.data.send_message = payload_buffer; - error = grpc_call_start_batch(call, &write_op, 1, tagarg, NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (call, &write_op, 1, tagarg, NULL); + GPR_ASSERT (GRPC_CALL_OK == error); } -static void start_send_status(void) { +static void +start_send_status (void) +{ grpc_call_error error; - void *tagarg = tag(FLING_SERVER_SEND_STATUS_FOR_STREAMING); + void *tagarg = tag (FLING_SERVER_SEND_STATUS_FOR_STREAMING); status_op[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER; status_op[0].data.send_status_from_server.status = GRPC_STATUS_OK; status_op[0].data.send_status_from_server.trailing_metadata_count = 0; @@ -171,15 +189,21 @@ static void start_send_status(void) { status_op[1].op = GRPC_OP_RECV_CLOSE_ON_SERVER; status_op[1].data.recv_close_on_server.cancelled = &was_cancelled; - error = grpc_call_start_batch(call, status_op, 2, tagarg, NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (call, status_op, 2, tagarg, NULL); + GPR_ASSERT (GRPC_CALL_OK == error); } /* We have some sort of deadlock, so let's not exit gracefully for now. When that is resolved, please remove the #include <unistd.h> above. */ -static void sigint_handler(int x) { _exit(0); } +static void +sigint_handler (int x) +{ + _exit (0); +} -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_event ev; call_state *s; char *addr_buf = NULL; @@ -192,136 +216,149 @@ int main(int argc, char **argv) { char *fake_argv[1]; - GPR_ASSERT(argc >= 1); + GPR_ASSERT (argc >= 1); fake_argv[0] = argv[0]; - grpc_test_init(1, fake_argv); - - grpc_init(); - srand((unsigned)clock()); - - cl = gpr_cmdline_create("fling server"); - gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr); - gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure); - gpr_cmdline_parse(cl, argc, argv); - gpr_cmdline_destroy(cl); - - if (addr == NULL) { - gpr_join_host_port(&addr_buf, "::", grpc_pick_unused_port_or_die()); - addr = addr_buf; - } - gpr_log(GPR_INFO, "creating server on: %s", addr); - - cq = grpc_completion_queue_create(NULL); - if (secure) { - grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key, - test_server1_cert}; - grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create( - NULL, &pem_key_cert_pair, 1, 0, NULL); - server = grpc_server_create(NULL, NULL); - GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds)); - grpc_server_credentials_release(ssl_creds); - } else { - server = grpc_server_create(NULL, NULL); - GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr)); - } - grpc_server_register_completion_queue(server, cq, NULL); - grpc_server_start(server); - - gpr_free(addr_buf); - addr = addr_buf = NULL; + grpc_test_init (1, fake_argv); + + grpc_init (); + srand ((unsigned) clock ()); - grpc_call_details_init(&call_details); - - request_call(); - - grpc_profiler_start("server.prof"); - signal(SIGINT, sigint_handler); - while (!shutdown_finished) { - if (got_sigint && !shutdown_started) { - gpr_log(GPR_INFO, "Shutting down due to SIGINT"); - grpc_server_shutdown_and_notify(server, cq, tag(1000)); - GPR_ASSERT(grpc_completion_queue_pluck( - cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) - .type == GRPC_OP_COMPLETE); - grpc_completion_queue_shutdown(cq); - shutdown_started = 1; + cl = gpr_cmdline_create ("fling server"); + gpr_cmdline_add_string (cl, "bind", "Bind host:port", &addr); + gpr_cmdline_add_flag (cl, "secure", "Run with security?", &secure); + gpr_cmdline_parse (cl, argc, argv); + gpr_cmdline_destroy (cl); + + if (addr == NULL) + { + gpr_join_host_port (&addr_buf, "::", grpc_pick_unused_port_or_die ()); + addr = addr_buf; + } + gpr_log (GPR_INFO, "creating server on: %s", addr); + + cq = grpc_completion_queue_create (NULL); + if (secure) + { + grpc_ssl_pem_key_cert_pair pem_key_cert_pair = { test_server1_key, + test_server1_cert + }; + grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create (NULL, &pem_key_cert_pair, 1, 0, NULL); + server = grpc_server_create (NULL, NULL); + GPR_ASSERT (grpc_server_add_secure_http2_port (server, addr, ssl_creds)); + grpc_server_credentials_release (ssl_creds); + } + else + { + server = grpc_server_create (NULL, NULL); + GPR_ASSERT (grpc_server_add_insecure_http2_port (server, addr)); } - ev = grpc_completion_queue_next( - cq, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_micros(1000000, GPR_TIMESPAN)), - NULL); - s = ev.tag; - switch (ev.type) { - case GRPC_OP_COMPLETE: - switch ((gpr_intptr)s) { - case FLING_SERVER_NEW_REQUEST: - if (call != NULL) { - if (0 == - strcmp(call_details.method, "/Reflector/reflectStream")) { - /* Received streaming call. Send metadata here. */ - start_read_op(FLING_SERVER_READ_FOR_STREAMING); - send_initial_metadata(); - } else { - /* Received unary call. Can do all ops in one batch. */ - start_read_op(FLING_SERVER_READ_FOR_UNARY); - } - } else { - GPR_ASSERT(shutdown_started); - } - /* request_call(); - */ - break; - case FLING_SERVER_READ_FOR_STREAMING: - if (payload_buffer != NULL) { - /* Received payload from client. */ - start_write_op(); - } else { - /* Received end of stream from client. */ - start_send_status(); - } - break; - case FLING_SERVER_WRITE_FOR_STREAMING: - /* Write completed at server */ - grpc_byte_buffer_destroy(payload_buffer); - payload_buffer = NULL; - start_read_op(FLING_SERVER_READ_FOR_STREAMING); - break; - case FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING: - /* Metadata send completed at server */ - break; - case FLING_SERVER_SEND_STATUS_FOR_STREAMING: - /* Send status and close completed at server */ - grpc_call_destroy(call); - if (!shutdown_started) request_call(); - break; - case FLING_SERVER_READ_FOR_UNARY: - /* Finished payload read for unary. Start all reamaining - * unary ops in a batch. - */ - handle_unary_method(); - break; - case FLING_SERVER_BATCH_OPS_FOR_UNARY: - /* Finished unary call. */ - grpc_byte_buffer_destroy(payload_buffer); - payload_buffer = NULL; - grpc_call_destroy(call); - if (!shutdown_started) request_call(); - break; - } - break; - case GRPC_QUEUE_SHUTDOWN: - GPR_ASSERT(shutdown_started); - shutdown_finished = 1; - break; - case GRPC_QUEUE_TIMEOUT: - break; + grpc_server_register_completion_queue (server, cq, NULL); + grpc_server_start (server); + + gpr_free (addr_buf); + addr = addr_buf = NULL; + + grpc_call_details_init (&call_details); + + request_call (); + + grpc_profiler_start ("server.prof"); + signal (SIGINT, sigint_handler); + while (!shutdown_finished) + { + if (got_sigint && !shutdown_started) + { + gpr_log (GPR_INFO, "Shutting down due to SIGINT"); + grpc_server_shutdown_and_notify (server, cq, tag (1000)); + GPR_ASSERT (grpc_completion_queue_pluck (cq, tag (1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), NULL).type == GRPC_OP_COMPLETE); + grpc_completion_queue_shutdown (cq); + shutdown_started = 1; + } + ev = grpc_completion_queue_next (cq, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000000, GPR_TIMESPAN)), NULL); + s = ev.tag; + switch (ev.type) + { + case GRPC_OP_COMPLETE: + switch ((gpr_intptr) s) + { + case FLING_SERVER_NEW_REQUEST: + if (call != NULL) + { + if (0 == strcmp (call_details.method, "/Reflector/reflectStream")) + { + /* Received streaming call. Send metadata here. */ + start_read_op (FLING_SERVER_READ_FOR_STREAMING); + send_initial_metadata (); + } + else + { + /* Received unary call. Can do all ops in one batch. */ + start_read_op (FLING_SERVER_READ_FOR_UNARY); + } + } + else + { + GPR_ASSERT (shutdown_started); + } + /* request_call(); + */ + break; + case FLING_SERVER_READ_FOR_STREAMING: + if (payload_buffer != NULL) + { + /* Received payload from client. */ + start_write_op (); + } + else + { + /* Received end of stream from client. */ + start_send_status (); + } + break; + case FLING_SERVER_WRITE_FOR_STREAMING: + /* Write completed at server */ + grpc_byte_buffer_destroy (payload_buffer); + payload_buffer = NULL; + start_read_op (FLING_SERVER_READ_FOR_STREAMING); + break; + case FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING: + /* Metadata send completed at server */ + break; + case FLING_SERVER_SEND_STATUS_FOR_STREAMING: + /* Send status and close completed at server */ + grpc_call_destroy (call); + if (!shutdown_started) + request_call (); + break; + case FLING_SERVER_READ_FOR_UNARY: + /* Finished payload read for unary. Start all reamaining + * unary ops in a batch. + */ + handle_unary_method (); + break; + case FLING_SERVER_BATCH_OPS_FOR_UNARY: + /* Finished unary call. */ + grpc_byte_buffer_destroy (payload_buffer); + payload_buffer = NULL; + grpc_call_destroy (call); + if (!shutdown_started) + request_call (); + break; + } + break; + case GRPC_QUEUE_SHUTDOWN: + GPR_ASSERT (shutdown_started); + shutdown_finished = 1; + break; + case GRPC_QUEUE_TIMEOUT: + break; + } } - } - grpc_profiler_stop(); - grpc_call_details_destroy(&call_details); + grpc_profiler_stop (); + grpc_call_details_destroy (&call_details); - grpc_server_destroy(server); - grpc_completion_queue_destroy(cq); - grpc_shutdown(); + grpc_server_destroy (server); + grpc_completion_queue_destroy (cq); + grpc_shutdown (); return 0; } diff --git a/test/core/httpcli/format_request_test.c b/test/core/httpcli/format_request_test.c index 82b2ccb122..7aa7624f20 100644 --- a/test/core/httpcli/format_request_test.c +++ b/test/core/httpcli/format_request_test.c @@ -38,87 +38,71 @@ #include <grpc/support/log.h> #include "test/core/util/test_config.h" -static void test_format_get_request(void) { - grpc_httpcli_header hdr = {"x-yz", "abc"}; +static void +test_format_get_request (void) +{ + grpc_httpcli_header hdr = { "x-yz", "abc" }; grpc_httpcli_request req; gpr_slice slice; - memset(&req, 0, sizeof(req)); + memset (&req, 0, sizeof (req)); req.host = "example.com"; req.path = "/index.html"; req.hdr_count = 1; req.hdrs = &hdr; - slice = grpc_httpcli_format_get_request(&req); + slice = grpc_httpcli_format_get_request (&req); - GPR_ASSERT(0 == gpr_slice_str_cmp(slice, - "GET /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT - "\r\n" - "x-yz: abc\r\n" - "\r\n")); + GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "GET /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "\r\n")); - gpr_slice_unref(slice); + gpr_slice_unref (slice); } -static void test_format_post_request(void) { - grpc_httpcli_header hdr = {"x-yz", "abc"}; +static void +test_format_post_request (void) +{ + grpc_httpcli_header hdr = { "x-yz", "abc" }; grpc_httpcli_request req; gpr_slice slice; char body_bytes[] = "fake body"; size_t body_len = 9; - memset(&req, 0, sizeof(req)); + memset (&req, 0, sizeof (req)); req.host = "example.com"; req.path = "/index.html"; req.hdr_count = 1; req.hdrs = &hdr; - slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len); - - GPR_ASSERT(0 == gpr_slice_str_cmp(slice, - "POST /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT - "\r\n" - "x-yz: abc\r\n" - "Content-Type: text/plain\r\n" - "Content-Length: 9\r\n" - "\r\n" - "fake body")); - - gpr_slice_unref(slice); + slice = grpc_httpcli_format_post_request (&req, body_bytes, body_len); + + GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "POST /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "Content-Type: text/plain\r\n" "Content-Length: 9\r\n" "\r\n" "fake body")); + + gpr_slice_unref (slice); } -static void test_format_post_request_no_body(void) { - grpc_httpcli_header hdr = {"x-yz", "abc"}; +static void +test_format_post_request_no_body (void) +{ + grpc_httpcli_header hdr = { "x-yz", "abc" }; grpc_httpcli_request req; gpr_slice slice; - memset(&req, 0, sizeof(req)); + memset (&req, 0, sizeof (req)); req.host = "example.com"; req.path = "/index.html"; req.hdr_count = 1; req.hdrs = &hdr; - slice = grpc_httpcli_format_post_request(&req, NULL, 0); + slice = grpc_httpcli_format_post_request (&req, NULL, 0); - GPR_ASSERT(0 == gpr_slice_str_cmp(slice, - "POST /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT - "\r\n" - "x-yz: abc\r\n" - "\r\n")); + GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "POST /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "\r\n")); - gpr_slice_unref(slice); + gpr_slice_unref (slice); } -static void test_format_post_request_content_type_override(void) { +static void +test_format_post_request_content_type_override (void) +{ grpc_httpcli_header hdrs[2]; grpc_httpcli_request req; gpr_slice slice; @@ -129,36 +113,28 @@ static void test_format_post_request_content_type_override(void) { hdrs[0].value = "abc"; hdrs[1].key = "Content-Type"; hdrs[1].value = "application/x-www-form-urlencoded"; - memset(&req, 0, sizeof(req)); + memset (&req, 0, sizeof (req)); req.host = "example.com"; req.path = "/index.html"; req.hdr_count = 2; req.hdrs = hdrs; - slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len); - - GPR_ASSERT(0 == gpr_slice_str_cmp( - slice, - "POST /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" - "x-yz: abc\r\n" - "Content-Type: application/x-www-form-urlencoded\r\n" - "Content-Length: 11\r\n" - "\r\n" - "fake%20body")); - - gpr_slice_unref(slice); + slice = grpc_httpcli_format_post_request (&req, body_bytes, body_len); + + GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "POST /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: 11\r\n" "\r\n" "fake%20body")); + + gpr_slice_unref (slice); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); - test_format_get_request(); - test_format_post_request(); - test_format_post_request_no_body(); - test_format_post_request_content_type_override(); + test_format_get_request (); + test_format_post_request (); + test_format_post_request_no_body (); + test_format_post_request_content_type_override (); return 0; } diff --git a/test/core/httpcli/httpcli_test.c b/test/core/httpcli/httpcli_test.c index 063565ecfc..1982b00cd1 100644 --- a/test/core/httpcli/httpcli_test.c +++ b/test/core/httpcli/httpcli_test.c @@ -49,138 +49,146 @@ static int g_done = 0; static grpc_httpcli_context g_context; static grpc_pollset g_pollset; -static gpr_timespec n_seconds_time(int seconds) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(seconds); +static gpr_timespec +n_seconds_time (int seconds) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (seconds); } -static void on_finish(void *arg, const grpc_httpcli_response *response, - grpc_closure_list *closure_list) { - const char *expect = - "<html><head><title>Hello world!</title></head>" - "<body><p>This is a test</p></body></html>"; - GPR_ASSERT(arg == (void *)42); - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); - GPR_ASSERT(response->body_length == strlen(expect)); - GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length)); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); +static void +on_finish (void *arg, const grpc_httpcli_response * response, grpc_closure_list * closure_list) +{ + const char *expect = "<html><head><title>Hello world!</title></head>" "<body><p>This is a test</p></body></html>"; + GPR_ASSERT (arg == (void *) 42); + GPR_ASSERT (response); + GPR_ASSERT (response->status == 200); + GPR_ASSERT (response->body_length == strlen (expect)); + GPR_ASSERT (0 == memcmp (expect, response->body, response->body_length)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); g_done = 1; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -static void test_get(int use_ssl, int port) { +static void +test_get (int use_ssl, int port) +{ grpc_httpcli_request req; char *host; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; g_done = 0; - gpr_log(GPR_INFO, "running %s with use_ssl=%d.", "test_get", use_ssl); + gpr_log (GPR_INFO, "running %s with use_ssl=%d.", "test_get", use_ssl); - gpr_asprintf(&host, "localhost:%d", port); - gpr_log(GPR_INFO, "requesting from %s", host); + gpr_asprintf (&host, "localhost:%d", port); + gpr_log (GPR_INFO, "requesting from %s", host); - memset(&req, 0, sizeof(req)); + memset (&req, 0, sizeof (req)); req.host = host; req.path = "/get"; req.handshaker = use_ssl ? &grpc_httpcli_ssl : &grpc_httpcli_plaintext; - grpc_httpcli_get(&g_context, &g_pollset, &req, n_seconds_time(15), on_finish, - (void *)42, &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (!g_done) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - n_seconds_time(20), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - gpr_free(host); + grpc_httpcli_get (&g_context, &g_pollset, &req, n_seconds_time (15), on_finish, (void *) 42, &closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (!g_done) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), n_seconds_time (20), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + gpr_free (host); } -static void test_post(int use_ssl, int port) { +static void +test_post (int use_ssl, int port) +{ grpc_httpcli_request req; char *host; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; g_done = 0; - gpr_log(GPR_INFO, "running %s with use_ssl=%d.", "test_post", (int)use_ssl); + gpr_log (GPR_INFO, "running %s with use_ssl=%d.", "test_post", (int) use_ssl); - gpr_asprintf(&host, "localhost:%d", port); - gpr_log(GPR_INFO, "posting to %s", host); + gpr_asprintf (&host, "localhost:%d", port); + gpr_log (GPR_INFO, "posting to %s", host); - memset(&req, 0, sizeof(req)); + memset (&req, 0, sizeof (req)); req.host = host; req.path = "/post"; req.handshaker = use_ssl ? &grpc_httpcli_ssl : &grpc_httpcli_plaintext; - grpc_httpcli_post(&g_context, &g_pollset, &req, "hello", 5, - n_seconds_time(15), on_finish, (void *)42, &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (!g_done) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - n_seconds_time(20), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - gpr_free(host); + grpc_httpcli_post (&g_context, &g_pollset, &req, "hello", 5, n_seconds_time (15), on_finish, (void *) 42, &closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (!g_done) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), n_seconds_time (20), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + gpr_free (host); } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; gpr_subprocess *server; char *me = argv[0]; - char *lslash = strrchr(me, '/'); + char *lslash = strrchr (me, '/'); char *args[4]; char root[1024]; - int port = grpc_pick_unused_port_or_die(); + int port = grpc_pick_unused_port_or_die (); /* figure out where we are */ - if (lslash) { - memcpy(root, me, (size_t)(lslash - me)); - root[lslash - me] = 0; - } else { - strcpy(root, "."); - } + if (lslash) + { + memcpy (root, me, (size_t) (lslash - me)); + root[lslash - me] = 0; + } + else + { + strcpy (root, "."); + } /* start the server */ - gpr_asprintf(&args[0], "%s/../../test/core/httpcli/test_server.py", root); + gpr_asprintf (&args[0], "%s/../../test/core/httpcli/test_server.py", root); args[1] = "--port"; - gpr_asprintf(&args[2], "%d", port); - server = gpr_subprocess_create(3, (const char **)args); - GPR_ASSERT(server); - gpr_free(args[0]); - gpr_free(args[2]); - - gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(5, GPR_TIMESPAN))); - - grpc_test_init(argc, argv); - grpc_init(); - grpc_httpcli_context_init(&g_context); - grpc_pollset_init(&g_pollset); - - test_get(0, port); - test_post(0, port); - - grpc_httpcli_context_destroy(&g_context); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_shutdown(); - - gpr_subprocess_destroy(server); + gpr_asprintf (&args[2], "%d", port); + server = gpr_subprocess_create (3, (const char **) args); + GPR_ASSERT (server); + gpr_free (args[0]); + gpr_free (args[2]); + + gpr_sleep_until (gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_seconds (5, GPR_TIMESPAN))); + + grpc_test_init (argc, argv); + grpc_init (); + grpc_httpcli_context_init (&g_context); + grpc_pollset_init (&g_pollset); + + test_get (0, port); + test_post (0, port); + + grpc_httpcli_context_destroy (&g_context); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_shutdown (); + + gpr_subprocess_destroy (server); return 0; } diff --git a/test/core/httpcli/parser_test.c b/test/core/httpcli/parser_test.c index dacec0f72f..227cbbf4ec 100644 --- a/test/core/httpcli/parser_test.c +++ b/test/core/httpcli/parser_test.c @@ -42,114 +42,117 @@ #include "test/core/util/slice_splitter.h" #include "test/core/util/test_config.h" -static void test_succeeds(grpc_slice_split_mode split_mode, char *response, - int expect_status, char *expect_body, ...) { +static void +test_succeeds (grpc_slice_split_mode split_mode, char *response, int expect_status, char *expect_body, ...) +{ grpc_httpcli_parser parser; - gpr_slice input_slice = gpr_slice_from_copied_string(response); + gpr_slice input_slice = gpr_slice_from_copied_string (response); size_t num_slices; size_t i; gpr_slice *slices; va_list args; - grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices); - gpr_slice_unref(input_slice); + grpc_split_slices (split_mode, &input_slice, 1, &slices, &num_slices); + gpr_slice_unref (input_slice); - grpc_httpcli_parser_init(&parser); + grpc_httpcli_parser_init (&parser); - for (i = 0; i < num_slices; i++) { - GPR_ASSERT(grpc_httpcli_parser_parse(&parser, slices[i])); - gpr_slice_unref(slices[i]); - } - GPR_ASSERT(grpc_httpcli_parser_eof(&parser)); + for (i = 0; i < num_slices; i++) + { + GPR_ASSERT (grpc_httpcli_parser_parse (&parser, slices[i])); + gpr_slice_unref (slices[i]); + } + GPR_ASSERT (grpc_httpcli_parser_eof (&parser)); - GPR_ASSERT(expect_status == parser.r.status); - if (expect_body != NULL) { - GPR_ASSERT(strlen(expect_body) == parser.r.body_length); - GPR_ASSERT(0 == memcmp(expect_body, parser.r.body, parser.r.body_length)); - } else { - GPR_ASSERT(parser.r.body_length == 0); - } + GPR_ASSERT (expect_status == parser.r.status); + if (expect_body != NULL) + { + GPR_ASSERT (strlen (expect_body) == parser.r.body_length); + GPR_ASSERT (0 == memcmp (expect_body, parser.r.body, parser.r.body_length)); + } + else + { + GPR_ASSERT (parser.r.body_length == 0); + } - va_start(args, expect_body); + va_start (args, expect_body); i = 0; - for (;;) { - char *expect_key; - char *expect_value; - expect_key = va_arg(args, char *); - if (!expect_key) break; - GPR_ASSERT(i < parser.r.hdr_count); - expect_value = va_arg(args, char *); - GPR_ASSERT(expect_value); - GPR_ASSERT(0 == strcmp(expect_key, parser.r.hdrs[i].key)); - GPR_ASSERT(0 == strcmp(expect_value, parser.r.hdrs[i].value)); - i++; - } - va_end(args); - GPR_ASSERT(i == parser.r.hdr_count); - - grpc_httpcli_parser_destroy(&parser); - gpr_free(slices); + for (;;) + { + char *expect_key; + char *expect_value; + expect_key = va_arg (args, char *); + if (!expect_key) + break; + GPR_ASSERT (i < parser.r.hdr_count); + expect_value = va_arg (args, char *); + GPR_ASSERT (expect_value); + GPR_ASSERT (0 == strcmp (expect_key, parser.r.hdrs[i].key)); + GPR_ASSERT (0 == strcmp (expect_value, parser.r.hdrs[i].value)); + i++; + } + va_end (args); + GPR_ASSERT (i == parser.r.hdr_count); + + grpc_httpcli_parser_destroy (&parser); + gpr_free (slices); } -static void test_fails(grpc_slice_split_mode split_mode, char *response) { +static void +test_fails (grpc_slice_split_mode split_mode, char *response) +{ grpc_httpcli_parser parser; - gpr_slice input_slice = gpr_slice_from_copied_string(response); + gpr_slice input_slice = gpr_slice_from_copied_string (response); size_t num_slices; size_t i; gpr_slice *slices; int done = 0; - grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices); - gpr_slice_unref(input_slice); + grpc_split_slices (split_mode, &input_slice, 1, &slices, &num_slices); + gpr_slice_unref (input_slice); - grpc_httpcli_parser_init(&parser); + grpc_httpcli_parser_init (&parser); - for (i = 0; i < num_slices; i++) { - if (!done && !grpc_httpcli_parser_parse(&parser, slices[i])) { + for (i = 0; i < num_slices; i++) + { + if (!done && !grpc_httpcli_parser_parse (&parser, slices[i])) + { + done = 1; + } + gpr_slice_unref (slices[i]); + } + if (!done && !grpc_httpcli_parser_eof (&parser)) + { done = 1; } - gpr_slice_unref(slices[i]); - } - if (!done && !grpc_httpcli_parser_eof(&parser)) { - done = 1; - } - GPR_ASSERT(done); - - grpc_httpcli_parser_destroy(&parser); - gpr_free(slices); + GPR_ASSERT (done); + + grpc_httpcli_parser_destroy (&parser); + gpr_free (slices); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY, - GRPC_SLICE_SPLIT_ONE_BYTE}; - - grpc_test_init(argc, argv); - - for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) { - test_succeeds(split_modes[i], - "HTTP/1.0 200 OK\r\n" - "xyz: abc\r\n" - "\r\n" - "hello world!", - 200, "hello world!", "xyz", "abc", NULL); - test_succeeds(split_modes[i], - "HTTP/1.0 404 Not Found\r\n" - "\r\n", - 404, NULL, NULL); - test_succeeds(split_modes[i], - "HTTP/1.1 200 OK\r\n" - "xyz: abc\r\n" - "\r\n" - "hello world!", - 200, "hello world!", "xyz", "abc", NULL); - test_fails(split_modes[i], "HTTP/1.0\r\n"); - test_fails(split_modes[i], "HTTP/1.2\r\n"); - test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n"); - test_fails(split_modes[i], "HTTP/1.0 200 OK\n"); - test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n"); - test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n"); - } + const grpc_slice_split_mode split_modes[] = { GRPC_SLICE_SPLIT_IDENTITY, + GRPC_SLICE_SPLIT_ONE_BYTE + }; + + grpc_test_init (argc, argv); + + for (i = 0; i < GPR_ARRAY_SIZE (split_modes); i++) + { + test_succeeds (split_modes[i], "HTTP/1.0 200 OK\r\n" "xyz: abc\r\n" "\r\n" "hello world!", 200, "hello world!", "xyz", "abc", NULL); + test_succeeds (split_modes[i], "HTTP/1.0 404 Not Found\r\n" "\r\n", 404, NULL, NULL); + test_succeeds (split_modes[i], "HTTP/1.1 200 OK\r\n" "xyz: abc\r\n" "\r\n" "hello world!", 200, "hello world!", "xyz", "abc", NULL); + test_fails (split_modes[i], "HTTP/1.0\r\n"); + test_fails (split_modes[i], "HTTP/1.2\r\n"); + test_fails (split_modes[i], "HTTP/1.0 000 XYX\r\n"); + test_fails (split_modes[i], "HTTP/1.0 200 OK\n"); + test_fails (split_modes[i], "HTTP/1.0 200 OK\r\n"); + test_fails (split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n"); + } return 0; } diff --git a/test/core/iomgr/alarm_heap_test.c b/test/core/iomgr/alarm_heap_test.c index 13bf7e43f6..b179e62de4 100644 --- a/test/core/iomgr/alarm_heap_test.c +++ b/test/core/iomgr/alarm_heap_test.c @@ -40,242 +40,280 @@ #include <grpc/support/log.h> #include "test/core/util/test_config.h" -static gpr_timespec random_deadline(void) { +static gpr_timespec +random_deadline (void) +{ gpr_timespec ts; - ts.tv_sec = rand(); - ts.tv_nsec = rand(); + ts.tv_sec = rand (); + ts.tv_nsec = rand (); ts.clock_type = GPR_CLOCK_REALTIME; return ts; } -static grpc_alarm *create_test_elements(size_t num_elements) { - grpc_alarm *elems = gpr_malloc(num_elements * sizeof(grpc_alarm)); +static grpc_alarm * +create_test_elements (size_t num_elements) +{ + grpc_alarm *elems = gpr_malloc (num_elements * sizeof (grpc_alarm)); size_t i; - for (i = 0; i < num_elements; i++) { - elems[i].deadline = random_deadline(); - } + for (i = 0; i < num_elements; i++) + { + elems[i].deadline = random_deadline (); + } return elems; } -static int cmp_elem(const void *a, const void *b) { - int i = *(const int *)a; - int j = *(const int *)b; +static int +cmp_elem (const void *a, const void *b) +{ + int i = *(const int *) a; + int j = *(const int *) b; return i - j; } -static size_t *all_top(grpc_alarm_heap *pq, size_t *n) { +static size_t * +all_top (grpc_alarm_heap * pq, size_t * n) +{ size_t *vec = NULL; size_t *need_to_check_children; size_t num_need_to_check_children = 0; *n = 0; - if (pq->alarm_count == 0) return vec; - need_to_check_children = - gpr_malloc(pq->alarm_count * sizeof(*need_to_check_children)); + if (pq->alarm_count == 0) + return vec; + need_to_check_children = gpr_malloc (pq->alarm_count * sizeof (*need_to_check_children)); need_to_check_children[num_need_to_check_children++] = 0; - vec = gpr_malloc(pq->alarm_count * sizeof(*vec)); - while (num_need_to_check_children > 0) { - size_t ind = need_to_check_children[0]; - size_t leftchild, rightchild; - num_need_to_check_children--; - memmove(need_to_check_children, need_to_check_children + 1, - num_need_to_check_children * sizeof(*need_to_check_children)); - vec[(*n)++] = ind; - leftchild = 1u + 2u * ind; - if (leftchild < pq->alarm_count) { - if (gpr_time_cmp(pq->alarms[leftchild]->deadline, - pq->alarms[ind]->deadline) >= 0) { - need_to_check_children[num_need_to_check_children++] = leftchild; - } - rightchild = leftchild + 1; - if (rightchild < pq->alarm_count && - gpr_time_cmp(pq->alarms[rightchild]->deadline, - pq->alarms[ind]->deadline) >= 0) { - need_to_check_children[num_need_to_check_children++] = rightchild; - } + vec = gpr_malloc (pq->alarm_count * sizeof (*vec)); + while (num_need_to_check_children > 0) + { + size_t ind = need_to_check_children[0]; + size_t leftchild, rightchild; + num_need_to_check_children--; + memmove (need_to_check_children, need_to_check_children + 1, num_need_to_check_children * sizeof (*need_to_check_children)); + vec[(*n)++] = ind; + leftchild = 1u + 2u * ind; + if (leftchild < pq->alarm_count) + { + if (gpr_time_cmp (pq->alarms[leftchild]->deadline, pq->alarms[ind]->deadline) >= 0) + { + need_to_check_children[num_need_to_check_children++] = leftchild; + } + rightchild = leftchild + 1; + if (rightchild < pq->alarm_count && gpr_time_cmp (pq->alarms[rightchild]->deadline, pq->alarms[ind]->deadline) >= 0) + { + need_to_check_children[num_need_to_check_children++] = rightchild; + } + } } - } - gpr_free(need_to_check_children); + gpr_free (need_to_check_children); return vec; } -static void check_pq_top(grpc_alarm *elements, grpc_alarm_heap *pq, - gpr_uint8 *inpq, size_t num_elements) { - gpr_timespec max_deadline = gpr_inf_past(GPR_CLOCK_REALTIME); - size_t *max_deadline_indices = - gpr_malloc(num_elements * sizeof(*max_deadline_indices)); +static void +check_pq_top (grpc_alarm * elements, grpc_alarm_heap * pq, gpr_uint8 * inpq, size_t num_elements) +{ + gpr_timespec max_deadline = gpr_inf_past (GPR_CLOCK_REALTIME); + size_t *max_deadline_indices = gpr_malloc (num_elements * sizeof (*max_deadline_indices)); size_t *top_elements; size_t num_max_deadline_indices = 0; size_t num_top_elements; size_t i; - for (i = 0; i < num_elements; ++i) { - if (inpq[i] && gpr_time_cmp(elements[i].deadline, max_deadline) >= 0) { - if (gpr_time_cmp(elements[i].deadline, max_deadline) > 0) { - num_max_deadline_indices = 0; - max_deadline = elements[i].deadline; - } - max_deadline_indices[num_max_deadline_indices++] = elements[i].heap_index; + for (i = 0; i < num_elements; ++i) + { + if (inpq[i] && gpr_time_cmp (elements[i].deadline, max_deadline) >= 0) + { + if (gpr_time_cmp (elements[i].deadline, max_deadline) > 0) + { + num_max_deadline_indices = 0; + max_deadline = elements[i].deadline; + } + max_deadline_indices[num_max_deadline_indices++] = elements[i].heap_index; + } + } + qsort (max_deadline_indices, num_max_deadline_indices, sizeof (*max_deadline_indices), cmp_elem); + top_elements = all_top (pq, &num_top_elements); + GPR_ASSERT (num_top_elements == num_max_deadline_indices); + for (i = 0; i < num_top_elements; i++) + { + GPR_ASSERT (max_deadline_indices[i] == top_elements[i]); } - } - qsort(max_deadline_indices, num_max_deadline_indices, - sizeof(*max_deadline_indices), cmp_elem); - top_elements = all_top(pq, &num_top_elements); - GPR_ASSERT(num_top_elements == num_max_deadline_indices); - for (i = 0; i < num_top_elements; i++) { - GPR_ASSERT(max_deadline_indices[i] == top_elements[i]); - } - gpr_free(max_deadline_indices); - gpr_free(top_elements); + gpr_free (max_deadline_indices); + gpr_free (top_elements); } -static int contains(grpc_alarm_heap *pq, grpc_alarm *el) { +static int +contains (grpc_alarm_heap * pq, grpc_alarm * el) +{ size_t i; - for (i = 0; i < pq->alarm_count; i++) { - if (pq->alarms[i] == el) return 1; - } + for (i = 0; i < pq->alarm_count; i++) + { + if (pq->alarms[i] == el) + return 1; + } return 0; } -static void check_valid(grpc_alarm_heap *pq) { +static void +check_valid (grpc_alarm_heap * pq) +{ size_t i; - for (i = 0; i < pq->alarm_count; ++i) { - size_t left_child = 1u + 2u * i; - size_t right_child = left_child + 1u; - if (left_child < pq->alarm_count) { - GPR_ASSERT(gpr_time_cmp(pq->alarms[i]->deadline, - pq->alarms[left_child]->deadline) >= 0); + for (i = 0; i < pq->alarm_count; ++i) + { + size_t left_child = 1u + 2u * i; + size_t right_child = left_child + 1u; + if (left_child < pq->alarm_count) + { + GPR_ASSERT (gpr_time_cmp (pq->alarms[i]->deadline, pq->alarms[left_child]->deadline) >= 0); + } + if (right_child < pq->alarm_count) + { + GPR_ASSERT (gpr_time_cmp (pq->alarms[i]->deadline, pq->alarms[right_child]->deadline) >= 0); + } } - if (right_child < pq->alarm_count) { - GPR_ASSERT(gpr_time_cmp(pq->alarms[i]->deadline, - pq->alarms[right_child]->deadline) >= 0); - } - } } -static void test1(void) { +static void +test1 (void) +{ grpc_alarm_heap pq; const size_t num_test_elements = 200; const size_t num_test_operations = 10000; size_t i; - grpc_alarm *test_elements = create_test_elements(num_test_elements); - gpr_uint8 *inpq = gpr_malloc(num_test_elements); - - grpc_alarm_heap_init(&pq); - memset(inpq, 0, num_test_elements); - GPR_ASSERT(grpc_alarm_heap_is_empty(&pq)); - check_valid(&pq); - for (i = 0; i < num_test_elements; ++i) { - GPR_ASSERT(!contains(&pq, &test_elements[i])); - grpc_alarm_heap_add(&pq, &test_elements[i]); - check_valid(&pq); - GPR_ASSERT(contains(&pq, &test_elements[i])); - inpq[i] = 1; - check_pq_top(test_elements, &pq, inpq, num_test_elements); - } - for (i = 0; i < num_test_elements; ++i) { - /* Test that check still succeeds even for element that wasn't just - inserted. */ - GPR_ASSERT(contains(&pq, &test_elements[i])); - } - - GPR_ASSERT(pq.alarm_count == num_test_elements); - - check_pq_top(test_elements, &pq, inpq, num_test_elements); - - for (i = 0; i < num_test_operations; ++i) { - size_t elem_num = (size_t)rand() % num_test_elements; - grpc_alarm *el = &test_elements[elem_num]; - if (!inpq[elem_num]) { /* not in pq */ - GPR_ASSERT(!contains(&pq, el)); - el->deadline = random_deadline(); - grpc_alarm_heap_add(&pq, el); - GPR_ASSERT(contains(&pq, el)); - inpq[elem_num] = 1; - check_pq_top(test_elements, &pq, inpq, num_test_elements); - check_valid(&pq); - } else { - GPR_ASSERT(contains(&pq, el)); - grpc_alarm_heap_remove(&pq, el); - GPR_ASSERT(!contains(&pq, el)); - inpq[elem_num] = 0; - check_pq_top(test_elements, &pq, inpq, num_test_elements); - check_valid(&pq); + grpc_alarm *test_elements = create_test_elements (num_test_elements); + gpr_uint8 *inpq = gpr_malloc (num_test_elements); + + grpc_alarm_heap_init (&pq); + memset (inpq, 0, num_test_elements); + GPR_ASSERT (grpc_alarm_heap_is_empty (&pq)); + check_valid (&pq); + for (i = 0; i < num_test_elements; ++i) + { + GPR_ASSERT (!contains (&pq, &test_elements[i])); + grpc_alarm_heap_add (&pq, &test_elements[i]); + check_valid (&pq); + GPR_ASSERT (contains (&pq, &test_elements[i])); + inpq[i] = 1; + check_pq_top (test_elements, &pq, inpq, num_test_elements); + } + for (i = 0; i < num_test_elements; ++i) + { + /* Test that check still succeeds even for element that wasn't just + inserted. */ + GPR_ASSERT (contains (&pq, &test_elements[i])); } - } - grpc_alarm_heap_destroy(&pq); - gpr_free(test_elements); - gpr_free(inpq); + GPR_ASSERT (pq.alarm_count == num_test_elements); + + check_pq_top (test_elements, &pq, inpq, num_test_elements); + + for (i = 0; i < num_test_operations; ++i) + { + size_t elem_num = (size_t) rand () % num_test_elements; + grpc_alarm *el = &test_elements[elem_num]; + if (!inpq[elem_num]) + { /* not in pq */ + GPR_ASSERT (!contains (&pq, el)); + el->deadline = random_deadline (); + grpc_alarm_heap_add (&pq, el); + GPR_ASSERT (contains (&pq, el)); + inpq[elem_num] = 1; + check_pq_top (test_elements, &pq, inpq, num_test_elements); + check_valid (&pq); + } + else + { + GPR_ASSERT (contains (&pq, el)); + grpc_alarm_heap_remove (&pq, el); + GPR_ASSERT (!contains (&pq, el)); + inpq[elem_num] = 0; + check_pq_top (test_elements, &pq, inpq, num_test_elements); + check_valid (&pq); + } + } + + grpc_alarm_heap_destroy (&pq); + gpr_free (test_elements); + gpr_free (inpq); } -static void shrink_test(void) { +static void +shrink_test (void) +{ grpc_alarm_heap pq; size_t i; size_t expected_size; /* A large random number to allow for multiple shrinkages, at least 512. */ - const size_t num_elements = (size_t)rand() % 2000 + 512; + const size_t num_elements = (size_t) rand () % 2000 + 512; - grpc_alarm_heap_init(&pq); + grpc_alarm_heap_init (&pq); /* Create a priority queue with many elements. Make sure the Size() is correct. */ - for (i = 0; i < num_elements; ++i) { - GPR_ASSERT(i == pq.alarm_count); - grpc_alarm_heap_add(&pq, create_test_elements(1)); - } - GPR_ASSERT(num_elements == pq.alarm_count); + for (i = 0; i < num_elements; ++i) + { + GPR_ASSERT (i == pq.alarm_count); + grpc_alarm_heap_add (&pq, create_test_elements (1)); + } + GPR_ASSERT (num_elements == pq.alarm_count); /* Remove elements until the Size is 1/4 the original size. */ - while (pq.alarm_count > num_elements / 4) { - grpc_alarm *const te = pq.alarms[pq.alarm_count - 1]; - grpc_alarm_heap_remove(&pq, te); - gpr_free(te); - } - GPR_ASSERT(num_elements / 4 == pq.alarm_count); + while (pq.alarm_count > num_elements / 4) + { + grpc_alarm *const te = pq.alarms[pq.alarm_count - 1]; + grpc_alarm_heap_remove (&pq, te); + gpr_free (te); + } + GPR_ASSERT (num_elements / 4 == pq.alarm_count); /* Expect that Capacity is in the right range: Size * 2 <= Capacity <= Size * 4 */ - GPR_ASSERT(pq.alarm_count * 2 <= pq.alarm_capacity); - GPR_ASSERT(pq.alarm_capacity <= pq.alarm_count * 4); - check_valid(&pq); + GPR_ASSERT (pq.alarm_count * 2 <= pq.alarm_capacity); + GPR_ASSERT (pq.alarm_capacity <= pq.alarm_count * 4); + check_valid (&pq); /* Remove the rest of the elements. Check that the Capacity is not more than 4 times the Size and not less than 2 times, but never goes below 16. */ expected_size = pq.alarm_count; - while (pq.alarm_count > 0) { - const size_t which = (size_t)rand() % pq.alarm_count; - grpc_alarm *te = pq.alarms[which]; - grpc_alarm_heap_remove(&pq, te); - gpr_free(te); - expected_size--; - GPR_ASSERT(expected_size == pq.alarm_count); - GPR_ASSERT(pq.alarm_count * 2 <= pq.alarm_capacity); - if (pq.alarm_count >= 8) { - GPR_ASSERT(pq.alarm_capacity <= pq.alarm_count * 4); - } else { - GPR_ASSERT(16 <= pq.alarm_capacity); + while (pq.alarm_count > 0) + { + const size_t which = (size_t) rand () % pq.alarm_count; + grpc_alarm *te = pq.alarms[which]; + grpc_alarm_heap_remove (&pq, te); + gpr_free (te); + expected_size--; + GPR_ASSERT (expected_size == pq.alarm_count); + GPR_ASSERT (pq.alarm_count * 2 <= pq.alarm_capacity); + if (pq.alarm_count >= 8) + { + GPR_ASSERT (pq.alarm_capacity <= pq.alarm_count * 4); + } + else + { + GPR_ASSERT (16 <= pq.alarm_capacity); + } + check_valid (&pq); } - check_valid(&pq); - } - GPR_ASSERT(0 == pq.alarm_count); - GPR_ASSERT(pq.alarm_capacity >= 16 && pq.alarm_capacity < 32); + GPR_ASSERT (0 == pq.alarm_count); + GPR_ASSERT (pq.alarm_capacity >= 16 && pq.alarm_capacity < 32); - grpc_alarm_heap_destroy(&pq); + grpc_alarm_heap_destroy (&pq); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ int i; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - for (i = 0; i < 5; i++) { - test1(); - shrink_test(); - } + for (i = 0; i < 5; i++) + { + test1 (); + shrink_test (); + } return 0; } diff --git a/test/core/iomgr/alarm_list_test.c b/test/core/iomgr/alarm_list_test.c index 90ff49cb8f..d81dd49c35 100644 --- a/test/core/iomgr/alarm_list_test.c +++ b/test/core/iomgr/alarm_list_test.c @@ -43,117 +43,115 @@ static int cb_called[MAX_CB][2]; -static void cb(void *arg, int success, grpc_closure_list *closure_list) { - cb_called[(gpr_intptr)arg][success]++; +static void +cb (void *arg, int success, grpc_closure_list * closure_list) +{ + cb_called[(gpr_intptr) arg][success]++; } -static void add_test(void) { - gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME); +static void +add_test (void) +{ + gpr_timespec start = gpr_now (GPR_CLOCK_REALTIME); int i; grpc_alarm alarms[20]; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_alarm_list_init(start); - memset(cb_called, 0, sizeof(cb_called)); + grpc_alarm_list_init (start); + memset (cb_called, 0, sizeof (cb_called)); /* 10 ms alarms. will expire in the current epoch */ - for (i = 0; i < 10; i++) { - grpc_alarm_init(&alarms[i], - gpr_time_add(start, gpr_time_from_millis(10, GPR_TIMESPAN)), - cb, (void *)(gpr_intptr)i, start, &closure_list); - } + for (i = 0; i < 10; i++) + { + grpc_alarm_init (&alarms[i], gpr_time_add (start, gpr_time_from_millis (10, GPR_TIMESPAN)), cb, (void *) (gpr_intptr) i, start, &closure_list); + } /* 1010 ms alarms. will expire in the next epoch */ - for (i = 10; i < 20; i++) { - grpc_alarm_init(&alarms[i], gpr_time_add(start, gpr_time_from_millis( - 1010, GPR_TIMESPAN)), - cb, (void *)(gpr_intptr)i, start, &closure_list); - } + for (i = 10; i < 20; i++) + { + grpc_alarm_init (&alarms[i], gpr_time_add (start, gpr_time_from_millis (1010, GPR_TIMESPAN)), cb, (void *) (gpr_intptr) i, start, &closure_list); + } /* collect alarms. Only the first batch should be ready. */ - GPR_ASSERT(10 == grpc_alarm_check(gpr_time_add(start, gpr_time_from_millis( - 500, GPR_TIMESPAN)), - NULL, &closure_list)); - grpc_closure_list_run(&closure_list); - for (i = 0; i < 20; i++) { - GPR_ASSERT(cb_called[i][1] == (i < 10)); - GPR_ASSERT(cb_called[i][0] == 0); - } - - GPR_ASSERT(0 == grpc_alarm_check(gpr_time_add(start, gpr_time_from_millis( - 600, GPR_TIMESPAN)), - NULL, &closure_list)); - grpc_closure_list_run(&closure_list); - for (i = 0; i < 30; i++) { - GPR_ASSERT(cb_called[i][1] == (i < 10)); - GPR_ASSERT(cb_called[i][0] == 0); - } + GPR_ASSERT (10 == grpc_alarm_check (gpr_time_add (start, gpr_time_from_millis (500, GPR_TIMESPAN)), NULL, &closure_list)); + grpc_closure_list_run (&closure_list); + for (i = 0; i < 20; i++) + { + GPR_ASSERT (cb_called[i][1] == (i < 10)); + GPR_ASSERT (cb_called[i][0] == 0); + } + + GPR_ASSERT (0 == grpc_alarm_check (gpr_time_add (start, gpr_time_from_millis (600, GPR_TIMESPAN)), NULL, &closure_list)); + grpc_closure_list_run (&closure_list); + for (i = 0; i < 30; i++) + { + GPR_ASSERT (cb_called[i][1] == (i < 10)); + GPR_ASSERT (cb_called[i][0] == 0); + } /* collect the rest of the alarms */ - GPR_ASSERT(10 == - grpc_alarm_check( - gpr_time_add(start, gpr_time_from_millis(1500, GPR_TIMESPAN)), - NULL, &closure_list)); - grpc_closure_list_run(&closure_list); - for (i = 0; i < 30; i++) { - GPR_ASSERT(cb_called[i][1] == (i < 20)); - GPR_ASSERT(cb_called[i][0] == 0); - } - - GPR_ASSERT(0 == grpc_alarm_check(gpr_time_add(start, gpr_time_from_millis( - 1600, GPR_TIMESPAN)), - NULL, &closure_list)); - for (i = 0; i < 30; i++) { - GPR_ASSERT(cb_called[i][1] == (i < 20)); - GPR_ASSERT(cb_called[i][0] == 0); - } - - grpc_alarm_list_shutdown(&closure_list); - grpc_closure_list_run(&closure_list); + GPR_ASSERT (10 == grpc_alarm_check (gpr_time_add (start, gpr_time_from_millis (1500, GPR_TIMESPAN)), NULL, &closure_list)); + grpc_closure_list_run (&closure_list); + for (i = 0; i < 30; i++) + { + GPR_ASSERT (cb_called[i][1] == (i < 20)); + GPR_ASSERT (cb_called[i][0] == 0); + } + + GPR_ASSERT (0 == grpc_alarm_check (gpr_time_add (start, gpr_time_from_millis (1600, GPR_TIMESPAN)), NULL, &closure_list)); + for (i = 0; i < 30; i++) + { + GPR_ASSERT (cb_called[i][1] == (i < 20)); + GPR_ASSERT (cb_called[i][0] == 0); + } + + grpc_alarm_list_shutdown (&closure_list); + grpc_closure_list_run (&closure_list); } -static gpr_timespec tfm(int m) { - gpr_timespec t = gpr_time_from_millis(m, GPR_TIMESPAN); +static gpr_timespec +tfm (int m) +{ + gpr_timespec t = gpr_time_from_millis (m, GPR_TIMESPAN); t.clock_type = GPR_CLOCK_REALTIME; return t; } /* Cleaning up a list with pending alarms. */ -void destruction_test(void) { +void +destruction_test (void) +{ grpc_alarm alarms[5]; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_alarm_list_init(gpr_time_0(GPR_CLOCK_REALTIME)); - memset(cb_called, 0, sizeof(cb_called)); - - grpc_alarm_init(&alarms[0], tfm(100), cb, (void *)(gpr_intptr)0, - gpr_time_0(GPR_CLOCK_REALTIME), &closure_list); - grpc_alarm_init(&alarms[1], tfm(3), cb, (void *)(gpr_intptr)1, - gpr_time_0(GPR_CLOCK_REALTIME), &closure_list); - grpc_alarm_init(&alarms[2], tfm(100), cb, (void *)(gpr_intptr)2, - gpr_time_0(GPR_CLOCK_REALTIME), &closure_list); - grpc_alarm_init(&alarms[3], tfm(3), cb, (void *)(gpr_intptr)3, - gpr_time_0(GPR_CLOCK_REALTIME), &closure_list); - grpc_alarm_init(&alarms[4], tfm(1), cb, (void *)(gpr_intptr)4, - gpr_time_0(GPR_CLOCK_REALTIME), &closure_list); - GPR_ASSERT(1 == grpc_alarm_check(tfm(2), NULL, &closure_list)); - grpc_closure_list_run(&closure_list); - GPR_ASSERT(1 == cb_called[4][1]); - grpc_alarm_cancel(&alarms[0], &closure_list); - grpc_alarm_cancel(&alarms[3], &closure_list); - grpc_closure_list_run(&closure_list); - GPR_ASSERT(1 == cb_called[0][0]); - GPR_ASSERT(1 == cb_called[3][0]); - - grpc_alarm_list_shutdown(&closure_list); - grpc_closure_list_run(&closure_list); - GPR_ASSERT(1 == cb_called[1][0]); - GPR_ASSERT(1 == cb_called[2][0]); + grpc_alarm_list_init (gpr_time_0 (GPR_CLOCK_REALTIME)); + memset (cb_called, 0, sizeof (cb_called)); + + grpc_alarm_init (&alarms[0], tfm (100), cb, (void *) (gpr_intptr) 0, gpr_time_0 (GPR_CLOCK_REALTIME), &closure_list); + grpc_alarm_init (&alarms[1], tfm (3), cb, (void *) (gpr_intptr) 1, gpr_time_0 (GPR_CLOCK_REALTIME), &closure_list); + grpc_alarm_init (&alarms[2], tfm (100), cb, (void *) (gpr_intptr) 2, gpr_time_0 (GPR_CLOCK_REALTIME), &closure_list); + grpc_alarm_init (&alarms[3], tfm (3), cb, (void *) (gpr_intptr) 3, gpr_time_0 (GPR_CLOCK_REALTIME), &closure_list); + grpc_alarm_init (&alarms[4], tfm (1), cb, (void *) (gpr_intptr) 4, gpr_time_0 (GPR_CLOCK_REALTIME), &closure_list); + GPR_ASSERT (1 == grpc_alarm_check (tfm (2), NULL, &closure_list)); + grpc_closure_list_run (&closure_list); + GPR_ASSERT (1 == cb_called[4][1]); + grpc_alarm_cancel (&alarms[0], &closure_list); + grpc_alarm_cancel (&alarms[3], &closure_list); + grpc_closure_list_run (&closure_list); + GPR_ASSERT (1 == cb_called[0][0]); + GPR_ASSERT (1 == cb_called[3][0]); + + grpc_alarm_list_shutdown (&closure_list); + grpc_closure_list_run (&closure_list); + GPR_ASSERT (1 == cb_called[1][0]); + GPR_ASSERT (1 == cb_called[2][0]); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - add_test(); - destruction_test(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + add_test (); + destruction_test (); return 0; } diff --git a/test/core/iomgr/endpoint_pair_test.c b/test/core/iomgr/endpoint_pair_test.c index 32c715c428..4e7079ef14 100644 --- a/test/core/iomgr/endpoint_pair_test.c +++ b/test/core/iomgr/endpoint_pair_test.c @@ -44,43 +44,50 @@ static grpc_pollset g_pollset; -static void clean_up(void) {} +static void +clean_up (void) +{ +} -static grpc_endpoint_test_fixture create_fixture_endpoint_pair( - size_t slice_size) { +static grpc_endpoint_test_fixture +create_fixture_endpoint_pair (size_t slice_size) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_endpoint_test_fixture f; - grpc_endpoint_pair p = grpc_iomgr_create_endpoint_pair("test", slice_size); + grpc_endpoint_pair p = grpc_iomgr_create_endpoint_pair ("test", slice_size); f.client_ep = p.client; f.server_ep = p.server; - grpc_endpoint_add_to_pollset(f.client_ep, &g_pollset, &closure_list); - grpc_endpoint_add_to_pollset(f.server_ep, &g_pollset, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_endpoint_add_to_pollset (f.client_ep, &g_pollset, &closure_list); + grpc_endpoint_add_to_pollset (f.server_ep, &g_pollset, &closure_list); + grpc_closure_list_run (&closure_list); return f; } static grpc_endpoint_test_config configs[] = { - {"tcp/tcp_socketpair", create_fixture_endpoint_pair, clean_up}, + {"tcp/tcp_socketpair", create_fixture_endpoint_pair, clean_up}, }; -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_init(); - grpc_pollset_init(&g_pollset); - grpc_endpoint_tests(configs[0], &g_pollset); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_shutdown(); + grpc_test_init (argc, argv); + grpc_init (); + grpc_pollset_init (&g_pollset); + grpc_endpoint_tests (configs[0], &g_pollset); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_shutdown (); return 0; } diff --git a/test/core/iomgr/endpoint_tests.c b/test/core/iomgr/endpoint_tests.c index e106881d91..4a72043283 100644 --- a/test/core/iomgr/endpoint_tests.c +++ b/test/core/iomgr/endpoint_tests.c @@ -60,56 +60,67 @@ static grpc_pollset *g_pollset; -size_t count_slices(gpr_slice *slices, size_t nslices, int *current_data) { +size_t +count_slices (gpr_slice * slices, size_t nslices, int *current_data) +{ size_t num_bytes = 0; size_t i; size_t j; unsigned char *buf; - for (i = 0; i < nslices; ++i) { - buf = GPR_SLICE_START_PTR(slices[i]); - for (j = 0; j < GPR_SLICE_LENGTH(slices[i]); ++j) { - GPR_ASSERT(buf[j] == *current_data); - *current_data = (*current_data + 1) % 256; + for (i = 0; i < nslices; ++i) + { + buf = GPR_SLICE_START_PTR (slices[i]); + for (j = 0; j < GPR_SLICE_LENGTH (slices[i]); ++j) + { + GPR_ASSERT (buf[j] == *current_data); + *current_data = (*current_data + 1) % 256; + } + num_bytes += GPR_SLICE_LENGTH (slices[i]); } - num_bytes += GPR_SLICE_LENGTH(slices[i]); - } return num_bytes; } -static grpc_endpoint_test_fixture begin_test(grpc_endpoint_test_config config, - const char *test_name, - size_t slice_size) { - gpr_log(GPR_INFO, "%s/%s", test_name, config.name); - return config.create_fixture(slice_size); +static grpc_endpoint_test_fixture +begin_test (grpc_endpoint_test_config config, const char *test_name, size_t slice_size) +{ + gpr_log (GPR_INFO, "%s/%s", test_name, config.name); + return config.create_fixture (slice_size); } -static void end_test(grpc_endpoint_test_config config) { config.clean_up(); } +static void +end_test (grpc_endpoint_test_config config) +{ + config.clean_up (); +} -static gpr_slice *allocate_blocks(size_t num_bytes, size_t slice_size, - size_t *num_blocks, gpr_uint8 *current_data) { +static gpr_slice * +allocate_blocks (size_t num_bytes, size_t slice_size, size_t * num_blocks, gpr_uint8 * current_data) +{ size_t nslices = num_bytes / slice_size + (num_bytes % slice_size ? 1 : 0); - gpr_slice *slices = malloc(sizeof(gpr_slice) * nslices); + gpr_slice *slices = malloc (sizeof (gpr_slice) * nslices); size_t num_bytes_left = num_bytes; size_t i; size_t j; unsigned char *buf; *num_blocks = nslices; - for (i = 0; i < nslices; ++i) { - slices[i] = gpr_slice_malloc(slice_size > num_bytes_left ? num_bytes_left - : slice_size); - num_bytes_left -= GPR_SLICE_LENGTH(slices[i]); - buf = GPR_SLICE_START_PTR(slices[i]); - for (j = 0; j < GPR_SLICE_LENGTH(slices[i]); ++j) { - buf[j] = *current_data; - (*current_data)++; + for (i = 0; i < nslices; ++i) + { + slices[i] = gpr_slice_malloc (slice_size > num_bytes_left ? num_bytes_left : slice_size); + num_bytes_left -= GPR_SLICE_LENGTH (slices[i]); + buf = GPR_SLICE_START_PTR (slices[i]); + for (j = 0; j < GPR_SLICE_LENGTH (slices[i]); ++j) + { + buf[j] = *current_data; + (*current_data)++; + } } - } - GPR_ASSERT(num_bytes_left == 0); + GPR_ASSERT (num_bytes_left == 0); return slices; } -struct read_and_write_test_state { +struct read_and_write_test_state +{ grpc_endpoint *read_ep; grpc_endpoint *write_ep; size_t target_bytes; @@ -126,76 +137,79 @@ struct read_and_write_test_state { grpc_closure done_write; }; -static void read_and_write_test_read_handler(void *data, int success, - grpc_closure_list *closure_list) { +static void +read_and_write_test_read_handler (void *data, int success, grpc_closure_list * closure_list) +{ struct read_and_write_test_state *state = data; - state->bytes_read += count_slices( - state->incoming.slices, state->incoming.count, &state->current_read_data); - if (state->bytes_read == state->target_bytes || !success) { - gpr_log(GPR_INFO, "Read handler done"); - gpr_mu_lock(GRPC_POLLSET_MU(g_pollset)); - state->read_done = 1 + success; - grpc_pollset_kick(g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(g_pollset)); - } else if (success) { - grpc_endpoint_read(state->read_ep, &state->incoming, &state->done_read, - closure_list); - } + state->bytes_read += count_slices (state->incoming.slices, state->incoming.count, &state->current_read_data); + if (state->bytes_read == state->target_bytes || !success) + { + gpr_log (GPR_INFO, "Read handler done"); + gpr_mu_lock (GRPC_POLLSET_MU (g_pollset)); + state->read_done = 1 + success; + grpc_pollset_kick (g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (g_pollset)); + } + else if (success) + { + grpc_endpoint_read (state->read_ep, &state->incoming, &state->done_read, closure_list); + } } -static void read_and_write_test_write_handler(void *data, int success, - grpc_closure_list *closure_list) { +static void +read_and_write_test_write_handler (void *data, int success, grpc_closure_list * closure_list) +{ struct read_and_write_test_state *state = data; gpr_slice *slices = NULL; size_t nslices; - if (success) { - state->bytes_written += state->current_write_size; - if (state->target_bytes - state->bytes_written < - state->current_write_size) { - state->current_write_size = state->target_bytes - state->bytes_written; + if (success) + { + state->bytes_written += state->current_write_size; + if (state->target_bytes - state->bytes_written < state->current_write_size) + { + state->current_write_size = state->target_bytes - state->bytes_written; + } + if (state->current_write_size != 0) + { + slices = allocate_blocks (state->current_write_size, 8192, &nslices, &state->current_write_data); + gpr_slice_buffer_reset_and_unref (&state->outgoing); + gpr_slice_buffer_addn (&state->outgoing, slices, nslices); + grpc_endpoint_write (state->write_ep, &state->outgoing, &state->done_write, closure_list); + free (slices); + return; + } } - if (state->current_write_size != 0) { - slices = allocate_blocks(state->current_write_size, 8192, &nslices, - &state->current_write_data); - gpr_slice_buffer_reset_and_unref(&state->outgoing); - gpr_slice_buffer_addn(&state->outgoing, slices, nslices); - grpc_endpoint_write(state->write_ep, &state->outgoing, &state->done_write, - closure_list); - free(slices); - return; - } - } - gpr_log(GPR_INFO, "Write handler done"); - gpr_mu_lock(GRPC_POLLSET_MU(g_pollset)); + gpr_log (GPR_INFO, "Write handler done"); + gpr_mu_lock (GRPC_POLLSET_MU (g_pollset)); state->write_done = 1 + success; - grpc_pollset_kick(g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(g_pollset)); + grpc_pollset_kick (g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (g_pollset)); } /* Do both reading and writing using the grpc_endpoint API. This also includes a test of the shutdown behavior. */ -static void read_and_write_test(grpc_endpoint_test_config config, - size_t num_bytes, size_t write_size, - size_t slice_size, int shutdown) { +static void +read_and_write_test (grpc_endpoint_test_config config, size_t num_bytes, size_t write_size, size_t slice_size, int shutdown) +{ struct read_and_write_test_state state; - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(20); - grpc_endpoint_test_fixture f = - begin_test(config, "read_and_write_test", slice_size); + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (20); + grpc_endpoint_test_fixture f = begin_test (config, "read_and_write_test", slice_size); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_DEBUG, "num_bytes=%d write_size=%d slice_size=%d shutdown=%d", - num_bytes, write_size, slice_size, shutdown); + gpr_log (GPR_DEBUG, "num_bytes=%d write_size=%d slice_size=%d shutdown=%d", num_bytes, write_size, slice_size, shutdown); - if (shutdown) { - gpr_log(GPR_INFO, "Start read and write shutdown test"); - } else { - gpr_log(GPR_INFO, "Start read and write test with %d bytes, slice size %d", - num_bytes, slice_size); - } + if (shutdown) + { + gpr_log (GPR_INFO, "Start read and write shutdown test"); + } + else + { + gpr_log (GPR_INFO, "Start read and write test with %d bytes, slice size %d", num_bytes, slice_size); + } state.read_ep = f.client_ep; state.write_ep = f.server_ep; @@ -207,58 +221,59 @@ static void read_and_write_test(grpc_endpoint_test_config config, state.write_done = 0; state.current_read_data = 0; state.current_write_data = 0; - grpc_closure_init(&state.done_read, read_and_write_test_read_handler, &state); - grpc_closure_init(&state.done_write, read_and_write_test_write_handler, - &state); - gpr_slice_buffer_init(&state.outgoing); - gpr_slice_buffer_init(&state.incoming); + grpc_closure_init (&state.done_read, read_and_write_test_read_handler, &state); + grpc_closure_init (&state.done_write, read_and_write_test_write_handler, &state); + gpr_slice_buffer_init (&state.outgoing); + gpr_slice_buffer_init (&state.incoming); /* Get started by pretending an initial write completed */ /* NOTE: Sets up initial conditions so we can have the same write handler for the first iteration as for later iterations. It does the right thing even when bytes_written is unsigned. */ state.bytes_written -= state.current_write_size; - read_and_write_test_write_handler(&state, 1, &closure_list); - grpc_closure_list_run(&closure_list); + read_and_write_test_write_handler (&state, 1, &closure_list); + grpc_closure_list_run (&closure_list); - grpc_endpoint_read(state.read_ep, &state.incoming, &state.done_read, - &closure_list); + grpc_endpoint_read (state.read_ep, &state.incoming, &state.done_read, &closure_list); - if (shutdown) { - gpr_log(GPR_DEBUG, "shutdown read"); - grpc_endpoint_shutdown(state.read_ep, &closure_list); - gpr_log(GPR_DEBUG, "shutdown write"); - grpc_endpoint_shutdown(state.write_ep, &closure_list); - } - grpc_closure_list_run(&closure_list); + if (shutdown) + { + gpr_log (GPR_DEBUG, "shutdown read"); + grpc_endpoint_shutdown (state.read_ep, &closure_list); + gpr_log (GPR_DEBUG, "shutdown write"); + grpc_endpoint_shutdown (state.write_ep, &closure_list); + } + grpc_closure_list_run (&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(g_pollset)); - while (!state.read_done || !state.write_done) { - grpc_pollset_worker worker; - GPR_ASSERT(gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) < 0); - grpc_pollset_work(g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - } - gpr_mu_unlock(GRPC_POLLSET_MU(g_pollset)); - grpc_closure_list_run(&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (g_pollset)); + while (!state.read_done || !state.write_done) + { + grpc_pollset_worker worker; + GPR_ASSERT (gpr_time_cmp (gpr_now (GPR_CLOCK_MONOTONIC), deadline) < 0); + grpc_pollset_work (g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + } + gpr_mu_unlock (GRPC_POLLSET_MU (g_pollset)); + grpc_closure_list_run (&closure_list); - end_test(config); - gpr_slice_buffer_destroy(&state.outgoing); - gpr_slice_buffer_destroy(&state.incoming); - grpc_endpoint_destroy(state.read_ep, &closure_list); - grpc_endpoint_destroy(state.write_ep, &closure_list); - grpc_closure_list_run(&closure_list); + end_test (config); + gpr_slice_buffer_destroy (&state.outgoing); + gpr_slice_buffer_destroy (&state.incoming); + grpc_endpoint_destroy (state.read_ep, &closure_list); + grpc_endpoint_destroy (state.write_ep, &closure_list); + grpc_closure_list_run (&closure_list); } -void grpc_endpoint_tests(grpc_endpoint_test_config config, - grpc_pollset *pollset) { +void +grpc_endpoint_tests (grpc_endpoint_test_config config, grpc_pollset * pollset) +{ size_t i; g_pollset = pollset; - read_and_write_test(config, 10000000, 100000, 8192, 0); - read_and_write_test(config, 1000000, 100000, 1, 0); - read_and_write_test(config, 100000000, 100000, 1, 1); - for (i = 1; i < 1000; i = GPR_MAX(i + 1, i * 5 / 4)) { - read_and_write_test(config, 40320, i, i, 0); - } + read_and_write_test (config, 10000000, 100000, 8192, 0); + read_and_write_test (config, 1000000, 100000, 1, 0); + read_and_write_test (config, 100000000, 100000, 1, 1); + for (i = 1; i < 1000; i = GPR_MAX (i + 1, i * 5 / 4)) + { + read_and_write_test (config, 40320, i, i, 0); + } g_pollset = NULL; } diff --git a/test/core/iomgr/endpoint_tests.h b/test/core/iomgr/endpoint_tests.h index 700f854891..2e6335c22e 100644 --- a/test/core/iomgr/endpoint_tests.h +++ b/test/core/iomgr/endpoint_tests.h @@ -41,18 +41,19 @@ typedef struct grpc_endpoint_test_config grpc_endpoint_test_config; typedef struct grpc_endpoint_test_fixture grpc_endpoint_test_fixture; -struct grpc_endpoint_test_fixture { +struct grpc_endpoint_test_fixture +{ grpc_endpoint *client_ep; grpc_endpoint *server_ep; }; -struct grpc_endpoint_test_config { +struct grpc_endpoint_test_config +{ const char *name; - grpc_endpoint_test_fixture (*create_fixture)(size_t slice_size); - void (*clean_up)(); + grpc_endpoint_test_fixture (*create_fixture) (size_t slice_size); + void (*clean_up) (); }; -void grpc_endpoint_tests(grpc_endpoint_test_config config, - grpc_pollset *pollset); +void grpc_endpoint_tests (grpc_endpoint_test_config config, grpc_pollset * pollset); #endif /* GRPC_TEST_CORE_IOMGR_ENDPOINT_TESTS_H */ diff --git a/test/core/iomgr/fd_conservation_posix_test.c b/test/core/iomgr/fd_conservation_posix_test.c index 1ffb5808b7..703edc660e 100644 --- a/test/core/iomgr/fd_conservation_posix_test.c +++ b/test/core/iomgr/fd_conservation_posix_test.c @@ -39,28 +39,31 @@ #include "src/core/iomgr/endpoint_pair.h" #include "src/core/iomgr/iomgr.h" -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ int i; struct rlimit rlim; grpc_endpoint_pair p; - grpc_test_init(argc, argv); - grpc_iomgr_init(); + grpc_test_init (argc, argv); + grpc_iomgr_init (); /* set max # of file descriptors to a low value, and verify we can create and destroy many more than this number of descriptors */ rlim.rlim_cur = rlim.rlim_max = 10; - GPR_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &rlim)); + GPR_ASSERT (0 == setrlimit (RLIMIT_NOFILE, &rlim)); - for (i = 0; i < 100; i++) { - grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - p = grpc_iomgr_create_endpoint_pair("test", 1); - grpc_endpoint_destroy(p.client, &closure_list); - grpc_endpoint_destroy(p.server, &closure_list); - grpc_closure_list_run(&closure_list); - } + for (i = 0; i < 100; i++) + { + grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; + p = grpc_iomgr_create_endpoint_pair ("test", 1); + grpc_endpoint_destroy (p.client, &closure_list); + grpc_endpoint_destroy (p.server, &closure_list); + grpc_closure_list_run (&closure_list); + } - grpc_iomgr_shutdown(); + grpc_iomgr_shutdown (); return 0; } diff --git a/test/core/iomgr/fd_posix_test.c b/test/core/iomgr/fd_posix_test.c index 0a8d40419b..5449c38768 100644 --- a/test/core/iomgr/fd_posix_test.c +++ b/test/core/iomgr/fd_posix_test.c @@ -60,162 +60,186 @@ static grpc_pollset g_pollset; /* Create a test socket with the right properties for testing. port is the TCP port to listen or connect to. Return a socket FD and sockaddr_in. */ -static void create_test_socket(int port, int *socket_fd, - struct sockaddr_in *sin) { +static void +create_test_socket (int port, int *socket_fd, struct sockaddr_in *sin) +{ int fd; int one = 1; int buf_size = BUF_SIZE; int flags; - fd = socket(AF_INET, SOCK_STREAM, 0); - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + fd = socket (AF_INET, SOCK_STREAM, 0); + setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)); /* Reset the size of socket send buffer to the minimal value to facilitate buffer filling up and triggering notify_on_write */ - GPR_ASSERT( - setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof(buf_size)) != -1); - GPR_ASSERT( - setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size)) != -1); + GPR_ASSERT (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof (buf_size)) != -1); + GPR_ASSERT (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof (buf_size)) != -1); /* Make fd non-blocking */ - flags = fcntl(fd, F_GETFL, 0); - GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0); + flags = fcntl (fd, F_GETFL, 0); + GPR_ASSERT (fcntl (fd, F_SETFL, flags | O_NONBLOCK) == 0); *socket_fd = fd; /* Use local address for test */ sin->sin_family = AF_INET; - sin->sin_addr.s_addr = htonl(0x7f000001); - GPR_ASSERT(port >= 0 && port < 65536); - sin->sin_port = htons((gpr_uint16)port); + sin->sin_addr.s_addr = htonl (0x7f000001); + GPR_ASSERT (port >= 0 && port < 65536); + sin->sin_port = htons ((gpr_uint16) port); } /* Dummy gRPC callback */ -void no_op_cb(void *arg, int success) {} +void +no_op_cb (void *arg, int success) +{ +} /* =======An upload server to test notify_on_read=========== The server simply reads and counts a stream of bytes. */ /* An upload server. */ -typedef struct { - grpc_fd *em_fd; /* listening fd */ - ssize_t read_bytes_total; /* total number of received bytes */ - int done; /* set to 1 when a server finishes serving */ +typedef struct +{ + grpc_fd *em_fd; /* listening fd */ + ssize_t read_bytes_total; /* total number of received bytes */ + int done; /* set to 1 when a server finishes serving */ grpc_closure listen_closure; } server; -static void server_init(server *sv) { +static void +server_init (server * sv) +{ sv->read_bytes_total = 0; sv->done = 0; } /* An upload session. Created when a new upload request arrives in the server. */ -typedef struct { - server *sv; /* not owned by a single session */ - grpc_fd *em_fd; /* fd to read upload bytes */ - char read_buf[BUF_SIZE]; /* buffer to store upload bytes */ +typedef struct +{ + server *sv; /* not owned by a single session */ + grpc_fd *em_fd; /* fd to read upload bytes */ + char read_buf[BUF_SIZE]; /* buffer to store upload bytes */ grpc_closure session_read_closure; } session; /* Called when an upload session can be safely shutdown. Close session FD and start to shutdown listen FD. */ -static void session_shutdown_cb(void *arg, /*session*/ - int success, grpc_closure_list *closure_list) { +static void +session_shutdown_cb (void *arg, /*session */ + int success, grpc_closure_list * closure_list) +{ session *se = arg; server *sv = se->sv; - grpc_fd_orphan(se->em_fd, NULL, "a", closure_list); - gpr_free(se); + grpc_fd_orphan (se->em_fd, NULL, "a", closure_list); + gpr_free (se); /* Start to shutdown listen fd. */ - grpc_fd_shutdown(sv->em_fd, closure_list); + grpc_fd_shutdown (sv->em_fd, closure_list); } /* Called when data become readable in a session. */ -static void session_read_cb(void *arg, /*session*/ - int success, grpc_closure_list *closure_list) { +static void +session_read_cb (void *arg, /*session */ + int success, grpc_closure_list * closure_list) +{ session *se = arg; int fd = se->em_fd->fd; ssize_t read_once = 0; ssize_t read_total = 0; - if (!success) { - session_shutdown_cb(arg, 1, closure_list); - return; - } + if (!success) + { + session_shutdown_cb (arg, 1, closure_list); + return; + } - do { - read_once = read(fd, se->read_buf, BUF_SIZE); - if (read_once > 0) read_total += read_once; - } while (read_once > 0); + do + { + read_once = read (fd, se->read_buf, BUF_SIZE); + if (read_once > 0) + read_total += read_once; + } + while (read_once > 0); se->sv->read_bytes_total += read_total; /* read() returns 0 to indicate the TCP connection was closed by the client. read(fd, read_buf, 0) also returns 0 which should never be called as such. It is possible to read nothing due to spurious edge event or data has been drained, In such a case, read() returns -1 and set errno to EAGAIN. */ - if (read_once == 0) { - session_shutdown_cb(arg, 1, closure_list); - } else if (read_once == -1) { - if (errno == EAGAIN) { - /* An edge triggered event is cached in the kernel until next poll. - In the current single thread implementation, session_read_cb is called - in the polling thread, such that polling only happens after this - callback, and will catch read edge event if data is available again - before notify_on_read. - TODO(chenw): in multi-threaded version, callback and polling can be - run in different threads. polling may catch a persist read edge event - before notify_on_read is called. */ - grpc_fd_notify_on_read(se->em_fd, &se->session_read_closure, - closure_list); - } else { - gpr_log(GPR_ERROR, "Unhandled read error %s", strerror(errno)); - abort(); + if (read_once == 0) + { + session_shutdown_cb (arg, 1, closure_list); + } + else if (read_once == -1) + { + if (errno == EAGAIN) + { + /* An edge triggered event is cached in the kernel until next poll. + In the current single thread implementation, session_read_cb is called + in the polling thread, such that polling only happens after this + callback, and will catch read edge event if data is available again + before notify_on_read. + TODO(chenw): in multi-threaded version, callback and polling can be + run in different threads. polling may catch a persist read edge event + before notify_on_read is called. */ + grpc_fd_notify_on_read (se->em_fd, &se->session_read_closure, closure_list); + } + else + { + gpr_log (GPR_ERROR, "Unhandled read error %s", strerror (errno)); + abort (); + } } - } } /* Called when the listen FD can be safely shutdown. Close listen FD and signal that server can be shutdown. */ -static void listen_shutdown_cb(void *arg /*server*/, int success, - grpc_closure_list *closure_list) { +static void +listen_shutdown_cb (void *arg /*server */ , int success, + grpc_closure_list * closure_list) +{ server *sv = arg; - grpc_fd_orphan(sv->em_fd, NULL, "b", closure_list); + grpc_fd_orphan (sv->em_fd, NULL, "b", closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); sv->done = 1; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } /* Called when a new TCP connection request arrives in the listening port. */ -static void listen_cb(void *arg, /*=sv_arg*/ - int success, grpc_closure_list *closure_list) { +static void +listen_cb (void *arg, /*=sv_arg*/ + int success, grpc_closure_list * closure_list) +{ server *sv = arg; int fd; int flags; session *se; struct sockaddr_storage ss; - socklen_t slen = sizeof(ss); + socklen_t slen = sizeof (ss); grpc_fd *listen_em_fd = sv->em_fd; - if (!success) { - listen_shutdown_cb(arg, 1, closure_list); - return; - } - - fd = accept(listen_em_fd->fd, (struct sockaddr *)&ss, &slen); - GPR_ASSERT(fd >= 0); - GPR_ASSERT(fd < FD_SETSIZE); - flags = fcntl(fd, F_GETFL, 0); - fcntl(fd, F_SETFL, flags | O_NONBLOCK); - se = gpr_malloc(sizeof(*se)); + if (!success) + { + listen_shutdown_cb (arg, 1, closure_list); + return; + } + + fd = accept (listen_em_fd->fd, (struct sockaddr *) &ss, &slen); + GPR_ASSERT (fd >= 0); + GPR_ASSERT (fd < FD_SETSIZE); + flags = fcntl (fd, F_GETFL, 0); + fcntl (fd, F_SETFL, flags | O_NONBLOCK); + se = gpr_malloc (sizeof (*se)); se->sv = sv; - se->em_fd = grpc_fd_create(fd, "listener"); - grpc_pollset_add_fd(&g_pollset, se->em_fd, closure_list); + se->em_fd = grpc_fd_create (fd, "listener"); + grpc_pollset_add_fd (&g_pollset, se->em_fd, closure_list); se->session_read_closure.cb = session_read_cb; se->session_read_closure.cb_arg = se; - grpc_fd_notify_on_read(se->em_fd, &se->session_read_closure, closure_list); + grpc_fd_notify_on_read (se->em_fd, &se->session_read_closure, closure_list); - grpc_fd_notify_on_read(listen_em_fd, &sv->listen_closure, closure_list); + grpc_fd_notify_on_read (listen_em_fd, &sv->listen_closure, closure_list); } /* Max number of connections pending to be accepted by listen(). */ @@ -225,42 +249,46 @@ static void listen_cb(void *arg, /*=sv_arg*/ listen_cb() is registered to be interested in reading from listen_fd. When connection request arrives, listen_cb() is called to accept the connection request. */ -static int server_start(server *sv, grpc_closure_list *closure_list) { +static int +server_start (server * sv, grpc_closure_list * closure_list) +{ int port = 0; int fd; struct sockaddr_in sin; socklen_t addr_len; - create_test_socket(port, &fd, &sin); - addr_len = sizeof(sin); - GPR_ASSERT(bind(fd, (struct sockaddr *)&sin, addr_len) == 0); - GPR_ASSERT(getsockname(fd, (struct sockaddr *)&sin, &addr_len) == 0); - port = ntohs(sin.sin_port); - GPR_ASSERT(listen(fd, MAX_NUM_FD) == 0); + create_test_socket (port, &fd, &sin); + addr_len = sizeof (sin); + GPR_ASSERT (bind (fd, (struct sockaddr *) &sin, addr_len) == 0); + GPR_ASSERT (getsockname (fd, (struct sockaddr *) &sin, &addr_len) == 0); + port = ntohs (sin.sin_port); + GPR_ASSERT (listen (fd, MAX_NUM_FD) == 0); - sv->em_fd = grpc_fd_create(fd, "server"); - grpc_pollset_add_fd(&g_pollset, sv->em_fd, closure_list); + sv->em_fd = grpc_fd_create (fd, "server"); + grpc_pollset_add_fd (&g_pollset, sv->em_fd, closure_list); /* Register to be interested in reading from listen_fd. */ sv->listen_closure.cb = listen_cb; sv->listen_closure.cb_arg = sv; - grpc_fd_notify_on_read(sv->em_fd, &sv->listen_closure, closure_list); + grpc_fd_notify_on_read (sv->em_fd, &sv->listen_closure, closure_list); return port; } /* Wait and shutdown a sever. */ -static void server_wait_and_shutdown(server *sv) { - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (!sv->done) { - grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); +static void +server_wait_and_shutdown (server * sv) +{ + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (!sv->done) + { + grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } /* ===An upload client to test notify_on_write=== */ @@ -271,7 +299,8 @@ static void server_wait_and_shutdown(server *sv) { #define CLIENT_TOTAL_WRITE_CNT 3 /* An upload client. */ -typedef struct { +typedef struct +{ grpc_fd *em_fd; char write_buf[CLIENT_WRITE_BUF_SIZE]; ssize_t write_bytes_total; @@ -279,158 +308,198 @@ typedef struct { notify_on_write to schedule another write. */ int client_write_cnt; - int done; /* set to 1 when a client finishes sending */ + int done; /* set to 1 when a client finishes sending */ grpc_closure write_closure; } client; -static void client_init(client *cl) { - memset(cl->write_buf, 0, sizeof(cl->write_buf)); +static void +client_init (client * cl) +{ + memset (cl->write_buf, 0, sizeof (cl->write_buf)); cl->write_bytes_total = 0; cl->client_write_cnt = 0; cl->done = 0; } /* Called when a client upload session is ready to shutdown. */ -static void client_session_shutdown_cb(void *arg /*client*/, int success, - grpc_closure_list *closure_list) { +static void +client_session_shutdown_cb (void *arg /*client */ , int success, + grpc_closure_list * closure_list) +{ client *cl = arg; - grpc_fd_orphan(cl->em_fd, NULL, "c", closure_list); + grpc_fd_orphan (cl->em_fd, NULL, "c", closure_list); cl->done = 1; - grpc_pollset_kick(&g_pollset, NULL); + grpc_pollset_kick (&g_pollset, NULL); } /* Write as much as possible, then register notify_on_write. */ -static void client_session_write(void *arg, /*client*/ - int success, grpc_closure_list *closure_list) { +static void +client_session_write (void *arg, /*client */ + int success, grpc_closure_list * closure_list) +{ client *cl = arg; int fd = cl->em_fd->fd; ssize_t write_once = 0; - if (!success) { - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - client_session_shutdown_cb(arg, 1, closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - return; - } - - do { - write_once = write(fd, cl->write_buf, CLIENT_WRITE_BUF_SIZE); - if (write_once > 0) cl->write_bytes_total += write_once; - } while (write_once > 0); - - if (errno == EAGAIN) { - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - if (cl->client_write_cnt < CLIENT_TOTAL_WRITE_CNT) { - cl->write_closure.cb = client_session_write; - cl->write_closure.cb_arg = cl; - grpc_fd_notify_on_write(cl->em_fd, &cl->write_closure, closure_list); - cl->client_write_cnt++; - } else { - client_session_shutdown_cb(arg, 1, closure_list); + if (!success) + { + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + client_session_shutdown_cb (arg, 1, closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + return; + } + + do + { + write_once = write (fd, cl->write_buf, CLIENT_WRITE_BUF_SIZE); + if (write_once > 0) + cl->write_bytes_total += write_once; + } + while (write_once > 0); + + if (errno == EAGAIN) + { + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + if (cl->client_write_cnt < CLIENT_TOTAL_WRITE_CNT) + { + cl->write_closure.cb = client_session_write; + cl->write_closure.cb_arg = cl; + grpc_fd_notify_on_write (cl->em_fd, &cl->write_closure, closure_list); + cl->client_write_cnt++; + } + else + { + client_session_shutdown_cb (arg, 1, closure_list); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + } + else + { + gpr_log (GPR_ERROR, "unknown errno %s", strerror (errno)); + abort (); } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - } else { - gpr_log(GPR_ERROR, "unknown errno %s", strerror(errno)); - abort(); - } } /* Start a client to send a stream of bytes. */ -static void client_start(client *cl, int port, - grpc_closure_list *closure_list) { +static void +client_start (client * cl, int port, grpc_closure_list * closure_list) +{ int fd; struct sockaddr_in sin; - create_test_socket(port, &fd, &sin); - if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { - if (errno == EINPROGRESS) { - struct pollfd pfd; - pfd.fd = fd; - pfd.events = POLLOUT; - pfd.revents = 0; - if (poll(&pfd, 1, -1) == -1) { - gpr_log(GPR_ERROR, "poll() failed during connect; errno=%d", errno); - abort(); - } - } else { - gpr_log(GPR_ERROR, "Failed to connect to the server (errno=%d)", errno); - abort(); + create_test_socket (port, &fd, &sin); + if (connect (fd, (struct sockaddr *) &sin, sizeof (sin)) == -1) + { + if (errno == EINPROGRESS) + { + struct pollfd pfd; + pfd.fd = fd; + pfd.events = POLLOUT; + pfd.revents = 0; + if (poll (&pfd, 1, -1) == -1) + { + gpr_log (GPR_ERROR, "poll() failed during connect; errno=%d", errno); + abort (); + } + } + else + { + gpr_log (GPR_ERROR, "Failed to connect to the server (errno=%d)", errno); + abort (); + } } - } - cl->em_fd = grpc_fd_create(fd, "client"); - grpc_pollset_add_fd(&g_pollset, cl->em_fd, closure_list); + cl->em_fd = grpc_fd_create (fd, "client"); + grpc_pollset_add_fd (&g_pollset, cl->em_fd, closure_list); - client_session_write(cl, 1, closure_list); + client_session_write (cl, 1, closure_list); } /* Wait for the signal to shutdown a client. */ -static void client_wait_and_shutdown(client *cl) { - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (!cl->done) { - grpc_pollset_worker worker; - grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); +static void +client_wait_and_shutdown (client * cl) +{ + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (!cl->done) + { + grpc_pollset_worker worker; + grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } /* Test grpc_fd. Start an upload server and client, upload a stream of bytes from the client to the server, and verify that the total number of sent bytes is equal to the total number of received bytes. */ -static void test_grpc_fd(void) { +static void +test_grpc_fd (void) +{ server sv; client cl; int port; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - server_init(&sv); - port = server_start(&sv, &closure_list); - client_init(&cl); - client_start(&cl, port, &closure_list); - grpc_closure_list_run(&closure_list); - client_wait_and_shutdown(&cl); - server_wait_and_shutdown(&sv); - GPR_ASSERT(sv.read_bytes_total == cl.write_bytes_total); - gpr_log(GPR_INFO, "Total read bytes %d", sv.read_bytes_total); + server_init (&sv); + port = server_start (&sv, &closure_list); + client_init (&cl); + client_start (&cl, port, &closure_list); + grpc_closure_list_run (&closure_list); + client_wait_and_shutdown (&cl); + server_wait_and_shutdown (&sv); + GPR_ASSERT (sv.read_bytes_total == cl.write_bytes_total); + gpr_log (GPR_INFO, "Total read bytes %d", sv.read_bytes_total); } -typedef struct fd_change_data { - void (*cb_that_ran)(void *, int success, grpc_closure_list *closure_list); +typedef struct fd_change_data +{ + void (*cb_that_ran) (void *, int success, grpc_closure_list * closure_list); } fd_change_data; -void init_change_data(fd_change_data *fdc) { fdc->cb_that_ran = NULL; } +void +init_change_data (fd_change_data * fdc) +{ + fdc->cb_that_ran = NULL; +} -void destroy_change_data(fd_change_data *fdc) {} +void +destroy_change_data (fd_change_data * fdc) +{ +} -static void first_read_callback(void *arg /* fd_change_data */, int success, - grpc_closure_list *closure_list) { +static void +first_read_callback (void *arg /* fd_change_data */ , int success, + grpc_closure_list * closure_list) +{ fd_change_data *fdc = arg; - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); fdc->cb_that_ran = first_read_callback; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -static void second_read_callback(void *arg /* fd_change_data */, int success, - grpc_closure_list *closure_list) { +static void +second_read_callback (void *arg /* fd_change_data */ , int success, + grpc_closure_list * closure_list) +{ fd_change_data *fdc = arg; - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); fdc->cb_that_ran = second_read_callback; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } /* Test that changing the callback we use for notify_on_read actually works. Note that we have two different but almost identical callbacks above -- the point is to have two different function pointers and two different data pointers and make sure that changing both really works. */ -static void test_grpc_fd_change(void) { +static void +test_grpc_fd_change (void) +{ grpc_fd *em_fd; fd_change_data a, b; int flags; @@ -446,84 +515,87 @@ static void test_grpc_fd_change(void) { second_closure.cb = second_read_callback; second_closure.cb_arg = &b; - init_change_data(&a); - init_change_data(&b); + init_change_data (&a); + init_change_data (&b); - GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); - flags = fcntl(sv[0], F_GETFL, 0); - GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0); - flags = fcntl(sv[1], F_GETFL, 0); - GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0); + GPR_ASSERT (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == 0); + flags = fcntl (sv[0], F_GETFL, 0); + GPR_ASSERT (fcntl (sv[0], F_SETFL, flags | O_NONBLOCK) == 0); + flags = fcntl (sv[1], F_GETFL, 0); + GPR_ASSERT (fcntl (sv[1], F_SETFL, flags | O_NONBLOCK) == 0); - em_fd = grpc_fd_create(sv[0], "test_grpc_fd_change"); - grpc_pollset_add_fd(&g_pollset, em_fd, &closure_list); + em_fd = grpc_fd_create (sv[0], "test_grpc_fd_change"); + grpc_pollset_add_fd (&g_pollset, em_fd, &closure_list); /* Register the first callback, then make its FD readable */ - grpc_fd_notify_on_read(em_fd, &first_closure, &closure_list); + grpc_fd_notify_on_read (em_fd, &first_closure, &closure_list); data = 0; - result = write(sv[1], &data, 1); - GPR_ASSERT(result == 1); + result = write (sv[1], &data, 1); + GPR_ASSERT (result == 1); /* And now wait for it to run. */ - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (a.cb_that_ran == NULL) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - GPR_ASSERT(a.cb_that_ran == first_read_callback); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (a.cb_that_ran == NULL) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + GPR_ASSERT (a.cb_that_ran == first_read_callback); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); /* And drain the socket so we can generate a new read edge */ - result = read(sv[0], &data, 1); - GPR_ASSERT(result == 1); + result = read (sv[0], &data, 1); + GPR_ASSERT (result == 1); /* Now register a second callback with distinct change data, and do the same thing again. */ - grpc_fd_notify_on_read(em_fd, &second_closure, &closure_list); + grpc_fd_notify_on_read (em_fd, &second_closure, &closure_list); data = 0; - result = write(sv[1], &data, 1); - GPR_ASSERT(result == 1); - - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (b.cb_that_ran == NULL) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } + result = write (sv[1], &data, 1); + GPR_ASSERT (result == 1); + + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (b.cb_that_ran == NULL) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } /* Except now we verify that second_read_callback ran instead */ - GPR_ASSERT(b.cb_that_ran == second_read_callback); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - - grpc_fd_orphan(em_fd, NULL, "d", &closure_list); - grpc_closure_list_run(&closure_list); - destroy_change_data(&a); - destroy_change_data(&b); - close(sv[1]); + GPR_ASSERT (b.cb_that_ran == second_read_callback); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + + grpc_fd_orphan (em_fd, NULL, "d", &closure_list); + grpc_closure_list_run (&closure_list); + destroy_change_data (&a); + destroy_change_data (&b); + close (sv[1]); } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_iomgr_init(); - grpc_pollset_init(&g_pollset); - test_grpc_fd(); - test_grpc_fd_change(); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_iomgr_shutdown(); + grpc_test_init (argc, argv); + grpc_iomgr_init (); + grpc_pollset_init (&g_pollset); + test_grpc_fd (); + test_grpc_fd_change (); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_iomgr_shutdown (); return 0; } diff --git a/test/core/iomgr/resolve_address_test.c b/test/core/iomgr/resolve_address_test.c index 4d41020768..a1ce7efaf0 100644 --- a/test/core/iomgr/resolve_address_test.c +++ b/test/core/iomgr/resolve_address_test.c @@ -38,101 +38,124 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -static gpr_timespec test_deadline(void) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(100); +static gpr_timespec +test_deadline (void) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (100); } -static void must_succeed(void* evp, grpc_resolved_addresses* p, - grpc_closure_list* closure_list) { - GPR_ASSERT(p); - GPR_ASSERT(p->naddrs >= 1); - grpc_resolved_addresses_destroy(p); - gpr_event_set(evp, (void*)1); +static void +must_succeed (void *evp, grpc_resolved_addresses * p, grpc_closure_list * closure_list) +{ + GPR_ASSERT (p); + GPR_ASSERT (p->naddrs >= 1); + grpc_resolved_addresses_destroy (p); + gpr_event_set (evp, (void *) 1); } -static void must_fail(void* evp, grpc_resolved_addresses* p, - grpc_closure_list* closure_list) { - GPR_ASSERT(!p); - gpr_event_set(evp, (void*)1); +static void +must_fail (void *evp, grpc_resolved_addresses * p, grpc_closure_list * closure_list) +{ + GPR_ASSERT (!p); + gpr_event_set (evp, (void *) 1); } -static void test_localhost(void) { +static void +test_localhost (void) +{ gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address("localhost:1", NULL, must_succeed, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); + gpr_event_init (&ev); + grpc_resolve_address ("localhost:1", NULL, must_succeed, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); } -static void test_default_port(void) { +static void +test_default_port (void) +{ gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address("localhost", "1", must_succeed, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); + gpr_event_init (&ev); + grpc_resolve_address ("localhost", "1", must_succeed, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); } -static void test_missing_default_port(void) { +static void +test_missing_default_port (void) +{ gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address("localhost", NULL, must_fail, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); + gpr_event_init (&ev); + grpc_resolve_address ("localhost", NULL, must_fail, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); } -static void test_ipv6_with_port(void) { +static void +test_ipv6_with_port (void) +{ gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address("[2001:db8::1]:1", NULL, must_succeed, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); + gpr_event_init (&ev); + grpc_resolve_address ("[2001:db8::1]:1", NULL, must_succeed, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); } -static void test_ipv6_without_port(void) { - const char* const kCases[] = { - "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]", +static void +test_ipv6_without_port (void) +{ + const char *const kCases[] = { + "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]", }; unsigned i; - for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) { - gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address(kCases[i], "80", must_succeed, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); - } + for (i = 0; i < sizeof (kCases) / sizeof (*kCases); i++) + { + gpr_event ev; + gpr_event_init (&ev); + grpc_resolve_address (kCases[i], "80", must_succeed, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); + } } -static void test_invalid_ip_addresses(void) { - const char* const kCases[] = { - "293.283.1238.3:1", "[2001:db8::11111]:1", +static void +test_invalid_ip_addresses (void) +{ + const char *const kCases[] = { + "293.283.1238.3:1", "[2001:db8::11111]:1", }; unsigned i; - for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) { - gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address(kCases[i], NULL, must_fail, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); - } + for (i = 0; i < sizeof (kCases) / sizeof (*kCases); i++) + { + gpr_event ev; + gpr_event_init (&ev); + grpc_resolve_address (kCases[i], NULL, must_fail, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); + } } -static void test_unparseable_hostports(void) { - const char* const kCases[] = { - "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1", +static void +test_unparseable_hostports (void) +{ + const char *const kCases[] = { + "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1", }; unsigned i; - for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) { - gpr_event ev; - gpr_event_init(&ev); - grpc_resolve_address(kCases[i], "1", must_fail, &ev); - GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); - } + for (i = 0; i < sizeof (kCases) / sizeof (*kCases); i++) + { + gpr_event ev; + gpr_event_init (&ev); + grpc_resolve_address (kCases[i], "1", must_fail, &ev); + GPR_ASSERT (gpr_event_wait (&ev, test_deadline ())); + } } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - grpc_iomgr_init(); - test_localhost(); - test_default_port(); - test_missing_default_port(); - test_ipv6_with_port(); - test_ipv6_without_port(); - test_invalid_ip_addresses(); - test_unparseable_hostports(); - grpc_iomgr_shutdown(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + grpc_iomgr_init (); + test_localhost (); + test_default_port (); + test_missing_default_port (); + test_ipv6_with_port (); + test_ipv6_without_port (); + test_invalid_ip_addresses (); + test_unparseable_hostports (); + grpc_iomgr_shutdown (); return 0; } diff --git a/test/core/iomgr/sockaddr_utils_test.c b/test/core/iomgr/sockaddr_utils_test.c index 72a0f71835..2f061cfa46 100644 --- a/test/core/iomgr/sockaddr_utils_test.c +++ b/test/core/iomgr/sockaddr_utils_test.c @@ -41,206 +41,216 @@ #include <grpc/support/port_platform.h> #include "test/core/util/test_config.h" -static struct sockaddr_in make_addr4(const gpr_uint8 *data, size_t data_len) { +static struct sockaddr_in +make_addr4 (const gpr_uint8 * data, size_t data_len) +{ struct sockaddr_in addr4; - memset(&addr4, 0, sizeof(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); + 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 struct sockaddr_in6 make_addr6(const gpr_uint8 *data, size_t data_len) { +static struct sockaddr_in6 +make_addr6 (const gpr_uint8 * data, size_t data_len) +{ struct sockaddr_in6 addr6; - memset(&addr6, 0, sizeof(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); + 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 const gpr_uint8 kMapped[] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0xff, 0xff, 192, 0, 2, 1}; -static const gpr_uint8 kNotQuiteMapped[] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0xff, 0xfe, 192, 0, 2, 99}; -static const gpr_uint8 kIPv4[] = {192, 0, 2, 1}; -static const gpr_uint8 kIPv6[] = {0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1}; +static const gpr_uint8 kMapped[] = { 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0xff, 0xff, 192, 0, 2, 1 +}; -static void test_sockaddr_is_v4mapped(void) { +static const gpr_uint8 kNotQuiteMapped[] = { 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0xff, 0xfe, 192, 0, 2, 99 +}; +static const gpr_uint8 kIPv4[] = { 192, 0, 2, 1 }; + +static const gpr_uint8 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; - gpr_log(GPR_INFO, "%s", "test_sockaddr_is_v4mapped"); + 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)); - expect4 = make_addr4(kIPv4, sizeof(kIPv4)); - GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0); + 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)); + 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)); + 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)); /* Output is unchanged. */ - GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0); + 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)); + input4 = make_addr4 (kIPv4, sizeof (kIPv4)); + GPR_ASSERT (!grpc_sockaddr_is_v4mapped ((const struct sockaddr *) &input4, NULL)); } -static void test_sockaddr_to_v4mapped(void) { +static void +test_sockaddr_to_v4mapped (void) +{ struct sockaddr_in input4; struct sockaddr_in6 input6; struct sockaddr_in6 output6; struct sockaddr_in6 expect6; - gpr_log(GPR_INFO, "%s", "test_sockaddr_to_v4mapped"); + 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)); - expect6 = make_addr6(kMapped, sizeof(kMapped)); - GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0); + input4 = make_addr4 (kIPv4, sizeof (kIPv4)); + GPR_ASSERT (grpc_sockaddr_to_v4mapped ((const struct sockaddr *) &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)); + input6 = make_addr6 (kIPv6, sizeof (kIPv6)); + GPR_ASSERT (!grpc_sockaddr_to_v4mapped ((const struct sockaddr *) &input6, &output6)); /* Output is unchanged. */ - GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0); + 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)); + input6 = make_addr6 (kMapped, sizeof (kMapped)); + GPR_ASSERT (!grpc_sockaddr_to_v4mapped ((const struct sockaddr *) &input6, &output6)); } -static void test_sockaddr_is_wildcard(void) { +static void +test_sockaddr_is_wildcard (void) +{ struct sockaddr_in wild4; struct sockaddr_in6 wild6; struct sockaddr_in6 wild_mapped; struct sockaddr dummy; int port; - gpr_log(GPR_INFO, "%s", "test_sockaddr_is_wildcard"); + 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)); + grpc_sockaddr_make_wildcards (555, &wild4, &wild6); + GPR_ASSERT (grpc_sockaddr_to_v4mapped ((const struct sockaddr *) &wild4, &wild_mapped)); /* Test 0.0.0.0:555 */ port = -1; - GPR_ASSERT(grpc_sockaddr_is_wildcard((const struct sockaddr *)&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)); + GPR_ASSERT (grpc_sockaddr_is_wildcard ((const struct sockaddr *) &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)); /* Test [::]:555 */ port = -1; - GPR_ASSERT(grpc_sockaddr_is_wildcard((const struct sockaddr *)&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)); + GPR_ASSERT (grpc_sockaddr_is_wildcard ((const struct sockaddr *) &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)); /* Test [::ffff:0.0.0.0]:555 */ port = -1; - GPR_ASSERT( - grpc_sockaddr_is_wildcard((const struct sockaddr *)&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)); + GPR_ASSERT (grpc_sockaddr_is_wildcard ((const struct sockaddr *) &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)); /* Test AF_UNSPEC. */ port = -1; - memset(&dummy, 0, sizeof(dummy)); - GPR_ASSERT(!grpc_sockaddr_is_wildcard(&dummy, &port)); - GPR_ASSERT(port == -1); + memset (&dummy, 0, sizeof (dummy)); + GPR_ASSERT (!grpc_sockaddr_is_wildcard (&dummy, &port)); + GPR_ASSERT (port == -1); } -static void expect_sockaddr_str(const char *expected, void *addr, - int normalize) { +static void +expect_sockaddr_str (const char *expected, void *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); - GPR_ASSERT(str != NULL); - GPR_ASSERT(result >= 0); - GPR_ASSERT((size_t)result == strlen(str)); - GPR_ASSERT(strcmp(expected, str) == 0); - gpr_free(str); + gpr_log (GPR_INFO, " expect_sockaddr_str(%s)", expected); + result = grpc_sockaddr_to_string (&str, (struct sockaddr *) addr, normalize); + GPR_ASSERT (str != NULL); + GPR_ASSERT (result >= 0); + GPR_ASSERT ((size_t) result == strlen (str)); + GPR_ASSERT (strcmp (expected, str) == 0); + gpr_free (str); } -static void expect_sockaddr_uri(const char *expected, void *addr) { +static void +expect_sockaddr_uri (const char *expected, void *addr) +{ char *str; - gpr_log(GPR_INFO, " expect_sockaddr_uri(%s)", expected); - str = grpc_sockaddr_to_uri((struct sockaddr *)addr); - GPR_ASSERT(str != NULL); - GPR_ASSERT(strcmp(expected, str) == 0); - gpr_free(str); + gpr_log (GPR_INFO, " expect_sockaddr_uri(%s)", expected); + str = grpc_sockaddr_to_uri ((struct sockaddr *) addr); + GPR_ASSERT (str != NULL); + GPR_ASSERT (strcmp (expected, str) == 0); + gpr_free (str); } -static void test_sockaddr_to_string(void) { +static void +test_sockaddr_to_string (void) +{ struct sockaddr_in input4; struct sockaddr_in6 input6; struct sockaddr dummy; - gpr_log(GPR_INFO, "%s", "test_sockaddr_to_string"); + gpr_log (GPR_INFO, "%s", "test_sockaddr_to_string"); errno = 0x7EADBEEF; - input4 = make_addr4(kIPv4, sizeof(kIPv4)); - expect_sockaddr_str("192.0.2.1:12345", &input4, 0); - expect_sockaddr_str("192.0.2.1:12345", &input4, 1); - expect_sockaddr_uri("ipv4:192.0.2.1:12345", &input4); + input4 = make_addr4 (kIPv4, sizeof (kIPv4)); + expect_sockaddr_str ("192.0.2.1:12345", &input4, 0); + expect_sockaddr_str ("192.0.2.1:12345", &input4, 1); + expect_sockaddr_uri ("ipv4:192.0.2.1:12345", &input4); - input6 = make_addr6(kIPv6, sizeof(kIPv6)); - expect_sockaddr_str("[2001:db8::1]:12345", &input6, 0); - expect_sockaddr_str("[2001:db8::1]:12345", &input6, 1); - expect_sockaddr_uri("ipv6:[2001:db8::1]:12345", &input6); + input6 = make_addr6 (kIPv6, sizeof (kIPv6)); + expect_sockaddr_str ("[2001:db8::1]:12345", &input6, 0); + expect_sockaddr_str ("[2001:db8::1]:12345", &input6, 1); + expect_sockaddr_uri ("ipv6:[2001:db8::1]:12345", &input6); - input6 = make_addr6(kMapped, sizeof(kMapped)); - expect_sockaddr_str("[::ffff:192.0.2.1]:12345", &input6, 0); - expect_sockaddr_str("192.0.2.1:12345", &input6, 1); - expect_sockaddr_uri("ipv4:192.0.2.1:12345", &input6); + input6 = make_addr6 (kMapped, sizeof (kMapped)); + expect_sockaddr_str ("[::ffff:192.0.2.1]:12345", &input6, 0); + expect_sockaddr_str ("192.0.2.1:12345", &input6, 1); + expect_sockaddr_uri ("ipv4:192.0.2.1:12345", &input6); - input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped)); - expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 0); - expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 1); - expect_sockaddr_uri("ipv6:[::fffe:c000:263]:12345", &input6); + input6 = make_addr6 (kNotQuiteMapped, sizeof (kNotQuiteMapped)); + expect_sockaddr_str ("[::fffe:c000:263]:12345", &input6, 0); + expect_sockaddr_str ("[::fffe:c000:263]:12345", &input6, 1); + expect_sockaddr_uri ("ipv6:[::fffe:c000:263]:12345", &input6); - memset(&dummy, 0, sizeof(dummy)); + memset (&dummy, 0, sizeof (dummy)); dummy.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); + expect_sockaddr_str ("(sockaddr family=123)", &dummy, 0); + expect_sockaddr_str ("(sockaddr family=123)", &dummy, 1); + GPR_ASSERT (grpc_sockaddr_to_uri (&dummy) == NULL); - GPR_ASSERT(errno == 0x7EADBEEF); + GPR_ASSERT (errno == 0x7EADBEEF); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); - test_sockaddr_is_v4mapped(); - test_sockaddr_to_v4mapped(); - test_sockaddr_is_wildcard(); - test_sockaddr_to_string(); + test_sockaddr_is_v4mapped (); + test_sockaddr_to_v4mapped (); + test_sockaddr_is_wildcard (); + test_sockaddr_to_string (); return 0; } diff --git a/test/core/iomgr/tcp_client_posix_test.c b/test/core/iomgr/tcp_client_posix_test.c index 250b071fc1..af9b96db14 100644 --- a/test/core/iomgr/tcp_client_posix_test.c +++ b/test/core/iomgr/tcp_client_posix_test.c @@ -52,126 +52,137 @@ static grpc_pollset g_pollset; static int g_connections_complete = 0; static grpc_endpoint *g_connecting = NULL; -static gpr_timespec test_deadline(void) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); +static gpr_timespec +test_deadline (void) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10); } -static void finish_connection() { - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); +static void +finish_connection () +{ + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); g_connections_complete++; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -static void must_succeed(void *arg, int success, - grpc_closure_list *closure_list) { - GPR_ASSERT(g_connecting != NULL); - GPR_ASSERT(success); - grpc_endpoint_shutdown(g_connecting, closure_list); - grpc_endpoint_destroy(g_connecting, closure_list); +static void +must_succeed (void *arg, int success, grpc_closure_list * closure_list) +{ + GPR_ASSERT (g_connecting != NULL); + GPR_ASSERT (success); + grpc_endpoint_shutdown (g_connecting, closure_list); + grpc_endpoint_destroy (g_connecting, closure_list); g_connecting = NULL; - finish_connection(); + finish_connection (); } -static void must_fail(void *arg, int success, grpc_closure_list *closure_list) { - GPR_ASSERT(g_connecting == NULL); - GPR_ASSERT(!success); - finish_connection(); +static void +must_fail (void *arg, int success, grpc_closure_list * closure_list) +{ + GPR_ASSERT (g_connecting == NULL); + GPR_ASSERT (!success); + finish_connection (); } -void test_succeeds(void) { +void +test_succeeds (void) +{ struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + socklen_t addr_len = sizeof (addr); int svr_fd; int r; int connections_complete_before; grpc_closure done; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_DEBUG, "test_succeeds"); + gpr_log (GPR_DEBUG, "test_succeeds"); - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); 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 == listen(svr_fd, 1)); + 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 == listen (svr_fd, 1)); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); connections_complete_before = g_connections_complete; - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); /* connect to it */ - GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0); - grpc_closure_init(&done, must_succeed, NULL); - grpc_tcp_client_connect(&done, &g_connecting, &g_pollset_set, - (struct sockaddr *)&addr, addr_len, - gpr_inf_future(GPR_CLOCK_REALTIME), &closure_list); + GPR_ASSERT (getsockname (svr_fd, (struct sockaddr *) &addr, &addr_len) == 0); + grpc_closure_init (&done, must_succeed, NULL); + grpc_tcp_client_connect (&done, &g_connecting, &g_pollset_set, (struct sockaddr *) &addr, addr_len, gpr_inf_future (GPR_CLOCK_REALTIME), &closure_list); /* await the connection */ - do { - addr_len = sizeof(addr); - r = accept(svr_fd, (struct sockaddr *)&addr, &addr_len); - } while (r == -1 && errno == EINTR); - GPR_ASSERT(r >= 0); - close(r); - - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - - while (g_connections_complete == connections_complete_before) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + do + { + addr_len = sizeof (addr); + r = accept (svr_fd, (struct sockaddr *) &addr, &addr_len); + } + while (r == -1 && errno == EINTR); + GPR_ASSERT (r >= 0); + close (r); + + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + + while (g_connections_complete == connections_complete_before) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -void test_fails(void) { +void +test_fails (void) +{ struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + socklen_t addr_len = sizeof (addr); int connections_complete_before; grpc_closure done; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_DEBUG, "test_fails"); + gpr_log (GPR_DEBUG, "test_fails"); - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); connections_complete_before = g_connections_complete; - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); /* connect to a broken address */ - grpc_closure_init(&done, must_fail, NULL); - grpc_tcp_client_connect(&done, &g_connecting, &g_pollset_set, - (struct sockaddr *)&addr, addr_len, - gpr_inf_future(GPR_CLOCK_REALTIME), &closure_list); + grpc_closure_init (&done, must_fail, NULL); + grpc_tcp_client_connect (&done, &g_connecting, &g_pollset_set, (struct sockaddr *) &addr, addr_len, gpr_inf_future (GPR_CLOCK_REALTIME), &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); /* wait for the connection callback to finish */ - while (g_connections_complete == connections_complete_before) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - test_deadline(), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + while (g_connections_complete == connections_complete_before) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), test_deadline (), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -void test_times_out(void) { +void +test_times_out (void) +{ struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + socklen_t addr_len = sizeof (addr); int svr_fd; #define NUM_CLIENT_CONNECTS 100 int client_fd[NUM_CLIENT_CONNECTS]; @@ -182,103 +193,107 @@ void test_times_out(void) { grpc_closure done; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_DEBUG, "test_times_out"); + gpr_log (GPR_DEBUG, "test_times_out"); - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); 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 == listen(svr_fd, 1)); + 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 == listen (svr_fd, 1)); /* Get its address */ - GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0); + GPR_ASSERT (getsockname (svr_fd, (struct sockaddr *) &addr, &addr_len) == 0); /* tie up the listen buffer, which is somewhat arbitrarily sized. */ - for (i = 0; i < NUM_CLIENT_CONNECTS; ++i) { - client_fd[i] = socket(AF_INET, SOCK_STREAM, 0); - grpc_set_socket_nonblocking(client_fd[i], 1); - do { - r = connect(client_fd[i], (struct sockaddr *)&addr, addr_len); - } while (r == -1 && errno == EINTR); - GPR_ASSERT(r < 0); - GPR_ASSERT(errno == EWOULDBLOCK || errno == EINPROGRESS); - } + for (i = 0; i < NUM_CLIENT_CONNECTS; ++i) + { + client_fd[i] = socket (AF_INET, SOCK_STREAM, 0); + grpc_set_socket_nonblocking (client_fd[i], 1); + do + { + r = connect (client_fd[i], (struct sockaddr *) &addr, addr_len); + } + while (r == -1 && errno == EINTR); + GPR_ASSERT (r < 0); + GPR_ASSERT (errno == EWOULDBLOCK || errno == EINPROGRESS); + } /* connect to dummy server address */ - connect_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1); + connect_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); connections_complete_before = g_connections_complete; - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); - grpc_closure_init(&done, must_fail, NULL); - grpc_tcp_client_connect(&done, &g_connecting, &g_pollset_set, - (struct sockaddr *)&addr, addr_len, connect_deadline, - &closure_list); + grpc_closure_init (&done, must_fail, NULL); + grpc_tcp_client_connect (&done, &g_connecting, &g_pollset_set, (struct sockaddr *) &addr, addr_len, connect_deadline, &closure_list); /* Make sure the event doesn't trigger early */ - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - for (;;) { - grpc_pollset_worker worker; - gpr_timespec now = gpr_now(connect_deadline.clock_type); - gpr_timespec continue_verifying_time = - gpr_time_from_seconds(5, GPR_TIMESPAN); - gpr_timespec grace_time = gpr_time_from_seconds(3, GPR_TIMESPAN); - gpr_timespec finish_time = - gpr_time_add(connect_deadline, continue_verifying_time); - gpr_timespec restart_verifying_time = - gpr_time_add(connect_deadline, grace_time); - int is_after_deadline = gpr_time_cmp(now, connect_deadline) > 0; - if (gpr_time_cmp(now, finish_time) > 0) { - break; + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + for (;;) + { + grpc_pollset_worker worker; + gpr_timespec now = gpr_now (connect_deadline.clock_type); + gpr_timespec continue_verifying_time = gpr_time_from_seconds (5, GPR_TIMESPAN); + gpr_timespec grace_time = gpr_time_from_seconds (3, GPR_TIMESPAN); + gpr_timespec finish_time = gpr_time_add (connect_deadline, continue_verifying_time); + gpr_timespec restart_verifying_time = gpr_time_add (connect_deadline, grace_time); + int is_after_deadline = gpr_time_cmp (now, connect_deadline) > 0; + if (gpr_time_cmp (now, finish_time) > 0) + { + break; + } + gpr_log (GPR_DEBUG, "now=%d.%09d connect_deadline=%d.%09d", now.tv_sec, now.tv_nsec, connect_deadline.tv_sec, connect_deadline.tv_nsec); + if (is_after_deadline && gpr_time_cmp (now, restart_verifying_time) <= 0) + { + /* allow some slack before insisting that things be done */ + } + else + { + GPR_ASSERT (g_connections_complete == connections_complete_before + is_after_deadline); + } + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), GRPC_TIMEOUT_MILLIS_TO_DEADLINE (10), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); } - gpr_log(GPR_DEBUG, "now=%d.%09d connect_deadline=%d.%09d", now.tv_sec, - now.tv_nsec, connect_deadline.tv_sec, connect_deadline.tv_nsec); - if (is_after_deadline && gpr_time_cmp(now, restart_verifying_time) <= 0) { - /* allow some slack before insisting that things be done */ - } else { - GPR_ASSERT(g_connections_complete == - connections_complete_before + is_after_deadline); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + + close (svr_fd); + for (i = 0; i < NUM_CLIENT_CONNECTS; ++i) + { + close (client_fd[i]); } - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_MILLIS_TO_DEADLINE(10), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - - close(svr_fd); - for (i = 0; i < NUM_CLIENT_CONNECTS; ++i) { - close(client_fd[i]); - } } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_init(); - grpc_pollset_set_init(&g_pollset_set); - grpc_pollset_init(&g_pollset); - grpc_pollset_set_add_pollset(&g_pollset_set, &g_pollset, &closure_list); - grpc_closure_list_run(&closure_list); - test_succeeds(); - gpr_log(GPR_ERROR, "End of first test"); - test_fails(); - test_times_out(); - grpc_pollset_set_destroy(&g_pollset_set); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_shutdown(); + grpc_test_init (argc, argv); + grpc_init (); + grpc_pollset_set_init (&g_pollset_set); + grpc_pollset_init (&g_pollset); + grpc_pollset_set_add_pollset (&g_pollset_set, &g_pollset, &closure_list); + grpc_closure_list_run (&closure_list); + test_succeeds (); + gpr_log (GPR_ERROR, "End of first test"); + test_fails (); + test_times_out (); + grpc_pollset_set_destroy (&g_pollset_set); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_shutdown (); return 0; } diff --git a/test/core/iomgr/tcp_posix_test.c b/test/core/iomgr/tcp_posix_test.c index 9edfe76e04..5683e7cf53 100644 --- a/test/core/iomgr/tcp_posix_test.c +++ b/test/core/iomgr/tcp_posix_test.c @@ -66,55 +66,70 @@ static grpc_pollset g_pollset; */ -static void create_sockets(int sv[2]) { +static void +create_sockets (int sv[2]) +{ int flags; - GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); - flags = fcntl(sv[0], F_GETFL, 0); - GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0); - flags = fcntl(sv[1], F_GETFL, 0); - GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0); + GPR_ASSERT (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == 0); + flags = fcntl (sv[0], F_GETFL, 0); + GPR_ASSERT (fcntl (sv[0], F_SETFL, flags | O_NONBLOCK) == 0); + flags = fcntl (sv[1], F_GETFL, 0); + GPR_ASSERT (fcntl (sv[1], F_SETFL, flags | O_NONBLOCK) == 0); } -static ssize_t fill_socket(int fd) { +static ssize_t +fill_socket (int fd) +{ ssize_t write_bytes; ssize_t total_bytes = 0; int i; unsigned char buf[256]; - for (i = 0; i < 256; ++i) { - buf[i] = (gpr_uint8)i; - } - do { - write_bytes = write(fd, buf, 256); - if (write_bytes > 0) { - total_bytes += write_bytes; + for (i = 0; i < 256; ++i) + { + buf[i] = (gpr_uint8) i; } - } while (write_bytes >= 0 || errno == EINTR); - GPR_ASSERT(errno == EAGAIN); + do + { + write_bytes = write (fd, buf, 256); + if (write_bytes > 0) + { + total_bytes += write_bytes; + } + } + while (write_bytes >= 0 || errno == EINTR); + GPR_ASSERT (errno == EAGAIN); return total_bytes; } -static size_t fill_socket_partial(int fd, size_t bytes) { +static size_t +fill_socket_partial (int fd, size_t bytes) +{ ssize_t write_bytes; size_t total_bytes = 0; - unsigned char *buf = malloc(bytes); + unsigned char *buf = malloc (bytes); unsigned i; - for (i = 0; i < bytes; ++i) { - buf[i] = (gpr_uint8)(i % 256); - } - - do { - write_bytes = write(fd, buf, bytes - total_bytes); - if (write_bytes > 0) { - total_bytes += (size_t)write_bytes; + for (i = 0; i < bytes; ++i) + { + buf[i] = (gpr_uint8) (i % 256); + } + + do + { + write_bytes = write (fd, buf, bytes - total_bytes); + if (write_bytes > 0) + { + total_bytes += (size_t) write_bytes; + } } - } while ((write_bytes >= 0 || errno == EINTR) && bytes > total_bytes); + while ((write_bytes >= 0 || errno == EINTR) && bytes > total_bytes); - gpr_free(buf); + gpr_free (buf); return total_bytes; } -struct read_socket_state { +struct read_socket_state +{ grpc_endpoint *ep; size_t read_bytes; size_t target_read_bytes; @@ -122,178 +137,191 @@ struct read_socket_state { grpc_closure read_cb; }; -static size_t count_slices(gpr_slice *slices, size_t nslices, - int *current_data) { +static size_t +count_slices (gpr_slice * slices, size_t nslices, int *current_data) +{ size_t num_bytes = 0; unsigned i, j; unsigned char *buf; - for (i = 0; i < nslices; ++i) { - buf = GPR_SLICE_START_PTR(slices[i]); - for (j = 0; j < GPR_SLICE_LENGTH(slices[i]); ++j) { - GPR_ASSERT(buf[j] == *current_data); - *current_data = (*current_data + 1) % 256; + for (i = 0; i < nslices; ++i) + { + buf = GPR_SLICE_START_PTR (slices[i]); + for (j = 0; j < GPR_SLICE_LENGTH (slices[i]); ++j) + { + GPR_ASSERT (buf[j] == *current_data); + *current_data = (*current_data + 1) % 256; + } + num_bytes += GPR_SLICE_LENGTH (slices[i]); } - num_bytes += GPR_SLICE_LENGTH(slices[i]); - } return num_bytes; } -static void read_cb(void *user_data, int success, - grpc_closure_list *closure_list) { - struct read_socket_state *state = (struct read_socket_state *)user_data; +static void +read_cb (void *user_data, int success, grpc_closure_list * closure_list) +{ + struct read_socket_state *state = (struct read_socket_state *) user_data; size_t read_bytes; int current_data; - GPR_ASSERT(success); + GPR_ASSERT (success); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); current_data = state->read_bytes % 256; - read_bytes = count_slices(state->incoming.slices, state->incoming.count, - ¤t_data); + read_bytes = count_slices (state->incoming.slices, state->incoming.count, ¤t_data); state->read_bytes += read_bytes; - gpr_log(GPR_INFO, "Read %d bytes of %d", read_bytes, - state->target_read_bytes); - if (state->read_bytes >= state->target_read_bytes) { - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - } else { - grpc_endpoint_read(state->ep, &state->incoming, &state->read_cb, - closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - } + gpr_log (GPR_INFO, "Read %d bytes of %d", read_bytes, state->target_read_bytes); + if (state->read_bytes >= state->target_read_bytes) + { + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + } + else + { + grpc_endpoint_read (state->ep, &state->incoming, &state->read_cb, closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + } } /* Write to a socket, then read from it using the grpc_tcp API. */ -static void read_test(size_t num_bytes, size_t slice_size) { +static void +read_test (size_t num_bytes, size_t slice_size) +{ int sv[2]; grpc_endpoint *ep; struct read_socket_state state; size_t written_bytes; - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(20); + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (20); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_INFO, "Read test of size %d, slice size %d", num_bytes, - slice_size); + gpr_log (GPR_INFO, "Read test of size %d, slice size %d", num_bytes, slice_size); - create_sockets(sv); + create_sockets (sv); - ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), slice_size, "test"); - grpc_endpoint_add_to_pollset(ep, &g_pollset, &closure_list); + ep = grpc_tcp_create (grpc_fd_create (sv[1], "read_test"), slice_size, "test"); + grpc_endpoint_add_to_pollset (ep, &g_pollset, &closure_list); - written_bytes = fill_socket_partial(sv[0], num_bytes); - gpr_log(GPR_INFO, "Wrote %d bytes", written_bytes); + written_bytes = fill_socket_partial (sv[0], num_bytes); + gpr_log (GPR_INFO, "Wrote %d bytes", written_bytes); state.ep = ep; state.read_bytes = 0; state.target_read_bytes = written_bytes; - gpr_slice_buffer_init(&state.incoming); - grpc_closure_init(&state.read_cb, read_cb, &state); - - grpc_endpoint_read(ep, &state.incoming, &state.read_cb, &closure_list); - - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (state.read_bytes < state.target_read_bytes) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - GPR_ASSERT(state.read_bytes == state.target_read_bytes); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - - gpr_slice_buffer_destroy(&state.incoming); - grpc_endpoint_destroy(ep, &closure_list); - grpc_closure_list_run(&closure_list); + gpr_slice_buffer_init (&state.incoming); + grpc_closure_init (&state.read_cb, read_cb, &state); + + grpc_endpoint_read (ep, &state.incoming, &state.read_cb, &closure_list); + + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (state.read_bytes < state.target_read_bytes) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + GPR_ASSERT (state.read_bytes == state.target_read_bytes); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + + gpr_slice_buffer_destroy (&state.incoming); + grpc_endpoint_destroy (ep, &closure_list); + grpc_closure_list_run (&closure_list); } /* Write to a socket until it fills up, then read from it using the grpc_tcp API. */ -static void large_read_test(size_t slice_size) { +static void +large_read_test (size_t slice_size) +{ int sv[2]; grpc_endpoint *ep; struct read_socket_state state; ssize_t written_bytes; - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(20); + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (20); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_INFO, "Start large read test, slice size %d", slice_size); + gpr_log (GPR_INFO, "Start large read test, slice size %d", slice_size); - create_sockets(sv); + create_sockets (sv); - ep = grpc_tcp_create(grpc_fd_create(sv[1], "large_read_test"), slice_size, - "test"); - grpc_endpoint_add_to_pollset(ep, &g_pollset, &closure_list); + ep = grpc_tcp_create (grpc_fd_create (sv[1], "large_read_test"), slice_size, "test"); + grpc_endpoint_add_to_pollset (ep, &g_pollset, &closure_list); - written_bytes = fill_socket(sv[0]); - gpr_log(GPR_INFO, "Wrote %d bytes", written_bytes); + written_bytes = fill_socket (sv[0]); + gpr_log (GPR_INFO, "Wrote %d bytes", written_bytes); state.ep = ep; state.read_bytes = 0; - state.target_read_bytes = (size_t)written_bytes; - gpr_slice_buffer_init(&state.incoming); - grpc_closure_init(&state.read_cb, read_cb, &state); - - grpc_endpoint_read(ep, &state.incoming, &state.read_cb, &closure_list); - - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - while (state.read_bytes < state.target_read_bytes) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - GPR_ASSERT(state.read_bytes == state.target_read_bytes); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - - gpr_slice_buffer_destroy(&state.incoming); - grpc_endpoint_destroy(ep, &closure_list); - grpc_closure_list_run(&closure_list); + state.target_read_bytes = (size_t) written_bytes; + gpr_slice_buffer_init (&state.incoming); + grpc_closure_init (&state.read_cb, read_cb, &state); + + grpc_endpoint_read (ep, &state.incoming, &state.read_cb, &closure_list); + + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + while (state.read_bytes < state.target_read_bytes) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + GPR_ASSERT (state.read_bytes == state.target_read_bytes); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + + gpr_slice_buffer_destroy (&state.incoming); + grpc_endpoint_destroy (ep, &closure_list); + grpc_closure_list_run (&closure_list); } -struct write_socket_state { +struct write_socket_state +{ grpc_endpoint *ep; int write_done; }; -static gpr_slice *allocate_blocks(size_t num_bytes, size_t slice_size, - size_t *num_blocks, gpr_uint8 *current_data) { +static gpr_slice * +allocate_blocks (size_t num_bytes, size_t slice_size, size_t * num_blocks, gpr_uint8 * current_data) +{ size_t nslices = num_bytes / slice_size + (num_bytes % slice_size ? 1u : 0u); - gpr_slice *slices = gpr_malloc(sizeof(gpr_slice) * nslices); + gpr_slice *slices = gpr_malloc (sizeof (gpr_slice) * nslices); size_t num_bytes_left = num_bytes; unsigned i, j; unsigned char *buf; *num_blocks = nslices; - for (i = 0; i < nslices; ++i) { - slices[i] = gpr_slice_malloc(slice_size > num_bytes_left ? num_bytes_left - : slice_size); - num_bytes_left -= GPR_SLICE_LENGTH(slices[i]); - buf = GPR_SLICE_START_PTR(slices[i]); - for (j = 0; j < GPR_SLICE_LENGTH(slices[i]); ++j) { - buf[j] = *current_data; - (*current_data)++; + for (i = 0; i < nslices; ++i) + { + slices[i] = gpr_slice_malloc (slice_size > num_bytes_left ? num_bytes_left : slice_size); + num_bytes_left -= GPR_SLICE_LENGTH (slices[i]); + buf = GPR_SLICE_START_PTR (slices[i]); + for (j = 0; j < GPR_SLICE_LENGTH (slices[i]); ++j) + { + buf[j] = *current_data; + (*current_data)++; + } } - } - GPR_ASSERT(num_bytes_left == 0); + GPR_ASSERT (num_bytes_left == 0); return slices; } -static void write_done(void *user_data /* write_socket_state */, int success, - grpc_closure_list *closure_list) { - struct write_socket_state *state = (struct write_socket_state *)user_data; - gpr_log(GPR_INFO, "Write done callback called"); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - gpr_log(GPR_INFO, "Signalling write done"); +static void +write_done (void *user_data /* write_socket_state */ , int success, + grpc_closure_list * closure_list) +{ + struct write_socket_state *state = (struct write_socket_state *) user_data; + gpr_log (GPR_INFO, "Write done callback called"); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + gpr_log (GPR_INFO, "Signalling write done"); state->write_done = 1; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -void drain_socket_blocking(int fd, size_t num_bytes, size_t read_size) { - unsigned char *buf = malloc(read_size); +void +drain_socket_blocking (int fd, size_t num_bytes, size_t read_size) +{ + unsigned char *buf = malloc (read_size); ssize_t bytes_read; size_t bytes_left = num_bytes; int flags; @@ -301,38 +329,43 @@ void drain_socket_blocking(int fd, size_t num_bytes, size_t read_size) { int i; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - flags = fcntl(fd, F_GETFL, 0); - GPR_ASSERT(fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == 0); - - for (;;) { - grpc_pollset_worker worker; - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_MILLIS_TO_DEADLINE(10), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - do { - bytes_read = - read(fd, buf, bytes_left > read_size ? read_size : bytes_left); - } while (bytes_read < 0 && errno == EINTR); - GPR_ASSERT(bytes_read >= 0); - for (i = 0; i < bytes_read; ++i) { - GPR_ASSERT(buf[i] == current); - current = (current + 1) % 256; + flags = fcntl (fd, F_GETFL, 0); + GPR_ASSERT (fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) == 0); + + for (;;) + { + grpc_pollset_worker worker; + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), GRPC_TIMEOUT_MILLIS_TO_DEADLINE (10), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + do + { + bytes_read = read (fd, buf, bytes_left > read_size ? read_size : bytes_left); + } + while (bytes_read < 0 && errno == EINTR); + GPR_ASSERT (bytes_read >= 0); + for (i = 0; i < bytes_read; ++i) + { + GPR_ASSERT (buf[i] == current); + current = (current + 1) % 256; + } + bytes_left -= (size_t) bytes_read; + if (bytes_left == 0) + break; } - bytes_left -= (size_t)bytes_read; - if (bytes_left == 0) break; - } - flags = fcntl(fd, F_GETFL, 0); - GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0); + flags = fcntl (fd, F_GETFL, 0); + GPR_ASSERT (fcntl (fd, F_SETFL, flags | O_NONBLOCK) == 0); - gpr_free(buf); + gpr_free (buf); } /* Write to a socket using the grpc_tcp API, then drain it directly. Note that if the write does not complete immediately we need to drain the socket in parallel with the read. */ -static void write_test(size_t num_bytes, size_t slice_size) { +static void +write_test (size_t num_bytes, size_t slice_size) +{ int sv[2]; grpc_endpoint *ep; struct write_socket_state state; @@ -341,112 +374,119 @@ static void write_test(size_t num_bytes, size_t slice_size) { gpr_uint8 current_data = 0; gpr_slice_buffer outgoing; grpc_closure write_done_closure; - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(20); + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (20); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_INFO, "Start write test with %d bytes, slice size %d", num_bytes, - slice_size); + gpr_log (GPR_INFO, "Start write test with %d bytes, slice size %d", num_bytes, slice_size); - create_sockets(sv); + create_sockets (sv); - ep = grpc_tcp_create(grpc_fd_create(sv[1], "write_test"), - GRPC_TCP_DEFAULT_READ_SLICE_SIZE, "test"); - grpc_endpoint_add_to_pollset(ep, &g_pollset, &closure_list); + ep = grpc_tcp_create (grpc_fd_create (sv[1], "write_test"), GRPC_TCP_DEFAULT_READ_SLICE_SIZE, "test"); + grpc_endpoint_add_to_pollset (ep, &g_pollset, &closure_list); state.ep = ep; state.write_done = 0; - slices = allocate_blocks(num_bytes, slice_size, &num_blocks, ¤t_data); - - gpr_slice_buffer_init(&outgoing); - gpr_slice_buffer_addn(&outgoing, slices, num_blocks); - grpc_closure_init(&write_done_closure, write_done, &state); - - grpc_endpoint_write(ep, &outgoing, &write_done_closure, &closure_list); - drain_socket_blocking(sv[0], num_bytes, num_bytes); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - for (;;) { - grpc_pollset_worker worker; - if (state.write_done) { - break; + slices = allocate_blocks (num_bytes, slice_size, &num_blocks, ¤t_data); + + gpr_slice_buffer_init (&outgoing); + gpr_slice_buffer_addn (&outgoing, slices, num_blocks); + grpc_closure_init (&write_done_closure, write_done, &state); + + grpc_endpoint_write (ep, &outgoing, &write_done_closure, &closure_list); + drain_socket_blocking (sv[0], num_bytes, num_bytes); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + for (;;) + { + grpc_pollset_worker worker; + if (state.write_done) + { + break; + } + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); } - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - - gpr_slice_buffer_destroy(&outgoing); - grpc_endpoint_destroy(ep, &closure_list); - gpr_free(slices); - grpc_closure_list_run(&closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + + gpr_slice_buffer_destroy (&outgoing); + grpc_endpoint_destroy (ep, &closure_list); + gpr_free (slices); + grpc_closure_list_run (&closure_list); } -void run_tests(void) { +void +run_tests (void) +{ size_t i = 0; - read_test(100, 8192); - read_test(10000, 8192); - read_test(10000, 137); - read_test(10000, 1); - large_read_test(8192); - large_read_test(1); - - write_test(100, 8192); - write_test(100, 1); - write_test(100000, 8192); - write_test(100000, 1); - write_test(100000, 137); - - for (i = 1; i < 1000; i = GPR_MAX(i + 1, i * 5 / 4)) { - write_test(40320, i); - } + read_test (100, 8192); + read_test (10000, 8192); + read_test (10000, 137); + read_test (10000, 1); + large_read_test (8192); + large_read_test (1); + + write_test (100, 8192); + write_test (100, 1); + write_test (100000, 8192); + write_test (100000, 1); + write_test (100000, 137); + + for (i = 1; i < 1000; i = GPR_MAX (i + 1, i * 5 / 4)) + { + write_test (40320, i); + } } -static void clean_up(void) {} +static void +clean_up (void) +{ +} -static grpc_endpoint_test_fixture create_fixture_tcp_socketpair( - size_t slice_size) { +static grpc_endpoint_test_fixture +create_fixture_tcp_socketpair (size_t slice_size) +{ int sv[2]; grpc_endpoint_test_fixture f; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - create_sockets(sv); - f.client_ep = grpc_tcp_create(grpc_fd_create(sv[0], "fixture:client"), - slice_size, "test"); - f.server_ep = grpc_tcp_create(grpc_fd_create(sv[1], "fixture:server"), - slice_size, "test"); - grpc_endpoint_add_to_pollset(f.client_ep, &g_pollset, &closure_list); - grpc_endpoint_add_to_pollset(f.server_ep, &g_pollset, &closure_list); + create_sockets (sv); + f.client_ep = grpc_tcp_create (grpc_fd_create (sv[0], "fixture:client"), slice_size, "test"); + f.server_ep = grpc_tcp_create (grpc_fd_create (sv[1], "fixture:server"), slice_size, "test"); + grpc_endpoint_add_to_pollset (f.client_ep, &g_pollset, &closure_list); + grpc_endpoint_add_to_pollset (f.server_ep, &g_pollset, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_closure_list_run (&closure_list); return f; } static grpc_endpoint_test_config configs[] = { - {"tcp/tcp_socketpair", create_fixture_tcp_socketpair, clean_up}, + {"tcp/tcp_socketpair", create_fixture_tcp_socketpair, clean_up}, }; -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_init(); - grpc_pollset_init(&g_pollset); - run_tests(); - grpc_endpoint_tests(configs[0], &g_pollset); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_shutdown(); + grpc_test_init (argc, argv); + grpc_init (); + grpc_pollset_init (&g_pollset); + run_tests (); + grpc_endpoint_tests (configs[0], &g_pollset); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_shutdown (); return 0; } diff --git a/test/core/iomgr/tcp_server_posix_test.c b/test/core/iomgr/tcp_server_posix_test.c index 4a3b51efc6..92edfdd850 100644 --- a/test/core/iomgr/tcp_server_posix_test.c +++ b/test/core/iomgr/tcp_server_posix_test.c @@ -48,143 +48,155 @@ static grpc_pollset g_pollset; static int g_nconnects = 0; -static void on_connect(void *arg, grpc_endpoint *tcp, - grpc_closure_list *closure_list) { - grpc_endpoint_shutdown(tcp, closure_list); - grpc_endpoint_destroy(tcp, closure_list); +static void +on_connect (void *arg, grpc_endpoint * tcp, grpc_closure_list * closure_list) +{ + grpc_endpoint_shutdown (tcp, closure_list); + grpc_endpoint_destroy (tcp, closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); g_nconnects++; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -static void test_no_op(void) { - grpc_tcp_server *s = grpc_tcp_server_create(); - grpc_tcp_server_destroy(s, NULL, NULL); +static void +test_no_op (void) +{ + grpc_tcp_server *s = grpc_tcp_server_create (); + grpc_tcp_server_destroy (s, NULL, NULL); } -static void test_no_op_with_start(void) { +static void +test_no_op_with_start (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_tcp_server *s = grpc_tcp_server_create(); - LOG_TEST("test_no_op_with_start"); - grpc_tcp_server_start(s, NULL, 0, on_connect, NULL, &closure_list); - grpc_tcp_server_destroy(s, NULL, NULL); - grpc_closure_list_run(&closure_list); + grpc_tcp_server *s = grpc_tcp_server_create (); + LOG_TEST ("test_no_op_with_start"); + grpc_tcp_server_start (s, NULL, 0, on_connect, NULL, &closure_list); + grpc_tcp_server_destroy (s, NULL, NULL); + grpc_closure_list_run (&closure_list); } -static void test_no_op_with_port(void) { +static void +test_no_op_with_port (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; struct sockaddr_in addr; - grpc_tcp_server *s = grpc_tcp_server_create(); - LOG_TEST("test_no_op_with_port"); + grpc_tcp_server *s = grpc_tcp_server_create (); + LOG_TEST ("test_no_op_with_port"); - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; - GPR_ASSERT( - grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr))); + GPR_ASSERT (grpc_tcp_server_add_port (s, (struct sockaddr *) &addr, sizeof (addr))); - grpc_tcp_server_destroy(s, NULL, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_tcp_server_destroy (s, NULL, &closure_list); + grpc_closure_list_run (&closure_list); } -static void test_no_op_with_port_and_start(void) { +static void +test_no_op_with_port_and_start (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; struct sockaddr_in addr; - grpc_tcp_server *s = grpc_tcp_server_create(); - LOG_TEST("test_no_op_with_port_and_start"); + grpc_tcp_server *s = grpc_tcp_server_create (); + LOG_TEST ("test_no_op_with_port_and_start"); - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; - GPR_ASSERT( - grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr))); + GPR_ASSERT (grpc_tcp_server_add_port (s, (struct sockaddr *) &addr, sizeof (addr))); - grpc_tcp_server_start(s, NULL, 0, on_connect, NULL, &closure_list); + grpc_tcp_server_start (s, NULL, 0, on_connect, NULL, &closure_list); - grpc_tcp_server_destroy(s, NULL, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_tcp_server_destroy (s, NULL, &closure_list); + grpc_closure_list_run (&closure_list); } -static void test_connect(int n) { +static void +test_connect (int n) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); + socklen_t addr_len = sizeof (addr); int svrfd, clifd; - grpc_tcp_server *s = grpc_tcp_server_create(); + grpc_tcp_server *s = grpc_tcp_server_create (); int nconnects_before; gpr_timespec deadline; grpc_pollset *pollsets[1]; int i; - LOG_TEST("test_connect"); - gpr_log(GPR_INFO, "clients=%d", n); + LOG_TEST ("test_connect"); + gpr_log (GPR_INFO, "clients=%d", n); - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); addr.ss_family = AF_INET; - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, addr_len)); + GPR_ASSERT (grpc_tcp_server_add_port (s, (struct sockaddr *) &addr, addr_len)); - svrfd = grpc_tcp_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)); + svrfd = grpc_tcp_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)); pollsets[0] = &g_pollset; - grpc_tcp_server_start(s, pollsets, 1, on_connect, NULL, &closure_list); - - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - - for (i = 0; i < n; i++) { - deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); - - nconnects_before = g_nconnects; - clifd = socket(addr.ss_family, SOCK_STREAM, 0); - GPR_ASSERT(clifd >= 0); - gpr_log(GPR_DEBUG, "start connect"); - GPR_ASSERT(connect(clifd, (struct sockaddr *)&addr, addr_len) == 0); - - gpr_log(GPR_DEBUG, "wait"); - while (g_nconnects == nconnects_before && - gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) > 0) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + grpc_tcp_server_start (s, pollsets, 1, on_connect, NULL, &closure_list); + + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + + for (i = 0; i < n; i++) + { + deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10); + + nconnects_before = g_nconnects; + clifd = socket (addr.ss_family, SOCK_STREAM, 0); + GPR_ASSERT (clifd >= 0); + gpr_log (GPR_DEBUG, "start connect"); + GPR_ASSERT (connect (clifd, (struct sockaddr *) &addr, addr_len) == 0); + + gpr_log (GPR_DEBUG, "wait"); + while (g_nconnects == nconnects_before && gpr_time_cmp (deadline, gpr_now (deadline.clock_type)) > 0) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + gpr_log (GPR_DEBUG, "wait done"); + + GPR_ASSERT (g_nconnects == nconnects_before + 1); + close (clifd); } - gpr_log(GPR_DEBUG, "wait done"); - GPR_ASSERT(g_nconnects == nconnects_before + 1); - close(clifd); - } + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - - grpc_tcp_server_destroy(s, NULL, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_tcp_server_destroy (s, NULL, &closure_list); + grpc_closure_list_run (&closure_list); } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_iomgr_init(); - grpc_pollset_init(&g_pollset); - - test_no_op(); - test_no_op_with_start(); - test_no_op_with_port(); - test_no_op_with_port_and_start(); - test_connect(1); - test_connect(10); - - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_iomgr_shutdown(); + grpc_test_init (argc, argv); + grpc_iomgr_init (); + grpc_pollset_init (&g_pollset); + + test_no_op (); + test_no_op_with_start (); + test_no_op_with_port (); + test_no_op_with_port_and_start (); + test_connect (1); + test_connect (10); + + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_iomgr_shutdown (); return 0; } diff --git a/test/core/iomgr/time_averaged_stats_test.c b/test/core/iomgr/time_averaged_stats_test.c index 4206a1c58f..9bef4e0526 100644 --- a/test/core/iomgr/time_averaged_stats_test.c +++ b/test/core/iomgr/time_averaged_stats_test.c @@ -41,168 +41,186 @@ #define EXPECT_EQ(a, b) GPR_ASSERT((a) == (b)) #define EXPECT_DOUBLE_EQ(a, b) GPR_ASSERT(fabs((a) - (b)) < 1e-9) -static void no_regress_no_persist_test_1(void) { +static void +no_regress_no_persist_test_1 (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0, 0.0); - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight); + grpc_time_averaged_stats_init (&tas, 1000, 0, 0.0); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (0, tas.aggregate_total_weight); /* Should have no effect */ - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (0, tas.aggregate_total_weight); /* Should replace old average */ - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (2000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (1, tas.aggregate_total_weight); } -static void no_regress_no_persist_test_2(void) { +static void +no_regress_no_persist_test_2 (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0, 0.0); - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); + grpc_time_averaged_stats_init (&tas, 1000, 0, 0.0); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); /* Should replace init value */ - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight); - - grpc_time_averaged_stats_add_sample(&tas, 3000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (2000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (1, tas.aggregate_total_weight); + + grpc_time_averaged_stats_add_sample (&tas, 3000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (3000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (1, tas.aggregate_total_weight); } -static void no_regress_no_persist_test_3(void) { +static void +no_regress_no_persist_test_3 (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0, 0.0); - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); + grpc_time_averaged_stats_init (&tas, 1000, 0, 0.0); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); /* Should replace init value */ - grpc_time_averaged_stats_add_sample(&tas, 2500); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(2500, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight); - - grpc_time_averaged_stats_add_sample(&tas, 3500); - grpc_time_averaged_stats_add_sample(&tas, 4500); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(4000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2500); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (2500, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (1, tas.aggregate_total_weight); + + grpc_time_averaged_stats_add_sample (&tas, 3500); + grpc_time_averaged_stats_add_sample (&tas, 4500); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (4000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2, tas.aggregate_total_weight); } -static void some_regress_no_persist_test(void) { +static void +some_regress_no_persist_test (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0.5, 0.0); - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight); - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); + grpc_time_averaged_stats_init (&tas, 1000, 0.5, 0.0); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (0, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); /* (2 * 2000 + 0.5 * 1000) / 2.5 */ - EXPECT_DOUBLE_EQ(1800, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2.5, tas.aggregate_total_weight); + EXPECT_DOUBLE_EQ (1800, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2.5, tas.aggregate_total_weight); } -static void some_decay_test(void) { +static void +some_decay_test (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 1, 0.0); - EXPECT_EQ(1000, tas.aggregate_weighted_avg); + grpc_time_averaged_stats_init (&tas, 1000, 1, 0.0); + EXPECT_EQ (1000, tas.aggregate_weighted_avg); /* Should avg with init value */ - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight); - - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight); - - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (1500, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2, tas.aggregate_total_weight); + + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (1500, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2, tas.aggregate_total_weight); + + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (1500, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2, tas.aggregate_total_weight); } -static void no_regress_full_persist_test(void) { +static void +no_regress_full_persist_test (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0, 1.0); - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight); + grpc_time_averaged_stats_init (&tas, 1000, 0, 1.0); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (0, tas.aggregate_total_weight); /* Should replace init value */ - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_EQ(2000, tas.aggregate_weighted_avg); - EXPECT_EQ(1, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_EQ (2000, tas.aggregate_weighted_avg); + EXPECT_EQ (1, tas.aggregate_total_weight); /* Will result in average of the 3 samples. */ - grpc_time_averaged_stats_add_sample(&tas, 2300); - grpc_time_averaged_stats_add_sample(&tas, 2300); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(2200, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(3, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2300); + grpc_time_averaged_stats_add_sample (&tas, 2300); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (2200, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (3, tas.aggregate_total_weight); } -static void no_regress_some_persist_test(void) { +static void +no_regress_some_persist_test (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0, 0.5); + grpc_time_averaged_stats_init (&tas, 1000, 0, 0.5); /* Should replace init value */ - grpc_time_averaged_stats_add_sample(&tas, 2000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight); - - grpc_time_averaged_stats_add_sample(&tas, 2500); - grpc_time_averaged_stats_add_sample(&tas, 4000); - grpc_time_averaged_stats_update_average(&tas); - EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2.5, tas.aggregate_total_weight); + grpc_time_averaged_stats_add_sample (&tas, 2000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (2000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (1, tas.aggregate_total_weight); + + grpc_time_averaged_stats_add_sample (&tas, 2500); + grpc_time_averaged_stats_add_sample (&tas, 4000); + grpc_time_averaged_stats_update_average (&tas); + EXPECT_DOUBLE_EQ (3000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2.5, tas.aggregate_total_weight); } -static void some_regress_some_persist_test(void) { +static void +some_regress_some_persist_test (void) +{ grpc_time_averaged_stats tas; - grpc_time_averaged_stats_init(&tas, 1000, 0.4, 0.6); + grpc_time_averaged_stats_init (&tas, 1000, 0.4, 0.6); /* Sample weight = 0 */ - EXPECT_EQ(1000, tas.aggregate_weighted_avg); - EXPECT_EQ(0, tas.aggregate_total_weight); + EXPECT_EQ (1000, tas.aggregate_weighted_avg); + EXPECT_EQ (0, tas.aggregate_total_weight); - grpc_time_averaged_stats_update_average(&tas); + grpc_time_averaged_stats_update_average (&tas); /* (0.6 * 0 * 1000 + 0.4 * 1000 / 0.4) */ - EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(0.4, tas.aggregate_total_weight); + EXPECT_DOUBLE_EQ (1000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (0.4, tas.aggregate_total_weight); - grpc_time_averaged_stats_add_sample(&tas, 2640); - grpc_time_averaged_stats_update_average(&tas); + grpc_time_averaged_stats_add_sample (&tas, 2640); + grpc_time_averaged_stats_update_average (&tas); /* (1 * 2640 + 0.6 * 0.4 * 1000 + 0.4 * 1000 / (1 + 0.6 * 0.4 + 0.4) */ - EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(1.64, tas.aggregate_total_weight); + EXPECT_DOUBLE_EQ (2000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (1.64, tas.aggregate_total_weight); - grpc_time_averaged_stats_add_sample(&tas, 2876.8); - grpc_time_averaged_stats_update_average(&tas); + grpc_time_averaged_stats_add_sample (&tas, 2876.8); + grpc_time_averaged_stats_update_average (&tas); /* (1 * 2876.8 + 0.6 * 1.64 * 2000 + 0.4 * 1000 / (1 + 0.6 * 1.64 + 0.4) */ - EXPECT_DOUBLE_EQ(2200, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2.384, tas.aggregate_total_weight); + EXPECT_DOUBLE_EQ (2200, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2.384, tas.aggregate_total_weight); - grpc_time_averaged_stats_add_sample(&tas, 4944.32); - grpc_time_averaged_stats_update_average(&tas); + grpc_time_averaged_stats_add_sample (&tas, 4944.32); + grpc_time_averaged_stats_update_average (&tas); /* (1 * 4944.32 + 0.6 * 2.384 * 2200 + 0.4 * 1000) / - (1 + 0.6 * 2.384 + 0.4) */ - EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg); - EXPECT_DOUBLE_EQ(2.8304, tas.aggregate_total_weight); + (1 + 0.6 * 2.384 + 0.4) */ + EXPECT_DOUBLE_EQ (3000, tas.aggregate_weighted_avg); + EXPECT_DOUBLE_EQ (2.8304, tas.aggregate_total_weight); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - no_regress_no_persist_test_1(); - no_regress_no_persist_test_2(); - no_regress_no_persist_test_3(); - some_regress_no_persist_test(); - some_decay_test(); - no_regress_full_persist_test(); - no_regress_some_persist_test(); - some_regress_some_persist_test(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + no_regress_no_persist_test_1 (); + no_regress_no_persist_test_2 (); + no_regress_no_persist_test_3 (); + some_regress_no_persist_test (); + some_decay_test (); + no_regress_full_persist_test (); + no_regress_some_persist_test (); + some_regress_some_persist_test (); return 0; } diff --git a/test/core/iomgr/udp_server_test.c b/test/core/iomgr/udp_server_test.c index 067838777a..b9cf289ed6 100644 --- a/test/core/iomgr/udp_server_test.c +++ b/test/core/iomgr/udp_server_test.c @@ -49,144 +49,156 @@ static grpc_pollset g_pollset; static int g_number_of_reads = 0; static int g_number_of_bytes_read = 0; -static void on_read(int fd) { +static void +on_read (int fd) +{ char read_buffer[512]; ssize_t byte_count; - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - byte_count = recv(fd, read_buffer, sizeof(read_buffer), 0); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + byte_count = recv (fd, read_buffer, sizeof (read_buffer), 0); g_number_of_reads++; - g_number_of_bytes_read += (int)byte_count; + g_number_of_bytes_read += (int) byte_count; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -static void test_no_op(void) { - grpc_udp_server *s = grpc_udp_server_create(); - grpc_udp_server_destroy(s, NULL, NULL); +static void +test_no_op (void) +{ + grpc_udp_server *s = grpc_udp_server_create (); + grpc_udp_server_destroy (s, NULL, NULL); } -static void test_no_op_with_start(void) { +static void +test_no_op_with_start (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_udp_server *s = grpc_udp_server_create(); - LOG_TEST("test_no_op_with_start"); - grpc_udp_server_start(s, NULL, 0, &closure_list); - grpc_udp_server_destroy(s, NULL, NULL); - grpc_closure_list_run(&closure_list); + grpc_udp_server *s = grpc_udp_server_create (); + LOG_TEST ("test_no_op_with_start"); + grpc_udp_server_start (s, NULL, 0, &closure_list); + grpc_udp_server_destroy (s, NULL, NULL); + grpc_closure_list_run (&closure_list); } -static void test_no_op_with_port(void) { +static void +test_no_op_with_port (void) +{ struct sockaddr_in addr; - grpc_udp_server *s = grpc_udp_server_create(); - LOG_TEST("test_no_op_with_port"); + grpc_udp_server *s = grpc_udp_server_create (); + LOG_TEST ("test_no_op_with_port"); - memset(&addr, 0, sizeof(addr)); + 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)); + GPR_ASSERT (grpc_udp_server_add_port (s, (struct sockaddr *) &addr, sizeof (addr), on_read)); - grpc_udp_server_destroy(s, NULL, NULL); + grpc_udp_server_destroy (s, NULL, NULL); } -static void test_no_op_with_port_and_start(void) { +static void +test_no_op_with_port_and_start (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; struct sockaddr_in addr; - grpc_udp_server *s = grpc_udp_server_create(); - LOG_TEST("test_no_op_with_port_and_start"); + grpc_udp_server *s = grpc_udp_server_create (); + LOG_TEST ("test_no_op_with_port_and_start"); - memset(&addr, 0, sizeof(addr)); + 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)); + GPR_ASSERT (grpc_udp_server_add_port (s, (struct sockaddr *) &addr, sizeof (addr), on_read)); - grpc_udp_server_start(s, NULL, 0, &closure_list); + grpc_udp_server_start (s, NULL, 0, &closure_list); - grpc_udp_server_destroy(s, NULL, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_udp_server_destroy (s, NULL, &closure_list); + grpc_closure_list_run (&closure_list); } -static void test_receive(int number_of_clients) { +static void +test_receive (int number_of_clients) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); + socklen_t addr_len = sizeof (addr); int clifd, svrfd; - grpc_udp_server *s = grpc_udp_server_create(); + grpc_udp_server *s = grpc_udp_server_create (); int i; int number_of_reads_before; gpr_timespec deadline; grpc_pollset *pollsets[1]; - LOG_TEST("test_receive"); - gpr_log(GPR_INFO, "clients=%d", number_of_clients); + LOG_TEST ("test_receive"); + gpr_log (GPR_INFO, "clients=%d", number_of_clients); g_number_of_bytes_read = 0; - memset(&addr, 0, sizeof(addr)); + 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)); + GPR_ASSERT (grpc_udp_server_add_port (s, (struct sockaddr *) &addr, addr_len, on_read)); - 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)); + 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)); pollsets[0] = &g_pollset; - grpc_udp_server_start(s, pollsets, 1, &closure_list); - - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - - for (i = 0; i < number_of_clients; i++) { - deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); - - 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); - GPR_ASSERT(clifd >= 0); - GPR_ASSERT(connect(clifd, (struct sockaddr *)&addr, 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) { - grpc_pollset_worker worker; - grpc_pollset_work(&g_pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); + grpc_udp_server_start (s, pollsets, 1, &closure_list); + + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + + for (i = 0; i < number_of_clients; i++) + { + deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10); + + 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); + GPR_ASSERT (clifd >= 0); + GPR_ASSERT (connect (clifd, (struct sockaddr *) &addr, 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) + { + grpc_pollset_worker worker; + grpc_pollset_work (&g_pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + } + GPR_ASSERT (g_number_of_reads == number_of_reads_before + 1); + close (clifd); } - GPR_ASSERT(g_number_of_reads == number_of_reads_before + 1); - close(clifd); - } - GPR_ASSERT(g_number_of_bytes_read == 5 * number_of_clients); + GPR_ASSERT (g_number_of_bytes_read == 5 * number_of_clients); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); - grpc_udp_server_destroy(s, NULL, NULL); + grpc_udp_server_destroy (s, NULL, NULL); } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_iomgr_init(); - grpc_pollset_init(&g_pollset); - - test_no_op(); - test_no_op_with_start(); - test_no_op_with_port(); - test_no_op_with_port_and_start(); - test_receive(1); - test_receive(10); - - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_iomgr_shutdown(); + grpc_test_init (argc, argv); + grpc_iomgr_init (); + grpc_pollset_init (&g_pollset); + + test_no_op (); + test_no_op_with_start (); + test_no_op_with_port (); + test_no_op_with_port_and_start (); + test_receive (1); + test_receive (10); + + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_iomgr_shutdown (); return 0; } diff --git a/test/core/iomgr/workqueue_test.c b/test/core/iomgr/workqueue_test.c index e4f172d32e..43eebc3fbf 100644 --- a/test/core/iomgr/workqueue_test.c +++ b/test/core/iomgr/workqueue_test.c @@ -40,56 +40,61 @@ static grpc_pollset g_pollset; -static void must_succeed(void *p, int success, - grpc_closure_list *closure_list) { - GPR_ASSERT(success == 1); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - *(int *)p = 1; - grpc_pollset_kick(&g_pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); +static void +must_succeed (void *p, int success, grpc_closure_list * closure_list) +{ + GPR_ASSERT (success == 1); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + *(int *) p = 1; + grpc_pollset_kick (&g_pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); } -static void test_add_closure(void) { +static void +test_add_closure (void) +{ grpc_closure c; int done = 0; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_workqueue *wq = grpc_workqueue_create(&closure_list); - gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5); + grpc_workqueue *wq = grpc_workqueue_create (&closure_list); + gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5); grpc_pollset_worker worker; - grpc_closure_init(&c, must_succeed, &done); + grpc_closure_init (&c, must_succeed, &done); - grpc_workqueue_push(wq, &c, 1); - grpc_workqueue_add_to_pollset(wq, &g_pollset, &closure_list); + grpc_workqueue_push (wq, &c, 1); + grpc_workqueue_add_to_pollset (wq, &g_pollset, &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); - GPR_ASSERT(!done); - grpc_pollset_work(&g_pollset, &worker, gpr_now(deadline.clock_type), deadline, - &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); - grpc_closure_list_run(&closure_list); - GPR_ASSERT(done); + gpr_mu_lock (GRPC_POLLSET_MU (&g_pollset)); + GPR_ASSERT (!done); + grpc_pollset_work (&g_pollset, &worker, gpr_now (deadline.clock_type), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&g_pollset)); + grpc_closure_list_run (&closure_list); + GPR_ASSERT (done); - GRPC_WORKQUEUE_UNREF(wq, "destroy", &closure_list); - grpc_closure_list_run(&closure_list); + GRPC_WORKQUEUE_UNREF (wq, "destroy", &closure_list); + grpc_closure_list_run (&closure_list); } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - grpc_init(); - grpc_pollset_init(&g_pollset); + grpc_test_init (argc, argv); + grpc_init (); + grpc_pollset_init (&g_pollset); - test_add_closure(); + test_add_closure (); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_shutdown(); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_shutdown (); return 0; } diff --git a/test/core/json/json_rewrite.c b/test/core/json/json_rewrite.c index 82cc4090f3..920c03164a 100644 --- a/test/core/json/json_rewrite.c +++ b/test/core/json/json_rewrite.c @@ -41,173 +41,221 @@ #include "src/core/json/json_reader.h" #include "src/core/json/json_writer.h" -typedef struct json_writer_userdata { FILE* out; } json_writer_userdata; +typedef struct json_writer_userdata +{ + FILE *out; +} json_writer_userdata; -typedef struct stacked_container { +typedef struct stacked_container +{ grpc_json_type type; - struct stacked_container* next; + struct stacked_container *next; } stacked_container; -typedef struct json_reader_userdata { - FILE* in; - grpc_json_writer* writer; - char* scratchpad; - char* ptr; +typedef struct json_reader_userdata +{ + FILE *in; + grpc_json_writer *writer; + char *scratchpad; + char *ptr; size_t free_space; size_t allocated; size_t string_len; - stacked_container* top; + stacked_container *top; } json_reader_userdata; -static void json_writer_output_char(void* userdata, char c) { - json_writer_userdata* state = userdata; - fputc(c, state->out); +static void +json_writer_output_char (void *userdata, char c) +{ + json_writer_userdata *state = userdata; + fputc (c, state->out); } -static void json_writer_output_string(void* userdata, const char* str) { - json_writer_userdata* state = userdata; - fputs(str, state->out); +static void +json_writer_output_string (void *userdata, const char *str) +{ + json_writer_userdata *state = userdata; + fputs (str, state->out); } -static void json_writer_output_string_with_len(void* userdata, const char* str, - size_t len) { - json_writer_userdata* state = userdata; - fwrite(str, len, 1, state->out); +static void +json_writer_output_string_with_len (void *userdata, const char *str, size_t len) +{ + json_writer_userdata *state = userdata; + fwrite (str, len, 1, state->out); } -grpc_json_writer_vtable writer_vtable = {json_writer_output_char, - json_writer_output_string, - json_writer_output_string_with_len}; +grpc_json_writer_vtable writer_vtable = { json_writer_output_char, + json_writer_output_string, + json_writer_output_string_with_len +}; -static void check_string(json_reader_userdata* state, size_t needed) { - if (state->free_space >= needed) return; +static void +check_string (json_reader_userdata * state, size_t needed) +{ + if (state->free_space >= needed) + return; needed -= state->free_space; needed = (needed + 0xffu) & ~0xffu; - state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); + state->scratchpad = gpr_realloc (state->scratchpad, state->allocated + needed); state->free_space += needed; state->allocated += needed; } -static void json_reader_string_clear(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_string_clear (void *userdata) +{ + json_reader_userdata *state = userdata; state->free_space = state->allocated; state->string_len = 0; } -static void json_reader_string_add_char(void* userdata, gpr_uint32 c) { - json_reader_userdata* state = userdata; - check_string(state, 1); - GPR_ASSERT(c < 256); - state->scratchpad[state->string_len++] = (char)c; -} - -static void json_reader_string_add_utf32(void* userdata, gpr_uint32 c) { - if (c <= 0x7f) { - json_reader_string_add_char(userdata, c); - } else if (c <= 0x7ff) { - gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); - gpr_uint32 b2 = 0x80u | (c & 0x3fu); - json_reader_string_add_char(userdata, b1); - json_reader_string_add_char(userdata, b2); - } else if (c <= 0xffffu) { - gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); - gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b3 = 0x80u | (c & 0x3fu); - json_reader_string_add_char(userdata, b1); - json_reader_string_add_char(userdata, b2); - json_reader_string_add_char(userdata, b3); - } else if (c <= 0x1fffffu) { - gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); - gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); - gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b4 = 0x80u | (c & 0x3fu); - json_reader_string_add_char(userdata, b1); - json_reader_string_add_char(userdata, b2); - json_reader_string_add_char(userdata, b3); - json_reader_string_add_char(userdata, b4); - } -} - -static gpr_uint32 json_reader_read_char(void* userdata) { +static void +json_reader_string_add_char (void *userdata, gpr_uint32 c) +{ + json_reader_userdata *state = userdata; + check_string (state, 1); + GPR_ASSERT (c < 256); + state->scratchpad[state->string_len++] = (char) c; +} + +static void +json_reader_string_add_utf32 (void *userdata, gpr_uint32 c) +{ + if (c <= 0x7f) + { + json_reader_string_add_char (userdata, c); + } + else if (c <= 0x7ff) + { + gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); + gpr_uint32 b2 = 0x80u | (c & 0x3fu); + json_reader_string_add_char (userdata, b1); + json_reader_string_add_char (userdata, b2); + } + else if (c <= 0xffffu) + { + gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); + gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b3 = 0x80u | (c & 0x3fu); + json_reader_string_add_char (userdata, b1); + json_reader_string_add_char (userdata, b2); + json_reader_string_add_char (userdata, b3); + } + else if (c <= 0x1fffffu) + { + gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); + gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); + gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b4 = 0x80u | (c & 0x3fu); + json_reader_string_add_char (userdata, b1); + json_reader_string_add_char (userdata, b2); + json_reader_string_add_char (userdata, b3); + json_reader_string_add_char (userdata, b4); + } +} + +static gpr_uint32 +json_reader_read_char (void *userdata) +{ int r; - json_reader_userdata* state = userdata; + json_reader_userdata *state = userdata; - r = fgetc(state->in); - if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; - return (gpr_uint32)r; + r = fgetc (state->in); + if (r == EOF) + r = GRPC_JSON_READ_CHAR_EOF; + return (gpr_uint32) r; } -static void json_reader_container_begins(void* userdata, grpc_json_type type) { - json_reader_userdata* state = userdata; - stacked_container* container = gpr_malloc(sizeof(stacked_container)); +static void +json_reader_container_begins (void *userdata, grpc_json_type type) +{ + json_reader_userdata *state = userdata; + stacked_container *container = gpr_malloc (sizeof (stacked_container)); container->type = type; container->next = state->top; state->top = container; - grpc_json_writer_container_begins(state->writer, type); + grpc_json_writer_container_begins (state->writer, type); } -static grpc_json_type json_reader_container_ends(void* userdata) { - json_reader_userdata* state = userdata; - stacked_container* container = state->top; +static grpc_json_type +json_reader_container_ends (void *userdata) +{ + json_reader_userdata *state = userdata; + stacked_container *container = state->top; - grpc_json_writer_container_ends(state->writer, container->type); + grpc_json_writer_container_ends (state->writer, container->type); state->top = container->next; - gpr_free(container); + gpr_free (container); return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; } -static void json_reader_set_key(void* userdata) { - json_reader_userdata* state = userdata; - json_reader_string_add_char(userdata, 0); +static void +json_reader_set_key (void *userdata) +{ + json_reader_userdata *state = userdata; + json_reader_string_add_char (userdata, 0); - grpc_json_writer_object_key(state->writer, state->scratchpad); + grpc_json_writer_object_key (state->writer, state->scratchpad); } -static void json_reader_set_string(void* userdata) { - json_reader_userdata* state = userdata; - json_reader_string_add_char(userdata, 0); +static void +json_reader_set_string (void *userdata) +{ + json_reader_userdata *state = userdata; + json_reader_string_add_char (userdata, 0); - grpc_json_writer_value_string(state->writer, state->scratchpad); + grpc_json_writer_value_string (state->writer, state->scratchpad); } -static int json_reader_set_number(void* userdata) { - json_reader_userdata* state = userdata; +static int +json_reader_set_number (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, - state->string_len); + grpc_json_writer_value_raw_with_len (state->writer, state->scratchpad, state->string_len); return 1; } -static void json_reader_set_true(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_set_true (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, "true", 4); + grpc_json_writer_value_raw_with_len (state->writer, "true", 4); } -static void json_reader_set_false(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_set_false (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, "false", 5); + grpc_json_writer_value_raw_with_len (state->writer, "false", 5); } -static void json_reader_set_null(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_set_null (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, "null", 4); + grpc_json_writer_value_raw_with_len (state->writer, "null", 4); } static grpc_json_reader_vtable reader_vtable = { - json_reader_string_clear, json_reader_string_add_char, - json_reader_string_add_utf32, json_reader_read_char, - json_reader_container_begins, json_reader_container_ends, - json_reader_set_key, json_reader_set_string, - json_reader_set_number, json_reader_set_true, - json_reader_set_false, json_reader_set_null}; - -int rewrite(FILE* in, FILE* out, int indent) { + json_reader_string_clear, json_reader_string_add_char, + json_reader_string_add_utf32, json_reader_read_char, + json_reader_container_begins, json_reader_container_ends, + json_reader_set_key, json_reader_set_string, + json_reader_set_number, json_reader_set_true, + json_reader_set_false, json_reader_set_null +}; + +int +rewrite (FILE * in, FILE * out, int indent) +{ grpc_json_writer writer; grpc_json_reader reader; grpc_json_reader_status status; @@ -224,29 +272,32 @@ int rewrite(FILE* in, FILE* out, int indent) { writer_user.out = out; - grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); - grpc_json_reader_init(&reader, &reader_vtable, &reader_user); + grpc_json_writer_init (&writer, indent, &writer_vtable, &writer_user); + grpc_json_reader_init (&reader, &reader_vtable, &reader_user); - status = grpc_json_reader_run(&reader); + status = grpc_json_reader_run (&reader); - free(reader_user.scratchpad); - while (reader_user.top) { - stacked_container* container = reader_user.top; - reader_user.top = container->next; - free(container); - } + free (reader_user.scratchpad); + while (reader_user.top) + { + stacked_container *container = reader_user.top; + reader_user.top = container->next; + free (container); + } return status == GRPC_JSON_DONE; } -int main(int argc, char** argv) { +int +main (int argc, char **argv) +{ int indent = 2; - gpr_cmdline* cl; + gpr_cmdline *cl; - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_int(cl, "indent", NULL, &indent); - gpr_cmdline_parse(cl, argc, argv); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_int (cl, "indent", NULL, &indent); + gpr_cmdline_parse (cl, argc, argv); + gpr_cmdline_destroy (cl); - return rewrite(stdin, stdout, indent) ? 0 : 1; + return rewrite (stdin, stdout, indent) ? 0 : 1; } diff --git a/test/core/json/json_rewrite_test.c b/test/core/json/json_rewrite_test.c index d26ef53b2d..470c0218c5 100644 --- a/test/core/json/json_rewrite_test.c +++ b/test/core/json/json_rewrite_test.c @@ -42,198 +42,250 @@ #include "src/core/json/json_reader.h" #include "src/core/json/json_writer.h" -typedef struct json_writer_userdata { FILE* cmp; } json_writer_userdata; +typedef struct json_writer_userdata +{ + FILE *cmp; +} json_writer_userdata; -typedef struct stacked_container { +typedef struct stacked_container +{ grpc_json_type type; - struct stacked_container* next; + struct stacked_container *next; } stacked_container; -typedef struct json_reader_userdata { - FILE* in; - grpc_json_writer* writer; - char* scratchpad; - char* ptr; +typedef struct json_reader_userdata +{ + FILE *in; + grpc_json_writer *writer; + char *scratchpad; + char *ptr; size_t free_space; size_t allocated; size_t string_len; - stacked_container* top; + stacked_container *top; int did_eagain; } json_reader_userdata; -static void json_writer_output_char(void* userdata, char c) { - json_writer_userdata* state = userdata; - int cmp = fgetc(state->cmp); +static void +json_writer_output_char (void *userdata, char c) +{ + json_writer_userdata *state = userdata; + int cmp = fgetc (state->cmp); /* treat CRLF as LF */ - if (cmp == '\r' && c == '\n') { - cmp = fgetc(state->cmp); - } - GPR_ASSERT(cmp == c); + if (cmp == '\r' && c == '\n') + { + cmp = fgetc (state->cmp); + } + GPR_ASSERT (cmp == c); } -static void json_writer_output_string(void* userdata, const char* str) { - while (*str) { - json_writer_output_char(userdata, *str++); - } +static void +json_writer_output_string (void *userdata, const char *str) +{ + while (*str) + { + json_writer_output_char (userdata, *str++); + } } -static void json_writer_output_string_with_len(void* userdata, const char* str, - size_t len) { +static void +json_writer_output_string_with_len (void *userdata, const char *str, size_t len) +{ size_t i; - for (i = 0; i < len; i++) { - json_writer_output_char(userdata, str[i]); - } + for (i = 0; i < len; i++) + { + json_writer_output_char (userdata, str[i]); + } } -grpc_json_writer_vtable writer_vtable = {json_writer_output_char, - json_writer_output_string, - json_writer_output_string_with_len}; +grpc_json_writer_vtable writer_vtable = { json_writer_output_char, + json_writer_output_string, + json_writer_output_string_with_len +}; -static void check_string(json_reader_userdata* state, size_t needed) { - if (state->free_space >= needed) return; +static void +check_string (json_reader_userdata * state, size_t needed) +{ + if (state->free_space >= needed) + return; needed -= state->free_space; needed = (needed + 0xffu) & ~0xffu; - state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); + state->scratchpad = gpr_realloc (state->scratchpad, state->allocated + needed); state->free_space += needed; state->allocated += needed; } -static void json_reader_string_clear(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_string_clear (void *userdata) +{ + json_reader_userdata *state = userdata; state->free_space = state->allocated; state->string_len = 0; } -static void json_reader_string_add_char(void* userdata, gpr_uint32 c) { - json_reader_userdata* state = userdata; - check_string(state, 1); - GPR_ASSERT(c <= 256); - state->scratchpad[state->string_len++] = (char)c; +static void +json_reader_string_add_char (void *userdata, gpr_uint32 c) +{ + json_reader_userdata *state = userdata; + check_string (state, 1); + GPR_ASSERT (c <= 256); + state->scratchpad[state->string_len++] = (char) c; } -static void json_reader_string_add_utf32(void* userdata, gpr_uint32 c) { - if (c <= 0x7f) { - json_reader_string_add_char(userdata, c); - } else if (c <= 0x7ffu) { - gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); - gpr_uint32 b2 = 0x80u | (c & 0x3fu); - json_reader_string_add_char(userdata, b1); - json_reader_string_add_char(userdata, b2); - } else if (c <= 0xffffu) { - gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); - gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b3 = 0x80u | (c & 0x3fu); - json_reader_string_add_char(userdata, b1); - json_reader_string_add_char(userdata, b2); - json_reader_string_add_char(userdata, b3); - } else if (c <= 0x1fffffu) { - gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); - gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); - gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); - gpr_uint32 b4 = 0x80u | (c & 0x3fu); - json_reader_string_add_char(userdata, b1); - json_reader_string_add_char(userdata, b2); - json_reader_string_add_char(userdata, b3); - json_reader_string_add_char(userdata, b4); - } +static void +json_reader_string_add_utf32 (void *userdata, gpr_uint32 c) +{ + if (c <= 0x7f) + { + json_reader_string_add_char (userdata, c); + } + else if (c <= 0x7ffu) + { + gpr_uint32 b1 = 0xc0u | ((c >> 6u) & 0x1fu); + gpr_uint32 b2 = 0x80u | (c & 0x3fu); + json_reader_string_add_char (userdata, b1); + json_reader_string_add_char (userdata, b2); + } + else if (c <= 0xffffu) + { + gpr_uint32 b1 = 0xe0u | ((c >> 12u) & 0x0fu); + gpr_uint32 b2 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b3 = 0x80u | (c & 0x3fu); + json_reader_string_add_char (userdata, b1); + json_reader_string_add_char (userdata, b2); + json_reader_string_add_char (userdata, b3); + } + else if (c <= 0x1fffffu) + { + gpr_uint32 b1 = 0xf0u | ((c >> 18u) & 0x07u); + gpr_uint32 b2 = 0x80u | ((c >> 12u) & 0x3fu); + gpr_uint32 b3 = 0x80u | ((c >> 6u) & 0x3fu); + gpr_uint32 b4 = 0x80u | (c & 0x3fu); + json_reader_string_add_char (userdata, b1); + json_reader_string_add_char (userdata, b2); + json_reader_string_add_char (userdata, b3); + json_reader_string_add_char (userdata, b4); + } } -static gpr_uint32 json_reader_read_char(void* userdata) { +static gpr_uint32 +json_reader_read_char (void *userdata) +{ int r; - json_reader_userdata* state = userdata; + json_reader_userdata *state = userdata; - if (!state->did_eagain) { - state->did_eagain = 1; - return GRPC_JSON_READ_CHAR_EAGAIN; - } + if (!state->did_eagain) + { + state->did_eagain = 1; + return GRPC_JSON_READ_CHAR_EAGAIN; + } state->did_eagain = 0; - r = fgetc(state->in); - if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; - return (gpr_uint32)r; + r = fgetc (state->in); + if (r == EOF) + r = GRPC_JSON_READ_CHAR_EOF; + return (gpr_uint32) r; } -static void json_reader_container_begins(void* userdata, grpc_json_type type) { - json_reader_userdata* state = userdata; - stacked_container* container = gpr_malloc(sizeof(stacked_container)); +static void +json_reader_container_begins (void *userdata, grpc_json_type type) +{ + json_reader_userdata *state = userdata; + stacked_container *container = gpr_malloc (sizeof (stacked_container)); container->type = type; container->next = state->top; state->top = container; - grpc_json_writer_container_begins(state->writer, type); + grpc_json_writer_container_begins (state->writer, type); } -static grpc_json_type json_reader_container_ends(void* userdata) { - json_reader_userdata* state = userdata; - stacked_container* container = state->top; +static grpc_json_type +json_reader_container_ends (void *userdata) +{ + json_reader_userdata *state = userdata; + stacked_container *container = state->top; - grpc_json_writer_container_ends(state->writer, container->type); + grpc_json_writer_container_ends (state->writer, container->type); state->top = container->next; - gpr_free(container); + gpr_free (container); return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; } -static void json_reader_set_key(void* userdata) { - json_reader_userdata* state = userdata; - json_reader_string_add_char(userdata, 0); +static void +json_reader_set_key (void *userdata) +{ + json_reader_userdata *state = userdata; + json_reader_string_add_char (userdata, 0); - grpc_json_writer_object_key(state->writer, state->scratchpad); + grpc_json_writer_object_key (state->writer, state->scratchpad); } -static void json_reader_set_string(void* userdata) { - json_reader_userdata* state = userdata; - json_reader_string_add_char(userdata, 0); +static void +json_reader_set_string (void *userdata) +{ + json_reader_userdata *state = userdata; + json_reader_string_add_char (userdata, 0); - grpc_json_writer_value_string(state->writer, state->scratchpad); + grpc_json_writer_value_string (state->writer, state->scratchpad); } -static int json_reader_set_number(void* userdata) { - json_reader_userdata* state = userdata; +static int +json_reader_set_number (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, - state->string_len); + grpc_json_writer_value_raw_with_len (state->writer, state->scratchpad, state->string_len); return 1; } -static void json_reader_set_true(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_set_true (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, "true", 4); + grpc_json_writer_value_raw_with_len (state->writer, "true", 4); } -static void json_reader_set_false(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_set_false (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, "false", 5); + grpc_json_writer_value_raw_with_len (state->writer, "false", 5); } -static void json_reader_set_null(void* userdata) { - json_reader_userdata* state = userdata; +static void +json_reader_set_null (void *userdata) +{ + json_reader_userdata *state = userdata; - grpc_json_writer_value_raw_with_len(state->writer, "null", 4); + grpc_json_writer_value_raw_with_len (state->writer, "null", 4); } static grpc_json_reader_vtable reader_vtable = { - json_reader_string_clear, json_reader_string_add_char, - json_reader_string_add_utf32, json_reader_read_char, - json_reader_container_begins, json_reader_container_ends, - json_reader_set_key, json_reader_set_string, - json_reader_set_number, json_reader_set_true, - json_reader_set_false, json_reader_set_null}; - -int rewrite_and_compare(FILE* in, FILE* cmp, int indent) { + json_reader_string_clear, json_reader_string_add_char, + json_reader_string_add_utf32, json_reader_read_char, + json_reader_container_begins, json_reader_container_ends, + json_reader_set_key, json_reader_set_string, + json_reader_set_number, json_reader_set_true, + json_reader_set_false, json_reader_set_null +}; + +int +rewrite_and_compare (FILE * in, FILE * cmp, int indent) +{ grpc_json_writer writer; grpc_json_reader reader; grpc_json_reader_status status; json_writer_userdata writer_user; json_reader_userdata reader_user; - GPR_ASSERT(in); - GPR_ASSERT(cmp); + GPR_ASSERT (in); + GPR_ASSERT (cmp); reader_user.writer = &writer; reader_user.in = in; @@ -246,60 +298,68 @@ int rewrite_and_compare(FILE* in, FILE* cmp, int indent) { writer_user.cmp = cmp; - grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); - grpc_json_reader_init(&reader, &reader_vtable, &reader_user); + grpc_json_writer_init (&writer, indent, &writer_vtable, &writer_user); + grpc_json_reader_init (&reader, &reader_vtable, &reader_user); - do { - status = grpc_json_reader_run(&reader); - } while (status == GRPC_JSON_EAGAIN); + do + { + status = grpc_json_reader_run (&reader); + } + while (status == GRPC_JSON_EAGAIN); - free(reader_user.scratchpad); - while (reader_user.top) { - stacked_container* container = reader_user.top; - reader_user.top = container->next; - free(container); - } + free (reader_user.scratchpad); + while (reader_user.top) + { + stacked_container *container = reader_user.top; + reader_user.top = container->next; + free (container); + } return status == GRPC_JSON_DONE; } -typedef struct test_file { - const char* input; - const char* cmp; +typedef struct test_file +{ + const char *input; + const char *cmp; int indent; } test_file; static test_file test_files[] = { - {"test/core/json/rewrite_test_input.json", - "test/core/json/rewrite_test_output_condensed.json", 0}, - {"test/core/json/rewrite_test_input.json", - "test/core/json/rewrite_test_output_indented.json", 2}, - {"test/core/json/rewrite_test_output_indented.json", - "test/core/json/rewrite_test_output_condensed.json", 0}, - {"test/core/json/rewrite_test_output_condensed.json", - "test/core/json/rewrite_test_output_indented.json", 2}, + {"test/core/json/rewrite_test_input.json", + "test/core/json/rewrite_test_output_condensed.json", 0}, + {"test/core/json/rewrite_test_input.json", + "test/core/json/rewrite_test_output_indented.json", 2}, + {"test/core/json/rewrite_test_output_indented.json", + "test/core/json/rewrite_test_output_condensed.json", 0}, + {"test/core/json/rewrite_test_output_condensed.json", + "test/core/json/rewrite_test_output_indented.json", 2}, }; -void test_rewrites() { +void +test_rewrites () +{ unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE(test_files); i++) { - test_file* test = test_files + i; - FILE* input = fopen(test->input, "rb"); - FILE* cmp = fopen(test->cmp, "rb"); - int status; - gpr_log(GPR_INFO, "Testing file %s against %s using indent=%i", test->input, - test->cmp, test->indent); - status = rewrite_and_compare(input, cmp, test->indent); - GPR_ASSERT(status); - fclose(input); - fclose(cmp); - } + for (i = 0; i < GPR_ARRAY_SIZE (test_files); i++) + { + test_file *test = test_files + i; + FILE *input = fopen (test->input, "rb"); + FILE *cmp = fopen (test->cmp, "rb"); + int status; + gpr_log (GPR_INFO, "Testing file %s against %s using indent=%i", test->input, test->cmp, test->indent); + status = rewrite_and_compare (input, cmp, test->indent); + GPR_ASSERT (status); + fclose (input); + fclose (cmp); + } } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - test_rewrites(); - gpr_log(GPR_INFO, "json_rewrite_test success"); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_rewrites (); + gpr_log (GPR_INFO, "json_rewrite_test success"); return 0; } diff --git a/test/core/json/json_test.c b/test/core/json/json_test.c index a500effcea..2f4e2921eb 100644 --- a/test/core/json/json_test.c +++ b/test/core/json/json_test.c @@ -42,141 +42,151 @@ #include "test/core/util/test_config.h" -typedef struct testing_pair { - const char* input; - const char* output; +typedef struct testing_pair +{ + const char *input; + const char *output; } testing_pair; static testing_pair testing_pairs[] = { - /* Testing valid parsing. */ - - /* Testing trivial parses, with de-indentation. */ - {" 0 ", "0"}, - {" 1 ", "1"}, - {" \"a\" ", "\"a\""}, - {" true ", "true"}, - /* Testing the parser's ability to decode trivial UTF-16. */ - {"\"\\u0020\\\\\\u0010\\u000a\\u000D\"", "\" \\\\\\u0010\\n\\r\""}, - /* Testing various UTF-8 sequences. */ - {"\"ßâñć௵⇒\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, - {"\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", - "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, - /* Testing UTF-8 character "𝄞", U+11D1E. */ - {"\"\xf0\x9d\x84\x9e\"", "\"\\ud834\\udd1e\""}, - {"\"\\ud834\\udd1e\"", "\"\\ud834\\udd1e\""}, - /* Testing nested empty containers. */ - { - " [ [ ] , { } , [ ] ] ", "[[],{},[]]", - }, - /* Testing escapes and control chars in key strings. */ - {" { \"\x7f\\n\\\\a , b\": 1, \"\": 0 } ", - "{\"\\u007f\\n\\\\a , b\":1,\"\":0}"}, - /* Testing the writer's ability to cut off invalid UTF-8 sequences. */ - {"\"abc\xf0\x9d\x24\"", "\"abc\""}, - {"\"\xff\"", "\"\""}, - /* Testing valid number parsing. */ - {"[0, 42 , 0.0123, 123.456]", "[0,42,0.0123,123.456]"}, - {"[1e4,-53.235e-31, 0.3e+3]", "[1e4,-53.235e-31,0.3e+3]"}, - /* Testing keywords parsing. */ - {"[true, false, null]", "[true,false,null]"}, - - /* Testing invalid parsing. */ - - /* Testing plain invalid things, exercising the state machine. */ - {"\\", NULL}, - {"nu ll", NULL}, - {"fals", NULL}, - /* Testing unterminated string. */ - {"\"\\x", NULL}, - /* Testing invalid UTF-16 number. */ - {"\"\\u123x", NULL}, - /* Testing imbalanced surrogate pairs. */ - {"\"\\ud834f", NULL}, - {"\"\\ud834\\n", NULL}, - {"\"\\udd1ef", NULL}, - {"\"\\ud834\\ud834\"", NULL}, - {"\"\\ud834\\u1234\"", NULL}, - /* Testing embedded invalid whitechars. */ - {"\"\n\"", NULL}, - {"\"\t\"", NULL}, - /* Testing empty json data. */ - {"", NULL}, - /* Testing extra characters after end of parsing. */ - {"{},", NULL}, - /* Testing imbalanced containers. */ - {"{}}", NULL}, - {"[]]", NULL}, - {"{{}", NULL}, - {"[[]", NULL}, - {"[}", NULL}, - {"{]", NULL}, - /*Testing trailing comma. */ - {"{,}", NULL}, - {"[1,2,3,4,]", NULL}, - /* Testing having a key syntax in an array. */ - {"[\"x\":0]", NULL}, - /* Testing invalid numbers. */ - {"1.", NULL}, - {"1e", NULL}, - {".12", NULL}, - {"1.x", NULL}, - {"1.12x", NULL}, - {"1ex", NULL}, - {"1e12x", NULL}, - {".12x", NULL}, - {"000", NULL}, + /* Testing valid parsing. */ + + /* Testing trivial parses, with de-indentation. */ + {" 0 ", "0"}, + {" 1 ", "1"}, + {" \"a\" ", "\"a\""}, + {" true ", "true"}, + /* Testing the parser's ability to decode trivial UTF-16. */ + {"\"\\u0020\\\\\\u0010\\u000a\\u000D\"", "\" \\\\\\u0010\\n\\r\""}, + /* Testing various UTF-8 sequences. */ + {"\"ßâñć௵⇒\"", "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, + {"\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", + "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\""}, + /* Testing UTF-8 character "𝄞", U+11D1E. */ + {"\"\xf0\x9d\x84\x9e\"", "\"\\ud834\\udd1e\""}, + {"\"\\ud834\\udd1e\"", "\"\\ud834\\udd1e\""}, + /* Testing nested empty containers. */ + { + " [ [ ] , { } , [ ] ] ", "[[],{},[]]", + }, + /* Testing escapes and control chars in key strings. */ + {" { \"\x7f\\n\\\\a , b\": 1, \"\": 0 } ", + "{\"\\u007f\\n\\\\a , b\":1,\"\":0}"}, + /* Testing the writer's ability to cut off invalid UTF-8 sequences. */ + {"\"abc\xf0\x9d\x24\"", "\"abc\""}, + {"\"\xff\"", "\"\""}, + /* Testing valid number parsing. */ + {"[0, 42 , 0.0123, 123.456]", "[0,42,0.0123,123.456]"}, + {"[1e4,-53.235e-31, 0.3e+3]", "[1e4,-53.235e-31,0.3e+3]"}, + /* Testing keywords parsing. */ + {"[true, false, null]", "[true,false,null]"}, + + /* Testing invalid parsing. */ + + /* Testing plain invalid things, exercising the state machine. */ + {"\\", NULL}, + {"nu ll", NULL}, + {"fals", NULL}, + /* Testing unterminated string. */ + {"\"\\x", NULL}, + /* Testing invalid UTF-16 number. */ + {"\"\\u123x", NULL}, + /* Testing imbalanced surrogate pairs. */ + {"\"\\ud834f", NULL}, + {"\"\\ud834\\n", NULL}, + {"\"\\udd1ef", NULL}, + {"\"\\ud834\\ud834\"", NULL}, + {"\"\\ud834\\u1234\"", NULL}, + /* Testing embedded invalid whitechars. */ + {"\"\n\"", NULL}, + {"\"\t\"", NULL}, + /* Testing empty json data. */ + {"", NULL}, + /* Testing extra characters after end of parsing. */ + {"{},", NULL}, + /* Testing imbalanced containers. */ + {"{}}", NULL}, + {"[]]", NULL}, + {"{{}", NULL}, + {"[[]", NULL}, + {"[}", NULL}, + {"{]", NULL}, + /*Testing trailing comma. */ + {"{,}", NULL}, + {"[1,2,3,4,]", NULL}, + /* Testing having a key syntax in an array. */ + {"[\"x\":0]", NULL}, + /* Testing invalid numbers. */ + {"1.", NULL}, + {"1e", NULL}, + {".12", NULL}, + {"1.x", NULL}, + {"1.12x", NULL}, + {"1ex", NULL}, + {"1e12x", NULL}, + {".12x", NULL}, + {"000", NULL}, }; -static void test_pairs() { +static void +test_pairs () +{ unsigned i; - for (i = 0; i < GPR_ARRAY_SIZE(testing_pairs); i++) { - testing_pair* pair = testing_pairs + i; - char* scratchpad = gpr_strdup(pair->input); - grpc_json* json; - - gpr_log(GPR_INFO, "parsing string %i - should %s", i, - pair->output ? "succeed" : "fail"); - json = grpc_json_parse_string(scratchpad); - - if (pair->output) { - char* output; - - GPR_ASSERT(json); - output = grpc_json_dump_to_string(json, 0); - GPR_ASSERT(output); - gpr_log(GPR_INFO, "succeeded with output = %s", output); - GPR_ASSERT(strcmp(output, pair->output) == 0); - - grpc_json_destroy(json); - gpr_free(output); - } else { - gpr_log(GPR_INFO, "failed"); - GPR_ASSERT(!json); + for (i = 0; i < GPR_ARRAY_SIZE (testing_pairs); i++) + { + testing_pair *pair = testing_pairs + i; + char *scratchpad = gpr_strdup (pair->input); + grpc_json *json; + + gpr_log (GPR_INFO, "parsing string %i - should %s", i, pair->output ? "succeed" : "fail"); + json = grpc_json_parse_string (scratchpad); + + if (pair->output) + { + char *output; + + GPR_ASSERT (json); + output = grpc_json_dump_to_string (json, 0); + GPR_ASSERT (output); + gpr_log (GPR_INFO, "succeeded with output = %s", output); + GPR_ASSERT (strcmp (output, pair->output) == 0); + + grpc_json_destroy (json); + gpr_free (output); + } + else + { + gpr_log (GPR_INFO, "failed"); + GPR_ASSERT (!json); + } + + gpr_free (scratchpad); } - - gpr_free(scratchpad); - } } -static void test_atypical() { - char* scratchpad = gpr_strdup("[[],[]]"); - grpc_json* json = grpc_json_parse_string(scratchpad); - grpc_json* brother; +static void +test_atypical () +{ + char *scratchpad = gpr_strdup ("[[],[]]"); + grpc_json *json = grpc_json_parse_string (scratchpad); + grpc_json *brother; - GPR_ASSERT(json); - GPR_ASSERT(json->child); + GPR_ASSERT (json); + GPR_ASSERT (json->child); brother = json->child->next; - grpc_json_destroy(json->child); + grpc_json_destroy (json->child); json->child = brother; - grpc_json_destroy(json); - gpr_free(scratchpad); + grpc_json_destroy (json); + gpr_free (scratchpad); } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - test_pairs(); - test_atypical(); - gpr_log(GPR_INFO, "json_test success"); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_pairs (); + test_atypical (); + gpr_log (GPR_INFO, "json_test success"); return 0; } diff --git a/test/core/network_benchmarks/low_level_ping_pong.c b/test/core/network_benchmarks/low_level_ping_pong.c index 0ce4bd4b25..35c892f2c8 100644 --- a/test/core/network_benchmarks/low_level_ping_pong.c +++ b/test/core/network_benchmarks/low_level_ping_pong.c @@ -57,17 +57,19 @@ #include <grpc/support/time.h> #include <grpc/support/useful.h> -typedef struct fd_pair { +typedef struct fd_pair +{ int read_fd; int write_fd; } fd_pair; -typedef struct thread_args { +typedef struct thread_args +{ fd_pair fds; size_t msg_size; - int (*read_bytes)(struct thread_args *args, char *buf); - int (*write_bytes)(struct thread_args *args, char *buf); - int (*setup)(struct thread_args *args); + int (*read_bytes) (struct thread_args * args, char *buf); + int (*write_bytes) (struct thread_args * args, char *buf); + int (*setup) (struct thread_args * args); int epoll_fd; char *strategy_name; } thread_args; @@ -80,116 +82,163 @@ typedef struct thread_args { */ /* Basic call to read() */ -static int read_bytes(int fd, char *buf, size_t read_size, int spin) { +static int +read_bytes (int fd, char *buf, size_t read_size, int spin) +{ size_t bytes_read = 0; int err; - do { - err = read(fd, buf + bytes_read, read_size - bytes_read); - if (err < 0) { - if (errno == EINTR) { - continue; - } else { - if (errno == EAGAIN && spin == 1) { - continue; - } - gpr_log(GPR_ERROR, "Read failed: %s", strerror(errno)); - return -1; - } - } else { - bytes_read += err; + do + { + err = read (fd, buf + bytes_read, read_size - bytes_read); + if (err < 0) + { + if (errno == EINTR) + { + continue; + } + else + { + if (errno == EAGAIN && spin == 1) + { + continue; + } + gpr_log (GPR_ERROR, "Read failed: %s", strerror (errno)); + return -1; + } + } + else + { + bytes_read += err; + } } - } while (bytes_read < read_size); + while (bytes_read < read_size); return 0; } -static int blocking_read_bytes(thread_args *args, char *buf) { - return read_bytes(args->fds.read_fd, buf, args->msg_size, 0); +static int +blocking_read_bytes (thread_args * args, char *buf) +{ + return read_bytes (args->fds.read_fd, buf, args->msg_size, 0); } -static int spin_read_bytes(thread_args *args, char *buf) { - return read_bytes(args->fds.read_fd, buf, args->msg_size, 1); +static int +spin_read_bytes (thread_args * args, char *buf) +{ + return read_bytes (args->fds.read_fd, buf, args->msg_size, 1); } /* Call poll() to monitor a non-blocking fd */ -static int poll_read_bytes(int fd, char *buf, size_t read_size, int spin) { +static int +poll_read_bytes (int fd, char *buf, size_t read_size, int spin) +{ struct pollfd pfd; size_t bytes_read = 0; int err; pfd.fd = fd; pfd.events = POLLIN; - do { - err = poll(&pfd, 1, spin ? 0 : -1); - if (err < 0) { - if (errno == EINTR) { - continue; - } else { - gpr_log(GPR_ERROR, "Poll failed: %s", strerror(errno)); - return -1; - } - } - if (err == 0 && spin) continue; - GPR_ASSERT(err == 1); - GPR_ASSERT(pfd.revents == POLLIN); - do { - err = read(fd, buf + bytes_read, read_size - bytes_read); - } while (err < 0 && errno == EINTR); - if (err < 0 && errno != EAGAIN) { - gpr_log(GPR_ERROR, "Read failed: %s", strerror(errno)); - return -1; + do + { + err = poll (&pfd, 1, spin ? 0 : -1); + if (err < 0) + { + if (errno == EINTR) + { + continue; + } + else + { + gpr_log (GPR_ERROR, "Poll failed: %s", strerror (errno)); + return -1; + } + } + if (err == 0 && spin) + continue; + GPR_ASSERT (err == 1); + GPR_ASSERT (pfd.revents == POLLIN); + do + { + err = read (fd, buf + bytes_read, read_size - bytes_read); + } + while (err < 0 && errno == EINTR); + if (err < 0 && errno != EAGAIN) + { + gpr_log (GPR_ERROR, "Read failed: %s", strerror (errno)); + return -1; + } + bytes_read += err; } - bytes_read += err; - } while (bytes_read < read_size); + while (bytes_read < read_size); return 0; } -static int poll_read_bytes_blocking(struct thread_args *args, char *buf) { - return poll_read_bytes(args->fds.read_fd, buf, args->msg_size, 0); +static int +poll_read_bytes_blocking (struct thread_args *args, char *buf) +{ + return poll_read_bytes (args->fds.read_fd, buf, args->msg_size, 0); } -static int poll_read_bytes_spin(struct thread_args *args, char *buf) { - return poll_read_bytes(args->fds.read_fd, buf, args->msg_size, 1); +static int +poll_read_bytes_spin (struct thread_args *args, char *buf) +{ + return poll_read_bytes (args->fds.read_fd, buf, args->msg_size, 1); } #ifdef __linux__ /* Call epoll_wait() to monitor a non-blocking fd */ -static int epoll_read_bytes(struct thread_args *args, char *buf, int spin) { +static int +epoll_read_bytes (struct thread_args *args, char *buf, int spin) +{ struct epoll_event ev; size_t bytes_read = 0; int err; size_t read_size = args->msg_size; - do { - err = epoll_wait(args->epoll_fd, &ev, 1, spin ? 0 : -1); - if (err < 0) { - if (errno == EINTR) continue; - gpr_log(GPR_ERROR, "epoll_wait failed: %s", strerror(errno)); - return -1; + do + { + err = epoll_wait (args->epoll_fd, &ev, 1, spin ? 0 : -1); + if (err < 0) + { + if (errno == EINTR) + continue; + gpr_log (GPR_ERROR, "epoll_wait failed: %s", strerror (errno)); + return -1; + } + if (err == 0 && spin) + continue; + GPR_ASSERT (err == 1); + GPR_ASSERT (ev.events & EPOLLIN); + GPR_ASSERT (ev.data.fd == args->fds.read_fd); + do + { + do + { + err = read (args->fds.read_fd, buf + bytes_read, read_size - bytes_read); + } + while (err < 0 && errno == EINTR); + if (errno == EAGAIN) + break; + bytes_read += err; + /* TODO(klempner): This should really be doing an extra call after we are + done to ensure we see an EAGAIN */ + } + while (bytes_read < read_size); } - if (err == 0 && spin) continue; - GPR_ASSERT(err == 1); - GPR_ASSERT(ev.events & EPOLLIN); - GPR_ASSERT(ev.data.fd == args->fds.read_fd); - do { - do { - err = read(args->fds.read_fd, buf + bytes_read, read_size - bytes_read); - } while (err < 0 && errno == EINTR); - if (errno == EAGAIN) break; - bytes_read += err; - /* TODO(klempner): This should really be doing an extra call after we are - done to ensure we see an EAGAIN */ - } while (bytes_read < read_size); - } while (bytes_read < read_size); - GPR_ASSERT(bytes_read == read_size); + while (bytes_read < read_size); + GPR_ASSERT (bytes_read == read_size); return 0; } -static int epoll_read_bytes_blocking(struct thread_args *args, char *buf) { - return epoll_read_bytes(args, buf, 0); +static int +epoll_read_bytes_blocking (struct thread_args *args, char *buf) +{ + return epoll_read_bytes (args, buf, 0); } -static int epoll_read_bytes_spin(struct thread_args *args, char *buf) { - return epoll_read_bytes(args, buf, 1); +static int +epoll_read_bytes_spin (struct thread_args *args, char *buf) +{ + return epoll_read_bytes (args, buf, 1); } #endif /* __linux__ */ @@ -197,24 +246,33 @@ static int epoll_read_bytes_spin(struct thread_args *args, char *buf) { At this point we only have one strategy, since in the common case these writes go directly out to the kernel. */ -static int blocking_write_bytes(struct thread_args *args, char *buf) { +static int +blocking_write_bytes (struct thread_args *args, char *buf) +{ size_t bytes_written = 0; int err; size_t write_size = args->msg_size; - do { - err = write(args->fds.write_fd, buf + bytes_written, - write_size - bytes_written); - if (err < 0) { - if (errno == EINTR) { - continue; - } else { - gpr_log(GPR_ERROR, "Read failed: %s", strerror(errno)); - return -1; - } - } else { - bytes_written += err; - } - } while (bytes_written < write_size); + do + { + err = write (args->fds.write_fd, buf + bytes_written, write_size - bytes_written); + if (err < 0) + { + if (errno == EINTR) + { + continue; + } + else + { + gpr_log (GPR_ERROR, "Read failed: %s", strerror (errno)); + return -1; + } + } + else + { + bytes_written += err; + } + } + while (bytes_written < write_size); return 0; } @@ -224,250 +282,305 @@ static int blocking_write_bytes(struct thread_args *args, char *buf) { These are called at the beginning of the client and server thread, depending on the scenario we're using. */ -static int set_socket_nonblocking(thread_args *args) { - if (!grpc_set_socket_nonblocking(args->fds.read_fd, 1)) { - gpr_log(GPR_ERROR, "Unable to set socket nonblocking: %s", strerror(errno)); - return -1; - } - if (!grpc_set_socket_nonblocking(args->fds.write_fd, 1)) { - gpr_log(GPR_ERROR, "Unable to set socket nonblocking: %s", strerror(errno)); - return -1; - } +static int +set_socket_nonblocking (thread_args * args) +{ + if (!grpc_set_socket_nonblocking (args->fds.read_fd, 1)) + { + gpr_log (GPR_ERROR, "Unable to set socket nonblocking: %s", strerror (errno)); + return -1; + } + if (!grpc_set_socket_nonblocking (args->fds.write_fd, 1)) + { + gpr_log (GPR_ERROR, "Unable to set socket nonblocking: %s", strerror (errno)); + return -1; + } return 0; } -static int do_nothing(thread_args *args) { return 0; } +static int +do_nothing (thread_args * args) +{ + return 0; +} #ifdef __linux__ /* Special case for epoll, where we need to create the fd ahead of time. */ -static int epoll_setup(thread_args *args) { +static int +epoll_setup (thread_args * args) +{ int epoll_fd; struct epoll_event ev; - set_socket_nonblocking(args); - epoll_fd = epoll_create(1); - if (epoll_fd < 0) { - gpr_log(GPR_ERROR, "epoll_create: %s", strerror(errno)); - return -1; - } + set_socket_nonblocking (args); + epoll_fd = epoll_create (1); + if (epoll_fd < 0) + { + gpr_log (GPR_ERROR, "epoll_create: %s", strerror (errno)); + return -1; + } args->epoll_fd = epoll_fd; ev.events = EPOLLIN | EPOLLET; ev.data.fd = args->fds.read_fd; - if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, args->fds.read_fd, &ev) < 0) { - gpr_log(GPR_ERROR, "epoll_ctl: %s", strerror(errno)); - } + if (epoll_ctl (epoll_fd, EPOLL_CTL_ADD, args->fds.read_fd, &ev) < 0) + { + gpr_log (GPR_ERROR, "epoll_ctl: %s", strerror (errno)); + } return 0; } #endif -static void server_thread(thread_args *args) { - char *buf = malloc(args->msg_size); - if (args->setup(args) < 0) { - gpr_log(GPR_ERROR, "Setup failed"); - } - for (;;) { - if (args->read_bytes(args, buf) < 0) { - gpr_log(GPR_ERROR, "Server read failed"); - free(buf); - return; - } - if (args->write_bytes(args, buf) < 0) { - gpr_log(GPR_ERROR, "Server write failed"); - free(buf); - return; - } - } +static void +server_thread (thread_args * args) +{ + char *buf = malloc (args->msg_size); + if (args->setup (args) < 0) + { + gpr_log (GPR_ERROR, "Setup failed"); + } + for (;;) + { + if (args->read_bytes (args, buf) < 0) + { + gpr_log (GPR_ERROR, "Server read failed"); + free (buf); + return; + } + if (args->write_bytes (args, buf) < 0) + { + gpr_log (GPR_ERROR, "Server write failed"); + free (buf); + return; + } + } } -static void server_thread_wrap(void *arg) { +static void +server_thread_wrap (void *arg) +{ thread_args *args = arg; - server_thread(args); + server_thread (args); } -static void print_histogram(gpr_histogram *histogram) { +static void +print_histogram (gpr_histogram * histogram) +{ /* TODO(klempner): Print more detailed information, such as detailed histogram buckets */ - gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", - gpr_histogram_percentile(histogram, 50), - gpr_histogram_percentile(histogram, 95), - gpr_histogram_percentile(histogram, 99), - gpr_histogram_percentile(histogram, 99.9)); + gpr_log (GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", gpr_histogram_percentile (histogram, 50), gpr_histogram_percentile (histogram, 95), gpr_histogram_percentile (histogram, 99), gpr_histogram_percentile (histogram, 99.9)); } -static double now(void) { - gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME); +static double +now (void) +{ + gpr_timespec tv = gpr_now (GPR_CLOCK_REALTIME); return 1e9 * tv.tv_sec + tv.tv_nsec; } -static void client_thread(thread_args *args) { - char *buf = calloc(args->msg_size, sizeof(char)); - gpr_histogram *histogram = gpr_histogram_create(0.01, 60e9); +static void +client_thread (thread_args * args) +{ + char *buf = calloc (args->msg_size, sizeof (char)); + gpr_histogram *histogram = gpr_histogram_create (0.01, 60e9); double start_time; double end_time; double interval; const int kNumIters = 100000; int i; - if (args->setup(args) < 0) { - gpr_log(GPR_ERROR, "Setup failed"); - } - for (i = 0; i < kNumIters; ++i) { - start_time = now(); - if (args->write_bytes(args, buf) < 0) { - gpr_log(GPR_ERROR, "Client write failed"); - goto error; + if (args->setup (args) < 0) + { + gpr_log (GPR_ERROR, "Setup failed"); } - if (args->read_bytes(args, buf) < 0) { - gpr_log(GPR_ERROR, "Client read failed"); - goto error; + for (i = 0; i < kNumIters; ++i) + { + start_time = now (); + if (args->write_bytes (args, buf) < 0) + { + gpr_log (GPR_ERROR, "Client write failed"); + goto error; + } + if (args->read_bytes (args, buf) < 0) + { + gpr_log (GPR_ERROR, "Client read failed"); + goto error; + } + end_time = now (); + if (i > kNumIters / 2) + { + interval = end_time - start_time; + gpr_histogram_add (histogram, interval); + } } - end_time = now(); - if (i > kNumIters / 2) { - interval = end_time - start_time; - gpr_histogram_add(histogram, interval); - } - } - print_histogram(histogram); + print_histogram (histogram); error: - free(buf); - gpr_histogram_destroy(histogram); + free (buf); + gpr_histogram_destroy (histogram); } /* This roughly matches tcp_server's create_listening_socket */ -static int create_listening_socket(struct sockaddr *port, socklen_t len) { - int fd = socket(port->sa_family, SOCK_STREAM, 0); - if (fd < 0) { - gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); - goto error; - } - - if (!grpc_set_socket_cloexec(fd, 1) || !grpc_set_socket_low_latency(fd, 1) || - !grpc_set_socket_reuse_addr(fd, 1)) { - gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, - strerror(errno)); - goto error; - } - - if (bind(fd, port, len) < 0) { - gpr_log(GPR_ERROR, "bind: %s", strerror(errno)); - goto error; - } - - if (listen(fd, 1) < 0) { - gpr_log(GPR_ERROR, "listen: %s", strerror(errno)); - goto error; - } - - if (getsockname(fd, port, &len) < 0) { - gpr_log(GPR_ERROR, "getsockname: %s", strerror(errno)); - goto error; - } +static int +create_listening_socket (struct sockaddr *port, socklen_t len) +{ + int fd = socket (port->sa_family, SOCK_STREAM, 0); + if (fd < 0) + { + gpr_log (GPR_ERROR, "Unable to create socket: %s", strerror (errno)); + goto error; + } + + if (!grpc_set_socket_cloexec (fd, 1) || !grpc_set_socket_low_latency (fd, 1) || !grpc_set_socket_reuse_addr (fd, 1)) + { + gpr_log (GPR_ERROR, "Unable to configure socket %d: %s", fd, strerror (errno)); + goto error; + } + + if (bind (fd, port, len) < 0) + { + gpr_log (GPR_ERROR, "bind: %s", strerror (errno)); + goto error; + } + + if (listen (fd, 1) < 0) + { + gpr_log (GPR_ERROR, "listen: %s", strerror (errno)); + goto error; + } + + if (getsockname (fd, port, &len) < 0) + { + gpr_log (GPR_ERROR, "getsockname: %s", strerror (errno)); + goto error; + } return fd; error: - if (fd >= 0) { - close(fd); - } + if (fd >= 0) + { + close (fd); + } return -1; } -static int connect_client(struct sockaddr *addr, int len) { - int fd = socket(addr->sa_family, SOCK_STREAM, 0); +static int +connect_client (struct sockaddr *addr, int len) +{ + int fd = socket (addr->sa_family, SOCK_STREAM, 0); int err; - if (fd < 0) { - gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); - goto error; - } - - if (!grpc_set_socket_cloexec(fd, 1) || !grpc_set_socket_low_latency(fd, 1)) { - gpr_log(GPR_ERROR, "Failed to configure socket"); - goto error; - } - - do { - err = connect(fd, addr, len); - } while (err < 0 && errno == EINTR); - - if (err < 0) { - gpr_log(GPR_ERROR, "connect error: %s", strerror(errno)); - goto error; - } + if (fd < 0) + { + gpr_log (GPR_ERROR, "Unable to create socket: %s", strerror (errno)); + goto error; + } + + if (!grpc_set_socket_cloexec (fd, 1) || !grpc_set_socket_low_latency (fd, 1)) + { + gpr_log (GPR_ERROR, "Failed to configure socket"); + goto error; + } + + do + { + err = connect (fd, addr, len); + } + while (err < 0 && errno == EINTR); + + if (err < 0) + { + gpr_log (GPR_ERROR, "connect error: %s", strerror (errno)); + goto error; + } return fd; error: - if (fd >= 0) { - close(fd); - } + if (fd >= 0) + { + close (fd); + } return -1; } -static int accept_server(int listen_fd) { - int fd = accept(listen_fd, NULL, NULL); - if (fd < 0) { - gpr_log(GPR_ERROR, "Accept failed: %s", strerror(errno)); - return -1; - } +static int +accept_server (int listen_fd) +{ + int fd = accept (listen_fd, NULL, NULL); + if (fd < 0) + { + gpr_log (GPR_ERROR, "Accept failed: %s", strerror (errno)); + return -1; + } return fd; } -static int create_sockets_tcp(fd_pair *client_fds, fd_pair *server_fds) { +static int +create_sockets_tcp (fd_pair * client_fds, fd_pair * server_fds) +{ int listen_fd = -1; int client_fd = -1; int server_fd = -1; struct sockaddr_in port; - struct sockaddr *sa_port = (struct sockaddr *)&port; + struct sockaddr *sa_port = (struct sockaddr *) &port; port.sin_family = AF_INET; port.sin_port = 0; port.sin_addr.s_addr = INADDR_ANY; - listen_fd = create_listening_socket(sa_port, sizeof(port)); - if (listen_fd == -1) { - gpr_log(GPR_ERROR, "Listen failed"); - goto error; - } + listen_fd = create_listening_socket (sa_port, sizeof (port)); + if (listen_fd == -1) + { + gpr_log (GPR_ERROR, "Listen failed"); + goto error; + } - client_fd = connect_client(sa_port, sizeof(port)); - if (client_fd == -1) { - gpr_log(GPR_ERROR, "Connect failed"); - goto error; - } + client_fd = connect_client (sa_port, sizeof (port)); + if (client_fd == -1) + { + gpr_log (GPR_ERROR, "Connect failed"); + goto error; + } - server_fd = accept_server(listen_fd); - if (server_fd == -1) { - gpr_log(GPR_ERROR, "Accept failed"); - goto error; - } + server_fd = accept_server (listen_fd); + if (server_fd == -1) + { + gpr_log (GPR_ERROR, "Accept failed"); + goto error; + } client_fds->read_fd = client_fd; client_fds->write_fd = client_fd; server_fds->read_fd = server_fd; server_fds->write_fd = server_fd; - close(listen_fd); + close (listen_fd); return 0; error: - if (listen_fd != -1) { - close(listen_fd); - } - if (client_fd != -1) { - close(client_fd); - } - if (server_fd != -1) { - close(server_fd); - } + if (listen_fd != -1) + { + close (listen_fd); + } + if (client_fd != -1) + { + close (client_fd); + } + if (server_fd != -1) + { + close (server_fd); + } return -1; } -static int create_sockets_socketpair(fd_pair *client_fds, fd_pair *server_fds) { +static int +create_sockets_socketpair (fd_pair * client_fds, fd_pair * server_fds) +{ int fds[2]; - if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) { - gpr_log(GPR_ERROR, "socketpair: %s", strerror(errno)); - return -1; - } + if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0) + { + gpr_log (GPR_ERROR, "socketpair: %s", strerror (errno)); + return -1; + } client_fds->read_fd = fds[0]; client_fds->write_fd = fds[0]; @@ -476,18 +589,22 @@ static int create_sockets_socketpair(fd_pair *client_fds, fd_pair *server_fds) { return 0; } -static int create_sockets_pipe(fd_pair *client_fds, fd_pair *server_fds) { +static int +create_sockets_pipe (fd_pair * client_fds, fd_pair * server_fds) +{ int cfds[2]; int sfds[2]; - if (pipe(cfds) < 0) { - gpr_log(GPR_ERROR, "pipe: %s", strerror(errno)); - return -1; - } + if (pipe (cfds) < 0) + { + gpr_log (GPR_ERROR, "pipe: %s", strerror (errno)); + return -1; + } - if (pipe(sfds) < 0) { - gpr_log(GPR_ERROR, "pipe: %s", strerror(errno)); - return -1; - } + if (pipe (sfds) < 0) + { + gpr_log (GPR_ERROR, "pipe: %s", strerror (errno)); + return -1; + } client_fds->read_fd = cfds[0]; client_fds->write_fd = cfds[1]; @@ -496,129 +613,142 @@ static int create_sockets_pipe(fd_pair *client_fds, fd_pair *server_fds) { return 0; } -static const char *read_strategy_usage = - "Strategy for doing reads, which is one of:\n" - " blocking: blocking read calls\n" - " same_thread_poll: poll() call on same thread \n" +static const char *read_strategy_usage = "Strategy for doing reads, which is one of:\n" " blocking: blocking read calls\n" " same_thread_poll: poll() call on same thread \n" #ifdef __linux__ - " same_thread_epoll: epoll_wait() on same thread \n" + " same_thread_epoll: epoll_wait() on same thread \n" #endif - " spin_read: spinning non-blocking read() calls \n" - " spin_poll: spinning 0 timeout poll() calls \n" + " spin_read: spinning non-blocking read() calls \n" " spin_poll: spinning 0 timeout poll() calls \n" #ifdef __linux__ - " spin_epoll: spinning 0 timeout epoll_wait() calls \n" + " spin_epoll: spinning 0 timeout epoll_wait() calls \n" #endif - ""; - -static const char *socket_type_usage = - "Type of socket used, one of:\n" - " tcp: fds are endpoints of a TCP connection\n" - " socketpair: fds come from socketpair()\n" - " pipe: fds come from pipe()\n"; - -void print_usage(char *argv0) { - fprintf(stderr, "%s usage:\n\n", argv0); - fprintf(stderr, "%s read_strategy socket_type msg_size\n\n", argv0); - fprintf(stderr, "where read_strategy is one of:\n"); - fprintf(stderr, " blocking: blocking read calls\n"); - fprintf(stderr, " same_thread_poll: poll() call on same thread \n"); + ""; + +static const char *socket_type_usage = "Type of socket used, one of:\n" " tcp: fds are endpoints of a TCP connection\n" " socketpair: fds come from socketpair()\n" " pipe: fds come from pipe()\n"; + +void +print_usage (char *argv0) +{ + fprintf (stderr, "%s usage:\n\n", argv0); + fprintf (stderr, "%s read_strategy socket_type msg_size\n\n", argv0); + fprintf (stderr, "where read_strategy is one of:\n"); + fprintf (stderr, " blocking: blocking read calls\n"); + fprintf (stderr, " same_thread_poll: poll() call on same thread \n"); #ifdef __linux__ - fprintf(stderr, " same_thread_epoll: epoll_wait() on same thread \n"); + fprintf (stderr, " same_thread_epoll: epoll_wait() on same thread \n"); #endif - fprintf(stderr, " spin_read: spinning non-blocking read() calls \n"); - fprintf(stderr, " spin_poll: spinning 0 timeout poll() calls \n"); + fprintf (stderr, " spin_read: spinning non-blocking read() calls \n"); + fprintf (stderr, " spin_poll: spinning 0 timeout poll() calls \n"); #ifdef __linux__ - fprintf(stderr, " spin_epoll: spinning 0 timeout epoll_wait() calls \n"); + fprintf (stderr, " spin_epoll: spinning 0 timeout epoll_wait() calls \n"); #endif - fprintf(stderr, "and socket_type is one of:\n"); - fprintf(stderr, " tcp: fds are endpoints of a TCP connection\n"); - fprintf(stderr, " socketpair: fds come from socketpair()\n"); - fprintf(stderr, " pipe: fds come from pipe()\n"); + fprintf (stderr, "and socket_type is one of:\n"); + fprintf (stderr, " tcp: fds are endpoints of a TCP connection\n"); + fprintf (stderr, " socketpair: fds come from socketpair()\n"); + fprintf (stderr, " pipe: fds come from pipe()\n"); } -typedef struct test_strategy { +typedef struct test_strategy +{ char *name; - int (*read_strategy)(struct thread_args *args, char *buf); - int (*setup)(struct thread_args *args); + int (*read_strategy) (struct thread_args * args, char *buf); + int (*setup) (struct thread_args * args); } test_strategy; static test_strategy test_strategies[] = { - {"blocking", blocking_read_bytes, do_nothing}, - {"same_thread_poll", poll_read_bytes_blocking, set_socket_nonblocking}, + {"blocking", blocking_read_bytes, do_nothing}, + {"same_thread_poll", poll_read_bytes_blocking, set_socket_nonblocking}, #ifdef __linux__ - {"same_thread_epoll", epoll_read_bytes_blocking, epoll_setup}, - {"spin_epoll", epoll_read_bytes_spin, epoll_setup}, + {"same_thread_epoll", epoll_read_bytes_blocking, epoll_setup}, + {"spin_epoll", epoll_read_bytes_spin, epoll_setup}, #endif /* __linux__ */ - {"spin_read", spin_read_bytes, set_socket_nonblocking}, - {"spin_poll", poll_read_bytes_spin, set_socket_nonblocking}}; - -static char *socket_types[] = {"tcp", "socketpair", "pipe"}; - -int create_socket(char *socket_type, fd_pair *client_fds, fd_pair *server_fds) { - if (strcmp(socket_type, "tcp") == 0) { - create_sockets_tcp(client_fds, server_fds); - } else if (strcmp(socket_type, "socketpair") == 0) { - create_sockets_socketpair(client_fds, server_fds); - } else if (strcmp(socket_type, "pipe") == 0) { - create_sockets_pipe(client_fds, server_fds); - } else { - fprintf(stderr, "Invalid socket type %s\n", socket_type); - return -1; - } + {"spin_read", spin_read_bytes, set_socket_nonblocking}, + {"spin_poll", poll_read_bytes_spin, set_socket_nonblocking} +}; + +static char *socket_types[] = { "tcp", "socketpair", "pipe" }; + +int +create_socket (char *socket_type, fd_pair * client_fds, fd_pair * server_fds) +{ + if (strcmp (socket_type, "tcp") == 0) + { + create_sockets_tcp (client_fds, server_fds); + } + else if (strcmp (socket_type, "socketpair") == 0) + { + create_sockets_socketpair (client_fds, server_fds); + } + else if (strcmp (socket_type, "pipe") == 0) + { + create_sockets_pipe (client_fds, server_fds); + } + else + { + fprintf (stderr, "Invalid socket type %s\n", socket_type); + return -1; + } return 0; } -static int run_benchmark(char *socket_type, thread_args *client_args, - thread_args *server_args) { +static int +run_benchmark (char *socket_type, thread_args * client_args, thread_args * server_args) +{ gpr_thd_id tid; int rv = 0; - rv = create_socket(socket_type, &client_args->fds, &server_args->fds); - if (rv < 0) { - return rv; - } + rv = create_socket (socket_type, &client_args->fds, &server_args->fds); + if (rv < 0) + { + return rv; + } - gpr_log(GPR_INFO, "Starting test %s %s %d", client_args->strategy_name, - socket_type, client_args->msg_size); + gpr_log (GPR_INFO, "Starting test %s %s %d", client_args->strategy_name, socket_type, client_args->msg_size); - gpr_thd_new(&tid, server_thread_wrap, server_args, NULL); - client_thread(client_args); + gpr_thd_new (&tid, server_thread_wrap, server_args, NULL); + client_thread (client_args); return 0; } -static int run_all_benchmarks(int msg_size) { +static int +run_all_benchmarks (int msg_size) +{ int error = 0; size_t i; - for (i = 0; i < GPR_ARRAY_SIZE(test_strategies); ++i) { - test_strategy *test_strategy = &test_strategies[i]; - size_t j; - for (j = 0; j < GPR_ARRAY_SIZE(socket_types); ++j) { - thread_args *client_args = malloc(sizeof(thread_args)); - thread_args *server_args = malloc(sizeof(thread_args)); - char *socket_type = socket_types[j]; - - client_args->read_bytes = test_strategy->read_strategy; - client_args->write_bytes = blocking_write_bytes; - client_args->setup = test_strategy->setup; - client_args->msg_size = msg_size; - client_args->strategy_name = test_strategy->name; - server_args->read_bytes = test_strategy->read_strategy; - server_args->write_bytes = blocking_write_bytes; - server_args->setup = test_strategy->setup; - server_args->msg_size = msg_size; - server_args->strategy_name = test_strategy->name; - error = run_benchmark(socket_type, client_args, server_args); - if (error < 0) { - return error; - } - } - } + for (i = 0; i < GPR_ARRAY_SIZE (test_strategies); ++i) + { + test_strategy *test_strategy = &test_strategies[i]; + size_t j; + for (j = 0; j < GPR_ARRAY_SIZE (socket_types); ++j) + { + thread_args *client_args = malloc (sizeof (thread_args)); + thread_args *server_args = malloc (sizeof (thread_args)); + char *socket_type = socket_types[j]; + + client_args->read_bytes = test_strategy->read_strategy; + client_args->write_bytes = blocking_write_bytes; + client_args->setup = test_strategy->setup; + client_args->msg_size = msg_size; + client_args->strategy_name = test_strategy->name; + server_args->read_bytes = test_strategy->read_strategy; + server_args->write_bytes = blocking_write_bytes; + server_args->setup = test_strategy->setup; + server_args->msg_size = msg_size; + server_args->strategy_name = test_strategy->name; + error = run_benchmark (socket_type, client_args, server_args); + if (error < 0) + { + return error; + } + } + } return error; } -int main(int argc, char **argv) { - thread_args *client_args = malloc(sizeof(thread_args)); - thread_args *server_args = malloc(sizeof(thread_args)); +int +main (int argc, char **argv) +{ + thread_args *client_args = malloc (sizeof (thread_args)); + thread_args *server_args = malloc (sizeof (thread_args)); int msg_size = -1; char *read_strategy = NULL; char *socket_type = NULL; @@ -626,44 +756,48 @@ int main(int argc, char **argv) { const test_strategy *test_strategy = NULL; int error = 0; - gpr_cmdline *cmdline = - gpr_cmdline_create("low_level_ping_pong network benchmarking tool"); - - gpr_cmdline_add_int(cmdline, "msg_size", "Size of sent messages", &msg_size); - gpr_cmdline_add_string(cmdline, "read_strategy", read_strategy_usage, - &read_strategy); - gpr_cmdline_add_string(cmdline, "socket_type", socket_type_usage, - &socket_type); - - gpr_cmdline_parse(cmdline, argc, argv); - - if (msg_size == -1) { - msg_size = 50; - } - - if (read_strategy == NULL) { - gpr_log(GPR_INFO, "No strategy specified, running all benchmarks"); - return run_all_benchmarks(msg_size); - } - - if (socket_type == NULL) { - socket_type = "tcp"; - } - if (msg_size <= 0) { - fprintf(stderr, "msg_size must be > 0\n"); - print_usage(argv[0]); - return -1; - } - - for (i = 0; i < GPR_ARRAY_SIZE(test_strategies); ++i) { - if (strcmp(test_strategies[i].name, read_strategy) == 0) { - test_strategy = &test_strategies[i]; - } - } - if (test_strategy == NULL) { - fprintf(stderr, "Invalid read strategy %s\n", read_strategy); - return -1; - } + gpr_cmdline *cmdline = gpr_cmdline_create ("low_level_ping_pong network benchmarking tool"); + + gpr_cmdline_add_int (cmdline, "msg_size", "Size of sent messages", &msg_size); + gpr_cmdline_add_string (cmdline, "read_strategy", read_strategy_usage, &read_strategy); + gpr_cmdline_add_string (cmdline, "socket_type", socket_type_usage, &socket_type); + + gpr_cmdline_parse (cmdline, argc, argv); + + if (msg_size == -1) + { + msg_size = 50; + } + + if (read_strategy == NULL) + { + gpr_log (GPR_INFO, "No strategy specified, running all benchmarks"); + return run_all_benchmarks (msg_size); + } + + if (socket_type == NULL) + { + socket_type = "tcp"; + } + if (msg_size <= 0) + { + fprintf (stderr, "msg_size must be > 0\n"); + print_usage (argv[0]); + return -1; + } + + for (i = 0; i < GPR_ARRAY_SIZE (test_strategies); ++i) + { + if (strcmp (test_strategies[i].name, read_strategy) == 0) + { + test_strategy = &test_strategies[i]; + } + } + if (test_strategy == NULL) + { + fprintf (stderr, "Invalid read strategy %s\n", read_strategy); + return -1; + } client_args->read_bytes = test_strategy->read_strategy; client_args->write_bytes = blocking_write_bytes; @@ -676,8 +810,8 @@ int main(int argc, char **argv) { server_args->msg_size = msg_size; server_args->strategy_name = read_strategy; - error = run_benchmark(socket_type, client_args, server_args); + error = run_benchmark (socket_type, client_args, server_args); - gpr_cmdline_destroy(cmdline); + gpr_cmdline_destroy (cmdline); return error; } diff --git a/test/core/profiling/timers_test.c b/test/core/profiling/timers_test.c index b79cde64bd..6017f130dd 100644 --- a/test/core/profiling/timers_test.c +++ b/test/core/profiling/timers_test.c @@ -35,49 +35,58 @@ #include <stdlib.h> #include "test/core/util/test_config.h" -void test_log_events(size_t num_seqs) { +void +test_log_events (size_t num_seqs) +{ size_t start = 0; size_t *state; - state = calloc(num_seqs, sizeof(state[0])); - while (start < num_seqs) { - size_t i; - size_t row; - if (state[start] == 3) { /* Already done with this posn */ - start++; - continue; - } + state = calloc (num_seqs, sizeof (state[0])); + while (start < num_seqs) + { + size_t i; + size_t row; + if (state[start] == 3) + { /* Already done with this posn */ + start++; + continue; + } - row = (size_t)rand() % 10; /* how many in a row */ - for (i = start; (i < start + row) && (i < num_seqs); i++) { - size_t j; - size_t advance = 1 + (size_t)rand() % 3; /* how many to advance by */ - for (j = 0; j < advance; j++) { - switch (state[i]) { - case 0: - GRPC_TIMER_MARK(STATE_0, i); - state[i]++; - break; - case 1: - GRPC_TIMER_MARK(STATE_1, i); - state[i]++; - break; - case 2: - GRPC_TIMER_MARK(STATE_2, i); - state[i]++; - break; - case 3: - break; - } - } + row = (size_t) rand () % 10; /* how many in a row */ + for (i = start; (i < start + row) && (i < num_seqs); i++) + { + size_t j; + size_t advance = 1 + (size_t) rand () % 3; /* how many to advance by */ + for (j = 0; j < advance; j++) + { + switch (state[i]) + { + case 0: + GRPC_TIMER_MARK (STATE_0, i); + state[i]++; + break; + case 1: + GRPC_TIMER_MARK (STATE_1, i); + state[i]++; + break; + case 2: + GRPC_TIMER_MARK (STATE_2, i); + state[i]++; + break; + case 3: + break; + } + } + } } - } - free(state); + free (state); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - grpc_timers_global_init(); - test_log_events(1000000); - grpc_timers_global_destroy(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + grpc_timers_global_init (); + test_log_events (1000000); + grpc_timers_global_destroy (); return 0; } diff --git a/test/core/security/auth_context_test.c b/test/core/security/auth_context_test.c index d091c7e7e6..9b58b3533a 100644 --- a/test/core/security/auth_context_test.c +++ b/test/core/security/auth_context_test.c @@ -39,115 +39,113 @@ #include <grpc/support/log.h> -static void test_empty_context(void) { - grpc_auth_context *ctx = grpc_auth_context_create(NULL); +static void +test_empty_context (void) +{ + grpc_auth_context *ctx = grpc_auth_context_create (NULL); 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); - GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "bar") == - 0); - GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL); - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + 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); + GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, "bar") == 0); + GPR_ASSERT (grpc_auth_context_peer_identity_property_name (ctx) == NULL); + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -static void test_simple_context(void) { - grpc_auth_context *ctx = grpc_auth_context_create(NULL); +static void +test_simple_context (void) +{ + grpc_auth_context *ctx = grpc_auth_context_create (NULL); grpc_auth_property_iterator it; size_t i; - gpr_log(GPR_INFO, "test_simple_context"); - GPR_ASSERT(ctx != NULL); - grpc_auth_context_add_cstring_property(ctx, "name", "chapi"); - grpc_auth_context_add_cstring_property(ctx, "name", "chapo"); - grpc_auth_context_add_cstring_property(ctx, "foo", "bar"); - GPR_ASSERT(ctx->properties.count == 3); - GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "name") == - 1); - - 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->properties.count; i++) { - const grpc_auth_property *p = grpc_auth_property_iterator_next(&it); - GPR_ASSERT(p == &ctx->properties.array[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.array[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.array[0]); - GPR_ASSERT(grpc_auth_property_iterator_next(&it) == - &ctx->properties.array[1]); - GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); - - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + gpr_log (GPR_INFO, "test_simple_context"); + GPR_ASSERT (ctx != NULL); + grpc_auth_context_add_cstring_property (ctx, "name", "chapi"); + grpc_auth_context_add_cstring_property (ctx, "name", "chapo"); + grpc_auth_context_add_cstring_property (ctx, "foo", "bar"); + GPR_ASSERT (ctx->properties.count == 3); + GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, "name") == 1); + + 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->properties.count; i++) + { + const grpc_auth_property *p = grpc_auth_property_iterator_next (&it); + GPR_ASSERT (p == &ctx->properties.array[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.array[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.array[0]); + GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[1]); + GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL); + + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -static void test_chained_context(void) { - grpc_auth_context *chained = grpc_auth_context_create(NULL); - grpc_auth_context *ctx = grpc_auth_context_create(chained); +static void +test_chained_context (void) +{ + grpc_auth_context *chained = grpc_auth_context_create (NULL); + grpc_auth_context *ctx = grpc_auth_context_create (chained); grpc_auth_property_iterator it; size_t i; - gpr_log(GPR_INFO, "test_chained_context"); - GRPC_AUTH_CONTEXT_UNREF(chained, "chained"); - grpc_auth_context_add_cstring_property(chained, "name", "padapo"); - grpc_auth_context_add_cstring_property(chained, "foo", "baz"); - grpc_auth_context_add_cstring_property(ctx, "name", "chapi"); - grpc_auth_context_add_cstring_property(ctx, "name", "chap0"); - grpc_auth_context_add_cstring_property(ctx, "foo", "bar"); - GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "name") == - 1); - - 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->properties.count; i++) { - const grpc_auth_property *p = grpc_auth_property_iterator_next(&it); - GPR_ASSERT(p == &ctx->properties.array[i]); - } - for (i = 0; i < chained->properties.count; i++) { - const grpc_auth_property *p = grpc_auth_property_iterator_next(&it); - GPR_ASSERT(p == &chained->properties.array[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.array[2]); - GPR_ASSERT(grpc_auth_property_iterator_next(&it) == - &chained->properties.array[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.array[0]); - GPR_ASSERT(grpc_auth_property_iterator_next(&it) == - &ctx->properties.array[1]); - GPR_ASSERT(grpc_auth_property_iterator_next(&it) == - &chained->properties.array[0]); - GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL); - - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + gpr_log (GPR_INFO, "test_chained_context"); + GRPC_AUTH_CONTEXT_UNREF (chained, "chained"); + grpc_auth_context_add_cstring_property (chained, "name", "padapo"); + grpc_auth_context_add_cstring_property (chained, "foo", "baz"); + grpc_auth_context_add_cstring_property (ctx, "name", "chapi"); + grpc_auth_context_add_cstring_property (ctx, "name", "chap0"); + grpc_auth_context_add_cstring_property (ctx, "foo", "bar"); + GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, "name") == 1); + + 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->properties.count; i++) + { + const grpc_auth_property *p = grpc_auth_property_iterator_next (&it); + GPR_ASSERT (p == &ctx->properties.array[i]); + } + for (i = 0; i < chained->properties.count; i++) + { + const grpc_auth_property *p = grpc_auth_property_iterator_next (&it); + GPR_ASSERT (p == &chained->properties.array[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.array[2]); + GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &chained->properties.array[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.array[0]); + GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[1]); + GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &chained->properties.array[0]); + GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL); + + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_empty_context(); - test_simple_context(); - test_chained_context(); +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/base64_test.c b/test/core/security/base64_test.c index 6d49b6d1a1..a8471c69e3 100644 --- a/test/core/security/base64_test.c +++ b/test/core/security/base64_test.c @@ -40,184 +40,215 @@ #include <grpc/support/slice.h> #include "test/core/util/test_config.h" -static int buffers_are_equal(const unsigned char *buf1, - const unsigned char *buf2, size_t size) { +static int +buffers_are_equal (const unsigned char *buf1, const unsigned char *buf2, size_t size) +{ size_t i; - for (i = 0; i < size; i++) { - if (buf1[i] != buf2[i]) { - gpr_log(GPR_ERROR, "buf1 and buf2 differ: buf1[%d] = %x vs buf2[%d] = %x", - (int)i, buf1[i], (int)i, buf2[i]); - return 0; + for (i = 0; i < size; i++) + { + if (buf1[i] != buf2[i]) + { + gpr_log (GPR_ERROR, "buf1 and buf2 differ: buf1[%d] = %x vs buf2[%d] = %x", (int) i, buf1[i], (int) i, buf2[i]); + return 0; + } } - } return 1; } -static void test_simple_encode_decode_b64(int url_safe, int multiline) { + +static void +test_simple_encode_decode_b64 (int url_safe, int multiline) +{ const char *hello = "hello"; - char *hello_b64 = - grpc_base64_encode(hello, strlen(hello), url_safe, multiline); - gpr_slice hello_slice = grpc_base64_decode(hello_b64, url_safe); - GPR_ASSERT(GPR_SLICE_LENGTH(hello_slice) == strlen(hello)); - GPR_ASSERT(strncmp((const char *)GPR_SLICE_START_PTR(hello_slice), hello, - GPR_SLICE_LENGTH(hello_slice)) == 0); - - gpr_slice_unref(hello_slice); - gpr_free(hello_b64); + char *hello_b64 = grpc_base64_encode (hello, strlen (hello), url_safe, multiline); + gpr_slice hello_slice = grpc_base64_decode (hello_b64, url_safe); + GPR_ASSERT (GPR_SLICE_LENGTH (hello_slice) == strlen (hello)); + GPR_ASSERT (strncmp ((const char *) GPR_SLICE_START_PTR (hello_slice), hello, GPR_SLICE_LENGTH (hello_slice)) == 0); + + gpr_slice_unref (hello_slice); + gpr_free (hello_b64); } -static void test_full_range_encode_decode_b64(int url_safe, int multiline) { +static void +test_full_range_encode_decode_b64 (int url_safe, int multiline) +{ unsigned char orig[256]; size_t i; char *b64; gpr_slice orig_decoded; - for (i = 0; i < sizeof(orig); i++) orig[i] = (gpr_uint8)i; + for (i = 0; i < sizeof (orig); i++) + orig[i] = (gpr_uint8) i; /* Try all the different paddings. */ - for (i = 0; i < 3; i++) { - b64 = grpc_base64_encode(orig, sizeof(orig) - i, url_safe, multiline); - orig_decoded = grpc_base64_decode(b64, url_safe); - GPR_ASSERT(GPR_SLICE_LENGTH(orig_decoded) == (sizeof(orig) - i)); - GPR_ASSERT(buffers_are_equal(orig, GPR_SLICE_START_PTR(orig_decoded), - sizeof(orig) - i)); - gpr_slice_unref(orig_decoded); - gpr_free(b64); - } + for (i = 0; i < 3; i++) + { + b64 = grpc_base64_encode (orig, sizeof (orig) - i, url_safe, multiline); + orig_decoded = grpc_base64_decode (b64, url_safe); + GPR_ASSERT (GPR_SLICE_LENGTH (orig_decoded) == (sizeof (orig) - i)); + GPR_ASSERT (buffers_are_equal (orig, GPR_SLICE_START_PTR (orig_decoded), sizeof (orig) - i)); + gpr_slice_unref (orig_decoded); + gpr_free (b64); + } } -static void test_simple_encode_decode_b64_no_multiline(void) { - test_simple_encode_decode_b64(0, 0); +static void +test_simple_encode_decode_b64_no_multiline (void) +{ + test_simple_encode_decode_b64 (0, 0); } -static void test_simple_encode_decode_b64_multiline(void) { - test_simple_encode_decode_b64(0, 1); +static void +test_simple_encode_decode_b64_multiline (void) +{ + test_simple_encode_decode_b64 (0, 1); } -static void test_simple_encode_decode_b64_urlsafe_no_multiline(void) { - test_simple_encode_decode_b64(1, 0); +static void +test_simple_encode_decode_b64_urlsafe_no_multiline (void) +{ + test_simple_encode_decode_b64 (1, 0); } -static void test_simple_encode_decode_b64_urlsafe_multiline(void) { - test_simple_encode_decode_b64(1, 1); +static void +test_simple_encode_decode_b64_urlsafe_multiline (void) +{ + test_simple_encode_decode_b64 (1, 1); } -static void test_full_range_encode_decode_b64_no_multiline(void) { - test_full_range_encode_decode_b64(0, 0); +static void +test_full_range_encode_decode_b64_no_multiline (void) +{ + test_full_range_encode_decode_b64 (0, 0); } -static void test_full_range_encode_decode_b64_multiline(void) { - test_full_range_encode_decode_b64(0, 1); +static void +test_full_range_encode_decode_b64_multiline (void) +{ + test_full_range_encode_decode_b64 (0, 1); } -static void test_full_range_encode_decode_b64_urlsafe_no_multiline(void) { - test_full_range_encode_decode_b64(1, 0); +static void +test_full_range_encode_decode_b64_urlsafe_no_multiline (void) +{ + test_full_range_encode_decode_b64 (1, 0); } -static void test_full_range_encode_decode_b64_urlsafe_multiline(void) { - test_full_range_encode_decode_b64(1, 1); +static void +test_full_range_encode_decode_b64_urlsafe_multiline (void) +{ + test_full_range_encode_decode_b64 (1, 1); } -static void test_url_safe_unsafe_mismtach_failure(void) { +static void +test_url_safe_unsafe_mismtach_failure (void) +{ unsigned char orig[256]; size_t i; char *b64; gpr_slice orig_decoded; int url_safe = 1; - for (i = 0; i < sizeof(orig); i++) orig[i] = (gpr_uint8)i; - - b64 = grpc_base64_encode(orig, sizeof(orig), url_safe, 0); - orig_decoded = grpc_base64_decode(b64, !url_safe); - GPR_ASSERT(GPR_SLICE_IS_EMPTY(orig_decoded)); - gpr_free(b64); - gpr_slice_unref(orig_decoded); - - b64 = grpc_base64_encode(orig, sizeof(orig), !url_safe, 0); - orig_decoded = grpc_base64_decode(b64, url_safe); - GPR_ASSERT(GPR_SLICE_IS_EMPTY(orig_decoded)); - gpr_free(b64); - gpr_slice_unref(orig_decoded); + for (i = 0; i < sizeof (orig); i++) + orig[i] = (gpr_uint8) i; + + b64 = grpc_base64_encode (orig, sizeof (orig), url_safe, 0); + orig_decoded = grpc_base64_decode (b64, !url_safe); + GPR_ASSERT (GPR_SLICE_IS_EMPTY (orig_decoded)); + gpr_free (b64); + gpr_slice_unref (orig_decoded); + + b64 = grpc_base64_encode (orig, sizeof (orig), !url_safe, 0); + orig_decoded = grpc_base64_decode (b64, url_safe); + GPR_ASSERT (GPR_SLICE_IS_EMPTY (orig_decoded)); + gpr_free (b64); + gpr_slice_unref (orig_decoded); } -static void test_rfc4648_test_vectors(void) { +static void +test_rfc4648_test_vectors (void) +{ char *b64; - b64 = grpc_base64_encode("", 0, 0, 0); - GPR_ASSERT(strcmp("", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("", 0, 0, 0); + GPR_ASSERT (strcmp ("", b64) == 0); + gpr_free (b64); - b64 = grpc_base64_encode("f", 1, 0, 0); - GPR_ASSERT(strcmp("Zg==", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("f", 1, 0, 0); + GPR_ASSERT (strcmp ("Zg==", b64) == 0); + gpr_free (b64); - b64 = grpc_base64_encode("fo", 2, 0, 0); - GPR_ASSERT(strcmp("Zm8=", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("fo", 2, 0, 0); + GPR_ASSERT (strcmp ("Zm8=", b64) == 0); + gpr_free (b64); - b64 = grpc_base64_encode("foo", 3, 0, 0); - GPR_ASSERT(strcmp("Zm9v", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("foo", 3, 0, 0); + GPR_ASSERT (strcmp ("Zm9v", b64) == 0); + gpr_free (b64); - b64 = grpc_base64_encode("foob", 4, 0, 0); - GPR_ASSERT(strcmp("Zm9vYg==", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("foob", 4, 0, 0); + GPR_ASSERT (strcmp ("Zm9vYg==", b64) == 0); + gpr_free (b64); - b64 = grpc_base64_encode("fooba", 5, 0, 0); - GPR_ASSERT(strcmp("Zm9vYmE=", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("fooba", 5, 0, 0); + GPR_ASSERT (strcmp ("Zm9vYmE=", b64) == 0); + gpr_free (b64); - b64 = grpc_base64_encode("foobar", 6, 0, 0); - GPR_ASSERT(strcmp("Zm9vYmFy", b64) == 0); - gpr_free(b64); + b64 = grpc_base64_encode ("foobar", 6, 0, 0); + GPR_ASSERT (strcmp ("Zm9vYmFy", b64) == 0); + gpr_free (b64); } -static void test_unpadded_decode(void) { +static void +test_unpadded_decode (void) +{ gpr_slice decoded; - decoded = grpc_base64_decode("Zm9vYmFy", 0); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(decoded)); - GPR_ASSERT(gpr_slice_str_cmp(decoded, "foobar") == 0); - gpr_slice_unref(decoded); - - decoded = grpc_base64_decode("Zm9vYmE", 0); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(decoded)); - GPR_ASSERT(gpr_slice_str_cmp(decoded, "fooba") == 0); - gpr_slice_unref(decoded); - - decoded = grpc_base64_decode("Zm9vYg", 0); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(decoded)); - GPR_ASSERT(gpr_slice_str_cmp(decoded, "foob") == 0); - gpr_slice_unref(decoded); - - decoded = grpc_base64_decode("Zm9v", 0); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(decoded)); - GPR_ASSERT(gpr_slice_str_cmp(decoded, "foo") == 0); - gpr_slice_unref(decoded); - - decoded = grpc_base64_decode("Zm8", 0); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(decoded)); - GPR_ASSERT(gpr_slice_str_cmp(decoded, "fo") == 0); - gpr_slice_unref(decoded); - - decoded = grpc_base64_decode("Zg", 0); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(decoded)); - GPR_ASSERT(gpr_slice_str_cmp(decoded, "f") == 0); - gpr_slice_unref(decoded); - - decoded = grpc_base64_decode("", 0); - GPR_ASSERT(GPR_SLICE_IS_EMPTY(decoded)); + decoded = grpc_base64_decode ("Zm9vYmFy", 0); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (decoded)); + GPR_ASSERT (gpr_slice_str_cmp (decoded, "foobar") == 0); + gpr_slice_unref (decoded); + + decoded = grpc_base64_decode ("Zm9vYmE", 0); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (decoded)); + GPR_ASSERT (gpr_slice_str_cmp (decoded, "fooba") == 0); + gpr_slice_unref (decoded); + + decoded = grpc_base64_decode ("Zm9vYg", 0); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (decoded)); + GPR_ASSERT (gpr_slice_str_cmp (decoded, "foob") == 0); + gpr_slice_unref (decoded); + + decoded = grpc_base64_decode ("Zm9v", 0); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (decoded)); + GPR_ASSERT (gpr_slice_str_cmp (decoded, "foo") == 0); + gpr_slice_unref (decoded); + + decoded = grpc_base64_decode ("Zm8", 0); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (decoded)); + GPR_ASSERT (gpr_slice_str_cmp (decoded, "fo") == 0); + gpr_slice_unref (decoded); + + decoded = grpc_base64_decode ("Zg", 0); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (decoded)); + GPR_ASSERT (gpr_slice_str_cmp (decoded, "f") == 0); + gpr_slice_unref (decoded); + + decoded = grpc_base64_decode ("", 0); + GPR_ASSERT (GPR_SLICE_IS_EMPTY (decoded)); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_simple_encode_decode_b64_no_multiline(); - test_simple_encode_decode_b64_multiline(); - test_simple_encode_decode_b64_urlsafe_no_multiline(); - test_simple_encode_decode_b64_urlsafe_multiline(); - test_full_range_encode_decode_b64_no_multiline(); - test_full_range_encode_decode_b64_multiline(); - test_full_range_encode_decode_b64_urlsafe_no_multiline(); - test_full_range_encode_decode_b64_urlsafe_multiline(); - test_url_safe_unsafe_mismtach_failure(); - test_rfc4648_test_vectors(); - test_unpadded_decode(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_simple_encode_decode_b64_no_multiline (); + test_simple_encode_decode_b64_multiline (); + test_simple_encode_decode_b64_urlsafe_no_multiline (); + test_simple_encode_decode_b64_urlsafe_multiline (); + test_full_range_encode_decode_b64_no_multiline (); + test_full_range_encode_decode_b64_multiline (); + test_full_range_encode_decode_b64_urlsafe_no_multiline (); + test_full_range_encode_decode_b64_urlsafe_multiline (); + test_url_safe_unsafe_mismtach_failure (); + test_rfc4648_test_vectors (); + test_unpadded_decode (); return 0; } diff --git a/test/core/security/create_jwt.c b/test/core/security/create_jwt.c index b02469fb35..47cc26cefa 100644 --- a/test/core/security/create_jwt.c +++ b/test/core/security/create_jwt.c @@ -43,68 +43,69 @@ #include <grpc/support/log.h> #include <grpc/support/slice.h> -void create_jwt(const char *json_key_file_path, const char *service_url, - const char *scope) { +void +create_jwt (const char *json_key_file_path, const char *service_url, const char *scope) +{ grpc_auth_json_key key; int ok = 0; char *jwt; - gpr_slice json_key_data = gpr_load_file(json_key_file_path, 1, &ok); - if (!ok) { - fprintf(stderr, "Could not read %s.\n", json_key_file_path); - exit(1); - } - key = grpc_auth_json_key_create_from_string( - (const char *)GPR_SLICE_START_PTR(json_key_data)); - gpr_slice_unref(json_key_data); - if (!grpc_auth_json_key_is_valid(&key)) { - fprintf(stderr, "Could not parse json key.\n"); - exit(1); - } - jwt = grpc_jwt_encode_and_sign( - &key, service_url == NULL ? GRPC_JWT_OAUTH2_AUDIENCE : service_url, - grpc_max_auth_token_lifetime, scope); - grpc_auth_json_key_destruct(&key); - if (jwt == NULL) { - fprintf(stderr, "Could not create JWT.\n"); - exit(1); - } - fprintf(stdout, "%s\n", jwt); - gpr_free(jwt); + gpr_slice json_key_data = gpr_load_file (json_key_file_path, 1, &ok); + if (!ok) + { + fprintf (stderr, "Could not read %s.\n", json_key_file_path); + exit (1); + } + key = grpc_auth_json_key_create_from_string ((const char *) GPR_SLICE_START_PTR (json_key_data)); + gpr_slice_unref (json_key_data); + if (!grpc_auth_json_key_is_valid (&key)) + { + fprintf (stderr, "Could not parse json key.\n"); + exit (1); + } + jwt = grpc_jwt_encode_and_sign (&key, service_url == NULL ? GRPC_JWT_OAUTH2_AUDIENCE : service_url, grpc_max_auth_token_lifetime, scope); + grpc_auth_json_key_destruct (&key); + if (jwt == NULL) + { + fprintf (stderr, "Could not create JWT.\n"); + exit (1); + } + fprintf (stdout, "%s\n", jwt); + gpr_free (jwt); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ char *scope = NULL; char *json_key_file_path = NULL; char *service_url = NULL; - gpr_cmdline *cl = gpr_cmdline_create("create_jwt"); - gpr_cmdline_add_string(cl, "json_key", "File path of the json key.", - &json_key_file_path); - gpr_cmdline_add_string(cl, "scope", - "OPTIONAL Space delimited permissions. Mutually " - "exclusive with service_url", - &scope); - gpr_cmdline_add_string(cl, "service_url", - "OPTIONAL service URL. Mutually exclusive with scope.", - &service_url); - gpr_cmdline_parse(cl, argc, argv); + gpr_cmdline *cl = gpr_cmdline_create ("create_jwt"); + gpr_cmdline_add_string (cl, "json_key", "File path of the json key.", &json_key_file_path); + gpr_cmdline_add_string (cl, "scope", "OPTIONAL Space delimited permissions. Mutually " "exclusive with service_url", &scope); + gpr_cmdline_add_string (cl, "service_url", "OPTIONAL service URL. Mutually exclusive with scope.", &service_url); + gpr_cmdline_parse (cl, argc, argv); - if (json_key_file_path == NULL) { - fprintf(stderr, "Missing --json_key option.\n"); - exit(1); - } - if (scope != NULL) { - if (service_url != NULL) { - fprintf(stderr, - "Options --scope and --service_url are mutually exclusive.\n"); - exit(1); + if (json_key_file_path == NULL) + { + fprintf (stderr, "Missing --json_key option.\n"); + exit (1); + } + if (scope != NULL) + { + if (service_url != NULL) + { + fprintf (stderr, "Options --scope and --service_url are mutually exclusive.\n"); + exit (1); + } + } + else if (service_url == NULL) + { + fprintf (stderr, "Need one of --service_url or --scope options.\n"); + exit (1); } - } else if (service_url == NULL) { - fprintf(stderr, "Need one of --service_url or --scope options.\n"); - exit(1); - } - create_jwt(json_key_file_path, service_url, scope); + create_jwt (json_key_file_path, service_url, scope); - gpr_cmdline_destroy(cl); + gpr_cmdline_destroy (cl); return 0; } diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index e48aab95fa..6769de1a60 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -52,897 +52,797 @@ static const char test_google_iam_authorization_token[] = "blahblahblhahb"; static const char test_google_iam_authority_selector[] = "respectmyauthoritah"; -static const char test_oauth2_bearer_token[] = - "Bearer blaaslkdjfaslkdfasdsfasf"; +static const char test_oauth2_bearer_token[] = "Bearer blaaslkdjfaslkdfasdsfasf"; static const char test_root_cert[] = "I am the root!"; /* This JSON key was generated with the GCE console and revoked immediately. The identifiers have been changed as well. Maximum size for a string literal is 509 chars in C89, yay! */ -static const char test_json_key_str_part1[] = - "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" - "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" - "qg" - "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" - "rWBQvS4hle4LfijkP3J5BG+" - "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" - "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" - "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" - "8HpCqFYM9V8f34SBWfD4fRFT+n/" - "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; -static const char test_json_key_str_part2[] = - "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" - "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" - "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" - "G" - "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" - "A" - "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" - "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" - "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" - "Y" - "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; -static const char test_json_key_str_part3[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; +static const char test_json_key_str_part1[] = "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" "qg" "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" "rWBQvS4hle4LfijkP3J5BG+" "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" "8HpCqFYM9V8f34SBWfD4fRFT+n/" "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; +static const char test_json_key_str_part2[] = "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" "G" "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" "A" "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" "Y" "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; +static const char test_json_key_str_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; /* Test refresh token. */ -static const char test_refresh_token_str[] = - "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," - " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," - " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," - " \"type\": \"authorized_user\"}"; +static const char test_refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," " \"type\": \"authorized_user\"}"; -static const char valid_oauth2_json_response[] = - "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," - " \"expires_in\":3599, " - " \"token_type\":\"Bearer\"}"; +static const char valid_oauth2_json_response[] = "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," " \"expires_in\":3599, " " \"token_type\":\"Bearer\"}"; static const char test_user_data[] = "user data"; static const char test_scope[] = "perm1 perm2"; -static const char test_signed_jwt[] = - "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM4YW" - "U0MDcyZTViYTdmZDkwODg2YzcifQ"; +static const char test_signed_jwt[] = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM4YW" "U0MDcyZTViYTdmZDkwODg2YzcifQ"; static const char test_service_url[] = "https://foo.com/foo.v1"; static const char other_test_service_url[] = "https://bar.com/bar.v1"; -static char *test_json_key_str(void) { - size_t result_len = strlen(test_json_key_str_part1) + - strlen(test_json_key_str_part2) + - strlen(test_json_key_str_part3); - char *result = gpr_malloc(result_len + 1); +static char * +test_json_key_str (void) +{ + size_t result_len = strlen (test_json_key_str_part1) + strlen (test_json_key_str_part2) + strlen (test_json_key_str_part3); + char *result = gpr_malloc (result_len + 1); char *current = result; - strcpy(result, test_json_key_str_part1); - current += strlen(test_json_key_str_part1); - strcpy(current, test_json_key_str_part2); - current += strlen(test_json_key_str_part2); - strcpy(current, test_json_key_str_part3); + strcpy (result, test_json_key_str_part1); + current += strlen (test_json_key_str_part1); + strcpy (current, test_json_key_str_part2); + current += strlen (test_json_key_str_part2); + strcpy (current, test_json_key_str_part3); return result; } -typedef struct { +typedef struct +{ const char *key; const char *value; } expected_md; -static grpc_httpcli_response http_response(int status, const char *body) { +static grpc_httpcli_response +http_response (int status, const char *body) +{ grpc_httpcli_response response; - memset(&response, 0, sizeof(grpc_httpcli_response)); + memset (&response, 0, sizeof (grpc_httpcli_response)); response.status = status; - response.body = (char *)body; - response.body_length = strlen(body); + response.body = (char *) body; + response.body_length = strlen (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); +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); + 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); + 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); + 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) { +static void +test_oauth2_token_fetcher_creds_parsing_ok (void) +{ 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, &token_md, &token_lifetime) == GRPC_CREDENTIALS_OK); - GPR_ASSERT(token_lifetime.tv_sec == 3599); - GPR_ASSERT(token_lifetime.tv_nsec == 0); - 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_httpcli_response response = http_response (200, valid_oauth2_json_response); + GPR_ASSERT (grpc_oauth2_token_fetcher_credentials_parse_server_response (&response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_OK); + GPR_ASSERT (token_lifetime.tv_sec == 3599); + GPR_ASSERT (token_lifetime.tv_nsec == 0); + 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_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, &token_md, &token_lifetime) == - GRPC_CREDENTIALS_ERROR); + grpc_httpcli_response response = http_response (401, valid_oauth2_json_response); + GPR_ASSERT (grpc_oauth2_token_fetcher_credentials_parse_server_response (&response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); } -static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) { +static void +test_oauth2_token_fetcher_creds_parsing_empty_http_body (void) +{ 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, &token_md, &token_lifetime) == - GRPC_CREDENTIALS_ERROR); + grpc_httpcli_response response = http_response (200, ""); + GPR_ASSERT (grpc_oauth2_token_fetcher_credentials_parse_server_response (&response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); } -static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) { +static void +test_oauth2_token_fetcher_creds_parsing_invalid_json (void) +{ grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; - grpc_httpcli_response response = - http_response(200, - "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," - " \"expires_in\":3599, " - " \"token_type\":\"Bearer\""); - GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, &token_md, &token_lifetime) == - GRPC_CREDENTIALS_ERROR); + grpc_httpcli_response response = http_response (200, + "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," " \"expires_in\":3599, " " \"token_type\":\"Bearer\""); + GPR_ASSERT (grpc_oauth2_token_fetcher_credentials_parse_server_response (&response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); } -static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) { +static void +test_oauth2_token_fetcher_creds_parsing_missing_token (void) +{ 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, &token_md, &token_lifetime) == - GRPC_CREDENTIALS_ERROR); + grpc_httpcli_response response = http_response (200, + "{" " \"expires_in\":3599, " " \"token_type\":\"Bearer\"}"); + GPR_ASSERT (grpc_oauth2_token_fetcher_credentials_parse_server_response (&response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); } -static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) { +static void +test_oauth2_token_fetcher_creds_parsing_missing_token_type (void) +{ grpc_credentials_md_store *token_md = NULL; gpr_timespec token_lifetime; - grpc_httpcli_response response = - http_response(200, - "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," - " \"expires_in\":3599, " - "}"); - GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( - &response, &token_md, &token_lifetime) == - GRPC_CREDENTIALS_ERROR); -} - -static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime( - void) { + grpc_httpcli_response response = http_response (200, + "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," " \"expires_in\":3599, " "}"); + GPR_ASSERT (grpc_oauth2_token_fetcher_credentials_parse_server_response (&response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); +} + +static void +test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime (void) +{ 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, &token_md, &token_lifetime) == - GRPC_CREDENTIALS_ERROR); + 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, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); } -static void check_metadata(expected_md *expected, grpc_credentials_md *md_elems, - size_t num_md) { +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, expected[i].key)) { - GPR_ASSERT(gpr_slice_str_cmp(md_elems[j].value, expected[i].value) == - 0); - break; - } - } - if (j == num_md) { - gpr_log(GPR_ERROR, "key %s not found", expected[i].key); - GPR_ASSERT(0); + 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, expected[i].key)) + { + GPR_ASSERT (gpr_slice_str_cmp (md_elems[j].value, expected[i].value) == 0); + break; + } + } + if (j == num_md) + { + gpr_log (GPR_ERROR, "key %s not found", expected[i].key); + GPR_ASSERT (0); + } } - } -} - -static void check_google_iam_metadata(void *user_data, - grpc_credentials_md *md_elems, - size_t num_md, - grpc_credentials_status status, - grpc_closure_list *closure_list) { - grpc_credentials *c = (grpc_credentials *)user_data; - expected_md emd[] = {{GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, - test_google_iam_authorization_token}, - {GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, - test_google_iam_authority_selector}}; - GPR_ASSERT(status == GRPC_CREDENTIALS_OK); - GPR_ASSERT(num_md == 2); - check_metadata(emd, md_elems, num_md); - grpc_credentials_unref(c); -} - -static void test_google_iam_creds(void) { +} + +static void +check_google_iam_metadata (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + grpc_credentials *c = (grpc_credentials *) user_data; + expected_md emd[] = { {GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, + test_google_iam_authorization_token}, + {GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, + test_google_iam_authority_selector} + }; + GPR_ASSERT (status == GRPC_CREDENTIALS_OK); + GPR_ASSERT (num_md == 2); + check_metadata (emd, md_elems, num_md); + grpc_credentials_unref (c); +} + +static void +test_google_iam_creds (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *creds = grpc_google_iam_credentials_create( - test_google_iam_authorization_token, test_google_iam_authority_selector, - NULL); - GPR_ASSERT(grpc_credentials_has_request_metadata(creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(creds)); - grpc_credentials_get_request_metadata(creds, NULL, test_service_url, - check_google_iam_metadata, creds, - &closure_list); - grpc_closure_list_run(&closure_list); -} - -static void check_access_token_metadata(void *user_data, - grpc_credentials_md *md_elems, - size_t num_md, - grpc_credentials_status status, - grpc_closure_list *closure_list) { - grpc_credentials *c = (grpc_credentials *)user_data; - expected_md emd[] = {{GRPC_AUTHORIZATION_METADATA_KEY, "Bearer blah"}}; - GPR_ASSERT(status == GRPC_CREDENTIALS_OK); - GPR_ASSERT(num_md == 1); - check_metadata(emd, md_elems, num_md); - grpc_credentials_unref(c); -} - -static void test_access_token_creds(void) { + grpc_credentials *creds = grpc_google_iam_credentials_create (test_google_iam_authorization_token, test_google_iam_authority_selector, + NULL); + GPR_ASSERT (grpc_credentials_has_request_metadata (creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (creds)); + grpc_credentials_get_request_metadata (creds, NULL, test_service_url, check_google_iam_metadata, creds, &closure_list); + grpc_closure_list_run (&closure_list); +} + +static void +check_access_token_metadata (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + grpc_credentials *c = (grpc_credentials *) user_data; + expected_md emd[] = { {GRPC_AUTHORIZATION_METADATA_KEY, "Bearer blah"} }; + GPR_ASSERT (status == GRPC_CREDENTIALS_OK); + GPR_ASSERT (num_md == 1); + check_metadata (emd, md_elems, num_md); + grpc_credentials_unref (c); +} + +static void +test_access_token_creds (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *creds = grpc_access_token_credentials_create("blah", NULL); - GPR_ASSERT(grpc_credentials_has_request_metadata(creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(creds)); - GPR_ASSERT(strcmp(creds->type, GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); - grpc_credentials_get_request_metadata(creds, NULL, test_service_url, - check_access_token_metadata, creds, - &closure_list); - grpc_closure_list_run(&closure_list); -} - -static void check_ssl_oauth2_composite_metadata( - void *user_data, grpc_credentials_md *md_elems, size_t num_md, - grpc_credentials_status status, grpc_closure_list *closure_list) { - grpc_credentials *c = (grpc_credentials *)user_data; + grpc_credentials *creds = grpc_access_token_credentials_create ("blah", NULL); + GPR_ASSERT (grpc_credentials_has_request_metadata (creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (creds)); + GPR_ASSERT (strcmp (creds->type, GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); + grpc_credentials_get_request_metadata (creds, NULL, test_service_url, check_access_token_metadata, creds, &closure_list); + grpc_closure_list_run (&closure_list); +} + +static void +check_ssl_oauth2_composite_metadata (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + grpc_credentials *c = (grpc_credentials *) user_data; expected_md emd[] = { - {GRPC_AUTHORIZATION_METADATA_KEY, test_oauth2_bearer_token}}; - GPR_ASSERT(status == GRPC_CREDENTIALS_OK); - GPR_ASSERT(num_md == 1); - check_metadata(emd, md_elems, num_md); - grpc_credentials_unref(c); + {GRPC_AUTHORIZATION_METADATA_KEY, test_oauth2_bearer_token} + }; + GPR_ASSERT (status == GRPC_CREDENTIALS_OK); + GPR_ASSERT (num_md == 1); + check_metadata (emd, md_elems, num_md); + grpc_credentials_unref (c); } -static void test_ssl_oauth2_composite_creds(void) { +static void +test_ssl_oauth2_composite_creds (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *ssl_creds = - grpc_ssl_credentials_create(test_root_cert, NULL, NULL); + grpc_credentials *ssl_creds = grpc_ssl_credentials_create (test_root_cert, NULL, NULL); const grpc_credentials_array *creds_array; - grpc_credentials *oauth2_creds = grpc_md_only_test_credentials_create( - "Authorization", test_oauth2_bearer_token, 0); - grpc_credentials *composite_creds = - grpc_composite_credentials_create(ssl_creds, oauth2_creds, NULL); - grpc_credentials_unref(ssl_creds); - grpc_credentials_unref(oauth2_creds); - GPR_ASSERT(strcmp(composite_creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == - 0); - GPR_ASSERT(grpc_credentials_has_request_metadata(composite_creds)); - GPR_ASSERT(!grpc_credentials_has_request_metadata_only(composite_creds)); - creds_array = grpc_composite_credentials_get_credentials(composite_creds); - GPR_ASSERT(creds_array->num_creds == 2); - GPR_ASSERT(strcmp(creds_array->creds_array[0]->type, - GRPC_CREDENTIALS_TYPE_SSL) == 0); - GPR_ASSERT(strcmp(creds_array->creds_array[1]->type, - GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); - grpc_credentials_get_request_metadata(composite_creds, NULL, test_service_url, - check_ssl_oauth2_composite_metadata, - composite_creds, &closure_list); - grpc_closure_list_run(&closure_list); -} - -void test_ssl_fake_transport_security_composite_creds_failure(void) { - grpc_credentials *ssl_creds = grpc_ssl_credentials_create(NULL, NULL, NULL); - grpc_credentials *fake_transport_security_creds = - grpc_fake_transport_security_credentials_create(); + grpc_credentials *oauth2_creds = grpc_md_only_test_credentials_create ("Authorization", test_oauth2_bearer_token, 0); + grpc_credentials *composite_creds = grpc_composite_credentials_create (ssl_creds, oauth2_creds, NULL); + grpc_credentials_unref (ssl_creds); + grpc_credentials_unref (oauth2_creds); + GPR_ASSERT (strcmp (composite_creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0); + GPR_ASSERT (grpc_credentials_has_request_metadata (composite_creds)); + GPR_ASSERT (!grpc_credentials_has_request_metadata_only (composite_creds)); + creds_array = grpc_composite_credentials_get_credentials (composite_creds); + GPR_ASSERT (creds_array->num_creds == 2); + GPR_ASSERT (strcmp (creds_array->creds_array[0]->type, GRPC_CREDENTIALS_TYPE_SSL) == 0); + GPR_ASSERT (strcmp (creds_array->creds_array[1]->type, GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); + grpc_credentials_get_request_metadata (composite_creds, NULL, test_service_url, check_ssl_oauth2_composite_metadata, composite_creds, &closure_list); + grpc_closure_list_run (&closure_list); +} + +void +test_ssl_fake_transport_security_composite_creds_failure (void) +{ + grpc_credentials *ssl_creds = grpc_ssl_credentials_create (NULL, NULL, NULL); + grpc_credentials *fake_transport_security_creds = grpc_fake_transport_security_credentials_create (); /* 2 connector credentials: should not work. */ - GPR_ASSERT(grpc_composite_credentials_create( - ssl_creds, fake_transport_security_creds, NULL) == NULL); - grpc_credentials_unref(ssl_creds); - grpc_credentials_unref(fake_transport_security_creds); + GPR_ASSERT (grpc_composite_credentials_create (ssl_creds, fake_transport_security_creds, NULL) == NULL); + grpc_credentials_unref (ssl_creds); + grpc_credentials_unref (fake_transport_security_creds); } -static void check_ssl_oauth2_google_iam_composite_metadata( - void *user_data, grpc_credentials_md *md_elems, size_t num_md, - grpc_credentials_status status, grpc_closure_list *closure_list) { - grpc_credentials *c = (grpc_credentials *)user_data; +static void +check_ssl_oauth2_google_iam_composite_metadata (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + grpc_credentials *c = (grpc_credentials *) user_data; expected_md emd[] = { - {GRPC_AUTHORIZATION_METADATA_KEY, test_oauth2_bearer_token}, - {GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, - test_google_iam_authorization_token}, - {GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, - test_google_iam_authority_selector}}; - GPR_ASSERT(status == GRPC_CREDENTIALS_OK); - GPR_ASSERT(num_md == 3); - check_metadata(emd, md_elems, num_md); - grpc_credentials_unref(c); -} - -static void test_ssl_oauth2_google_iam_composite_creds(void) { + {GRPC_AUTHORIZATION_METADATA_KEY, test_oauth2_bearer_token}, + {GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, + test_google_iam_authorization_token}, + {GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, + test_google_iam_authority_selector} + }; + GPR_ASSERT (status == GRPC_CREDENTIALS_OK); + GPR_ASSERT (num_md == 3); + check_metadata (emd, md_elems, num_md); + grpc_credentials_unref (c); +} + +static void +test_ssl_oauth2_google_iam_composite_creds (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *ssl_creds = - grpc_ssl_credentials_create(test_root_cert, NULL, NULL); + grpc_credentials *ssl_creds = grpc_ssl_credentials_create (test_root_cert, NULL, NULL); const grpc_credentials_array *creds_array; - grpc_credentials *oauth2_creds = grpc_md_only_test_credentials_create( - "Authorization", test_oauth2_bearer_token, 0); - grpc_credentials *aux_creds = - grpc_composite_credentials_create(ssl_creds, oauth2_creds, NULL); - grpc_credentials *google_iam_creds = grpc_google_iam_credentials_create( - test_google_iam_authorization_token, test_google_iam_authority_selector, - NULL); - grpc_credentials *composite_creds = - grpc_composite_credentials_create(aux_creds, google_iam_creds, NULL); - grpc_credentials_unref(ssl_creds); - grpc_credentials_unref(oauth2_creds); - grpc_credentials_unref(aux_creds); - grpc_credentials_unref(google_iam_creds); - GPR_ASSERT(strcmp(composite_creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == - 0); - GPR_ASSERT(grpc_credentials_has_request_metadata(composite_creds)); - GPR_ASSERT(!grpc_credentials_has_request_metadata_only(composite_creds)); - creds_array = grpc_composite_credentials_get_credentials(composite_creds); - GPR_ASSERT(creds_array->num_creds == 3); - GPR_ASSERT(strcmp(creds_array->creds_array[0]->type, - GRPC_CREDENTIALS_TYPE_SSL) == 0); - GPR_ASSERT(strcmp(creds_array->creds_array[1]->type, - GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); - GPR_ASSERT(strcmp(creds_array->creds_array[2]->type, - GRPC_CREDENTIALS_TYPE_IAM) == 0); - grpc_credentials_get_request_metadata( - composite_creds, NULL, test_service_url, - check_ssl_oauth2_google_iam_composite_metadata, composite_creds, - &closure_list); - grpc_closure_list_run(&closure_list); -} - -static void on_oauth2_creds_get_metadata_success( - void *user_data, grpc_credentials_md *md_elems, size_t num_md, - grpc_credentials_status status, grpc_closure_list *closure_list) { - GPR_ASSERT(status == GRPC_CREDENTIALS_OK); - GPR_ASSERT(num_md == 1); - 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_credentials_md *md_elems, size_t num_md, - grpc_credentials_status status, grpc_closure_list *closure_list) { - GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR); - GPR_ASSERT(num_md == 0); - GPR_ASSERT(user_data != NULL); - GPR_ASSERT(strcmp((const char *)user_data, test_user_data) == 0); -} - -static void validate_compute_engine_http_request( - const grpc_httpcli_request *request) { - GPR_ASSERT(request->handshaker != &grpc_httpcli_ssl); - GPR_ASSERT(strcmp(request->host, "metadata") == 0); - GPR_ASSERT( - strcmp(request->path, - "/computeMetadata/v1/instance/service-accounts/default/token") == - 0); - GPR_ASSERT(request->hdr_count == 1); - GPR_ASSERT(strcmp(request->hdrs[0].key, "Metadata-Flavor") == 0); - GPR_ASSERT(strcmp(request->hdrs[0].value, "Google") == 0); -} - -static int compute_engine_httpcli_get_success_override( - const grpc_httpcli_request *request, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = - http_response(200, valid_oauth2_json_response); - validate_compute_engine_http_request(request); - on_response(user_data, &response, closure_list); + grpc_credentials *oauth2_creds = grpc_md_only_test_credentials_create ("Authorization", test_oauth2_bearer_token, 0); + grpc_credentials *aux_creds = grpc_composite_credentials_create (ssl_creds, oauth2_creds, NULL); + grpc_credentials *google_iam_creds = grpc_google_iam_credentials_create (test_google_iam_authorization_token, test_google_iam_authority_selector, + NULL); + grpc_credentials *composite_creds = grpc_composite_credentials_create (aux_creds, google_iam_creds, NULL); + grpc_credentials_unref (ssl_creds); + grpc_credentials_unref (oauth2_creds); + grpc_credentials_unref (aux_creds); + grpc_credentials_unref (google_iam_creds); + GPR_ASSERT (strcmp (composite_creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0); + GPR_ASSERT (grpc_credentials_has_request_metadata (composite_creds)); + GPR_ASSERT (!grpc_credentials_has_request_metadata_only (composite_creds)); + creds_array = grpc_composite_credentials_get_credentials (composite_creds); + GPR_ASSERT (creds_array->num_creds == 3); + GPR_ASSERT (strcmp (creds_array->creds_array[0]->type, GRPC_CREDENTIALS_TYPE_SSL) == 0); + GPR_ASSERT (strcmp (creds_array->creds_array[1]->type, GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); + GPR_ASSERT (strcmp (creds_array->creds_array[2]->type, GRPC_CREDENTIALS_TYPE_IAM) == 0); + grpc_credentials_get_request_metadata (composite_creds, NULL, test_service_url, check_ssl_oauth2_google_iam_composite_metadata, composite_creds, &closure_list); + grpc_closure_list_run (&closure_list); +} + +static void +on_oauth2_creds_get_metadata_success (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + GPR_ASSERT (status == GRPC_CREDENTIALS_OK); + GPR_ASSERT (num_md == 1); + 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_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + GPR_ASSERT (status == GRPC_CREDENTIALS_ERROR); + GPR_ASSERT (num_md == 0); + GPR_ASSERT (user_data != NULL); + GPR_ASSERT (strcmp ((const char *) user_data, test_user_data) == 0); +} + +static void +validate_compute_engine_http_request (const grpc_httpcli_request * request) +{ + GPR_ASSERT (request->handshaker != &grpc_httpcli_ssl); + GPR_ASSERT (strcmp (request->host, "metadata") == 0); + GPR_ASSERT (strcmp (request->path, "/computeMetadata/v1/instance/service-accounts/default/token") == 0); + GPR_ASSERT (request->hdr_count == 1); + GPR_ASSERT (strcmp (request->hdrs[0].key, "Metadata-Flavor") == 0); + GPR_ASSERT (strcmp (request->hdrs[0].value, "Google") == 0); +} + +static int +compute_engine_httpcli_get_success_override (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, valid_oauth2_json_response); + validate_compute_engine_http_request (request); + on_response (user_data, &response, closure_list); return 1; } -static int compute_engine_httpcli_get_failure_override( - const grpc_httpcli_request *request, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = http_response(403, "Not Authorized."); - validate_compute_engine_http_request(request); - on_response(user_data, &response, closure_list); +static int +compute_engine_httpcli_get_failure_override (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (403, "Not Authorized."); + validate_compute_engine_http_request (request); + on_response (user_data, &response, closure_list); return 1; } -static int httpcli_post_should_not_be_called( - const grpc_httpcli_request *request, const char *body_bytes, - size_t body_size, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - GPR_ASSERT("HTTP POST should not be called" == NULL); +static int +httpcli_post_should_not_be_called (const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + GPR_ASSERT ("HTTP POST should not be called" == NULL); return 1; } -static int httpcli_get_should_not_be_called( - const grpc_httpcli_request *request, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - GPR_ASSERT("HTTP GET should not be called" == NULL); +static int +httpcli_get_should_not_be_called (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + GPR_ASSERT ("HTTP GET should not be called" == NULL); return 1; } -static void test_compute_engine_creds_success(void) { +static void +test_compute_engine_creds_success (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *compute_engine_creds = - grpc_google_compute_engine_credentials_create(NULL); - GPR_ASSERT(grpc_credentials_has_request_metadata(compute_engine_creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(compute_engine_creds)); + grpc_credentials *compute_engine_creds = grpc_google_compute_engine_credentials_create (NULL); + GPR_ASSERT (grpc_credentials_has_request_metadata (compute_engine_creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (compute_engine_creds)); /* First request: http get should be called. */ - grpc_httpcli_set_override(compute_engine_httpcli_get_success_override, - httpcli_post_should_not_be_called); - grpc_credentials_get_request_metadata(compute_engine_creds, NULL, - test_service_url, - on_oauth2_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_httpcli_set_override (compute_engine_httpcli_get_success_override, httpcli_post_should_not_be_called); + grpc_credentials_get_request_metadata (compute_engine_creds, NULL, test_service_url, on_oauth2_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); /* Second request: the cached token should be served directly. */ - grpc_httpcli_set_override(httpcli_get_should_not_be_called, - httpcli_post_should_not_be_called); - grpc_credentials_get_request_metadata(compute_engine_creds, NULL, - test_service_url, - on_oauth2_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_httpcli_set_override (httpcli_get_should_not_be_called, httpcli_post_should_not_be_called); + grpc_credentials_get_request_metadata (compute_engine_creds, NULL, test_service_url, on_oauth2_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); - grpc_credentials_unref(compute_engine_creds); - grpc_httpcli_set_override(NULL, NULL); + grpc_credentials_unref (compute_engine_creds); + grpc_httpcli_set_override (NULL, NULL); } -static void test_compute_engine_creds_failure(void) { +static void +test_compute_engine_creds_failure (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *compute_engine_creds = - grpc_google_compute_engine_credentials_create(NULL); - grpc_httpcli_set_override(compute_engine_httpcli_get_failure_override, - httpcli_post_should_not_be_called); - GPR_ASSERT(grpc_credentials_has_request_metadata(compute_engine_creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(compute_engine_creds)); - grpc_credentials_get_request_metadata(compute_engine_creds, NULL, - test_service_url, - on_oauth2_creds_get_metadata_failure, - (void *)test_user_data, &closure_list); - grpc_credentials_unref(compute_engine_creds); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); -} - -static void validate_refresh_token_http_request( - const grpc_httpcli_request *request, const char *body, size_t body_size) { + grpc_credentials *compute_engine_creds = grpc_google_compute_engine_credentials_create (NULL); + grpc_httpcli_set_override (compute_engine_httpcli_get_failure_override, httpcli_post_should_not_be_called); + GPR_ASSERT (grpc_credentials_has_request_metadata (compute_engine_creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (compute_engine_creds)); + grpc_credentials_get_request_metadata (compute_engine_creds, NULL, test_service_url, on_oauth2_creds_get_metadata_failure, (void *) test_user_data, &closure_list); + grpc_credentials_unref (compute_engine_creds); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); +} + +static void +validate_refresh_token_http_request (const grpc_httpcli_request * request, const char *body, size_t body_size) +{ /* The content of the assertion is tested extensively in json_token_test. */ char *expected_body = NULL; - GPR_ASSERT(body != NULL); - GPR_ASSERT(body_size != 0); - gpr_asprintf(&expected_body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING, - "32555999999.apps.googleusercontent.com", - "EmssLNjJy1332hD4KFsecret", - "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42"); - GPR_ASSERT(strlen(expected_body) == body_size); - GPR_ASSERT(memcmp(expected_body, body, body_size) == 0); - gpr_free(expected_body); - GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl); - GPR_ASSERT(strcmp(request->host, GRPC_GOOGLE_OAUTH2_SERVICE_HOST) == 0); - GPR_ASSERT(strcmp(request->path, GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH) == 0); - GPR_ASSERT(request->hdr_count == 1); - GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); - GPR_ASSERT( - strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); -} - -static int refresh_token_httpcli_post_success( - const grpc_httpcli_request *request, const char *body, size_t body_size, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data, grpc_closure_list *closure_list) { - grpc_httpcli_response response = - http_response(200, valid_oauth2_json_response); - validate_refresh_token_http_request(request, body, body_size); - on_response(user_data, &response, closure_list); + GPR_ASSERT (body != NULL); + GPR_ASSERT (body_size != 0); + gpr_asprintf (&expected_body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING, "32555999999.apps.googleusercontent.com", "EmssLNjJy1332hD4KFsecret", "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42"); + GPR_ASSERT (strlen (expected_body) == body_size); + GPR_ASSERT (memcmp (expected_body, body, body_size) == 0); + gpr_free (expected_body); + GPR_ASSERT (request->handshaker == &grpc_httpcli_ssl); + GPR_ASSERT (strcmp (request->host, GRPC_GOOGLE_OAUTH2_SERVICE_HOST) == 0); + GPR_ASSERT (strcmp (request->path, GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH) == 0); + GPR_ASSERT (request->hdr_count == 1); + GPR_ASSERT (strcmp (request->hdrs[0].key, "Content-Type") == 0); + GPR_ASSERT (strcmp (request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); +} + +static int +refresh_token_httpcli_post_success (const grpc_httpcli_request * request, const char *body, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, valid_oauth2_json_response); + validate_refresh_token_http_request (request, body, body_size); + on_response (user_data, &response, closure_list); return 1; } -static int refresh_token_httpcli_post_failure( - const grpc_httpcli_request *request, const char *body, size_t body_size, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data, grpc_closure_list *closure_list) { - grpc_httpcli_response response = http_response(403, "Not Authorized."); - validate_refresh_token_http_request(request, body, body_size); - on_response(user_data, &response, closure_list); +static int +refresh_token_httpcli_post_failure (const grpc_httpcli_request * request, const char *body, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (403, "Not Authorized."); + validate_refresh_token_http_request (request, body, body_size); + on_response (user_data, &response, closure_list); return 1; } -static void test_refresh_token_creds_success(void) { +static void +test_refresh_token_creds_success (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *refresh_token_creds = - grpc_google_refresh_token_credentials_create(test_refresh_token_str, - NULL); - GPR_ASSERT(grpc_credentials_has_request_metadata(refresh_token_creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(refresh_token_creds)); + grpc_credentials *refresh_token_creds = grpc_google_refresh_token_credentials_create (test_refresh_token_str, + NULL); + GPR_ASSERT (grpc_credentials_has_request_metadata (refresh_token_creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (refresh_token_creds)); /* First request: http get should be called. */ - grpc_httpcli_set_override(httpcli_get_should_not_be_called, - refresh_token_httpcli_post_success); - grpc_credentials_get_request_metadata(refresh_token_creds, NULL, - test_service_url, - on_oauth2_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_httpcli_set_override (httpcli_get_should_not_be_called, refresh_token_httpcli_post_success); + grpc_credentials_get_request_metadata (refresh_token_creds, NULL, test_service_url, on_oauth2_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); /* Second request: the cached token should be served directly. */ - grpc_httpcli_set_override(httpcli_get_should_not_be_called, - httpcli_post_should_not_be_called); - grpc_credentials_get_request_metadata(refresh_token_creds, NULL, - test_service_url, - on_oauth2_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_httpcli_set_override (httpcli_get_should_not_be_called, httpcli_post_should_not_be_called); + grpc_credentials_get_request_metadata (refresh_token_creds, NULL, test_service_url, on_oauth2_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); - grpc_credentials_unref(refresh_token_creds); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + grpc_credentials_unref (refresh_token_creds); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static void test_refresh_token_creds_failure(void) { +static void +test_refresh_token_creds_failure (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *refresh_token_creds = - grpc_google_refresh_token_credentials_create(test_refresh_token_str, - NULL); - grpc_httpcli_set_override(httpcli_get_should_not_be_called, - refresh_token_httpcli_post_failure); - GPR_ASSERT(grpc_credentials_has_request_metadata(refresh_token_creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(refresh_token_creds)); - grpc_credentials_get_request_metadata(refresh_token_creds, NULL, - test_service_url, - on_oauth2_creds_get_metadata_failure, - (void *)test_user_data, &closure_list); - grpc_credentials_unref(refresh_token_creds); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); -} - -static void validate_jwt_encode_and_sign_params( - const grpc_auth_json_key *json_key, const char *scope, - gpr_timespec token_lifetime) { - GPR_ASSERT(grpc_auth_json_key_is_valid(json_key)); - GPR_ASSERT(json_key->private_key != NULL); - GPR_ASSERT(RSA_check_key(json_key->private_key)); - GPR_ASSERT(json_key->type != NULL && - strcmp(json_key->type, "service_account") == 0); - GPR_ASSERT(json_key->private_key_id != NULL && - strcmp(json_key->private_key_id, - "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0); - GPR_ASSERT(json_key->client_id != NULL && - strcmp(json_key->client_id, - "777-abaslkan11hlb6nmim3bpspl31ud.apps." - "googleusercontent.com") == 0); - GPR_ASSERT(json_key->client_email != NULL && - strcmp(json_key->client_email, - "777-abaslkan11hlb6nmim3bpspl31ud@developer." - "gserviceaccount.com") == 0); - if (scope != NULL) GPR_ASSERT(strcmp(scope, test_scope) == 0); - GPR_ASSERT(!gpr_time_cmp(token_lifetime, grpc_max_auth_token_lifetime)); -} - -static char *encode_and_sign_jwt_success(const grpc_auth_json_key *json_key, - const char *audience, - gpr_timespec token_lifetime, - const char *scope) { - validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime); - return gpr_strdup(test_signed_jwt); -} - -static char *encode_and_sign_jwt_failure(const grpc_auth_json_key *json_key, - const char *audience, - gpr_timespec token_lifetime, - const char *scope) { - validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime); + grpc_credentials *refresh_token_creds = grpc_google_refresh_token_credentials_create (test_refresh_token_str, + NULL); + grpc_httpcli_set_override (httpcli_get_should_not_be_called, refresh_token_httpcli_post_failure); + GPR_ASSERT (grpc_credentials_has_request_metadata (refresh_token_creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (refresh_token_creds)); + grpc_credentials_get_request_metadata (refresh_token_creds, NULL, test_service_url, on_oauth2_creds_get_metadata_failure, (void *) test_user_data, &closure_list); + grpc_credentials_unref (refresh_token_creds); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); +} + +static void +validate_jwt_encode_and_sign_params (const grpc_auth_json_key * json_key, const char *scope, gpr_timespec token_lifetime) +{ + GPR_ASSERT (grpc_auth_json_key_is_valid (json_key)); + GPR_ASSERT (json_key->private_key != NULL); + GPR_ASSERT (RSA_check_key (json_key->private_key)); + GPR_ASSERT (json_key->type != NULL && strcmp (json_key->type, "service_account") == 0); + GPR_ASSERT (json_key->private_key_id != NULL && strcmp (json_key->private_key_id, "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0); + GPR_ASSERT (json_key->client_id != NULL && strcmp (json_key->client_id, "777-abaslkan11hlb6nmim3bpspl31ud.apps." "googleusercontent.com") == 0); + GPR_ASSERT (json_key->client_email != NULL && strcmp (json_key->client_email, "777-abaslkan11hlb6nmim3bpspl31ud@developer." "gserviceaccount.com") == 0); + if (scope != NULL) + GPR_ASSERT (strcmp (scope, test_scope) == 0); + GPR_ASSERT (!gpr_time_cmp (token_lifetime, grpc_max_auth_token_lifetime)); +} + +static char * +encode_and_sign_jwt_success (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope) +{ + validate_jwt_encode_and_sign_params (json_key, scope, token_lifetime); + return gpr_strdup (test_signed_jwt); +} + +static char * +encode_and_sign_jwt_failure (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope) +{ + validate_jwt_encode_and_sign_params (json_key, scope, token_lifetime); return NULL; } -static char *encode_and_sign_jwt_should_not_be_called( - const grpc_auth_json_key *json_key, const char *audience, - gpr_timespec token_lifetime, const char *scope) { - GPR_ASSERT("grpc_jwt_encode_and_sign should not be called" == NULL); +static char * +encode_and_sign_jwt_should_not_be_called (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope) +{ + GPR_ASSERT ("grpc_jwt_encode_and_sign should not be called" == NULL); } -static void on_jwt_creds_get_metadata_success(void *user_data, - grpc_credentials_md *md_elems, - size_t num_md, - grpc_credentials_status status, - grpc_closure_list *closure_list) { +static void +on_jwt_creds_get_metadata_success (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ 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(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_credentials_md *md_elems, - size_t num_md, - grpc_credentials_status status, - grpc_closure_list *closure_list) { - GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR); - GPR_ASSERT(num_md == 0); - GPR_ASSERT(user_data != NULL); - GPR_ASSERT(strcmp((const char *)user_data, test_user_data) == 0); -} - -static void test_jwt_creds_success(void) { - char *json_key_string = test_json_key_str(); + gpr_asprintf (&expected_md_value, "Bearer %s", test_signed_jwt); + GPR_ASSERT (status == GRPC_CREDENTIALS_OK); + GPR_ASSERT (num_md == 1); + 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_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + GPR_ASSERT (status == GRPC_CREDENTIALS_ERROR); + GPR_ASSERT (num_md == 0); + GPR_ASSERT (user_data != NULL); + GPR_ASSERT (strcmp ((const char *) user_data, test_user_data) == 0); +} + +static void +test_jwt_creds_success (void) +{ + char *json_key_string = test_json_key_str (); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *jwt_creds = - grpc_service_account_jwt_access_credentials_create( - json_key_string, grpc_max_auth_token_lifetime, NULL); - GPR_ASSERT(grpc_credentials_has_request_metadata(jwt_creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(jwt_creds)); + grpc_credentials *jwt_creds = grpc_service_account_jwt_access_credentials_create (json_key_string, grpc_max_auth_token_lifetime, NULL); + GPR_ASSERT (grpc_credentials_has_request_metadata (jwt_creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (jwt_creds)); /* First request: jwt_encode_and_sign should be called. */ - grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success); - grpc_credentials_get_request_metadata(jwt_creds, NULL, test_service_url, - on_jwt_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_jwt_encode_and_sign_set_override (encode_and_sign_jwt_success); + grpc_credentials_get_request_metadata (jwt_creds, NULL, test_service_url, on_jwt_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); /* Second request: the cached token should be served directly. */ - grpc_jwt_encode_and_sign_set_override( - encode_and_sign_jwt_should_not_be_called); - grpc_credentials_get_request_metadata(jwt_creds, NULL, test_service_url, - on_jwt_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_jwt_encode_and_sign_set_override (encode_and_sign_jwt_should_not_be_called); + grpc_credentials_get_request_metadata (jwt_creds, NULL, test_service_url, on_jwt_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); /* Third request: Different service url so jwt_encode_and_sign should be called again (no caching). */ - grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success); - grpc_credentials_get_request_metadata(jwt_creds, NULL, other_test_service_url, - on_jwt_creds_get_metadata_success, - (void *)test_user_data, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_jwt_encode_and_sign_set_override (encode_and_sign_jwt_success); + grpc_credentials_get_request_metadata (jwt_creds, NULL, other_test_service_url, on_jwt_creds_get_metadata_success, (void *) test_user_data, &closure_list); + grpc_closure_list_run (&closure_list); - gpr_free(json_key_string); - grpc_credentials_unref(jwt_creds); - grpc_jwt_encode_and_sign_set_override(NULL); + gpr_free (json_key_string); + grpc_credentials_unref (jwt_creds); + grpc_jwt_encode_and_sign_set_override (NULL); } -static void test_jwt_creds_signing_failure(void) { - char *json_key_string = test_json_key_str(); +static void +test_jwt_creds_signing_failure (void) +{ + char *json_key_string = test_json_key_str (); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_credentials *jwt_creds = - grpc_service_account_jwt_access_credentials_create( - json_key_string, grpc_max_auth_token_lifetime, NULL); - GPR_ASSERT(grpc_credentials_has_request_metadata(jwt_creds)); - GPR_ASSERT(grpc_credentials_has_request_metadata_only(jwt_creds)); - - grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_failure); - grpc_credentials_get_request_metadata(jwt_creds, NULL, test_service_url, - on_jwt_creds_get_metadata_failure, - (void *)test_user_data, &closure_list); - - gpr_free(json_key_string); - grpc_credentials_unref(jwt_creds); - grpc_jwt_encode_and_sign_set_override(NULL); - grpc_closure_list_run(&closure_list); -} - -static void set_google_default_creds_env_var_with_file_contents( - const char *file_prefix, const char *contents) { - size_t contents_len = strlen(contents); - char *creds_file_name; - FILE *creds_file = gpr_tmpfile(file_prefix, &creds_file_name); - GPR_ASSERT(creds_file_name != NULL); - GPR_ASSERT(creds_file != NULL); - GPR_ASSERT(fwrite(contents, 1, contents_len, creds_file) == contents_len); - fclose(creds_file); - gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, creds_file_name); - gpr_free(creds_file_name); + grpc_credentials *jwt_creds = grpc_service_account_jwt_access_credentials_create (json_key_string, grpc_max_auth_token_lifetime, NULL); + GPR_ASSERT (grpc_credentials_has_request_metadata (jwt_creds)); + GPR_ASSERT (grpc_credentials_has_request_metadata_only (jwt_creds)); + + grpc_jwt_encode_and_sign_set_override (encode_and_sign_jwt_failure); + grpc_credentials_get_request_metadata (jwt_creds, NULL, test_service_url, on_jwt_creds_get_metadata_failure, (void *) test_user_data, &closure_list); + + gpr_free (json_key_string); + grpc_credentials_unref (jwt_creds); + grpc_jwt_encode_and_sign_set_override (NULL); + grpc_closure_list_run (&closure_list); } -static grpc_credentials *composite_inner_creds(grpc_credentials *creds, - const char *inner_creds_type) { +static void +set_google_default_creds_env_var_with_file_contents (const char *file_prefix, const char *contents) +{ + size_t contents_len = strlen (contents); + char *creds_file_name; + FILE *creds_file = gpr_tmpfile (file_prefix, &creds_file_name); + GPR_ASSERT (creds_file_name != NULL); + GPR_ASSERT (creds_file != NULL); + GPR_ASSERT (fwrite (contents, 1, contents_len, creds_file) == contents_len); + fclose (creds_file); + gpr_setenv (GRPC_GOOGLE_CREDENTIALS_ENV_VAR, creds_file_name); + gpr_free (creds_file_name); +} + +static grpc_credentials * +composite_inner_creds (grpc_credentials * creds, const char *inner_creds_type) +{ size_t i; grpc_composite_credentials *composite; - GPR_ASSERT(strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0); - composite = (grpc_composite_credentials *)creds; - for (i = 0; i < composite->inner.num_creds; i++) { - grpc_credentials *c = composite->inner.creds_array[i]; - if (strcmp(c->type, inner_creds_type) == 0) return c; - } - GPR_ASSERT(0); /* Not found. */ + GPR_ASSERT (strcmp (creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0); + composite = (grpc_composite_credentials *) creds; + for (i = 0; i < composite->inner.num_creds; i++) + { + grpc_credentials *c = composite->inner.creds_array[i]; + if (strcmp (c->type, inner_creds_type) == 0) + return c; + } + GPR_ASSERT (0); /* Not found. */ } -static void test_google_default_creds_auth_key(void) { +static void +test_google_default_creds_auth_key (void) +{ grpc_service_account_jwt_access_credentials *jwt; grpc_credentials *creds; - char *json_key = test_json_key_str(); - grpc_flush_cached_google_default_credentials(); - set_google_default_creds_env_var_with_file_contents( - "json_key_google_default_creds", json_key); - gpr_free(json_key); - creds = grpc_google_default_credentials_create(); - GPR_ASSERT(creds != NULL); - jwt = (grpc_service_account_jwt_access_credentials *)composite_inner_creds( - creds, GRPC_CREDENTIALS_TYPE_JWT); - GPR_ASSERT( - strcmp(jwt->key.client_id, - "777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent.com") == - 0); - grpc_credentials_unref(creds); - gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ -} - -static void test_google_default_creds_access_token(void) { + char *json_key = test_json_key_str (); + grpc_flush_cached_google_default_credentials (); + set_google_default_creds_env_var_with_file_contents ("json_key_google_default_creds", json_key); + gpr_free (json_key); + creds = grpc_google_default_credentials_create (); + GPR_ASSERT (creds != NULL); + jwt = (grpc_service_account_jwt_access_credentials *) composite_inner_creds (creds, GRPC_CREDENTIALS_TYPE_JWT); + GPR_ASSERT (strcmp (jwt->key.client_id, "777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent.com") == 0); + grpc_credentials_unref (creds); + gpr_setenv (GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ +} + +static void +test_google_default_creds_access_token (void) +{ grpc_google_refresh_token_credentials *refresh; grpc_credentials *creds; - grpc_flush_cached_google_default_credentials(); - set_google_default_creds_env_var_with_file_contents( - "refresh_token_google_default_creds", test_refresh_token_str); - creds = grpc_google_default_credentials_create(); - GPR_ASSERT(creds != NULL); - refresh = (grpc_google_refresh_token_credentials *)composite_inner_creds( - creds, GRPC_CREDENTIALS_TYPE_OAUTH2); - GPR_ASSERT(strcmp(refresh->refresh_token.client_id, - "32555999999.apps.googleusercontent.com") == 0); - grpc_credentials_unref(creds); - gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ -} - -typedef enum { + grpc_flush_cached_google_default_credentials (); + set_google_default_creds_env_var_with_file_contents ("refresh_token_google_default_creds", test_refresh_token_str); + creds = grpc_google_default_credentials_create (); + GPR_ASSERT (creds != NULL); + refresh = (grpc_google_refresh_token_credentials *) composite_inner_creds (creds, GRPC_CREDENTIALS_TYPE_OAUTH2); + GPR_ASSERT (strcmp (refresh->refresh_token.client_id, "32555999999.apps.googleusercontent.com") == 0); + grpc_credentials_unref (creds); + gpr_setenv (GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ +} + +typedef enum +{ PLUGIN_INITIAL_STATE, PLUGIN_GET_METADATA_CALLED_STATE, PLUGIN_DESTROY_CALLED_STATE } plugin_state; -typedef struct { +typedef struct +{ const char *key; const char *value; } plugin_metadata; -static const plugin_metadata plugin_md[] = {{"foo", "bar"}, {"hi", "there"}}; +static const plugin_metadata plugin_md[] = { {"foo", "bar"}, {"hi", "there"} }; -static void plugin_get_metadata_success(void *state, const char *service_url, - grpc_credentials_plugin_metadata_cb cb, - void *user_data) { +static void +plugin_get_metadata_success (void *state, const char *service_url, grpc_credentials_plugin_metadata_cb cb, void *user_data) +{ size_t i; - grpc_metadata md[GPR_ARRAY_SIZE(plugin_md)]; - plugin_state *s = (plugin_state *)state; - GPR_ASSERT(strcmp(service_url, test_service_url) == 0); + grpc_metadata md[GPR_ARRAY_SIZE (plugin_md)]; + plugin_state *s = (plugin_state *) state; + GPR_ASSERT (strcmp (service_url, test_service_url) == 0); *s = PLUGIN_GET_METADATA_CALLED_STATE; - for (i = 0; i < GPR_ARRAY_SIZE(plugin_md); i++) { - memset(&md[i], 0, sizeof(grpc_metadata)); - md[i].key = plugin_md[i].key; - md[i].value = plugin_md[i].value; - md[i].value_length = strlen(plugin_md[i].value); - } - cb(user_data, md, GPR_ARRAY_SIZE(md), GRPC_STATUS_OK, NULL); -} - -static void plugin_get_metadata_failure(void *state, const char *service_url, - grpc_credentials_plugin_metadata_cb cb, - void *user_data) { - plugin_state *s = (plugin_state *)state; - GPR_ASSERT(strcmp(service_url, test_service_url) == 0); + for (i = 0; i < GPR_ARRAY_SIZE (plugin_md); i++) + { + memset (&md[i], 0, sizeof (grpc_metadata)); + md[i].key = plugin_md[i].key; + md[i].value = plugin_md[i].value; + md[i].value_length = strlen (plugin_md[i].value); + } + cb (user_data, md, GPR_ARRAY_SIZE (md), GRPC_STATUS_OK, NULL); +} + +static void +plugin_get_metadata_failure (void *state, const char *service_url, grpc_credentials_plugin_metadata_cb cb, void *user_data) +{ + plugin_state *s = (plugin_state *) state; + GPR_ASSERT (strcmp (service_url, test_service_url) == 0); *s = PLUGIN_GET_METADATA_CALLED_STATE; - cb(user_data, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, - "Could not get metadata for plugin."); + cb (user_data, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, "Could not get metadata for plugin."); } -static void on_plugin_metadata_received_success( - void *user_data, grpc_credentials_md *md_elems, size_t num_md, - grpc_credentials_status status, grpc_closure_list *closure_list) { +static void +on_plugin_metadata_received_success (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ size_t i = 0; - GPR_ASSERT(user_data == NULL); - GPR_ASSERT(md_elems != NULL); - GPR_ASSERT(num_md == GPR_ARRAY_SIZE(plugin_md)); - for (i = 0; i < num_md; i++) { - GPR_ASSERT(gpr_slice_str_cmp(md_elems[i].key, plugin_md[i].key) == 0); - GPR_ASSERT(gpr_slice_str_cmp(md_elems[i].value, plugin_md[i].value) == 0); - } -} - -static void on_plugin_metadata_received_failure( - void *user_data, grpc_credentials_md *md_elems, size_t num_md, - grpc_credentials_status status, grpc_closure_list *closure_list) { - GPR_ASSERT(user_data == NULL); - GPR_ASSERT(md_elems == NULL); - GPR_ASSERT(num_md == 0); - GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR); -} - -static void plugin_destroy(void *state) { - plugin_state *s = (plugin_state *)state; + GPR_ASSERT (user_data == NULL); + GPR_ASSERT (md_elems != NULL); + GPR_ASSERT (num_md == GPR_ARRAY_SIZE (plugin_md)); + for (i = 0; i < num_md; i++) + { + GPR_ASSERT (gpr_slice_str_cmp (md_elems[i].key, plugin_md[i].key) == 0); + GPR_ASSERT (gpr_slice_str_cmp (md_elems[i].value, plugin_md[i].value) == 0); + } +} + +static void +on_plugin_metadata_received_failure (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ + GPR_ASSERT (user_data == NULL); + GPR_ASSERT (md_elems == NULL); + GPR_ASSERT (num_md == 0); + GPR_ASSERT (status == GRPC_CREDENTIALS_ERROR); +} + +static void +plugin_destroy (void *state) +{ + plugin_state *s = (plugin_state *) state; *s = PLUGIN_DESTROY_CALLED_STATE; } -static void test_metadata_plugin_success(void) { +static void +test_metadata_plugin_success (void) +{ grpc_credentials *creds; plugin_state state = PLUGIN_INITIAL_STATE; grpc_metadata_credentials_plugin plugin; @@ -952,18 +852,18 @@ static void test_metadata_plugin_success(void) { plugin.get_metadata = plugin_get_metadata_success; plugin.destroy = plugin_destroy; - creds = grpc_metadata_credentials_create_from_plugin(plugin, NULL); - GPR_ASSERT(state == PLUGIN_INITIAL_STATE); - grpc_credentials_get_request_metadata(creds, NULL, test_service_url, - on_plugin_metadata_received_success, - NULL, &closure_list); - GPR_ASSERT(state == PLUGIN_GET_METADATA_CALLED_STATE); - grpc_credentials_release(creds); - GPR_ASSERT(state == PLUGIN_DESTROY_CALLED_STATE); - grpc_closure_list_run(&closure_list); + creds = grpc_metadata_credentials_create_from_plugin (plugin, NULL); + GPR_ASSERT (state == PLUGIN_INITIAL_STATE); + grpc_credentials_get_request_metadata (creds, NULL, test_service_url, on_plugin_metadata_received_success, NULL, &closure_list); + GPR_ASSERT (state == PLUGIN_GET_METADATA_CALLED_STATE); + grpc_credentials_release (creds); + GPR_ASSERT (state == PLUGIN_DESTROY_CALLED_STATE); + grpc_closure_list_run (&closure_list); } -static void test_metadata_plugin_failure(void) { +static void +test_metadata_plugin_failure (void) +{ grpc_credentials *creds; plugin_state state = PLUGIN_INITIAL_STATE; grpc_metadata_credentials_plugin plugin; @@ -973,45 +873,45 @@ static void test_metadata_plugin_failure(void) { plugin.get_metadata = plugin_get_metadata_failure; plugin.destroy = plugin_destroy; - creds = grpc_metadata_credentials_create_from_plugin(plugin, NULL); - GPR_ASSERT(state == PLUGIN_INITIAL_STATE); - grpc_credentials_get_request_metadata(creds, NULL, test_service_url, - on_plugin_metadata_received_failure, - NULL, &closure_list); - GPR_ASSERT(state == PLUGIN_GET_METADATA_CALLED_STATE); - grpc_credentials_release(creds); - GPR_ASSERT(state == PLUGIN_DESTROY_CALLED_STATE); - grpc_closure_list_run(&closure_list); -} - -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(); - test_oauth2_token_fetcher_creds_parsing_invalid_json(); - test_oauth2_token_fetcher_creds_parsing_missing_token(); - test_oauth2_token_fetcher_creds_parsing_missing_token_type(); - test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime(); - test_google_iam_creds(); - test_access_token_creds(); - test_ssl_oauth2_composite_creds(); - test_ssl_oauth2_google_iam_composite_creds(); - test_compute_engine_creds_success(); - test_compute_engine_creds_failure(); - test_refresh_token_creds_success(); - test_refresh_token_creds_failure(); - test_jwt_creds_success(); - test_jwt_creds_signing_failure(); - test_google_default_creds_auth_key(); - test_google_default_creds_access_token(); - test_metadata_plugin_success(); - test_metadata_plugin_failure(); + creds = grpc_metadata_credentials_create_from_plugin (plugin, NULL); + GPR_ASSERT (state == PLUGIN_INITIAL_STATE); + grpc_credentials_get_request_metadata (creds, NULL, test_service_url, on_plugin_metadata_received_failure, NULL, &closure_list); + GPR_ASSERT (state == PLUGIN_GET_METADATA_CALLED_STATE); + grpc_credentials_release (creds); + GPR_ASSERT (state == PLUGIN_DESTROY_CALLED_STATE); + grpc_closure_list_run (&closure_list); +} + +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 (); + test_oauth2_token_fetcher_creds_parsing_invalid_json (); + test_oauth2_token_fetcher_creds_parsing_missing_token (); + test_oauth2_token_fetcher_creds_parsing_missing_token_type (); + test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime (); + test_google_iam_creds (); + test_access_token_creds (); + test_ssl_oauth2_composite_creds (); + test_ssl_oauth2_google_iam_composite_creds (); + test_compute_engine_creds_success (); + test_compute_engine_creds_failure (); + test_refresh_token_creds_success (); + test_refresh_token_creds_failure (); + test_jwt_creds_success (); + test_jwt_creds_signing_failure (); + test_google_default_creds_auth_key (); + test_google_default_creds_access_token (); + test_metadata_plugin_success (); + test_metadata_plugin_failure (); return 0; } diff --git a/test/core/security/fetch_oauth2.c b/test/core/security/fetch_oauth2.c index 764d8da9b6..529d455b85 100644 --- a/test/core/security/fetch_oauth2.c +++ b/test/core/security/fetch_oauth2.c @@ -46,77 +46,78 @@ #include "src/core/support/file.h" #include "test/core/security/oauth2_utils.h" -static grpc_credentials *create_refresh_token_creds( - const char *json_refresh_token_file_path) { +static grpc_credentials * +create_refresh_token_creds (const char *json_refresh_token_file_path) +{ int success; - gpr_slice refresh_token = - gpr_load_file(json_refresh_token_file_path, 1, &success); - if (!success) { - gpr_log(GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path); - exit(1); - } - return grpc_google_refresh_token_credentials_create( - (const char *)GPR_SLICE_START_PTR(refresh_token), NULL); + gpr_slice refresh_token = gpr_load_file (json_refresh_token_file_path, 1, &success); + if (!success) + { + gpr_log (GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path); + exit (1); + } + return grpc_google_refresh_token_credentials_create ((const char *) GPR_SLICE_START_PTR (refresh_token), NULL); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_credentials *creds = NULL; char *json_key_file_path = NULL; char *json_refresh_token_file_path = NULL; char *token = NULL; int use_gce = 0; char *scope = NULL; - gpr_cmdline *cl = gpr_cmdline_create("fetch_oauth2"); - gpr_cmdline_add_string(cl, "json_refresh_token", - "File path of the json refresh token.", - &json_refresh_token_file_path); - gpr_cmdline_add_flag( - cl, "gce", - "Get a token from the GCE metadata server (only works in GCE).", - &use_gce); - gpr_cmdline_parse(cl, argc, argv); + gpr_cmdline *cl = gpr_cmdline_create ("fetch_oauth2"); + gpr_cmdline_add_string (cl, "json_refresh_token", "File path of the json refresh token.", &json_refresh_token_file_path); + gpr_cmdline_add_flag (cl, "gce", "Get a token from the GCE metadata server (only works in GCE).", &use_gce); + gpr_cmdline_parse (cl, argc, argv); - grpc_init(); + grpc_init (); - if (json_key_file_path != NULL && json_refresh_token_file_path != NULL) { - gpr_log(GPR_ERROR, - "--json_key and --json_refresh_token are mutually exclusive."); - exit(1); - } + if (json_key_file_path != NULL && json_refresh_token_file_path != NULL) + { + gpr_log (GPR_ERROR, "--json_key and --json_refresh_token are mutually exclusive."); + exit (1); + } - if (use_gce) { - if (json_key_file_path != NULL || scope != NULL) { - gpr_log(GPR_INFO, - "Ignoring json key and scope to get a token from the GCE " - "metadata server."); + if (use_gce) + { + if (json_key_file_path != NULL || scope != NULL) + { + gpr_log (GPR_INFO, "Ignoring json key and scope to get a token from the GCE " "metadata server."); + } + creds = grpc_google_compute_engine_credentials_create (NULL); + if (creds == NULL) + { + gpr_log (GPR_ERROR, "Could not create gce credentials."); + exit (1); + } } - creds = grpc_google_compute_engine_credentials_create(NULL); - if (creds == NULL) { - gpr_log(GPR_ERROR, "Could not create gce credentials."); - exit(1); + else if (json_refresh_token_file_path != NULL) + { + creds = create_refresh_token_creds (json_refresh_token_file_path); + if (creds == NULL) + { + gpr_log (GPR_ERROR, "Could not create refresh token creds. %s does probably not " "contain a valid json refresh token.", json_refresh_token_file_path); + exit (1); + } } - } else if (json_refresh_token_file_path != NULL) { - creds = create_refresh_token_creds(json_refresh_token_file_path); - if (creds == NULL) { - gpr_log(GPR_ERROR, - "Could not create refresh token creds. %s does probably not " - "contain a valid json refresh token.", - json_refresh_token_file_path); - exit(1); + else + { + gpr_log (GPR_ERROR, "Missing --gce or --json_refresh_token option."); + exit (1); } - } else { - gpr_log(GPR_ERROR, "Missing --gce or --json_refresh_token option."); - exit(1); - } - GPR_ASSERT(creds != NULL); + GPR_ASSERT (creds != NULL); - token = grpc_test_fetch_oauth2_token_with_credentials(creds); - if (token != NULL) { - printf("Got token: %s.\n", token); - gpr_free(token); - } - grpc_credentials_release(creds); - gpr_cmdline_destroy(cl); - grpc_shutdown(); + token = grpc_test_fetch_oauth2_token_with_credentials (creds); + if (token != NULL) + { + printf ("Got token: %s.\n", token); + gpr_free (token); + } + grpc_credentials_release (creds); + gpr_cmdline_destroy (cl); + grpc_shutdown (); return 0; } diff --git a/test/core/security/json_token_test.c b/test/core/security/json_token_test.c index 740fd018b6..d57340f107 100644 --- a/test/core/security/json_token_test.c +++ b/test/core/security/json_token_test.c @@ -47,230 +47,174 @@ /* This JSON key was generated with the GCE console and revoked immediately. The identifiers have been changed as well. Maximum size for a string literal is 509 chars in C89, yay! */ -static const char test_json_key_str_part1[] = - "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" - "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" - "qg" - "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" - "rWBQvS4hle4LfijkP3J5BG+" - "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" - "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" - "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" - "8HpCqFYM9V8f34SBWfD4fRFT+n/" - "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; -static const char test_json_key_str_part2[] = - "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" - "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" - "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" - "G" - "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" - "A" - "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" - "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" - "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" - "Y" - "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; -static const char test_json_key_str_part3[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; +static const char test_json_key_str_part1[] = "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" "qg" "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" "rWBQvS4hle4LfijkP3J5BG+" "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" "8HpCqFYM9V8f34SBWfD4fRFT+n/" "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; +static const char test_json_key_str_part2[] = "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" "G" "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" "A" "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" "Y" "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; +static const char test_json_key_str_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; /* Test refresh token. */ -static const char test_refresh_token_str[] = - "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," - " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," - " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," - " \"type\": \"authorized_user\"}"; +static const char test_refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," " \"type\": \"authorized_user\"}"; static const char test_scope[] = "myperm1 myperm2"; static const char test_service_url[] = "https://foo.com/foo.v1"; -static char *test_json_key_str(const char *bad_part3) { +static char * +test_json_key_str (const char *bad_part3) +{ const char *part3 = bad_part3 != NULL ? bad_part3 : test_json_key_str_part3; - size_t result_len = strlen(test_json_key_str_part1) + - strlen(test_json_key_str_part2) + strlen(part3); - char *result = gpr_malloc(result_len + 1); + size_t result_len = strlen (test_json_key_str_part1) + strlen (test_json_key_str_part2) + strlen (part3); + char *result = gpr_malloc (result_len + 1); char *current = result; - strcpy(result, test_json_key_str_part1); - current += strlen(test_json_key_str_part1); - strcpy(current, test_json_key_str_part2); - current += strlen(test_json_key_str_part2); - strcpy(current, part3); + strcpy (result, test_json_key_str_part1); + current += strlen (test_json_key_str_part1); + strcpy (current, test_json_key_str_part2); + current += strlen (test_json_key_str_part2); + strcpy (current, part3); return result; } -static void test_parse_json_key_success(void) { - char *json_string = test_json_key_str(NULL); - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); - GPR_ASSERT(grpc_auth_json_key_is_valid(&json_key)); - GPR_ASSERT(json_key.type != NULL && - strcmp(json_key.type, "service_account") == 0); - GPR_ASSERT(json_key.private_key_id != NULL && - strcmp(json_key.private_key_id, - "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0); - GPR_ASSERT(json_key.client_id != NULL && - strcmp(json_key.client_id, - "777-abaslkan11hlb6nmim3bpspl31ud.apps." - "googleusercontent.com") == 0); - GPR_ASSERT(json_key.client_email != NULL && - strcmp(json_key.client_email, - "777-abaslkan11hlb6nmim3bpspl31ud@developer." - "gserviceaccount.com") == 0); - GPR_ASSERT(json_key.private_key != NULL); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_success (void) +{ + char *json_string = test_json_key_str (NULL); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); + GPR_ASSERT (grpc_auth_json_key_is_valid (&json_key)); + GPR_ASSERT (json_key.type != NULL && strcmp (json_key.type, "service_account") == 0); + GPR_ASSERT (json_key.private_key_id != NULL && strcmp (json_key.private_key_id, "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0); + GPR_ASSERT (json_key.client_id != NULL && strcmp (json_key.client_id, "777-abaslkan11hlb6nmim3bpspl31ud.apps." "googleusercontent.com") == 0); + GPR_ASSERT (json_key.client_email != NULL && strcmp (json_key.client_email, "777-abaslkan11hlb6nmim3bpspl31ud@developer." "gserviceaccount.com") == 0); + GPR_ASSERT (json_key.private_key != NULL); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); } -static void test_parse_json_key_failure_bad_json(void) { - const char non_closing_part3[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" "; - char *json_string = test_json_key_str(non_closing_part3); - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); - GPR_ASSERT(!grpc_auth_json_key_is_valid(&json_key)); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_failure_bad_json (void) +{ + const char non_closing_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" "; + char *json_string = test_json_key_str (non_closing_part3); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); + GPR_ASSERT (!grpc_auth_json_key_is_valid (&json_key)); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); } -static void test_parse_json_key_failure_no_type(void) { - const char no_type_part3[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\" }"; - char *json_string = test_json_key_str(no_type_part3); - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); - GPR_ASSERT(!grpc_auth_json_key_is_valid(&json_key)); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_failure_no_type (void) +{ + const char no_type_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\" }"; + char *json_string = test_json_key_str (no_type_part3); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); + GPR_ASSERT (!grpc_auth_json_key_is_valid (&json_key)); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); } -static void test_parse_json_key_failure_no_client_id(void) { - const char no_client_id_part3[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", " - "\"type\": \"service_account\" }"; - char *json_string = test_json_key_str(no_client_id_part3); - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); - GPR_ASSERT(!grpc_auth_json_key_is_valid(&json_key)); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_failure_no_client_id (void) +{ + const char no_client_id_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", " "\"type\": \"service_account\" }"; + char *json_string = test_json_key_str (no_client_id_part3); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); + GPR_ASSERT (!grpc_auth_json_key_is_valid (&json_key)); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); } -static void test_parse_json_key_failure_no_client_email(void) { - const char no_client_email_part3[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; - char *json_string = test_json_key_str(no_client_email_part3); - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); - GPR_ASSERT(!grpc_auth_json_key_is_valid(&json_key)); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_failure_no_client_email (void) +{ + const char no_client_email_part3[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; + char *json_string = test_json_key_str (no_client_email_part3); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); + GPR_ASSERT (!grpc_auth_json_key_is_valid (&json_key)); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); } -static void test_parse_json_key_failure_no_private_key_id(void) { - const char no_private_key_id_part3[] = - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; - char *json_string = test_json_key_str(no_private_key_id_part3); - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); - GPR_ASSERT(!grpc_auth_json_key_is_valid(&json_key)); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_failure_no_private_key_id (void) +{ + const char no_private_key_id_part3[] = "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; + char *json_string = test_json_key_str (no_private_key_id_part3); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); + GPR_ASSERT (!grpc_auth_json_key_is_valid (&json_key)); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); } -static void test_parse_json_key_failure_no_private_key(void) { - const char no_private_key_json_string[] = - "{ \"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(no_private_key_json_string); - GPR_ASSERT(!grpc_auth_json_key_is_valid(&json_key)); - grpc_auth_json_key_destruct(&json_key); +static void +test_parse_json_key_failure_no_private_key (void) +{ + const char no_private_key_json_string[] = "{ \"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (no_private_key_json_string); + GPR_ASSERT (!grpc_auth_json_key_is_valid (&json_key)); + grpc_auth_json_key_destruct (&json_key); } -static grpc_json *parse_json_part_from_jwt(const char *str, size_t len, - char **scratchpad) { +static grpc_json * +parse_json_part_from_jwt (const char *str, size_t len, char **scratchpad) +{ char *b64; char *decoded; grpc_json *json; gpr_slice slice; - b64 = gpr_malloc(len + 1); - strncpy(b64, str, len); + b64 = gpr_malloc (len + 1); + strncpy (b64, str, len); b64[len] = '\0'; - slice = grpc_base64_decode(b64, 1); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(slice)); - decoded = gpr_malloc(GPR_SLICE_LENGTH(slice) + 1); - strncpy(decoded, (const char *)GPR_SLICE_START_PTR(slice), - GPR_SLICE_LENGTH(slice)); - decoded[GPR_SLICE_LENGTH(slice)] = '\0'; - json = grpc_json_parse_string(decoded); - gpr_free(b64); + slice = grpc_base64_decode (b64, 1); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (slice)); + decoded = gpr_malloc (GPR_SLICE_LENGTH (slice) + 1); + strncpy (decoded, (const char *) GPR_SLICE_START_PTR (slice), GPR_SLICE_LENGTH (slice)); + decoded[GPR_SLICE_LENGTH (slice)] = '\0'; + json = grpc_json_parse_string (decoded); + gpr_free (b64); *scratchpad = decoded; - gpr_slice_unref(slice); + gpr_slice_unref (slice); return json; } -static void check_jwt_header(grpc_json *header) { +static void +check_jwt_header (grpc_json * header) +{ grpc_json *ptr; grpc_json *alg = NULL; grpc_json *typ = NULL; grpc_json *kid = NULL; - for (ptr = header->child; ptr; ptr = ptr->next) { - if (strcmp(ptr->key, "alg") == 0) { - alg = ptr; - } else if (strcmp(ptr->key, "typ") == 0) { - typ = ptr; - } else if (strcmp(ptr->key, "kid") == 0) { - kid = ptr; + for (ptr = header->child; ptr; ptr = ptr->next) + { + if (strcmp (ptr->key, "alg") == 0) + { + alg = ptr; + } + else if (strcmp (ptr->key, "typ") == 0) + { + typ = ptr; + } + else if (strcmp (ptr->key, "kid") == 0) + { + kid = ptr; + } } - } - GPR_ASSERT(alg != NULL); - GPR_ASSERT(alg->type == GRPC_JSON_STRING); - GPR_ASSERT(strcmp(alg->value, "RS256") == 0); - - GPR_ASSERT(typ != NULL); - GPR_ASSERT(typ->type == GRPC_JSON_STRING); - GPR_ASSERT(strcmp(typ->value, "JWT") == 0); - - GPR_ASSERT(kid != NULL); - GPR_ASSERT(kid->type == GRPC_JSON_STRING); - GPR_ASSERT(strcmp(kid->value, "e6b5137873db8d2ef81e06a47289e6434ec8a165") == - 0); + GPR_ASSERT (alg != NULL); + GPR_ASSERT (alg->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (alg->value, "RS256") == 0); + + GPR_ASSERT (typ != NULL); + GPR_ASSERT (typ->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (typ->value, "JWT") == 0); + + GPR_ASSERT (kid != NULL); + GPR_ASSERT (kid->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (kid->value, "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0); } -static void check_jwt_claim(grpc_json *claim, const char *expected_audience, - const char *expected_scope) { - gpr_timespec expiration = gpr_time_0(GPR_CLOCK_REALTIME); - gpr_timespec issue_time = gpr_time_0(GPR_CLOCK_REALTIME); +static void +check_jwt_claim (grpc_json * claim, const char *expected_audience, const char *expected_scope) +{ + gpr_timespec expiration = gpr_time_0 (GPR_CLOCK_REALTIME); + gpr_timespec issue_time = gpr_time_0 (GPR_CLOCK_REALTIME); gpr_timespec parsed_lifetime; grpc_json *iss = NULL; grpc_json *scope = NULL; @@ -280,228 +224,231 @@ static void check_jwt_claim(grpc_json *claim, const char *expected_audience, grpc_json *sub = NULL; grpc_json *ptr; - for (ptr = claim->child; ptr; ptr = ptr->next) { - if (strcmp(ptr->key, "iss") == 0) { - iss = ptr; - } else if (strcmp(ptr->key, "sub") == 0) { - sub = ptr; - } else if (strcmp(ptr->key, "scope") == 0) { - scope = ptr; - } else if (strcmp(ptr->key, "aud") == 0) { - aud = ptr; - } else if (strcmp(ptr->key, "exp") == 0) { - exp = ptr; - } else if (strcmp(ptr->key, "iat") == 0) { - iat = ptr; + for (ptr = claim->child; ptr; ptr = ptr->next) + { + if (strcmp (ptr->key, "iss") == 0) + { + iss = ptr; + } + else if (strcmp (ptr->key, "sub") == 0) + { + sub = ptr; + } + else if (strcmp (ptr->key, "scope") == 0) + { + scope = ptr; + } + else if (strcmp (ptr->key, "aud") == 0) + { + aud = ptr; + } + else if (strcmp (ptr->key, "exp") == 0) + { + exp = ptr; + } + else if (strcmp (ptr->key, "iat") == 0) + { + iat = ptr; + } + } + + GPR_ASSERT (iss != NULL); + GPR_ASSERT (iss->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (iss->value, "777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount.com") == 0); + + if (expected_scope != NULL) + { + GPR_ASSERT (scope != NULL); + GPR_ASSERT (sub == NULL); + GPR_ASSERT (scope->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (scope->value, expected_scope) == 0); } - } - - GPR_ASSERT(iss != NULL); - GPR_ASSERT(iss->type == GRPC_JSON_STRING); - GPR_ASSERT( - strcmp( - iss->value, - "777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount.com") == - 0); - - if (expected_scope != NULL) { - GPR_ASSERT(scope != NULL); - GPR_ASSERT(sub == NULL); - GPR_ASSERT(scope->type == GRPC_JSON_STRING); - GPR_ASSERT(strcmp(scope->value, expected_scope) == 0); - } else { - /* Claims without scope must have a sub. */ - GPR_ASSERT(scope == NULL); - GPR_ASSERT(sub != NULL); - GPR_ASSERT(sub->type == GRPC_JSON_STRING); - GPR_ASSERT(strcmp(iss->value, sub->value) == 0); - } - - GPR_ASSERT(aud != NULL); - GPR_ASSERT(aud->type == GRPC_JSON_STRING); - GPR_ASSERT(strcmp(aud->value, expected_audience) == 0); - - GPR_ASSERT(exp != NULL); - GPR_ASSERT(exp->type == GRPC_JSON_NUMBER); - expiration.tv_sec = strtol(exp->value, NULL, 10); - - GPR_ASSERT(iat != NULL); - GPR_ASSERT(iat->type == GRPC_JSON_NUMBER); - issue_time.tv_sec = strtol(iat->value, NULL, 10); - - parsed_lifetime = gpr_time_sub(expiration, issue_time); - GPR_ASSERT(parsed_lifetime.tv_sec == grpc_max_auth_token_lifetime.tv_sec); + else + { + /* Claims without scope must have a sub. */ + GPR_ASSERT (scope == NULL); + GPR_ASSERT (sub != NULL); + GPR_ASSERT (sub->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (iss->value, sub->value) == 0); + } + + GPR_ASSERT (aud != NULL); + GPR_ASSERT (aud->type == GRPC_JSON_STRING); + GPR_ASSERT (strcmp (aud->value, expected_audience) == 0); + + GPR_ASSERT (exp != NULL); + GPR_ASSERT (exp->type == GRPC_JSON_NUMBER); + expiration.tv_sec = strtol (exp->value, NULL, 10); + + GPR_ASSERT (iat != NULL); + GPR_ASSERT (iat->type == GRPC_JSON_NUMBER); + issue_time.tv_sec = strtol (iat->value, NULL, 10); + + parsed_lifetime = gpr_time_sub (expiration, issue_time); + GPR_ASSERT (parsed_lifetime.tv_sec == grpc_max_auth_token_lifetime.tv_sec); } -static void check_jwt_signature(const char *b64_signature, RSA *rsa_key, - const char *signed_data, - size_t signed_data_size) { - EVP_MD_CTX *md_ctx = EVP_MD_CTX_create(); - EVP_PKEY *key = EVP_PKEY_new(); - - gpr_slice sig = grpc_base64_decode(b64_signature, 1); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(sig)); - GPR_ASSERT(GPR_SLICE_LENGTH(sig) == 128); - - GPR_ASSERT(md_ctx != NULL); - GPR_ASSERT(key != NULL); - EVP_PKEY_set1_RSA(key, rsa_key); - - GPR_ASSERT(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, key) == 1); - GPR_ASSERT(EVP_DigestVerifyUpdate(md_ctx, signed_data, signed_data_size) == - 1); - GPR_ASSERT(EVP_DigestVerifyFinal(md_ctx, GPR_SLICE_START_PTR(sig), - GPR_SLICE_LENGTH(sig)) == 1); - - gpr_slice_unref(sig); - if (key != NULL) EVP_PKEY_free(key); - if (md_ctx != NULL) EVP_MD_CTX_destroy(md_ctx); +static void +check_jwt_signature (const char *b64_signature, RSA * rsa_key, const char *signed_data, size_t signed_data_size) +{ + EVP_MD_CTX *md_ctx = EVP_MD_CTX_create (); + EVP_PKEY *key = EVP_PKEY_new (); + + gpr_slice sig = grpc_base64_decode (b64_signature, 1); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (sig)); + GPR_ASSERT (GPR_SLICE_LENGTH (sig) == 128); + + GPR_ASSERT (md_ctx != NULL); + GPR_ASSERT (key != NULL); + EVP_PKEY_set1_RSA (key, rsa_key); + + GPR_ASSERT (EVP_DigestVerifyInit (md_ctx, NULL, EVP_sha256 (), NULL, key) == 1); + GPR_ASSERT (EVP_DigestVerifyUpdate (md_ctx, signed_data, signed_data_size) == 1); + GPR_ASSERT (EVP_DigestVerifyFinal (md_ctx, GPR_SLICE_START_PTR (sig), GPR_SLICE_LENGTH (sig)) == 1); + + gpr_slice_unref (sig); + if (key != NULL) + EVP_PKEY_free (key); + if (md_ctx != NULL) + EVP_MD_CTX_destroy (md_ctx); } -static char *service_account_creds_jwt_encode_and_sign( - const grpc_auth_json_key *key) { - return grpc_jwt_encode_and_sign(key, GRPC_JWT_OAUTH2_AUDIENCE, - grpc_max_auth_token_lifetime, test_scope); +static char * +service_account_creds_jwt_encode_and_sign (const grpc_auth_json_key * key) +{ + return grpc_jwt_encode_and_sign (key, GRPC_JWT_OAUTH2_AUDIENCE, grpc_max_auth_token_lifetime, test_scope); } -static char *jwt_creds_jwt_encode_and_sign(const grpc_auth_json_key *key) { - return grpc_jwt_encode_and_sign(key, test_service_url, - grpc_max_auth_token_lifetime, NULL); +static char * +jwt_creds_jwt_encode_and_sign (const grpc_auth_json_key * key) +{ + return grpc_jwt_encode_and_sign (key, test_service_url, grpc_max_auth_token_lifetime, NULL); } -static void service_account_creds_check_jwt_claim(grpc_json *claim) { - check_jwt_claim(claim, GRPC_JWT_OAUTH2_AUDIENCE, test_scope); +static void +service_account_creds_check_jwt_claim (grpc_json * claim) +{ + check_jwt_claim (claim, GRPC_JWT_OAUTH2_AUDIENCE, test_scope); } -static void jwt_creds_check_jwt_claim(grpc_json *claim) { - check_jwt_claim(claim, test_service_url, NULL); +static void +jwt_creds_check_jwt_claim (grpc_json * claim) +{ + check_jwt_claim (claim, test_service_url, NULL); } -static void test_jwt_encode_and_sign( - char *(*jwt_encode_and_sign_func)(const grpc_auth_json_key *), - void (*check_jwt_claim_func)(grpc_json *)) { - char *json_string = test_json_key_str(NULL); +static void +test_jwt_encode_and_sign (char *(*jwt_encode_and_sign_func) (const grpc_auth_json_key *), void (*check_jwt_claim_func) (grpc_json *)) +{ + char *json_string = test_json_key_str (NULL); grpc_json *parsed_header = NULL; grpc_json *parsed_claim = NULL; char *scratchpad; - grpc_auth_json_key json_key = - grpc_auth_json_key_create_from_string(json_string); + grpc_auth_json_key json_key = grpc_auth_json_key_create_from_string (json_string); const char *b64_signature; size_t offset = 0; - char *jwt = jwt_encode_and_sign_func(&json_key); - const char *dot = strchr(jwt, '.'); - GPR_ASSERT(dot != NULL); - parsed_header = - parse_json_part_from_jwt(jwt, (size_t)(dot - jwt), &scratchpad); - GPR_ASSERT(parsed_header != NULL); - check_jwt_header(parsed_header); - offset = (size_t)(dot - jwt) + 1; - grpc_json_destroy(parsed_header); - gpr_free(scratchpad); - - dot = strchr(jwt + offset, '.'); - GPR_ASSERT(dot != NULL); - parsed_claim = parse_json_part_from_jwt( - jwt + offset, (size_t)(dot - (jwt + offset)), &scratchpad); - GPR_ASSERT(parsed_claim != NULL); - check_jwt_claim_func(parsed_claim); - offset = (size_t)(dot - jwt) + 1; - grpc_json_destroy(parsed_claim); - gpr_free(scratchpad); - - dot = strchr(jwt + offset, '.'); - GPR_ASSERT(dot == NULL); /* no more part. */ + char *jwt = jwt_encode_and_sign_func (&json_key); + const char *dot = strchr (jwt, '.'); + GPR_ASSERT (dot != NULL); + parsed_header = parse_json_part_from_jwt (jwt, (size_t) (dot - jwt), &scratchpad); + GPR_ASSERT (parsed_header != NULL); + check_jwt_header (parsed_header); + offset = (size_t) (dot - jwt) + 1; + grpc_json_destroy (parsed_header); + gpr_free (scratchpad); + + dot = strchr (jwt + offset, '.'); + GPR_ASSERT (dot != NULL); + parsed_claim = parse_json_part_from_jwt (jwt + offset, (size_t) (dot - (jwt + offset)), &scratchpad); + GPR_ASSERT (parsed_claim != NULL); + check_jwt_claim_func (parsed_claim); + offset = (size_t) (dot - jwt) + 1; + grpc_json_destroy (parsed_claim); + gpr_free (scratchpad); + + dot = strchr (jwt + offset, '.'); + GPR_ASSERT (dot == NULL); /* no more part. */ b64_signature = jwt + offset; - check_jwt_signature(b64_signature, json_key.private_key, jwt, offset - 1); + check_jwt_signature (b64_signature, json_key.private_key, jwt, offset - 1); - gpr_free(json_string); - grpc_auth_json_key_destruct(&json_key); - gpr_free(jwt); + gpr_free (json_string); + grpc_auth_json_key_destruct (&json_key); + gpr_free (jwt); } -static void test_service_account_creds_jwt_encode_and_sign(void) { - test_jwt_encode_and_sign(service_account_creds_jwt_encode_and_sign, - service_account_creds_check_jwt_claim); +static void +test_service_account_creds_jwt_encode_and_sign (void) +{ + test_jwt_encode_and_sign (service_account_creds_jwt_encode_and_sign, service_account_creds_check_jwt_claim); } -static void test_jwt_creds_jwt_encode_and_sign(void) { - test_jwt_encode_and_sign(jwt_creds_jwt_encode_and_sign, - jwt_creds_check_jwt_claim); +static void +test_jwt_creds_jwt_encode_and_sign (void) +{ + test_jwt_encode_and_sign (jwt_creds_jwt_encode_and_sign, jwt_creds_check_jwt_claim); } -static void test_parse_refresh_token_success(void) { - grpc_auth_refresh_token refresh_token = - grpc_auth_refresh_token_create_from_string(test_refresh_token_str); - GPR_ASSERT(grpc_auth_refresh_token_is_valid(&refresh_token)); - GPR_ASSERT(refresh_token.type != NULL && - (strcmp(refresh_token.type, "authorized_user") == 0)); - GPR_ASSERT(refresh_token.client_id != NULL && - (strcmp(refresh_token.client_id, - "32555999999.apps.googleusercontent.com") == 0)); - GPR_ASSERT( - refresh_token.client_secret != NULL && - (strcmp(refresh_token.client_secret, "EmssLNjJy1332hD4KFsecret") == 0)); - GPR_ASSERT(refresh_token.refresh_token != NULL && - (strcmp(refresh_token.refresh_token, - "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42") == 0)); - grpc_auth_refresh_token_destruct(&refresh_token); +static void +test_parse_refresh_token_success (void) +{ + grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string (test_refresh_token_str); + GPR_ASSERT (grpc_auth_refresh_token_is_valid (&refresh_token)); + GPR_ASSERT (refresh_token.type != NULL && (strcmp (refresh_token.type, "authorized_user") == 0)); + GPR_ASSERT (refresh_token.client_id != NULL && (strcmp (refresh_token.client_id, "32555999999.apps.googleusercontent.com") == 0)); + GPR_ASSERT (refresh_token.client_secret != NULL && (strcmp (refresh_token.client_secret, "EmssLNjJy1332hD4KFsecret") == 0)); + GPR_ASSERT (refresh_token.refresh_token != NULL && (strcmp (refresh_token.refresh_token, "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42") == 0)); + grpc_auth_refresh_token_destruct (&refresh_token); } -static void test_parse_refresh_token_failure_no_type(void) { - const char refresh_token_str[] = - "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," - " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," - " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"}"; - grpc_auth_refresh_token refresh_token = - grpc_auth_refresh_token_create_from_string(refresh_token_str); - GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); +static void +test_parse_refresh_token_failure_no_type (void) +{ + const char refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"}"; + grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string (refresh_token_str); + GPR_ASSERT (!grpc_auth_refresh_token_is_valid (&refresh_token)); } -static void test_parse_refresh_token_failure_no_client_id(void) { - const char refresh_token_str[] = - "{ \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," - " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," - " \"type\": \"authorized_user\"}"; - grpc_auth_refresh_token refresh_token = - grpc_auth_refresh_token_create_from_string(refresh_token_str); - GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); +static void +test_parse_refresh_token_failure_no_client_id (void) +{ + const char refresh_token_str[] = "{ \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," " \"type\": \"authorized_user\"}"; + grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string (refresh_token_str); + GPR_ASSERT (!grpc_auth_refresh_token_is_valid (&refresh_token)); } -static void test_parse_refresh_token_failure_no_client_secret(void) { - const char refresh_token_str[] = - "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," - " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," - " \"type\": \"authorized_user\"}"; - grpc_auth_refresh_token refresh_token = - grpc_auth_refresh_token_create_from_string(refresh_token_str); - GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); +static void +test_parse_refresh_token_failure_no_client_secret (void) +{ + const char refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," " \"type\": \"authorized_user\"}"; + grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string (refresh_token_str); + GPR_ASSERT (!grpc_auth_refresh_token_is_valid (&refresh_token)); } -static void test_parse_refresh_token_failure_no_refresh_token(void) { - const char refresh_token_str[] = - "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," - " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," - " \"type\": \"authorized_user\"}"; - grpc_auth_refresh_token refresh_token = - grpc_auth_refresh_token_create_from_string(refresh_token_str); - GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); +static void +test_parse_refresh_token_failure_no_refresh_token (void) +{ + const char refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"type\": \"authorized_user\"}"; + grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string (refresh_token_str); + GPR_ASSERT (!grpc_auth_refresh_token_is_valid (&refresh_token)); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_parse_json_key_success(); - test_parse_json_key_failure_bad_json(); - test_parse_json_key_failure_no_type(); - test_parse_json_key_failure_no_client_id(); - test_parse_json_key_failure_no_client_email(); - test_parse_json_key_failure_no_private_key_id(); - test_parse_json_key_failure_no_private_key(); - test_service_account_creds_jwt_encode_and_sign(); - test_jwt_creds_jwt_encode_and_sign(); - test_parse_refresh_token_success(); - test_parse_refresh_token_failure_no_type(); - test_parse_refresh_token_failure_no_client_id(); - test_parse_refresh_token_failure_no_client_secret(); - test_parse_refresh_token_failure_no_refresh_token(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_parse_json_key_success (); + test_parse_json_key_failure_bad_json (); + test_parse_json_key_failure_no_type (); + test_parse_json_key_failure_no_client_id (); + test_parse_json_key_failure_no_client_email (); + test_parse_json_key_failure_no_private_key_id (); + test_parse_json_key_failure_no_private_key (); + test_service_account_creds_jwt_encode_and_sign (); + test_jwt_creds_jwt_encode_and_sign (); + test_parse_refresh_token_success (); + test_parse_refresh_token_failure_no_type (); + test_parse_refresh_token_failure_no_client_id (); + test_parse_refresh_token_failure_no_client_secret (); + test_parse_refresh_token_failure_no_refresh_token (); return 0; } diff --git a/test/core/security/jwt_verifier_test.c b/test/core/security/jwt_verifier_test.c index f2215e1822..15c19e2533 100644 --- a/test/core/security/jwt_verifier_test.c +++ b/test/core/security/jwt_verifier_test.c @@ -48,540 +48,429 @@ /* This JSON key was generated with the GCE console and revoked immediately. The identifiers have been changed as well. Maximum size for a string literal is 509 chars in C89, yay! */ -static const char json_key_str_part1[] = - "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" - "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" - "qg" - "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" - "rWBQvS4hle4LfijkP3J5BG+" - "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" - "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" - "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" - "8HpCqFYM9V8f34SBWfD4fRFT+n/" - "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; -static const char json_key_str_part2[] = - "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" - "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" - "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" - "G" - "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" - "A" - "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" - "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" - "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" - "Y" - "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; -static const char json_key_str_part3_for_google_email_issuer[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." - "com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; +static const char json_key_str_part1[] = "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" "qg" "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" "rWBQvS4hle4LfijkP3J5BG+" "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" "8HpCqFYM9V8f34SBWfD4fRFT+n/" "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; +static const char json_key_str_part2[] = "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" "G" "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" "A" "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" "Y" "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; +static const char json_key_str_part3_for_google_email_issuer[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount." "com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; /* Trick our JWT library into issuing a JWT with iss=accounts.google.com. */ -static const char json_key_str_part3_for_url_issuer[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": \"accounts.google.com\", " - "\"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; -static const char json_key_str_part3_for_custom_email_issuer[] = - "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " - "\"client_email\": " - "\"foo@bar.com\", \"client_id\": " - "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." - "com\", \"type\": \"service_account\" }"; +static const char json_key_str_part3_for_url_issuer[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": \"accounts.google.com\", " "\"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; +static const char json_key_str_part3_for_custom_email_issuer[] = "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " "\"client_email\": " "\"foo@bar.com\", \"client_id\": " "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." "com\", \"type\": \"service_account\" }"; static grpc_jwt_verifier_email_domain_key_url_mapping custom_mapping = { - "bar.com", "keys.bar.com/jwk"}; + "bar.com", "keys.bar.com/jwk" +}; static const char expected_user_data[] = "user data"; -static const char good_jwk_set[] = - "{" - " \"keys\": [" - " {" - " \"kty\": \"RSA\"," - " \"alg\": \"RS256\"," - " \"use\": \"sig\"," - " \"kid\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\"," - " \"n\": " - "\"4S8myegefIeRdynuYkSqBYaOLDvU19cHKC56RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ6mxP" - "QutZC1nKNJ6D-tYFC9LiGV7gt-KOQ_cnkEb4hcMw_xF_OI1FCx6cBcM0-" - "RjiQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFU=\"," - " \"e\": \"AQAB\"" - " }" - " ]" - "}"; - -static gpr_timespec expected_lifetime = {3600, 0, GPR_TIMESPAN}; - -static const char good_google_email_keys_part1[] = - "{\"e6b5137873db8d2ef81e06a47289e6434ec8a165\": \"-----BEGIN " - "CERTIFICATE-----" - "\\nMIICATCCAWoCCQDEywLhxvHjnDANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB\\nVTET" - "MBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\\ncyBQdHkgTHR" - "kMB4XDTE1MDYyOTA4Mzk1MFoXDTI1MDYyNjA4Mzk1MFowRTELMAkG\\nA1UEBhMCQVUxEzARBg" - "NVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\\nIFdpZGdpdHMgUHR5IEx0ZDCBn" - "zANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA4S8m\\nyegefIeRdynuYkSqBYaOLDvU19cHKC56" - "RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ\\n6mxPQutZC1nKNJ6D+tYFC9LiGV7gt+KOQ/"; - -static const char good_google_email_keys_part2[] = - "cnkEb4hcMw/xF/OI1FCx6cBcM0+" - "Rji\\nQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFUCAwEAATANBgkqhkiG9w0BAQsF\\nA" - "AOBgQBfu69FkPmBknbKNFgurPz78kbs3VNN+k/" - "PUgO5DHKskJmgK2TbtvX2VMpx\\nkftmHGzgzMzUlOtigCaGMgHWjfqjpP9uuDbahXrZBJzB8c" - "Oq7MrQF8r17qVvo3Ue\\nPjTKQMAsU8uxTEMmeuz9L6yExs0rfd6bPOrQkAoVfFfiYB3/" - "pA==\\n-----END CERTIFICATE-----\\n\"}"; +static const char good_jwk_set[] = "{" " \"keys\": [" " {" " \"kty\": \"RSA\"," " \"alg\": \"RS256\"," " \"use\": \"sig\"," " \"kid\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\"," " \"n\": " "\"4S8myegefIeRdynuYkSqBYaOLDvU19cHKC56RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ6mxP" "QutZC1nKNJ6D-tYFC9LiGV7gt-KOQ_cnkEb4hcMw_xF_OI1FCx6cBcM0-" "RjiQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFU=\"," " \"e\": \"AQAB\"" " }" " ]" "}"; + +static gpr_timespec expected_lifetime = { 3600, 0, GPR_TIMESPAN }; + +static const char good_google_email_keys_part1[] = "{\"e6b5137873db8d2ef81e06a47289e6434ec8a165\": \"-----BEGIN " "CERTIFICATE-----" "\\nMIICATCCAWoCCQDEywLhxvHjnDANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB\\nVTET" "MBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\\ncyBQdHkgTHR" "kMB4XDTE1MDYyOTA4Mzk1MFoXDTI1MDYyNjA4Mzk1MFowRTELMAkG\\nA1UEBhMCQVUxEzARBg" "NVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\\nIFdpZGdpdHMgUHR5IEx0ZDCBn" "zANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA4S8m\\nyegefIeRdynuYkSqBYaOLDvU19cHKC56" "RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ\\n6mxPQutZC1nKNJ6D+tYFC9LiGV7gt+KOQ/"; + +static const char good_google_email_keys_part2[] = "cnkEb4hcMw/xF/OI1FCx6cBcM0+" "Rji\\nQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFUCAwEAATANBgkqhkiG9w0BAQsF\\nA" "AOBgQBfu69FkPmBknbKNFgurPz78kbs3VNN+k/" "PUgO5DHKskJmgK2TbtvX2VMpx\\nkftmHGzgzMzUlOtigCaGMgHWjfqjpP9uuDbahXrZBJzB8c" "Oq7MrQF8r17qVvo3Ue\\nPjTKQMAsU8uxTEMmeuz9L6yExs0rfd6bPOrQkAoVfFfiYB3/" "pA==\\n-----END CERTIFICATE-----\\n\"}"; static const char expected_audience[] = "https://foo.com"; -static const char good_openid_config[] = - "{" - " \"issuer\": \"https://accounts.google.com\"," - " \"authorization_endpoint\": " - "\"https://accounts.google.com/o/oauth2/v2/auth\"," - " \"token_endpoint\": \"https://www.googleapis.com/oauth2/v4/token\"," - " \"userinfo_endpoint\": \"https://www.googleapis.com/oauth2/v3/userinfo\"," - " \"revocation_endpoint\": \"https://accounts.google.com/o/oauth2/revoke\"," - " \"jwks_uri\": \"https://www.googleapis.com/oauth2/v3/certs\"" - "}"; - -static const char expired_claims[] = - "{ \"aud\": \"https://foo.com\"," - " \"iss\": \"blah.foo.com\"," - " \"sub\": \"juju@blah.foo.com\"," - " \"jti\": \"jwtuniqueid\"," - " \"iat\": 100," /* Way back in the past... */ - " \"exp\": 120," - " \"nbf\": 60," - " \"foo\": \"bar\"}"; - -static const char claims_without_time_constraint[] = - "{ \"aud\": \"https://foo.com\"," - " \"iss\": \"blah.foo.com\"," - " \"sub\": \"juju@blah.foo.com\"," - " \"jti\": \"jwtuniqueid\"," - " \"foo\": \"bar\"}"; - -static const char invalid_claims[] = - "{ \"aud\": \"https://foo.com\"," - " \"iss\": 46," /* Issuer cannot be a number. */ - " \"sub\": \"juju@blah.foo.com\"," - " \"jti\": \"jwtuniqueid\"," - " \"foo\": \"bar\"}"; - -typedef struct { +static const char good_openid_config[] = "{" " \"issuer\": \"https://accounts.google.com\"," " \"authorization_endpoint\": " "\"https://accounts.google.com/o/oauth2/v2/auth\"," " \"token_endpoint\": \"https://www.googleapis.com/oauth2/v4/token\"," " \"userinfo_endpoint\": \"https://www.googleapis.com/oauth2/v3/userinfo\"," " \"revocation_endpoint\": \"https://accounts.google.com/o/oauth2/revoke\"," " \"jwks_uri\": \"https://www.googleapis.com/oauth2/v3/certs\"" "}"; + +static const char expired_claims[] = "{ \"aud\": \"https://foo.com\"," " \"iss\": \"blah.foo.com\"," " \"sub\": \"juju@blah.foo.com\"," " \"jti\": \"jwtuniqueid\"," " \"iat\": 100," /* Way back in the past... */ + " \"exp\": 120," " \"nbf\": 60," " \"foo\": \"bar\"}"; + +static const char claims_without_time_constraint[] = "{ \"aud\": \"https://foo.com\"," " \"iss\": \"blah.foo.com\"," " \"sub\": \"juju@blah.foo.com\"," " \"jti\": \"jwtuniqueid\"," " \"foo\": \"bar\"}"; + +static const char invalid_claims[] = "{ \"aud\": \"https://foo.com\"," " \"iss\": 46," /* Issuer cannot be a number. */ + " \"sub\": \"juju@blah.foo.com\"," " \"jti\": \"jwtuniqueid\"," " \"foo\": \"bar\"}"; + +typedef struct +{ grpc_jwt_verifier_status expected_status; const char *expected_issuer; const char *expected_subject; } verifier_test_config; -static void test_claims_success(void) { +static void +test_claims_success (void) +{ grpc_jwt_claims *claims; - gpr_slice s = gpr_slice_from_copied_string(claims_without_time_constraint); - grpc_json *json = grpc_json_parse_string_with_len( - (char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s)); - GPR_ASSERT(json != NULL); - claims = grpc_jwt_claims_from_json(json, s); - GPR_ASSERT(claims != NULL); - GPR_ASSERT(grpc_jwt_claims_json(claims) == json); - GPR_ASSERT(strcmp(grpc_jwt_claims_audience(claims), "https://foo.com") == 0); - GPR_ASSERT(strcmp(grpc_jwt_claims_issuer(claims), "blah.foo.com") == 0); - GPR_ASSERT(strcmp(grpc_jwt_claims_subject(claims), "juju@blah.foo.com") == 0); - GPR_ASSERT(strcmp(grpc_jwt_claims_id(claims), "jwtuniqueid") == 0); - GPR_ASSERT(grpc_jwt_claims_check(claims, "https://foo.com") == - GRPC_JWT_VERIFIER_OK); - grpc_jwt_claims_destroy(claims); + gpr_slice s = gpr_slice_from_copied_string (claims_without_time_constraint); + grpc_json *json = grpc_json_parse_string_with_len ((char *) GPR_SLICE_START_PTR (s), GPR_SLICE_LENGTH (s)); + GPR_ASSERT (json != NULL); + claims = grpc_jwt_claims_from_json (json, s); + GPR_ASSERT (claims != NULL); + GPR_ASSERT (grpc_jwt_claims_json (claims) == json); + GPR_ASSERT (strcmp (grpc_jwt_claims_audience (claims), "https://foo.com") == 0); + GPR_ASSERT (strcmp (grpc_jwt_claims_issuer (claims), "blah.foo.com") == 0); + GPR_ASSERT (strcmp (grpc_jwt_claims_subject (claims), "juju@blah.foo.com") == 0); + GPR_ASSERT (strcmp (grpc_jwt_claims_id (claims), "jwtuniqueid") == 0); + GPR_ASSERT (grpc_jwt_claims_check (claims, "https://foo.com") == GRPC_JWT_VERIFIER_OK); + grpc_jwt_claims_destroy (claims); } -static void test_expired_claims_failure(void) { +static void +test_expired_claims_failure (void) +{ grpc_jwt_claims *claims; - gpr_slice s = gpr_slice_from_copied_string(expired_claims); - grpc_json *json = grpc_json_parse_string_with_len( - (char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s)); - gpr_timespec exp_iat = {100, 0, GPR_CLOCK_REALTIME}; - gpr_timespec exp_exp = {120, 0, GPR_CLOCK_REALTIME}; - gpr_timespec exp_nbf = {60, 0, GPR_CLOCK_REALTIME}; - GPR_ASSERT(json != NULL); - claims = grpc_jwt_claims_from_json(json, s); - GPR_ASSERT(claims != NULL); - GPR_ASSERT(grpc_jwt_claims_json(claims) == json); - GPR_ASSERT(strcmp(grpc_jwt_claims_audience(claims), "https://foo.com") == 0); - GPR_ASSERT(strcmp(grpc_jwt_claims_issuer(claims), "blah.foo.com") == 0); - GPR_ASSERT(strcmp(grpc_jwt_claims_subject(claims), "juju@blah.foo.com") == 0); - GPR_ASSERT(strcmp(grpc_jwt_claims_id(claims), "jwtuniqueid") == 0); - GPR_ASSERT(gpr_time_cmp(grpc_jwt_claims_issued_at(claims), exp_iat) == 0); - GPR_ASSERT(gpr_time_cmp(grpc_jwt_claims_expires_at(claims), exp_exp) == 0); - GPR_ASSERT(gpr_time_cmp(grpc_jwt_claims_not_before(claims), exp_nbf) == 0); - - GPR_ASSERT(grpc_jwt_claims_check(claims, "https://foo.com") == - GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE); - grpc_jwt_claims_destroy(claims); + gpr_slice s = gpr_slice_from_copied_string (expired_claims); + grpc_json *json = grpc_json_parse_string_with_len ((char *) GPR_SLICE_START_PTR (s), GPR_SLICE_LENGTH (s)); + gpr_timespec exp_iat = { 100, 0, GPR_CLOCK_REALTIME }; + gpr_timespec exp_exp = { 120, 0, GPR_CLOCK_REALTIME }; + gpr_timespec exp_nbf = { 60, 0, GPR_CLOCK_REALTIME }; + GPR_ASSERT (json != NULL); + claims = grpc_jwt_claims_from_json (json, s); + GPR_ASSERT (claims != NULL); + GPR_ASSERT (grpc_jwt_claims_json (claims) == json); + GPR_ASSERT (strcmp (grpc_jwt_claims_audience (claims), "https://foo.com") == 0); + GPR_ASSERT (strcmp (grpc_jwt_claims_issuer (claims), "blah.foo.com") == 0); + GPR_ASSERT (strcmp (grpc_jwt_claims_subject (claims), "juju@blah.foo.com") == 0); + GPR_ASSERT (strcmp (grpc_jwt_claims_id (claims), "jwtuniqueid") == 0); + GPR_ASSERT (gpr_time_cmp (grpc_jwt_claims_issued_at (claims), exp_iat) == 0); + GPR_ASSERT (gpr_time_cmp (grpc_jwt_claims_expires_at (claims), exp_exp) == 0); + GPR_ASSERT (gpr_time_cmp (grpc_jwt_claims_not_before (claims), exp_nbf) == 0); + + GPR_ASSERT (grpc_jwt_claims_check (claims, "https://foo.com") == GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE); + grpc_jwt_claims_destroy (claims); } -static void test_invalid_claims_failure(void) { - gpr_slice s = gpr_slice_from_copied_string(invalid_claims); - grpc_json *json = grpc_json_parse_string_with_len( - (char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s)); - GPR_ASSERT(grpc_jwt_claims_from_json(json, s) == NULL); +static void +test_invalid_claims_failure (void) +{ + gpr_slice s = gpr_slice_from_copied_string (invalid_claims); + grpc_json *json = grpc_json_parse_string_with_len ((char *) GPR_SLICE_START_PTR (s), GPR_SLICE_LENGTH (s)); + GPR_ASSERT (grpc_jwt_claims_from_json (json, s) == NULL); } -static void test_bad_audience_claims_failure(void) { +static void +test_bad_audience_claims_failure (void) +{ grpc_jwt_claims *claims; - gpr_slice s = gpr_slice_from_copied_string(claims_without_time_constraint); - grpc_json *json = grpc_json_parse_string_with_len( - (char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s)); - GPR_ASSERT(json != NULL); - claims = grpc_jwt_claims_from_json(json, s); - GPR_ASSERT(claims != NULL); - GPR_ASSERT(grpc_jwt_claims_check(claims, "https://bar.com") == - GRPC_JWT_VERIFIER_BAD_AUDIENCE); - grpc_jwt_claims_destroy(claims); + gpr_slice s = gpr_slice_from_copied_string (claims_without_time_constraint); + grpc_json *json = grpc_json_parse_string_with_len ((char *) GPR_SLICE_START_PTR (s), GPR_SLICE_LENGTH (s)); + GPR_ASSERT (json != NULL); + claims = grpc_jwt_claims_from_json (json, s); + GPR_ASSERT (claims != NULL); + GPR_ASSERT (grpc_jwt_claims_check (claims, "https://bar.com") == GRPC_JWT_VERIFIER_BAD_AUDIENCE); + grpc_jwt_claims_destroy (claims); } -static char *json_key_str(const char *last_part) { - size_t result_len = strlen(json_key_str_part1) + strlen(json_key_str_part2) + - strlen(last_part); - char *result = gpr_malloc(result_len + 1); +static char * +json_key_str (const char *last_part) +{ + size_t result_len = strlen (json_key_str_part1) + strlen (json_key_str_part2) + strlen (last_part); + char *result = gpr_malloc (result_len + 1); char *current = result; - strcpy(result, json_key_str_part1); - current += strlen(json_key_str_part1); - strcpy(current, json_key_str_part2); - current += strlen(json_key_str_part2); - strcpy(current, last_part); + strcpy (result, json_key_str_part1); + current += strlen (json_key_str_part1); + strcpy (current, json_key_str_part2); + current += strlen (json_key_str_part2); + strcpy (current, last_part); return result; } -static char *good_google_email_keys(void) { - size_t result_len = strlen(good_google_email_keys_part1) + - strlen(good_google_email_keys_part2); - char *result = gpr_malloc(result_len + 1); +static char * +good_google_email_keys (void) +{ + size_t result_len = strlen (good_google_email_keys_part1) + strlen (good_google_email_keys_part2); + char *result = gpr_malloc (result_len + 1); char *current = result; - strcpy(result, good_google_email_keys_part1); - current += strlen(good_google_email_keys_part1); - strcpy(current, good_google_email_keys_part2); + strcpy (result, good_google_email_keys_part1); + current += strlen (good_google_email_keys_part1); + strcpy (current, good_google_email_keys_part2); return result; } -static grpc_httpcli_response http_response(int status, char *body) { +static grpc_httpcli_response +http_response (int status, char *body) +{ grpc_httpcli_response response; - memset(&response, 0, sizeof(grpc_httpcli_response)); + memset (&response, 0, sizeof (grpc_httpcli_response)); response.status = status; response.body = body; - response.body_length = strlen(body); + response.body_length = strlen (body); return response; } -static int httpcli_post_should_not_be_called( - const grpc_httpcli_request *request, const char *body_bytes, - size_t body_size, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - GPR_ASSERT("HTTP POST should not be called" == NULL); +static int +httpcli_post_should_not_be_called (const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + GPR_ASSERT ("HTTP POST should not be called" == NULL); return 1; } -static int httpcli_get_google_keys_for_email( - const grpc_httpcli_request *request, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = http_response(200, good_google_email_keys()); - GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl); - GPR_ASSERT(strcmp(request->host, "www.googleapis.com") == 0); - GPR_ASSERT(strcmp(request->path, - "/robot/v1/metadata/x509/" - "777-abaslkan11hlb6nmim3bpspl31ud@developer." - "gserviceaccount.com") == 0); - on_response(user_data, &response, closure_list); - gpr_free(response.body); +static int +httpcli_get_google_keys_for_email (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, good_google_email_keys ()); + GPR_ASSERT (request->handshaker == &grpc_httpcli_ssl); + GPR_ASSERT (strcmp (request->host, "www.googleapis.com") == 0); + GPR_ASSERT (strcmp (request->path, "/robot/v1/metadata/x509/" "777-abaslkan11hlb6nmim3bpspl31ud@developer." "gserviceaccount.com") == 0); + on_response (user_data, &response, closure_list); + gpr_free (response.body); return 1; } -static void on_verification_success(void *user_data, - grpc_jwt_verifier_status status, - grpc_jwt_claims *claims) { - GPR_ASSERT(status == GRPC_JWT_VERIFIER_OK); - GPR_ASSERT(claims != NULL); - GPR_ASSERT(user_data == (void *)expected_user_data); - GPR_ASSERT(strcmp(grpc_jwt_claims_audience(claims), expected_audience) == 0); - grpc_jwt_claims_destroy(claims); +static void +on_verification_success (void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims * claims) +{ + GPR_ASSERT (status == GRPC_JWT_VERIFIER_OK); + GPR_ASSERT (claims != NULL); + GPR_ASSERT (user_data == (void *) expected_user_data); + GPR_ASSERT (strcmp (grpc_jwt_claims_audience (claims), expected_audience) == 0); + grpc_jwt_claims_destroy (claims); } -static void test_jwt_verifier_google_email_issuer_success(void) { +static void +test_jwt_verifier_google_email_issuer_success (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (NULL, 0); char *jwt = NULL; - char *key_str = json_key_str(json_key_str_part3_for_google_email_issuer); - grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); - gpr_free(key_str); - GPR_ASSERT(grpc_auth_json_key_is_valid(&key)); - grpc_httpcli_set_override(httpcli_get_google_keys_for_email, - httpcli_post_should_not_be_called); - jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, - NULL); - grpc_auth_json_key_destruct(&key); - GPR_ASSERT(jwt != NULL); - grpc_jwt_verifier_verify(verifier, NULL, jwt, expected_audience, - on_verification_success, (void *)expected_user_data, - &closure_list); - gpr_free(jwt); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + char *key_str = json_key_str (json_key_str_part3_for_google_email_issuer); + grpc_auth_json_key key = grpc_auth_json_key_create_from_string (key_str); + gpr_free (key_str); + GPR_ASSERT (grpc_auth_json_key_is_valid (&key)); + grpc_httpcli_set_override (httpcli_get_google_keys_for_email, httpcli_post_should_not_be_called); + jwt = grpc_jwt_encode_and_sign (&key, expected_audience, expected_lifetime, NULL); + grpc_auth_json_key_destruct (&key); + GPR_ASSERT (jwt != NULL); + grpc_jwt_verifier_verify (verifier, NULL, jwt, expected_audience, on_verification_success, (void *) expected_user_data, &closure_list); + gpr_free (jwt); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static int httpcli_get_custom_keys_for_email( - const grpc_httpcli_request *request, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = http_response(200, gpr_strdup(good_jwk_set)); - GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl); - GPR_ASSERT(strcmp(request->host, "keys.bar.com") == 0); - GPR_ASSERT(strcmp(request->path, "/jwk/foo@bar.com") == 0); - on_response(user_data, &response, closure_list); - gpr_free(response.body); +static int +httpcli_get_custom_keys_for_email (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, gpr_strdup (good_jwk_set)); + GPR_ASSERT (request->handshaker == &grpc_httpcli_ssl); + GPR_ASSERT (strcmp (request->host, "keys.bar.com") == 0); + GPR_ASSERT (strcmp (request->path, "/jwk/foo@bar.com") == 0); + on_response (user_data, &response, closure_list); + gpr_free (response.body); return 1; } -static void test_jwt_verifier_custom_email_issuer_success(void) { +static void +test_jwt_verifier_custom_email_issuer_success (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(&custom_mapping, 1); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (&custom_mapping, 1); char *jwt = NULL; - char *key_str = json_key_str(json_key_str_part3_for_custom_email_issuer); - grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); - gpr_free(key_str); - GPR_ASSERT(grpc_auth_json_key_is_valid(&key)); - grpc_httpcli_set_override(httpcli_get_custom_keys_for_email, - httpcli_post_should_not_be_called); - jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, - NULL); - grpc_auth_json_key_destruct(&key); - GPR_ASSERT(jwt != NULL); - grpc_jwt_verifier_verify(verifier, NULL, jwt, expected_audience, - on_verification_success, (void *)expected_user_data, - &closure_list); - gpr_free(jwt); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + char *key_str = json_key_str (json_key_str_part3_for_custom_email_issuer); + grpc_auth_json_key key = grpc_auth_json_key_create_from_string (key_str); + gpr_free (key_str); + GPR_ASSERT (grpc_auth_json_key_is_valid (&key)); + grpc_httpcli_set_override (httpcli_get_custom_keys_for_email, httpcli_post_should_not_be_called); + jwt = grpc_jwt_encode_and_sign (&key, expected_audience, expected_lifetime, NULL); + grpc_auth_json_key_destruct (&key); + GPR_ASSERT (jwt != NULL); + grpc_jwt_verifier_verify (verifier, NULL, jwt, expected_audience, on_verification_success, (void *) expected_user_data, &closure_list); + gpr_free (jwt); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static int httpcli_get_jwk_set(const grpc_httpcli_request *request, - gpr_timespec deadline, - grpc_httpcli_response_cb on_response, - void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = http_response(200, gpr_strdup(good_jwk_set)); - GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl); - GPR_ASSERT(strcmp(request->host, "www.googleapis.com") == 0); - GPR_ASSERT(strcmp(request->path, "/oauth2/v3/certs") == 0); - on_response(user_data, &response, closure_list); - gpr_free(response.body); +static int +httpcli_get_jwk_set (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, gpr_strdup (good_jwk_set)); + GPR_ASSERT (request->handshaker == &grpc_httpcli_ssl); + GPR_ASSERT (strcmp (request->host, "www.googleapis.com") == 0); + GPR_ASSERT (strcmp (request->path, "/oauth2/v3/certs") == 0); + on_response (user_data, &response, closure_list); + gpr_free (response.body); return 1; } -static int httpcli_get_openid_config(const grpc_httpcli_request *request, - gpr_timespec deadline, - grpc_httpcli_response_cb on_response, - void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = - http_response(200, gpr_strdup(good_openid_config)); - GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl); - GPR_ASSERT(strcmp(request->host, "accounts.google.com") == 0); - GPR_ASSERT(strcmp(request->path, GRPC_OPENID_CONFIG_URL_SUFFIX) == 0); - grpc_httpcli_set_override(httpcli_get_jwk_set, - httpcli_post_should_not_be_called); - on_response(user_data, &response, closure_list); - gpr_free(response.body); +static int +httpcli_get_openid_config (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, gpr_strdup (good_openid_config)); + GPR_ASSERT (request->handshaker == &grpc_httpcli_ssl); + GPR_ASSERT (strcmp (request->host, "accounts.google.com") == 0); + GPR_ASSERT (strcmp (request->path, GRPC_OPENID_CONFIG_URL_SUFFIX) == 0); + grpc_httpcli_set_override (httpcli_get_jwk_set, httpcli_post_should_not_be_called); + on_response (user_data, &response, closure_list); + gpr_free (response.body); return 1; } -static void test_jwt_verifier_url_issuer_success(void) { +static void +test_jwt_verifier_url_issuer_success (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (NULL, 0); char *jwt = NULL; - char *key_str = json_key_str(json_key_str_part3_for_url_issuer); - grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); - gpr_free(key_str); - GPR_ASSERT(grpc_auth_json_key_is_valid(&key)); - grpc_httpcli_set_override(httpcli_get_openid_config, - httpcli_post_should_not_be_called); - jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, - NULL); - grpc_auth_json_key_destruct(&key); - GPR_ASSERT(jwt != NULL); - grpc_jwt_verifier_verify(verifier, NULL, jwt, expected_audience, - on_verification_success, (void *)expected_user_data, - &closure_list); - gpr_free(jwt); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + char *key_str = json_key_str (json_key_str_part3_for_url_issuer); + grpc_auth_json_key key = grpc_auth_json_key_create_from_string (key_str); + gpr_free (key_str); + GPR_ASSERT (grpc_auth_json_key_is_valid (&key)); + grpc_httpcli_set_override (httpcli_get_openid_config, httpcli_post_should_not_be_called); + jwt = grpc_jwt_encode_and_sign (&key, expected_audience, expected_lifetime, NULL); + grpc_auth_json_key_destruct (&key); + GPR_ASSERT (jwt != NULL); + grpc_jwt_verifier_verify (verifier, NULL, jwt, expected_audience, on_verification_success, (void *) expected_user_data, &closure_list); + gpr_free (jwt); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static void on_verification_key_retrieval_error(void *user_data, - grpc_jwt_verifier_status status, - grpc_jwt_claims *claims) { - GPR_ASSERT(status == GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR); - GPR_ASSERT(claims == NULL); - GPR_ASSERT(user_data == (void *)expected_user_data); +static void +on_verification_key_retrieval_error (void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims * claims) +{ + GPR_ASSERT (status == GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR); + GPR_ASSERT (claims == NULL); + GPR_ASSERT (user_data == (void *) expected_user_data); } -static int httpcli_get_bad_json(const grpc_httpcli_request *request, - gpr_timespec deadline, - grpc_httpcli_response_cb on_response, - void *user_data, - grpc_closure_list *closure_list) { - grpc_httpcli_response response = - http_response(200, gpr_strdup("{\"bad\": \"stuff\"}")); - GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl); - on_response(user_data, &response, closure_list); - gpr_free(response.body); +static int +httpcli_get_bad_json (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + grpc_httpcli_response response = http_response (200, gpr_strdup ("{\"bad\": \"stuff\"}")); + GPR_ASSERT (request->handshaker == &grpc_httpcli_ssl); + on_response (user_data, &response, closure_list); + gpr_free (response.body); return 1; } -static void test_jwt_verifier_url_issuer_bad_config(void) { +static void +test_jwt_verifier_url_issuer_bad_config (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (NULL, 0); char *jwt = NULL; - char *key_str = json_key_str(json_key_str_part3_for_url_issuer); - grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); - gpr_free(key_str); - GPR_ASSERT(grpc_auth_json_key_is_valid(&key)); - grpc_httpcli_set_override(httpcli_get_bad_json, - httpcli_post_should_not_be_called); - jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, - NULL); - grpc_auth_json_key_destruct(&key); - GPR_ASSERT(jwt != NULL); - grpc_jwt_verifier_verify(verifier, NULL, jwt, expected_audience, - on_verification_key_retrieval_error, - (void *)expected_user_data, &closure_list); - gpr_free(jwt); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + char *key_str = json_key_str (json_key_str_part3_for_url_issuer); + grpc_auth_json_key key = grpc_auth_json_key_create_from_string (key_str); + gpr_free (key_str); + GPR_ASSERT (grpc_auth_json_key_is_valid (&key)); + grpc_httpcli_set_override (httpcli_get_bad_json, httpcli_post_should_not_be_called); + jwt = grpc_jwt_encode_and_sign (&key, expected_audience, expected_lifetime, NULL); + grpc_auth_json_key_destruct (&key); + GPR_ASSERT (jwt != NULL); + grpc_jwt_verifier_verify (verifier, NULL, jwt, expected_audience, on_verification_key_retrieval_error, (void *) expected_user_data, &closure_list); + gpr_free (jwt); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static void test_jwt_verifier_bad_json_key(void) { +static void +test_jwt_verifier_bad_json_key (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (NULL, 0); char *jwt = NULL; - char *key_str = json_key_str(json_key_str_part3_for_google_email_issuer); - grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); - gpr_free(key_str); - GPR_ASSERT(grpc_auth_json_key_is_valid(&key)); - grpc_httpcli_set_override(httpcli_get_bad_json, - httpcli_post_should_not_be_called); - jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, - NULL); - grpc_auth_json_key_destruct(&key); - GPR_ASSERT(jwt != NULL); - grpc_jwt_verifier_verify(verifier, NULL, jwt, expected_audience, - on_verification_key_retrieval_error, - (void *)expected_user_data, &closure_list); - gpr_free(jwt); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + char *key_str = json_key_str (json_key_str_part3_for_google_email_issuer); + grpc_auth_json_key key = grpc_auth_json_key_create_from_string (key_str); + gpr_free (key_str); + GPR_ASSERT (grpc_auth_json_key_is_valid (&key)); + grpc_httpcli_set_override (httpcli_get_bad_json, httpcli_post_should_not_be_called); + jwt = grpc_jwt_encode_and_sign (&key, expected_audience, expected_lifetime, NULL); + grpc_auth_json_key_destruct (&key); + GPR_ASSERT (jwt != NULL); + grpc_jwt_verifier_verify (verifier, NULL, jwt, expected_audience, on_verification_key_retrieval_error, (void *) expected_user_data, &closure_list); + gpr_free (jwt); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static void corrupt_jwt_sig(char *jwt) { +static void +corrupt_jwt_sig (char *jwt) +{ gpr_slice sig; char *bad_b64_sig; gpr_uint8 *sig_bytes; - char *last_dot = strrchr(jwt, '.'); - GPR_ASSERT(last_dot != NULL); - sig = grpc_base64_decode(last_dot + 1, 1); - GPR_ASSERT(!GPR_SLICE_IS_EMPTY(sig)); - sig_bytes = GPR_SLICE_START_PTR(sig); - (*sig_bytes)++; /* Corrupt first byte. */ - bad_b64_sig = - grpc_base64_encode(GPR_SLICE_START_PTR(sig), GPR_SLICE_LENGTH(sig), 1, 0); - memcpy(last_dot + 1, bad_b64_sig, strlen(bad_b64_sig)); - gpr_free(bad_b64_sig); - gpr_slice_unref(sig); + char *last_dot = strrchr (jwt, '.'); + GPR_ASSERT (last_dot != NULL); + sig = grpc_base64_decode (last_dot + 1, 1); + GPR_ASSERT (!GPR_SLICE_IS_EMPTY (sig)); + sig_bytes = GPR_SLICE_START_PTR (sig); + (*sig_bytes)++; /* Corrupt first byte. */ + bad_b64_sig = grpc_base64_encode (GPR_SLICE_START_PTR (sig), GPR_SLICE_LENGTH (sig), 1, 0); + memcpy (last_dot + 1, bad_b64_sig, strlen (bad_b64_sig)); + gpr_free (bad_b64_sig); + gpr_slice_unref (sig); } -static void on_verification_bad_signature(void *user_data, - grpc_jwt_verifier_status status, - grpc_jwt_claims *claims) { - GPR_ASSERT(status == GRPC_JWT_VERIFIER_BAD_SIGNATURE); - GPR_ASSERT(claims == NULL); - GPR_ASSERT(user_data == (void *)expected_user_data); +static void +on_verification_bad_signature (void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims * claims) +{ + GPR_ASSERT (status == GRPC_JWT_VERIFIER_BAD_SIGNATURE); + GPR_ASSERT (claims == NULL); + GPR_ASSERT (user_data == (void *) expected_user_data); } -static void test_jwt_verifier_bad_signature(void) { +static void +test_jwt_verifier_bad_signature (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (NULL, 0); char *jwt = NULL; - char *key_str = json_key_str(json_key_str_part3_for_url_issuer); - grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); - gpr_free(key_str); - GPR_ASSERT(grpc_auth_json_key_is_valid(&key)); - grpc_httpcli_set_override(httpcli_get_openid_config, - httpcli_post_should_not_be_called); - jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, - NULL); - grpc_auth_json_key_destruct(&key); - corrupt_jwt_sig(jwt); - GPR_ASSERT(jwt != NULL); - grpc_jwt_verifier_verify(verifier, NULL, jwt, expected_audience, - on_verification_bad_signature, - (void *)expected_user_data, &closure_list); - gpr_free(jwt); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + char *key_str = json_key_str (json_key_str_part3_for_url_issuer); + grpc_auth_json_key key = grpc_auth_json_key_create_from_string (key_str); + gpr_free (key_str); + GPR_ASSERT (grpc_auth_json_key_is_valid (&key)); + grpc_httpcli_set_override (httpcli_get_openid_config, httpcli_post_should_not_be_called); + jwt = grpc_jwt_encode_and_sign (&key, expected_audience, expected_lifetime, NULL); + grpc_auth_json_key_destruct (&key); + corrupt_jwt_sig (jwt); + GPR_ASSERT (jwt != NULL); + grpc_jwt_verifier_verify (verifier, NULL, jwt, expected_audience, on_verification_bad_signature, (void *) expected_user_data, &closure_list); + gpr_free (jwt); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } -static int httpcli_get_should_not_be_called( - const grpc_httpcli_request *request, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data, - grpc_closure_list *closure_list) { - GPR_ASSERT(0); +static int +httpcli_get_should_not_be_called (const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, grpc_closure_list * closure_list) +{ + GPR_ASSERT (0); return 1; } -static void on_verification_bad_format(void *user_data, - grpc_jwt_verifier_status status, - grpc_jwt_claims *claims) { - GPR_ASSERT(status == GRPC_JWT_VERIFIER_BAD_FORMAT); - GPR_ASSERT(claims == NULL); - GPR_ASSERT(user_data == (void *)expected_user_data); +static void +on_verification_bad_format (void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims * claims) +{ + GPR_ASSERT (status == GRPC_JWT_VERIFIER_BAD_FORMAT); + GPR_ASSERT (claims == NULL); + GPR_ASSERT (user_data == (void *) expected_user_data); } -static void test_jwt_verifier_bad_format(void) { +static void +test_jwt_verifier_bad_format (void) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0); - grpc_httpcli_set_override(httpcli_get_should_not_be_called, - httpcli_post_should_not_be_called); - grpc_jwt_verifier_verify(verifier, NULL, "bad jwt", expected_audience, - on_verification_bad_format, - (void *)expected_user_data, &closure_list); - grpc_jwt_verifier_destroy(verifier); - grpc_httpcli_set_override(NULL, NULL); - grpc_closure_list_run(&closure_list); + grpc_jwt_verifier *verifier = grpc_jwt_verifier_create (NULL, 0); + grpc_httpcli_set_override (httpcli_get_should_not_be_called, httpcli_post_should_not_be_called); + grpc_jwt_verifier_verify (verifier, NULL, "bad jwt", expected_audience, on_verification_bad_format, (void *) expected_user_data, &closure_list); + grpc_jwt_verifier_destroy (verifier); + grpc_httpcli_set_override (NULL, NULL); + grpc_closure_list_run (&closure_list); } /* find verification key: bad jks, cannot find key in jks */ /* bad signature custom provided email*/ /* bad key */ -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_claims_success(); - test_expired_claims_failure(); - test_invalid_claims_failure(); - test_bad_audience_claims_failure(); - test_jwt_verifier_google_email_issuer_success(); - test_jwt_verifier_custom_email_issuer_success(); - test_jwt_verifier_url_issuer_success(); - test_jwt_verifier_url_issuer_bad_config(); - test_jwt_verifier_bad_json_key(); - test_jwt_verifier_bad_signature(); - test_jwt_verifier_bad_format(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_claims_success (); + test_expired_claims_failure (); + test_invalid_claims_failure (); + test_bad_audience_claims_failure (); + test_jwt_verifier_google_email_issuer_success (); + test_jwt_verifier_custom_email_issuer_success (); + test_jwt_verifier_url_issuer_success (); + test_jwt_verifier_url_issuer_bad_config (); + test_jwt_verifier_bad_json_key (); + test_jwt_verifier_bad_signature (); + test_jwt_verifier_bad_format (); return 0; } diff --git a/test/core/security/oauth2_utils.c b/test/core/security/oauth2_utils.c index 30ad00cfbc..a490ba4668 100644 --- a/test/core/security/oauth2_utils.c +++ b/test/core/security/oauth2_utils.c @@ -44,62 +44,68 @@ #include "src/core/security/credentials.h" -typedef struct { +typedef struct +{ grpc_pollset pollset; int is_done; char *token; } oauth2_request; -static void on_oauth2_response(void *user_data, grpc_credentials_md *md_elems, - size_t num_md, grpc_credentials_status status, - grpc_closure_list *closure_list) { +static void +on_oauth2_response (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ oauth2_request *request = user_data; char *token = NULL; gpr_slice token_slice; - if (status == GRPC_CREDENTIALS_ERROR) { - gpr_log(GPR_ERROR, "Fetching token failed."); - } else { - GPR_ASSERT(num_md == 1); - token_slice = md_elems[0].value; - token = gpr_malloc(GPR_SLICE_LENGTH(token_slice) + 1); - memcpy(token, GPR_SLICE_START_PTR(token_slice), - GPR_SLICE_LENGTH(token_slice)); - token[GPR_SLICE_LENGTH(token_slice)] = '\0'; - } - gpr_mu_lock(GRPC_POLLSET_MU(&request->pollset)); + if (status == GRPC_CREDENTIALS_ERROR) + { + gpr_log (GPR_ERROR, "Fetching token failed."); + } + else + { + GPR_ASSERT (num_md == 1); + token_slice = md_elems[0].value; + token = gpr_malloc (GPR_SLICE_LENGTH (token_slice) + 1); + memcpy (token, GPR_SLICE_START_PTR (token_slice), GPR_SLICE_LENGTH (token_slice)); + token[GPR_SLICE_LENGTH (token_slice)] = '\0'; + } + gpr_mu_lock (GRPC_POLLSET_MU (&request->pollset)); request->is_done = 1; request->token = token; - grpc_pollset_kick(&request->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&request->pollset)); + grpc_pollset_kick (&request->pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&request->pollset)); } -static void do_nothing(void *unused, int success, - grpc_closure_list *closure_list) {} +static void +do_nothing (void *unused, int success, grpc_closure_list * closure_list) +{ +} -char *grpc_test_fetch_oauth2_token_with_credentials(grpc_credentials *creds) { +char * +grpc_test_fetch_oauth2_token_with_credentials (grpc_credentials * creds) +{ oauth2_request request; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_closure do_nothing_closure; - grpc_pollset_init(&request.pollset); + grpc_pollset_init (&request.pollset); request.is_done = 0; - grpc_closure_init(&do_nothing_closure, do_nothing, NULL); + grpc_closure_init (&do_nothing_closure, do_nothing, NULL); - grpc_credentials_get_request_metadata( - creds, &request.pollset, "", on_oauth2_response, &request, &closure_list); + grpc_credentials_get_request_metadata (creds, &request.pollset, "", on_oauth2_response, &request, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_closure_list_run (&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&request.pollset)); - while (!request.is_done) { - grpc_pollset_worker worker; - grpc_pollset_work(&request.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&request.pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&request.pollset)); + while (!request.is_done) + { + grpc_pollset_worker worker; + grpc_pollset_work (&request.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&request.pollset)); - grpc_pollset_shutdown(&request.pollset, &do_nothing_closure, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_pollset_destroy(&request.pollset); + grpc_pollset_shutdown (&request.pollset, &do_nothing_closure, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_pollset_destroy (&request.pollset); return request.token; } diff --git a/test/core/security/oauth2_utils.h b/test/core/security/oauth2_utils.h index 8082351b8a..a2eb06834d 100644 --- a/test/core/security/oauth2_utils.h +++ b/test/core/security/oauth2_utils.h @@ -37,15 +37,16 @@ #include "src/core/security/credentials.h" #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* Fetch oauth2 access token with a credentials object. Does not take ownership. Returns NULL on a failure. The caller should call gpr_free on the token. */ -char *grpc_test_fetch_oauth2_token_with_credentials(grpc_credentials *creds); + char *grpc_test_fetch_oauth2_token_with_credentials (grpc_credentials * creds); #ifdef __cplusplus } #endif -#endif /* GRPC_TEST_CORE_SECURITY_OAUTH2_UTILS_H */ +#endif /* GRPC_TEST_CORE_SECURITY_OAUTH2_UTILS_H */ diff --git a/test/core/security/print_google_default_creds_token.c b/test/core/security/print_google_default_creds_token.c index 646109c0a1..faac2c7846 100644 --- a/test/core/security/print_google_default_creds_token.c +++ b/test/core/security/print_google_default_creds_token.c @@ -44,72 +44,76 @@ #include <grpc/support/slice.h> #include <grpc/support/sync.h> -typedef struct { +typedef struct +{ grpc_pollset pollset; int is_done; } synchronizer; -static void on_metadata_response(void *user_data, grpc_credentials_md *md_elems, - size_t num_md, grpc_credentials_status status, - grpc_closure_list *closure_list) { +static void +on_metadata_response (void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status, grpc_closure_list * closure_list) +{ synchronizer *sync = user_data; - if (status == GRPC_CREDENTIALS_ERROR) { - fprintf(stderr, "Fetching token failed.\n"); - } else { - char *token; - GPR_ASSERT(num_md == 1); - token = gpr_dump_slice(md_elems[0].value, GPR_DUMP_ASCII); - printf("\nGot token: %s\n\n", token); - gpr_free(token); - } - gpr_mu_lock(GRPC_POLLSET_MU(&sync->pollset)); + if (status == GRPC_CREDENTIALS_ERROR) + { + fprintf (stderr, "Fetching token failed.\n"); + } + else + { + char *token; + GPR_ASSERT (num_md == 1); + token = gpr_dump_slice (md_elems[0].value, GPR_DUMP_ASCII); + printf ("\nGot token: %s\n\n", token); + gpr_free (token); + } + gpr_mu_lock (GRPC_POLLSET_MU (&sync->pollset)); sync->is_done = 1; - grpc_pollset_kick(&sync->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&sync->pollset)); + grpc_pollset_kick (&sync->pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&sync->pollset)); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ int result = 0; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; synchronizer sync; grpc_credentials *creds = NULL; char *service_url = "https://test.foo.google.com/Foo"; - gpr_cmdline *cl = gpr_cmdline_create("print_google_default_creds_token"); - gpr_cmdline_add_string(cl, "service_url", - "Service URL for the token request.", &service_url); - gpr_cmdline_parse(cl, argc, argv); + gpr_cmdline *cl = gpr_cmdline_create ("print_google_default_creds_token"); + gpr_cmdline_add_string (cl, "service_url", "Service URL for the token request.", &service_url); + gpr_cmdline_parse (cl, argc, argv); - grpc_init(); + grpc_init (); - creds = grpc_google_default_credentials_create(); - if (creds == NULL) { - fprintf(stderr, "\nCould not find default credentials.\n\n"); - result = 1; - goto end; - } + creds = grpc_google_default_credentials_create (); + if (creds == NULL) + { + fprintf (stderr, "\nCould not find default credentials.\n\n"); + result = 1; + goto end; + } - grpc_pollset_init(&sync.pollset); + grpc_pollset_init (&sync.pollset); sync.is_done = 0; - grpc_credentials_get_request_metadata(creds, &sync.pollset, service_url, - on_metadata_response, &sync, - &closure_list); + grpc_credentials_get_request_metadata (creds, &sync.pollset, service_url, on_metadata_response, &sync, &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&sync.pollset)); - while (!sync.is_done) { - grpc_pollset_worker worker; - grpc_pollset_work(&sync.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&sync.pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&sync.pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&sync.pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&sync.pollset)); + while (!sync.is_done) + { + grpc_pollset_worker worker; + grpc_pollset_work (&sync.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&sync.pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&sync.pollset)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&sync.pollset)); - grpc_credentials_release(creds); + grpc_credentials_release (creds); end: - gpr_cmdline_destroy(cl); - grpc_shutdown(); + gpr_cmdline_destroy (cl); + grpc_shutdown (); return result; } diff --git a/test/core/security/secure_endpoint_test.c b/test/core/security/secure_endpoint_test.c index f68fde4c0a..32db8194e4 100644 --- a/test/core/security/secure_endpoint_test.c +++ b/test/core/security/secure_endpoint_test.c @@ -47,149 +47,158 @@ static grpc_pollset g_pollset; -static grpc_endpoint_test_fixture secure_endpoint_create_fixture_tcp_socketpair( - size_t slice_size, gpr_slice *leftover_slices, size_t leftover_nslices) { +static grpc_endpoint_test_fixture +secure_endpoint_create_fixture_tcp_socketpair (size_t slice_size, gpr_slice * leftover_slices, size_t leftover_nslices) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - tsi_frame_protector *fake_read_protector = tsi_create_fake_protector(NULL); - tsi_frame_protector *fake_write_protector = tsi_create_fake_protector(NULL); + tsi_frame_protector *fake_read_protector = tsi_create_fake_protector (NULL); + tsi_frame_protector *fake_write_protector = tsi_create_fake_protector (NULL); grpc_endpoint_test_fixture f; grpc_endpoint_pair tcp; - tcp = grpc_iomgr_create_endpoint_pair("fixture", slice_size); - grpc_endpoint_add_to_pollset(tcp.client, &g_pollset, &closure_list); - grpc_endpoint_add_to_pollset(tcp.server, &g_pollset, &closure_list); - - if (leftover_nslices == 0) { - f.client_ep = - grpc_secure_endpoint_create(fake_read_protector, tcp.client, NULL, 0); - } else { - unsigned i; - tsi_result result; - size_t still_pending_size; - size_t total_buffer_size = 8192; - size_t buffer_size = total_buffer_size; - gpr_uint8 *encrypted_buffer = gpr_malloc(buffer_size); - gpr_uint8 *cur = encrypted_buffer; - gpr_slice encrypted_leftover; - for (i = 0; i < leftover_nslices; i++) { - gpr_slice plain = leftover_slices[i]; - gpr_uint8 *message_bytes = GPR_SLICE_START_PTR(plain); - size_t message_size = GPR_SLICE_LENGTH(plain); - while (message_size > 0) { - size_t protected_buffer_size_to_send = buffer_size; - size_t processed_message_size = message_size; - result = tsi_frame_protector_protect( - fake_write_protector, message_bytes, &processed_message_size, cur, - &protected_buffer_size_to_send); - GPR_ASSERT(result == TSI_OK); - message_bytes += processed_message_size; - message_size -= processed_message_size; - cur += protected_buffer_size_to_send; - GPR_ASSERT(buffer_size >= protected_buffer_size_to_send); - buffer_size -= protected_buffer_size_to_send; - } - gpr_slice_unref(plain); + tcp = grpc_iomgr_create_endpoint_pair ("fixture", slice_size); + grpc_endpoint_add_to_pollset (tcp.client, &g_pollset, &closure_list); + grpc_endpoint_add_to_pollset (tcp.server, &g_pollset, &closure_list); + + if (leftover_nslices == 0) + { + f.client_ep = grpc_secure_endpoint_create (fake_read_protector, tcp.client, NULL, 0); + } + else + { + unsigned i; + tsi_result result; + size_t still_pending_size; + size_t total_buffer_size = 8192; + size_t buffer_size = total_buffer_size; + gpr_uint8 *encrypted_buffer = gpr_malloc (buffer_size); + gpr_uint8 *cur = encrypted_buffer; + gpr_slice encrypted_leftover; + for (i = 0; i < leftover_nslices; i++) + { + gpr_slice plain = leftover_slices[i]; + gpr_uint8 *message_bytes = GPR_SLICE_START_PTR (plain); + size_t message_size = GPR_SLICE_LENGTH (plain); + while (message_size > 0) + { + size_t protected_buffer_size_to_send = buffer_size; + size_t processed_message_size = message_size; + result = tsi_frame_protector_protect (fake_write_protector, message_bytes, &processed_message_size, cur, &protected_buffer_size_to_send); + GPR_ASSERT (result == TSI_OK); + message_bytes += processed_message_size; + message_size -= processed_message_size; + cur += protected_buffer_size_to_send; + GPR_ASSERT (buffer_size >= protected_buffer_size_to_send); + buffer_size -= protected_buffer_size_to_send; + } + gpr_slice_unref (plain); + } + do + { + size_t protected_buffer_size_to_send = buffer_size; + result = tsi_frame_protector_protect_flush (fake_write_protector, cur, &protected_buffer_size_to_send, &still_pending_size); + GPR_ASSERT (result == TSI_OK); + cur += protected_buffer_size_to_send; + GPR_ASSERT (buffer_size >= protected_buffer_size_to_send); + buffer_size -= protected_buffer_size_to_send; + } + while (still_pending_size > 0); + encrypted_leftover = gpr_slice_from_copied_buffer ((const char *) encrypted_buffer, total_buffer_size - buffer_size); + f.client_ep = grpc_secure_endpoint_create (fake_read_protector, tcp.client, &encrypted_leftover, 1); + gpr_slice_unref (encrypted_leftover); + gpr_free (encrypted_buffer); } - do { - size_t protected_buffer_size_to_send = buffer_size; - result = tsi_frame_protector_protect_flush(fake_write_protector, cur, - &protected_buffer_size_to_send, - &still_pending_size); - GPR_ASSERT(result == TSI_OK); - cur += protected_buffer_size_to_send; - GPR_ASSERT(buffer_size >= protected_buffer_size_to_send); - buffer_size -= protected_buffer_size_to_send; - } while (still_pending_size > 0); - encrypted_leftover = gpr_slice_from_copied_buffer( - (const char *)encrypted_buffer, total_buffer_size - buffer_size); - f.client_ep = grpc_secure_endpoint_create(fake_read_protector, tcp.client, - &encrypted_leftover, 1); - gpr_slice_unref(encrypted_leftover); - gpr_free(encrypted_buffer); - } - - f.server_ep = - grpc_secure_endpoint_create(fake_write_protector, tcp.server, NULL, 0); - grpc_closure_list_run(&closure_list); + + f.server_ep = grpc_secure_endpoint_create (fake_write_protector, tcp.server, NULL, 0); + grpc_closure_list_run (&closure_list); return f; } static grpc_endpoint_test_fixture -secure_endpoint_create_fixture_tcp_socketpair_noleftover(size_t slice_size) { - return secure_endpoint_create_fixture_tcp_socketpair(slice_size, NULL, 0); +secure_endpoint_create_fixture_tcp_socketpair_noleftover (size_t slice_size) +{ + return secure_endpoint_create_fixture_tcp_socketpair (slice_size, NULL, 0); } static grpc_endpoint_test_fixture -secure_endpoint_create_fixture_tcp_socketpair_leftover(size_t slice_size) { - gpr_slice s = - gpr_slice_from_copied_string("hello world 12345678900987654321"); +secure_endpoint_create_fixture_tcp_socketpair_leftover (size_t slice_size) +{ + gpr_slice s = gpr_slice_from_copied_string ("hello world 12345678900987654321"); grpc_endpoint_test_fixture f; - f = secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1); + f = secure_endpoint_create_fixture_tcp_socketpair (slice_size, &s, 1); return f; } -static void clean_up(void) {} +static void +clean_up (void) +{ +} static grpc_endpoint_test_config configs[] = { - {"secure_ep/tcp_socketpair", - secure_endpoint_create_fixture_tcp_socketpair_noleftover, clean_up}, - {"secure_ep/tcp_socketpair_leftover", - secure_endpoint_create_fixture_tcp_socketpair_leftover, clean_up}, + {"secure_ep/tcp_socketpair", + secure_endpoint_create_fixture_tcp_socketpair_noleftover, clean_up}, + {"secure_ep/tcp_socketpair_leftover", + secure_endpoint_create_fixture_tcp_socketpair_leftover, clean_up}, }; -static void inc_call_ctr(void *arg, int success, - grpc_closure_list *closure_list) { - ++*(int *)arg; +static void +inc_call_ctr (void *arg, int success, grpc_closure_list * closure_list) +{ + ++*(int *) arg; } -static void test_leftover(grpc_endpoint_test_config config, size_t slice_size) { - grpc_endpoint_test_fixture f = config.create_fixture(slice_size); +static void +test_leftover (grpc_endpoint_test_config config, size_t slice_size) +{ + grpc_endpoint_test_fixture f = config.create_fixture (slice_size); gpr_slice_buffer incoming; - gpr_slice s = - gpr_slice_from_copied_string("hello world 12345678900987654321"); + gpr_slice s = gpr_slice_from_copied_string ("hello world 12345678900987654321"); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; int n = 0; grpc_closure done_closure; - gpr_log(GPR_INFO, "Start test left over"); - - gpr_slice_buffer_init(&incoming); - grpc_closure_init(&done_closure, inc_call_ctr, &n); - grpc_endpoint_read(f.client_ep, &incoming, &done_closure, &closure_list); - grpc_closure_list_run(&closure_list); - GPR_ASSERT(n == 1); - GPR_ASSERT(incoming.count == 1); - GPR_ASSERT(0 == gpr_slice_cmp(s, incoming.slices[0])); - - grpc_endpoint_shutdown(f.client_ep, &closure_list); - grpc_endpoint_shutdown(f.server_ep, &closure_list); - grpc_endpoint_destroy(f.client_ep, &closure_list); - grpc_endpoint_destroy(f.server_ep, &closure_list); - grpc_closure_list_run(&closure_list); - gpr_slice_unref(s); - gpr_slice_buffer_destroy(&incoming); - - clean_up(); + gpr_log (GPR_INFO, "Start test left over"); + + gpr_slice_buffer_init (&incoming); + grpc_closure_init (&done_closure, inc_call_ctr, &n); + grpc_endpoint_read (f.client_ep, &incoming, &done_closure, &closure_list); + grpc_closure_list_run (&closure_list); + GPR_ASSERT (n == 1); + GPR_ASSERT (incoming.count == 1); + GPR_ASSERT (0 == gpr_slice_cmp (s, incoming.slices[0])); + + grpc_endpoint_shutdown (f.client_ep, &closure_list); + grpc_endpoint_shutdown (f.server_ep, &closure_list); + grpc_endpoint_destroy (f.client_ep, &closure_list); + grpc_endpoint_destroy (f.server_ep, &closure_list); + grpc_closure_list_run (&closure_list); + gpr_slice_unref (s); + gpr_slice_buffer_destroy (&incoming); + + clean_up (); } -static void destroy_pollset(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); +static void +destroy_pollset (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_closure destroyed; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - grpc_test_init(argc, argv); - - grpc_init(); - grpc_pollset_init(&g_pollset); - grpc_endpoint_tests(configs[0], &g_pollset); - test_leftover(configs[1], 1); - grpc_closure_init(&destroyed, destroy_pollset, &g_pollset); - grpc_pollset_shutdown(&g_pollset, &destroyed, &closure_list); - grpc_closure_list_run(&closure_list); - grpc_shutdown(); + grpc_test_init (argc, argv); + + grpc_init (); + grpc_pollset_init (&g_pollset); + grpc_endpoint_tests (configs[0], &g_pollset); + test_leftover (configs[1], 1); + grpc_closure_init (&destroyed, destroy_pollset, &g_pollset); + grpc_pollset_shutdown (&g_pollset, &destroyed, &closure_list); + grpc_closure_list_run (&closure_list); + grpc_shutdown (); return 0; } diff --git a/test/core/security/security_connector_test.c b/test/core/security/security_connector_test.c index 3f6c592b0b..691c68685b 100644 --- a/test/core/security/security_connector_test.c +++ b/test/core/security/security_connector_test.c @@ -46,212 +46,200 @@ #include <grpc/support/log.h> #include <grpc/support/useful.h> -static int check_transport_security_type(const grpc_auth_context *ctx) { - grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name( - ctx, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME); - const grpc_auth_property *prop = grpc_auth_property_iterator_next(&it); - if (prop == NULL) return 0; - if (strncmp(prop->value, GRPC_SSL_TRANSPORT_SECURITY_TYPE, - prop->value_length) != 0) { +static int +check_transport_security_type (const grpc_auth_context * ctx) +{ + grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name (ctx, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME); + const grpc_auth_property *prop = grpc_auth_property_iterator_next (&it); + if (prop == NULL) return 0; - } + if (strncmp (prop->value, GRPC_SSL_TRANSPORT_SECURITY_TYPE, prop->value_length) != 0) + { + return 0; + } /* Check that we have only one property with this name. */ - if (grpc_auth_property_iterator_next(&it) != NULL) return 0; + if (grpc_auth_property_iterator_next (&it) != NULL) + return 0; return 1; } -static void test_unauthenticated_ssl_peer(void) { +static void +test_unauthenticated_ssl_peer (void) +{ tsi_peer peer; grpc_auth_context *ctx; - GPR_ASSERT(tsi_construct_peer(1, &peer) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, - &peer.properties[0]) == TSI_OK); - ctx = tsi_ssl_peer_to_auth_context(&peer); - GPR_ASSERT(ctx != NULL); - GPR_ASSERT(!grpc_auth_context_peer_is_authenticated(ctx)); - GPR_ASSERT(check_transport_security_type(ctx)); - - tsi_peer_destruct(&peer); - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + GPR_ASSERT (tsi_construct_peer (1, &peer) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer.properties[0]) == TSI_OK); + ctx = tsi_ssl_peer_to_auth_context (&peer); + GPR_ASSERT (ctx != NULL); + GPR_ASSERT (!grpc_auth_context_peer_is_authenticated (ctx)); + GPR_ASSERT (check_transport_security_type (ctx)); + + tsi_peer_destruct (&peer); + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -static int check_identity(const grpc_auth_context *ctx, - const char *expected_property_name, - const char **expected_identities, - size_t num_identities) { +static int +check_identity (const grpc_auth_context * ctx, const char *expected_property_name, const char **expected_identities, size_t num_identities) +{ grpc_auth_property_iterator it; const grpc_auth_property *prop; size_t i; - GPR_ASSERT(grpc_auth_context_peer_is_authenticated(ctx)); - it = grpc_auth_context_peer_identity(ctx); - for (i = 0; i < num_identities; i++) { - prop = grpc_auth_property_iterator_next(&it); - if (prop == NULL) { - gpr_log(GPR_ERROR, "Expected identity value %s not found.", - expected_identities[i]); + GPR_ASSERT (grpc_auth_context_peer_is_authenticated (ctx)); + it = grpc_auth_context_peer_identity (ctx); + for (i = 0; i < num_identities; i++) + { + prop = grpc_auth_property_iterator_next (&it); + if (prop == NULL) + { + gpr_log (GPR_ERROR, "Expected identity value %s not found.", expected_identities[i]); + return 0; + } + if (strcmp (prop->name, expected_property_name) != 0) + { + gpr_log (GPR_ERROR, "Expected peer identity property name %s and got %s.", expected_property_name, prop->name); + return 0; + } + if (strncmp (prop->value, expected_identities[i], prop->value_length) != 0) + { + gpr_log (GPR_ERROR, "Expected peer identity %s and got %s.", expected_identities[i], prop->value); + return 0; + } + } + return 1; +} + +static int +check_x509_cn (const grpc_auth_context * ctx, const char *expected_cn) +{ + grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name (ctx, GRPC_X509_CN_PROPERTY_NAME); + const grpc_auth_property *prop = grpc_auth_property_iterator_next (&it); + if (prop == NULL) + { + gpr_log (GPR_ERROR, "CN property not found."); return 0; } - if (strcmp(prop->name, expected_property_name) != 0) { - gpr_log(GPR_ERROR, "Expected peer identity property name %s and got %s.", - expected_property_name, prop->name); + if (strncmp (prop->value, expected_cn, prop->value_length) != 0) + { + gpr_log (GPR_ERROR, "Expected CN %s and got %s", expected_cn, prop->value); return 0; } - if (strncmp(prop->value, expected_identities[i], prop->value_length) != 0) { - gpr_log(GPR_ERROR, "Expected peer identity %s and got %s.", - expected_identities[i], prop->value); + if (grpc_auth_property_iterator_next (&it) != NULL) + { + gpr_log (GPR_ERROR, "Expected only one property for CN."); return 0; } - } - return 1; -} - -static int check_x509_cn(const grpc_auth_context *ctx, - const char *expected_cn) { - grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name( - ctx, GRPC_X509_CN_PROPERTY_NAME); - const grpc_auth_property *prop = grpc_auth_property_iterator_next(&it); - if (prop == NULL) { - gpr_log(GPR_ERROR, "CN property not found."); - return 0; - } - if (strncmp(prop->value, expected_cn, prop->value_length) != 0) { - gpr_log(GPR_ERROR, "Expected CN %s and got %s", expected_cn, prop->value); - return 0; - } - if (grpc_auth_property_iterator_next(&it) != NULL) { - gpr_log(GPR_ERROR, "Expected only one property for CN."); - return 0; - } return 1; } -static void test_cn_only_ssl_peer_to_auth_context(void) { +static void +test_cn_only_ssl_peer_to_auth_context (void) +{ tsi_peer peer; grpc_auth_context *ctx; const char *expected_cn = "cn1"; - GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, - &peer.properties[0]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, - &peer.properties[1]) == TSI_OK); - ctx = tsi_ssl_peer_to_auth_context(&peer); - GPR_ASSERT(ctx != NULL); - GPR_ASSERT(grpc_auth_context_peer_is_authenticated(ctx)); - GPR_ASSERT(check_identity(ctx, GRPC_X509_CN_PROPERTY_NAME, &expected_cn, 1)); - GPR_ASSERT(check_transport_security_type(ctx)); - GPR_ASSERT(check_x509_cn(ctx, expected_cn)); - - tsi_peer_destruct(&peer); - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + GPR_ASSERT (tsi_construct_peer (2, &peer) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer.properties[0]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, &peer.properties[1]) == TSI_OK); + ctx = tsi_ssl_peer_to_auth_context (&peer); + GPR_ASSERT (ctx != NULL); + GPR_ASSERT (grpc_auth_context_peer_is_authenticated (ctx)); + GPR_ASSERT (check_identity (ctx, GRPC_X509_CN_PROPERTY_NAME, &expected_cn, 1)); + GPR_ASSERT (check_transport_security_type (ctx)); + GPR_ASSERT (check_x509_cn (ctx, expected_cn)); + + tsi_peer_destruct (&peer); + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -static void test_cn_and_one_san_ssl_peer_to_auth_context(void) { +static void +test_cn_and_one_san_ssl_peer_to_auth_context (void) +{ tsi_peer peer; grpc_auth_context *ctx; const char *expected_cn = "cn1"; const char *expected_san = "san1"; - GPR_ASSERT(tsi_construct_peer(3, &peer) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, - &peer.properties[0]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, - &peer.properties[1]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, expected_san, - &peer.properties[2]) == TSI_OK); - ctx = tsi_ssl_peer_to_auth_context(&peer); - GPR_ASSERT(ctx != NULL); - GPR_ASSERT(grpc_auth_context_peer_is_authenticated(ctx)); - GPR_ASSERT( - check_identity(ctx, GRPC_X509_SAN_PROPERTY_NAME, &expected_san, 1)); - GPR_ASSERT(check_transport_security_type(ctx)); - GPR_ASSERT(check_x509_cn(ctx, expected_cn)); - - tsi_peer_destruct(&peer); - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + GPR_ASSERT (tsi_construct_peer (3, &peer) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer.properties[0]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, &peer.properties[1]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, expected_san, &peer.properties[2]) == TSI_OK); + ctx = tsi_ssl_peer_to_auth_context (&peer); + GPR_ASSERT (ctx != NULL); + GPR_ASSERT (grpc_auth_context_peer_is_authenticated (ctx)); + GPR_ASSERT (check_identity (ctx, GRPC_X509_SAN_PROPERTY_NAME, &expected_san, 1)); + GPR_ASSERT (check_transport_security_type (ctx)); + GPR_ASSERT (check_x509_cn (ctx, expected_cn)); + + tsi_peer_destruct (&peer); + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -static void test_cn_and_multiple_sans_ssl_peer_to_auth_context(void) { +static void +test_cn_and_multiple_sans_ssl_peer_to_auth_context (void) +{ tsi_peer peer; grpc_auth_context *ctx; const char *expected_cn = "cn1"; - const char *expected_sans[] = {"san1", "san2", "san3"}; + const char *expected_sans[] = { "san1", "san2", "san3" }; size_t i; - GPR_ASSERT(tsi_construct_peer(2 + GPR_ARRAY_SIZE(expected_sans), &peer) == - TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, - &peer.properties[0]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, - &peer.properties[1]) == TSI_OK); - for (i = 0; i < GPR_ARRAY_SIZE(expected_sans); i++) { - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, - expected_sans[i], &peer.properties[2 + i]) == TSI_OK); - } - ctx = tsi_ssl_peer_to_auth_context(&peer); - GPR_ASSERT(ctx != NULL); - GPR_ASSERT(grpc_auth_context_peer_is_authenticated(ctx)); - GPR_ASSERT(check_identity(ctx, GRPC_X509_SAN_PROPERTY_NAME, expected_sans, - GPR_ARRAY_SIZE(expected_sans))); - GPR_ASSERT(check_transport_security_type(ctx)); - GPR_ASSERT(check_x509_cn(ctx, expected_cn)); - - tsi_peer_destruct(&peer); - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + GPR_ASSERT (tsi_construct_peer (2 + GPR_ARRAY_SIZE (expected_sans), &peer) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer.properties[0]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, &peer.properties[1]) == TSI_OK); + for (i = 0; i < GPR_ARRAY_SIZE (expected_sans); i++) + { + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, expected_sans[i], &peer.properties[2 + i]) == TSI_OK); + } + ctx = tsi_ssl_peer_to_auth_context (&peer); + GPR_ASSERT (ctx != NULL); + GPR_ASSERT (grpc_auth_context_peer_is_authenticated (ctx)); + GPR_ASSERT (check_identity (ctx, GRPC_X509_SAN_PROPERTY_NAME, expected_sans, GPR_ARRAY_SIZE (expected_sans))); + GPR_ASSERT (check_transport_security_type (ctx)); + GPR_ASSERT (check_x509_cn (ctx, expected_cn)); + + tsi_peer_destruct (&peer); + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -static void test_cn_and_multiple_sans_and_others_ssl_peer_to_auth_context( - void) { +static void +test_cn_and_multiple_sans_and_others_ssl_peer_to_auth_context (void) +{ tsi_peer peer; grpc_auth_context *ctx; const char *expected_cn = "cn1"; - const char *expected_sans[] = {"san1", "san2", "san3"}; + const char *expected_sans[] = { "san1", "san2", "san3" }; size_t i; - GPR_ASSERT(tsi_construct_peer(4 + GPR_ARRAY_SIZE(expected_sans), &peer) == - TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, - &peer.properties[0]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - "foo", "bar", &peer.properties[1]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, - &peer.properties[2]) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - "chapi", "chapo", &peer.properties[3]) == TSI_OK); - for (i = 0; i < GPR_ARRAY_SIZE(expected_sans); i++) { - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, - expected_sans[i], &peer.properties[4 + i]) == TSI_OK); - } - ctx = tsi_ssl_peer_to_auth_context(&peer); - GPR_ASSERT(ctx != NULL); - GPR_ASSERT(grpc_auth_context_peer_is_authenticated(ctx)); - GPR_ASSERT(check_identity(ctx, GRPC_X509_SAN_PROPERTY_NAME, expected_sans, - GPR_ARRAY_SIZE(expected_sans))); - GPR_ASSERT(check_transport_security_type(ctx)); - GPR_ASSERT(check_x509_cn(ctx, expected_cn)); - - tsi_peer_destruct(&peer); - GRPC_AUTH_CONTEXT_UNREF(ctx, "test"); + GPR_ASSERT (tsi_construct_peer (4 + GPR_ARRAY_SIZE (expected_sans), &peer) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer.properties[0]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring ("foo", "bar", &peer.properties[1]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, expected_cn, &peer.properties[2]) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring ("chapi", "chapo", &peer.properties[3]) == TSI_OK); + for (i = 0; i < GPR_ARRAY_SIZE (expected_sans); i++) + { + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, expected_sans[i], &peer.properties[4 + i]) == TSI_OK); + } + ctx = tsi_ssl_peer_to_auth_context (&peer); + GPR_ASSERT (ctx != NULL); + GPR_ASSERT (grpc_auth_context_peer_is_authenticated (ctx)); + GPR_ASSERT (check_identity (ctx, GRPC_X509_SAN_PROPERTY_NAME, expected_sans, GPR_ARRAY_SIZE (expected_sans))); + GPR_ASSERT (check_transport_security_type (ctx)); + GPR_ASSERT (check_x509_cn (ctx, expected_cn)); + + tsi_peer_destruct (&peer); + GRPC_AUTH_CONTEXT_UNREF (ctx, "test"); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - grpc_init(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + grpc_init (); - test_unauthenticated_ssl_peer(); - test_cn_only_ssl_peer_to_auth_context(); - test_cn_and_one_san_ssl_peer_to_auth_context(); - test_cn_and_multiple_sans_ssl_peer_to_auth_context(); - test_cn_and_multiple_sans_and_others_ssl_peer_to_auth_context(); + test_unauthenticated_ssl_peer (); + test_cn_only_ssl_peer_to_auth_context (); + test_cn_and_one_san_ssl_peer_to_auth_context (); + test_cn_and_multiple_sans_ssl_peer_to_auth_context (); + test_cn_and_multiple_sans_and_others_ssl_peer_to_auth_context (); - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/security/verify_jwt.c b/test/core/security/verify_jwt.c index 9be6ee6e33..fd8aaeb484 100644 --- a/test/core/security/verify_jwt.c +++ b/test/core/security/verify_jwt.c @@ -43,47 +43,53 @@ #include <grpc/support/slice.h> #include <grpc/support/sync.h> -typedef struct { +typedef struct +{ grpc_pollset pollset; int is_done; int success; } synchronizer; -static void print_usage_and_exit(gpr_cmdline *cl, const char *argv0) { - char *usage = gpr_cmdline_usage_string(cl, argv0); - fprintf(stderr, "%s", usage); - gpr_free(usage); - gpr_cmdline_destroy(cl); - exit(1); +static void +print_usage_and_exit (gpr_cmdline * cl, const char *argv0) +{ + char *usage = gpr_cmdline_usage_string (cl, argv0); + fprintf (stderr, "%s", usage); + gpr_free (usage); + gpr_cmdline_destroy (cl); + exit (1); } -static void on_jwt_verification_done(void *user_data, - grpc_jwt_verifier_status status, - grpc_jwt_claims *claims) { +static void +on_jwt_verification_done (void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims * claims) +{ synchronizer *sync = user_data; sync->success = (status == GRPC_JWT_VERIFIER_OK); - if (sync->success) { - char *claims_str; - GPR_ASSERT(claims != NULL); - claims_str = - grpc_json_dump_to_string((grpc_json *)grpc_jwt_claims_json(claims), 2); - printf("Claims: \n\n%s\n", claims_str); - gpr_free(claims_str); - grpc_jwt_claims_destroy(claims); - } else { - GPR_ASSERT(claims == NULL); - fprintf(stderr, "Verification failed with error %s\n", - grpc_jwt_verifier_status_to_string(status)); - } + if (sync->success) + { + char *claims_str; + GPR_ASSERT (claims != NULL); + claims_str = grpc_json_dump_to_string ((grpc_json *) grpc_jwt_claims_json (claims), 2); + printf ("Claims: \n\n%s\n", claims_str); + gpr_free (claims_str); + grpc_jwt_claims_destroy (claims); + } + else + { + GPR_ASSERT (claims == NULL); + fprintf (stderr, "Verification failed with error %s\n", grpc_jwt_verifier_status_to_string (status)); + } - gpr_mu_lock(GRPC_POLLSET_MU(&sync->pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&sync->pollset)); sync->is_done = 1; - grpc_pollset_kick(&sync->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&sync->pollset)); + grpc_pollset_kick (&sync->pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&sync->pollset)); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ synchronizer sync; grpc_jwt_verifier *verifier; gpr_cmdline *cl; @@ -91,36 +97,36 @@ int main(int argc, char **argv) { char *aud = NULL; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - cl = gpr_cmdline_create("JWT verifier tool"); - gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt); - gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud); - gpr_cmdline_parse(cl, argc, argv); - if (jwt == NULL || aud == NULL) { - print_usage_and_exit(cl, argv[0]); - } + cl = gpr_cmdline_create ("JWT verifier tool"); + gpr_cmdline_add_string (cl, "jwt", "JSON web token to verify", &jwt); + gpr_cmdline_add_string (cl, "aud", "Audience for the JWT", &aud); + gpr_cmdline_parse (cl, argc, argv); + if (jwt == NULL || aud == NULL) + { + print_usage_and_exit (cl, argv[0]); + } - verifier = grpc_jwt_verifier_create(NULL, 0); + verifier = grpc_jwt_verifier_create (NULL, 0); - grpc_init(); + grpc_init (); - grpc_pollset_init(&sync.pollset); + grpc_pollset_init (&sync.pollset); sync.is_done = 0; - grpc_jwt_verifier_verify(verifier, &sync.pollset, jwt, aud, - on_jwt_verification_done, &sync, &closure_list); + grpc_jwt_verifier_verify (verifier, &sync.pollset, jwt, aud, on_jwt_verification_done, &sync, &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&sync.pollset)); - while (!sync.is_done) { - grpc_pollset_worker worker; - grpc_pollset_work(&sync.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - gpr_inf_future(GPR_CLOCK_MONOTONIC), &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&sync.pollset)); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&sync.pollset)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&sync.pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&sync.pollset)); + while (!sync.is_done) + { + grpc_pollset_worker worker; + grpc_pollset_work (&sync.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC), &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&sync.pollset)); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&sync.pollset)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&sync.pollset)); - grpc_jwt_verifier_destroy(verifier); - gpr_cmdline_destroy(cl); + grpc_jwt_verifier_destroy (verifier); + gpr_cmdline_destroy (cl); return !sync.success; } diff --git a/test/core/statistics/census_log_tests.c b/test/core/statistics/census_log_tests.c index 3292f8a64d..3622713e85 100644 --- a/test/core/statistics/census_log_tests.c +++ b/test/core/statistics/census_log_tests.c @@ -46,93 +46,104 @@ /* Fills in 'record' of size 'size'. Each byte in record is filled in with the same value. The value is extracted from 'record' pointer. */ -static void write_record(char* record, size_t size) { - char data = (gpr_uintptr)record % 255; - memset(record, data, size); +static void +write_record (char *record, size_t size) +{ + char data = (gpr_uintptr) record % 255; + memset (record, data, size); } /* Reads fixed size records. Returns the number of records read in 'num_records'. */ -static void read_records(size_t record_size, const char* buffer, - size_t buffer_size, gpr_int32* num_records) { +static void +read_records (size_t record_size, const char *buffer, size_t buffer_size, gpr_int32 * num_records) +{ gpr_int32 ix; - GPR_ASSERT(buffer_size >= record_size); - GPR_ASSERT(buffer_size % record_size == 0); + GPR_ASSERT (buffer_size >= record_size); + GPR_ASSERT (buffer_size % record_size == 0); *num_records = buffer_size / record_size; - for (ix = 0; ix < *num_records; ++ix) { - size_t jx; - const char* record = buffer + (record_size * ix); - char data = (gpr_uintptr)record % 255; - for (jx = 0; jx < record_size; ++jx) { - GPR_ASSERT(data == record[jx]); + for (ix = 0; ix < *num_records; ++ix) + { + size_t jx; + const char *record = buffer + (record_size * ix); + char data = (gpr_uintptr) record % 255; + for (jx = 0; jx < record_size; ++jx) + { + GPR_ASSERT (data == record[jx]); + } } - } } /* Tries to write the specified number of records. Stops when the log gets full. Returns the number of records written. Spins for random number of times, up to 'max_spin_count', between writes. */ -static size_t write_records_to_log(int writer_id, gpr_int32 record_size, - gpr_int32 num_records, - gpr_int32 max_spin_count) { +static size_t +write_records_to_log (int writer_id, gpr_int32 record_size, gpr_int32 num_records, gpr_int32 max_spin_count) +{ gpr_int32 ix; int counter = 0; - for (ix = 0; ix < num_records; ++ix) { - gpr_int32 jx; - gpr_int32 spin_count = max_spin_count ? rand() % max_spin_count : 0; - char* record; - if (counter++ == num_records / 10) { - printf(" Writer %d: %d out of %d written\n", writer_id, ix, - num_records); - counter = 0; + for (ix = 0; ix < num_records; ++ix) + { + gpr_int32 jx; + gpr_int32 spin_count = max_spin_count ? rand () % max_spin_count : 0; + char *record; + if (counter++ == num_records / 10) + { + printf (" Writer %d: %d out of %d written\n", writer_id, ix, num_records); + counter = 0; + } + record = (char *) (census_log_start_write (record_size)); + if (record == NULL) + { + return ix; + } + write_record (record, record_size); + census_log_end_write (record, record_size); + for (jx = 0; jx < spin_count; ++jx) + { + GPR_ASSERT (jx >= 0); + } } - record = (char*)(census_log_start_write(record_size)); - if (record == NULL) { - return ix; - } - write_record(record, record_size); - census_log_end_write(record, record_size); - for (jx = 0; jx < spin_count; ++jx) { - GPR_ASSERT(jx >= 0); - } - } return num_records; } /* Performs a single read iteration. Returns the number of records read. */ -static size_t perform_read_iteration(size_t record_size) { - const void* read_buffer = NULL; +static size_t +perform_read_iteration (size_t record_size) +{ + const void *read_buffer = NULL; size_t bytes_available; size_t records_read = 0; - census_log_init_reader(); - while ((read_buffer = census_log_read_next(&bytes_available))) { - gpr_int32 num_records = 0; - read_records(record_size, (const char*)read_buffer, bytes_available, - &num_records); - records_read += num_records; - } + census_log_init_reader (); + while ((read_buffer = census_log_read_next (&bytes_available))) + { + gpr_int32 num_records = 0; + read_records (record_size, (const char *) read_buffer, bytes_available, &num_records); + records_read += num_records; + } return records_read; } /* Asserts that the log is empty. */ -static void assert_log_empty(void) { +static void +assert_log_empty (void) +{ size_t bytes_available; - census_log_init_reader(); - GPR_ASSERT(census_log_read_next(&bytes_available) == NULL); + census_log_init_reader (); + GPR_ASSERT (census_log_read_next (&bytes_available) == NULL); } /* Given log size and record size, computes the minimum usable space. */ -static gpr_int32 min_usable_space(size_t log_size, size_t record_size) { +static gpr_int32 +min_usable_space (size_t log_size, size_t record_size) +{ gpr_int32 usable_space; - gpr_int32 num_blocks = - GPR_MAX(log_size / CENSUS_LOG_MAX_RECORD_SIZE, gpr_cpu_num_cores()); + gpr_int32 num_blocks = GPR_MAX (log_size / CENSUS_LOG_MAX_RECORD_SIZE, gpr_cpu_num_cores ()); gpr_int32 waste_per_block = CENSUS_LOG_MAX_RECORD_SIZE % record_size; /* In the worst case, all except one core-local block is full. */ gpr_int32 num_full_blocks = num_blocks - 1; - usable_space = (gpr_int32)log_size - - (num_full_blocks * CENSUS_LOG_MAX_RECORD_SIZE) - - ((num_blocks - num_full_blocks) * waste_per_block); - GPR_ASSERT(usable_space > 0); + usable_space = (gpr_int32) log_size - (num_full_blocks * CENSUS_LOG_MAX_RECORD_SIZE) - ((num_blocks - num_full_blocks) * waste_per_block); + GPR_ASSERT (usable_space > 0); return usable_space; } @@ -140,36 +151,44 @@ static gpr_int32 min_usable_space(size_t log_size, size_t record_size) { are sized such that CENSUS_LOG_2_MAX_RECORD_SIZE is a multiple of record size. If not a circular log, verifies that the number of records written match the number of records read. */ -static void fill_log(size_t log_size, int no_fragmentation, int circular_log) { +static void +fill_log (size_t log_size, int no_fragmentation, int circular_log) +{ int size; gpr_int32 records_written; gpr_int32 usable_space; gpr_int32 records_read; - if (no_fragmentation) { - int log2size = rand() % (CENSUS_LOG_2_MAX_RECORD_SIZE + 1); - size = (1 << log2size); - } else { - while (1) { - size = 1 + (rand() % CENSUS_LOG_MAX_RECORD_SIZE); - if (CENSUS_LOG_MAX_RECORD_SIZE % size) { - break; - } + if (no_fragmentation) + { + int log2size = rand () % (CENSUS_LOG_2_MAX_RECORD_SIZE + 1); + size = (1 << log2size); } - } - printf(" Fill record size: %d\n", size); - records_written = write_records_to_log( - 0 /* writer id */, size, (log_size / size) * 2, 0 /* spin count */); - usable_space = min_usable_space(log_size, size); - GPR_ASSERT(records_written * size >= usable_space); - records_read = perform_read_iteration(size); - if (!circular_log) { - GPR_ASSERT(records_written == records_read); - } - assert_log_empty(); + else + { + while (1) + { + size = 1 + (rand () % CENSUS_LOG_MAX_RECORD_SIZE); + if (CENSUS_LOG_MAX_RECORD_SIZE % size) + { + break; + } + } + } + printf (" Fill record size: %d\n", size); + records_written = write_records_to_log (0 /* writer id */ , size, (log_size / size) * 2, 0 /* spin count */ ); + usable_space = min_usable_space (log_size, size); + GPR_ASSERT (records_written * size >= usable_space); + records_read = perform_read_iteration (size); + if (!circular_log) + { + GPR_ASSERT (records_written == records_read); + } + assert_log_empty (); } /* Structure to pass args to writer_thread */ -typedef struct writer_thread_args { +typedef struct writer_thread_args +{ /* Index of this thread in the writers vector. */ int index; /* Record size. */ @@ -177,41 +196,43 @@ typedef struct writer_thread_args { /* Number of records to write. */ gpr_int32 num_records; /* Used to signal when writer is complete */ - gpr_cv* done; - gpr_mu* mu; - int* count; + gpr_cv *done; + gpr_mu *mu; + int *count; } writer_thread_args; /* Writes the given number of records of random size (up to kMaxRecordSize) and random data to the specified log. */ -static void writer_thread(void* arg) { - writer_thread_args* args = (writer_thread_args*)arg; +static void +writer_thread (void *arg) +{ + writer_thread_args *args = (writer_thread_args *) arg; /* Maximum number of times to spin between writes. */ static const gpr_int32 MAX_SPIN_COUNT = 50; int records_written = 0; - printf(" Writer: %d\n", args->index); - while (records_written < args->num_records) { - records_written += write_records_to_log(args->index, args->record_size, - args->num_records - records_written, - MAX_SPIN_COUNT); - if (records_written < args->num_records) { - /* Ran out of log space. Sleep for a bit and let the reader catch up. - This should never happen for circular logs. */ - printf(" Writer stalled due to out-of-space: %d out of %d written\n", - records_written, args->num_records); - gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(10)); + printf (" Writer: %d\n", args->index); + while (records_written < args->num_records) + { + records_written += write_records_to_log (args->index, args->record_size, args->num_records - records_written, MAX_SPIN_COUNT); + if (records_written < args->num_records) + { + /* Ran out of log space. Sleep for a bit and let the reader catch up. + This should never happen for circular logs. */ + printf (" Writer stalled due to out-of-space: %d out of %d written\n", records_written, args->num_records); + gpr_sleep_until (GRPC_TIMEOUT_MILLIS_TO_DEADLINE (10)); + } } - } /* Done. Decrement count and signal. */ - gpr_mu_lock(args->mu); + gpr_mu_lock (args->mu); (*args->count)--; - gpr_cv_broadcast(args->done); - printf(" Writer done: %d\n", args->index); - gpr_mu_unlock(args->mu); + gpr_cv_broadcast (args->done); + printf (" Writer done: %d\n", args->index); + gpr_mu_unlock (args->mu); } /* struct to pass args to reader_thread */ -typedef struct reader_thread_args { +typedef struct reader_thread_args +{ /* Record size. */ size_t record_size; /* Interval between read iterations. */ @@ -222,43 +243,45 @@ typedef struct reader_thread_args { gpr_cv stop; int stop_flag; /* Used to signal when reader has finished */ - gpr_cv* done; - gpr_mu* mu; + gpr_cv *done; + gpr_mu *mu; int running; } reader_thread_args; /* Reads and verifies the specified number of records. Reader can also be stopped via gpr_cv_signal(&args->stop). Sleeps for 'read_interval_in_msec' between read iterations. */ -static void reader_thread(void* arg) { +static void +reader_thread (void *arg) +{ gpr_int32 records_read = 0; - reader_thread_args* args = (reader_thread_args*)arg; + reader_thread_args *args = (reader_thread_args *) arg; gpr_int32 num_iterations = 0; gpr_timespec interval; int counter = 0; - printf(" Reader starting\n"); - interval = gpr_time_from_micros(args->read_iteration_interval_in_msec * 1000, - GPR_TIMESPAN); - gpr_mu_lock(args->mu); - while (!args->stop_flag && records_read < args->total_records) { - gpr_cv_wait(&args->stop, args->mu, interval); - if (!args->stop_flag) { - records_read += perform_read_iteration(args->record_size); - GPR_ASSERT(records_read <= args->total_records); - if (counter++ == 100000) { - printf(" Reader: %d out of %d read\n", records_read, - args->total_records); - counter = 0; - } - ++num_iterations; + printf (" Reader starting\n"); + interval = gpr_time_from_micros (args->read_iteration_interval_in_msec * 1000, GPR_TIMESPAN); + gpr_mu_lock (args->mu); + while (!args->stop_flag && records_read < args->total_records) + { + gpr_cv_wait (&args->stop, args->mu, interval); + if (!args->stop_flag) + { + records_read += perform_read_iteration (args->record_size); + GPR_ASSERT (records_read <= args->total_records); + if (counter++ == 100000) + { + printf (" Reader: %d out of %d read\n", records_read, args->total_records); + counter = 0; + } + ++num_iterations; + } } - } /* Done */ args->running = 0; - gpr_cv_broadcast(args->done); - printf(" Reader: records: %d, iterations: %d\n", records_read, - num_iterations); - gpr_mu_unlock(args->mu); + gpr_cv_broadcast (args->done); + printf (" Reader: records: %d, iterations: %d\n", records_read, num_iterations); + gpr_mu_unlock (args->mu); } /* Creates NUM_WRITERS writers where each writer writes NUM_RECORDS_PER_WRITER @@ -266,7 +289,9 @@ static void reader_thread(void* arg) { READ_ITERATION_INTERVAL_IN_MSEC. */ /* Number of writers. */ #define NUM_WRITERS 5 -static void multiple_writers_single_reader(int circular_log) { +static void +multiple_writers_single_reader (int circular_log) +{ /* Sleep interval between read iterations. */ static const gpr_int32 READ_ITERATION_INTERVAL_IN_MSEC = 10; /* Number of records written by each writer. */ @@ -277,315 +302,347 @@ static void multiple_writers_single_reader(int circular_log) { gpr_thd_id id; gpr_cv writers_done; int writers_count = NUM_WRITERS; - gpr_mu writers_mu; /* protects writers_done and writers_count */ + gpr_mu writers_mu; /* protects writers_done and writers_count */ writer_thread_args writers[NUM_WRITERS]; gpr_cv reader_done; - gpr_mu reader_mu; /* protects reader_done and reader.running */ + gpr_mu reader_mu; /* protects reader_done and reader.running */ reader_thread_args reader; - gpr_int32 record_size = 1 + rand() % MAX_RECORD_SIZE; - printf(" Record size: %d\n", record_size); + gpr_int32 record_size = 1 + rand () % MAX_RECORD_SIZE; + printf (" Record size: %d\n", record_size); /* Create and start writers. */ - gpr_cv_init(&writers_done); - gpr_mu_init(&writers_mu); - for (ix = 0; ix < NUM_WRITERS; ++ix) { - writers[ix].index = ix; - writers[ix].record_size = record_size; - writers[ix].num_records = NUM_RECORDS_PER_WRITER; - writers[ix].done = &writers_done; - writers[ix].count = &writers_count; - writers[ix].mu = &writers_mu; - gpr_thd_new(&id, &writer_thread, &writers[ix], NULL); - } + gpr_cv_init (&writers_done); + gpr_mu_init (&writers_mu); + for (ix = 0; ix < NUM_WRITERS; ++ix) + { + writers[ix].index = ix; + writers[ix].record_size = record_size; + writers[ix].num_records = NUM_RECORDS_PER_WRITER; + writers[ix].done = &writers_done; + writers[ix].count = &writers_count; + writers[ix].mu = &writers_mu; + gpr_thd_new (&id, &writer_thread, &writers[ix], NULL); + } /* Start reader. */ reader.record_size = record_size; reader.read_iteration_interval_in_msec = READ_ITERATION_INTERVAL_IN_MSEC; reader.total_records = NUM_WRITERS * NUM_RECORDS_PER_WRITER; reader.stop_flag = 0; - gpr_cv_init(&reader.stop); - gpr_cv_init(&reader_done); + gpr_cv_init (&reader.stop); + gpr_cv_init (&reader_done); reader.done = &reader_done; - gpr_mu_init(&reader_mu); + gpr_mu_init (&reader_mu); reader.mu = &reader_mu; reader.running = 1; - gpr_thd_new(&id, &reader_thread, &reader, NULL); + gpr_thd_new (&id, &reader_thread, &reader, NULL); /* Wait for writers to finish. */ - gpr_mu_lock(&writers_mu); - while (writers_count != 0) { - gpr_cv_wait(&writers_done, &writers_mu, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - gpr_mu_unlock(&writers_mu); - gpr_mu_destroy(&writers_mu); - gpr_cv_destroy(&writers_done); - gpr_mu_lock(&reader_mu); - if (circular_log) { - /* Stop reader. */ - reader.stop_flag = 1; - gpr_cv_signal(&reader.stop); - } + gpr_mu_lock (&writers_mu); + while (writers_count != 0) + { + gpr_cv_wait (&writers_done, &writers_mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + gpr_mu_unlock (&writers_mu); + gpr_mu_destroy (&writers_mu); + gpr_cv_destroy (&writers_done); + gpr_mu_lock (&reader_mu); + if (circular_log) + { + /* Stop reader. */ + reader.stop_flag = 1; + gpr_cv_signal (&reader.stop); + } /* wait for reader to finish */ - while (reader.running) { - gpr_cv_wait(&reader_done, &reader_mu, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - if (circular_log) { - /* Assert that there were no out-of-space errors. */ - GPR_ASSERT(0 == census_log_out_of_space_count()); - } - gpr_mu_unlock(&reader_mu); - gpr_mu_destroy(&reader_mu); - gpr_cv_destroy(&reader_done); - printf(" Reader: finished\n"); + while (reader.running) + { + gpr_cv_wait (&reader_done, &reader_mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + if (circular_log) + { + /* Assert that there were no out-of-space errors. */ + GPR_ASSERT (0 == census_log_out_of_space_count ()); + } + gpr_mu_unlock (&reader_mu); + gpr_mu_destroy (&reader_mu); + gpr_cv_destroy (&reader_done); + printf (" Reader: finished\n"); } /* Log sizes to use for all tests. */ #define LOG_SIZE_IN_MB 1 #define LOG_SIZE_IN_BYTES (LOG_SIZE_IN_MB << 20) -static void setup_test(int circular_log) { - census_log_initialize(LOG_SIZE_IN_MB, circular_log); - GPR_ASSERT(census_log_remaining_space() == LOG_SIZE_IN_BYTES); +static void +setup_test (int circular_log) +{ + census_log_initialize (LOG_SIZE_IN_MB, circular_log); + GPR_ASSERT (census_log_remaining_space () == LOG_SIZE_IN_BYTES); } /* Attempts to create a record of invalid size (size > CENSUS_LOG_MAX_RECORD_SIZE). */ -void test_invalid_record_size(void) { +void +test_invalid_record_size (void) +{ static const size_t INVALID_SIZE = CENSUS_LOG_MAX_RECORD_SIZE + 1; static const size_t VALID_SIZE = 1; - void* record; - printf("Starting test: invalid record size\n"); - setup_test(0); - record = census_log_start_write(INVALID_SIZE); - GPR_ASSERT(record == NULL); + void *record; + printf ("Starting test: invalid record size\n"); + setup_test (0); + record = census_log_start_write (INVALID_SIZE); + GPR_ASSERT (record == NULL); /* Now try writing a valid record. */ - record = census_log_start_write(VALID_SIZE); - GPR_ASSERT(record != NULL); - census_log_end_write(record, VALID_SIZE); + record = census_log_start_write (VALID_SIZE); + GPR_ASSERT (record != NULL); + census_log_end_write (record, VALID_SIZE); /* Verifies that available space went down by one block. In theory, this check can fail if the thread is context switched to a new CPU during the start_write execution (multiple blocks get allocated), but this has not been observed in practice. */ - GPR_ASSERT(LOG_SIZE_IN_BYTES - CENSUS_LOG_MAX_RECORD_SIZE == - census_log_remaining_space()); - census_log_shutdown(); + GPR_ASSERT (LOG_SIZE_IN_BYTES - CENSUS_LOG_MAX_RECORD_SIZE == census_log_remaining_space ()); + census_log_shutdown (); } /* Tests end_write() with a different size than what was specified in start_write(). */ -void test_end_write_with_different_size(void) { +void +test_end_write_with_different_size (void) +{ static const size_t START_WRITE_SIZE = 10; static const size_t END_WRITE_SIZE = 7; - void* record_written; - const void* record_read; + void *record_written; + const void *record_read; size_t bytes_available; - printf("Starting test: end write with different size\n"); - setup_test(0); - record_written = census_log_start_write(START_WRITE_SIZE); - GPR_ASSERT(record_written != NULL); - census_log_end_write(record_written, END_WRITE_SIZE); - census_log_init_reader(); - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(record_written == record_read); - GPR_ASSERT(END_WRITE_SIZE == bytes_available); - assert_log_empty(); - census_log_shutdown(); + printf ("Starting test: end write with different size\n"); + setup_test (0); + record_written = census_log_start_write (START_WRITE_SIZE); + GPR_ASSERT (record_written != NULL); + census_log_end_write (record_written, END_WRITE_SIZE); + census_log_init_reader (); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (record_written == record_read); + GPR_ASSERT (END_WRITE_SIZE == bytes_available); + assert_log_empty (); + census_log_shutdown (); } /* Verifies that pending records are not available via read_next(). */ -void test_read_pending_record(void) { +void +test_read_pending_record (void) +{ static const size_t PR_RECORD_SIZE = 1024; size_t bytes_available; - const void* record_read; - void* record_written; - printf("Starting test: read pending record\n"); - setup_test(0); + const void *record_read; + void *record_written; + printf ("Starting test: read pending record\n"); + setup_test (0); /* Start a write. */ - record_written = census_log_start_write(PR_RECORD_SIZE); - GPR_ASSERT(record_written != NULL); + record_written = census_log_start_write (PR_RECORD_SIZE); + GPR_ASSERT (record_written != NULL); /* As write is pending, read should fail. */ - census_log_init_reader(); - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(record_read == NULL); + census_log_init_reader (); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (record_read == NULL); /* A read followed by end_write() should succeed. */ - census_log_end_write(record_written, PR_RECORD_SIZE); - census_log_init_reader(); - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(record_written == record_read); - GPR_ASSERT(PR_RECORD_SIZE == bytes_available); - assert_log_empty(); - census_log_shutdown(); + census_log_end_write (record_written, PR_RECORD_SIZE); + census_log_init_reader (); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (record_written == record_read); + GPR_ASSERT (PR_RECORD_SIZE == bytes_available); + assert_log_empty (); + census_log_shutdown (); } /* Tries reading beyond pending write. */ -void test_read_beyond_pending_record(void) { +void +test_read_beyond_pending_record (void) +{ /* Start a write. */ gpr_uint32 incomplete_record_size = 10; gpr_uint32 complete_record_size = 20; size_t bytes_available; - void* complete_record; - const void* record_read; - void* incomplete_record; - printf("Starting test: read beyond pending record\n"); - setup_test(0); - incomplete_record = census_log_start_write(incomplete_record_size); - GPR_ASSERT(incomplete_record != NULL); - complete_record = census_log_start_write(complete_record_size); - GPR_ASSERT(complete_record != NULL); - GPR_ASSERT(complete_record != incomplete_record); - census_log_end_write(complete_record, complete_record_size); + void *complete_record; + const void *record_read; + void *incomplete_record; + printf ("Starting test: read beyond pending record\n"); + setup_test (0); + incomplete_record = census_log_start_write (incomplete_record_size); + GPR_ASSERT (incomplete_record != NULL); + complete_record = census_log_start_write (complete_record_size); + GPR_ASSERT (complete_record != NULL); + GPR_ASSERT (complete_record != incomplete_record); + census_log_end_write (complete_record, complete_record_size); /* Now iterate over blocks to read completed records. */ - census_log_init_reader(); - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(complete_record == record_read); - GPR_ASSERT(complete_record_size == bytes_available); + census_log_init_reader (); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (complete_record == record_read); + GPR_ASSERT (complete_record_size == bytes_available); /* Complete first record. */ - census_log_end_write(incomplete_record, incomplete_record_size); + census_log_end_write (incomplete_record, incomplete_record_size); /* Have read past the incomplete record, so read_next() should return NULL. */ /* NB: this test also assumes our thread did not get switched to a different CPU between the two start_write calls */ - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(record_read == NULL); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (record_read == NULL); /* Reset reader to get the newly completed record. */ - census_log_init_reader(); - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(incomplete_record == record_read); - GPR_ASSERT(incomplete_record_size == bytes_available); - assert_log_empty(); - census_log_shutdown(); + census_log_init_reader (); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (incomplete_record == record_read); + GPR_ASSERT (incomplete_record_size == bytes_available); + assert_log_empty (); + census_log_shutdown (); } /* Tests scenario where block being read is detached from a core and put on the dirty list. */ -void test_detached_while_reading(void) { +void +test_detached_while_reading (void) +{ static const size_t DWR_RECORD_SIZE = 10; size_t bytes_available; - const void* record_read; - void* record_written; + const void *record_read; + void *record_written; gpr_uint32 block_read = 0; - printf("Starting test: detached while reading\n"); - setup_test(0); + printf ("Starting test: detached while reading\n"); + setup_test (0); /* Start a write. */ - record_written = census_log_start_write(DWR_RECORD_SIZE); - GPR_ASSERT(record_written != NULL); - census_log_end_write(record_written, DWR_RECORD_SIZE); + record_written = census_log_start_write (DWR_RECORD_SIZE); + GPR_ASSERT (record_written != NULL); + census_log_end_write (record_written, DWR_RECORD_SIZE); /* Read this record. */ - census_log_init_reader(); - record_read = census_log_read_next(&bytes_available); - GPR_ASSERT(record_read != NULL); - GPR_ASSERT(DWR_RECORD_SIZE == bytes_available); + census_log_init_reader (); + record_read = census_log_read_next (&bytes_available); + GPR_ASSERT (record_read != NULL); + GPR_ASSERT (DWR_RECORD_SIZE == bytes_available); /* Now fill the log. This will move the block being read from core-local array to the dirty list. */ - while ((record_written = census_log_start_write(DWR_RECORD_SIZE))) { - census_log_end_write(record_written, DWR_RECORD_SIZE); - } + while ((record_written = census_log_start_write (DWR_RECORD_SIZE))) + { + census_log_end_write (record_written, DWR_RECORD_SIZE); + } /* In this iteration, read_next() should only traverse blocks in the core-local array. Therefore, we expect at most gpr_cpu_num_cores() more blocks. As log is full, if read_next() is traversing the dirty list, we will get more than gpr_cpu_num_cores() blocks. */ - while ((record_read = census_log_read_next(&bytes_available))) { - ++block_read; - GPR_ASSERT(block_read <= gpr_cpu_num_cores()); - } - census_log_shutdown(); + while ((record_read = census_log_read_next (&bytes_available))) + { + ++block_read; + GPR_ASSERT (block_read <= gpr_cpu_num_cores ()); + } + census_log_shutdown (); } /* Fills non-circular log with records sized such that size is a multiple of CENSUS_LOG_MAX_RECORD_SIZE (no per-block fragmentation). */ -void test_fill_log_no_fragmentation(void) { +void +test_fill_log_no_fragmentation (void) +{ const int circular = 0; - printf("Starting test: fill log no fragmentation\n"); - setup_test(circular); - fill_log(LOG_SIZE_IN_BYTES, 1 /* no fragmentation */, circular); - census_log_shutdown(); + printf ("Starting test: fill log no fragmentation\n"); + setup_test (circular); + fill_log (LOG_SIZE_IN_BYTES, 1 /* no fragmentation */ , circular); + census_log_shutdown (); } /* Fills circular log with records sized such that size is a multiple of CENSUS_LOG_MAX_RECORD_SIZE (no per-block fragmentation). */ -void test_fill_circular_log_no_fragmentation(void) { +void +test_fill_circular_log_no_fragmentation (void) +{ const int circular = 1; - printf("Starting test: fill circular log no fragmentation\n"); - setup_test(circular); - fill_log(LOG_SIZE_IN_BYTES, 1 /* no fragmentation */, circular); - census_log_shutdown(); + printf ("Starting test: fill circular log no fragmentation\n"); + setup_test (circular); + fill_log (LOG_SIZE_IN_BYTES, 1 /* no fragmentation */ , circular); + census_log_shutdown (); } /* Fills non-circular log with records that may straddle end of a block. */ -void test_fill_log_with_straddling_records(void) { +void +test_fill_log_with_straddling_records (void) +{ const int circular = 0; - printf("Starting test: fill log with straddling records\n"); - setup_test(circular); - fill_log(LOG_SIZE_IN_BYTES, 0 /* block straddling records */, circular); - census_log_shutdown(); + printf ("Starting test: fill log with straddling records\n"); + setup_test (circular); + fill_log (LOG_SIZE_IN_BYTES, 0 /* block straddling records */ , circular); + census_log_shutdown (); } /* Fills circular log with records that may straddle end of a block. */ -void test_fill_circular_log_with_straddling_records(void) { +void +test_fill_circular_log_with_straddling_records (void) +{ const int circular = 1; - printf("Starting test: fill circular log with straddling records\n"); - setup_test(circular); - fill_log(LOG_SIZE_IN_BYTES, 0 /* block straddling records */, circular); - census_log_shutdown(); + printf ("Starting test: fill circular log with straddling records\n"); + setup_test (circular); + fill_log (LOG_SIZE_IN_BYTES, 0 /* block straddling records */ , circular); + census_log_shutdown (); } /* Tests scenario where multiple writers and a single reader are using a log that is configured to discard old records. */ -void test_multiple_writers_circular_log(void) { +void +test_multiple_writers_circular_log (void) +{ const int circular = 1; - printf("Starting test: multiple writers circular log\n"); - setup_test(circular); - multiple_writers_single_reader(circular); - census_log_shutdown(); + printf ("Starting test: multiple writers circular log\n"); + setup_test (circular); + multiple_writers_single_reader (circular); + census_log_shutdown (); } /* Tests scenario where multiple writers and a single reader are using a log that is configured to discard old records. */ -void test_multiple_writers(void) { +void +test_multiple_writers (void) +{ const int circular = 0; - printf("Starting test: multiple writers\n"); - setup_test(circular); - multiple_writers_single_reader(circular); - census_log_shutdown(); + printf ("Starting test: multiple writers\n"); + setup_test (circular); + multiple_writers_single_reader (circular); + census_log_shutdown (); } /* Repeat the straddling records and multiple writers tests with a small log. */ -void test_small_log(void) { +void +test_small_log (void) +{ size_t log_size; const int circular = 0; - printf("Starting test: small log\n"); - census_log_initialize(0, circular); - log_size = census_log_remaining_space(); - GPR_ASSERT(log_size > 0); - fill_log(log_size, 0, circular); - census_log_shutdown(); - census_log_initialize(0, circular); - multiple_writers_single_reader(circular); - census_log_shutdown(); + printf ("Starting test: small log\n"); + census_log_initialize (0, circular); + log_size = census_log_remaining_space (); + GPR_ASSERT (log_size > 0); + fill_log (log_size, 0, circular); + census_log_shutdown (); + census_log_initialize (0, circular); + multiple_writers_single_reader (circular); + census_log_shutdown (); } -void test_performance(void) { +void +test_performance (void) +{ int write_size = 1; - for (; write_size < CENSUS_LOG_MAX_RECORD_SIZE; write_size *= 2) { - gpr_timespec write_time; - gpr_timespec start_time; - double write_time_micro = 0.0; - int nrecords = 0; - setup_test(0); - start_time = gpr_now(GPR_CLOCK_REALTIME); - while (1) { - void* record = census_log_start_write(write_size); - if (record == NULL) { - break; - } - census_log_end_write(record, write_size); - nrecords++; + for (; write_size < CENSUS_LOG_MAX_RECORD_SIZE; write_size *= 2) + { + gpr_timespec write_time; + gpr_timespec start_time; + double write_time_micro = 0.0; + int nrecords = 0; + setup_test (0); + start_time = gpr_now (GPR_CLOCK_REALTIME); + while (1) + { + void *record = census_log_start_write (write_size); + if (record == NULL) + { + break; + } + census_log_end_write (record, write_size); + nrecords++; + } + write_time = gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), start_time); + write_time_micro = write_time.tv_sec * 1000000 + write_time.tv_nsec / 1000; + census_log_shutdown (); + printf ("Wrote %d %d byte records in %.3g microseconds: %g records/us " "(%g ns/record), %g gigabytes/s\n", nrecords, write_size, write_time_micro, nrecords / write_time_micro, 1000 * write_time_micro / nrecords, (write_size * nrecords) / write_time_micro / 1000); } - write_time = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start_time); - write_time_micro = write_time.tv_sec * 1000000 + write_time.tv_nsec / 1000; - census_log_shutdown(); - printf( - "Wrote %d %d byte records in %.3g microseconds: %g records/us " - "(%g ns/record), %g gigabytes/s\n", - nrecords, write_size, write_time_micro, nrecords / write_time_micro, - 1000 * write_time_micro / nrecords, - (write_size * nrecords) / write_time_micro / 1000); - } } diff --git a/test/core/statistics/census_log_tests.h b/test/core/statistics/census_log_tests.h index ec3241b4f9..684b014ceb 100644 --- a/test/core/statistics/census_log_tests.h +++ b/test/core/statistics/census_log_tests.h @@ -34,18 +34,18 @@ #ifndef GRPC_TEST_CORE_STATISTICS_CENSUS_LOG_TESTS_H #define GRPC_TEST_CORE_STATISTICS_CENSUS_LOG_TESTS_H -void test_invalid_record_size(); -void test_end_write_with_different_size(); -void test_read_pending_record(); -void test_read_beyond_pending_record(); -void test_detached_while_reading(); -void test_fill_log_no_fragmentation(); -void test_fill_circular_log_no_fragmentation(); -void test_fill_log_with_straddling_records(); -void test_fill_circular_log_with_straddling_records(); -void test_multiple_writers_circular_log(); -void test_multiple_writers(); -void test_performance(); -void test_small_log(); +void test_invalid_record_size (); +void test_end_write_with_different_size (); +void test_read_pending_record (); +void test_read_beyond_pending_record (); +void test_detached_while_reading (); +void test_fill_log_no_fragmentation (); +void test_fill_circular_log_no_fragmentation (); +void test_fill_log_with_straddling_records (); +void test_fill_circular_log_with_straddling_records (); +void test_multiple_writers_circular_log (); +void test_multiple_writers (); +void test_performance (); +void test_small_log (); #endif /* GRPC_TEST_CORE_STATISTICS_CENSUS_LOG_TESTS_H */ diff --git a/test/core/statistics/census_stub_test.c b/test/core/statistics/census_stub_test.c index 26a45ae58a..ed4da9463b 100644 --- a/test/core/statistics/census_stub_test.c +++ b/test/core/statistics/census_stub_test.c @@ -41,37 +41,41 @@ #include "test/core/util/test_config.h" /* Tests census noop stubs in a simulated rpc flow */ -void test_census_stubs(void) { +void +test_census_stubs (void) +{ census_op_id op_id; - census_rpc_stats* stats = census_rpc_stats_create_empty(); - census_aggregated_rpc_stats data_map = {0, NULL}; + census_rpc_stats *stats = census_rpc_stats_create_empty (); + census_aggregated_rpc_stats data_map = { 0, NULL }; /* Initializes census library at server start up time. */ - census_init(); + census_init (); /* Starts tracing at the beginning of a rpc. */ - op_id = census_tracing_start_op(); + op_id = census_tracing_start_op (); /* Appends custom annotations on a trace object. */ - census_tracing_print(op_id, "annotation foo"); - census_tracing_print(op_id, "annotation bar"); + census_tracing_print (op_id, "annotation foo"); + census_tracing_print (op_id, "annotation bar"); /* Appends method tag on the trace object. */ - census_add_method_tag(op_id, "service_foo/method.bar"); + census_add_method_tag (op_id, "service_foo/method.bar"); /* Either record client side stats or server side stats associated with the op_id. Here for testing purpose, we record both. */ - census_record_rpc_client_stats(op_id, stats); - census_record_rpc_server_stats(op_id, stats); + census_record_rpc_client_stats (op_id, stats); + census_record_rpc_server_stats (op_id, stats); /* Ends a tracing. */ - census_tracing_end_op(op_id); + census_tracing_end_op (op_id); /* In process stats queries. */ - census_get_server_stats(&data_map); - census_aggregated_rpc_stats_set_empty(&data_map); - census_get_client_stats(&data_map); - census_aggregated_rpc_stats_set_empty(&data_map); - gpr_free(stats); - census_shutdown(); + census_get_server_stats (&data_map); + census_aggregated_rpc_stats_set_empty (&data_map); + census_get_client_stats (&data_map); + census_aggregated_rpc_stats_set_empty (&data_map); + gpr_free (stats); + census_shutdown (); } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - test_census_stubs(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_census_stubs (); return 0; } diff --git a/test/core/statistics/hash_table_test.c b/test/core/statistics/hash_table_test.c index 2568e96cba..bcf3fb7b87 100644 --- a/test/core/statistics/hash_table_test.c +++ b/test/core/statistics/hash_table_test.c @@ -44,258 +44,296 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -static gpr_uint64 hash64(const void* k) { - size_t len = strlen(k); - gpr_uint64 higher = gpr_murmur_hash3((const char*)k, len / 2, 0); - return higher << 32 | - gpr_murmur_hash3((const char*)(k) + len / 2, len - len / 2, 0); +static gpr_uint64 +hash64 (const void *k) +{ + size_t len = strlen (k); + gpr_uint64 higher = gpr_murmur_hash3 ((const char *) k, len / 2, 0); + return higher << 32 | gpr_murmur_hash3 ((const char *) (k) + len / 2, len - len / 2, 0); } -static int cmp_str_keys(const void* k1, const void* k2) { - return strcmp((const char*)k1, (const char*)k2); +static int +cmp_str_keys (const void *k1, const void *k2) +{ + return strcmp ((const char *) k1, (const char *) k2); } -static gpr_uint64 force_collision(const void* k) { - return (1997 + hash64(k) % 3); +static gpr_uint64 +force_collision (const void *k) +{ + return (1997 + hash64 (k) % 3); } -static void free_data(void* data) { gpr_free(data); } +static void +free_data (void *data) +{ + gpr_free (data); +} /* Basic tests that empty hash table can be created and destroyed. */ -static void test_create_table(void) { +static void +test_create_table (void) +{ /* Create table with uint64 key type */ - census_ht* ht = NULL; + census_ht *ht = NULL; census_ht_option ht_options = { - CENSUS_HT_UINT64, 1999, NULL, NULL, NULL, NULL}; - ht = census_ht_create(&ht_options); - GPR_ASSERT(ht != NULL); - GPR_ASSERT(census_ht_get_size(ht) == 0); - census_ht_destroy(ht); + CENSUS_HT_UINT64, 1999, NULL, NULL, NULL, NULL + }; + ht = census_ht_create (&ht_options); + GPR_ASSERT (ht != NULL); + GPR_ASSERT (census_ht_get_size (ht) == 0); + census_ht_destroy (ht); /* Create table with pointer key type */ ht = NULL; ht_options.key_type = CENSUS_HT_POINTER; ht_options.hash = &hash64; ht_options.compare_keys = &cmp_str_keys; - ht = census_ht_create(&ht_options); - GPR_ASSERT(ht != NULL); - GPR_ASSERT(census_ht_get_size(ht) == 0); - census_ht_destroy(ht); + ht = census_ht_create (&ht_options); + GPR_ASSERT (ht != NULL); + GPR_ASSERT (census_ht_get_size (ht) == 0); + census_ht_destroy (ht); } -static void test_table_with_int_key(void) { - census_ht_option opt = {CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL}; - census_ht* ht = census_ht_create(&opt); +static void +test_table_with_int_key (void) +{ + census_ht_option opt = { CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL }; + census_ht *ht = census_ht_create (&opt); gpr_uint64 i = 0; gpr_uint64 sum_of_keys = 0; size_t num_elements; - census_ht_kv* elements = NULL; - GPR_ASSERT(ht != NULL); - GPR_ASSERT(census_ht_get_size(ht) == 0); - elements = census_ht_get_all_elements(ht, &num_elements); - GPR_ASSERT(num_elements == 0); - GPR_ASSERT(elements == NULL); - for (i = 0; i < 20; ++i) { - census_ht_key key; - key.val = i; - census_ht_insert(ht, key, (void*)(gpr_intptr)i); - GPR_ASSERT(census_ht_get_size(ht) == i + 1); - } - for (i = 0; i < 20; i++) { - gpr_uint64* val = NULL; - census_ht_key key; - key.val = i; - val = census_ht_find(ht, key); - GPR_ASSERT(val == (void*)(gpr_intptr)i); - } - elements = census_ht_get_all_elements(ht, &num_elements); - GPR_ASSERT(elements != NULL); - GPR_ASSERT(num_elements == 20); - for (i = 0; i < num_elements; i++) { - sum_of_keys += elements[i].k.val; - } - GPR_ASSERT(sum_of_keys == 190); - gpr_free(elements); - census_ht_destroy(ht); + census_ht_kv *elements = NULL; + GPR_ASSERT (ht != NULL); + GPR_ASSERT (census_ht_get_size (ht) == 0); + elements = census_ht_get_all_elements (ht, &num_elements); + GPR_ASSERT (num_elements == 0); + GPR_ASSERT (elements == NULL); + for (i = 0; i < 20; ++i) + { + census_ht_key key; + key.val = i; + census_ht_insert (ht, key, (void *) (gpr_intptr) i); + GPR_ASSERT (census_ht_get_size (ht) == i + 1); + } + for (i = 0; i < 20; i++) + { + gpr_uint64 *val = NULL; + census_ht_key key; + key.val = i; + val = census_ht_find (ht, key); + GPR_ASSERT (val == (void *) (gpr_intptr) i); + } + elements = census_ht_get_all_elements (ht, &num_elements); + GPR_ASSERT (elements != NULL); + GPR_ASSERT (num_elements == 20); + for (i = 0; i < num_elements; i++) + { + sum_of_keys += elements[i].k.val; + } + GPR_ASSERT (sum_of_keys == 190); + gpr_free (elements); + census_ht_destroy (ht); } /* Test that there is no memory leak when keys and values are owned by table. */ -static void test_value_and_key_deleter(void) { - census_ht_option opt = {CENSUS_HT_POINTER, 7, &hash64, - &cmp_str_keys, &free_data, &free_data}; - census_ht* ht = census_ht_create(&opt); +static void +test_value_and_key_deleter (void) +{ + census_ht_option opt = { CENSUS_HT_POINTER, 7, &hash64, + &cmp_str_keys, &free_data, &free_data + }; + census_ht *ht = census_ht_create (&opt); census_ht_key key; - char* val = NULL; - char* val2 = NULL; - key.ptr = gpr_malloc(100); - val = gpr_malloc(10); - strcpy(val, "value"); - strcpy(key.ptr, "some string as a key"); - GPR_ASSERT(ht != NULL); - GPR_ASSERT(census_ht_get_size(ht) == 0); - census_ht_insert(ht, key, val); - GPR_ASSERT(census_ht_get_size(ht) == 1); - val = census_ht_find(ht, key); - GPR_ASSERT(val != NULL); - GPR_ASSERT(strcmp(val, "value") == 0); + char *val = NULL; + char *val2 = NULL; + key.ptr = gpr_malloc (100); + val = gpr_malloc (10); + strcpy (val, "value"); + strcpy (key.ptr, "some string as a key"); + GPR_ASSERT (ht != NULL); + GPR_ASSERT (census_ht_get_size (ht) == 0); + census_ht_insert (ht, key, val); + GPR_ASSERT (census_ht_get_size (ht) == 1); + val = census_ht_find (ht, key); + GPR_ASSERT (val != NULL); + GPR_ASSERT (strcmp (val, "value") == 0); /* Insert same key different value, old value is overwritten. */ - val2 = gpr_malloc(10); - strcpy(val2, "v2"); - census_ht_insert(ht, key, val2); - GPR_ASSERT(census_ht_get_size(ht) == 1); - val2 = census_ht_find(ht, key); - GPR_ASSERT(val2 != NULL); - GPR_ASSERT(strcmp(val2, "v2") == 0); - census_ht_destroy(ht); + val2 = gpr_malloc (10); + strcpy (val2, "v2"); + census_ht_insert (ht, key, val2); + GPR_ASSERT (census_ht_get_size (ht) == 1); + val2 = census_ht_find (ht, key); + GPR_ASSERT (val2 != NULL); + GPR_ASSERT (strcmp (val2, "v2") == 0); + census_ht_destroy (ht); } /* Test simple insert and erase operations. */ -static void test_simple_add_and_erase(void) { - census_ht_option opt = {CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL}; - census_ht* ht = census_ht_create(&opt); - GPR_ASSERT(ht != NULL); - GPR_ASSERT(census_ht_get_size(ht) == 0); +static void +test_simple_add_and_erase (void) +{ + census_ht_option opt = { CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL }; + census_ht *ht = census_ht_create (&opt); + GPR_ASSERT (ht != NULL); + GPR_ASSERT (census_ht_get_size (ht) == 0); { census_ht_key key; int val = 3; key.val = 2; - census_ht_insert(ht, key, (void*)&val); - GPR_ASSERT(census_ht_get_size(ht) == 1); - census_ht_erase(ht, key); - GPR_ASSERT(census_ht_get_size(ht) == 0); + census_ht_insert (ht, key, (void *) &val); + GPR_ASSERT (census_ht_get_size (ht) == 1); + census_ht_erase (ht, key); + GPR_ASSERT (census_ht_get_size (ht) == 0); /* Erasing a key from an empty table should be noop. */ - census_ht_erase(ht, key); - GPR_ASSERT(census_ht_get_size(ht) == 0); + census_ht_erase (ht, key); + GPR_ASSERT (census_ht_get_size (ht) == 0); /* Erasing a non-existant key from a table should be noop. */ - census_ht_insert(ht, key, (void*)&val); + census_ht_insert (ht, key, (void *) &val); key.val = 3; - census_ht_insert(ht, key, (void*)&val); + census_ht_insert (ht, key, (void *) &val); key.val = 9; - census_ht_insert(ht, key, (void*)&val); - GPR_ASSERT(census_ht_get_size(ht) == 3); + census_ht_insert (ht, key, (void *) &val); + GPR_ASSERT (census_ht_get_size (ht) == 3); key.val = 1; - census_ht_erase(ht, key); + census_ht_erase (ht, key); /* size unchanged after deleting non-existant key. */ - GPR_ASSERT(census_ht_get_size(ht) == 3); + GPR_ASSERT (census_ht_get_size (ht) == 3); /* size decrease by 1 after deleting an existant key. */ key.val = 2; - census_ht_erase(ht, key); - GPR_ASSERT(census_ht_get_size(ht) == 2); + census_ht_erase (ht, key); + GPR_ASSERT (census_ht_get_size (ht) == 2); } - census_ht_destroy(ht); + census_ht_destroy (ht); } -static void test_insertion_and_deletion_with_high_collision_rate(void) { - census_ht_option opt = {CENSUS_HT_POINTER, 13, &force_collision, - &cmp_str_keys, NULL, NULL}; - census_ht* ht = census_ht_create(&opt); +static void +test_insertion_and_deletion_with_high_collision_rate (void) +{ + census_ht_option opt = { CENSUS_HT_POINTER, 13, &force_collision, + &cmp_str_keys, NULL, NULL + }; + census_ht *ht = census_ht_create (&opt); char key_str[1000][GPR_LTOA_MIN_BUFSIZE]; gpr_uint64 val = 0; unsigned i = 0; - for (i = 0; i < 1000; i++) { - census_ht_key key; - key.ptr = key_str[i]; - gpr_ltoa(i, key_str[i]); - census_ht_insert(ht, key, (void*)(&val)); - gpr_log(GPR_INFO, "%d\n", i); - GPR_ASSERT(census_ht_get_size(ht) == (i + 1)); - } - for (i = 0; i < 1000; i++) { - census_ht_key key; - key.ptr = key_str[i]; - census_ht_erase(ht, key); - GPR_ASSERT(census_ht_get_size(ht) == (999 - i)); - } - census_ht_destroy(ht); + for (i = 0; i < 1000; i++) + { + census_ht_key key; + key.ptr = key_str[i]; + gpr_ltoa (i, key_str[i]); + census_ht_insert (ht, key, (void *) (&val)); + gpr_log (GPR_INFO, "%d\n", i); + GPR_ASSERT (census_ht_get_size (ht) == (i + 1)); + } + for (i = 0; i < 1000; i++) + { + census_ht_key key; + key.ptr = key_str[i]; + census_ht_erase (ht, key); + GPR_ASSERT (census_ht_get_size (ht) == (999 - i)); + } + census_ht_destroy (ht); } -static void test_table_with_string_key(void) { - census_ht_option opt = {CENSUS_HT_POINTER, 7, &hash64, - &cmp_str_keys, NULL, NULL}; - census_ht* ht = census_ht_create(&opt); - const char* keys[] = { - "k1", "a", "000", "apple", "banana_a_long_long_long_banana", - "%$", "111", "foo", "b"}; - const int vals[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; +static void +test_table_with_string_key (void) +{ + census_ht_option opt = { CENSUS_HT_POINTER, 7, &hash64, + &cmp_str_keys, NULL, NULL + }; + census_ht *ht = census_ht_create (&opt); + const char *keys[] = { + "k1", "a", "000", "apple", "banana_a_long_long_long_banana", + "%$", "111", "foo", "b" + }; + const int vals[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; int i = 0; - GPR_ASSERT(ht != NULL); - GPR_ASSERT(census_ht_get_size(ht) == 0); - for (i = 0; i < 9; i++) { - census_ht_key key; - key.ptr = (void*)(keys[i]); - census_ht_insert(ht, key, (void*)(vals + i)); - } - GPR_ASSERT(census_ht_get_size(ht) == 9); - for (i = 0; i < 9; i++) { - census_ht_key key; - int* val_ptr; - key.ptr = (void*)(keys[i]); - val_ptr = census_ht_find(ht, key); - GPR_ASSERT(*val_ptr == vals[i]); - } + GPR_ASSERT (ht != NULL); + GPR_ASSERT (census_ht_get_size (ht) == 0); + for (i = 0; i < 9; i++) + { + census_ht_key key; + key.ptr = (void *) (keys[i]); + census_ht_insert (ht, key, (void *) (vals + i)); + } + GPR_ASSERT (census_ht_get_size (ht) == 9); + for (i = 0; i < 9; i++) + { + census_ht_key key; + int *val_ptr; + key.ptr = (void *) (keys[i]); + val_ptr = census_ht_find (ht, key); + GPR_ASSERT (*val_ptr == vals[i]); + } { /* inserts duplicate keys */ census_ht_key key; - int* val_ptr = NULL; - key.ptr = (void*)(keys[2]); - census_ht_insert(ht, key, (void*)(vals + 8)); + int *val_ptr = NULL; + key.ptr = (void *) (keys[2]); + census_ht_insert (ht, key, (void *) (vals + 8)); /* expect value to be over written by new insertion */ - GPR_ASSERT(census_ht_get_size(ht) == 9); - val_ptr = census_ht_find(ht, key); - GPR_ASSERT(*val_ptr == vals[8]); - } - for (i = 0; i < 9; i++) { - census_ht_key key; - int* val_ptr; - gpr_uint32 expected_tbl_sz = 9 - i; - GPR_ASSERT(census_ht_get_size(ht) == expected_tbl_sz); - key.ptr = (void*)(keys[i]); - val_ptr = census_ht_find(ht, key); - GPR_ASSERT(val_ptr != NULL); - census_ht_erase(ht, key); - GPR_ASSERT(census_ht_get_size(ht) == expected_tbl_sz - 1); - val_ptr = census_ht_find(ht, key); - GPR_ASSERT(val_ptr == NULL); + GPR_ASSERT (census_ht_get_size (ht) == 9); + val_ptr = census_ht_find (ht, key); + GPR_ASSERT (*val_ptr == vals[8]); } - census_ht_destroy(ht); + for (i = 0; i < 9; i++) + { + census_ht_key key; + int *val_ptr; + gpr_uint32 expected_tbl_sz = 9 - i; + GPR_ASSERT (census_ht_get_size (ht) == expected_tbl_sz); + key.ptr = (void *) (keys[i]); + val_ptr = census_ht_find (ht, key); + GPR_ASSERT (val_ptr != NULL); + census_ht_erase (ht, key); + GPR_ASSERT (census_ht_get_size (ht) == expected_tbl_sz - 1); + val_ptr = census_ht_find (ht, key); + GPR_ASSERT (val_ptr == NULL); + } + census_ht_destroy (ht); } -static void test_insertion_with_same_key(void) { - census_ht_option opt = {CENSUS_HT_UINT64, 11, NULL, NULL, NULL, NULL}; - census_ht* ht = census_ht_create(&opt); +static void +test_insertion_with_same_key (void) +{ + census_ht_option opt = { CENSUS_HT_UINT64, 11, NULL, NULL, NULL, NULL }; + census_ht *ht = census_ht_create (&opt); census_ht_key key; - const char vals[] = {'a', 'b', 'c'}; - char* val_ptr; + const char vals[] = { 'a', 'b', 'c' }; + char *val_ptr; key.val = 3; - census_ht_insert(ht, key, (void*)&(vals[0])); - GPR_ASSERT(census_ht_get_size(ht) == 1); - val_ptr = (char*)census_ht_find(ht, key); - GPR_ASSERT(val_ptr != NULL); - GPR_ASSERT(*val_ptr == 'a'); + census_ht_insert (ht, key, (void *) &(vals[0])); + GPR_ASSERT (census_ht_get_size (ht) == 1); + val_ptr = (char *) census_ht_find (ht, key); + GPR_ASSERT (val_ptr != NULL); + GPR_ASSERT (*val_ptr == 'a'); key.val = 4; - val_ptr = (char*)census_ht_find(ht, key); - GPR_ASSERT(val_ptr == NULL); + val_ptr = (char *) census_ht_find (ht, key); + GPR_ASSERT (val_ptr == NULL); key.val = 3; - census_ht_insert(ht, key, (void*)&(vals[1])); - GPR_ASSERT(census_ht_get_size(ht) == 1); - val_ptr = (char*)census_ht_find(ht, key); - GPR_ASSERT(val_ptr != NULL); - GPR_ASSERT(*val_ptr == 'b'); - census_ht_insert(ht, key, (void*)&(vals[2])); - GPR_ASSERT(census_ht_get_size(ht) == 1); - val_ptr = (char*)census_ht_find(ht, key); - GPR_ASSERT(val_ptr != NULL); - GPR_ASSERT(*val_ptr == 'c'); - census_ht_destroy(ht); + census_ht_insert (ht, key, (void *) &(vals[1])); + GPR_ASSERT (census_ht_get_size (ht) == 1); + val_ptr = (char *) census_ht_find (ht, key); + GPR_ASSERT (val_ptr != NULL); + GPR_ASSERT (*val_ptr == 'b'); + census_ht_insert (ht, key, (void *) &(vals[2])); + GPR_ASSERT (census_ht_get_size (ht) == 1); + val_ptr = (char *) census_ht_find (ht, key); + GPR_ASSERT (val_ptr != NULL); + GPR_ASSERT (*val_ptr == 'c'); + census_ht_destroy (ht); } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - test_create_table(); - test_simple_add_and_erase(); - test_table_with_int_key(); - test_table_with_string_key(); - test_value_and_key_deleter(); - test_insertion_with_same_key(); - test_insertion_and_deletion_with_high_collision_rate(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_create_table (); + test_simple_add_and_erase (); + test_table_with_int_key (); + test_table_with_string_key (); + test_value_and_key_deleter (); + test_insertion_with_same_key (); + test_insertion_and_deletion_with_high_collision_rate (); return 0; } diff --git a/test/core/statistics/multiple_writers_circular_buffer_test.c b/test/core/statistics/multiple_writers_circular_buffer_test.c index 56ada893ef..c42c8633da 100644 --- a/test/core/statistics/multiple_writers_circular_buffer_test.c +++ b/test/core/statistics/multiple_writers_circular_buffer_test.c @@ -38,9 +38,11 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - test_multiple_writers_circular_log(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + test_multiple_writers_circular_log (); return 0; } diff --git a/test/core/statistics/multiple_writers_test.c b/test/core/statistics/multiple_writers_test.c index e524927da6..aed57a972b 100644 --- a/test/core/statistics/multiple_writers_test.c +++ b/test/core/statistics/multiple_writers_test.c @@ -38,9 +38,11 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - test_multiple_writers(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + test_multiple_writers (); return 0; } diff --git a/test/core/statistics/performance_test.c b/test/core/statistics/performance_test.c index 3f0e080093..44f4a30cfb 100644 --- a/test/core/statistics/performance_test.c +++ b/test/core/statistics/performance_test.c @@ -38,9 +38,11 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - test_performance(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + test_performance (); return 0; } diff --git a/test/core/statistics/quick_test.c b/test/core/statistics/quick_test.c index c72ae77b98..91b484fd31 100644 --- a/test/core/statistics/quick_test.c +++ b/test/core/statistics/quick_test.c @@ -38,17 +38,19 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - test_invalid_record_size(); - test_end_write_with_different_size(); - test_read_pending_record(); - test_read_beyond_pending_record(); - test_detached_while_reading(); - test_fill_log_no_fragmentation(); - test_fill_circular_log_no_fragmentation(); - test_fill_log_with_straddling_records(); - test_fill_circular_log_with_straddling_records(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + test_invalid_record_size (); + test_end_write_with_different_size (); + test_read_pending_record (); + test_read_beyond_pending_record (); + test_detached_while_reading (); + test_fill_log_no_fragmentation (); + test_fill_circular_log_no_fragmentation (); + test_fill_log_with_straddling_records (); + test_fill_circular_log_with_straddling_records (); return 0; } diff --git a/test/core/statistics/rpc_stats_test.c b/test/core/statistics/rpc_stats_test.c index df076b9c1e..134c478d81 100644 --- a/test/core/statistics/rpc_stats_test.c +++ b/test/core/statistics/rpc_stats_test.c @@ -45,153 +45,153 @@ #include "test/core/util/test_config.h" /* Ensure all possible state transitions are called without causing problem */ -static void test_init_shutdown(void) { - census_stats_store_init(); - census_stats_store_init(); - census_stats_store_shutdown(); - census_stats_store_shutdown(); - census_stats_store_init(); +static void +test_init_shutdown (void) +{ + census_stats_store_init (); + census_stats_store_init (); + census_stats_store_shutdown (); + census_stats_store_shutdown (); + census_stats_store_init (); } -static void test_create_and_destroy(void) { - census_rpc_stats* stats = NULL; - census_aggregated_rpc_stats agg_stats = {0, NULL}; +static void +test_create_and_destroy (void) +{ + census_rpc_stats *stats = NULL; + census_aggregated_rpc_stats agg_stats = { 0, NULL }; - stats = census_rpc_stats_create_empty(); - GPR_ASSERT(stats != NULL); - GPR_ASSERT(stats->cnt == 0 && stats->rpc_error_cnt == 0 && - stats->app_error_cnt == 0 && stats->elapsed_time_ms == 0.0 && - stats->api_request_bytes == 0 && stats->wire_request_bytes == 0 && - stats->api_response_bytes == 0 && stats->wire_response_bytes == 0); - gpr_free(stats); + stats = census_rpc_stats_create_empty (); + GPR_ASSERT (stats != NULL); + GPR_ASSERT (stats->cnt == 0 && stats->rpc_error_cnt == 0 && stats->app_error_cnt == 0 && stats->elapsed_time_ms == 0.0 && stats->api_request_bytes == 0 && stats->wire_request_bytes == 0 && stats->api_response_bytes == 0 && stats->wire_response_bytes == 0); + gpr_free (stats); - census_aggregated_rpc_stats_set_empty(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 0); - GPR_ASSERT(agg_stats.stats == NULL); + census_aggregated_rpc_stats_set_empty (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 0); + GPR_ASSERT (agg_stats.stats == NULL); agg_stats.num_entries = 1; - agg_stats.stats = (census_per_method_rpc_stats*)gpr_malloc( - sizeof(census_per_method_rpc_stats)); - agg_stats.stats[0].method = gpr_strdup("foo"); - census_aggregated_rpc_stats_set_empty(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 0); - GPR_ASSERT(agg_stats.stats == NULL); + agg_stats.stats = (census_per_method_rpc_stats *) gpr_malloc (sizeof (census_per_method_rpc_stats)); + agg_stats.stats[0].method = gpr_strdup ("foo"); + census_aggregated_rpc_stats_set_empty (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 0); + GPR_ASSERT (agg_stats.stats == NULL); } #define ASSERT_NEAR(a, b) \ GPR_ASSERT((a - b) * (a - b) < 1e-24 * (a + b) * (a + b)) -static void test_record_and_get_stats(void) { - census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; +static void +test_record_and_get_stats (void) +{ + census_rpc_stats stats = { 1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4 }; census_op_id id; - census_aggregated_rpc_stats agg_stats = {0, NULL}; + census_aggregated_rpc_stats agg_stats = { 0, NULL }; /* Record client stats twice with the same op_id. */ - census_init(); - id = census_tracing_start_op(); - census_add_method_tag(id, "m1"); - census_record_rpc_client_stats(id, &stats); - census_record_rpc_client_stats(id, &stats); - census_tracing_end_op(id); + census_init (); + id = census_tracing_start_op (); + census_add_method_tag (id, "m1"); + census_record_rpc_client_stats (id, &stats); + census_record_rpc_client_stats (id, &stats); + census_tracing_end_op (id); /* Server stats expect to be empty */ - census_get_server_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 0); - GPR_ASSERT(agg_stats.stats == NULL); + census_get_server_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 0); + GPR_ASSERT (agg_stats.stats == NULL); /* Client stats expect to have one entry */ - census_get_client_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 1); - GPR_ASSERT(agg_stats.stats != NULL); - GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m1") == 0); - GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 2 && - agg_stats.stats[0].hour_stats.cnt == 2 && - agg_stats.stats[0].total_stats.cnt == 2); - ASSERT_NEAR(agg_stats.stats[0].minute_stats.wire_response_bytes, 16.8); - ASSERT_NEAR(agg_stats.stats[0].hour_stats.wire_response_bytes, 16.8); - ASSERT_NEAR(agg_stats.stats[0].total_stats.wire_response_bytes, 16.8); + census_get_client_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 1); + GPR_ASSERT (agg_stats.stats != NULL); + GPR_ASSERT (strcmp (agg_stats.stats[0].method, "m1") == 0); + GPR_ASSERT (agg_stats.stats[0].minute_stats.cnt == 2 && agg_stats.stats[0].hour_stats.cnt == 2 && agg_stats.stats[0].total_stats.cnt == 2); + ASSERT_NEAR (agg_stats.stats[0].minute_stats.wire_response_bytes, 16.8); + ASSERT_NEAR (agg_stats.stats[0].hour_stats.wire_response_bytes, 16.8); + ASSERT_NEAR (agg_stats.stats[0].total_stats.wire_response_bytes, 16.8); /* Get stats again, results should be the same. */ - census_get_client_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 1); - census_aggregated_rpc_stats_set_empty(&agg_stats); - census_shutdown(); + census_get_client_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 1); + census_aggregated_rpc_stats_set_empty (&agg_stats); + census_shutdown (); - /* Record both server (once) and client (twice) stats with different op_ids.*/ - census_init(); - id = census_tracing_start_op(); - census_add_method_tag(id, "m2"); - census_record_rpc_client_stats(id, &stats); - census_tracing_end_op(id); - id = census_tracing_start_op(); - census_add_method_tag(id, "m3"); - census_record_rpc_server_stats(id, &stats); - census_tracing_end_op(id); - id = census_tracing_start_op(); - census_add_method_tag(id, "m4"); - census_record_rpc_client_stats(id, &stats); - census_tracing_end_op(id); + /* Record both server (once) and client (twice) stats with different op_ids. */ + census_init (); + id = census_tracing_start_op (); + census_add_method_tag (id, "m2"); + census_record_rpc_client_stats (id, &stats); + census_tracing_end_op (id); + id = census_tracing_start_op (); + census_add_method_tag (id, "m3"); + census_record_rpc_server_stats (id, &stats); + census_tracing_end_op (id); + id = census_tracing_start_op (); + census_add_method_tag (id, "m4"); + census_record_rpc_client_stats (id, &stats); + census_tracing_end_op (id); /* Check server stats */ - census_get_server_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 1); - GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m3") == 0); - GPR_ASSERT(agg_stats.stats[0].minute_stats.app_error_cnt == 3 && - agg_stats.stats[0].hour_stats.app_error_cnt == 3 && - agg_stats.stats[0].total_stats.app_error_cnt == 3); - census_aggregated_rpc_stats_set_empty(&agg_stats); + census_get_server_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 1); + GPR_ASSERT (strcmp (agg_stats.stats[0].method, "m3") == 0); + GPR_ASSERT (agg_stats.stats[0].minute_stats.app_error_cnt == 3 && agg_stats.stats[0].hour_stats.app_error_cnt == 3 && agg_stats.stats[0].total_stats.app_error_cnt == 3); + census_aggregated_rpc_stats_set_empty (&agg_stats); /* Check client stats */ - census_get_client_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 2); - GPR_ASSERT(agg_stats.stats != NULL); - GPR_ASSERT((strcmp(agg_stats.stats[0].method, "m2") == 0 && - strcmp(agg_stats.stats[1].method, "m4") == 0) || - (strcmp(agg_stats.stats[0].method, "m4") == 0 && - strcmp(agg_stats.stats[1].method, "m2") == 0)); - GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 1 && - agg_stats.stats[1].minute_stats.cnt == 1); - census_aggregated_rpc_stats_set_empty(&agg_stats); - census_shutdown(); + census_get_client_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 2); + GPR_ASSERT (agg_stats.stats != NULL); + GPR_ASSERT ((strcmp (agg_stats.stats[0].method, "m2") == 0 && strcmp (agg_stats.stats[1].method, "m4") == 0) || (strcmp (agg_stats.stats[0].method, "m4") == 0 && strcmp (agg_stats.stats[1].method, "m2") == 0)); + GPR_ASSERT (agg_stats.stats[0].minute_stats.cnt == 1 && agg_stats.stats[1].minute_stats.cnt == 1); + census_aggregated_rpc_stats_set_empty (&agg_stats); + census_shutdown (); } -static void test_record_stats_on_unknown_op_id(void) { - census_op_id unknown_id = {0xDEAD, 0xBEEF}; - census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; - census_aggregated_rpc_stats agg_stats = {0, NULL}; +static void +test_record_stats_on_unknown_op_id (void) +{ + census_op_id unknown_id = { 0xDEAD, 0xBEEF }; + census_rpc_stats stats = { 1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4 }; + census_aggregated_rpc_stats agg_stats = { 0, NULL }; - census_init(); + census_init (); /* Tests that recording stats against unknown id is noop. */ - census_record_rpc_client_stats(unknown_id, &stats); - census_record_rpc_server_stats(unknown_id, &stats); - census_get_server_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 0); - GPR_ASSERT(agg_stats.stats == NULL); - census_get_client_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 0); - GPR_ASSERT(agg_stats.stats == NULL); - census_aggregated_rpc_stats_set_empty(&agg_stats); - census_shutdown(); + census_record_rpc_client_stats (unknown_id, &stats); + census_record_rpc_server_stats (unknown_id, &stats); + census_get_server_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 0); + GPR_ASSERT (agg_stats.stats == NULL); + census_get_client_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 0); + GPR_ASSERT (agg_stats.stats == NULL); + census_aggregated_rpc_stats_set_empty (&agg_stats); + census_shutdown (); } /* Test that record stats is noop when trace store is uninitialized. */ -static void test_record_stats_with_trace_store_uninitialized(void) { - census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; - census_op_id id = {0, 0}; - census_aggregated_rpc_stats agg_stats = {0, NULL}; +static void +test_record_stats_with_trace_store_uninitialized (void) +{ + census_rpc_stats stats = { 1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4 }; + census_op_id id = { 0, 0 }; + census_aggregated_rpc_stats agg_stats = { 0, NULL }; - census_init(); - id = census_tracing_start_op(); - census_add_method_tag(id, "m"); - census_tracing_end_op(id); + census_init (); + id = census_tracing_start_op (); + census_add_method_tag (id, "m"); + census_tracing_end_op (id); /* shuts down trace store only. */ - census_tracing_shutdown(); - census_record_rpc_client_stats(id, &stats); - census_get_client_stats(&agg_stats); - GPR_ASSERT(agg_stats.num_entries == 0); - census_stats_store_shutdown(); + census_tracing_shutdown (); + census_record_rpc_client_stats (id, &stats); + census_get_client_stats (&agg_stats); + GPR_ASSERT (agg_stats.num_entries == 0); + census_stats_store_shutdown (); } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - test_init_shutdown(); - test_create_and_destroy(); - test_record_and_get_stats(); - test_record_stats_on_unknown_op_id(); - test_record_stats_with_trace_store_uninitialized(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_init_shutdown (); + test_create_and_destroy (); + test_record_and_get_stats (); + test_record_stats_on_unknown_op_id (); + test_record_stats_with_trace_store_uninitialized (); return 0; } diff --git a/test/core/statistics/small_log_test.c b/test/core/statistics/small_log_test.c index b26b95f639..4f1f80f229 100644 --- a/test/core/statistics/small_log_test.c +++ b/test/core/statistics/small_log_test.c @@ -38,9 +38,11 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - test_small_log(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + test_small_log (); return 0; } diff --git a/test/core/statistics/trace_test.c b/test/core/statistics/trace_test.c index 187884d90f..ac2e0d824d 100644 --- a/test/core/statistics/trace_test.c +++ b/test/core/statistics/trace_test.c @@ -47,209 +47,237 @@ #include "test/core/util/test_config.h" /* Ensure all possible state transitions are called without causing problem */ -static void test_init_shutdown(void) { - census_tracing_init(); - census_tracing_init(); - census_tracing_shutdown(); - census_tracing_shutdown(); - census_tracing_init(); +static void +test_init_shutdown (void) +{ + census_tracing_init (); + census_tracing_init (); + census_tracing_shutdown (); + census_tracing_shutdown (); + census_tracing_init (); } -static void test_start_op_generates_locally_unique_ids(void) { +static void +test_start_op_generates_locally_unique_ids (void) +{ /* Check that ids generated within window size of 1000 are unique. TODO(hongyu): Replace O(n^2) duplicate detection algorithm with O(nlogn) algorithm. Enhance the test to larger window size (>10^6) */ #define WINDOW_SIZE 1000 census_op_id ids[WINDOW_SIZE]; int i; - census_init(); - for (i = 0; i < WINDOW_SIZE; i++) { - ids[i] = census_tracing_start_op(); - census_tracing_end_op(ids[i]); - } - for (i = 0; i < WINDOW_SIZE - 1; i++) { - int j; - for (j = i + 1; j < WINDOW_SIZE; j++) { - GPR_ASSERT(ids[i].upper != ids[j].upper || ids[i].lower != ids[j].lower); + census_init (); + for (i = 0; i < WINDOW_SIZE; i++) + { + ids[i] = census_tracing_start_op (); + census_tracing_end_op (ids[i]); + } + for (i = 0; i < WINDOW_SIZE - 1; i++) + { + int j; + for (j = i + 1; j < WINDOW_SIZE; j++) + { + GPR_ASSERT (ids[i].upper != ids[j].upper || ids[i].lower != ids[j].lower); + } } - } #undef WINDOW_SIZE - census_shutdown(); + census_shutdown (); } -static void test_get_trace_method_name(void) { +static void +test_get_trace_method_name (void) +{ census_op_id id; const char write_name[] = "service/method"; - census_tracing_init(); - id = census_tracing_start_op(); - census_add_method_tag(id, write_name); - census_internal_lock_trace_store(); + census_tracing_init (); + id = census_tracing_start_op (); + census_add_method_tag (id, write_name); + census_internal_lock_trace_store (); { - const char* read_name = - census_get_trace_method_name(census_get_trace_obj_locked(id)); - GPR_ASSERT(strcmp(read_name, write_name) == 0); + const char *read_name = census_get_trace_method_name (census_get_trace_obj_locked (id)); + GPR_ASSERT (strcmp (read_name, write_name) == 0); } - census_internal_unlock_trace_store(); - census_tracing_shutdown(); + census_internal_unlock_trace_store (); + census_tracing_shutdown (); } -typedef struct thd_arg { +typedef struct thd_arg +{ int num_done; gpr_cv done; gpr_mu mu; } thd_arg; -static void mimic_trace_op_sequences(void* arg) { +static void +mimic_trace_op_sequences (void *arg) +{ census_op_id id; - const char* method_name = "service_foo/method_bar"; + const char *method_name = "service_foo/method_bar"; int i = 0; const int num_iter = 200; - thd_arg* args = (thd_arg*)arg; - GPR_ASSERT(args != NULL); - gpr_log(GPR_INFO, "Start trace op sequence thread."); - for (i = 0; i < num_iter; i++) { - id = census_tracing_start_op(); - census_add_method_tag(id, method_name); - /* pretend doing 1us work. */ - gpr_sleep_until(GRPC_TIMEOUT_MICROS_TO_DEADLINE(1)); - census_tracing_end_op(id); - } - gpr_log(GPR_INFO, "End trace op sequence thread."); - gpr_mu_lock(&args->mu); + thd_arg *args = (thd_arg *) arg; + GPR_ASSERT (args != NULL); + gpr_log (GPR_INFO, "Start trace op sequence thread."); + for (i = 0; i < num_iter; i++) + { + id = census_tracing_start_op (); + census_add_method_tag (id, method_name); + /* pretend doing 1us work. */ + gpr_sleep_until (GRPC_TIMEOUT_MICROS_TO_DEADLINE (1)); + census_tracing_end_op (id); + } + gpr_log (GPR_INFO, "End trace op sequence thread."); + gpr_mu_lock (&args->mu); args->num_done += 1; - gpr_cv_broadcast(&args->done); - gpr_mu_unlock(&args->mu); + gpr_cv_broadcast (&args->done); + gpr_mu_unlock (&args->mu); } -static void test_concurrency(void) { +static void +test_concurrency (void) +{ #define NUM_THREADS 1000 gpr_thd_id tid[NUM_THREADS]; int i = 0; thd_arg arg; arg.num_done = 0; - gpr_mu_init(&arg.mu); - gpr_cv_init(&arg.done); - census_tracing_init(); - for (i = 0; i < NUM_THREADS; ++i) { - gpr_thd_new(tid + i, mimic_trace_op_sequences, &arg, NULL); - } - gpr_mu_lock(&arg.mu); - while (arg.num_done < NUM_THREADS) { - gpr_log(GPR_INFO, "num done %d", arg.num_done); - gpr_cv_wait(&arg.done, &arg.mu, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - gpr_mu_unlock(&arg.mu); - census_tracing_shutdown(); + gpr_mu_init (&arg.mu); + gpr_cv_init (&arg.done); + census_tracing_init (); + for (i = 0; i < NUM_THREADS; ++i) + { + gpr_thd_new (tid + i, mimic_trace_op_sequences, &arg, NULL); + } + gpr_mu_lock (&arg.mu); + while (arg.num_done < NUM_THREADS) + { + gpr_log (GPR_INFO, "num done %d", arg.num_done); + gpr_cv_wait (&arg.done, &arg.mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + gpr_mu_unlock (&arg.mu); + census_tracing_shutdown (); #undef NUM_THREADS } -static void test_add_method_tag_to_unknown_op_id(void) { - census_op_id unknown_id = {0xDEAD, 0xBEEF}; +static void +test_add_method_tag_to_unknown_op_id (void) +{ + census_op_id unknown_id = { 0xDEAD, 0xBEEF }; int ret = 0; - census_tracing_init(); - ret = census_add_method_tag(unknown_id, "foo"); - GPR_ASSERT(ret != 0); - census_tracing_shutdown(); + census_tracing_init (); + ret = census_add_method_tag (unknown_id, "foo"); + GPR_ASSERT (ret != 0); + census_tracing_shutdown (); } -static void test_trace_print(void) { +static void +test_trace_print (void) +{ census_op_id id; int i; - const char* annotation_txt[4] = {"abc", "", "$%^ *()_"}; + const char *annotation_txt[4] = { "abc", "", "$%^ *()_" }; char long_txt[CENSUS_MAX_ANNOTATION_LENGTH + 10]; - memset(long_txt, 'a', GPR_ARRAY_SIZE(long_txt)); + memset (long_txt, 'a', GPR_ARRAY_SIZE (long_txt)); long_txt[CENSUS_MAX_ANNOTATION_LENGTH + 9] = '\0'; annotation_txt[3] = long_txt; - census_tracing_init(); - id = census_tracing_start_op(); + census_tracing_init (); + id = census_tracing_start_op (); /* Adds large number of annotations to each trace */ - for (i = 0; i < 1000; i++) { - census_tracing_print(id, - annotation_txt[i % GPR_ARRAY_SIZE(annotation_txt)]); - } - census_tracing_end_op(id); + for (i = 0; i < 1000; i++) + { + census_tracing_print (id, annotation_txt[i % GPR_ARRAY_SIZE (annotation_txt)]); + } + census_tracing_end_op (id); - census_tracing_shutdown(); + census_tracing_shutdown (); } /* Returns 1 if two ids are equal, otherwise returns 0. */ -static int ids_equal(census_op_id id1, census_op_id id2) { +static int +ids_equal (census_op_id id1, census_op_id id2) +{ return (id1.upper == id2.upper) && (id1.lower == id2.lower); } -static void test_get_active_ops(void) { +static void +test_get_active_ops (void) +{ census_op_id id_1, id_2, id_3; - census_trace_obj** active_ops; - const char* annotation_txt[] = {"annotation 1", "a2"}; + census_trace_obj **active_ops; + const char *annotation_txt[] = { "annotation 1", "a2" }; int i = 0; int n = 0; - gpr_log(GPR_INFO, "test_get_active_ops"); - census_tracing_init(); + gpr_log (GPR_INFO, "test_get_active_ops"); + census_tracing_init (); /* No active ops before calling start_op(). */ - active_ops = census_get_active_ops(&n); - GPR_ASSERT(active_ops == NULL); - GPR_ASSERT(n == 0); + active_ops = census_get_active_ops (&n); + GPR_ASSERT (active_ops == NULL); + GPR_ASSERT (n == 0); /* Starts one op */ - id_1 = census_tracing_start_op(); - census_add_method_tag(id_1, "foo_1"); - active_ops = census_get_active_ops(&n); - GPR_ASSERT(active_ops != NULL); - GPR_ASSERT(n == 1); - GPR_ASSERT(ids_equal(active_ops[0]->id, id_1)); - census_trace_obj_destroy(active_ops[0]); - gpr_free(active_ops); + id_1 = census_tracing_start_op (); + census_add_method_tag (id_1, "foo_1"); + active_ops = census_get_active_ops (&n); + GPR_ASSERT (active_ops != NULL); + GPR_ASSERT (n == 1); + GPR_ASSERT (ids_equal (active_ops[0]->id, id_1)); + census_trace_obj_destroy (active_ops[0]); + gpr_free (active_ops); active_ops = NULL; /* Start the second and the third ops */ - id_2 = census_tracing_start_op(); - census_add_method_tag(id_2, "foo_2"); - id_3 = census_tracing_start_op(); - census_add_method_tag(id_3, "foo_3"); - - active_ops = census_get_active_ops(&n); - GPR_ASSERT(n == 3); - for (i = 0; i < 3; i++) { - census_trace_obj_destroy(active_ops[i]); - } - gpr_free(active_ops); + id_2 = census_tracing_start_op (); + census_add_method_tag (id_2, "foo_2"); + id_3 = census_tracing_start_op (); + census_add_method_tag (id_3, "foo_3"); + + active_ops = census_get_active_ops (&n); + GPR_ASSERT (n == 3); + for (i = 0; i < 3; i++) + { + census_trace_obj_destroy (active_ops[i]); + } + gpr_free (active_ops); active_ops = NULL; - /* End the second op and add annotations to the third ops*/ - census_tracing_end_op(id_2); - census_tracing_print(id_3, annotation_txt[0]); - census_tracing_print(id_3, annotation_txt[1]); + /* End the second op and add annotations to the third ops */ + census_tracing_end_op (id_2); + census_tracing_print (id_3, annotation_txt[0]); + census_tracing_print (id_3, annotation_txt[1]); - active_ops = census_get_active_ops(&n); - GPR_ASSERT(active_ops != NULL); - GPR_ASSERT(n == 2); - for (i = 0; i < 2; i++) { - census_trace_obj_destroy(active_ops[i]); - } - gpr_free(active_ops); + active_ops = census_get_active_ops (&n); + GPR_ASSERT (active_ops != NULL); + GPR_ASSERT (n == 2); + for (i = 0; i < 2; i++) + { + census_trace_obj_destroy (active_ops[i]); + } + gpr_free (active_ops); active_ops = NULL; /* End all ops. */ - census_tracing_end_op(id_1); - census_tracing_end_op(id_3); - active_ops = census_get_active_ops(&n); - GPR_ASSERT(active_ops == NULL); - GPR_ASSERT(n == 0); + census_tracing_end_op (id_1); + census_tracing_end_op (id_3); + active_ops = census_get_active_ops (&n); + GPR_ASSERT (active_ops == NULL); + GPR_ASSERT (n == 0); - census_tracing_shutdown(); + census_tracing_shutdown (); } -int main(int argc, char** argv) { - grpc_test_init(argc, argv); - test_init_shutdown(); - test_start_op_generates_locally_unique_ids(); - test_get_trace_method_name(); - test_concurrency(); - test_add_method_tag_to_unknown_op_id(); - test_trace_print(); - test_get_active_ops(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_init_shutdown (); + test_start_op_generates_locally_unique_ids (); + test_get_trace_method_name (); + test_concurrency (); + test_add_method_tag_to_unknown_op_id (); + test_trace_print (); + test_get_active_ops (); return 0; } diff --git a/test/core/statistics/window_stats_test.c b/test/core/statistics/window_stats_test.c index 9e637ccce9..54999d02be 100644 --- a/test/core/statistics/window_stats_test.c +++ b/test/core/statistics/window_stats_test.c @@ -37,281 +37,276 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -typedef struct test_stat { +typedef struct test_stat +{ double value1; int value2; } test_stat; -void add_test_stat(void* base, const void* addme) { - test_stat* b = (test_stat*)base; - const test_stat* a = (const test_stat*)addme; +void +add_test_stat (void *base, const void *addme) +{ + test_stat *b = (test_stat *) base; + const test_stat *a = (const test_stat *) addme; b->value1 += a->value1; b->value2 += a->value2; } -void add_proportion_test_stat(double p, void* base, const void* addme) { - test_stat* b = (test_stat*)base; - const test_stat* a = (const test_stat*)addme; +void +add_proportion_test_stat (double p, void *base, const void *addme) +{ + test_stat *b = (test_stat *) base; + const test_stat *a = (const test_stat *) addme; b->value1 += p * a->value1; - b->value2 += p * a->value2 + 0.5; /* +0.5 is poor mans (no c99) round() */ + b->value2 += p * a->value2 + 0.5; /* +0.5 is poor mans (no c99) round() */ } const struct census_window_stats_stat_info kMyStatInfo = { - sizeof(test_stat), NULL, add_test_stat, add_proportion_test_stat}; + sizeof (test_stat), NULL, add_test_stat, add_proportion_test_stat +}; -const gpr_timespec kMilliSecInterval = {0, 1000000}; -const gpr_timespec kSecInterval = {1, 0}; -const gpr_timespec kMinInterval = {60, 0}; -const gpr_timespec kHourInterval = {3600, 0}; -const gpr_timespec kPrimeInterval = {0, 101}; +const gpr_timespec kMilliSecInterval = { 0, 1000000 }; +const gpr_timespec kSecInterval = { 1, 0 }; +const gpr_timespec kMinInterval = { 60, 0 }; +const gpr_timespec kHourInterval = { 3600, 0 }; +const gpr_timespec kPrimeInterval = { 0, 101 }; -static int compare_double(double a, double b, double epsilon) { - if (a >= b) { - return (a > b + epsilon) ? 1 : 0; - } else { - return (b > a + epsilon) ? -1 : 0; - } +static int +compare_double (double a, double b, double epsilon) +{ + if (a >= b) + { + return (a > b + epsilon) ? 1 : 0; + } + else + { + return (b > a + epsilon) ? -1 : 0; + } } -void empty_test(void) { +void +empty_test (void) +{ census_window_stats_sums result; - const gpr_timespec zero = {0, 0}; + const gpr_timespec zero = { 0, 0 }; test_stat sum; - struct census_window_stats* stats = - census_window_stats_create(1, &kMinInterval, 5, &kMyStatInfo); - GPR_ASSERT(stats != NULL); + struct census_window_stats *stats = census_window_stats_create (1, &kMinInterval, 5, &kMyStatInfo); + GPR_ASSERT (stats != NULL); result.statistic = ∑ - census_window_stats_get_sums(stats, zero, &result); - GPR_ASSERT(result.count == 0 && sum.value1 == 0 && sum.value2 == 0); - census_window_stats_get_sums(stats, gpr_now(GPR_CLOCK_REALTIME), &result); - GPR_ASSERT(result.count == 0 && sum.value1 == 0 && sum.value2 == 0); - census_window_stats_destroy(stats); + census_window_stats_get_sums (stats, zero, &result); + GPR_ASSERT (result.count == 0 && sum.value1 == 0 && sum.value2 == 0); + census_window_stats_get_sums (stats, gpr_now (GPR_CLOCK_REALTIME), &result); + GPR_ASSERT (result.count == 0 && sum.value1 == 0 && sum.value2 == 0); + census_window_stats_destroy (stats); } -void one_interval_test(void) { - const test_stat value = {0.1, 4}; +void +one_interval_test (void) +{ + const test_stat value = { 0.1, 4 }; const double epsilon = 1e10 - 11; - gpr_timespec when = {0, 0}; + gpr_timespec when = { 0, 0 }; census_window_stats_sums result; test_stat sum; /* granularity == 5 so width of internal windows should be 12s */ - struct census_window_stats* stats = - census_window_stats_create(1, &kMinInterval, 5, &kMyStatInfo); - GPR_ASSERT(stats != NULL); + struct census_window_stats *stats = census_window_stats_create (1, &kMinInterval, 5, &kMyStatInfo); + GPR_ASSERT (stats != NULL); /* phase 1: insert a single value at t=0s, and check that various measurement times result in expected output values */ - census_window_stats_add(stats, when, &value); + census_window_stats_add (stats, when, &value); result.statistic = ∑ /* when = 0s, values extracted should be everything */ - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 1, epsilon) == 0 && - compare_double(sum.value1, value.value1, epsilon) == 0 && - sum.value2 == value.value2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 1, epsilon) == 0 && compare_double (sum.value1, value.value1, epsilon) == 0 && sum.value2 == value.value2); /* when = 6,30,60s, should be all of the data */ when.tv_sec = 6; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 1.0, epsilon) == 0 && - compare_double(sum.value1, value.value1, epsilon) == 0 && - sum.value2 == value.value2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 1.0, epsilon) == 0 && compare_double (sum.value1, value.value1, epsilon) == 0 && sum.value2 == value.value2); /* when == 30s,60s, should be all of the data */ when.tv_sec = 30; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 1.0, epsilon) == 0 && - compare_double(sum.value1, value.value1, epsilon) == 0 && - sum.value2 == value.value2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 1.0, epsilon) == 0 && compare_double (sum.value1, value.value1, epsilon) == 0 && sum.value2 == value.value2); when.tv_sec = 60; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 1.0, epsilon) == 0 && - compare_double(sum.value1, value.value1, epsilon) == 0 && - sum.value2 == value.value2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 1.0, epsilon) == 0 && compare_double (sum.value1, value.value1, epsilon) == 0 && sum.value2 == value.value2); /* when = 66s, should be half (only take half of bottom bucket) */ when.tv_sec = 66; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 0.5, epsilon) == 0 && - compare_double(sum.value1, value.value1 / 2, epsilon) == 0 && - sum.value2 == value.value2 / 2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 0.5, epsilon) == 0 && compare_double (sum.value1, value.value1 / 2, epsilon) == 0 && sum.value2 == value.value2 / 2); /* when = 72s, should be completely out of window */ when.tv_sec = 72; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 0, epsilon) == 0 && - compare_double(sum.value1, 0, epsilon) == 0 && sum.value2 == 0); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 0, epsilon) == 0 && compare_double (sum.value1, 0, epsilon) == 0 && sum.value2 == 0); /* phase 2: tear down and do as before, but inserting two values */ - census_window_stats_destroy(stats); - stats = census_window_stats_create(1, &kMinInterval, 5, &kMyStatInfo); - GPR_ASSERT(stats != NULL); + census_window_stats_destroy (stats); + stats = census_window_stats_create (1, &kMinInterval, 5, &kMyStatInfo); + GPR_ASSERT (stats != NULL); when.tv_sec = 0; when.tv_nsec = 17; - census_window_stats_add(stats, when, &value); + census_window_stats_add (stats, when, &value); when.tv_sec = 1; - census_window_stats_add(stats, when, &value); + census_window_stats_add (stats, when, &value); when.tv_sec = 0; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 0, epsilon) == 0 && - compare_double(sum.value1, 0, epsilon) == 0 && sum.value2 == 0); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 0, epsilon) == 0 && compare_double (sum.value1, 0, epsilon) == 0 && sum.value2 == 0); /* time = 3s, 30s, should get all data */ when.tv_sec = 3; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 2, epsilon) == 0 && - compare_double(sum.value1, 2 * value.value1, epsilon) == 0 && - sum.value2 == 2 * value.value2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 2, epsilon) == 0 && compare_double (sum.value1, 2 * value.value1, epsilon) == 0 && sum.value2 == 2 * value.value2); when.tv_sec = 30; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(compare_double(result.count, 2, epsilon) == 0 && - compare_double(sum.value1, 2 * value.value1, epsilon) == 0 && - sum.value2 == 2 * value.value2); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (compare_double (result.count, 2, epsilon) == 0 && compare_double (sum.value1, 2 * value.value1, epsilon) == 0 && sum.value2 == 2 * value.value2); /* phase 3: insert into "middle" bucket, and force a shift, pushing out the two values in bottom bucket */ when.tv_sec = 30; - census_window_stats_add(stats, when, &value); + census_window_stats_add (stats, when, &value); when.tv_sec = 76; - census_window_stats_add(stats, when, &value); + census_window_stats_add (stats, when, &value); when.tv_sec = 0; - census_window_stats_get_sums(stats, when, &result); - GPR_ASSERT(result.count == 0 && sum.value1 == 0 && sum.value2 == 0); + census_window_stats_get_sums (stats, when, &result); + GPR_ASSERT (result.count == 0 && sum.value1 == 0 && sum.value2 == 0); when.tv_sec = 30; - census_window_stats_get_sums(stats, when, &result); + census_window_stats_get_sums (stats, when, &result); /* half of the single value in the 30 second bucket */ - GPR_ASSERT(compare_double(result.count, 0.5, epsilon) == 0 && - compare_double(sum.value1, value.value1 / 2, epsilon) == 0 && - sum.value2 == value.value2 / 2); + GPR_ASSERT (compare_double (result.count, 0.5, epsilon) == 0 && compare_double (sum.value1, value.value1 / 2, epsilon) == 0 && sum.value2 == value.value2 / 2); when.tv_sec = 74; - census_window_stats_get_sums(stats, when, &result); + census_window_stats_get_sums (stats, when, &result); /* half of the 76 second bucket, all of the 30 second bucket */ - GPR_ASSERT(compare_double(result.count, 1.5, epsilon) == 0 && - compare_double(sum.value1, value.value1 * 1.5, epsilon) == 0 && - sum.value2 == value.value2 / 2 * 3); + GPR_ASSERT (compare_double (result.count, 1.5, epsilon) == 0 && compare_double (sum.value1, value.value1 * 1.5, epsilon) == 0 && sum.value2 == value.value2 / 2 * 3); when.tv_sec = 76; - census_window_stats_get_sums(stats, when, &result); + census_window_stats_get_sums (stats, when, &result); /* >=76s, get all of the 76 second bucket, all of the 30 second bucket */ - GPR_ASSERT(compare_double(result.count, 2, epsilon) == 0 && - compare_double(sum.value1, value.value1 * 2, epsilon) == 0 && - sum.value2 == value.value2 * 2); + GPR_ASSERT (compare_double (result.count, 2, epsilon) == 0 && compare_double (sum.value1, value.value1 * 2, epsilon) == 0 && sum.value2 == value.value2 * 2); when.tv_sec = 78; - census_window_stats_get_sums(stats, when, &result); + census_window_stats_get_sums (stats, when, &result); /* half of the 76 second bucket, all of the 30 second bucket */ - GPR_ASSERT(compare_double(result.count, 2, epsilon) == 0 && - compare_double(sum.value1, value.value1 * 2, epsilon) == 0 && - sum.value2 == value.value2 * 2); - census_window_stats_destroy(stats); + GPR_ASSERT (compare_double (result.count, 2, epsilon) == 0 && compare_double (sum.value1, value.value1 * 2, epsilon) == 0 && sum.value2 == value.value2 * 2); + census_window_stats_destroy (stats); } -void many_interval_test(void) { +void +many_interval_test (void) +{ gpr_timespec intervals[4]; - const test_stat value = {123.45, 8}; + const test_stat value = { 123.45, 8 }; const double epsilon = 1e10 - 11; - gpr_timespec when = {3600, 0}; /* one hour */ + gpr_timespec when = { 3600, 0 }; /* one hour */ census_window_stats_sums result[4]; test_stat sums[4]; int i; - struct census_window_stats* stats; + struct census_window_stats *stats; intervals[0] = kMilliSecInterval; intervals[1] = kSecInterval; intervals[2] = kMinInterval; intervals[3] = kHourInterval; - for (i = 0; i < 4; i++) { - result[i].statistic = &sums[i]; - } - stats = census_window_stats_create(4, intervals, 100, &kMyStatInfo); - GPR_ASSERT(stats != NULL); + for (i = 0; i < 4; i++) + { + result[i].statistic = &sums[i]; + } + stats = census_window_stats_create (4, intervals, 100, &kMyStatInfo); + GPR_ASSERT (stats != NULL); /* add 10 stats within half of each time range */ - for (i = 0; i < 10; i++) { - when.tv_sec += 180; /* covers 30 min of one hour range */ - census_window_stats_add(stats, when, &value); - } + for (i = 0; i < 10; i++) + { + when.tv_sec += 180; /* covers 30 min of one hour range */ + census_window_stats_add (stats, when, &value); + } when.tv_sec += 120; - for (i = 0; i < 10; i++) { - when.tv_sec += 3; /* covers 30 sec of one minute range */ - census_window_stats_add(stats, when, &value); - } + for (i = 0; i < 10; i++) + { + when.tv_sec += 3; /* covers 30 sec of one minute range */ + census_window_stats_add (stats, when, &value); + } when.tv_sec += 2; - for (i = 0; i < 10; i++) { - when.tv_nsec += 50000000; /* covers 0.5s of 1s range */ - census_window_stats_add(stats, when, &value); - } + for (i = 0; i < 10; i++) + { + when.tv_nsec += 50000000; /* covers 0.5s of 1s range */ + census_window_stats_add (stats, when, &value); + } when.tv_nsec += 2000000; - for (i = 0; i < 10; i++) { - when.tv_nsec += 50000; /* covers 0.5 ms of 1 ms range */ - census_window_stats_add(stats, when, &value); - } + for (i = 0; i < 10; i++) + { + when.tv_nsec += 50000; /* covers 0.5 ms of 1 ms range */ + census_window_stats_add (stats, when, &value); + } when.tv_nsec += 20000; - census_window_stats_get_sums(stats, when, result); - GPR_ASSERT(compare_double(result[0].count, 10, epsilon) == 0 && - compare_double(sums[0].value1, value.value1 * 10, epsilon) == 0 && - sums[0].value2 == value.value2 * 10); + census_window_stats_get_sums (stats, when, result); + GPR_ASSERT (compare_double (result[0].count, 10, epsilon) == 0 && compare_double (sums[0].value1, value.value1 * 10, epsilon) == 0 && sums[0].value2 == value.value2 * 10); when.tv_nsec += 20000000; - census_window_stats_get_sums(stats, when, result); - GPR_ASSERT(compare_double(result[1].count, 20, epsilon) == 0 && - compare_double(sums[1].value1, value.value1 * 20, epsilon) == 0 && - sums[1].value2 == value.value2 * 20); + census_window_stats_get_sums (stats, when, result); + GPR_ASSERT (compare_double (result[1].count, 20, epsilon) == 0 && compare_double (sums[1].value1, value.value1 * 20, epsilon) == 0 && sums[1].value2 == value.value2 * 20); when.tv_sec += 2; - census_window_stats_get_sums(stats, when, result); - GPR_ASSERT(compare_double(result[2].count, 30, epsilon) == 0 && - compare_double(sums[2].value1, value.value1 * 30, epsilon) == 0 && - sums[2].value2 == value.value2 * 30); + census_window_stats_get_sums (stats, when, result); + GPR_ASSERT (compare_double (result[2].count, 30, epsilon) == 0 && compare_double (sums[2].value1, value.value1 * 30, epsilon) == 0 && sums[2].value2 == value.value2 * 30); when.tv_sec += 72; - census_window_stats_get_sums(stats, when, result); - GPR_ASSERT(compare_double(result[3].count, 40, epsilon) == 0 && - compare_double(sums[3].value1, value.value1 * 40, epsilon) == 0 && - sums[3].value2 == value.value2 * 40); - census_window_stats_destroy(stats); + census_window_stats_get_sums (stats, when, result); + GPR_ASSERT (compare_double (result[3].count, 40, epsilon) == 0 && compare_double (sums[3].value1, value.value1 * 40, epsilon) == 0 && sums[3].value2 == value.value2 * 40); + census_window_stats_destroy (stats); } -void rolling_time_test(void) { - const test_stat value = {0.1, 4}; - gpr_timespec when = {0, 0}; +void +rolling_time_test (void) +{ + const test_stat value = { 0.1, 4 }; + gpr_timespec when = { 0, 0 }; census_window_stats_sums result; test_stat sum; int i; - gpr_timespec increment = {0, 0}; - struct census_window_stats* stats = - census_window_stats_create(1, &kMinInterval, 7, &kMyStatInfo); - GPR_ASSERT(stats != NULL); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - for (i = 0; i < 100000; i++) { - increment.tv_nsec = rand() % 100000000; /* up to 1/10th second */ - when = gpr_time_add(when, increment); - census_window_stats_add(stats, when, &value); - } + gpr_timespec increment = { 0, 0 }; + struct census_window_stats *stats = census_window_stats_create (1, &kMinInterval, 7, &kMyStatInfo); + GPR_ASSERT (stats != NULL); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + for (i = 0; i < 100000; i++) + { + increment.tv_nsec = rand () % 100000000; /* up to 1/10th second */ + when = gpr_time_add (when, increment); + census_window_stats_add (stats, when, &value); + } result.statistic = ∑ - census_window_stats_get_sums(stats, when, &result); + census_window_stats_get_sums (stats, when, &result); /* With 1/20th second average between samples, we expect 20*60 = 1200 samples on average. Make sure we are within 100 of that. */ - GPR_ASSERT(compare_double(result.count, 1200, 100) == 0); - census_window_stats_destroy(stats); + GPR_ASSERT (compare_double (result.count, 1200, 100) == 0); + census_window_stats_destroy (stats); } + #include <stdio.h> -void infinite_interval_test(void) { - const test_stat value = {0.1, 4}; - gpr_timespec when = {0, 0}; +void +infinite_interval_test (void) +{ + const test_stat value = { 0.1, 4 }; + gpr_timespec when = { 0, 0 }; census_window_stats_sums result; test_stat sum; int i; const int count = 100000; - gpr_timespec increment = {0, 0}; - struct census_window_stats* stats = census_window_stats_create( - 1, &gpr_inf_future(GPR_CLOCK_REALTIME), 10, &kMyStatInfo); - srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); - for (i = 0; i < count; i++) { - increment.tv_sec = rand() % 21600; /* 6 hours */ - when = gpr_time_add(when, increment); - census_window_stats_add(stats, when, &value); - } + gpr_timespec increment = { 0, 0 }; + struct census_window_stats *stats = census_window_stats_create (1, &gpr_inf_future (GPR_CLOCK_REALTIME), 10, &kMyStatInfo); + srand (gpr_now (GPR_CLOCK_REALTIME).tv_nsec); + for (i = 0; i < count; i++) + { + increment.tv_sec = rand () % 21600; /* 6 hours */ + when = gpr_time_add (when, increment); + census_window_stats_add (stats, when, &value); + } result.statistic = ∑ - census_window_stats_get_sums(stats, when, &result); + census_window_stats_get_sums (stats, when, &result); /* The only thing it makes sense to compare for "infinite" periods is the total counts */ - GPR_ASSERT(result.count == count); - census_window_stats_destroy(stats); + GPR_ASSERT (result.count == count); + census_window_stats_destroy (stats); } -int main(int argc, char* argv[]) { - grpc_test_init(argc, argv); - empty_test(); - one_interval_test(); - many_interval_test(); - rolling_time_test(); - infinite_interval_test(); +int +main (int argc, char *argv[]) +{ + grpc_test_init (argc, argv); + empty_test (); + one_interval_test (); + many_interval_test (); + rolling_time_test (); + infinite_interval_test (); return 0; } diff --git a/test/core/support/cmdline_test.c b/test/core/support/cmdline_test.c index 1c77c15233..5102beb519 100644 --- a/test/core/support/cmdline_test.c +++ b/test/core/support/cmdline_test.c @@ -42,238 +42,270 @@ #define LOG_TEST() gpr_log(GPR_INFO, "%s", __FILE__) -static void test_simple_int(void) { +static void +test_simple_int (void) +{ int x = 1; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "-foo", "3"}; + char *args[] = { (char *) __FILE__, "-foo", "3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_int(cl, "foo", NULL, &x); - GPR_ASSERT(x == 1); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 3); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_int (cl, "foo", NULL, &x); + GPR_ASSERT (x == 1); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 3); + gpr_cmdline_destroy (cl); } -static void test_eq_int(void) { +static void +test_eq_int (void) +{ int x = 1; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "-foo=3"}; + char *args[] = { (char *) __FILE__, "-foo=3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_int(cl, "foo", NULL, &x); - GPR_ASSERT(x == 1); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 3); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_int (cl, "foo", NULL, &x); + GPR_ASSERT (x == 1); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 3); + gpr_cmdline_destroy (cl); } -static void test_2dash_int(void) { +static void +test_2dash_int (void) +{ int x = 1; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo", "3"}; + char *args[] = { (char *) __FILE__, "--foo", "3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_int(cl, "foo", NULL, &x); - GPR_ASSERT(x == 1); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 3); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_int (cl, "foo", NULL, &x); + GPR_ASSERT (x == 1); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 3); + gpr_cmdline_destroy (cl); } -static void test_2dash_eq_int(void) { +static void +test_2dash_eq_int (void) +{ int x = 1; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo=3"}; + char *args[] = { (char *) __FILE__, "--foo=3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_int(cl, "foo", NULL, &x); - GPR_ASSERT(x == 1); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 3); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_int (cl, "foo", NULL, &x); + GPR_ASSERT (x == 1); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 3); + gpr_cmdline_destroy (cl); } -static void test_simple_string(void) { +static void +test_simple_string (void) +{ char *x = NULL; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "-foo", "3"}; + char *args[] = { (char *) __FILE__, "-foo", "3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_string(cl, "foo", NULL, &x); - GPR_ASSERT(x == NULL); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(0 == strcmp(x, "3")); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_string (cl, "foo", NULL, &x); + GPR_ASSERT (x == NULL); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (0 == strcmp (x, "3")); + gpr_cmdline_destroy (cl); } -static void test_eq_string(void) { +static void +test_eq_string (void) +{ char *x = NULL; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "-foo=3"}; + char *args[] = { (char *) __FILE__, "-foo=3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_string(cl, "foo", NULL, &x); - GPR_ASSERT(x == NULL); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(0 == strcmp(x, "3")); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_string (cl, "foo", NULL, &x); + GPR_ASSERT (x == NULL); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (0 == strcmp (x, "3")); + gpr_cmdline_destroy (cl); } -static void test_2dash_string(void) { +static void +test_2dash_string (void) +{ char *x = NULL; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo", "3"}; + char *args[] = { (char *) __FILE__, "--foo", "3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_string(cl, "foo", NULL, &x); - GPR_ASSERT(x == NULL); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(0 == strcmp(x, "3")); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_string (cl, "foo", NULL, &x); + GPR_ASSERT (x == NULL); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (0 == strcmp (x, "3")); + gpr_cmdline_destroy (cl); } -static void test_2dash_eq_string(void) { +static void +test_2dash_eq_string (void) +{ char *x = NULL; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo=3"}; + char *args[] = { (char *) __FILE__, "--foo=3" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_string(cl, "foo", NULL, &x); - GPR_ASSERT(x == NULL); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(0 == strcmp(x, "3")); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_string (cl, "foo", NULL, &x); + GPR_ASSERT (x == NULL); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (0 == strcmp (x, "3")); + gpr_cmdline_destroy (cl); } -static void test_flag_on(void) { +static void +test_flag_on (void) +{ int x = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo"}; + char *args[] = { (char *) __FILE__, "--foo" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_flag(cl, "foo", NULL, &x); - GPR_ASSERT(x == 2); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 1); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_flag (cl, "foo", NULL, &x); + GPR_ASSERT (x == 2); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 1); + gpr_cmdline_destroy (cl); } -static void test_flag_no(void) { +static void +test_flag_no (void) +{ int x = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--no-foo"}; + char *args[] = { (char *) __FILE__, "--no-foo" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_flag(cl, "foo", NULL, &x); - GPR_ASSERT(x == 2); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 0); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_flag (cl, "foo", NULL, &x); + GPR_ASSERT (x == 2); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 0); + gpr_cmdline_destroy (cl); } -static void test_flag_val_1(void) { +static void +test_flag_val_1 (void) +{ int x = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo=1"}; + char *args[] = { (char *) __FILE__, "--foo=1" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_flag(cl, "foo", NULL, &x); - GPR_ASSERT(x == 2); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 1); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_flag (cl, "foo", NULL, &x); + GPR_ASSERT (x == 2); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 1); + gpr_cmdline_destroy (cl); } -static void test_flag_val_0(void) { +static void +test_flag_val_0 (void) +{ int x = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo=0"}; + char *args[] = { (char *) __FILE__, "--foo=0" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_flag(cl, "foo", NULL, &x); - GPR_ASSERT(x == 2); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 0); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_flag (cl, "foo", NULL, &x); + GPR_ASSERT (x == 2); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 0); + gpr_cmdline_destroy (cl); } -static void test_flag_val_true(void) { +static void +test_flag_val_true (void) +{ int x = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo=true"}; + char *args[] = { (char *) __FILE__, "--foo=true" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_flag(cl, "foo", NULL, &x); - GPR_ASSERT(x == 2); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 1); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_flag (cl, "foo", NULL, &x); + GPR_ASSERT (x == 2); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 1); + gpr_cmdline_destroy (cl); } -static void test_flag_val_false(void) { +static void +test_flag_val_false (void) +{ int x = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--foo=false"}; + char *args[] = { (char *) __FILE__, "--foo=false" }; - LOG_TEST(); + LOG_TEST (); - cl = gpr_cmdline_create(NULL); - gpr_cmdline_add_flag(cl, "foo", NULL, &x); - GPR_ASSERT(x == 2); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 0); - gpr_cmdline_destroy(cl); + cl = gpr_cmdline_create (NULL); + gpr_cmdline_add_flag (cl, "foo", NULL, &x); + GPR_ASSERT (x == 2); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 0); + gpr_cmdline_destroy (cl); } -static void test_many(void) { +static void +test_many (void) +{ char *str = NULL; int x = 0; int flag = 2; gpr_cmdline *cl; - char *args[] = {(char *)__FILE__, "--str", "hello", "-x=4", "-no-flag"}; + char *args[] = { (char *) __FILE__, "--str", "hello", "-x=4", "-no-flag" }; - LOG_TEST(); + LOG_TEST (); - 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); - gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); - GPR_ASSERT(x == 4); - GPR_ASSERT(0 == strcmp(str, "hello")); - GPR_ASSERT(flag == 0); - gpr_cmdline_destroy(cl); + 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); + gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); + GPR_ASSERT (x == 4); + GPR_ASSERT (0 == strcmp (str, "hello")); + GPR_ASSERT (flag == 0); + gpr_cmdline_destroy (cl); } -static void test_usage(void) { +static void +test_usage (void) +{ gpr_cmdline *cl; char *usage; @@ -281,37 +313,37 @@ static void test_usage(void) { 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); + 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); + 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); + gpr_cmdline_destroy (cl); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_simple_int(); - test_eq_int(); - test_2dash_int(); - test_2dash_eq_int(); - test_simple_string(); - test_eq_string(); - test_2dash_string(); - test_2dash_eq_string(); - test_flag_on(); - test_flag_no(); - test_flag_val_1(); - test_flag_val_0(); - test_flag_val_true(); - test_flag_val_false(); - test_many(); - test_usage(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_simple_int (); + test_eq_int (); + test_2dash_int (); + test_2dash_eq_int (); + test_simple_string (); + test_eq_string (); + test_2dash_string (); + test_2dash_eq_string (); + test_flag_on (); + test_flag_no (); + test_flag_val_1 (); + test_flag_val_0 (); + test_flag_val_true (); + test_flag_val_false (); + test_many (); + test_usage (); return 0; } diff --git a/test/core/support/env_test.c b/test/core/support/env_test.c index 69aebcc918..f208253540 100644 --- a/test/core/support/env_test.c +++ b/test/core/support/env_test.c @@ -43,22 +43,26 @@ #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x) -static void test_setenv_getenv(void) { +static void +test_setenv_getenv (void) +{ const char *name = "FOO"; const char *value = "BAR"; char *retrieved_value; - LOG_TEST_NAME("test_setenv_getenv"); + LOG_TEST_NAME ("test_setenv_getenv"); - gpr_setenv(name, value); - retrieved_value = gpr_getenv(name); - GPR_ASSERT(retrieved_value != NULL); - GPR_ASSERT(strcmp(value, retrieved_value) == 0); - gpr_free(retrieved_value); + gpr_setenv (name, value); + retrieved_value = gpr_getenv (name); + GPR_ASSERT (retrieved_value != NULL); + GPR_ASSERT (strcmp (value, retrieved_value) == 0); + gpr_free (retrieved_value); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_setenv_getenv(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_setenv_getenv (); return 0; } diff --git a/test/core/support/file_test.c b/test/core/support/file_test.c index 330b2173ef..600448d778 100644 --- a/test/core/support/file_test.c +++ b/test/core/support/file_test.c @@ -46,57 +46,63 @@ static const char prefix[] = "file_test"; -static void test_load_empty_file(void) { +static void +test_load_empty_file (void) +{ FILE *tmp = NULL; gpr_slice slice; gpr_slice slice_with_null_term; int success; char *tmp_name; - LOG_TEST_NAME("test_load_empty_file"); + LOG_TEST_NAME ("test_load_empty_file"); - tmp = gpr_tmpfile(prefix, &tmp_name); - GPR_ASSERT(tmp_name != NULL); - GPR_ASSERT(tmp != NULL); - fclose(tmp); + tmp = gpr_tmpfile (prefix, &tmp_name); + GPR_ASSERT (tmp_name != NULL); + GPR_ASSERT (tmp != NULL); + fclose (tmp); - slice = gpr_load_file(tmp_name, 0, &success); - GPR_ASSERT(success == 1); - GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); + slice = gpr_load_file (tmp_name, 0, &success); + GPR_ASSERT (success == 1); + GPR_ASSERT (GPR_SLICE_LENGTH (slice) == 0); - slice_with_null_term = gpr_load_file(tmp_name, 1, &success); - GPR_ASSERT(success == 1); - GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == 1); - GPR_ASSERT(GPR_SLICE_START_PTR(slice_with_null_term)[0] == 0); + slice_with_null_term = gpr_load_file (tmp_name, 1, &success); + GPR_ASSERT (success == 1); + GPR_ASSERT (GPR_SLICE_LENGTH (slice_with_null_term) == 1); + GPR_ASSERT (GPR_SLICE_START_PTR (slice_with_null_term)[0] == 0); - remove(tmp_name); - gpr_free(tmp_name); - gpr_slice_unref(slice); - gpr_slice_unref(slice_with_null_term); + remove (tmp_name); + gpr_free (tmp_name); + gpr_slice_unref (slice); + gpr_slice_unref (slice_with_null_term); } -static void test_load_failure(void) { +static void +test_load_failure (void) +{ FILE *tmp = NULL; gpr_slice slice; int success; char *tmp_name; - LOG_TEST_NAME("test_load_failure"); + LOG_TEST_NAME ("test_load_failure"); - tmp = gpr_tmpfile(prefix, &tmp_name); - GPR_ASSERT(tmp_name != NULL); - GPR_ASSERT(tmp != NULL); - fclose(tmp); - remove(tmp_name); + tmp = gpr_tmpfile (prefix, &tmp_name); + GPR_ASSERT (tmp_name != NULL); + GPR_ASSERT (tmp != NULL); + fclose (tmp); + remove (tmp_name); - slice = gpr_load_file(tmp_name, 0, &success); - GPR_ASSERT(success == 0); - GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); - gpr_free(tmp_name); - gpr_slice_unref(slice); + slice = gpr_load_file (tmp_name, 0, &success); + GPR_ASSERT (success == 0); + GPR_ASSERT (GPR_SLICE_LENGTH (slice) == 0); + gpr_free (tmp_name); + gpr_slice_unref (slice); } -static void test_load_small_file(void) { +static void +test_load_small_file (void) +{ FILE *tmp = NULL; gpr_slice slice; gpr_slice slice_with_null_term; @@ -104,32 +110,33 @@ static void test_load_small_file(void) { char *tmp_name; const char *blah = "blah"; - LOG_TEST_NAME("test_load_small_file"); - - tmp = gpr_tmpfile(prefix, &tmp_name); - GPR_ASSERT(tmp_name != NULL); - GPR_ASSERT(tmp != NULL); - GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah)); - fclose(tmp); - - slice = gpr_load_file(tmp_name, 0, &success); - GPR_ASSERT(success == 1); - GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah)); - GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah))); - - slice_with_null_term = gpr_load_file(tmp_name, 1, &success); - GPR_ASSERT(success == 1); - GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1)); - GPR_ASSERT(strcmp((const char *)GPR_SLICE_START_PTR(slice_with_null_term), - blah) == 0); - - remove(tmp_name); - gpr_free(tmp_name); - gpr_slice_unref(slice); - gpr_slice_unref(slice_with_null_term); + LOG_TEST_NAME ("test_load_small_file"); + + tmp = gpr_tmpfile (prefix, &tmp_name); + GPR_ASSERT (tmp_name != NULL); + GPR_ASSERT (tmp != NULL); + GPR_ASSERT (fwrite (blah, 1, strlen (blah), tmp) == strlen (blah)); + fclose (tmp); + + slice = gpr_load_file (tmp_name, 0, &success); + GPR_ASSERT (success == 1); + GPR_ASSERT (GPR_SLICE_LENGTH (slice) == strlen (blah)); + GPR_ASSERT (!memcmp (GPR_SLICE_START_PTR (slice), blah, strlen (blah))); + + slice_with_null_term = gpr_load_file (tmp_name, 1, &success); + GPR_ASSERT (success == 1); + GPR_ASSERT (GPR_SLICE_LENGTH (slice_with_null_term) == (strlen (blah) + 1)); + GPR_ASSERT (strcmp ((const char *) GPR_SLICE_START_PTR (slice_with_null_term), blah) == 0); + + remove (tmp_name); + gpr_free (tmp_name); + gpr_slice_unref (slice); + gpr_slice_unref (slice_with_null_term); } -static void test_load_big_file(void) { +static void +test_load_big_file (void) +{ FILE *tmp = NULL; gpr_slice slice; int success; @@ -138,36 +145,40 @@ static void test_load_big_file(void) { unsigned char *current; size_t i; - LOG_TEST_NAME("test_load_big_file"); - - for (i = 0; i < sizeof(buffer); i++) { - buffer[i] = 42; - } - - tmp = gpr_tmpfile(prefix, &tmp_name); - GPR_ASSERT(tmp != NULL); - GPR_ASSERT(tmp_name != NULL); - GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer)); - fclose(tmp); - - slice = gpr_load_file(tmp_name, 0, &success); - GPR_ASSERT(success == 1); - GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer)); - current = GPR_SLICE_START_PTR(slice); - for (i = 0; i < sizeof(buffer); i++) { - GPR_ASSERT(current[i] == 42); - } - - remove(tmp_name); - gpr_free(tmp_name); - gpr_slice_unref(slice); + LOG_TEST_NAME ("test_load_big_file"); + + for (i = 0; i < sizeof (buffer); i++) + { + buffer[i] = 42; + } + + tmp = gpr_tmpfile (prefix, &tmp_name); + GPR_ASSERT (tmp != NULL); + GPR_ASSERT (tmp_name != NULL); + GPR_ASSERT (fwrite (buffer, 1, sizeof (buffer), tmp) == sizeof (buffer)); + fclose (tmp); + + slice = gpr_load_file (tmp_name, 0, &success); + GPR_ASSERT (success == 1); + GPR_ASSERT (GPR_SLICE_LENGTH (slice) == sizeof (buffer)); + current = GPR_SLICE_START_PTR (slice); + for (i = 0; i < sizeof (buffer); i++) + { + GPR_ASSERT (current[i] == 42); + } + + remove (tmp_name); + gpr_free (tmp_name); + gpr_slice_unref (slice); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_load_empty_file(); - test_load_failure(); - test_load_small_file(); - test_load_big_file(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_load_empty_file (); + test_load_failure (); + test_load_small_file (); + test_load_big_file (); return 0; } diff --git a/test/core/support/histogram_test.c b/test/core/support/histogram_test.c index be935ede09..d13aeb651d 100644 --- a/test/core/support/histogram_test.c +++ b/test/core/support/histogram_test.c @@ -36,143 +36,155 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x); -static void test_no_op(void) { - gpr_histogram_destroy(gpr_histogram_create(0.01, 60e9)); +static void +test_no_op (void) +{ + gpr_histogram_destroy (gpr_histogram_create (0.01, 60e9)); } -static void expect_percentile(gpr_histogram *h, double percentile, - double min_expect, double max_expect) { - double got = gpr_histogram_percentile(h, percentile); - gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, - max_expect); - GPR_ASSERT(min_expect <= got); - GPR_ASSERT(got <= max_expect); +static void +expect_percentile (gpr_histogram * h, double percentile, double min_expect, double max_expect) +{ + double got = gpr_histogram_percentile (h, percentile); + gpr_log (GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, max_expect); + GPR_ASSERT (min_expect <= got); + GPR_ASSERT (got <= max_expect); } -static void test_simple(void) { +static void +test_simple (void) +{ gpr_histogram *h; - LOG_TEST("test_simple"); + LOG_TEST ("test_simple"); - h = gpr_histogram_create(0.01, 60e9); - gpr_histogram_add(h, 10000); - gpr_histogram_add(h, 10000); - gpr_histogram_add(h, 11000); - gpr_histogram_add(h, 11000); + h = gpr_histogram_create (0.01, 60e9); + gpr_histogram_add (h, 10000); + gpr_histogram_add (h, 10000); + gpr_histogram_add (h, 11000); + gpr_histogram_add (h, 11000); - expect_percentile(h, 50, 10001, 10999); - GPR_ASSERT(gpr_histogram_mean(h) == 10500); + expect_percentile (h, 50, 10001, 10999); + GPR_ASSERT (gpr_histogram_mean (h) == 10500); - gpr_histogram_destroy(h); + gpr_histogram_destroy (h); } -static void test_percentile(void) { +static void +test_percentile (void) +{ gpr_histogram *h; double last; double i; double cur; - LOG_TEST("test_percentile"); - - h = gpr_histogram_create(0.05, 1e9); - gpr_histogram_add(h, 2.5); - gpr_histogram_add(h, 2.5); - gpr_histogram_add(h, 8); - gpr_histogram_add(h, 4); - - GPR_ASSERT(gpr_histogram_count(h) == 4); - GPR_ASSERT(gpr_histogram_minimum(h) == 2.5); - GPR_ASSERT(gpr_histogram_maximum(h) == 8); - GPR_ASSERT(gpr_histogram_sum(h) == 17); - GPR_ASSERT(gpr_histogram_sum_of_squares(h) == 92.5); - GPR_ASSERT(gpr_histogram_mean(h) == 4.25); - GPR_ASSERT(gpr_histogram_variance(h) == 5.0625); - GPR_ASSERT(gpr_histogram_stddev(h) == 2.25); - - expect_percentile(h, -10, 2.5, 2.5); - expect_percentile(h, 0, 2.5, 2.5); - expect_percentile(h, 12.5, 2.5, 2.5); - expect_percentile(h, 25, 2.5, 2.5); - expect_percentile(h, 37.5, 2.5, 2.8); - expect_percentile(h, 50, 3.0, 3.5); - expect_percentile(h, 62.5, 3.5, 4.5); - expect_percentile(h, 75, 5, 7.9); - expect_percentile(h, 100, 8, 8); - expect_percentile(h, 110, 8, 8); + LOG_TEST ("test_percentile"); + + h = gpr_histogram_create (0.05, 1e9); + gpr_histogram_add (h, 2.5); + gpr_histogram_add (h, 2.5); + gpr_histogram_add (h, 8); + gpr_histogram_add (h, 4); + + GPR_ASSERT (gpr_histogram_count (h) == 4); + GPR_ASSERT (gpr_histogram_minimum (h) == 2.5); + GPR_ASSERT (gpr_histogram_maximum (h) == 8); + GPR_ASSERT (gpr_histogram_sum (h) == 17); + GPR_ASSERT (gpr_histogram_sum_of_squares (h) == 92.5); + GPR_ASSERT (gpr_histogram_mean (h) == 4.25); + GPR_ASSERT (gpr_histogram_variance (h) == 5.0625); + GPR_ASSERT (gpr_histogram_stddev (h) == 2.25); + + expect_percentile (h, -10, 2.5, 2.5); + expect_percentile (h, 0, 2.5, 2.5); + expect_percentile (h, 12.5, 2.5, 2.5); + expect_percentile (h, 25, 2.5, 2.5); + expect_percentile (h, 37.5, 2.5, 2.8); + expect_percentile (h, 50, 3.0, 3.5); + expect_percentile (h, 62.5, 3.5, 4.5); + expect_percentile (h, 75, 5, 7.9); + expect_percentile (h, 100, 8, 8); + expect_percentile (h, 110, 8, 8); /* test monotonicity */ last = 0.0; - for (i = 0; i < 100.0; i += 0.01) { - cur = gpr_histogram_percentile(h, i); - GPR_ASSERT(cur >= last); - last = cur; - } - - gpr_histogram_destroy(h); + for (i = 0; i < 100.0; i += 0.01) + { + cur = gpr_histogram_percentile (h, i); + GPR_ASSERT (cur >= last); + last = cur; + } + + gpr_histogram_destroy (h); } -static void test_merge(void) { +static void +test_merge (void) +{ gpr_histogram *h1, *h2; double last; double i; double cur; - LOG_TEST("test_merge"); - - h1 = gpr_histogram_create(0.05, 1e9); - gpr_histogram_add(h1, 2.5); - gpr_histogram_add(h1, 2.5); - gpr_histogram_add(h1, 8); - gpr_histogram_add(h1, 4); - - h2 = gpr_histogram_create(0.01, 1e9); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); - gpr_histogram_destroy(h2); - - h2 = gpr_histogram_create(0.05, 1e10); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); - gpr_histogram_destroy(h2); - - h2 = gpr_histogram_create(0.05, 1e9); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); - GPR_ASSERT(gpr_histogram_count(h1) == 4); - GPR_ASSERT(gpr_histogram_minimum(h1) == 2.5); - GPR_ASSERT(gpr_histogram_maximum(h1) == 8); - GPR_ASSERT(gpr_histogram_sum(h1) == 17); - GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 92.5); - GPR_ASSERT(gpr_histogram_mean(h1) == 4.25); - GPR_ASSERT(gpr_histogram_variance(h1) == 5.0625); - GPR_ASSERT(gpr_histogram_stddev(h1) == 2.25); - gpr_histogram_destroy(h2); - - h2 = gpr_histogram_create(0.05, 1e9); - gpr_histogram_add(h2, 7.0); - gpr_histogram_add(h2, 17.0); - gpr_histogram_add(h2, 1.0); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); - GPR_ASSERT(gpr_histogram_count(h1) == 7); - GPR_ASSERT(gpr_histogram_minimum(h1) == 1.0); - GPR_ASSERT(gpr_histogram_maximum(h1) == 17.0); - GPR_ASSERT(gpr_histogram_sum(h1) == 42.0); - GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 431.5); - GPR_ASSERT(gpr_histogram_mean(h1) == 6.0); + LOG_TEST ("test_merge"); + + h1 = gpr_histogram_create (0.05, 1e9); + gpr_histogram_add (h1, 2.5); + gpr_histogram_add (h1, 2.5); + gpr_histogram_add (h1, 8); + gpr_histogram_add (h1, 4); + + h2 = gpr_histogram_create (0.01, 1e9); + GPR_ASSERT (gpr_histogram_merge (h1, h2) == 0); + gpr_histogram_destroy (h2); + + h2 = gpr_histogram_create (0.05, 1e10); + GPR_ASSERT (gpr_histogram_merge (h1, h2) == 0); + gpr_histogram_destroy (h2); + + h2 = gpr_histogram_create (0.05, 1e9); + GPR_ASSERT (gpr_histogram_merge (h1, h2) == 1); + GPR_ASSERT (gpr_histogram_count (h1) == 4); + GPR_ASSERT (gpr_histogram_minimum (h1) == 2.5); + GPR_ASSERT (gpr_histogram_maximum (h1) == 8); + GPR_ASSERT (gpr_histogram_sum (h1) == 17); + GPR_ASSERT (gpr_histogram_sum_of_squares (h1) == 92.5); + GPR_ASSERT (gpr_histogram_mean (h1) == 4.25); + GPR_ASSERT (gpr_histogram_variance (h1) == 5.0625); + GPR_ASSERT (gpr_histogram_stddev (h1) == 2.25); + gpr_histogram_destroy (h2); + + h2 = gpr_histogram_create (0.05, 1e9); + gpr_histogram_add (h2, 7.0); + gpr_histogram_add (h2, 17.0); + gpr_histogram_add (h2, 1.0); + GPR_ASSERT (gpr_histogram_merge (h1, h2) == 1); + GPR_ASSERT (gpr_histogram_count (h1) == 7); + GPR_ASSERT (gpr_histogram_minimum (h1) == 1.0); + GPR_ASSERT (gpr_histogram_maximum (h1) == 17.0); + GPR_ASSERT (gpr_histogram_sum (h1) == 42.0); + GPR_ASSERT (gpr_histogram_sum_of_squares (h1) == 431.5); + GPR_ASSERT (gpr_histogram_mean (h1) == 6.0); /* test monotonicity */ last = 0.0; - for (i = 0; i < 100.0; i += 0.01) { - cur = gpr_histogram_percentile(h1, i); - GPR_ASSERT(cur >= last); - last = cur; - } - - gpr_histogram_destroy(h1); - gpr_histogram_destroy(h2); + for (i = 0; i < 100.0; i += 0.01) + { + cur = gpr_histogram_percentile (h1, i); + GPR_ASSERT (cur >= last); + last = cur; + } + + gpr_histogram_destroy (h1); + gpr_histogram_destroy (h2); } -int main(void) { - test_no_op(); - test_simple(); - test_percentile(); - test_merge(); +int +main (void) +{ + test_no_op (); + test_simple (); + test_percentile (); + test_merge (); return 0; } diff --git a/test/core/support/host_port_test.c b/test/core/support/host_port_test.c index eccc39a2db..71a774191b 100644 --- a/test/core/support/host_port_test.c +++ b/test/core/support/host_port_test.c @@ -38,36 +38,43 @@ #include <grpc/support/log.h> #include "test/core/util/test_config.h" -static void join_host_port_expect(const char *host, int port, - const char *expected) { +static void +join_host_port_expect (const char *host, int port, const char *expected) +{ char *buf; int len; - len = gpr_join_host_port(&buf, host, port); - GPR_ASSERT(len >= 0); - GPR_ASSERT(strlen(expected) == (size_t)len); - GPR_ASSERT(strcmp(expected, buf) == 0); - gpr_free(buf); + len = gpr_join_host_port (&buf, host, port); + GPR_ASSERT (len >= 0); + GPR_ASSERT (strlen (expected) == (size_t) len); + GPR_ASSERT (strcmp (expected, buf) == 0); + gpr_free (buf); } -static void test_join_host_port(void) { - join_host_port_expect("foo", 101, "foo:101"); - join_host_port_expect("", 102, ":102"); - join_host_port_expect("1::2", 103, "[1::2]:103"); - join_host_port_expect("[::1]", 104, "[::1]:104"); +static void +test_join_host_port (void) +{ + join_host_port_expect ("foo", 101, "foo:101"); + join_host_port_expect ("", 102, ":102"); + join_host_port_expect ("1::2", 103, "[1::2]:103"); + join_host_port_expect ("[::1]", 104, "[::1]:104"); } /* Garbage in, garbage out. */ -static void test_join_host_port_garbage(void) { - join_host_port_expect("[foo]", 105, "[foo]:105"); - join_host_port_expect("[::", 106, "[:::106"); - join_host_port_expect("::]", 107, "[::]]:107"); +static void +test_join_host_port_garbage (void) +{ + join_host_port_expect ("[foo]", 105, "[foo]:105"); + join_host_port_expect ("[::", 106, "[:::106"); + join_host_port_expect ("::]", 107, "[::]]:107"); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); - test_join_host_port(); - test_join_host_port_garbage(); + test_join_host_port (); + test_join_host_port_garbage (); return 0; } diff --git a/test/core/support/log_test.c b/test/core/support/log_test.c index b39b069913..c30ce6958f 100644 --- a/test/core/support/log_test.c +++ b/test/core/support/log_test.c @@ -37,23 +37,27 @@ #include "test/core/util/test_config.h" -static void test_callback(gpr_log_func_args *args) { - GPR_ASSERT(0 == strcmp(__FILE__, args->file)); - GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO); - GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3")); +static void +test_callback (gpr_log_func_args * args) +{ + GPR_ASSERT (0 == strcmp (__FILE__, args->file)); + GPR_ASSERT (args->severity == GPR_LOG_SEVERITY_INFO); + GPR_ASSERT (0 == strcmp (args->message, "hello 1 2 3")); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); /* test logging at various verbosity levels */ - gpr_log(GPR_DEBUG, "%s", "hello world"); - gpr_log(GPR_INFO, "%s", "hello world"); - gpr_log(GPR_ERROR, "%s", "hello world"); + gpr_log (GPR_DEBUG, "%s", "hello world"); + gpr_log (GPR_INFO, "%s", "hello world"); + gpr_log (GPR_ERROR, "%s", "hello world"); /* should succeed */ - GPR_ASSERT(1); - gpr_set_log_function(test_callback); - gpr_log_message(GPR_INFO, "hello 1 2 3"); - gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3); + GPR_ASSERT (1); + gpr_set_log_function (test_callback); + gpr_log_message (GPR_INFO, "hello 1 2 3"); + gpr_log (GPR_INFO, "hello %d %d %d", 1, 2, 3); /* TODO(ctiller): should we add a GPR_ASSERT failure test here */ return 0; } diff --git a/test/core/support/murmur_hash_test.c b/test/core/support/murmur_hash_test.c index 1762486776..946a800c60 100644 --- a/test/core/support/murmur_hash_test.c +++ b/test/core/support/murmur_hash_test.c @@ -38,51 +38,58 @@ #include <string.h> -typedef gpr_uint32 (*hash_func)(const void *key, size_t len, gpr_uint32 seed); +typedef gpr_uint32 (*hash_func) (const void *key, size_t len, gpr_uint32 seed); /* From smhasher: This should hopefully be a thorough and uambiguous test of whether a hash is correctly implemented on a given platform */ -static void verification_test(hash_func hash, gpr_uint32 expected) { +static void +verification_test (hash_func hash, gpr_uint32 expected) +{ gpr_uint8 key[256]; gpr_uint32 hashes[256]; gpr_uint32 final = 0; size_t i; - memset(key, 0, sizeof(key)); - memset(hashes, 0, sizeof(hashes)); + memset (key, 0, sizeof (key)); + memset (hashes, 0, sizeof (hashes)); /* Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as the seed */ - for (i = 0; i < 256; i++) { - key[i] = (gpr_uint8)i; - hashes[i] = hash(key, i, (gpr_uint32)(256u - i)); - } + for (i = 0; i < 256; i++) + { + key[i] = (gpr_uint8) i; + hashes[i] = hash (key, i, (gpr_uint32) (256u - i)); + } /* Then hash the result array */ - final = hash(hashes, sizeof(hashes), 0); + final = hash (hashes, sizeof (hashes), 0); /* The first four bytes of that hash, interpreted as a little-endian integer, is our verification value */ - if (expected != final) { - gpr_log(GPR_INFO, "Verification value 0x%08X : Failed! (Expected 0x%08x)", - final, expected); - abort(); - } else { - gpr_log(GPR_INFO, "Verification value 0x%08X : Passed!", final); - } + if (expected != final) + { + gpr_log (GPR_INFO, "Verification value 0x%08X : Failed! (Expected 0x%08x)", final, expected); + abort (); + } + else + { + gpr_log (GPR_INFO, "Verification value 0x%08X : Passed!", final); + } } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); /* basic tests to verify that things don't crash */ - gpr_murmur_hash3("", 0, 0); - gpr_murmur_hash3("xyz", 3, 0); - verification_test(gpr_murmur_hash3, 0xB0F57EE3); + gpr_murmur_hash3 ("", 0, 0); + gpr_murmur_hash3 ("xyz", 3, 0); + verification_test (gpr_murmur_hash3, 0xB0F57EE3); return 0; } diff --git a/test/core/support/slice_buffer_test.c b/test/core/support/slice_buffer_test.c index a48278434f..9254f709e3 100644 --- a/test/core/support/slice_buffer_test.c +++ b/test/core/support/slice_buffer_test.c @@ -35,41 +35,46 @@ #include <grpc/support/slice_buffer.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ gpr_slice_buffer buf; - gpr_slice aaa = gpr_slice_from_copied_string("aaa"); - gpr_slice bb = gpr_slice_from_copied_string("bb"); + gpr_slice aaa = gpr_slice_from_copied_string ("aaa"); + gpr_slice bb = gpr_slice_from_copied_string ("bb"); size_t i; - grpc_test_init(argc, argv); - gpr_slice_buffer_init(&buf); - for (i = 0; i < 10; i++) { - gpr_slice_ref(aaa); - gpr_slice_ref(bb); - gpr_slice_buffer_add(&buf, aaa); - gpr_slice_buffer_add(&buf, bb); - } - GPR_ASSERT(buf.count > 0); - GPR_ASSERT(buf.length == 50); - gpr_slice_buffer_reset_and_unref(&buf); - GPR_ASSERT(buf.count == 0); - GPR_ASSERT(buf.length == 0); - for (i = 0; i < 10; i++) { - gpr_slice_ref(aaa); - gpr_slice_ref(bb); - gpr_slice_buffer_add(&buf, aaa); - gpr_slice_buffer_add(&buf, bb); - } - GPR_ASSERT(buf.count > 0); - GPR_ASSERT(buf.length == 50); - for (i = 0; i < 10; i++) { - gpr_slice_buffer_pop(&buf); - gpr_slice_unref(aaa); - gpr_slice_unref(bb); - } - GPR_ASSERT(buf.count == 0); - GPR_ASSERT(buf.length == 0); - gpr_slice_buffer_destroy(&buf); + grpc_test_init (argc, argv); + gpr_slice_buffer_init (&buf); + for (i = 0; i < 10; i++) + { + gpr_slice_ref (aaa); + gpr_slice_ref (bb); + gpr_slice_buffer_add (&buf, aaa); + gpr_slice_buffer_add (&buf, bb); + } + GPR_ASSERT (buf.count > 0); + GPR_ASSERT (buf.length == 50); + gpr_slice_buffer_reset_and_unref (&buf); + GPR_ASSERT (buf.count == 0); + GPR_ASSERT (buf.length == 0); + for (i = 0; i < 10; i++) + { + gpr_slice_ref (aaa); + gpr_slice_ref (bb); + gpr_slice_buffer_add (&buf, aaa); + gpr_slice_buffer_add (&buf, bb); + } + GPR_ASSERT (buf.count > 0); + GPR_ASSERT (buf.length == 50); + for (i = 0; i < 10; i++) + { + gpr_slice_buffer_pop (&buf); + gpr_slice_unref (aaa); + gpr_slice_unref (bb); + } + GPR_ASSERT (buf.count == 0); + GPR_ASSERT (buf.length == 0); + gpr_slice_buffer_destroy (&buf); return 0; } diff --git a/test/core/support/slice_test.c b/test/core/support/slice_test.c index 1d202f0618..e041532c8b 100644 --- a/test/core/support/slice_test.c +++ b/test/core/support/slice_test.c @@ -41,188 +41,220 @@ #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x); -static void test_slice_malloc_returns_something_sensible(void) { +static void +test_slice_malloc_returns_something_sensible (void) +{ /* Calls gpr_slice_create for various lengths and verifies the internals for consistency. */ size_t length; size_t i; gpr_slice slice; - LOG_TEST_NAME("test_slice_malloc_returns_something_sensible"); - - for (length = 0; length <= 1024; length++) { - slice = gpr_slice_malloc(length); - /* If there is a length, slice.data must be non-NULL. If length is zero - we don't care. */ - if (length) { - GPR_ASSERT(GPR_SLICE_START_PTR(slice)); - } - /* Returned slice length must be what was requested. */ - GPR_ASSERT(GPR_SLICE_LENGTH(slice) == length); - /* If the slice has a refcount, it must be destroyable. */ - if (slice.refcount) { - GPR_ASSERT(slice.refcount->ref != NULL); - GPR_ASSERT(slice.refcount->unref != NULL); - } - /* We must be able to write to every byte of the data */ - for (i = 0; i < length; i++) { - GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; + LOG_TEST_NAME ("test_slice_malloc_returns_something_sensible"); + + for (length = 0; length <= 1024; length++) + { + slice = gpr_slice_malloc (length); + /* If there is a length, slice.data must be non-NULL. If length is zero + we don't care. */ + if (length) + { + GPR_ASSERT (GPR_SLICE_START_PTR (slice)); + } + /* Returned slice length must be what was requested. */ + GPR_ASSERT (GPR_SLICE_LENGTH (slice) == length); + /* If the slice has a refcount, it must be destroyable. */ + if (slice.refcount) + { + GPR_ASSERT (slice.refcount->ref != NULL); + GPR_ASSERT (slice.refcount->unref != NULL); + } + /* We must be able to write to every byte of the data */ + for (i = 0; i < length; i++) + { + GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; + } + /* And finally we must succeed in destroying the slice */ + gpr_slice_unref (slice); } - /* And finally we must succeed in destroying the slice */ - gpr_slice_unref(slice); - } } -static void do_nothing(void *ignored) {} +static void +do_nothing (void *ignored) +{ +} -static void test_slice_new_returns_something_sensible(void) { +static void +test_slice_new_returns_something_sensible (void) +{ gpr_uint8 x; - gpr_slice slice = gpr_slice_new(&x, 1, do_nothing); - GPR_ASSERT(slice.refcount); - GPR_ASSERT(slice.data.refcounted.bytes == &x); - GPR_ASSERT(slice.data.refcounted.length == 1); - gpr_slice_unref(slice); + gpr_slice slice = gpr_slice_new (&x, 1, do_nothing); + GPR_ASSERT (slice.refcount); + GPR_ASSERT (slice.data.refcounted.bytes == &x); + GPR_ASSERT (slice.data.refcounted.length == 1); + gpr_slice_unref (slice); } static int do_nothing_with_len_1_calls = 0; -static void do_nothing_with_len_1(void *ignored, size_t len) { - GPR_ASSERT(len == 1); +static void +do_nothing_with_len_1 (void *ignored, size_t len) +{ + GPR_ASSERT (len == 1); do_nothing_with_len_1_calls++; } -static void test_slice_new_with_len_returns_something_sensible(void) { +static void +test_slice_new_with_len_returns_something_sensible (void) +{ gpr_uint8 x; - gpr_slice slice = gpr_slice_new_with_len(&x, 1, do_nothing_with_len_1); - GPR_ASSERT(slice.refcount); - GPR_ASSERT(slice.data.refcounted.bytes == &x); - GPR_ASSERT(slice.data.refcounted.length == 1); - GPR_ASSERT(do_nothing_with_len_1_calls == 0); - gpr_slice_unref(slice); - GPR_ASSERT(do_nothing_with_len_1_calls == 1); + gpr_slice slice = gpr_slice_new_with_len (&x, 1, do_nothing_with_len_1); + GPR_ASSERT (slice.refcount); + GPR_ASSERT (slice.data.refcounted.bytes == &x); + GPR_ASSERT (slice.data.refcounted.length == 1); + GPR_ASSERT (do_nothing_with_len_1_calls == 0); + gpr_slice_unref (slice); + GPR_ASSERT (do_nothing_with_len_1_calls == 1); } -static void test_slice_sub_works(unsigned length) { +static void +test_slice_sub_works (unsigned length) +{ gpr_slice slice; gpr_slice sub; unsigned i, j, k; - LOG_TEST_NAME("test_slice_sub_works"); - gpr_log(GPR_INFO, "length=%d", length); + LOG_TEST_NAME ("test_slice_sub_works"); + gpr_log (GPR_INFO, "length=%d", length); /* Create a slice in which each byte is equal to the distance from it to the beginning of the slice. */ - slice = gpr_slice_malloc(length); - for (i = 0; i < length; i++) { - GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; - } + slice = gpr_slice_malloc (length); + for (i = 0; i < length; i++) + { + GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; + } /* Ensure that for all subsets length is correct and that we start on the correct byte. Additionally check that no copies were made. */ - for (i = 0; i < length; i++) { - for (j = i; j < length; j++) { - sub = gpr_slice_sub(slice, i, j); - GPR_ASSERT(GPR_SLICE_LENGTH(sub) == j - i); - for (k = 0; k < j - i; k++) { - GPR_ASSERT(GPR_SLICE_START_PTR(sub)[k] == (gpr_uint8)(i + k)); - } - gpr_slice_unref(sub); + for (i = 0; i < length; i++) + { + for (j = i; j < length; j++) + { + sub = gpr_slice_sub (slice, i, j); + GPR_ASSERT (GPR_SLICE_LENGTH (sub) == j - i); + for (k = 0; k < j - i; k++) + { + GPR_ASSERT (GPR_SLICE_START_PTR (sub)[k] == (gpr_uint8) (i + k)); + } + gpr_slice_unref (sub); + } } - } - gpr_slice_unref(slice); + gpr_slice_unref (slice); } -static void check_head_tail(gpr_slice slice, gpr_slice head, gpr_slice tail) { - GPR_ASSERT(GPR_SLICE_LENGTH(slice) == - GPR_SLICE_LENGTH(head) + GPR_SLICE_LENGTH(tail)); - GPR_ASSERT(0 == memcmp(GPR_SLICE_START_PTR(slice), GPR_SLICE_START_PTR(head), - GPR_SLICE_LENGTH(head))); - GPR_ASSERT(0 == memcmp(GPR_SLICE_START_PTR(slice) + GPR_SLICE_LENGTH(head), - GPR_SLICE_START_PTR(tail), GPR_SLICE_LENGTH(tail))); +static void +check_head_tail (gpr_slice slice, gpr_slice head, gpr_slice tail) +{ + GPR_ASSERT (GPR_SLICE_LENGTH (slice) == GPR_SLICE_LENGTH (head) + GPR_SLICE_LENGTH (tail)); + GPR_ASSERT (0 == memcmp (GPR_SLICE_START_PTR (slice), GPR_SLICE_START_PTR (head), GPR_SLICE_LENGTH (head))); + GPR_ASSERT (0 == memcmp (GPR_SLICE_START_PTR (slice) + GPR_SLICE_LENGTH (head), GPR_SLICE_START_PTR (tail), GPR_SLICE_LENGTH (tail))); } -static void test_slice_split_head_works(size_t length) { +static void +test_slice_split_head_works (size_t length) +{ gpr_slice slice; gpr_slice head, tail; size_t i; - LOG_TEST_NAME("test_slice_split_head_works"); - gpr_log(GPR_INFO, "length=%d", length); + LOG_TEST_NAME ("test_slice_split_head_works"); + gpr_log (GPR_INFO, "length=%d", length); /* Create a slice in which each byte is equal to the distance from it to the beginning of the slice. */ - slice = gpr_slice_malloc(length); - for (i = 0; i < length; i++) { - GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; - } + slice = gpr_slice_malloc (length); + for (i = 0; i < length; i++) + { + GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; + } /* Ensure that for all subsets length is correct and that we start on the correct byte. Additionally check that no copies were made. */ - for (i = 0; i < length; i++) { - tail = gpr_slice_ref(slice); - head = gpr_slice_split_head(&tail, i); - check_head_tail(slice, head, tail); - gpr_slice_unref(tail); - gpr_slice_unref(head); - } - - gpr_slice_unref(slice); + for (i = 0; i < length; i++) + { + tail = gpr_slice_ref (slice); + head = gpr_slice_split_head (&tail, i); + check_head_tail (slice, head, tail); + gpr_slice_unref (tail); + gpr_slice_unref (head); + } + + gpr_slice_unref (slice); } -static void test_slice_split_tail_works(size_t length) { +static void +test_slice_split_tail_works (size_t length) +{ gpr_slice slice; gpr_slice head, tail; size_t i; - LOG_TEST_NAME("test_slice_split_tail_works"); - gpr_log(GPR_INFO, "length=%d", length); + LOG_TEST_NAME ("test_slice_split_tail_works"); + gpr_log (GPR_INFO, "length=%d", length); /* Create a slice in which each byte is equal to the distance from it to the beginning of the slice. */ - slice = gpr_slice_malloc(length); - for (i = 0; i < length; i++) { - GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; - } + slice = gpr_slice_malloc (length); + for (i = 0; i < length; i++) + { + GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; + } /* Ensure that for all subsets length is correct and that we start on the correct byte. Additionally check that no copies were made. */ - for (i = 0; i < length; i++) { - head = gpr_slice_ref(slice); - tail = gpr_slice_split_tail(&head, i); - check_head_tail(slice, head, tail); - gpr_slice_unref(tail); - gpr_slice_unref(head); - } - - gpr_slice_unref(slice); + for (i = 0; i < length; i++) + { + head = gpr_slice_ref (slice); + tail = gpr_slice_split_tail (&head, i); + check_head_tail (slice, head, tail); + gpr_slice_unref (tail); + gpr_slice_unref (head); + } + + gpr_slice_unref (slice); } -static void test_slice_from_copied_string_works(void) { +static void +test_slice_from_copied_string_works (void) +{ static const char *text = "HELLO WORLD!"; gpr_slice slice; - LOG_TEST_NAME("test_slice_from_copied_string_works"); + LOG_TEST_NAME ("test_slice_from_copied_string_works"); - slice = gpr_slice_from_copied_string(text); - GPR_ASSERT(strlen(text) == GPR_SLICE_LENGTH(slice)); - GPR_ASSERT(0 == - memcmp(text, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice))); - gpr_slice_unref(slice); + slice = gpr_slice_from_copied_string (text); + GPR_ASSERT (strlen (text) == GPR_SLICE_LENGTH (slice)); + GPR_ASSERT (0 == memcmp (text, GPR_SLICE_START_PTR (slice), GPR_SLICE_LENGTH (slice))); + gpr_slice_unref (slice); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ unsigned length; - grpc_test_init(argc, argv); - test_slice_malloc_returns_something_sensible(); - test_slice_new_returns_something_sensible(); - test_slice_new_with_len_returns_something_sensible(); - for (length = 0; length < 128; length++) { - test_slice_sub_works(length); - test_slice_split_head_works(length); - test_slice_split_tail_works(length); - } - test_slice_from_copied_string_works(); + grpc_test_init (argc, argv); + test_slice_malloc_returns_something_sensible (); + test_slice_new_returns_something_sensible (); + test_slice_new_with_len_returns_something_sensible (); + for (length = 0; length < 128; length++) + { + test_slice_sub_works (length); + test_slice_split_head_works (length); + test_slice_split_tail_works (length); + } + test_slice_from_copied_string_works (); return 0; } diff --git a/test/core/support/stack_lockfree_test.c b/test/core/support/stack_lockfree_test.c index 0f49e6fa52..8ad4ad26df 100644 --- a/test/core/support/stack_lockfree_test.c +++ b/test/core/support/stack_lockfree_test.c @@ -46,42 +46,51 @@ #define MAX_THREADS 32 -static void test_serial_sized(size_t size) { - gpr_stack_lockfree *stack = gpr_stack_lockfree_create(size); +static void +test_serial_sized (size_t size) +{ + gpr_stack_lockfree *stack = gpr_stack_lockfree_create (size); size_t i; size_t j; /* First try popping empty */ - GPR_ASSERT(gpr_stack_lockfree_pop(stack) == -1); + GPR_ASSERT (gpr_stack_lockfree_pop (stack) == -1); /* Now add one item and check it */ - gpr_stack_lockfree_push(stack, 3); - GPR_ASSERT(gpr_stack_lockfree_pop(stack) == 3); - GPR_ASSERT(gpr_stack_lockfree_pop(stack) == -1); + gpr_stack_lockfree_push (stack, 3); + GPR_ASSERT (gpr_stack_lockfree_pop (stack) == 3); + GPR_ASSERT (gpr_stack_lockfree_pop (stack) == -1); /* Now add repeatedly more items and check them */ - for (i = 1; i < size; i *= 2) { - for (j = 0; j <= i; j++) { - GPR_ASSERT(gpr_stack_lockfree_push(stack, (int)j) == (j == 0)); + for (i = 1; i < size; i *= 2) + { + for (j = 0; j <= i; j++) + { + GPR_ASSERT (gpr_stack_lockfree_push (stack, (int) j) == (j == 0)); + } + for (j = 0; j <= i; j++) + { + GPR_ASSERT (gpr_stack_lockfree_pop (stack) == (int) (i - j)); + } + GPR_ASSERT (gpr_stack_lockfree_pop (stack) == -1); } - for (j = 0; j <= i; j++) { - GPR_ASSERT(gpr_stack_lockfree_pop(stack) == (int)(i - j)); - } - GPR_ASSERT(gpr_stack_lockfree_pop(stack) == -1); - } - gpr_stack_lockfree_destroy(stack); + gpr_stack_lockfree_destroy (stack); } -static void test_serial() { +static void +test_serial () +{ size_t i; - for (i = 128; i < MAX_STACK_SIZE; i *= 2) { - test_serial_sized(i); - } - test_serial_sized(MAX_STACK_SIZE); + for (i = 128; i < MAX_STACK_SIZE; i *= 2) + { + test_serial_sized (i); + } + test_serial_sized (MAX_STACK_SIZE); } -struct test_arg { +struct test_arg +{ gpr_stack_lockfree *stack; int stack_size; int nthreads; @@ -89,67 +98,83 @@ struct test_arg { int sum; }; -static void test_mt_body(void *v) { - struct test_arg *arg = (struct test_arg *)v; +static void +test_mt_body (void *v) +{ + struct test_arg *arg = (struct test_arg *) v; int lo, hi; int i; int res; lo = arg->rank * arg->stack_size / arg->nthreads; hi = (arg->rank + 1) * arg->stack_size / arg->nthreads; - for (i = lo; i < hi; i++) { - gpr_stack_lockfree_push(arg->stack, i); - if ((res = gpr_stack_lockfree_pop(arg->stack)) != -1) { + for (i = lo; i < hi; i++) + { + gpr_stack_lockfree_push (arg->stack, i); + if ((res = gpr_stack_lockfree_pop (arg->stack)) != -1) + { + arg->sum += res; + } + } + while ((res = gpr_stack_lockfree_pop (arg->stack)) != -1) + { arg->sum += res; } - } - while ((res = gpr_stack_lockfree_pop(arg->stack)) != -1) { - arg->sum += res; - } } -static void test_mt_sized(size_t size, int nth) { +static void +test_mt_sized (size_t size, int nth) +{ gpr_stack_lockfree *stack; struct test_arg args[MAX_THREADS]; gpr_thd_id thds[MAX_THREADS]; int sum; int i; - gpr_thd_options options = gpr_thd_options_default(); - - stack = gpr_stack_lockfree_create(size); - for (i = 0; i < nth; i++) { - args[i].stack = stack; - args[i].stack_size = (int)size; - args[i].nthreads = nth; - args[i].rank = i; - args[i].sum = 0; - } - gpr_thd_options_set_joinable(&options); - for (i = 0; i < nth; i++) { - GPR_ASSERT(gpr_thd_new(&thds[i], test_mt_body, &args[i], &options)); - } + gpr_thd_options options = gpr_thd_options_default (); + + stack = gpr_stack_lockfree_create (size); + for (i = 0; i < nth; i++) + { + args[i].stack = stack; + args[i].stack_size = (int) size; + args[i].nthreads = nth; + args[i].rank = i; + args[i].sum = 0; + } + gpr_thd_options_set_joinable (&options); + for (i = 0; i < nth; i++) + { + GPR_ASSERT (gpr_thd_new (&thds[i], test_mt_body, &args[i], &options)); + } sum = 0; - for (i = 0; i < nth; i++) { - gpr_thd_join(thds[i]); - sum = sum + args[i].sum; - } - GPR_ASSERT((unsigned)sum == ((unsigned)size * (size - 1)) / 2); - gpr_stack_lockfree_destroy(stack); + for (i = 0; i < nth; i++) + { + gpr_thd_join (thds[i]); + sum = sum + args[i].sum; + } + GPR_ASSERT ((unsigned) sum == ((unsigned) size * (size - 1)) / 2); + gpr_stack_lockfree_destroy (stack); } -static void test_mt() { +static void +test_mt () +{ size_t size; int nth; - for (nth = 1; nth < MAX_THREADS; nth++) { - for (size = 128; size < MAX_STACK_SIZE; size *= 2) { - test_mt_sized(size, nth); + for (nth = 1; nth < MAX_THREADS; nth++) + { + for (size = 128; size < MAX_STACK_SIZE; size *= 2) + { + test_mt_sized (size, nth); + } + test_mt_sized (MAX_STACK_SIZE, nth); } - test_mt_sized(MAX_STACK_SIZE, nth); - } } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_serial(); - test_mt(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_serial (); + test_mt (); return 0; } diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c index f62cbe3435..faf4d01a11 100644 --- a/test/core/support/string_test.c +++ b/test/core/support/string_test.c @@ -45,256 +45,273 @@ #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x) -static void test_strdup(void) { +static void +test_strdup (void) +{ static const char *src1 = "hello world"; char *dst1; - LOG_TEST_NAME("test_strdup"); + LOG_TEST_NAME ("test_strdup"); - dst1 = gpr_strdup(src1); - GPR_ASSERT(0 == strcmp(src1, dst1)); - gpr_free(dst1); + dst1 = gpr_strdup (src1); + GPR_ASSERT (0 == strcmp (src1, dst1)); + gpr_free (dst1); - GPR_ASSERT(NULL == gpr_strdup(NULL)); + GPR_ASSERT (NULL == gpr_strdup (NULL)); } -static void expect_dump(const char *buf, size_t len, gpr_uint32 flags, - const char *result) { - char *got = gpr_dump(buf, len, flags); - GPR_ASSERT(0 == strcmp(got, result)); - gpr_free(got); +static void +expect_dump (const char *buf, size_t len, gpr_uint32 flags, const char *result) +{ + char *got = gpr_dump (buf, len, flags); + GPR_ASSERT (0 == strcmp (got, result)); + gpr_free (got); } -static void test_dump(void) { - LOG_TEST_NAME("test_dump"); - expect_dump("\x01", 1, GPR_DUMP_HEX, "01"); - expect_dump("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); - expect_dump("\x01\x02", 2, GPR_DUMP_HEX, "01 02"); - expect_dump("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX, - "01 23 45 67 89 ab cd ef"); - expect_dump("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'"); +static void +test_dump (void) +{ + LOG_TEST_NAME ("test_dump"); + expect_dump ("\x01", 1, GPR_DUMP_HEX, "01"); + expect_dump ("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); + expect_dump ("\x01\x02", 2, GPR_DUMP_HEX, "01 02"); + expect_dump ("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX, "01 23 45 67 89 ab cd ef"); + expect_dump ("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'"); } -static void expect_slice_dump(gpr_slice slice, gpr_uint32 flags, - const char *result) { - char *got = gpr_dump_slice(slice, flags); - GPR_ASSERT(0 == strcmp(got, result)); - gpr_free(got); - gpr_slice_unref(slice); +static void +expect_slice_dump (gpr_slice slice, gpr_uint32 flags, const char *result) +{ + char *got = gpr_dump_slice (slice, flags); + GPR_ASSERT (0 == strcmp (got, result)); + gpr_free (got); + gpr_slice_unref (slice); } -static void test_dump_slice(void) { +static void +test_dump_slice (void) +{ static const char *text = "HELLO WORLD!"; - static const char *long_text = - "It was a bright cold day in April, and the clocks were striking " - "thirteen. Winston Smith, his chin nuzzled into his breast in an effort " - "to escape the vile wind, slipped quickly through the glass doors of " - "Victory Mansions, though not quickly enough to prevent a swirl of " - "gritty dust from entering along with him."; - - LOG_TEST_NAME("test_dump_slice"); - - expect_slice_dump(gpr_slice_from_copied_string(text), GPR_DUMP_ASCII, text); - expect_slice_dump(gpr_slice_from_copied_string(long_text), GPR_DUMP_ASCII, - long_text); - expect_slice_dump(gpr_slice_from_copied_buffer("\x01", 1), GPR_DUMP_HEX, - "01"); - expect_slice_dump(gpr_slice_from_copied_buffer("\x01", 1), - GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); + static const char *long_text = "It was a bright cold day in April, and the clocks were striking " "thirteen. Winston Smith, his chin nuzzled into his breast in an effort " "to escape the vile wind, slipped quickly through the glass doors of " "Victory Mansions, though not quickly enough to prevent a swirl of " "gritty dust from entering along with him."; + + LOG_TEST_NAME ("test_dump_slice"); + + expect_slice_dump (gpr_slice_from_copied_string (text), GPR_DUMP_ASCII, text); + expect_slice_dump (gpr_slice_from_copied_string (long_text), GPR_DUMP_ASCII, long_text); + expect_slice_dump (gpr_slice_from_copied_buffer ("\x01", 1), GPR_DUMP_HEX, "01"); + expect_slice_dump (gpr_slice_from_copied_buffer ("\x01", 1), GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); } -static void test_pu32_fail(const char *s) { +static void +test_pu32_fail (const char *s) +{ gpr_uint32 out; - GPR_ASSERT(!gpr_parse_bytes_to_uint32(s, strlen(s), &out)); + GPR_ASSERT (!gpr_parse_bytes_to_uint32 (s, strlen (s), &out)); } -static void test_pu32_succeed(const char *s, gpr_uint32 want) { +static void +test_pu32_succeed (const char *s, gpr_uint32 want) +{ gpr_uint32 out; - GPR_ASSERT(gpr_parse_bytes_to_uint32(s, strlen(s), &out)); - GPR_ASSERT(out == want); + GPR_ASSERT (gpr_parse_bytes_to_uint32 (s, strlen (s), &out)); + GPR_ASSERT (out == want); } -static void test_parse_uint32(void) { - LOG_TEST_NAME("test_parse_uint32"); - - test_pu32_fail("-1"); - test_pu32_fail("a"); - test_pu32_fail(""); - test_pu32_succeed("0", 0); - test_pu32_succeed("1", 1); - test_pu32_succeed("2", 2); - test_pu32_succeed("3", 3); - test_pu32_succeed("4", 4); - test_pu32_succeed("5", 5); - test_pu32_succeed("6", 6); - test_pu32_succeed("7", 7); - test_pu32_succeed("8", 8); - test_pu32_succeed("9", 9); - test_pu32_succeed("10", 10); - test_pu32_succeed("11", 11); - test_pu32_succeed("12", 12); - test_pu32_succeed("13", 13); - test_pu32_succeed("14", 14); - test_pu32_succeed("15", 15); - test_pu32_succeed("16", 16); - test_pu32_succeed("17", 17); - test_pu32_succeed("18", 18); - test_pu32_succeed("19", 19); - test_pu32_succeed("1234567890", 1234567890); - test_pu32_succeed("4294967295", 4294967295u); - test_pu32_fail("4294967296"); - test_pu32_fail("4294967297"); - test_pu32_fail("4294967298"); - test_pu32_fail("4294967299"); +static void +test_parse_uint32 (void) +{ + LOG_TEST_NAME ("test_parse_uint32"); + + test_pu32_fail ("-1"); + test_pu32_fail ("a"); + test_pu32_fail (""); + test_pu32_succeed ("0", 0); + test_pu32_succeed ("1", 1); + test_pu32_succeed ("2", 2); + test_pu32_succeed ("3", 3); + test_pu32_succeed ("4", 4); + test_pu32_succeed ("5", 5); + test_pu32_succeed ("6", 6); + test_pu32_succeed ("7", 7); + test_pu32_succeed ("8", 8); + test_pu32_succeed ("9", 9); + test_pu32_succeed ("10", 10); + test_pu32_succeed ("11", 11); + test_pu32_succeed ("12", 12); + test_pu32_succeed ("13", 13); + test_pu32_succeed ("14", 14); + test_pu32_succeed ("15", 15); + test_pu32_succeed ("16", 16); + test_pu32_succeed ("17", 17); + test_pu32_succeed ("18", 18); + test_pu32_succeed ("19", 19); + test_pu32_succeed ("1234567890", 1234567890); + test_pu32_succeed ("4294967295", 4294967295u); + test_pu32_fail ("4294967296"); + test_pu32_fail ("4294967297"); + test_pu32_fail ("4294967298"); + test_pu32_fail ("4294967299"); } -static void test_asprintf(void) { +static void +test_asprintf (void) +{ char *buf; int i, j; - LOG_TEST_NAME("test_asprintf"); + LOG_TEST_NAME ("test_asprintf"); /* Print an empty string. */ - GPR_ASSERT(gpr_asprintf(&buf, "") == 0); - GPR_ASSERT(buf[0] == '\0'); - gpr_free(buf); + GPR_ASSERT (gpr_asprintf (&buf, "") == 0); + GPR_ASSERT (buf[0] == '\0'); + gpr_free (buf); /* Print strings of various lengths. */ - for (i = 1; i < 100; i++) { - GPR_ASSERT(gpr_asprintf(&buf, "%0*d", i, 1) == i); - - /* The buffer should resemble "000001\0". */ - for (j = 0; j < i - 2; j++) { - GPR_ASSERT(buf[j] == '0'); + for (i = 1; i < 100; i++) + { + GPR_ASSERT (gpr_asprintf (&buf, "%0*d", i, 1) == i); + + /* The buffer should resemble "000001\0". */ + for (j = 0; j < i - 2; j++) + { + GPR_ASSERT (buf[j] == '0'); + } + GPR_ASSERT (buf[i - 1] == '1'); + GPR_ASSERT (buf[i] == '\0'); + gpr_free (buf); } - GPR_ASSERT(buf[i - 1] == '1'); - GPR_ASSERT(buf[i] == '\0'); - gpr_free(buf); - } } -static void test_strjoin(void) { - const char *parts[4] = {"one", "two", "three", "four"}; +static void +test_strjoin (void) +{ + const char *parts[4] = { "one", "two", "three", "four" }; size_t joined_len; char *joined; - LOG_TEST_NAME("test_strjoin"); + LOG_TEST_NAME ("test_strjoin"); - joined = gpr_strjoin(parts, 4, &joined_len); - GPR_ASSERT(0 == strcmp("onetwothreefour", joined)); - gpr_free(joined); + joined = gpr_strjoin (parts, 4, &joined_len); + GPR_ASSERT (0 == strcmp ("onetwothreefour", joined)); + gpr_free (joined); - joined = gpr_strjoin(parts, 0, &joined_len); - GPR_ASSERT(0 == strcmp("", joined)); - gpr_free(joined); + joined = gpr_strjoin (parts, 0, &joined_len); + GPR_ASSERT (0 == strcmp ("", joined)); + gpr_free (joined); - joined = gpr_strjoin(parts, 1, &joined_len); - GPR_ASSERT(0 == strcmp("one", joined)); - gpr_free(joined); + joined = gpr_strjoin (parts, 1, &joined_len); + GPR_ASSERT (0 == strcmp ("one", joined)); + gpr_free (joined); } -static void test_strjoin_sep(void) { - const char *parts[4] = {"one", "two", "three", "four"}; +static void +test_strjoin_sep (void) +{ + const char *parts[4] = { "one", "two", "three", "four" }; size_t joined_len; char *joined; - LOG_TEST_NAME("test_strjoin_sep"); + LOG_TEST_NAME ("test_strjoin_sep"); - joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len); - GPR_ASSERT(0 == strcmp("one, two, three, four", joined)); - gpr_free(joined); + joined = gpr_strjoin_sep (parts, 4, ", ", &joined_len); + GPR_ASSERT (0 == strcmp ("one, two, three, four", joined)); + gpr_free (joined); /* empty separator */ - joined = gpr_strjoin_sep(parts, 4, "", &joined_len); - GPR_ASSERT(0 == strcmp("onetwothreefour", joined)); - gpr_free(joined); + joined = gpr_strjoin_sep (parts, 4, "", &joined_len); + GPR_ASSERT (0 == strcmp ("onetwothreefour", joined)); + gpr_free (joined); /* degenerated case specifying zero input parts */ - joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len); - GPR_ASSERT(0 == strcmp("", joined)); - gpr_free(joined); + joined = gpr_strjoin_sep (parts, 0, ", ", &joined_len); + GPR_ASSERT (0 == strcmp ("", joined)); + gpr_free (joined); /* single part should have no separator */ - joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len); - GPR_ASSERT(0 == strcmp("one", joined)); - gpr_free(joined); + joined = gpr_strjoin_sep (parts, 1, ", ", &joined_len); + GPR_ASSERT (0 == strcmp ("one", joined)); + gpr_free (joined); } -static void test_strsplit(void) { +static void +test_strsplit (void) +{ gpr_slice_buffer *parts; gpr_slice str; - LOG_TEST_NAME("test_strsplit"); + LOG_TEST_NAME ("test_strsplit"); - parts = gpr_malloc(sizeof(gpr_slice_buffer)); - gpr_slice_buffer_init(parts); + parts = gpr_malloc (sizeof (gpr_slice_buffer)); + gpr_slice_buffer_init (parts); - str = gpr_slice_from_copied_string("one, two, three, four"); - gpr_slice_split(str, ", ", parts); - GPR_ASSERT(4 == parts->count); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one")); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "two")); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[2], "three")); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[3], "four")); - gpr_slice_buffer_reset_and_unref(parts); - gpr_slice_unref(str); + str = gpr_slice_from_copied_string ("one, two, three, four"); + gpr_slice_split (str, ", ", parts); + GPR_ASSERT (4 == parts->count); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "one")); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "two")); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[2], "three")); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[3], "four")); + gpr_slice_buffer_reset_and_unref (parts); + gpr_slice_unref (str); /* separator not present in string */ - str = gpr_slice_from_copied_string("one two three four"); - gpr_slice_split(str, ", ", parts); - GPR_ASSERT(1 == parts->count); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one two three four")); - gpr_slice_buffer_reset_and_unref(parts); - gpr_slice_unref(str); + str = gpr_slice_from_copied_string ("one two three four"); + gpr_slice_split (str, ", ", parts); + GPR_ASSERT (1 == parts->count); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "one two three four")); + gpr_slice_buffer_reset_and_unref (parts); + gpr_slice_unref (str); /* separator at the end */ - str = gpr_slice_from_copied_string("foo,"); - gpr_slice_split(str, ",", parts); - GPR_ASSERT(2 == parts->count); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "foo")); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "")); - gpr_slice_buffer_reset_and_unref(parts); - gpr_slice_unref(str); + str = gpr_slice_from_copied_string ("foo,"); + gpr_slice_split (str, ",", parts); + GPR_ASSERT (2 == parts->count); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "foo")); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "")); + gpr_slice_buffer_reset_and_unref (parts); + gpr_slice_unref (str); /* separator at the beginning */ - str = gpr_slice_from_copied_string(",foo"); - gpr_slice_split(str, ",", parts); - GPR_ASSERT(2 == parts->count); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "")); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "foo")); - gpr_slice_buffer_reset_and_unref(parts); - gpr_slice_unref(str); + str = gpr_slice_from_copied_string (",foo"); + gpr_slice_split (str, ",", parts); + GPR_ASSERT (2 == parts->count); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "")); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "foo")); + gpr_slice_buffer_reset_and_unref (parts); + gpr_slice_unref (str); /* standalone separator */ - str = gpr_slice_from_copied_string(","); - gpr_slice_split(str, ",", parts); - GPR_ASSERT(2 == parts->count); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "")); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "")); - gpr_slice_buffer_reset_and_unref(parts); - gpr_slice_unref(str); + str = gpr_slice_from_copied_string (","); + gpr_slice_split (str, ",", parts); + GPR_ASSERT (2 == parts->count); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "")); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "")); + gpr_slice_buffer_reset_and_unref (parts); + gpr_slice_unref (str); /* empty input */ - str = gpr_slice_from_copied_string(""); - gpr_slice_split(str, ", ", parts); - GPR_ASSERT(1 == parts->count); - GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "")); - gpr_slice_buffer_reset_and_unref(parts); - gpr_slice_unref(str); - - gpr_slice_buffer_destroy(parts); - gpr_free(parts); + str = gpr_slice_from_copied_string (""); + gpr_slice_split (str, ", ", parts); + GPR_ASSERT (1 == parts->count); + GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "")); + gpr_slice_buffer_reset_and_unref (parts); + gpr_slice_unref (str); + + gpr_slice_buffer_destroy (parts); + gpr_free (parts); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_strdup(); - test_dump(); - test_dump_slice(); - test_parse_uint32(); - test_asprintf(); - test_strjoin(); - test_strjoin_sep(); - test_strsplit(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_strdup (); + test_dump (); + test_dump_slice (); + test_parse_uint32 (); + test_asprintf (); + test_strjoin (); + test_strjoin_sep (); + test_strsplit (); return 0; } diff --git a/test/core/support/sync_test.c b/test/core/support/sync_test.c index f729eb0b92..72788a75ba 100644 --- a/test/core/support/sync_test.c +++ b/test/core/support/sync_test.c @@ -49,37 +49,44 @@ #define N 4 -typedef struct queue { - gpr_cv non_empty; /* Signalled when length becomes non-zero. */ - gpr_cv non_full; /* Signalled when length becomes non-N. */ - gpr_mu mu; /* Protects all fields below. - (That is, except during initialization or - destruction, the fields below should be accessed - only by a thread that holds mu.) */ - int head; /* Index of head of queue 0..N-1. */ - int length; /* Number of valid elements in queue 0..N. */ - int elem[N]; /* elem[head .. head+length-1] are queue elements. */ +typedef struct queue +{ + gpr_cv non_empty; /* Signalled when length becomes non-zero. */ + gpr_cv non_full; /* Signalled when length becomes non-N. */ + gpr_mu mu; /* Protects all fields below. + (That is, except during initialization or + destruction, the fields below should be accessed + only by a thread that holds mu.) */ + int head; /* Index of head of queue 0..N-1. */ + int length; /* Number of valid elements in queue 0..N. */ + int elem[N]; /* elem[head .. head+length-1] are queue elements. */ } queue; /* Initialize *q. */ -void queue_init(queue *q) { - gpr_mu_init(&q->mu); - gpr_cv_init(&q->non_empty); - gpr_cv_init(&q->non_full); +void +queue_init (queue * q) +{ + gpr_mu_init (&q->mu); + gpr_cv_init (&q->non_empty); + gpr_cv_init (&q->non_full); q->head = 0; q->length = 0; } /* Free storage associated with *q. */ -void queue_destroy(queue *q) { - gpr_mu_destroy(&q->mu); - gpr_cv_destroy(&q->non_empty); - gpr_cv_destroy(&q->non_full); +void +queue_destroy (queue * q) +{ + gpr_mu_destroy (&q->mu); + gpr_cv_destroy (&q->non_empty); + gpr_cv_destroy (&q->non_full); } /* Wait until there is room in *q, then append x to *q. */ -void queue_append(queue *q, int x) { - gpr_mu_lock(&q->mu); +void +queue_append (queue * q, int x) +{ + gpr_mu_lock (&q->mu); /* To wait for a predicate without a deadline, loop on the negation of the predicate, and use gpr_cv_wait(..., gpr_inf_future(GPR_CLOCK_REALTIME)) inside the loop @@ -87,78 +94,91 @@ void queue_append(queue *q, int x) { makes the condition true should use gpr_cv_broadcast() on the corresponding condition variable. The predicate must be on state protected by the lock. */ - while (q->length == N) { - gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - if (q->length == 0) { /* Wake threads blocked in queue_remove(). */ - /* It's normal to use gpr_cv_broadcast() or gpr_signal() while - holding the lock. */ - gpr_cv_broadcast(&q->non_empty); - } + while (q->length == N) + { + gpr_cv_wait (&q->non_full, &q->mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + if (q->length == 0) + { /* Wake threads blocked in queue_remove(). */ + /* It's normal to use gpr_cv_broadcast() or gpr_signal() while + holding the lock. */ + gpr_cv_broadcast (&q->non_empty); + } q->elem[(q->head + q->length) % N] = x; q->length++; - gpr_mu_unlock(&q->mu); + gpr_mu_unlock (&q->mu); } /* If it can be done without blocking, append x to *q and return non-zero. Otherwise return 0. */ -int queue_try_append(queue *q, int x) { +int +queue_try_append (queue * q, int x) +{ int result = 0; - if (gpr_mu_trylock(&q->mu)) { - if (q->length != N) { - if (q->length == 0) { /* Wake threads blocked in queue_remove(). */ - gpr_cv_broadcast(&q->non_empty); - } - q->elem[(q->head + q->length) % N] = x; - q->length++; - result = 1; + if (gpr_mu_trylock (&q->mu)) + { + if (q->length != N) + { + if (q->length == 0) + { /* Wake threads blocked in queue_remove(). */ + gpr_cv_broadcast (&q->non_empty); + } + q->elem[(q->head + q->length) % N] = x; + q->length++; + result = 1; + } + gpr_mu_unlock (&q->mu); } - gpr_mu_unlock(&q->mu); - } return result; } /* Wait until the *q is non-empty or deadline abs_deadline passes. If the queue is non-empty, remove its head entry, place it in *head, and return non-zero. Otherwise return 0. */ -int queue_remove(queue *q, int *head, gpr_timespec abs_deadline) { +int +queue_remove (queue * q, int *head, gpr_timespec abs_deadline) +{ int result = 0; - gpr_mu_lock(&q->mu); + gpr_mu_lock (&q->mu); /* To wait for a predicate with a deadline, loop on the negation of the predicate or until gpr_cv_wait() returns true. Code that makes the condition true should use gpr_cv_broadcast() on the corresponding condition variable. The predicate must be on state protected by the lock. */ - while (q->length == 0 && !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) { - } - if (q->length != 0) { /* Queue is non-empty. */ - result = 1; - if (q->length == N) { /* Wake threads blocked in queue_append(). */ - gpr_cv_broadcast(&q->non_full); + while (q->length == 0 && !gpr_cv_wait (&q->non_empty, &q->mu, abs_deadline)) + { } - *head = q->elem[q->head]; - q->head = (q->head + 1) % N; - q->length--; - } /* else deadline exceeded */ - gpr_mu_unlock(&q->mu); + if (q->length != 0) + { /* Queue is non-empty. */ + result = 1; + if (q->length == N) + { /* Wake threads blocked in queue_append(). */ + gpr_cv_broadcast (&q->non_full); + } + *head = q->elem[q->head]; + q->head = (q->head + 1) % N; + q->length--; + } /* else deadline exceeded */ + gpr_mu_unlock (&q->mu); return result; } /* ------------------------------------------------- */ /* Tests for gpr_mu and gpr_cv, and the queue example. */ -struct test { - int threads; /* number of threads */ +struct test +{ + int threads; /* number of threads */ - gpr_int64 iterations; /* number of iterations per thread */ + gpr_int64 iterations; /* number of iterations per thread */ gpr_int64 counter; - int thread_count; /* used to allocate thread ids */ - int done; /* threads not yet completed */ + int thread_count; /* used to allocate thread ids */ + int done; /* threads not yet completed */ - gpr_mu mu; /* protects iterations, counter, thread_count, done */ + gpr_mu mu; /* protects iterations, counter, thread_count, done */ - gpr_cv cv; /* signalling depends on test */ + gpr_cv cv; /* signalling depends on test */ - gpr_cv done_cv; /* signalled when done == 0 */ + gpr_cv done_cv; /* signalled when done == 0 */ queue q; @@ -170,287 +190,338 @@ struct test { }; /* Return pointer to a new struct test. */ -static struct test *test_new(int threads, gpr_int64 iterations) { - struct test *m = gpr_malloc(sizeof(*m)); +static struct test * +test_new (int threads, gpr_int64 iterations) +{ + struct test *m = gpr_malloc (sizeof (*m)); m->threads = threads; m->iterations = iterations; m->counter = 0; m->thread_count = 0; m->done = threads; - gpr_mu_init(&m->mu); - gpr_cv_init(&m->cv); - gpr_cv_init(&m->done_cv); - queue_init(&m->q); - gpr_stats_init(&m->stats_counter, 0); - gpr_ref_init(&m->refcount, 0); - gpr_ref_init(&m->thread_refcount, threads); - gpr_event_init(&m->event); + gpr_mu_init (&m->mu); + gpr_cv_init (&m->cv); + gpr_cv_init (&m->done_cv); + queue_init (&m->q); + gpr_stats_init (&m->stats_counter, 0); + gpr_ref_init (&m->refcount, 0); + gpr_ref_init (&m->thread_refcount, threads); + gpr_event_init (&m->event); return m; } /* Return pointer to a new struct test. */ -static void test_destroy(struct test *m) { - gpr_mu_destroy(&m->mu); - gpr_cv_destroy(&m->cv); - gpr_cv_destroy(&m->done_cv); - queue_destroy(&m->q); - gpr_free(m); +static void +test_destroy (struct test *m) +{ + gpr_mu_destroy (&m->mu); + gpr_cv_destroy (&m->cv); + gpr_cv_destroy (&m->done_cv); + queue_destroy (&m->q); + gpr_free (m); } /* Create m->threads threads, each running (*body)(m) */ -static void test_create_threads(struct test *m, void (*body)(void *arg)) { +static void +test_create_threads (struct test *m, void (*body) (void *arg)) +{ gpr_thd_id id; int i; - for (i = 0; i != m->threads; i++) { - GPR_ASSERT(gpr_thd_new(&id, body, m, NULL)); - } + for (i = 0; i != m->threads; i++) + { + GPR_ASSERT (gpr_thd_new (&id, body, m, NULL)); + } } /* Wait until all threads report done. */ -static void test_wait(struct test *m) { - gpr_mu_lock(&m->mu); - while (m->done != 0) { - gpr_cv_wait(&m->done_cv, &m->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - gpr_mu_unlock(&m->mu); +static void +test_wait (struct test *m) +{ + gpr_mu_lock (&m->mu); + while (m->done != 0) + { + gpr_cv_wait (&m->done_cv, &m->mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + gpr_mu_unlock (&m->mu); } /* Get an integer thread id in the raneg 0..threads-1 */ -static int thread_id(struct test *m) { +static int +thread_id (struct test *m) +{ int id; - gpr_mu_lock(&m->mu); + gpr_mu_lock (&m->mu); id = m->thread_count++; - gpr_mu_unlock(&m->mu); + gpr_mu_unlock (&m->mu); return id; } /* Indicate that a thread is done, by decrementing m->done and signalling done_cv if m->done==0. */ -static void mark_thread_done(struct test *m) { - gpr_mu_lock(&m->mu); - GPR_ASSERT(m->done != 0); +static void +mark_thread_done (struct test *m) +{ + gpr_mu_lock (&m->mu); + GPR_ASSERT (m->done != 0); m->done--; - if (m->done == 0) { - gpr_cv_signal(&m->done_cv); - } - gpr_mu_unlock(&m->mu); + if (m->done == 0) + { + gpr_cv_signal (&m->done_cv); + } + gpr_mu_unlock (&m->mu); } /* Test several threads running (*body)(struct test *m) for increasing settings of m->iterations, until about timeout_s to 2*timeout_s seconds have elapsed. If extra!=NULL, run (*extra)(m) in an additional thread. */ -static void test(const char *name, void (*body)(void *m), - void (*extra)(void *m), int timeout_s) { +static void +test (const char *name, void (*body) (void *m), void (*extra) (void *m), int timeout_s) +{ gpr_int64 iterations = 1024; struct test *m; - gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME); + gpr_timespec start = gpr_now (GPR_CLOCK_REALTIME); gpr_timespec time_taken; - gpr_timespec deadline = gpr_time_add( - start, gpr_time_from_micros(timeout_s * 1000000, GPR_TIMESPAN)); - fprintf(stderr, "%s:", name); - while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0) { - iterations <<= 1; - fprintf(stderr, " %ld", (long)iterations); - m = test_new(10, iterations); - if (extra != NULL) { - gpr_thd_id id; - GPR_ASSERT(gpr_thd_new(&id, extra, m, NULL)); - m->done++; /* one more thread to wait for */ - } - test_create_threads(m, body); - test_wait(m); - if (m->counter != m->threads * m->iterations) { - fprintf(stderr, "counter %ld threads %d iterations %ld\n", - (long)m->counter, m->threads, (long)m->iterations); - GPR_ASSERT(0); + gpr_timespec deadline = gpr_time_add (start, gpr_time_from_micros (timeout_s * 1000000, GPR_TIMESPAN)); + fprintf (stderr, "%s:", name); + while (gpr_time_cmp (gpr_now (GPR_CLOCK_REALTIME), deadline) < 0) + { + iterations <<= 1; + fprintf (stderr, " %ld", (long) iterations); + m = test_new (10, iterations); + if (extra != NULL) + { + gpr_thd_id id; + GPR_ASSERT (gpr_thd_new (&id, extra, m, NULL)); + m->done++; /* one more thread to wait for */ + } + test_create_threads (m, body); + test_wait (m); + if (m->counter != m->threads * m->iterations) + { + fprintf (stderr, "counter %ld threads %d iterations %ld\n", (long) m->counter, m->threads, (long) m->iterations); + GPR_ASSERT (0); + } + test_destroy (m); } - test_destroy(m); - } - time_taken = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start); - fprintf(stderr, " done %ld.%09d s\n", (long)time_taken.tv_sec, - (int)time_taken.tv_nsec); + time_taken = gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), start); + fprintf (stderr, " done %ld.%09d s\n", (long) time_taken.tv_sec, (int) time_taken.tv_nsec); } /* Increment m->counter on each iteration; then mark thread as done. */ -static void inc(void *v /*=m*/) { +static void +inc (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) { - gpr_mu_lock(&m->mu); - m->counter++; - gpr_mu_unlock(&m->mu); - } - mark_thread_done(m); + for (i = 0; i != m->iterations; i++) + { + gpr_mu_lock (&m->mu); + m->counter++; + gpr_mu_unlock (&m->mu); + } + mark_thread_done (m); } /* Increment m->counter under lock acquired with trylock, m->iterations times; then mark thread as done. */ -static void inctry(void *v /*=m*/) { +static void +inctry (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations;) { - if (gpr_mu_trylock(&m->mu)) { - m->counter++; - gpr_mu_unlock(&m->mu); - i++; + for (i = 0; i != m->iterations;) + { + if (gpr_mu_trylock (&m->mu)) + { + m->counter++; + gpr_mu_unlock (&m->mu); + i++; + } } - } - mark_thread_done(m); + mark_thread_done (m); } /* Increment counter only when (m->counter%m->threads)==m->thread_id; then mark thread as done. */ -static void inc_by_turns(void *v /*=m*/) { +static void +inc_by_turns (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - int id = thread_id(m); - for (i = 0; i != m->iterations; i++) { - gpr_mu_lock(&m->mu); - while ((m->counter % m->threads) != id) { - gpr_cv_wait(&m->cv, &m->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); + int id = thread_id (m); + for (i = 0; i != m->iterations; i++) + { + gpr_mu_lock (&m->mu); + while ((m->counter % m->threads) != id) + { + gpr_cv_wait (&m->cv, &m->mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + m->counter++; + gpr_cv_broadcast (&m->cv); + gpr_mu_unlock (&m->mu); } - m->counter++; - gpr_cv_broadcast(&m->cv); - gpr_mu_unlock(&m->mu); - } - mark_thread_done(m); + mark_thread_done (m); } /* Wait a millisecond and increment counter on each iteration; then mark thread as done. */ -static void inc_with_1ms_delay(void *v /*=m*/) { +static void +inc_with_1ms_delay (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) { - gpr_timespec deadline; - gpr_mu_lock(&m->mu); - deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_micros(1000, GPR_TIMESPAN)); - while (!gpr_cv_wait(&m->cv, &m->mu, deadline)) { + for (i = 0; i != m->iterations; i++) + { + gpr_timespec deadline; + gpr_mu_lock (&m->mu); + deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000, GPR_TIMESPAN)); + while (!gpr_cv_wait (&m->cv, &m->mu, deadline)) + { + } + m->counter++; + gpr_mu_unlock (&m->mu); } - m->counter++; - gpr_mu_unlock(&m->mu); - } - mark_thread_done(m); + mark_thread_done (m); } /* Wait a millisecond and increment counter on each iteration, using an event for timing; then mark thread as done. */ -static void inc_with_1ms_delay_event(void *v /*=m*/) { +static void +inc_with_1ms_delay_event (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) { - gpr_timespec deadline; - deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_micros(1000, GPR_TIMESPAN)); - GPR_ASSERT(gpr_event_wait(&m->event, deadline) == NULL); - gpr_mu_lock(&m->mu); - m->counter++; - gpr_mu_unlock(&m->mu); - } - mark_thread_done(m); + for (i = 0; i != m->iterations; i++) + { + gpr_timespec deadline; + deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000, GPR_TIMESPAN)); + GPR_ASSERT (gpr_event_wait (&m->event, deadline) == NULL); + gpr_mu_lock (&m->mu); + m->counter++; + gpr_mu_unlock (&m->mu); + } + mark_thread_done (m); } /* Produce m->iterations elements on queue m->q, then mark thread as done. Even threads use queue_append(), and odd threads use queue_try_append() until it succeeds. */ -static void many_producers(void *v /*=m*/) { +static void +many_producers (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - int x = thread_id(m); - if ((x & 1) == 0) { - for (i = 0; i != m->iterations; i++) { - queue_append(&m->q, 1); + int x = thread_id (m); + if ((x & 1) == 0) + { + for (i = 0; i != m->iterations; i++) + { + queue_append (&m->q, 1); + } } - } else { - for (i = 0; i != m->iterations; i++) { - while (!queue_try_append(&m->q, 1)) { - } + else + { + for (i = 0; i != m->iterations; i++) + { + while (!queue_try_append (&m->q, 1)) + { + } + } } - } - mark_thread_done(m); + mark_thread_done (m); } /* Consume elements from m->q until m->threads*m->iterations are seen, wait an extra second to confirm that no more elements are arriving, then mark thread as done. */ -static void consumer(void *v /*=m*/) { +static void +consumer (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 n = m->iterations * m->threads; gpr_int64 i; int value; - for (i = 0; i != n; i++) { - queue_remove(&m->q, &value, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - gpr_mu_lock(&m->mu); + for (i = 0; i != n; i++) + { + queue_remove (&m->q, &value, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + gpr_mu_lock (&m->mu); m->counter = n; - gpr_mu_unlock(&m->mu); - GPR_ASSERT( - !queue_remove(&m->q, &value, - gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_micros(1000000, GPR_TIMESPAN)))); - mark_thread_done(m); + gpr_mu_unlock (&m->mu); + GPR_ASSERT (!queue_remove (&m->q, &value, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000000, GPR_TIMESPAN)))); + mark_thread_done (m); } /* Increment m->stats_counter m->iterations times, transfer counter value to m->counter, then mark thread as done. */ -static void statsinc(void *v /*=m*/) { +static void +statsinc (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) { - gpr_stats_inc(&m->stats_counter, 1); - } - gpr_mu_lock(&m->mu); - m->counter = gpr_stats_read(&m->stats_counter); - gpr_mu_unlock(&m->mu); - mark_thread_done(m); + for (i = 0; i != m->iterations; i++) + { + gpr_stats_inc (&m->stats_counter, 1); + } + gpr_mu_lock (&m->mu); + m->counter = gpr_stats_read (&m->stats_counter); + gpr_mu_unlock (&m->mu); + mark_thread_done (m); } /* Increment m->refcount m->iterations times, decrement m->thread_refcount once, and if it reaches zero, set m->event to (void*)1; then mark thread as done. */ -static void refinc(void *v /*=m*/) { +static void +refinc (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) { - gpr_ref(&m->refcount); - } - if (gpr_unref(&m->thread_refcount)) { - gpr_event_set(&m->event, (void *)1); - } - mark_thread_done(m); + for (i = 0; i != m->iterations; i++) + { + gpr_ref (&m->refcount); + } + if (gpr_unref (&m->thread_refcount)) + { + gpr_event_set (&m->event, (void *) 1); + } + mark_thread_done (m); } /* Wait until m->event is set to (void *)1, then decrement m->refcount m->stats_counter m->iterations times, and ensure that the last decrement caused the counter to reach zero, then mark thread as done. */ -static void refcheck(void *v /*=m*/) { +static void +refcheck (void *v /*=m*/ ) +{ struct test *m = v; gpr_int64 n = m->iterations * m->threads; gpr_int64 i; - GPR_ASSERT(gpr_event_wait(&m->event, gpr_inf_future(GPR_CLOCK_REALTIME)) == - (void *)1); - GPR_ASSERT(gpr_event_get(&m->event) == (void *)1); - for (i = 1; i != n; i++) { - GPR_ASSERT(!gpr_unref(&m->refcount)); - m->counter++; - } - GPR_ASSERT(gpr_unref(&m->refcount)); + GPR_ASSERT (gpr_event_wait (&m->event, gpr_inf_future (GPR_CLOCK_REALTIME)) == (void *) 1); + GPR_ASSERT (gpr_event_get (&m->event) == (void *) 1); + for (i = 1; i != n; i++) + { + GPR_ASSERT (!gpr_unref (&m->refcount)); + m->counter++; + } + GPR_ASSERT (gpr_unref (&m->refcount)); m->counter++; - mark_thread_done(m); + mark_thread_done (m); } /* ------------------------------------------------- */ -int main(int argc, char *argv[]) { - grpc_test_init(argc, argv); - test("mutex", &inc, NULL, 1); - test("mutex try", &inctry, NULL, 1); - test("cv", &inc_by_turns, NULL, 1); - test("timedcv", &inc_with_1ms_delay, NULL, 1); - test("queue", &many_producers, &consumer, 10); - test("stats_counter", &statsinc, NULL, 1); - test("refcount", &refinc, &refcheck, 1); - test("timedevent", &inc_with_1ms_delay_event, NULL, 1); +int +main (int argc, char *argv[]) +{ + grpc_test_init (argc, argv); + test ("mutex", &inc, NULL, 1); + test ("mutex try", &inctry, NULL, 1); + test ("cv", &inc_by_turns, NULL, 1); + test ("timedcv", &inc_with_1ms_delay, NULL, 1); + test ("queue", &many_producers, &consumer, 10); + test ("stats_counter", &statsinc, NULL, 1); + test ("refcount", &refinc, &refcheck, 1); + test ("timedevent", &inc_with_1ms_delay_event, NULL, 1); return 0; } diff --git a/test/core/support/thd_test.c b/test/core/support/thd_test.c index faba33c5e8..09303c1958 100644 --- a/test/core/support/thd_test.c +++ b/test/core/support/thd_test.c @@ -41,7 +41,8 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -struct test { +struct test +{ gpr_mu mu; int n; int is_done; @@ -49,53 +50,67 @@ struct test { }; /* A Thread body. Decrement t->n, and if is becomes zero, set t->done. */ -static void thd_body(void *v) { +static void +thd_body (void *v) +{ struct test *t = v; - gpr_mu_lock(&t->mu); + gpr_mu_lock (&t->mu); t->n--; - if (t->n == 0) { - t->is_done = 1; - gpr_cv_signal(&t->done_cv); - } - gpr_mu_unlock(&t->mu); + if (t->n == 0) + { + t->is_done = 1; + gpr_cv_signal (&t->done_cv); + } + gpr_mu_unlock (&t->mu); } -static void thd_body_joinable(void *v) {} +static void +thd_body_joinable (void *v) +{ +} /* Test that we can create a number of threads and wait for them. */ -static void test(void) { +static void +test (void) +{ int i; gpr_thd_id thd; gpr_thd_id thds[1000]; struct test t; int n = 1000; - gpr_thd_options options = gpr_thd_options_default(); - gpr_mu_init(&t.mu); - gpr_cv_init(&t.done_cv); + gpr_thd_options options = gpr_thd_options_default (); + gpr_mu_init (&t.mu); + gpr_cv_init (&t.done_cv); t.n = n; t.is_done = 0; - for (i = 0; i != n; i++) { - GPR_ASSERT(gpr_thd_new(&thd, &thd_body, &t, NULL)); - } - gpr_mu_lock(&t.mu); - while (!t.is_done) { - gpr_cv_wait(&t.done_cv, &t.mu, gpr_inf_future(GPR_CLOCK_REALTIME)); - } - gpr_mu_unlock(&t.mu); - GPR_ASSERT(t.n == 0); - gpr_thd_options_set_joinable(&options); - for (i = 0; i < n; i++) { - GPR_ASSERT(gpr_thd_new(&thds[i], &thd_body_joinable, NULL, &options)); - } - for (i = 0; i < n; i++) { - gpr_thd_join(thds[i]); - } + for (i = 0; i != n; i++) + { + GPR_ASSERT (gpr_thd_new (&thd, &thd_body, &t, NULL)); + } + gpr_mu_lock (&t.mu); + while (!t.is_done) + { + gpr_cv_wait (&t.done_cv, &t.mu, gpr_inf_future (GPR_CLOCK_REALTIME)); + } + gpr_mu_unlock (&t.mu); + GPR_ASSERT (t.n == 0); + gpr_thd_options_set_joinable (&options); + for (i = 0; i < n; i++) + { + GPR_ASSERT (gpr_thd_new (&thds[i], &thd_body_joinable, NULL, &options)); + } + for (i = 0; i < n; i++) + { + gpr_thd_join (thds[i]); + } } /* ------------------------------------------------- */ -int main(int argc, char *argv[]) { - grpc_test_init(argc, argv); - test(); +int +main (int argc, char *argv[]) +{ + grpc_test_init (argc, argv); + test (); return 0; } diff --git a/test/core/support/time_test.c b/test/core/support/time_test.c index ce35edd83c..df65b45ddd 100644 --- a/test/core/support/time_test.c +++ b/test/core/support/time_test.c @@ -43,225 +43,236 @@ #include <grpc/support/time.h> #include "test/core/util/test_config.h" -static void to_fp(void *arg, const char *buf, size_t len) { - fwrite(buf, 1, len, (FILE *)arg); +static void +to_fp (void *arg, const char *buf, size_t len) +{ + fwrite (buf, 1, len, (FILE *) arg); } /* Convert gpr_uintmax x to ascii base b (2..16), and write with (*writer)(arg, ...), zero padding to "chars" digits). */ -static void u_to_s(gpr_uintmax x, unsigned base, int chars, - void (*writer)(void *arg, const char *buf, size_t len), - void *arg) { +static void +u_to_s (gpr_uintmax x, unsigned base, int chars, void (*writer) (void *arg, const char *buf, size_t len), void *arg) +{ char buf[64]; - char *p = buf + sizeof(buf); - do { - *--p = "0123456789abcdef"[x % base]; - x /= base; - chars--; - } while (x != 0 || chars > 0); - (*writer)(arg, p, (size_t)(buf + sizeof(buf) - p)); + char *p = buf + sizeof (buf); + do + { + *--p = "0123456789abcdef"[x % base]; + x /= base; + chars--; + } + while (x != 0 || chars > 0); + (*writer) (arg, p, (size_t) (buf + sizeof (buf) - p)); } /* Convert gpr_intmax x to ascii base b (2..16), and write with (*writer)(arg, ...), zero padding to "chars" digits). */ -static void i_to_s(gpr_intmax x, unsigned base, int chars, - void (*writer)(void *arg, const char *buf, size_t len), - void *arg) { - if (x < 0) { - (*writer)(arg, "-", 1); - u_to_s((gpr_uintmax)-x, base, chars - 1, writer, arg); - } else { - u_to_s((gpr_uintmax)x, base, chars, writer, arg); - } +static void +i_to_s (gpr_intmax x, unsigned base, int chars, void (*writer) (void *arg, const char *buf, size_t len), void *arg) +{ + if (x < 0) + { + (*writer) (arg, "-", 1); + u_to_s ((gpr_uintmax) - x, base, chars - 1, writer, arg); + } + else + { + u_to_s ((gpr_uintmax) x, base, chars, writer, arg); + } } /* Convert ts to ascii, and write with (*writer)(arg, ...). */ -static void ts_to_s(gpr_timespec t, - void (*writer)(void *arg, const char *buf, size_t len), - void *arg) { - if (t.tv_sec < 0 && t.tv_nsec != 0) { - t.tv_sec++; - t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec; - } - i_to_s(t.tv_sec, 10, 0, writer, arg); - (*writer)(arg, ".", 1); - i_to_s(t.tv_nsec, 10, 9, writer, arg); +static void +ts_to_s (gpr_timespec t, void (*writer) (void *arg, const char *buf, size_t len), void *arg) +{ + if (t.tv_sec < 0 && t.tv_nsec != 0) + { + t.tv_sec++; + t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec; + } + i_to_s (t.tv_sec, 10, 0, writer, arg); + (*writer) (arg, ".", 1); + i_to_s (t.tv_nsec, 10, 9, writer, arg); } -static void test_values(void) { +static void +test_values (void) +{ int i; - gpr_timespec x = gpr_time_0(GPR_CLOCK_REALTIME); - GPR_ASSERT(x.tv_sec == 0 && x.tv_nsec == 0); + gpr_timespec x = gpr_time_0 (GPR_CLOCK_REALTIME); + GPR_ASSERT (x.tv_sec == 0 && x.tv_nsec == 0); - x = gpr_inf_future(GPR_CLOCK_REALTIME); - fprintf(stderr, "far future "); - i_to_s(x.tv_sec, 16, 16, &to_fp, stderr); - fprintf(stderr, "\n"); - GPR_ASSERT(x.tv_sec >= INT_MAX); - fprintf(stderr, "far future "); - ts_to_s(x, &to_fp, stderr); - fprintf(stderr, "\n"); + x = gpr_inf_future (GPR_CLOCK_REALTIME); + fprintf (stderr, "far future "); + i_to_s (x.tv_sec, 16, 16, &to_fp, stderr); + fprintf (stderr, "\n"); + GPR_ASSERT (x.tv_sec >= INT_MAX); + fprintf (stderr, "far future "); + ts_to_s (x, &to_fp, stderr); + fprintf (stderr, "\n"); - x = gpr_inf_past(GPR_CLOCK_REALTIME); - fprintf(stderr, "far past "); - i_to_s(x.tv_sec, 16, 16, &to_fp, stderr); - fprintf(stderr, "\n"); - GPR_ASSERT(x.tv_sec <= INT_MIN); - fprintf(stderr, "far past "); - ts_to_s(x, &to_fp, stderr); - fprintf(stderr, "\n"); + x = gpr_inf_past (GPR_CLOCK_REALTIME); + fprintf (stderr, "far past "); + i_to_s (x.tv_sec, 16, 16, &to_fp, stderr); + fprintf (stderr, "\n"); + GPR_ASSERT (x.tv_sec <= INT_MIN); + fprintf (stderr, "far past "); + ts_to_s (x, &to_fp, stderr); + fprintf (stderr, "\n"); - for (i = 1; i != 1000 * 1000 * 1000; i *= 10) { - x = gpr_time_from_micros(i, GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec == i / GPR_US_PER_SEC && - x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US); - x = gpr_time_from_nanos(i, GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec == i / GPR_NS_PER_SEC && - x.tv_nsec == (i % GPR_NS_PER_SEC)); - x = gpr_time_from_millis(i, GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec == i / GPR_MS_PER_SEC && - x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS); - } + for (i = 1; i != 1000 * 1000 * 1000; i *= 10) + { + x = gpr_time_from_micros (i, GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec == i / GPR_US_PER_SEC && x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US); + x = gpr_time_from_nanos (i, GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec == i / GPR_NS_PER_SEC && x.tv_nsec == (i % GPR_NS_PER_SEC)); + x = gpr_time_from_millis (i, GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec == i / GPR_MS_PER_SEC && x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS); + } /* Test possible overflow in conversion of -ve values. */ - x = gpr_time_from_micros(-(LONG_MAX - 999997), GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec < 0); - GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); + x = gpr_time_from_micros (-(LONG_MAX - 999997), GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec < 0); + GPR_ASSERT (x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); - x = gpr_time_from_nanos(-(LONG_MAX - 999999997), GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec < 0); - GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); + x = gpr_time_from_nanos (-(LONG_MAX - 999999997), GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec < 0); + GPR_ASSERT (x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); - x = gpr_time_from_millis(-(LONG_MAX - 997), GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec < 0); - GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); + x = gpr_time_from_millis (-(LONG_MAX - 997), GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec < 0); + GPR_ASSERT (x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); /* Test general -ve values. */ - for (i = -1; i > -1000 * 1000 * 1000; i *= 7) { - x = gpr_time_from_micros(i, GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i); - x = gpr_time_from_nanos(i, GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i); - x = gpr_time_from_millis(i, GPR_TIMESPAN); - GPR_ASSERT(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i); - } + for (i = -1; i > -1000 * 1000 * 1000; i *= 7) + { + x = gpr_time_from_micros (i, GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i); + x = gpr_time_from_nanos (i, GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i); + x = gpr_time_from_millis (i, GPR_TIMESPAN); + GPR_ASSERT (x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i); + } } -static void test_add_sub(void) { +static void +test_add_sub (void) +{ int i; int j; int k; /* Basic addition and subtraction. */ - for (i = -100; i <= 100; i++) { - for (j = -100; j <= 100; j++) { - for (k = 1; k <= 10000000; k *= 10) { - int sum = i + j; - int diff = i - j; - gpr_timespec it = gpr_time_from_micros(i * k, GPR_TIMESPAN); - gpr_timespec jt = gpr_time_from_micros(j * k, GPR_TIMESPAN); - gpr_timespec sumt = gpr_time_add(it, jt); - gpr_timespec difft = gpr_time_sub(it, jt); - if (gpr_time_cmp(gpr_time_from_micros(sum * k, GPR_TIMESPAN), sumt) != - 0) { - fprintf(stderr, "i %d j %d sum %d sumt ", i, j, sum); - ts_to_s(sumt, &to_fp, stderr); - fprintf(stderr, "\n"); - GPR_ASSERT(0); - } - if (gpr_time_cmp(gpr_time_from_micros(diff * k, GPR_TIMESPAN), difft) != - 0) { - fprintf(stderr, "i %d j %d diff %d diff ", i, j, diff); - ts_to_s(sumt, &to_fp, stderr); - fprintf(stderr, "\n"); - GPR_ASSERT(0); - } - } + for (i = -100; i <= 100; i++) + { + for (j = -100; j <= 100; j++) + { + for (k = 1; k <= 10000000; k *= 10) + { + int sum = i + j; + int diff = i - j; + gpr_timespec it = gpr_time_from_micros (i * k, GPR_TIMESPAN); + gpr_timespec jt = gpr_time_from_micros (j * k, GPR_TIMESPAN); + gpr_timespec sumt = gpr_time_add (it, jt); + gpr_timespec difft = gpr_time_sub (it, jt); + if (gpr_time_cmp (gpr_time_from_micros (sum * k, GPR_TIMESPAN), sumt) != 0) + { + fprintf (stderr, "i %d j %d sum %d sumt ", i, j, sum); + ts_to_s (sumt, &to_fp, stderr); + fprintf (stderr, "\n"); + GPR_ASSERT (0); + } + if (gpr_time_cmp (gpr_time_from_micros (diff * k, GPR_TIMESPAN), difft) != 0) + { + fprintf (stderr, "i %d j %d diff %d diff ", i, j, diff); + ts_to_s (sumt, &to_fp, stderr); + fprintf (stderr, "\n"); + GPR_ASSERT (0); + } + } + } } - } } -static void test_overflow(void) { +static void +test_overflow (void) +{ /* overflow */ - gpr_timespec x = gpr_time_from_micros(1, GPR_TIMESPAN); - do { - x = gpr_time_add(x, x); - } while (gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) < 0); - GPR_ASSERT(gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) == 0); - x = gpr_time_from_micros(-1, GPR_TIMESPAN); - do { - x = gpr_time_add(x, x); - } while (gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) > 0); - GPR_ASSERT(gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) == 0); + gpr_timespec x = gpr_time_from_micros (1, GPR_TIMESPAN); + do + { + x = gpr_time_add (x, x); + } + while (gpr_time_cmp (x, gpr_inf_future (GPR_TIMESPAN)) < 0); + GPR_ASSERT (gpr_time_cmp (x, gpr_inf_future (GPR_TIMESPAN)) == 0); + x = gpr_time_from_micros (-1, GPR_TIMESPAN); + do + { + x = gpr_time_add (x, x); + } + while (gpr_time_cmp (x, gpr_inf_past (GPR_TIMESPAN)) > 0); + GPR_ASSERT (gpr_time_cmp (x, gpr_inf_past (GPR_TIMESPAN)) == 0); } -static void test_sticky_infinities(void) { +static void +test_sticky_infinities (void) +{ int i; int j; int k; gpr_timespec infinity[2]; gpr_timespec addend[3]; - infinity[0] = gpr_inf_future(GPR_TIMESPAN); - infinity[1] = gpr_inf_past(GPR_TIMESPAN); - addend[0] = gpr_inf_future(GPR_TIMESPAN); - addend[1] = gpr_inf_past(GPR_TIMESPAN); - addend[2] = gpr_time_0(GPR_TIMESPAN); + infinity[0] = gpr_inf_future (GPR_TIMESPAN); + infinity[1] = gpr_inf_past (GPR_TIMESPAN); + addend[0] = gpr_inf_future (GPR_TIMESPAN); + addend[1] = gpr_inf_past (GPR_TIMESPAN); + addend[2] = gpr_time_0 (GPR_TIMESPAN); /* Infinities are sticky */ - for (i = 0; i != sizeof(infinity) / sizeof(infinity[0]); i++) { - for (j = 0; j != sizeof(addend) / sizeof(addend[0]); j++) { - gpr_timespec x = gpr_time_add(infinity[i], addend[j]); - GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); - x = gpr_time_sub(infinity[i], addend[j]); - GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); - } - for (k = -200; k <= 200; k++) { - gpr_timespec y = gpr_time_from_micros(k * 100000, GPR_TIMESPAN); - gpr_timespec x = gpr_time_add(infinity[i], y); - GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); - x = gpr_time_sub(infinity[i], y); - GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); + for (i = 0; i != sizeof (infinity) / sizeof (infinity[0]); i++) + { + for (j = 0; j != sizeof (addend) / sizeof (addend[0]); j++) + { + gpr_timespec x = gpr_time_add (infinity[i], addend[j]); + GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); + x = gpr_time_sub (infinity[i], addend[j]); + GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); + } + for (k = -200; k <= 200; k++) + { + gpr_timespec y = gpr_time_from_micros (k * 100000, GPR_TIMESPAN); + gpr_timespec x = gpr_time_add (infinity[i], y); + GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); + x = gpr_time_sub (infinity[i], y); + GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); + } } - } } -static void test_similar(void) { - GPR_ASSERT(1 == gpr_time_similar(gpr_inf_future(GPR_TIMESPAN), - gpr_inf_future(GPR_TIMESPAN), - gpr_time_0(GPR_TIMESPAN))); - GPR_ASSERT(1 == gpr_time_similar(gpr_inf_past(GPR_TIMESPAN), - gpr_inf_past(GPR_TIMESPAN), - gpr_time_0(GPR_TIMESPAN))); - GPR_ASSERT(0 == gpr_time_similar(gpr_inf_past(GPR_TIMESPAN), - gpr_inf_future(GPR_TIMESPAN), - gpr_time_0(GPR_TIMESPAN))); - GPR_ASSERT(0 == gpr_time_similar(gpr_inf_future(GPR_TIMESPAN), - gpr_inf_past(GPR_TIMESPAN), - gpr_time_0(GPR_TIMESPAN))); - GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN), - gpr_time_0(GPR_TIMESPAN))); - GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN), - gpr_time_from_micros(15, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN))); - GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(15, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN))); - GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN), - gpr_time_from_micros(25, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN))); - GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(25, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN), - gpr_time_from_micros(10, GPR_TIMESPAN))); +static void +test_similar (void) +{ + GPR_ASSERT (1 == gpr_time_similar (gpr_inf_future (GPR_TIMESPAN), gpr_inf_future (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); + GPR_ASSERT (1 == gpr_time_similar (gpr_inf_past (GPR_TIMESPAN), gpr_inf_past (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); + GPR_ASSERT (0 == gpr_time_similar (gpr_inf_past (GPR_TIMESPAN), gpr_inf_future (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); + GPR_ASSERT (0 == gpr_time_similar (gpr_inf_future (GPR_TIMESPAN), gpr_inf_past (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); + GPR_ASSERT (1 == gpr_time_similar (gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); + GPR_ASSERT (1 == gpr_time_similar (gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (15, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); + GPR_ASSERT (1 == gpr_time_similar (gpr_time_from_micros (15, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); + GPR_ASSERT (0 == gpr_time_similar (gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (25, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); + GPR_ASSERT (0 == gpr_time_similar (gpr_time_from_micros (25, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); } -int main(int argc, char *argv[]) { - grpc_test_init(argc, argv); +int +main (int argc, char *argv[]) +{ + grpc_test_init (argc, argv); - test_values(); - test_add_sub(); - test_overflow(); - test_sticky_infinities(); - test_similar(); + test_values (); + test_add_sub (); + test_overflow (); + test_sticky_infinities (); + test_similar (); return 0; } diff --git a/test/core/support/tls_test.c b/test/core/support/tls_test.c index 0a3c28417f..9c5cd54d63 100644 --- a/test/core/support/tls_test.c +++ b/test/core/support/tls_test.c @@ -43,41 +43,48 @@ #define NUM_THREADS 100 -GPR_TLS_DECL(test_var); +GPR_TLS_DECL (test_var); -static void thd_body(void *arg) { +static void +thd_body (void *arg) +{ gpr_intptr i; - GPR_ASSERT(gpr_tls_get(&test_var) == 0); + GPR_ASSERT (gpr_tls_get (&test_var) == 0); - for (i = 0; i < 10000000; i++) { - gpr_tls_set(&test_var, i); - GPR_ASSERT(gpr_tls_get(&test_var) == i); - } - gpr_tls_set(&test_var, 0); + for (i = 0; i < 10000000; i++) + { + gpr_tls_set (&test_var, i); + GPR_ASSERT (gpr_tls_get (&test_var) == i); + } + gpr_tls_set (&test_var, 0); } /* ------------------------------------------------- */ -int main(int argc, char *argv[]) { - gpr_thd_options opt = gpr_thd_options_default(); +int +main (int argc, char *argv[]) +{ + gpr_thd_options opt = gpr_thd_options_default (); int i; gpr_thd_id threads[NUM_THREADS]; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - gpr_tls_init(&test_var); + gpr_tls_init (&test_var); - gpr_thd_options_set_joinable(&opt); + gpr_thd_options_set_joinable (&opt); - for (i = 0; i < NUM_THREADS; i++) { - gpr_thd_new(&threads[i], thd_body, NULL, &opt); - } - for (i = 0; i < NUM_THREADS; i++) { - gpr_thd_join(threads[i]); - } + for (i = 0; i < NUM_THREADS; i++) + { + gpr_thd_new (&threads[i], thd_body, NULL, &opt); + } + for (i = 0; i < NUM_THREADS; i++) + { + gpr_thd_join (threads[i]); + } - gpr_tls_destroy(&test_var); + gpr_tls_destroy (&test_var); return 0; } diff --git a/test/core/support/useful_test.c b/test/core/support/useful_test.c index cbf4f02e26..49a1bd353e 100644 --- a/test/core/support/useful_test.c +++ b/test/core/support/useful_test.c @@ -36,38 +36,40 @@ #include <grpc/support/log.h> #include "test/core/util/test_config.h" -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ int four[4]; int five[5]; gpr_uint32 bitset = 0; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - GPR_ASSERT(GPR_MIN(1, 2) == 1); - GPR_ASSERT(GPR_MAX(1, 2) == 2); - GPR_ASSERT(GPR_MIN(2, 1) == 1); - GPR_ASSERT(GPR_MAX(2, 1) == 2); - GPR_ASSERT(GPR_CLAMP(1, 0, 2) == 1); - GPR_ASSERT(GPR_CLAMP(0, 0, 2) == 0); - GPR_ASSERT(GPR_CLAMP(2, 0, 2) == 2); - GPR_ASSERT(GPR_CLAMP(-1, 0, 2) == 0); - GPR_ASSERT(GPR_CLAMP(3, 0, 2) == 2); - GPR_ASSERT(GPR_ROTL((gpr_uint32)0x80000001, 1) == 3); - GPR_ASSERT(GPR_ROTR((gpr_uint32)0x80000001, 1) == 0xc0000000); - GPR_ASSERT(GPR_ARRAY_SIZE(four) == 4); - GPR_ASSERT(GPR_ARRAY_SIZE(five) == 5); + GPR_ASSERT (GPR_MIN (1, 2) == 1); + GPR_ASSERT (GPR_MAX (1, 2) == 2); + GPR_ASSERT (GPR_MIN (2, 1) == 1); + GPR_ASSERT (GPR_MAX (2, 1) == 2); + GPR_ASSERT (GPR_CLAMP (1, 0, 2) == 1); + GPR_ASSERT (GPR_CLAMP (0, 0, 2) == 0); + GPR_ASSERT (GPR_CLAMP (2, 0, 2) == 2); + GPR_ASSERT (GPR_CLAMP (-1, 0, 2) == 0); + GPR_ASSERT (GPR_CLAMP (3, 0, 2) == 2); + GPR_ASSERT (GPR_ROTL ((gpr_uint32) 0x80000001, 1) == 3); + GPR_ASSERT (GPR_ROTR ((gpr_uint32) 0x80000001, 1) == 0xc0000000); + GPR_ASSERT (GPR_ARRAY_SIZE (four) == 4); + GPR_ASSERT (GPR_ARRAY_SIZE (five) == 5); - GPR_ASSERT(GPR_BITCOUNT((1u << 31) - 1) == 31); - GPR_ASSERT(GPR_BITCOUNT(1u << 3) == 1); - GPR_ASSERT(GPR_BITCOUNT(0) == 0); + GPR_ASSERT (GPR_BITCOUNT ((1u << 31) - 1) == 31); + GPR_ASSERT (GPR_BITCOUNT (1u << 3) == 1); + GPR_ASSERT (GPR_BITCOUNT (0) == 0); - GPR_ASSERT(GPR_BITSET(&bitset, 3) == 8); - GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); - GPR_ASSERT(GPR_BITGET(bitset, 3) == 1); - GPR_ASSERT(GPR_BITSET(&bitset, 1) == 10); - GPR_ASSERT(GPR_BITCOUNT(bitset) == 2); - GPR_ASSERT(GPR_BITCLEAR(&bitset, 3) == 2); - GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); - GPR_ASSERT(GPR_BITGET(bitset, 3) == 0); + GPR_ASSERT (GPR_BITSET (&bitset, 3) == 8); + GPR_ASSERT (GPR_BITCOUNT (bitset) == 1); + GPR_ASSERT (GPR_BITGET (bitset, 3) == 1); + GPR_ASSERT (GPR_BITSET (&bitset, 1) == 10); + GPR_ASSERT (GPR_BITCOUNT (bitset) == 2); + GPR_ASSERT (GPR_BITCLEAR (&bitset, 3) == 2); + GPR_ASSERT (GPR_BITCOUNT (bitset) == 1); + GPR_ASSERT (GPR_BITGET (bitset, 3) == 0); return 0; } diff --git a/test/core/surface/byte_buffer_reader_test.c b/test/core/surface/byte_buffer_reader_test.c index 560e0ac7b2..dd56ba86e4 100644 --- a/test/core/surface/byte_buffer_reader_test.c +++ b/test/core/surface/byte_buffer_reader_test.c @@ -48,72 +48,79 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) -static void test_read_one_slice(void) { +static void +test_read_one_slice (void) +{ gpr_slice slice; grpc_byte_buffer *buffer; grpc_byte_buffer_reader reader; gpr_slice first_slice, second_slice; int first_code, second_code; - LOG_TEST("test_read_one_slice"); - slice = gpr_slice_from_copied_string("test"); - buffer = grpc_raw_byte_buffer_create(&slice, 1); - gpr_slice_unref(slice); - grpc_byte_buffer_reader_init(&reader, buffer); - first_code = grpc_byte_buffer_reader_next(&reader, &first_slice); - GPR_ASSERT(first_code != 0); - GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0); - gpr_slice_unref(first_slice); - second_code = grpc_byte_buffer_reader_next(&reader, &second_slice); - GPR_ASSERT(second_code == 0); - grpc_byte_buffer_destroy(buffer); + LOG_TEST ("test_read_one_slice"); + slice = gpr_slice_from_copied_string ("test"); + buffer = grpc_raw_byte_buffer_create (&slice, 1); + gpr_slice_unref (slice); + grpc_byte_buffer_reader_init (&reader, buffer); + first_code = grpc_byte_buffer_reader_next (&reader, &first_slice); + GPR_ASSERT (first_code != 0); + GPR_ASSERT (memcmp (GPR_SLICE_START_PTR (first_slice), "test", 4) == 0); + gpr_slice_unref (first_slice); + second_code = grpc_byte_buffer_reader_next (&reader, &second_slice); + GPR_ASSERT (second_code == 0); + grpc_byte_buffer_destroy (buffer); } -static void test_read_one_slice_malloc(void) { +static void +test_read_one_slice_malloc (void) +{ gpr_slice slice; grpc_byte_buffer *buffer; grpc_byte_buffer_reader reader; gpr_slice first_slice, second_slice; int first_code, second_code; - LOG_TEST("test_read_one_slice_malloc"); - slice = gpr_slice_malloc(4); - memcpy(GPR_SLICE_START_PTR(slice), "test", 4); - buffer = grpc_raw_byte_buffer_create(&slice, 1); - gpr_slice_unref(slice); - grpc_byte_buffer_reader_init(&reader, buffer); - first_code = grpc_byte_buffer_reader_next(&reader, &first_slice); - GPR_ASSERT(first_code != 0); - GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0); - gpr_slice_unref(first_slice); - second_code = grpc_byte_buffer_reader_next(&reader, &second_slice); - GPR_ASSERT(second_code == 0); - grpc_byte_buffer_destroy(buffer); + LOG_TEST ("test_read_one_slice_malloc"); + slice = gpr_slice_malloc (4); + memcpy (GPR_SLICE_START_PTR (slice), "test", 4); + buffer = grpc_raw_byte_buffer_create (&slice, 1); + gpr_slice_unref (slice); + grpc_byte_buffer_reader_init (&reader, buffer); + first_code = grpc_byte_buffer_reader_next (&reader, &first_slice); + GPR_ASSERT (first_code != 0); + GPR_ASSERT (memcmp (GPR_SLICE_START_PTR (first_slice), "test", 4) == 0); + gpr_slice_unref (first_slice); + second_code = grpc_byte_buffer_reader_next (&reader, &second_slice); + GPR_ASSERT (second_code == 0); + grpc_byte_buffer_destroy (buffer); } -static void test_read_none_compressed_slice(void) { +static void +test_read_none_compressed_slice (void) +{ gpr_slice slice; grpc_byte_buffer *buffer; grpc_byte_buffer_reader reader; gpr_slice first_slice, second_slice; int first_code, second_code; - LOG_TEST("test_read_none_compressed_slice"); - slice = gpr_slice_from_copied_string("test"); - buffer = grpc_raw_byte_buffer_create(&slice, 1); - gpr_slice_unref(slice); - grpc_byte_buffer_reader_init(&reader, buffer); - first_code = grpc_byte_buffer_reader_next(&reader, &first_slice); - GPR_ASSERT(first_code != 0); - GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0); - gpr_slice_unref(first_slice); - second_code = grpc_byte_buffer_reader_next(&reader, &second_slice); - GPR_ASSERT(second_code == 0); - grpc_byte_buffer_destroy(buffer); + LOG_TEST ("test_read_none_compressed_slice"); + slice = gpr_slice_from_copied_string ("test"); + buffer = grpc_raw_byte_buffer_create (&slice, 1); + gpr_slice_unref (slice); + grpc_byte_buffer_reader_init (&reader, buffer); + first_code = grpc_byte_buffer_reader_next (&reader, &first_slice); + GPR_ASSERT (first_code != 0); + GPR_ASSERT (memcmp (GPR_SLICE_START_PTR (first_slice), "test", 4) == 0); + gpr_slice_unref (first_slice); + second_code = grpc_byte_buffer_reader_next (&reader, &second_slice); + GPR_ASSERT (second_code == 0); + grpc_byte_buffer_destroy (buffer); } -static void read_compressed_slice(grpc_compression_algorithm algorithm, - size_t input_size) { +static void +read_compressed_slice (grpc_compression_algorithm algorithm, size_t input_size) +{ gpr_slice input_slice; gpr_slice_buffer sliceb_in; gpr_slice_buffer sliceb_out; @@ -122,76 +129,80 @@ static void read_compressed_slice(grpc_compression_algorithm algorithm, gpr_slice read_slice; size_t read_count = 0; - gpr_slice_buffer_init(&sliceb_in); - gpr_slice_buffer_init(&sliceb_out); - - input_slice = gpr_slice_malloc(input_size); - memset(GPR_SLICE_START_PTR(input_slice), 'a', input_size); - gpr_slice_buffer_add(&sliceb_in, input_slice); /* takes ownership */ - GPR_ASSERT(grpc_msg_compress(algorithm, &sliceb_in, &sliceb_out)); - - buffer = grpc_raw_compressed_byte_buffer_create(sliceb_out.slices, - sliceb_out.count, algorithm); - grpc_byte_buffer_reader_init(&reader, buffer); - - while (grpc_byte_buffer_reader_next(&reader, &read_slice)) { - GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(read_slice), - GPR_SLICE_START_PTR(input_slice) + read_count, - GPR_SLICE_LENGTH(read_slice)) == 0); - read_count += GPR_SLICE_LENGTH(read_slice); - gpr_slice_unref(read_slice); - } - GPR_ASSERT(read_count == input_size); - grpc_byte_buffer_reader_destroy(&reader); - grpc_byte_buffer_destroy(buffer); - gpr_slice_buffer_destroy(&sliceb_out); - gpr_slice_buffer_destroy(&sliceb_in); + gpr_slice_buffer_init (&sliceb_in); + gpr_slice_buffer_init (&sliceb_out); + + input_slice = gpr_slice_malloc (input_size); + memset (GPR_SLICE_START_PTR (input_slice), 'a', input_size); + gpr_slice_buffer_add (&sliceb_in, input_slice); /* takes ownership */ + GPR_ASSERT (grpc_msg_compress (algorithm, &sliceb_in, &sliceb_out)); + + buffer = grpc_raw_compressed_byte_buffer_create (sliceb_out.slices, sliceb_out.count, algorithm); + grpc_byte_buffer_reader_init (&reader, buffer); + + while (grpc_byte_buffer_reader_next (&reader, &read_slice)) + { + GPR_ASSERT (memcmp (GPR_SLICE_START_PTR (read_slice), GPR_SLICE_START_PTR (input_slice) + read_count, GPR_SLICE_LENGTH (read_slice)) == 0); + read_count += GPR_SLICE_LENGTH (read_slice); + gpr_slice_unref (read_slice); + } + GPR_ASSERT (read_count == input_size); + grpc_byte_buffer_reader_destroy (&reader); + grpc_byte_buffer_destroy (buffer); + gpr_slice_buffer_destroy (&sliceb_out); + gpr_slice_buffer_destroy (&sliceb_in); } -static void test_read_gzip_compressed_slice(void) { +static void +test_read_gzip_compressed_slice (void) +{ const size_t INPUT_SIZE = 2048; - LOG_TEST("test_read_gzip_compressed_slice"); - read_compressed_slice(GRPC_COMPRESS_GZIP, INPUT_SIZE); + LOG_TEST ("test_read_gzip_compressed_slice"); + read_compressed_slice (GRPC_COMPRESS_GZIP, INPUT_SIZE); } -static void test_read_deflate_compressed_slice(void) { +static void +test_read_deflate_compressed_slice (void) +{ const size_t INPUT_SIZE = 2048; - LOG_TEST("test_read_deflate_compressed_slice"); - read_compressed_slice(GRPC_COMPRESS_DEFLATE, INPUT_SIZE); + LOG_TEST ("test_read_deflate_compressed_slice"); + read_compressed_slice (GRPC_COMPRESS_DEFLATE, INPUT_SIZE); } -static void test_byte_buffer_from_reader(void) { +static void +test_byte_buffer_from_reader (void) +{ gpr_slice slice; grpc_byte_buffer *buffer, *buffer_from_reader; grpc_byte_buffer_reader reader; - LOG_TEST("test_byte_buffer_from_reader"); - slice = gpr_slice_malloc(4); - memcpy(GPR_SLICE_START_PTR(slice), "test", 4); - buffer = grpc_raw_byte_buffer_create(&slice, 1); - gpr_slice_unref(slice); - grpc_byte_buffer_reader_init(&reader, buffer); - - buffer_from_reader = grpc_raw_byte_buffer_from_reader(&reader); - GPR_ASSERT(buffer->type == buffer_from_reader->type); - GPR_ASSERT(buffer_from_reader->data.raw.compression == GRPC_COMPRESS_NONE); - GPR_ASSERT(buffer_from_reader->data.raw.slice_buffer.count == 1); - GPR_ASSERT(memcmp(GPR_SLICE_START_PTR( - buffer_from_reader->data.raw.slice_buffer.slices[0]), - "test", 4) == 0); - - grpc_byte_buffer_destroy(buffer); - grpc_byte_buffer_destroy(buffer_from_reader); + LOG_TEST ("test_byte_buffer_from_reader"); + slice = gpr_slice_malloc (4); + memcpy (GPR_SLICE_START_PTR (slice), "test", 4); + buffer = grpc_raw_byte_buffer_create (&slice, 1); + gpr_slice_unref (slice); + grpc_byte_buffer_reader_init (&reader, buffer); + + buffer_from_reader = grpc_raw_byte_buffer_from_reader (&reader); + GPR_ASSERT (buffer->type == buffer_from_reader->type); + GPR_ASSERT (buffer_from_reader->data.raw.compression == GRPC_COMPRESS_NONE); + GPR_ASSERT (buffer_from_reader->data.raw.slice_buffer.count == 1); + GPR_ASSERT (memcmp (GPR_SLICE_START_PTR (buffer_from_reader->data.raw.slice_buffer.slices[0]), "test", 4) == 0); + + grpc_byte_buffer_destroy (buffer); + grpc_byte_buffer_destroy (buffer_from_reader); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_read_one_slice(); - test_read_one_slice_malloc(); - test_read_none_compressed_slice(); - test_read_gzip_compressed_slice(); - test_read_deflate_compressed_slice(); - test_byte_buffer_from_reader(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_read_one_slice (); + test_read_one_slice_malloc (); + test_read_none_compressed_slice (); + test_read_gzip_compressed_slice (); + test_read_deflate_compressed_slice (); + test_byte_buffer_from_reader (); return 0; } diff --git a/test/core/surface/completion_queue_test.c b/test/core/surface/completion_queue_test.c index 3dee8f23af..cd0bce8fc2 100644 --- a/test/core/surface/completion_queue_test.c +++ b/test/core/surface/completion_queue_test.c @@ -43,141 +43,159 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) -static void *create_test_tag(void) { +static void * +create_test_tag (void) +{ static gpr_intptr i = 0; - return (void *)(++i); + return (void *) (++i); } /* helper for tests to shutdown correctly and tersely */ -static void shutdown_and_destroy(grpc_completion_queue *cc) { +static void +shutdown_and_destroy (grpc_completion_queue * cc) +{ grpc_event ev; - grpc_completion_queue_shutdown(cc); - ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN); - grpc_completion_queue_destroy(cc); + grpc_completion_queue_shutdown (cc); + ev = grpc_completion_queue_next (cc, gpr_inf_past (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (ev.type == GRPC_QUEUE_SHUTDOWN); + grpc_completion_queue_destroy (cc); } /* ensure we can create and destroy a completion channel */ -static void test_no_op(void) { - LOG_TEST("test_no_op"); - shutdown_and_destroy(grpc_completion_queue_create(NULL)); +static void +test_no_op (void) +{ + LOG_TEST ("test_no_op"); + shutdown_and_destroy (grpc_completion_queue_create (NULL)); } -static void test_wait_empty(void) { +static void +test_wait_empty (void) +{ grpc_completion_queue *cc; grpc_event event; - LOG_TEST("test_wait_empty"); + LOG_TEST ("test_wait_empty"); - cc = grpc_completion_queue_create(NULL); - event = grpc_completion_queue_next(cc, gpr_now(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(event.type == GRPC_QUEUE_TIMEOUT); - shutdown_and_destroy(cc); + cc = grpc_completion_queue_create (NULL); + event = grpc_completion_queue_next (cc, gpr_now (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (event.type == GRPC_QUEUE_TIMEOUT); + shutdown_and_destroy (cc); } -static void do_nothing_end_completion(void *arg, grpc_cq_completion *c, - grpc_closure_list *closure_list) {} +static void +do_nothing_end_completion (void *arg, grpc_cq_completion * c, grpc_closure_list * closure_list) +{ +} -static void test_cq_end_op(void) { +static void +test_cq_end_op (void) +{ grpc_event ev; grpc_completion_queue *cc; grpc_cq_completion completion; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - void *tag = create_test_tag(); + void *tag = create_test_tag (); - LOG_TEST("test_cq_end_op"); + LOG_TEST ("test_cq_end_op"); - cc = grpc_completion_queue_create(NULL); + cc = grpc_completion_queue_create (NULL); - grpc_cq_begin_op(cc); - grpc_cq_end_op(cc, tag, 1, do_nothing_end_completion, NULL, &completion, - &closure_list); + grpc_cq_begin_op (cc); + grpc_cq_end_op (cc, tag, 1, do_nothing_end_completion, NULL, &completion, &closure_list); - ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); - GPR_ASSERT(ev.tag == tag); - GPR_ASSERT(ev.success); + ev = grpc_completion_queue_next (cc, gpr_inf_past (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (ev.type == GRPC_OP_COMPLETE); + GPR_ASSERT (ev.tag == tag); + GPR_ASSERT (ev.success); - shutdown_and_destroy(cc); - GPR_ASSERT(grpc_closure_list_empty(closure_list)); + shutdown_and_destroy (cc); + GPR_ASSERT (grpc_closure_list_empty (closure_list)); } -static void test_shutdown_then_next_polling(void) { +static void +test_shutdown_then_next_polling (void) +{ grpc_completion_queue *cc; grpc_event event; - LOG_TEST("test_shutdown_then_next_polling"); - - cc = grpc_completion_queue_create(NULL); - grpc_completion_queue_shutdown(cc); - event = - grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(event.type == GRPC_QUEUE_SHUTDOWN); - grpc_completion_queue_destroy(cc); + LOG_TEST ("test_shutdown_then_next_polling"); + + cc = grpc_completion_queue_create (NULL); + grpc_completion_queue_shutdown (cc); + event = grpc_completion_queue_next (cc, gpr_inf_past (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (event.type == GRPC_QUEUE_SHUTDOWN); + grpc_completion_queue_destroy (cc); } -static void test_shutdown_then_next_with_timeout(void) { +static void +test_shutdown_then_next_with_timeout (void) +{ grpc_completion_queue *cc; grpc_event event; - LOG_TEST("test_shutdown_then_next_with_timeout"); - - cc = grpc_completion_queue_create(NULL); - grpc_completion_queue_shutdown(cc); - event = - grpc_completion_queue_next(cc, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(event.type == GRPC_QUEUE_SHUTDOWN); - grpc_completion_queue_destroy(cc); + LOG_TEST ("test_shutdown_then_next_with_timeout"); + + cc = grpc_completion_queue_create (NULL); + grpc_completion_queue_shutdown (cc); + event = grpc_completion_queue_next (cc, gpr_inf_future (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (event.type == GRPC_QUEUE_SHUTDOWN); + grpc_completion_queue_destroy (cc); } -static void test_pluck(void) { +static void +test_pluck (void) +{ grpc_event ev; grpc_completion_queue *cc; void *tags[128]; - grpc_cq_completion completions[GPR_ARRAY_SIZE(tags)]; + grpc_cq_completion completions[GPR_ARRAY_SIZE (tags)]; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; unsigned i, j; - LOG_TEST("test_pluck"); + LOG_TEST ("test_pluck"); + + for (i = 0; i < GPR_ARRAY_SIZE (tags); i++) + { + tags[i] = create_test_tag (); + for (j = 0; j < i; j++) + { + GPR_ASSERT (tags[i] != tags[j]); + } + } + + cc = grpc_completion_queue_create (NULL); + + for (i = 0; i < GPR_ARRAY_SIZE (tags); i++) + { + grpc_cq_begin_op (cc); + grpc_cq_end_op (cc, tags[i], 1, do_nothing_end_completion, NULL, &completions[i], &closure_list); + } + + for (i = 0; i < GPR_ARRAY_SIZE (tags); i++) + { + ev = grpc_completion_queue_pluck (cc, tags[i], gpr_inf_past (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (ev.tag == tags[i]); + } + + for (i = 0; i < GPR_ARRAY_SIZE (tags); i++) + { + grpc_cq_begin_op (cc); + grpc_cq_end_op (cc, tags[i], 1, do_nothing_end_completion, NULL, &completions[i], &closure_list); + } - for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { - tags[i] = create_test_tag(); - for (j = 0; j < i; j++) { - GPR_ASSERT(tags[i] != tags[j]); + for (i = 0; i < GPR_ARRAY_SIZE (tags); i++) + { + ev = grpc_completion_queue_pluck (cc, tags[GPR_ARRAY_SIZE (tags) - i - 1], gpr_inf_past (GPR_CLOCK_REALTIME), NULL); + GPR_ASSERT (ev.tag == tags[GPR_ARRAY_SIZE (tags) - i - 1]); } - } - - cc = grpc_completion_queue_create(NULL); - - for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { - grpc_cq_begin_op(cc); - grpc_cq_end_op(cc, tags[i], 1, do_nothing_end_completion, NULL, - &completions[i], &closure_list); - } - - for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { - ev = grpc_completion_queue_pluck(cc, tags[i], - gpr_inf_past(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(ev.tag == tags[i]); - } - - for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { - grpc_cq_begin_op(cc); - grpc_cq_end_op(cc, tags[i], 1, do_nothing_end_completion, NULL, - &completions[i], &closure_list); - } - - for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { - ev = grpc_completion_queue_pluck(cc, tags[GPR_ARRAY_SIZE(tags) - i - 1], - gpr_inf_past(GPR_CLOCK_REALTIME), NULL); - GPR_ASSERT(ev.tag == tags[GPR_ARRAY_SIZE(tags) - i - 1]); - } - - shutdown_and_destroy(cc); - GPR_ASSERT(grpc_closure_list_empty(closure_list)); + + shutdown_and_destroy (cc); + GPR_ASSERT (grpc_closure_list_empty (closure_list)); } #define TEST_THREAD_EVENTS 10000 -typedef struct test_thread_options { +typedef struct test_thread_options +{ gpr_event on_started; gpr_event *phase1; gpr_event on_phase1_done; @@ -188,164 +206,181 @@ typedef struct test_thread_options { grpc_completion_queue *cc; } test_thread_options; -gpr_timespec ten_seconds_time(void) { - return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); +gpr_timespec +ten_seconds_time (void) +{ + return GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10); } -static void free_completion(void *arg, grpc_cq_completion *completion, - grpc_closure_list *closure_list) { - gpr_free(completion); +static void +free_completion (void *arg, grpc_cq_completion * completion, grpc_closure_list * closure_list) +{ + gpr_free (completion); } -static void producer_thread(void *arg) { +static void +producer_thread (void *arg) +{ test_thread_options *opt = arg; int i; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_log(GPR_INFO, "producer %d started", opt->id); - gpr_event_set(&opt->on_started, (void *)(gpr_intptr)1); - GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time())); - - gpr_log(GPR_INFO, "producer %d phase 1", opt->id); - for (i = 0; i < TEST_THREAD_EVENTS; i++) { - grpc_cq_begin_op(opt->cc); - } - - gpr_log(GPR_INFO, "producer %d phase 1 done", opt->id); - gpr_event_set(&opt->on_phase1_done, (void *)(gpr_intptr)1); - GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time())); - - gpr_log(GPR_INFO, "producer %d phase 2", opt->id); - for (i = 0; i < TEST_THREAD_EVENTS; i++) { - grpc_cq_end_op(opt->cc, (void *)(gpr_intptr)1, 1, free_completion, NULL, - gpr_malloc(sizeof(grpc_cq_completion)), &closure_list); - opt->events_triggered++; - grpc_closure_list_run(&closure_list); - } - - gpr_log(GPR_INFO, "producer %d phase 2 done", opt->id); - gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1); - GPR_ASSERT(grpc_closure_list_empty(closure_list)); + gpr_log (GPR_INFO, "producer %d started", opt->id); + gpr_event_set (&opt->on_started, (void *) (gpr_intptr) 1); + GPR_ASSERT (gpr_event_wait (opt->phase1, ten_seconds_time ())); + + gpr_log (GPR_INFO, "producer %d phase 1", opt->id); + for (i = 0; i < TEST_THREAD_EVENTS; i++) + { + grpc_cq_begin_op (opt->cc); + } + + gpr_log (GPR_INFO, "producer %d phase 1 done", opt->id); + gpr_event_set (&opt->on_phase1_done, (void *) (gpr_intptr) 1); + GPR_ASSERT (gpr_event_wait (opt->phase2, ten_seconds_time ())); + + gpr_log (GPR_INFO, "producer %d phase 2", opt->id); + for (i = 0; i < TEST_THREAD_EVENTS; i++) + { + grpc_cq_end_op (opt->cc, (void *) (gpr_intptr) 1, 1, free_completion, NULL, gpr_malloc (sizeof (grpc_cq_completion)), &closure_list); + opt->events_triggered++; + grpc_closure_list_run (&closure_list); + } + + gpr_log (GPR_INFO, "producer %d phase 2 done", opt->id); + gpr_event_set (&opt->on_finished, (void *) (gpr_intptr) 1); + GPR_ASSERT (grpc_closure_list_empty (closure_list)); } -static void consumer_thread(void *arg) { +static void +consumer_thread (void *arg) +{ test_thread_options *opt = arg; grpc_event ev; - gpr_log(GPR_INFO, "consumer %d started", opt->id); - gpr_event_set(&opt->on_started, (void *)(gpr_intptr)1); - GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time())); - - gpr_log(GPR_INFO, "consumer %d phase 1", opt->id); - - gpr_log(GPR_INFO, "consumer %d phase 1 done", opt->id); - gpr_event_set(&opt->on_phase1_done, (void *)(gpr_intptr)1); - GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time())); - - gpr_log(GPR_INFO, "consumer %d phase 2", opt->id); - for (;;) { - ev = grpc_completion_queue_next(opt->cc, ten_seconds_time(), NULL); - switch (ev.type) { - case GRPC_OP_COMPLETE: - GPR_ASSERT(ev.success); - opt->events_triggered++; - break; - case GRPC_QUEUE_SHUTDOWN: - gpr_log(GPR_INFO, "consumer %d phase 2 done", opt->id); - gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1); - return; - case GRPC_QUEUE_TIMEOUT: - gpr_log(GPR_ERROR, "Invalid timeout received"); - abort(); + gpr_log (GPR_INFO, "consumer %d started", opt->id); + gpr_event_set (&opt->on_started, (void *) (gpr_intptr) 1); + GPR_ASSERT (gpr_event_wait (opt->phase1, ten_seconds_time ())); + + gpr_log (GPR_INFO, "consumer %d phase 1", opt->id); + + gpr_log (GPR_INFO, "consumer %d phase 1 done", opt->id); + gpr_event_set (&opt->on_phase1_done, (void *) (gpr_intptr) 1); + GPR_ASSERT (gpr_event_wait (opt->phase2, ten_seconds_time ())); + + gpr_log (GPR_INFO, "consumer %d phase 2", opt->id); + for (;;) + { + ev = grpc_completion_queue_next (opt->cc, ten_seconds_time (), NULL); + switch (ev.type) + { + case GRPC_OP_COMPLETE: + GPR_ASSERT (ev.success); + opt->events_triggered++; + break; + case GRPC_QUEUE_SHUTDOWN: + gpr_log (GPR_INFO, "consumer %d phase 2 done", opt->id); + gpr_event_set (&opt->on_finished, (void *) (gpr_intptr) 1); + return; + case GRPC_QUEUE_TIMEOUT: + gpr_log (GPR_ERROR, "Invalid timeout received"); + abort (); + } } - } } -static void test_threading(size_t producers, size_t consumers) { - test_thread_options *options = - gpr_malloc((producers + consumers) * sizeof(test_thread_options)); +static void +test_threading (size_t producers, size_t consumers) +{ + test_thread_options *options = gpr_malloc ((producers + consumers) * sizeof (test_thread_options)); gpr_event phase1 = GPR_EVENT_INIT; gpr_event phase2 = GPR_EVENT_INIT; - grpc_completion_queue *cc = grpc_completion_queue_create(NULL); + grpc_completion_queue *cc = grpc_completion_queue_create (NULL); size_t i; size_t total_consumed = 0; static int optid = 101; - gpr_log(GPR_INFO, "%s: %d producers, %d consumers", "test_threading", - producers, consumers); + gpr_log (GPR_INFO, "%s: %d producers, %d consumers", "test_threading", producers, consumers); /* start all threads: they will wait for phase1 */ - for (i = 0; i < producers + consumers; i++) { - gpr_thd_id id; - gpr_event_init(&options[i].on_started); - gpr_event_init(&options[i].on_phase1_done); - gpr_event_init(&options[i].on_finished); - options[i].phase1 = &phase1; - options[i].phase2 = &phase2; - options[i].events_triggered = 0; - options[i].cc = cc; - options[i].id = optid++; - GPR_ASSERT(gpr_thd_new(&id, - i < producers ? producer_thread : consumer_thread, - options + i, NULL)); - gpr_event_wait(&options[i].on_started, ten_seconds_time()); - } + for (i = 0; i < producers + consumers; i++) + { + gpr_thd_id id; + gpr_event_init (&options[i].on_started); + gpr_event_init (&options[i].on_phase1_done); + gpr_event_init (&options[i].on_finished); + options[i].phase1 = &phase1; + options[i].phase2 = &phase2; + options[i].events_triggered = 0; + options[i].cc = cc; + options[i].id = optid++; + GPR_ASSERT (gpr_thd_new (&id, i < producers ? producer_thread : consumer_thread, options + i, NULL)); + gpr_event_wait (&options[i].on_started, ten_seconds_time ()); + } /* start phase1: producers will pre-declare all operations they will complete */ - gpr_log(GPR_INFO, "start phase 1"); - gpr_event_set(&phase1, (void *)(gpr_intptr)1); + gpr_log (GPR_INFO, "start phase 1"); + gpr_event_set (&phase1, (void *) (gpr_intptr) 1); - gpr_log(GPR_INFO, "wait phase 1"); - for (i = 0; i < producers + consumers; i++) { - GPR_ASSERT(gpr_event_wait(&options[i].on_phase1_done, ten_seconds_time())); - } - gpr_log(GPR_INFO, "done phase 1"); + gpr_log (GPR_INFO, "wait phase 1"); + for (i = 0; i < producers + consumers; i++) + { + GPR_ASSERT (gpr_event_wait (&options[i].on_phase1_done, ten_seconds_time ())); + } + gpr_log (GPR_INFO, "done phase 1"); /* start phase2: operations will complete, and consumers will consume them */ - gpr_log(GPR_INFO, "start phase 2"); - gpr_event_set(&phase2, (void *)(gpr_intptr)1); + gpr_log (GPR_INFO, "start phase 2"); + gpr_event_set (&phase2, (void *) (gpr_intptr) 1); /* in parallel, we shutdown the completion channel - all events should still be consumed */ - grpc_completion_queue_shutdown(cc); + grpc_completion_queue_shutdown (cc); /* join all threads */ - gpr_log(GPR_INFO, "wait phase 2"); - for (i = 0; i < producers + consumers; i++) { - GPR_ASSERT(gpr_event_wait(&options[i].on_finished, ten_seconds_time())); - } - gpr_log(GPR_INFO, "done phase 2"); + gpr_log (GPR_INFO, "wait phase 2"); + for (i = 0; i < producers + consumers; i++) + { + GPR_ASSERT (gpr_event_wait (&options[i].on_finished, ten_seconds_time ())); + } + gpr_log (GPR_INFO, "done phase 2"); /* destroy the completion channel */ - grpc_completion_queue_destroy(cc); + grpc_completion_queue_destroy (cc); /* verify that everything was produced and consumed */ - for (i = 0; i < producers + consumers; i++) { - if (i < producers) { - GPR_ASSERT(options[i].events_triggered == TEST_THREAD_EVENTS); - } else { - total_consumed += options[i].events_triggered; + for (i = 0; i < producers + consumers; i++) + { + if (i < producers) + { + GPR_ASSERT (options[i].events_triggered == TEST_THREAD_EVENTS); + } + else + { + total_consumed += options[i].events_triggered; + } } - } - GPR_ASSERT(total_consumed == producers * TEST_THREAD_EVENTS); + GPR_ASSERT (total_consumed == producers * TEST_THREAD_EVENTS); - gpr_free(options); + gpr_free (options); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - grpc_init(); - test_no_op(); - test_wait_empty(); - test_shutdown_then_next_polling(); - test_shutdown_then_next_with_timeout(); - test_cq_end_op(); - test_pluck(); - test_threading(1, 1); - test_threading(1, 10); - test_threading(10, 1); - test_threading(10, 10); - grpc_shutdown(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + grpc_init (); + test_no_op (); + test_wait_empty (); + test_shutdown_then_next_polling (); + test_shutdown_then_next_with_timeout (); + test_cq_end_op (); + test_pluck (); + test_threading (1, 1); + test_threading (1, 10); + test_threading (10, 1); + test_threading (10, 10); + grpc_shutdown (); return 0; } diff --git a/test/core/surface/lame_client_test.c b/test/core/surface/lame_client_test.c index 0d29bea555..434779b273 100644 --- a/test/core/surface/lame_client_test.c +++ b/test/core/surface/lame_client_test.c @@ -38,9 +38,15 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> -static void *tag(gpr_intptr x) { return (void *)x; } +static void * +tag (gpr_intptr x) +{ + return (void *) x; +} -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ grpc_channel *chan; grpc_call *call; grpc_completion_queue *cq; @@ -53,20 +59,17 @@ int main(int argc, char **argv) { char *details = NULL; size_t details_capacity = 0; - grpc_test_init(argc, argv); - grpc_init(); + grpc_test_init (argc, argv); + grpc_init (); - grpc_metadata_array_init(&trailing_metadata_recv); + grpc_metadata_array_init (&trailing_metadata_recv); - chan = grpc_lame_client_channel_create( - "lampoon:national", GRPC_STATUS_UNKNOWN, "Rpc sent on a lame channel."); - GPR_ASSERT(chan); - cq = grpc_completion_queue_create(NULL); - call = grpc_channel_create_call(chan, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - "/Foo", "anywhere", - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(100), NULL); - GPR_ASSERT(call); - cqv = cq_verifier_create(cq); + chan = grpc_lame_client_channel_create ("lampoon:national", GRPC_STATUS_UNKNOWN, "Rpc sent on a lame channel."); + GPR_ASSERT (chan); + cq = grpc_completion_queue_create (NULL); + call = grpc_channel_create_call (chan, NULL, GRPC_PROPAGATE_DEFAULTS, cq, "/Foo", "anywhere", GRPC_TIMEOUT_SECONDS_TO_DEADLINE (100), NULL); + GPR_ASSERT (call); + cqv = cq_verifier_create (cq); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; @@ -82,22 +85,22 @@ int main(int argc, char **argv) { op->flags = 0; op->reserved = NULL; op++; - error = grpc_call_start_batch(call, ops, (size_t)(op - ops), tag(1), NULL); - GPR_ASSERT(GRPC_CALL_OK == error); + error = grpc_call_start_batch (call, ops, (size_t) (op - ops), tag (1), NULL); + GPR_ASSERT (GRPC_CALL_OK == error); /* the call should immediately fail */ - cq_expect_completion(cqv, tag(1), 1); - cq_verify(cqv); + cq_expect_completion (cqv, tag (1), 1); + cq_verify (cqv); - grpc_call_destroy(call); - grpc_channel_destroy(chan); - cq_verifier_destroy(cqv); - grpc_completion_queue_destroy(cq); + grpc_call_destroy (call); + grpc_channel_destroy (chan); + cq_verifier_destroy (cqv); + grpc_completion_queue_destroy (cq); - grpc_metadata_array_destroy(&trailing_metadata_recv); - gpr_free(details); + grpc_metadata_array_destroy (&trailing_metadata_recv); + gpr_free (details); - grpc_shutdown(); + grpc_shutdown (); return 0; } diff --git a/test/core/surface/multi_init_test.c b/test/core/surface/multi_init_test.c index 99b7a52ff9..fafaae5902 100644 --- a/test/core/surface/multi_init_test.c +++ b/test/core/surface/multi_init_test.c @@ -34,30 +34,38 @@ #include <grpc/grpc.h> #include "test/core/util/test_config.h" -static void test(int rounds) { +static void +test (int rounds) +{ int i; - for (i = 0; i < rounds; i++) { - grpc_init(); - } - for (i = 0; i < rounds; i++) { - grpc_shutdown(); - } + for (i = 0; i < rounds; i++) + { + grpc_init (); + } + for (i = 0; i < rounds; i++) + { + grpc_shutdown (); + } } -static void test_mixed() { - grpc_init(); - grpc_init(); - grpc_shutdown(); - grpc_init(); - grpc_shutdown(); - grpc_shutdown(); +static void +test_mixed () +{ + grpc_init (); + grpc_init (); + grpc_shutdown (); + grpc_init (); + grpc_shutdown (); + grpc_shutdown (); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test(1); - test(2); - test(3); - test_mixed(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test (1); + test (2); + test (3); + test_mixed (); return 0; } diff --git a/test/core/transport/chttp2/alpn_test.c b/test/core/transport/chttp2/alpn_test.c index 9a7d5ef0c3..0a9b67e5fd 100644 --- a/test/core/transport/chttp2/alpn_test.c +++ b/test/core/transport/chttp2/alpn_test.c @@ -36,18 +36,24 @@ #include <grpc/support/log.h> #include "test/core/util/test_config.h" -static void test_alpn_success(void) { - GPR_ASSERT(grpc_chttp2_is_alpn_version_supported("h2", 2)); +static void +test_alpn_success (void) +{ + GPR_ASSERT (grpc_chttp2_is_alpn_version_supported ("h2", 2)); } -static void test_alpn_failure(void) { - GPR_ASSERT(!grpc_chttp2_is_alpn_version_supported("h2-155", 6)); - GPR_ASSERT(!grpc_chttp2_is_alpn_version_supported("h1-15", 5)); +static void +test_alpn_failure (void) +{ + GPR_ASSERT (!grpc_chttp2_is_alpn_version_supported ("h2-155", 6)); + GPR_ASSERT (!grpc_chttp2_is_alpn_version_supported ("h1-15", 5)); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_alpn_success(); - test_alpn_failure(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_alpn_success (); + test_alpn_failure (); return 0; } diff --git a/test/core/transport/chttp2/bin_encoder_test.c b/test/core/transport/chttp2/bin_encoder_test.c index 1ffd8ed3cb..7d3ba22497 100644 --- a/test/core/transport/chttp2/bin_encoder_test.c +++ b/test/core/transport/chttp2/bin_encoder_test.c @@ -41,32 +41,37 @@ static int all_ok = 1; -static void expect_slice_eq(gpr_slice expected, gpr_slice slice, char *debug, - int line) { - if (0 != gpr_slice_cmp(slice, expected)) { - char *hs = gpr_dump_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); - char *he = gpr_dump_slice(expected, GPR_DUMP_HEX | GPR_DUMP_ASCII); - gpr_log(GPR_ERROR, "FAILED:%d: %s\ngot: %s\nwant: %s", line, debug, hs, - he); - gpr_free(hs); - gpr_free(he); - all_ok = 0; - } - gpr_slice_unref(expected); - gpr_slice_unref(slice); +static void +expect_slice_eq (gpr_slice expected, gpr_slice slice, char *debug, int line) +{ + if (0 != gpr_slice_cmp (slice, expected)) + { + char *hs = gpr_dump_slice (slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); + char *he = gpr_dump_slice (expected, GPR_DUMP_HEX | GPR_DUMP_ASCII); + gpr_log (GPR_ERROR, "FAILED:%d: %s\ngot: %s\nwant: %s", line, debug, hs, he); + gpr_free (hs); + gpr_free (he); + all_ok = 0; + } + gpr_slice_unref (expected); + gpr_slice_unref (slice); } -static gpr_slice B64(const char *s) { - gpr_slice ss = gpr_slice_from_copied_string(s); - gpr_slice out = grpc_chttp2_base64_encode(ss); - gpr_slice_unref(ss); +static gpr_slice +B64 (const char *s) +{ + gpr_slice ss = gpr_slice_from_copied_string (s); + gpr_slice out = grpc_chttp2_base64_encode (ss); + gpr_slice_unref (ss); return out; } -static gpr_slice HUFF(const char *s) { - gpr_slice ss = gpr_slice_from_copied_string(s); - gpr_slice out = grpc_chttp2_huffman_compress(ss); - gpr_slice_unref(ss); +static gpr_slice +HUFF (const char *s) +{ + gpr_slice ss = gpr_slice_from_copied_string (s); + gpr_slice out = grpc_chttp2_huffman_compress (ss); + gpr_slice_unref (ss); return out; } @@ -75,108 +80,93 @@ static gpr_slice HUFF(const char *s) { gpr_slice_from_copied_buffer(expected, sizeof(expected) - 1), slice, \ #slice, __LINE__); -static void expect_combined_equiv(const char *s, size_t len, int line) { - gpr_slice input = gpr_slice_from_copied_buffer(s, len); - gpr_slice base64 = grpc_chttp2_base64_encode(input); - gpr_slice expect = grpc_chttp2_huffman_compress(base64); - gpr_slice got = grpc_chttp2_base64_encode_and_huffman_compress(input); - if (0 != gpr_slice_cmp(expect, got)) { - char *t = gpr_dump_slice(input, GPR_DUMP_HEX | GPR_DUMP_ASCII); - char *e = gpr_dump_slice(expect, GPR_DUMP_HEX | GPR_DUMP_ASCII); - char *g = gpr_dump_slice(got, GPR_DUMP_HEX | GPR_DUMP_ASCII); - gpr_log(GPR_ERROR, "FAILED:%d:\ntest: %s\ngot: %s\nwant: %s", t, g, e); - gpr_free(t); - gpr_free(e); - gpr_free(g); - all_ok = 0; - } - gpr_slice_unref(input); - gpr_slice_unref(base64); - gpr_slice_unref(expect); - gpr_slice_unref(got); +static void +expect_combined_equiv (const char *s, size_t len, int line) +{ + gpr_slice input = gpr_slice_from_copied_buffer (s, len); + gpr_slice base64 = grpc_chttp2_base64_encode (input); + gpr_slice expect = grpc_chttp2_huffman_compress (base64); + gpr_slice got = grpc_chttp2_base64_encode_and_huffman_compress (input); + if (0 != gpr_slice_cmp (expect, got)) + { + char *t = gpr_dump_slice (input, GPR_DUMP_HEX | GPR_DUMP_ASCII); + char *e = gpr_dump_slice (expect, GPR_DUMP_HEX | GPR_DUMP_ASCII); + char *g = gpr_dump_slice (got, GPR_DUMP_HEX | GPR_DUMP_ASCII); + gpr_log (GPR_ERROR, "FAILED:%d:\ntest: %s\ngot: %s\nwant: %s", t, g, e); + gpr_free (t); + gpr_free (e); + gpr_free (g); + all_ok = 0; + } + gpr_slice_unref (input); + gpr_slice_unref (base64); + gpr_slice_unref (expect); + gpr_slice_unref (got); } #define EXPECT_COMBINED_EQUIV(x) \ expect_combined_equiv(x, sizeof(x) - 1, __LINE__) -static void expect_binary_header(const char *hdr, int binary) { - if (grpc_is_binary_header(hdr, strlen(hdr)) != binary) { - gpr_log(GPR_ERROR, "FAILED: expected header '%s' to be %s", hdr, - binary ? "binary" : "not binary"); - all_ok = 0; - } +static void +expect_binary_header (const char *hdr, int binary) +{ + if (grpc_is_binary_header (hdr, strlen (hdr)) != binary) + { + gpr_log (GPR_ERROR, "FAILED: expected header '%s' to be %s", hdr, binary ? "binary" : "not binary"); + all_ok = 0; + } } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ /* Base64 test vectors from RFC 4648, with padding removed */ /* BASE64("") = "" */ - EXPECT_SLICE_EQ("", B64("")); + EXPECT_SLICE_EQ ("", B64 ("")); /* BASE64("f") = "Zg" */ - EXPECT_SLICE_EQ("Zg", B64("f")); + EXPECT_SLICE_EQ ("Zg", B64 ("f")); /* BASE64("fo") = "Zm8" */ - EXPECT_SLICE_EQ("Zm8", B64("fo")); + EXPECT_SLICE_EQ ("Zm8", B64 ("fo")); /* BASE64("foo") = "Zm9v" */ - EXPECT_SLICE_EQ("Zm9v", B64("foo")); + EXPECT_SLICE_EQ ("Zm9v", B64 ("foo")); /* BASE64("foob") = "Zm9vYg" */ - EXPECT_SLICE_EQ("Zm9vYg", B64("foob")); + EXPECT_SLICE_EQ ("Zm9vYg", B64 ("foob")); /* BASE64("fooba") = "Zm9vYmE" */ - EXPECT_SLICE_EQ("Zm9vYmE", B64("fooba")); + EXPECT_SLICE_EQ ("Zm9vYmE", B64 ("fooba")); /* BASE64("foobar") = "Zm9vYmFy" */ - EXPECT_SLICE_EQ("Zm9vYmFy", B64("foobar")); + EXPECT_SLICE_EQ ("Zm9vYmFy", B64 ("foobar")); - EXPECT_SLICE_EQ("wMHCw8TF", B64("\xc0\xc1\xc2\xc3\xc4\xc5")); + EXPECT_SLICE_EQ ("wMHCw8TF", B64 ("\xc0\xc1\xc2\xc3\xc4\xc5")); /* Huffman encoding tests */ - EXPECT_SLICE_EQ("\xf1\xe3\xc2\xe5\xf2\x3a\x6b\xa0\xab\x90\xf4\xff", - HUFF("www.example.com")); - EXPECT_SLICE_EQ("\xa8\xeb\x10\x64\x9c\xbf", HUFF("no-cache")); - EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f", HUFF("custom-key")); - EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xb8\xe8\xb4\xbf", HUFF("custom-value")); - EXPECT_SLICE_EQ("\xae\xc3\x77\x1a\x4b", HUFF("private")); - EXPECT_SLICE_EQ( - "\xd0\x7a\xbe\x94\x10\x54\xd4\x44\xa8\x20\x05\x95\x04\x0b\x81\x66\xe0\x82" - "\xa6\x2d\x1b\xff", - HUFF("Mon, 21 Oct 2013 20:13:21 GMT")); - EXPECT_SLICE_EQ( - "\x9d\x29\xad\x17\x18\x63\xc7\x8f\x0b\x97\xc8\xe9\xae\x82\xae\x43\xd3", - HUFF("https://www.example.com")); + EXPECT_SLICE_EQ ("\xf1\xe3\xc2\xe5\xf2\x3a\x6b\xa0\xab\x90\xf4\xff", HUFF ("www.example.com")); + EXPECT_SLICE_EQ ("\xa8\xeb\x10\x64\x9c\xbf", HUFF ("no-cache")); + EXPECT_SLICE_EQ ("\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f", HUFF ("custom-key")); + EXPECT_SLICE_EQ ("\x25\xa8\x49\xe9\x5b\xb8\xe8\xb4\xbf", HUFF ("custom-value")); + EXPECT_SLICE_EQ ("\xae\xc3\x77\x1a\x4b", HUFF ("private")); + EXPECT_SLICE_EQ ("\xd0\x7a\xbe\x94\x10\x54\xd4\x44\xa8\x20\x05\x95\x04\x0b\x81\x66\xe0\x82" "\xa6\x2d\x1b\xff", HUFF ("Mon, 21 Oct 2013 20:13:21 GMT")); + EXPECT_SLICE_EQ ("\x9d\x29\xad\x17\x18\x63\xc7\x8f\x0b\x97\xc8\xe9\xae\x82\xae\x43\xd3", HUFF ("https://www.example.com")); /* Various test vectors for combined encoding */ - EXPECT_COMBINED_EQUIV(""); - EXPECT_COMBINED_EQUIV("f"); - EXPECT_COMBINED_EQUIV("fo"); - EXPECT_COMBINED_EQUIV("foo"); - EXPECT_COMBINED_EQUIV("foob"); - EXPECT_COMBINED_EQUIV("fooba"); - EXPECT_COMBINED_EQUIV("foobar"); - EXPECT_COMBINED_EQUIV("www.example.com"); - EXPECT_COMBINED_EQUIV("no-cache"); - EXPECT_COMBINED_EQUIV("custom-key"); - EXPECT_COMBINED_EQUIV("custom-value"); - EXPECT_COMBINED_EQUIV("private"); - EXPECT_COMBINED_EQUIV("Mon, 21 Oct 2013 20:13:21 GMT"); - EXPECT_COMBINED_EQUIV("https://www.example.com"); - EXPECT_COMBINED_EQUIV( - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" - "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" - "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" - "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" - "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" - "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" - "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" - "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" - "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" - "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" - "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" - "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" - "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" - "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" - "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"); - - expect_binary_header("foo-bin", 1); - expect_binary_header("foo-bar", 0); - expect_binary_header("-bin", 0); + EXPECT_COMBINED_EQUIV (""); + EXPECT_COMBINED_EQUIV ("f"); + EXPECT_COMBINED_EQUIV ("fo"); + EXPECT_COMBINED_EQUIV ("foo"); + EXPECT_COMBINED_EQUIV ("foob"); + EXPECT_COMBINED_EQUIV ("fooba"); + EXPECT_COMBINED_EQUIV ("foobar"); + EXPECT_COMBINED_EQUIV ("www.example.com"); + EXPECT_COMBINED_EQUIV ("no-cache"); + EXPECT_COMBINED_EQUIV ("custom-key"); + EXPECT_COMBINED_EQUIV ("custom-value"); + EXPECT_COMBINED_EQUIV ("private"); + EXPECT_COMBINED_EQUIV ("Mon, 21 Oct 2013 20:13:21 GMT"); + EXPECT_COMBINED_EQUIV ("https://www.example.com"); + EXPECT_COMBINED_EQUIV ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"); + + expect_binary_header ("foo-bin", 1); + expect_binary_header ("foo-bar", 0); + expect_binary_header ("-bin", 0); return all_ok ? 0 : 1; } diff --git a/test/core/transport/chttp2/hpack_parser_test.c b/test/core/transport/chttp2/hpack_parser_test.c index 3a313375a4..2d8ac2e09a 100644 --- a/test/core/transport/chttp2/hpack_parser_test.c +++ b/test/core/transport/chttp2/hpack_parser_test.c @@ -42,182 +42,120 @@ #include "test/core/util/slice_splitter.h" #include "test/core/util/test_config.h" -typedef struct { va_list args; } test_checker; - -static void onhdr(void *ud, grpc_mdelem *md) { +typedef struct +{ + va_list args; +} test_checker; + +static void +onhdr (void *ud, grpc_mdelem * md) +{ const char *ekey, *evalue; test_checker *chk = ud; - ekey = va_arg(chk->args, char *); - GPR_ASSERT(ekey); - evalue = va_arg(chk->args, char *); - GPR_ASSERT(evalue); - GPR_ASSERT(gpr_slice_str_cmp(md->key->slice, ekey) == 0); - GPR_ASSERT(gpr_slice_str_cmp(md->value->slice, evalue) == 0); - GRPC_MDELEM_UNREF(md); + ekey = va_arg (chk->args, char *); + GPR_ASSERT (ekey); + evalue = va_arg (chk->args, char *); + GPR_ASSERT (evalue); + GPR_ASSERT (gpr_slice_str_cmp (md->key->slice, ekey) == 0); + GPR_ASSERT (gpr_slice_str_cmp (md->value->slice, evalue) == 0); + GRPC_MDELEM_UNREF (md); } -static void test_vector(grpc_chttp2_hpack_parser *parser, - grpc_slice_split_mode mode, const char *hexstring, - ... /* char *key, char *value */) { - gpr_slice input = parse_hexstring(hexstring); +static void +test_vector (grpc_chttp2_hpack_parser * parser, grpc_slice_split_mode mode, const char *hexstring, ... /* char *key, char *value */ ) +{ + gpr_slice input = parse_hexstring (hexstring); gpr_slice *slices; size_t nslices; size_t i; test_checker chk; - va_start(chk.args, hexstring); + va_start (chk.args, hexstring); parser->on_header = onhdr; parser->on_header_user_data = &chk; - grpc_split_slices(mode, &input, 1, &slices, &nslices); - gpr_slice_unref(input); + grpc_split_slices (mode, &input, 1, &slices, &nslices); + gpr_slice_unref (input); - for (i = 0; i < nslices; i++) { - GPR_ASSERT(grpc_chttp2_hpack_parser_parse( - parser, GPR_SLICE_START_PTR(slices[i]), GPR_SLICE_END_PTR(slices[i]))); - } + for (i = 0; i < nslices; i++) + { + GPR_ASSERT (grpc_chttp2_hpack_parser_parse (parser, GPR_SLICE_START_PTR (slices[i]), GPR_SLICE_END_PTR (slices[i]))); + } - for (i = 0; i < nslices; i++) { - gpr_slice_unref(slices[i]); - } - gpr_free(slices); + for (i = 0; i < nslices; i++) + { + gpr_slice_unref (slices[i]); + } + gpr_free (slices); - GPR_ASSERT(NULL == va_arg(chk.args, char *)); + GPR_ASSERT (NULL == va_arg (chk.args, char *)); - va_end(chk.args); + va_end (chk.args); } -static void test_vectors(grpc_slice_split_mode mode) { +static void +test_vectors (grpc_slice_split_mode mode) +{ grpc_chttp2_hpack_parser parser; - grpc_mdctx *mdctx = grpc_mdctx_create(); + grpc_mdctx *mdctx = grpc_mdctx_create (); - grpc_chttp2_hpack_parser_init(&parser, mdctx); + grpc_chttp2_hpack_parser_init (&parser, mdctx); /* D.2.1 */ - test_vector(&parser, mode, - "400a 6375 7374 6f6d 2d6b 6579 0d63 7573" - "746f 6d2d 6865 6164 6572", - "custom-key", "custom-header", NULL); + test_vector (&parser, mode, "400a 6375 7374 6f6d 2d6b 6579 0d63 7573" "746f 6d2d 6865 6164 6572", "custom-key", "custom-header", NULL); /* D.2.2 */ - test_vector(&parser, mode, "040c 2f73 616d 706c 652f 7061 7468", ":path", - "/sample/path", NULL); + test_vector (&parser, mode, "040c 2f73 616d 706c 652f 7061 7468", ":path", "/sample/path", NULL); /* D.2.3 */ - test_vector(&parser, mode, - "1008 7061 7373 776f 7264 0673 6563 7265" - "74", - "password", "secret", NULL); + test_vector (&parser, mode, "1008 7061 7373 776f 7264 0673 6563 7265" "74", "password", "secret", NULL); /* D.2.4 */ - test_vector(&parser, mode, "82", ":method", "GET", NULL); - grpc_chttp2_hpack_parser_destroy(&parser); + test_vector (&parser, mode, "82", ":method", "GET", NULL); + grpc_chttp2_hpack_parser_destroy (&parser); - grpc_chttp2_hpack_parser_init(&parser, mdctx); + grpc_chttp2_hpack_parser_init (&parser, mdctx); /* D.3.1 */ - test_vector(&parser, mode, - "8286 8441 0f77 7777 2e65 7861 6d70 6c65" - "2e63 6f6d", - ":method", "GET", ":scheme", "http", ":path", "/", ":authority", - "www.example.com", NULL); + test_vector (&parser, mode, "8286 8441 0f77 7777 2e65 7861 6d70 6c65" "2e63 6f6d", ":method", "GET", ":scheme", "http", ":path", "/", ":authority", "www.example.com", NULL); /* D.3.2 */ - test_vector(&parser, mode, "8286 84be 5808 6e6f 2d63 6163 6865", ":method", - "GET", ":scheme", "http", ":path", "/", ":authority", - "www.example.com", "cache-control", "no-cache", NULL); + test_vector (&parser, mode, "8286 84be 5808 6e6f 2d63 6163 6865", ":method", "GET", ":scheme", "http", ":path", "/", ":authority", "www.example.com", "cache-control", "no-cache", NULL); /* D.3.3 */ - test_vector(&parser, mode, - "8287 85bf 400a 6375 7374 6f6d 2d6b 6579" - "0c63 7573 746f 6d2d 7661 6c75 65", - ":method", "GET", ":scheme", "https", ":path", "/index.html", - ":authority", "www.example.com", "custom-key", "custom-value", - NULL); - grpc_chttp2_hpack_parser_destroy(&parser); - - grpc_chttp2_hpack_parser_init(&parser, mdctx); + test_vector (&parser, mode, "8287 85bf 400a 6375 7374 6f6d 2d6b 6579" "0c63 7573 746f 6d2d 7661 6c75 65", ":method", "GET", ":scheme", "https", ":path", "/index.html", ":authority", "www.example.com", "custom-key", "custom-value", NULL); + grpc_chttp2_hpack_parser_destroy (&parser); + + grpc_chttp2_hpack_parser_init (&parser, mdctx); /* D.4.1 */ - test_vector(&parser, mode, - "8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4" - "ff", - ":method", "GET", ":scheme", "http", ":path", "/", ":authority", - "www.example.com", NULL); + test_vector (&parser, mode, "8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4" "ff", ":method", "GET", ":scheme", "http", ":path", "/", ":authority", "www.example.com", NULL); /* D.4.2 */ - test_vector(&parser, mode, "8286 84be 5886 a8eb 1064 9cbf", ":method", "GET", - ":scheme", "http", ":path", "/", ":authority", "www.example.com", - "cache-control", "no-cache", NULL); + test_vector (&parser, mode, "8286 84be 5886 a8eb 1064 9cbf", ":method", "GET", ":scheme", "http", ":path", "/", ":authority", "www.example.com", "cache-control", "no-cache", NULL); /* D.4.3 */ - test_vector(&parser, mode, - "8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925" - "a849 e95b b8e8 b4bf", - ":method", "GET", ":scheme", "https", ":path", "/index.html", - ":authority", "www.example.com", "custom-key", "custom-value", - NULL); - grpc_chttp2_hpack_parser_destroy(&parser); - - grpc_chttp2_hpack_parser_init(&parser, mdctx); + test_vector (&parser, mode, "8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925" "a849 e95b b8e8 b4bf", ":method", "GET", ":scheme", "https", ":path", "/index.html", ":authority", "www.example.com", "custom-key", "custom-value", NULL); + grpc_chttp2_hpack_parser_destroy (&parser); + + grpc_chttp2_hpack_parser_init (&parser, mdctx); parser.table.max_bytes = 256; /* D.5.1 */ - test_vector(&parser, mode, - "4803 3330 3258 0770 7269 7661 7465 611d" - "4d6f 6e2c 2032 3120 4f63 7420 3230 3133" - "2032 303a 3133 3a32 3120 474d 546e 1768" - "7474 7073 3a2f 2f77 7777 2e65 7861 6d70" - "6c65 2e63 6f6d", - ":status", "302", "cache-control", "private", "date", - "Mon, 21 Oct 2013 20:13:21 GMT", "location", - "https://www.example.com", NULL); + test_vector (&parser, mode, "4803 3330 3258 0770 7269 7661 7465 611d" "4d6f 6e2c 2032 3120 4f63 7420 3230 3133" "2032 303a 3133 3a32 3120 474d 546e 1768" "7474 7073 3a2f 2f77 7777 2e65 7861 6d70" "6c65 2e63 6f6d", ":status", "302", "cache-control", "private", "date", "Mon, 21 Oct 2013 20:13:21 GMT", "location", "https://www.example.com", NULL); /* D.5.2 */ - test_vector(&parser, mode, "4803 3330 37c1 c0bf", ":status", "307", - "cache-control", "private", "date", - "Mon, 21 Oct 2013 20:13:21 GMT", "location", - "https://www.example.com", NULL); + test_vector (&parser, mode, "4803 3330 37c1 c0bf", ":status", "307", "cache-control", "private", "date", "Mon, 21 Oct 2013 20:13:21 GMT", "location", "https://www.example.com", NULL); /* D.5.3 */ - test_vector(&parser, mode, - "88c1 611d 4d6f 6e2c 2032 3120 4f63 7420" - "3230 3133 2032 303a 3133 3a32 3220 474d" - "54c0 5a04 677a 6970 7738 666f 6f3d 4153" - "444a 4b48 514b 425a 584f 5157 454f 5049" - "5541 5851 5745 4f49 553b 206d 6178 2d61" - "6765 3d33 3630 303b 2076 6572 7369 6f6e" - "3d31", - ":status", "200", "cache-control", "private", "date", - "Mon, 21 Oct 2013 20:13:22 GMT", "location", - "https://www.example.com", "content-encoding", "gzip", - "set-cookie", - "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", NULL); - grpc_chttp2_hpack_parser_destroy(&parser); - - grpc_chttp2_hpack_parser_init(&parser, mdctx); + test_vector (&parser, mode, "88c1 611d 4d6f 6e2c 2032 3120 4f63 7420" "3230 3133 2032 303a 3133 3a32 3220 474d" "54c0 5a04 677a 6970 7738 666f 6f3d 4153" "444a 4b48 514b 425a 584f 5157 454f 5049" "5541 5851 5745 4f49 553b 206d 6178 2d61" "6765 3d33 3630 303b 2076 6572 7369 6f6e" "3d31", ":status", "200", "cache-control", "private", "date", "Mon, 21 Oct 2013 20:13:22 GMT", "location", "https://www.example.com", "content-encoding", "gzip", "set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", NULL); + grpc_chttp2_hpack_parser_destroy (&parser); + + grpc_chttp2_hpack_parser_init (&parser, mdctx); parser.table.max_bytes = 256; /* D.6.1 */ - test_vector(&parser, mode, - "4882 6402 5885 aec3 771a 4b61 96d0 7abe" - "9410 54d4 44a8 2005 9504 0b81 66e0 82a6" - "2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8" - "e9ae 82ae 43d3", - ":status", "302", "cache-control", "private", "date", - "Mon, 21 Oct 2013 20:13:21 GMT", "location", - "https://www.example.com", NULL); + test_vector (&parser, mode, "4882 6402 5885 aec3 771a 4b61 96d0 7abe" "9410 54d4 44a8 2005 9504 0b81 66e0 82a6" "2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8" "e9ae 82ae 43d3", ":status", "302", "cache-control", "private", "date", "Mon, 21 Oct 2013 20:13:21 GMT", "location", "https://www.example.com", NULL); /* D.6.2 */ - test_vector(&parser, mode, "4883 640e ffc1 c0bf", ":status", "307", - "cache-control", "private", "date", - "Mon, 21 Oct 2013 20:13:21 GMT", "location", - "https://www.example.com", NULL); + test_vector (&parser, mode, "4883 640e ffc1 c0bf", ":status", "307", "cache-control", "private", "date", "Mon, 21 Oct 2013 20:13:21 GMT", "location", "https://www.example.com", NULL); /* D.6.3 */ - test_vector(&parser, mode, - "88c1 6196 d07a be94 1054 d444 a820 0595" - "040b 8166 e084 a62d 1bff c05a 839b d9ab" - "77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b" - "3960 d5af 2708 7f36 72c1 ab27 0fb5 291f" - "9587 3160 65c0 03ed 4ee5 b106 3d50 07", - ":status", "200", "cache-control", "private", "date", - "Mon, 21 Oct 2013 20:13:22 GMT", "location", - "https://www.example.com", "content-encoding", "gzip", - "set-cookie", - "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", NULL); - grpc_chttp2_hpack_parser_destroy(&parser); - grpc_mdctx_unref(mdctx); + test_vector (&parser, mode, "88c1 6196 d07a be94 1054 d444 a820 0595" "040b 8166 e084 a62d 1bff c05a 839b d9ab" "77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b" "3960 d5af 2708 7f36 72c1 ab27 0fb5 291f" "9587 3160 65c0 03ed 4ee5 b106 3d50 07", ":status", "200", "cache-control", "private", "date", "Mon, 21 Oct 2013 20:13:22 GMT", "location", "https://www.example.com", "content-encoding", "gzip", "set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", NULL); + grpc_chttp2_hpack_parser_destroy (&parser); + grpc_mdctx_unref (mdctx); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_vectors(GRPC_SLICE_SPLIT_MERGE_ALL); - test_vectors(GRPC_SLICE_SPLIT_ONE_BYTE); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_vectors (GRPC_SLICE_SPLIT_MERGE_ALL); + test_vectors (GRPC_SLICE_SPLIT_ONE_BYTE); return 0; } diff --git a/test/core/transport/chttp2/hpack_table_test.c b/test/core/transport/chttp2/hpack_table_test.c index aa3e273a6c..cf64105a94 100644 --- a/test/core/transport/chttp2/hpack_table_test.c +++ b/test/core/transport/chttp2/hpack_table_test.c @@ -44,232 +44,245 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) -static void assert_str(const grpc_chttp2_hptbl *tbl, grpc_mdstr *mdstr, - const char *str) { - GPR_ASSERT(gpr_slice_str_cmp(mdstr->slice, str) == 0); +static void +assert_str (const grpc_chttp2_hptbl * tbl, grpc_mdstr * mdstr, const char *str) +{ + GPR_ASSERT (gpr_slice_str_cmp (mdstr->slice, str) == 0); } -static void assert_index(const grpc_chttp2_hptbl *tbl, gpr_uint32 idx, - const char *key, const char *value) { - grpc_mdelem *md = grpc_chttp2_hptbl_lookup(tbl, idx); - assert_str(tbl, md->key, key); - assert_str(tbl, md->value, value); +static void +assert_index (const grpc_chttp2_hptbl * tbl, gpr_uint32 idx, const char *key, const char *value) +{ + grpc_mdelem *md = grpc_chttp2_hptbl_lookup (tbl, idx); + assert_str (tbl, md->key, key); + assert_str (tbl, md->value, value); } -static void test_static_lookup(void) { +static void +test_static_lookup (void) +{ grpc_chttp2_hptbl tbl; grpc_mdctx *mdctx; - mdctx = grpc_mdctx_create(); - grpc_chttp2_hptbl_init(&tbl, mdctx); - - LOG_TEST("test_static_lookup"); - assert_index(&tbl, 1, ":authority", ""); - assert_index(&tbl, 2, ":method", "GET"); - assert_index(&tbl, 3, ":method", "POST"); - assert_index(&tbl, 4, ":path", "/"); - assert_index(&tbl, 5, ":path", "/index.html"); - assert_index(&tbl, 6, ":scheme", "http"); - assert_index(&tbl, 7, ":scheme", "https"); - assert_index(&tbl, 8, ":status", "200"); - assert_index(&tbl, 9, ":status", "204"); - assert_index(&tbl, 10, ":status", "206"); - assert_index(&tbl, 11, ":status", "304"); - assert_index(&tbl, 12, ":status", "400"); - assert_index(&tbl, 13, ":status", "404"); - assert_index(&tbl, 14, ":status", "500"); - assert_index(&tbl, 15, "accept-charset", ""); - assert_index(&tbl, 16, "accept-encoding", "gzip, deflate"); - assert_index(&tbl, 17, "accept-language", ""); - assert_index(&tbl, 18, "accept-ranges", ""); - assert_index(&tbl, 19, "accept", ""); - assert_index(&tbl, 20, "access-control-allow-origin", ""); - assert_index(&tbl, 21, "age", ""); - assert_index(&tbl, 22, "allow", ""); - assert_index(&tbl, 23, "authorization", ""); - assert_index(&tbl, 24, "cache-control", ""); - assert_index(&tbl, 25, "content-disposition", ""); - assert_index(&tbl, 26, "content-encoding", ""); - assert_index(&tbl, 27, "content-language", ""); - assert_index(&tbl, 28, "content-length", ""); - assert_index(&tbl, 29, "content-location", ""); - assert_index(&tbl, 30, "content-range", ""); - assert_index(&tbl, 31, "content-type", ""); - assert_index(&tbl, 32, "cookie", ""); - assert_index(&tbl, 33, "date", ""); - assert_index(&tbl, 34, "etag", ""); - assert_index(&tbl, 35, "expect", ""); - assert_index(&tbl, 36, "expires", ""); - assert_index(&tbl, 37, "from", ""); - assert_index(&tbl, 38, "host", ""); - assert_index(&tbl, 39, "if-match", ""); - assert_index(&tbl, 40, "if-modified-since", ""); - assert_index(&tbl, 41, "if-none-match", ""); - assert_index(&tbl, 42, "if-range", ""); - assert_index(&tbl, 43, "if-unmodified-since", ""); - assert_index(&tbl, 44, "last-modified", ""); - assert_index(&tbl, 45, "link", ""); - assert_index(&tbl, 46, "location", ""); - assert_index(&tbl, 47, "max-forwards", ""); - assert_index(&tbl, 48, "proxy-authenticate", ""); - assert_index(&tbl, 49, "proxy-authorization", ""); - assert_index(&tbl, 50, "range", ""); - assert_index(&tbl, 51, "referer", ""); - assert_index(&tbl, 52, "refresh", ""); - assert_index(&tbl, 53, "retry-after", ""); - assert_index(&tbl, 54, "server", ""); - assert_index(&tbl, 55, "set-cookie", ""); - assert_index(&tbl, 56, "strict-transport-security", ""); - assert_index(&tbl, 57, "transfer-encoding", ""); - assert_index(&tbl, 58, "user-agent", ""); - assert_index(&tbl, 59, "vary", ""); - assert_index(&tbl, 60, "via", ""); - assert_index(&tbl, 61, "www-authenticate", ""); - - grpc_chttp2_hptbl_destroy(&tbl); - grpc_mdctx_unref(mdctx); + mdctx = grpc_mdctx_create (); + grpc_chttp2_hptbl_init (&tbl, mdctx); + + LOG_TEST ("test_static_lookup"); + assert_index (&tbl, 1, ":authority", ""); + assert_index (&tbl, 2, ":method", "GET"); + assert_index (&tbl, 3, ":method", "POST"); + assert_index (&tbl, 4, ":path", "/"); + assert_index (&tbl, 5, ":path", "/index.html"); + assert_index (&tbl, 6, ":scheme", "http"); + assert_index (&tbl, 7, ":scheme", "https"); + assert_index (&tbl, 8, ":status", "200"); + assert_index (&tbl, 9, ":status", "204"); + assert_index (&tbl, 10, ":status", "206"); + assert_index (&tbl, 11, ":status", "304"); + assert_index (&tbl, 12, ":status", "400"); + assert_index (&tbl, 13, ":status", "404"); + assert_index (&tbl, 14, ":status", "500"); + assert_index (&tbl, 15, "accept-charset", ""); + assert_index (&tbl, 16, "accept-encoding", "gzip, deflate"); + assert_index (&tbl, 17, "accept-language", ""); + assert_index (&tbl, 18, "accept-ranges", ""); + assert_index (&tbl, 19, "accept", ""); + assert_index (&tbl, 20, "access-control-allow-origin", ""); + assert_index (&tbl, 21, "age", ""); + assert_index (&tbl, 22, "allow", ""); + assert_index (&tbl, 23, "authorization", ""); + assert_index (&tbl, 24, "cache-control", ""); + assert_index (&tbl, 25, "content-disposition", ""); + assert_index (&tbl, 26, "content-encoding", ""); + assert_index (&tbl, 27, "content-language", ""); + assert_index (&tbl, 28, "content-length", ""); + assert_index (&tbl, 29, "content-location", ""); + assert_index (&tbl, 30, "content-range", ""); + assert_index (&tbl, 31, "content-type", ""); + assert_index (&tbl, 32, "cookie", ""); + assert_index (&tbl, 33, "date", ""); + assert_index (&tbl, 34, "etag", ""); + assert_index (&tbl, 35, "expect", ""); + assert_index (&tbl, 36, "expires", ""); + assert_index (&tbl, 37, "from", ""); + assert_index (&tbl, 38, "host", ""); + assert_index (&tbl, 39, "if-match", ""); + assert_index (&tbl, 40, "if-modified-since", ""); + assert_index (&tbl, 41, "if-none-match", ""); + assert_index (&tbl, 42, "if-range", ""); + assert_index (&tbl, 43, "if-unmodified-since", ""); + assert_index (&tbl, 44, "last-modified", ""); + assert_index (&tbl, 45, "link", ""); + assert_index (&tbl, 46, "location", ""); + assert_index (&tbl, 47, "max-forwards", ""); + assert_index (&tbl, 48, "proxy-authenticate", ""); + assert_index (&tbl, 49, "proxy-authorization", ""); + assert_index (&tbl, 50, "range", ""); + assert_index (&tbl, 51, "referer", ""); + assert_index (&tbl, 52, "refresh", ""); + assert_index (&tbl, 53, "retry-after", ""); + assert_index (&tbl, 54, "server", ""); + assert_index (&tbl, 55, "set-cookie", ""); + assert_index (&tbl, 56, "strict-transport-security", ""); + assert_index (&tbl, 57, "transfer-encoding", ""); + assert_index (&tbl, 58, "user-agent", ""); + assert_index (&tbl, 59, "vary", ""); + assert_index (&tbl, 60, "via", ""); + assert_index (&tbl, 61, "www-authenticate", ""); + + grpc_chttp2_hptbl_destroy (&tbl); + grpc_mdctx_unref (mdctx); } -static void test_many_additions(void) { +static void +test_many_additions (void) +{ grpc_chttp2_hptbl tbl; int i; char *key; char *value; grpc_mdctx *mdctx; - LOG_TEST("test_many_additions"); - - mdctx = grpc_mdctx_create(); - grpc_chttp2_hptbl_init(&tbl, mdctx); - - for (i = 0; i < 1000000; i++) { - gpr_asprintf(&key, "K:%d", i); - gpr_asprintf(&value, "VALUE:%d", i); - grpc_chttp2_hptbl_add(&tbl, grpc_mdelem_from_strings(mdctx, key, value)); - assert_index(&tbl, 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); - gpr_free(key); - gpr_free(value); - if (i) { - gpr_asprintf(&key, "K:%d", i - 1); - gpr_asprintf(&value, "VALUE:%d", i - 1); - assert_index(&tbl, 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); - gpr_free(key); - gpr_free(value); + LOG_TEST ("test_many_additions"); + + mdctx = grpc_mdctx_create (); + grpc_chttp2_hptbl_init (&tbl, mdctx); + + for (i = 0; i < 1000000; i++) + { + gpr_asprintf (&key, "K:%d", i); + gpr_asprintf (&value, "VALUE:%d", i); + grpc_chttp2_hptbl_add (&tbl, grpc_mdelem_from_strings (mdctx, key, value)); + assert_index (&tbl, 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); + gpr_free (key); + gpr_free (value); + if (i) + { + gpr_asprintf (&key, "K:%d", i - 1); + gpr_asprintf (&value, "VALUE:%d", i - 1); + assert_index (&tbl, 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); + gpr_free (key); + gpr_free (value); + } } - } - grpc_chttp2_hptbl_destroy(&tbl); - grpc_mdctx_unref(mdctx); + grpc_chttp2_hptbl_destroy (&tbl); + grpc_mdctx_unref (mdctx); } -static grpc_chttp2_hptbl_find_result find_simple(grpc_chttp2_hptbl *tbl, - const char *key, - const char *value) { - grpc_mdelem *md = grpc_mdelem_from_strings(tbl->mdctx, key, value); - grpc_chttp2_hptbl_find_result r = grpc_chttp2_hptbl_find(tbl, md); - GRPC_MDELEM_UNREF(md); +static grpc_chttp2_hptbl_find_result +find_simple (grpc_chttp2_hptbl * tbl, const char *key, const char *value) +{ + grpc_mdelem *md = grpc_mdelem_from_strings (tbl->mdctx, key, value); + grpc_chttp2_hptbl_find_result r = grpc_chttp2_hptbl_find (tbl, md); + GRPC_MDELEM_UNREF (md); return r; } -static void test_find(void) { +static void +test_find (void) +{ grpc_chttp2_hptbl tbl; int i; char buffer[32]; grpc_mdctx *mdctx; grpc_chttp2_hptbl_find_result r; - LOG_TEST("test_find"); + LOG_TEST ("test_find"); - mdctx = grpc_mdctx_create(); - grpc_chttp2_hptbl_init(&tbl, mdctx); - grpc_chttp2_hptbl_add(&tbl, grpc_mdelem_from_strings(mdctx, "abc", "xyz")); - grpc_chttp2_hptbl_add(&tbl, grpc_mdelem_from_strings(mdctx, "abc", "123")); - grpc_chttp2_hptbl_add(&tbl, grpc_mdelem_from_strings(mdctx, "x", "1")); + mdctx = grpc_mdctx_create (); + grpc_chttp2_hptbl_init (&tbl, mdctx); + grpc_chttp2_hptbl_add (&tbl, grpc_mdelem_from_strings (mdctx, "abc", "xyz")); + grpc_chttp2_hptbl_add (&tbl, grpc_mdelem_from_strings (mdctx, "abc", "123")); + grpc_chttp2_hptbl_add (&tbl, grpc_mdelem_from_strings (mdctx, "x", "1")); - r = find_simple(&tbl, "abc", "123"); - GPR_ASSERT(r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 1); + r = find_simple (&tbl, "abc", "123"); + GPR_ASSERT (r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 1); - r = find_simple(&tbl, "abc", "xyz"); - GPR_ASSERT(r.index == 3 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 1); + r = find_simple (&tbl, "abc", "xyz"); + GPR_ASSERT (r.index == 3 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 1); - r = find_simple(&tbl, "x", "1"); - GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 1); + r = find_simple (&tbl, "x", "1"); + GPR_ASSERT (r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 1); - r = find_simple(&tbl, "x", "2"); - GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 0); + r = find_simple (&tbl, "x", "2"); + GPR_ASSERT (r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 0); - r = find_simple(&tbl, "vary", "some-vary-arg"); - GPR_ASSERT(r.index == 59); - GPR_ASSERT(r.has_value == 0); + r = find_simple (&tbl, "vary", "some-vary-arg"); + GPR_ASSERT (r.index == 59); + GPR_ASSERT (r.has_value == 0); - r = find_simple(&tbl, "accept-encoding", "gzip, deflate"); - GPR_ASSERT(r.index == 16); - GPR_ASSERT(r.has_value == 1); + r = find_simple (&tbl, "accept-encoding", "gzip, deflate"); + GPR_ASSERT (r.index == 16); + GPR_ASSERT (r.has_value == 1); - r = find_simple(&tbl, "accept-encoding", "gzip"); - GPR_ASSERT(r.index == 16); - GPR_ASSERT(r.has_value == 0); + r = find_simple (&tbl, "accept-encoding", "gzip"); + GPR_ASSERT (r.index == 16); + GPR_ASSERT (r.has_value == 0); - r = find_simple(&tbl, ":method", "GET"); - GPR_ASSERT(r.index == 2); - GPR_ASSERT(r.has_value == 1); + r = find_simple (&tbl, ":method", "GET"); + GPR_ASSERT (r.index == 2); + GPR_ASSERT (r.has_value == 1); - r = find_simple(&tbl, ":method", "POST"); - GPR_ASSERT(r.index == 3); - GPR_ASSERT(r.has_value == 1); + r = find_simple (&tbl, ":method", "POST"); + GPR_ASSERT (r.index == 3); + GPR_ASSERT (r.has_value == 1); - r = find_simple(&tbl, ":method", "PUT"); - GPR_ASSERT(r.index == 2 || r.index == 3); - GPR_ASSERT(r.has_value == 0); + r = find_simple (&tbl, ":method", "PUT"); + GPR_ASSERT (r.index == 2 || r.index == 3); + GPR_ASSERT (r.has_value == 0); - r = find_simple(&tbl, "this-does-not-exist", ""); - GPR_ASSERT(r.index == 0); - GPR_ASSERT(r.has_value == 0); + r = find_simple (&tbl, "this-does-not-exist", ""); + GPR_ASSERT (r.index == 0); + GPR_ASSERT (r.has_value == 0); /* overflow the string buffer, check find still works */ - for (i = 0; i < 10000; i++) { - gpr_ltoa(i, buffer); - grpc_chttp2_hptbl_add(&tbl, - grpc_mdelem_from_strings(mdctx, "test", buffer)); - } - - r = find_simple(&tbl, "abc", "123"); - GPR_ASSERT(r.index == 0); - GPR_ASSERT(r.has_value == 0); - - r = find_simple(&tbl, "test", "9999"); - GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 1); - - r = find_simple(&tbl, "test", "9998"); - GPR_ASSERT(r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 1); - - for (i = 0; i < tbl.num_ents; i++) { - int expect = 9999 - i; - gpr_ltoa(expect, buffer); - - r = find_simple(&tbl, "test", buffer); - GPR_ASSERT(r.index == i + 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); - GPR_ASSERT(r.has_value == 1); - } - - r = find_simple(&tbl, "test", "10000"); - GPR_ASSERT(r.index != 0); - GPR_ASSERT(r.has_value == 0); - - grpc_chttp2_hptbl_destroy(&tbl); - grpc_mdctx_unref(mdctx); + for (i = 0; i < 10000; i++) + { + gpr_ltoa (i, buffer); + grpc_chttp2_hptbl_add (&tbl, grpc_mdelem_from_strings (mdctx, "test", buffer)); + } + + r = find_simple (&tbl, "abc", "123"); + GPR_ASSERT (r.index == 0); + GPR_ASSERT (r.has_value == 0); + + r = find_simple (&tbl, "test", "9999"); + GPR_ASSERT (r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 1); + + r = find_simple (&tbl, "test", "9998"); + GPR_ASSERT (r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 1); + + for (i = 0; i < tbl.num_ents; i++) + { + int expect = 9999 - i; + gpr_ltoa (expect, buffer); + + r = find_simple (&tbl, "test", buffer); + GPR_ASSERT (r.index == i + 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY); + GPR_ASSERT (r.has_value == 1); + } + + r = find_simple (&tbl, "test", "10000"); + GPR_ASSERT (r.index != 0); + GPR_ASSERT (r.has_value == 0); + + grpc_chttp2_hptbl_destroy (&tbl); + grpc_mdctx_unref (mdctx); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_static_lookup(); - test_many_additions(); - test_find(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_static_lookup (); + test_many_additions (); + test_find (); return 0; } diff --git a/test/core/transport/chttp2/status_conversion_test.c b/test/core/transport/chttp2/status_conversion_test.c index e2729a0a19..9b8fb28c12 100644 --- a/test/core/transport/chttp2/status_conversion_test.c +++ b/test/core/transport/chttp2/status_conversion_test.c @@ -44,95 +44,81 @@ #define HTTP2_STATUS_TO_GRPC_STATUS(a, b) \ GPR_ASSERT(grpc_chttp2_http2_status_to_grpc_status(a) == (b)) -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ int i; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_OK, GRPC_CHTTP2_NO_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_CANCELLED, GRPC_CHTTP2_CANCEL); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNKNOWN, GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_INVALID_ARGUMENT, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_DEADLINE_EXCEEDED, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_NOT_FOUND, GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_ALREADY_EXISTS, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_PERMISSION_DENIED, - GRPC_CHTTP2_INADEQUATE_SECURITY); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNAUTHENTICATED, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_RESOURCE_EXHAUSTED, - GRPC_CHTTP2_ENHANCE_YOUR_CALM); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_FAILED_PRECONDITION, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_ABORTED, GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_OUT_OF_RANGE, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNIMPLEMENTED, - GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_INTERNAL, GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNAVAILABLE, - GRPC_CHTTP2_REFUSED_STREAM); - GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_DATA_LOSS, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_OK, GRPC_CHTTP2_NO_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_CANCELLED, GRPC_CHTTP2_CANCEL); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_UNKNOWN, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_INVALID_ARGUMENT, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_DEADLINE_EXCEEDED, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_NOT_FOUND, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_ALREADY_EXISTS, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_PERMISSION_DENIED, GRPC_CHTTP2_INADEQUATE_SECURITY); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_UNAUTHENTICATED, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_RESOURCE_EXHAUSTED, GRPC_CHTTP2_ENHANCE_YOUR_CALM); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_FAILED_PRECONDITION, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_ABORTED, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_OUT_OF_RANGE, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_UNIMPLEMENTED, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_INTERNAL, GRPC_CHTTP2_INTERNAL_ERROR); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_UNAVAILABLE, GRPC_CHTTP2_REFUSED_STREAM); + GRPC_STATUS_TO_HTTP2_ERROR (GRPC_STATUS_DATA_LOSS, GRPC_CHTTP2_INTERNAL_ERROR); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_OK, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_CANCELLED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNKNOWN, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_INVALID_ARGUMENT, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_DEADLINE_EXCEEDED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_NOT_FOUND, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_ALREADY_EXISTS, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_PERMISSION_DENIED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNAUTHENTICATED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_RESOURCE_EXHAUSTED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_FAILED_PRECONDITION, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_ABORTED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_OUT_OF_RANGE, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNIMPLEMENTED, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_INTERNAL, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNAVAILABLE, 200); - GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_DATA_LOSS, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_OK, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_CANCELLED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_UNKNOWN, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_INVALID_ARGUMENT, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_DEADLINE_EXCEEDED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_NOT_FOUND, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_ALREADY_EXISTS, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_PERMISSION_DENIED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_UNAUTHENTICATED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_RESOURCE_EXHAUSTED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_FAILED_PRECONDITION, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_ABORTED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_OUT_OF_RANGE, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_UNIMPLEMENTED, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_INTERNAL, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_UNAVAILABLE, 200); + GRPC_STATUS_TO_HTTP2_STATUS (GRPC_STATUS_DATA_LOSS, 200); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_NO_ERROR, GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_PROTOCOL_ERROR, GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_INTERNAL_ERROR, GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_FLOW_CONTROL_ERROR, - GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_SETTINGS_TIMEOUT, - GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_STREAM_CLOSED, GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_FRAME_SIZE_ERROR, - GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_REFUSED_STREAM, - GRPC_STATUS_UNAVAILABLE); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_CANCEL, GRPC_STATUS_CANCELLED); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_COMPRESSION_ERROR, - GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_CONNECT_ERROR, GRPC_STATUS_INTERNAL); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_ENHANCE_YOUR_CALM, - GRPC_STATUS_RESOURCE_EXHAUSTED); - HTTP2_ERROR_TO_GRPC_STATUS(GRPC_CHTTP2_INADEQUATE_SECURITY, - GRPC_STATUS_PERMISSION_DENIED); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_NO_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_PROTOCOL_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_INTERNAL_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_FLOW_CONTROL_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_SETTINGS_TIMEOUT, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_STREAM_CLOSED, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_FRAME_SIZE_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_REFUSED_STREAM, GRPC_STATUS_UNAVAILABLE); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_CANCEL, GRPC_STATUS_CANCELLED); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_COMPRESSION_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_CONNECT_ERROR, GRPC_STATUS_INTERNAL); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_ENHANCE_YOUR_CALM, GRPC_STATUS_RESOURCE_EXHAUSTED); + HTTP2_ERROR_TO_GRPC_STATUS (GRPC_CHTTP2_INADEQUATE_SECURITY, GRPC_STATUS_PERMISSION_DENIED); - HTTP2_STATUS_TO_GRPC_STATUS(200, GRPC_STATUS_OK); - HTTP2_STATUS_TO_GRPC_STATUS(400, GRPC_STATUS_INVALID_ARGUMENT); - HTTP2_STATUS_TO_GRPC_STATUS(401, GRPC_STATUS_UNAUTHENTICATED); - HTTP2_STATUS_TO_GRPC_STATUS(403, GRPC_STATUS_PERMISSION_DENIED); - HTTP2_STATUS_TO_GRPC_STATUS(404, GRPC_STATUS_NOT_FOUND); - HTTP2_STATUS_TO_GRPC_STATUS(409, GRPC_STATUS_ABORTED); - HTTP2_STATUS_TO_GRPC_STATUS(412, GRPC_STATUS_FAILED_PRECONDITION); - HTTP2_STATUS_TO_GRPC_STATUS(429, GRPC_STATUS_RESOURCE_EXHAUSTED); - HTTP2_STATUS_TO_GRPC_STATUS(499, GRPC_STATUS_CANCELLED); - HTTP2_STATUS_TO_GRPC_STATUS(500, GRPC_STATUS_UNKNOWN); - HTTP2_STATUS_TO_GRPC_STATUS(503, GRPC_STATUS_UNAVAILABLE); - HTTP2_STATUS_TO_GRPC_STATUS(504, GRPC_STATUS_DEADLINE_EXCEEDED); + HTTP2_STATUS_TO_GRPC_STATUS (200, GRPC_STATUS_OK); + HTTP2_STATUS_TO_GRPC_STATUS (400, GRPC_STATUS_INVALID_ARGUMENT); + HTTP2_STATUS_TO_GRPC_STATUS (401, GRPC_STATUS_UNAUTHENTICATED); + HTTP2_STATUS_TO_GRPC_STATUS (403, GRPC_STATUS_PERMISSION_DENIED); + HTTP2_STATUS_TO_GRPC_STATUS (404, GRPC_STATUS_NOT_FOUND); + HTTP2_STATUS_TO_GRPC_STATUS (409, GRPC_STATUS_ABORTED); + HTTP2_STATUS_TO_GRPC_STATUS (412, GRPC_STATUS_FAILED_PRECONDITION); + HTTP2_STATUS_TO_GRPC_STATUS (429, GRPC_STATUS_RESOURCE_EXHAUSTED); + HTTP2_STATUS_TO_GRPC_STATUS (499, GRPC_STATUS_CANCELLED); + HTTP2_STATUS_TO_GRPC_STATUS (500, GRPC_STATUS_UNKNOWN); + HTTP2_STATUS_TO_GRPC_STATUS (503, GRPC_STATUS_UNAVAILABLE); + HTTP2_STATUS_TO_GRPC_STATUS (504, GRPC_STATUS_DEADLINE_EXCEEDED); /* check all status values can be converted */ - for (i = 0; i <= 999; i++) { - grpc_chttp2_http2_status_to_grpc_status(i); - } + for (i = 0; i <= 999; i++) + { + grpc_chttp2_http2_status_to_grpc_status (i); + } return 0; } diff --git a/test/core/transport/chttp2/stream_encoder_test.c b/test/core/transport/chttp2/stream_encoder_test.c index 71db98c69e..452b1184a5 100644 --- a/test/core/transport/chttp2/stream_encoder_test.c +++ b/test/core/transport/chttp2/stream_encoder_test.c @@ -55,217 +55,241 @@ void **to_delete = NULL; size_t num_to_delete = 0; size_t cap_to_delete = 0; -static gpr_slice create_test_slice(size_t length) { - gpr_slice slice = gpr_slice_malloc(length); +static gpr_slice +create_test_slice (size_t length) +{ + gpr_slice slice = gpr_slice_malloc (length); size_t i; - for (i = 0; i < length; i++) { - GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; - } + for (i = 0; i < length; i++) + { + GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; + } return slice; } /* verify that the output generated by encoding the stream matches the hexstring passed in */ -static void verify_sopb(size_t window_available, int eof, - size_t expect_window_used, const char *expected) { +static void +verify_sopb (size_t window_available, int eof, size_t expect_window_used, const char *expected) +{ gpr_slice_buffer output; grpc_stream_op_buffer encops; gpr_slice merged; - gpr_slice expect = parse_hexstring(expected); - gpr_slice_buffer_init(&output); - grpc_sopb_init(&encops); - GPR_ASSERT(expect_window_used == - grpc_chttp2_preencode(g_sopb.ops, &g_sopb.nops, - (gpr_uint32)window_available, &encops)); - grpc_chttp2_encode(encops.ops, encops.nops, eof, 0xdeadbeef, &g_compressor, - &output); + gpr_slice expect = parse_hexstring (expected); + gpr_slice_buffer_init (&output); + grpc_sopb_init (&encops); + GPR_ASSERT (expect_window_used == grpc_chttp2_preencode (g_sopb.ops, &g_sopb.nops, (gpr_uint32) window_available, &encops)); + grpc_chttp2_encode (encops.ops, encops.nops, eof, 0xdeadbeef, &g_compressor, &output); encops.nops = 0; - merged = grpc_slice_merge(output.slices, output.count); - gpr_slice_buffer_destroy(&output); - grpc_sopb_destroy(&encops); - - if (0 != gpr_slice_cmp(merged, expect)) { - char *expect_str = gpr_dump_slice(expect, GPR_DUMP_HEX | GPR_DUMP_ASCII); - char *got_str = gpr_dump_slice(merged, GPR_DUMP_HEX | GPR_DUMP_ASCII); - gpr_log(GPR_ERROR, "mismatched output for %s", expected); - gpr_log(GPR_ERROR, "EXPECT: %s", expect_str); - gpr_log(GPR_ERROR, "GOT: %s", got_str); - gpr_free(expect_str); - gpr_free(got_str); - g_failure = 1; - } - - gpr_slice_unref(merged); - gpr_slice_unref(expect); + merged = grpc_slice_merge (output.slices, output.count); + gpr_slice_buffer_destroy (&output); + grpc_sopb_destroy (&encops); + + if (0 != gpr_slice_cmp (merged, expect)) + { + char *expect_str = gpr_dump_slice (expect, GPR_DUMP_HEX | GPR_DUMP_ASCII); + char *got_str = gpr_dump_slice (merged, GPR_DUMP_HEX | GPR_DUMP_ASCII); + gpr_log (GPR_ERROR, "mismatched output for %s", expected); + gpr_log (GPR_ERROR, "EXPECT: %s", expect_str); + gpr_log (GPR_ERROR, "GOT: %s", got_str); + gpr_free (expect_str); + gpr_free (got_str); + g_failure = 1; + } + + gpr_slice_unref (merged); + gpr_slice_unref (expect); } -static void test_small_data_framing(void) { - grpc_sopb_add_no_op(&g_sopb); - verify_sopb(10, 0, 0, ""); +static void +test_small_data_framing (void) +{ + grpc_sopb_add_no_op (&g_sopb); + verify_sopb (10, 0, 0, ""); - grpc_sopb_add_slice(&g_sopb, create_test_slice(3)); - verify_sopb(10, 0, 3, "000003 0000 deadbeef 000102"); + grpc_sopb_add_slice (&g_sopb, create_test_slice (3)); + verify_sopb (10, 0, 3, "000003 0000 deadbeef 000102"); - grpc_sopb_add_slice(&g_sopb, create_test_slice(4)); - verify_sopb(10, 0, 4, "000004 0000 deadbeef 00010203"); + grpc_sopb_add_slice (&g_sopb, create_test_slice (4)); + verify_sopb (10, 0, 4, "000004 0000 deadbeef 00010203"); - grpc_sopb_add_slice(&g_sopb, create_test_slice(3)); - grpc_sopb_add_slice(&g_sopb, create_test_slice(4)); - verify_sopb(10, 0, 7, "000007 0000 deadbeef 000102 00010203"); + grpc_sopb_add_slice (&g_sopb, create_test_slice (3)); + grpc_sopb_add_slice (&g_sopb, create_test_slice (4)); + verify_sopb (10, 0, 7, "000007 0000 deadbeef 000102 00010203"); - grpc_sopb_add_slice(&g_sopb, create_test_slice(0)); - grpc_sopb_add_slice(&g_sopb, create_test_slice(0)); - grpc_sopb_add_slice(&g_sopb, create_test_slice(0)); - grpc_sopb_add_slice(&g_sopb, create_test_slice(0)); - grpc_sopb_add_slice(&g_sopb, create_test_slice(3)); - verify_sopb(10, 0, 3, "000003 0000 deadbeef 000102"); + grpc_sopb_add_slice (&g_sopb, create_test_slice (0)); + grpc_sopb_add_slice (&g_sopb, create_test_slice (0)); + grpc_sopb_add_slice (&g_sopb, create_test_slice (0)); + grpc_sopb_add_slice (&g_sopb, create_test_slice (0)); + grpc_sopb_add_slice (&g_sopb, create_test_slice (3)); + verify_sopb (10, 0, 3, "000003 0000 deadbeef 000102"); - verify_sopb(10, 1, 0, "000000 0001 deadbeef"); + verify_sopb (10, 1, 0, "000000 0001 deadbeef"); - grpc_sopb_add_begin_message(&g_sopb, 255, 0); - verify_sopb(10, 0, 5, "000005 0000 deadbeef 00000000ff"); + grpc_sopb_add_begin_message (&g_sopb, 255, 0); + verify_sopb (10, 0, 5, "000005 0000 deadbeef 00000000ff"); } -static void add_sopb_headers(size_t n, ...) { +static void +add_sopb_headers (size_t n, ...) +{ size_t i; grpc_metadata_batch b; va_list l; - grpc_linked_mdelem *e = gpr_malloc(sizeof(*e) * n); - - grpc_metadata_batch_init(&b); - - va_start(l, n); - for (i = 0; i < n; i++) { - char *key = va_arg(l, char *); - char *value = va_arg(l, char *); - if (i) { - e[i - 1].next = &e[i]; - e[i].prev = &e[i - 1]; + grpc_linked_mdelem *e = gpr_malloc (sizeof (*e) * n); + + grpc_metadata_batch_init (&b); + + va_start (l, n); + for (i = 0; i < n; i++) + { + char *key = va_arg (l, char *); + char *value = va_arg (l, char *); + if (i) + { + e[i - 1].next = &e[i]; + e[i].prev = &e[i - 1]; + } + e[i].md = grpc_mdelem_from_strings (g_mdctx, key, value); } - e[i].md = grpc_mdelem_from_strings(g_mdctx, key, value); - } e[0].prev = NULL; e[n - 1].next = NULL; - va_end(l); + va_end (l); b.list.head = &e[0]; b.list.tail = &e[n - 1]; - if (cap_to_delete == num_to_delete) { - cap_to_delete = GPR_MAX(2 * cap_to_delete, 1000); - to_delete = gpr_realloc(to_delete, sizeof(*to_delete) * cap_to_delete); - } + if (cap_to_delete == num_to_delete) + { + cap_to_delete = GPR_MAX (2 * cap_to_delete, 1000); + to_delete = gpr_realloc (to_delete, sizeof (*to_delete) * cap_to_delete); + } to_delete[num_to_delete++] = e; - grpc_sopb_add_metadata(&g_sopb, b); + grpc_sopb_add_metadata (&g_sopb, b); } -static void test_basic_headers(void) { +static void +test_basic_headers (void) +{ int i; - add_sopb_headers(1, "a", "a"); - verify_sopb(0, 0, 0, "000005 0104 deadbeef 40 0161 0161"); + add_sopb_headers (1, "a", "a"); + verify_sopb (0, 0, 0, "000005 0104 deadbeef 40 0161 0161"); - add_sopb_headers(1, "a", "a"); - verify_sopb(0, 0, 0, "000001 0104 deadbeef be"); + add_sopb_headers (1, "a", "a"); + verify_sopb (0, 0, 0, "000001 0104 deadbeef be"); - add_sopb_headers(1, "a", "a"); - verify_sopb(0, 0, 0, "000001 0104 deadbeef be"); + add_sopb_headers (1, "a", "a"); + verify_sopb (0, 0, 0, "000001 0104 deadbeef be"); - add_sopb_headers(2, "a", "a", "b", "c"); - verify_sopb(0, 0, 0, "000006 0104 deadbeef be 40 0162 0163"); + add_sopb_headers (2, "a", "a", "b", "c"); + verify_sopb (0, 0, 0, "000006 0104 deadbeef be 40 0162 0163"); - add_sopb_headers(2, "a", "a", "b", "c"); - verify_sopb(0, 0, 0, "000002 0104 deadbeef bf be"); + add_sopb_headers (2, "a", "a", "b", "c"); + verify_sopb (0, 0, 0, "000002 0104 deadbeef bf be"); - add_sopb_headers(1, "a", "d"); - verify_sopb(0, 0, 0, "000004 0104 deadbeef 7f 00 0164"); + add_sopb_headers (1, "a", "d"); + verify_sopb (0, 0, 0, "000004 0104 deadbeef 7f 00 0164"); /* flush out what's there to make a few values look very popular */ - for (i = 0; i < 350; i++) { - add_sopb_headers(3, "a", "a", "b", "c", "a", "d"); - verify_sopb(0, 0, 0, "000003 0104 deadbeef c0 bf be"); - } + for (i = 0; i < 350; i++) + { + add_sopb_headers (3, "a", "a", "b", "c", "a", "d"); + verify_sopb (0, 0, 0, "000003 0104 deadbeef c0 bf be"); + } - add_sopb_headers(2, "a", "a", "k", "v"); - verify_sopb(0, 0, 0, "000006 0104 deadbeef c0 00 016b 0176"); + add_sopb_headers (2, "a", "a", "k", "v"); + verify_sopb (0, 0, 0, "000006 0104 deadbeef c0 00 016b 0176"); - add_sopb_headers(1, "a", "v"); + add_sopb_headers (1, "a", "v"); /* this could be 000004 0104 deadbeef 0f 30 0176 also */ - verify_sopb(0, 0, 0, "000004 0104 deadbeef 0f 2f 0176"); + verify_sopb (0, 0, 0, "000004 0104 deadbeef 0f 2f 0176"); } -static void encode_int_to_str(int i, char *p) { - p[0] = (char)('a' + i % 26); +static void +encode_int_to_str (int i, char *p) +{ + p[0] = (char) ('a' + i % 26); i /= 26; - GPR_ASSERT(i < 26); - p[1] = (char)('a' + i); + GPR_ASSERT (i < 26); + p[1] = (char) ('a' + i); p[2] = 0; } -static void test_decode_table_overflow(void) { +static void +test_decode_table_overflow (void) +{ int i; char key[3], value[3]; char *expect; - for (i = 0; i < 114; i++) { - if (i > 0) { - add_sopb_headers(1, "aa", "ba"); + for (i = 0; i < 114; i++) + { + if (i > 0) + { + add_sopb_headers (1, "aa", "ba"); + } + + encode_int_to_str (i, key); + encode_int_to_str (i + 1, value); + + if (i + 61 >= 127) + { + gpr_asprintf (&expect, "000002 0104 deadbeef ff%02x 000007 0104 deadbeef 40 " "02%02x%02x 02%02x%02x", i + 61 - 127, key[0], key[1], value[0], value[1]); + } + else if (i > 0) + { + gpr_asprintf (&expect, "000001 0104 deadbeef %02x 000007 0104 deadbeef 40 " "02%02x%02x 02%02x%02x", 0x80 + 61 + i, key[0], key[1], value[0], value[1]); + } + else + { + gpr_asprintf (&expect, "000007 0104 deadbeef 40 02%02x%02x 02%02x%02x", key[0], key[1], value[0], value[1]); + } + + add_sopb_headers (1, key, value); + verify_sopb (0, 0, 0, expect); + gpr_free (expect); } - encode_int_to_str(i, key); - encode_int_to_str(i + 1, value); - - if (i + 61 >= 127) { - gpr_asprintf(&expect, - "000002 0104 deadbeef ff%02x 000007 0104 deadbeef 40 " - "02%02x%02x 02%02x%02x", - i + 61 - 127, key[0], key[1], value[0], value[1]); - } else if (i > 0) { - gpr_asprintf(&expect, - "000001 0104 deadbeef %02x 000007 0104 deadbeef 40 " - "02%02x%02x 02%02x%02x", - 0x80 + 61 + i, key[0], key[1], value[0], value[1]); - } else { - gpr_asprintf(&expect, "000007 0104 deadbeef 40 02%02x%02x 02%02x%02x", - key[0], key[1], value[0], value[1]); - } - - add_sopb_headers(1, key, value); - verify_sopb(0, 0, 0, expect); - gpr_free(expect); - } - /* if the above passes, then we must have just knocked this pair out of the decoder stack, and so we'll be forced to re-encode it */ - add_sopb_headers(1, "aa", "ba"); - verify_sopb(0, 0, 0, "000007 0104 deadbeef 40 026161 026261"); + add_sopb_headers (1, "aa", "ba"); + verify_sopb (0, 0, 0, "000007 0104 deadbeef 40 026161 026261"); } -static void randstr(char *p, int bufsz) { +static void +randstr (char *p, int bufsz) +{ int i; - int len = 1 + rand() % bufsz; - for (i = 0; i < len; i++) { - p[i] = (char)('a' + rand() % 26); - } + int len = 1 + rand () % bufsz; + for (i = 0; i < len; i++) + { + p[i] = (char) ('a' + rand () % 26); + } p[len] = 0; } -typedef struct { +typedef struct +{ char key[300]; char value[300]; int got_hdr; } test_decode_random_header_state; -static void chk_hdr(void *p, grpc_mdelem *el) { +static void +chk_hdr (void *p, grpc_mdelem * el) +{ test_decode_random_header_state *st = p; - GPR_ASSERT(0 == gpr_slice_str_cmp(el->key->slice, st->key)); - GPR_ASSERT(0 == gpr_slice_str_cmp(el->value->slice, st->value)); + GPR_ASSERT (0 == gpr_slice_str_cmp (el->key->slice, st->key)); + GPR_ASSERT (0 == gpr_slice_str_cmp (el->value->slice, st->value)); st->got_hdr = 1; - GRPC_MDELEM_UNREF(el); + GRPC_MDELEM_UNREF (el); } -static void test_decode_random_headers_inner(int max_len) { +static void +test_decode_random_headers_inner (int max_len) +{ int i; test_decode_random_header_state st; gpr_slice_buffer output; @@ -273,37 +297,35 @@ static void test_decode_random_headers_inner(int max_len) { grpc_stream_op_buffer encops; grpc_chttp2_hpack_parser parser; - grpc_chttp2_hpack_parser_init(&parser, g_mdctx); - grpc_sopb_init(&encops); - - gpr_log(GPR_INFO, "max_len = %d", max_len); - - for (i = 0; i < 10000; i++) { - randstr(st.key, max_len); - randstr(st.value, max_len); - - add_sopb_headers(1, st.key, st.value); - gpr_slice_buffer_init(&output); - GPR_ASSERT(0 == - grpc_chttp2_preencode(g_sopb.ops, &g_sopb.nops, 0, &encops)); - grpc_chttp2_encode(encops.ops, encops.nops, 0, 0xdeadbeef, &g_compressor, - &output); - encops.nops = 0; - merged = grpc_slice_merge(output.slices, output.count); - gpr_slice_buffer_destroy(&output); - - st.got_hdr = 0; - parser.on_header = chk_hdr; - parser.on_header_user_data = &st; - grpc_chttp2_hpack_parser_parse(&parser, GPR_SLICE_START_PTR(merged) + 9, - GPR_SLICE_END_PTR(merged)); - GPR_ASSERT(st.got_hdr); - - gpr_slice_unref(merged); - } - - grpc_chttp2_hpack_parser_destroy(&parser); - grpc_sopb_destroy(&encops); + grpc_chttp2_hpack_parser_init (&parser, g_mdctx); + grpc_sopb_init (&encops); + + gpr_log (GPR_INFO, "max_len = %d", max_len); + + for (i = 0; i < 10000; i++) + { + randstr (st.key, max_len); + randstr (st.value, max_len); + + add_sopb_headers (1, st.key, st.value); + gpr_slice_buffer_init (&output); + GPR_ASSERT (0 == grpc_chttp2_preencode (g_sopb.ops, &g_sopb.nops, 0, &encops)); + grpc_chttp2_encode (encops.ops, encops.nops, 0, 0xdeadbeef, &g_compressor, &output); + encops.nops = 0; + merged = grpc_slice_merge (output.slices, output.count); + gpr_slice_buffer_destroy (&output); + + st.got_hdr = 0; + parser.on_header = chk_hdr; + parser.on_header_user_data = &st; + grpc_chttp2_hpack_parser_parse (&parser, GPR_SLICE_START_PTR (merged) + 9, GPR_SLICE_END_PTR (merged)); + GPR_ASSERT (st.got_hdr); + + gpr_slice_unref (merged); + } + + grpc_chttp2_hpack_parser_destroy (&parser); + grpc_sopb_destroy (&encops); } #define DECL_TEST_DECODE_RANDOM_HEADERS(n) \ @@ -312,48 +334,53 @@ static void test_decode_random_headers_inner(int max_len) { } \ int keeps_formatting_correct_##n -DECL_TEST_DECODE_RANDOM_HEADERS(1); -DECL_TEST_DECODE_RANDOM_HEADERS(2); -DECL_TEST_DECODE_RANDOM_HEADERS(3); -DECL_TEST_DECODE_RANDOM_HEADERS(5); -DECL_TEST_DECODE_RANDOM_HEADERS(8); -DECL_TEST_DECODE_RANDOM_HEADERS(13); -DECL_TEST_DECODE_RANDOM_HEADERS(21); -DECL_TEST_DECODE_RANDOM_HEADERS(34); -DECL_TEST_DECODE_RANDOM_HEADERS(55); -DECL_TEST_DECODE_RANDOM_HEADERS(89); -DECL_TEST_DECODE_RANDOM_HEADERS(144); - -static void run_test(void (*test)(), const char *name) { - gpr_log(GPR_INFO, "RUN TEST: %s", name); - g_mdctx = grpc_mdctx_create_with_seed(0); - grpc_chttp2_hpack_compressor_init(&g_compressor, g_mdctx); - grpc_sopb_init(&g_sopb); - test(); - grpc_chttp2_hpack_compressor_destroy(&g_compressor); - grpc_mdctx_unref(g_mdctx); - grpc_sopb_destroy(&g_sopb); +DECL_TEST_DECODE_RANDOM_HEADERS (1); +DECL_TEST_DECODE_RANDOM_HEADERS (2); +DECL_TEST_DECODE_RANDOM_HEADERS (3); +DECL_TEST_DECODE_RANDOM_HEADERS (5); +DECL_TEST_DECODE_RANDOM_HEADERS (8); +DECL_TEST_DECODE_RANDOM_HEADERS (13); +DECL_TEST_DECODE_RANDOM_HEADERS (21); +DECL_TEST_DECODE_RANDOM_HEADERS (34); +DECL_TEST_DECODE_RANDOM_HEADERS (55); +DECL_TEST_DECODE_RANDOM_HEADERS (89); +DECL_TEST_DECODE_RANDOM_HEADERS (144); + +static void +run_test (void (*test) (), const char *name) +{ + gpr_log (GPR_INFO, "RUN TEST: %s", name); + g_mdctx = grpc_mdctx_create_with_seed (0); + grpc_chttp2_hpack_compressor_init (&g_compressor, g_mdctx); + grpc_sopb_init (&g_sopb); + test (); + grpc_chttp2_hpack_compressor_destroy (&g_compressor); + grpc_mdctx_unref (g_mdctx); + grpc_sopb_destroy (&g_sopb); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ size_t i; - grpc_test_init(argc, argv); - TEST(test_small_data_framing); - TEST(test_basic_headers); - TEST(test_decode_table_overflow); - TEST(test_decode_random_headers_1); - TEST(test_decode_random_headers_2); - TEST(test_decode_random_headers_3); - TEST(test_decode_random_headers_5); - TEST(test_decode_random_headers_8); - TEST(test_decode_random_headers_13); - TEST(test_decode_random_headers_21); - TEST(test_decode_random_headers_34); - TEST(test_decode_random_headers_55); - TEST(test_decode_random_headers_89); - TEST(test_decode_random_headers_144); - for (i = 0; i < num_to_delete; i++) { - gpr_free(to_delete[i]); - } + grpc_test_init (argc, argv); + TEST (test_small_data_framing); + TEST (test_basic_headers); + TEST (test_decode_table_overflow); + TEST (test_decode_random_headers_1); + TEST (test_decode_random_headers_2); + TEST (test_decode_random_headers_3); + TEST (test_decode_random_headers_5); + TEST (test_decode_random_headers_8); + TEST (test_decode_random_headers_13); + TEST (test_decode_random_headers_21); + TEST (test_decode_random_headers_34); + TEST (test_decode_random_headers_55); + TEST (test_decode_random_headers_89); + TEST (test_decode_random_headers_144); + for (i = 0; i < num_to_delete; i++) + { + gpr_free (to_delete[i]); + } return g_failure; } diff --git a/test/core/transport/chttp2/stream_map_test.c b/test/core/transport/chttp2/stream_map_test.c index 81fb80f84f..87efab5fa5 100644 --- a/test/core/transport/chttp2/stream_map_test.c +++ b/test/core/transport/chttp2/stream_map_test.c @@ -38,193 +38,228 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) /* test creation & destruction */ -static void test_no_op(void) { +static void +test_no_op (void) +{ grpc_chttp2_stream_map map; - LOG_TEST("test_no_op"); + LOG_TEST ("test_no_op"); - grpc_chttp2_stream_map_init(&map, 8); - grpc_chttp2_stream_map_destroy(&map); + grpc_chttp2_stream_map_init (&map, 8); + grpc_chttp2_stream_map_destroy (&map); } /* test lookup on an empty map */ -static void test_empty_find(void) { +static void +test_empty_find (void) +{ grpc_chttp2_stream_map map; - LOG_TEST("test_empty_find"); + LOG_TEST ("test_empty_find"); - grpc_chttp2_stream_map_init(&map, 8); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, 39128)); - grpc_chttp2_stream_map_destroy(&map); + grpc_chttp2_stream_map_init (&map, 8); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, 39128)); + grpc_chttp2_stream_map_destroy (&map); } /* test it's safe to delete twice */ -static void test_double_deletion(void) { +static void +test_double_deletion (void) +{ grpc_chttp2_stream_map map; - LOG_TEST("test_double_deletion"); - - grpc_chttp2_stream_map_init(&map, 8); - GPR_ASSERT(0 == grpc_chttp2_stream_map_size(&map)); - grpc_chttp2_stream_map_add(&map, 1, (void *)1); - GPR_ASSERT((void *)1 == grpc_chttp2_stream_map_find(&map, 1)); - GPR_ASSERT(1 == grpc_chttp2_stream_map_size(&map)); - GPR_ASSERT((void *)1 == grpc_chttp2_stream_map_delete(&map, 1)); - GPR_ASSERT(0 == grpc_chttp2_stream_map_size(&map)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, 1)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_delete(&map, 1)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, 1)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_delete(&map, 1)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, 1)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_delete(&map, 1)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, 1)); - grpc_chttp2_stream_map_destroy(&map); + LOG_TEST ("test_double_deletion"); + + grpc_chttp2_stream_map_init (&map, 8); + GPR_ASSERT (0 == grpc_chttp2_stream_map_size (&map)); + grpc_chttp2_stream_map_add (&map, 1, (void *) 1); + GPR_ASSERT ((void *) 1 == grpc_chttp2_stream_map_find (&map, 1)); + GPR_ASSERT (1 == grpc_chttp2_stream_map_size (&map)); + GPR_ASSERT ((void *) 1 == grpc_chttp2_stream_map_delete (&map, 1)); + GPR_ASSERT (0 == grpc_chttp2_stream_map_size (&map)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, 1)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_delete (&map, 1)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, 1)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_delete (&map, 1)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, 1)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_delete (&map, 1)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, 1)); + grpc_chttp2_stream_map_destroy (&map); } /* test add & lookup */ -static void test_basic_add_find(gpr_uint32 n) { +static void +test_basic_add_find (gpr_uint32 n) +{ grpc_chttp2_stream_map map; gpr_uint32 i; size_t got; - LOG_TEST("test_basic_add_find"); - gpr_log(GPR_INFO, "n = %d", n); - - grpc_chttp2_stream_map_init(&map, 8); - GPR_ASSERT(0 == grpc_chttp2_stream_map_size(&map)); - for (i = 1; i <= n; i++) { - grpc_chttp2_stream_map_add(&map, i, (void *)(gpr_uintptr)i); - } - GPR_ASSERT(n == grpc_chttp2_stream_map_size(&map)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, 0)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(&map, n + 1)); - for (i = 1; i <= n; i++) { - got = (gpr_uintptr)grpc_chttp2_stream_map_find(&map, i); - GPR_ASSERT(i == got); - } - grpc_chttp2_stream_map_destroy(&map); + LOG_TEST ("test_basic_add_find"); + gpr_log (GPR_INFO, "n = %d", n); + + grpc_chttp2_stream_map_init (&map, 8); + GPR_ASSERT (0 == grpc_chttp2_stream_map_size (&map)); + for (i = 1; i <= n; i++) + { + grpc_chttp2_stream_map_add (&map, i, (void *) (gpr_uintptr) i); + } + GPR_ASSERT (n == grpc_chttp2_stream_map_size (&map)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, 0)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (&map, n + 1)); + for (i = 1; i <= n; i++) + { + got = (gpr_uintptr) grpc_chttp2_stream_map_find (&map, i); + GPR_ASSERT (i == got); + } + grpc_chttp2_stream_map_destroy (&map); } /* verify that for_each gets the right values during test_delete_evens_XXX */ -static void verify_for_each(void *user_data, gpr_uint32 stream_id, void *ptr) { +static void +verify_for_each (void *user_data, gpr_uint32 stream_id, void *ptr) +{ gpr_uint32 *for_each_check = user_data; - GPR_ASSERT(ptr); - GPR_ASSERT(*for_each_check == stream_id); + GPR_ASSERT (ptr); + GPR_ASSERT (*for_each_check == stream_id); *for_each_check += 2; } -static void check_delete_evens(grpc_chttp2_stream_map *map, gpr_uint32 n) { +static void +check_delete_evens (grpc_chttp2_stream_map * map, gpr_uint32 n) +{ gpr_uint32 for_each_check = 1; gpr_uint32 i; size_t got; - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(map, 0)); - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(map, n + 1)); - for (i = 1; i <= n; i++) { - if (i & 1) { - got = (gpr_uintptr)grpc_chttp2_stream_map_find(map, i); - GPR_ASSERT(i == got); - } else { - GPR_ASSERT(NULL == grpc_chttp2_stream_map_find(map, i)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (map, 0)); + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (map, n + 1)); + for (i = 1; i <= n; i++) + { + if (i & 1) + { + got = (gpr_uintptr) grpc_chttp2_stream_map_find (map, i); + GPR_ASSERT (i == got); + } + else + { + GPR_ASSERT (NULL == grpc_chttp2_stream_map_find (map, i)); + } + } + + grpc_chttp2_stream_map_for_each (map, verify_for_each, &for_each_check); + if (n & 1) + { + GPR_ASSERT (for_each_check == n + 2); + } + else + { + GPR_ASSERT (for_each_check == n + 1); } - } - - grpc_chttp2_stream_map_for_each(map, verify_for_each, &for_each_check); - if (n & 1) { - GPR_ASSERT(for_each_check == n + 2); - } else { - GPR_ASSERT(for_each_check == n + 1); - } } /* add a bunch of keys, delete the even ones, and make sure the map is consistent */ -static void test_delete_evens_sweep(gpr_uint32 n) { +static void +test_delete_evens_sweep (gpr_uint32 n) +{ grpc_chttp2_stream_map map; gpr_uint32 i; - LOG_TEST("test_delete_evens_sweep"); - gpr_log(GPR_INFO, "n = %d", n); - - grpc_chttp2_stream_map_init(&map, 8); - for (i = 1; i <= n; i++) { - grpc_chttp2_stream_map_add(&map, i, (void *)(gpr_uintptr)i); - } - for (i = 1; i <= n; i++) { - if ((i & 1) == 0) { - GPR_ASSERT((void *)(gpr_uintptr)i == - grpc_chttp2_stream_map_delete(&map, i)); + LOG_TEST ("test_delete_evens_sweep"); + gpr_log (GPR_INFO, "n = %d", n); + + grpc_chttp2_stream_map_init (&map, 8); + for (i = 1; i <= n; i++) + { + grpc_chttp2_stream_map_add (&map, i, (void *) (gpr_uintptr) i); + } + for (i = 1; i <= n; i++) + { + if ((i & 1) == 0) + { + GPR_ASSERT ((void *) (gpr_uintptr) i == grpc_chttp2_stream_map_delete (&map, i)); + } } - } - check_delete_evens(&map, n); - grpc_chttp2_stream_map_destroy(&map); + check_delete_evens (&map, n); + grpc_chttp2_stream_map_destroy (&map); } /* add a bunch of keys, delete the even ones immediately, and make sure the map is consistent */ -static void test_delete_evens_incremental(gpr_uint32 n) { +static void +test_delete_evens_incremental (gpr_uint32 n) +{ grpc_chttp2_stream_map map; gpr_uint32 i; - LOG_TEST("test_delete_evens_incremental"); - gpr_log(GPR_INFO, "n = %d", n); - - grpc_chttp2_stream_map_init(&map, 8); - for (i = 1; i <= n; i++) { - grpc_chttp2_stream_map_add(&map, i, (void *)(gpr_uintptr)i); - if ((i & 1) == 0) { - grpc_chttp2_stream_map_delete(&map, i); + LOG_TEST ("test_delete_evens_incremental"); + gpr_log (GPR_INFO, "n = %d", n); + + grpc_chttp2_stream_map_init (&map, 8); + for (i = 1; i <= n; i++) + { + grpc_chttp2_stream_map_add (&map, i, (void *) (gpr_uintptr) i); + if ((i & 1) == 0) + { + grpc_chttp2_stream_map_delete (&map, i); + } } - } - check_delete_evens(&map, n); - grpc_chttp2_stream_map_destroy(&map); + check_delete_evens (&map, n); + grpc_chttp2_stream_map_destroy (&map); } /* add a bunch of keys, delete old ones after some time, ensure the backing array does not grow */ -static void test_periodic_compaction(gpr_uint32 n) { +static void +test_periodic_compaction (gpr_uint32 n) +{ grpc_chttp2_stream_map map; gpr_uint32 i; gpr_uint32 del; - LOG_TEST("test_periodic_compaction"); - gpr_log(GPR_INFO, "n = %d", n); - - grpc_chttp2_stream_map_init(&map, 16); - GPR_ASSERT(map.capacity == 16); - for (i = 1; i <= n; i++) { - grpc_chttp2_stream_map_add(&map, i, (void *)(gpr_uintptr)i); - if (i > 8) { - del = i - 8; - GPR_ASSERT((void *)(gpr_uintptr)del == - grpc_chttp2_stream_map_delete(&map, del)); + LOG_TEST ("test_periodic_compaction"); + gpr_log (GPR_INFO, "n = %d", n); + + grpc_chttp2_stream_map_init (&map, 16); + GPR_ASSERT (map.capacity == 16); + for (i = 1; i <= n; i++) + { + grpc_chttp2_stream_map_add (&map, i, (void *) (gpr_uintptr) i); + if (i > 8) + { + del = i - 8; + GPR_ASSERT ((void *) (gpr_uintptr) del == grpc_chttp2_stream_map_delete (&map, del)); + } } - } - GPR_ASSERT(map.capacity == 16); - grpc_chttp2_stream_map_destroy(&map); + GPR_ASSERT (map.capacity == 16); + grpc_chttp2_stream_map_destroy (&map); } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ gpr_uint32 n = 1; gpr_uint32 prev = 1; gpr_uint32 tmp; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); - test_no_op(); - test_empty_find(); - test_double_deletion(); + test_no_op (); + test_empty_find (); + test_double_deletion (); - while (n < 100000) { - test_basic_add_find(n); - test_delete_evens_sweep(n); - test_delete_evens_incremental(n); - test_periodic_compaction(n); + while (n < 100000) + { + test_basic_add_find (n); + test_delete_evens_sweep (n); + test_delete_evens_incremental (n); + test_periodic_compaction (n); - tmp = n; - n += prev; - prev = tmp; - } + tmp = n; + n += prev; + prev = tmp; + } return 0; } diff --git a/test/core/transport/chttp2/timeout_encoding_test.c b/test/core/transport/chttp2/timeout_encoding_test.c index ba6c3191f1..949bd3fae6 100644 --- a/test/core/transport/chttp2/timeout_encoding_test.c +++ b/test/core/transport/chttp2/timeout_encoding_test.c @@ -45,109 +45,122 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) -static void assert_encodes_as(gpr_timespec ts, const char *s) { +static void +assert_encodes_as (gpr_timespec ts, const char *s) +{ char buffer[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE]; - grpc_chttp2_encode_timeout(ts, buffer); - gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s); - GPR_ASSERT(0 == strcmp(buffer, s)); + grpc_chttp2_encode_timeout (ts, buffer); + gpr_log (GPR_INFO, "check '%s' == '%s'", buffer, s); + GPR_ASSERT (0 == strcmp (buffer, s)); } -void test_encoding(void) { - LOG_TEST("test_encoding"); - assert_encodes_as(gpr_time_from_micros(-1, GPR_TIMESPAN), "1n"); - assert_encodes_as(gpr_time_from_seconds(-10, GPR_TIMESPAN), "1n"); - assert_encodes_as(gpr_time_from_nanos(10, GPR_TIMESPAN), "10n"); - assert_encodes_as(gpr_time_from_nanos(999999999, GPR_TIMESPAN), "1S"); - assert_encodes_as(gpr_time_from_micros(1, GPR_TIMESPAN), "1u"); - assert_encodes_as(gpr_time_from_micros(10, GPR_TIMESPAN), "10u"); - assert_encodes_as(gpr_time_from_micros(100, GPR_TIMESPAN), "100u"); - assert_encodes_as(gpr_time_from_micros(890, GPR_TIMESPAN), "890u"); - assert_encodes_as(gpr_time_from_micros(900, GPR_TIMESPAN), "900u"); - assert_encodes_as(gpr_time_from_micros(901, GPR_TIMESPAN), "901u"); - assert_encodes_as(gpr_time_from_millis(1, GPR_TIMESPAN), "1m"); - assert_encodes_as(gpr_time_from_millis(2, GPR_TIMESPAN), "2m"); - assert_encodes_as(gpr_time_from_micros(10001, GPR_TIMESPAN), "10100u"); - assert_encodes_as(gpr_time_from_micros(999999, GPR_TIMESPAN), "1S"); - assert_encodes_as(gpr_time_from_millis(1000, GPR_TIMESPAN), "1S"); - assert_encodes_as(gpr_time_from_millis(2000, GPR_TIMESPAN), "2S"); - assert_encodes_as(gpr_time_from_millis(2500, GPR_TIMESPAN), "2500m"); - assert_encodes_as(gpr_time_from_millis(59900, GPR_TIMESPAN), "59900m"); - assert_encodes_as(gpr_time_from_seconds(50, GPR_TIMESPAN), "50S"); - assert_encodes_as(gpr_time_from_seconds(59, GPR_TIMESPAN), "59S"); - assert_encodes_as(gpr_time_from_seconds(60, GPR_TIMESPAN), "1M"); - assert_encodes_as(gpr_time_from_seconds(80, GPR_TIMESPAN), "80S"); - assert_encodes_as(gpr_time_from_seconds(90, GPR_TIMESPAN), "90S"); - assert_encodes_as(gpr_time_from_minutes(2, GPR_TIMESPAN), "2M"); - assert_encodes_as(gpr_time_from_minutes(20, GPR_TIMESPAN), "20M"); - assert_encodes_as(gpr_time_from_hours(1, GPR_TIMESPAN), "1H"); - assert_encodes_as(gpr_time_from_hours(10, GPR_TIMESPAN), "10H"); - assert_encodes_as(gpr_time_from_seconds(1000000000, GPR_TIMESPAN), - "1000000000S"); +void +test_encoding (void) +{ + LOG_TEST ("test_encoding"); + assert_encodes_as (gpr_time_from_micros (-1, GPR_TIMESPAN), "1n"); + assert_encodes_as (gpr_time_from_seconds (-10, GPR_TIMESPAN), "1n"); + assert_encodes_as (gpr_time_from_nanos (10, GPR_TIMESPAN), "10n"); + assert_encodes_as (gpr_time_from_nanos (999999999, GPR_TIMESPAN), "1S"); + assert_encodes_as (gpr_time_from_micros (1, GPR_TIMESPAN), "1u"); + assert_encodes_as (gpr_time_from_micros (10, GPR_TIMESPAN), "10u"); + assert_encodes_as (gpr_time_from_micros (100, GPR_TIMESPAN), "100u"); + assert_encodes_as (gpr_time_from_micros (890, GPR_TIMESPAN), "890u"); + assert_encodes_as (gpr_time_from_micros (900, GPR_TIMESPAN), "900u"); + assert_encodes_as (gpr_time_from_micros (901, GPR_TIMESPAN), "901u"); + assert_encodes_as (gpr_time_from_millis (1, GPR_TIMESPAN), "1m"); + assert_encodes_as (gpr_time_from_millis (2, GPR_TIMESPAN), "2m"); + assert_encodes_as (gpr_time_from_micros (10001, GPR_TIMESPAN), "10100u"); + assert_encodes_as (gpr_time_from_micros (999999, GPR_TIMESPAN), "1S"); + assert_encodes_as (gpr_time_from_millis (1000, GPR_TIMESPAN), "1S"); + assert_encodes_as (gpr_time_from_millis (2000, GPR_TIMESPAN), "2S"); + assert_encodes_as (gpr_time_from_millis (2500, GPR_TIMESPAN), "2500m"); + assert_encodes_as (gpr_time_from_millis (59900, GPR_TIMESPAN), "59900m"); + assert_encodes_as (gpr_time_from_seconds (50, GPR_TIMESPAN), "50S"); + assert_encodes_as (gpr_time_from_seconds (59, GPR_TIMESPAN), "59S"); + assert_encodes_as (gpr_time_from_seconds (60, GPR_TIMESPAN), "1M"); + assert_encodes_as (gpr_time_from_seconds (80, GPR_TIMESPAN), "80S"); + assert_encodes_as (gpr_time_from_seconds (90, GPR_TIMESPAN), "90S"); + assert_encodes_as (gpr_time_from_minutes (2, GPR_TIMESPAN), "2M"); + assert_encodes_as (gpr_time_from_minutes (20, GPR_TIMESPAN), "20M"); + assert_encodes_as (gpr_time_from_hours (1, GPR_TIMESPAN), "1H"); + assert_encodes_as (gpr_time_from_hours (10, GPR_TIMESPAN), "10H"); + assert_encodes_as (gpr_time_from_seconds (1000000000, GPR_TIMESPAN), "1000000000S"); } -static void assert_decodes_as(const char *buffer, gpr_timespec expected) { +static void +assert_decodes_as (const char *buffer, gpr_timespec expected) +{ gpr_timespec got; - gpr_log(GPR_INFO, "check decoding '%s'", buffer); - GPR_ASSERT(1 == grpc_chttp2_decode_timeout(buffer, &got)); - GPR_ASSERT(0 == gpr_time_cmp(got, expected)); + gpr_log (GPR_INFO, "check decoding '%s'", buffer); + GPR_ASSERT (1 == grpc_chttp2_decode_timeout (buffer, &got)); + GPR_ASSERT (0 == gpr_time_cmp (got, expected)); } -void decode_suite(char ext, - gpr_timespec (*answer)(long x, gpr_clock_type clock)) { - long test_vals[] = {1, 12, 123, 1234, 12345, 123456, - 1234567, 12345678, 123456789, 98765432, 9876543, 987654, - 98765, 9876, 987, 98, 9}; +void +decode_suite (char ext, gpr_timespec (*answer) (long x, gpr_clock_type clock)) +{ + long test_vals[] = { 1, 12, 123, 1234, 12345, 123456, + 1234567, 12345678, 123456789, 98765432, 9876543, 987654, + 98765, 9876, 987, 98, 9 + }; unsigned i; char *input; - for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) { - gpr_asprintf(&input, "%ld%c", test_vals[i], ext); - assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN)); - gpr_free(input); + for (i = 0; i < GPR_ARRAY_SIZE (test_vals); i++) + { + gpr_asprintf (&input, "%ld%c", test_vals[i], ext); + assert_decodes_as (input, answer (test_vals[i], GPR_TIMESPAN)); + gpr_free (input); - gpr_asprintf(&input, " %ld%c", test_vals[i], ext); - assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN)); - gpr_free(input); + gpr_asprintf (&input, " %ld%c", test_vals[i], ext); + assert_decodes_as (input, answer (test_vals[i], GPR_TIMESPAN)); + gpr_free (input); - gpr_asprintf(&input, "%ld %c", test_vals[i], ext); - assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN)); - gpr_free(input); + gpr_asprintf (&input, "%ld %c", test_vals[i], ext); + assert_decodes_as (input, answer (test_vals[i], GPR_TIMESPAN)); + gpr_free (input); - gpr_asprintf(&input, "%ld %c ", test_vals[i], ext); - assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN)); - gpr_free(input); - } + gpr_asprintf (&input, "%ld %c ", test_vals[i], ext); + assert_decodes_as (input, answer (test_vals[i], GPR_TIMESPAN)); + gpr_free (input); + } } -void test_decoding(void) { - LOG_TEST("test_decoding"); - decode_suite('n', gpr_time_from_nanos); - decode_suite('u', gpr_time_from_micros); - decode_suite('m', gpr_time_from_millis); - decode_suite('S', gpr_time_from_seconds); - decode_suite('M', gpr_time_from_minutes); - decode_suite('H', gpr_time_from_hours); - assert_decodes_as("1000000000000000000000u", - gpr_inf_future(GPR_CLOCK_REALTIME)); +void +test_decoding (void) +{ + LOG_TEST ("test_decoding"); + decode_suite ('n', gpr_time_from_nanos); + decode_suite ('u', gpr_time_from_micros); + decode_suite ('m', gpr_time_from_millis); + decode_suite ('S', gpr_time_from_seconds); + decode_suite ('M', gpr_time_from_minutes); + decode_suite ('H', gpr_time_from_hours); + assert_decodes_as ("1000000000000000000000u", gpr_inf_future (GPR_CLOCK_REALTIME)); } -void test_decoding_fails(void) { +void +test_decoding_fails (void) +{ gpr_timespec x; - LOG_TEST("test_decoding_fails"); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout(" ", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("x", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1x", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1ux", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("!", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("n1", &x)); - GPR_ASSERT(0 == grpc_chttp2_decode_timeout("-1u", &x)); + LOG_TEST ("test_decoding_fails"); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout (" ", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("x", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("1", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("1x", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("1ux", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("!", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("n1", &x)); + GPR_ASSERT (0 == grpc_chttp2_decode_timeout ("-1u", &x)); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_encoding(); - test_decoding(); - test_decoding_fails(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_encoding (); + test_decoding (); + test_decoding_fails (); return 0; } diff --git a/test/core/transport/metadata_test.c b/test/core/transport/metadata_test.c index 1861442bb1..33c2ddf091 100644 --- a/test/core/transport/metadata_test.c +++ b/test/core/transport/metadata_test.c @@ -47,237 +47,263 @@ /* a large number */ #define MANY 10000 -static void test_no_op(void) { +static void +test_no_op (void) +{ grpc_mdctx *ctx; - LOG_TEST("test_no_op"); + LOG_TEST ("test_no_op"); - ctx = grpc_mdctx_create(); - grpc_mdctx_unref(ctx); + ctx = grpc_mdctx_create (); + grpc_mdctx_unref (ctx); } -static void test_create_string(void) { +static void +test_create_string (void) +{ grpc_mdctx *ctx; grpc_mdstr *s1, *s2, *s3; - LOG_TEST("test_create_string"); - - ctx = grpc_mdctx_create(); - s1 = grpc_mdstr_from_string(ctx, "hello", 0); - s2 = grpc_mdstr_from_string(ctx, "hello", 0); - s3 = grpc_mdstr_from_string(ctx, "very much not hello", 0); - GPR_ASSERT(s1 == s2); - GPR_ASSERT(s3 != s1); - GPR_ASSERT(gpr_slice_str_cmp(s1->slice, "hello") == 0); - GPR_ASSERT(gpr_slice_str_cmp(s3->slice, "very much not hello") == 0); - GRPC_MDSTR_UNREF(s1); - GRPC_MDSTR_UNREF(s2); - grpc_mdctx_unref(ctx); - GRPC_MDSTR_UNREF(s3); + LOG_TEST ("test_create_string"); + + ctx = grpc_mdctx_create (); + s1 = grpc_mdstr_from_string (ctx, "hello", 0); + s2 = grpc_mdstr_from_string (ctx, "hello", 0); + s3 = grpc_mdstr_from_string (ctx, "very much not hello", 0); + GPR_ASSERT (s1 == s2); + GPR_ASSERT (s3 != s1); + GPR_ASSERT (gpr_slice_str_cmp (s1->slice, "hello") == 0); + GPR_ASSERT (gpr_slice_str_cmp (s3->slice, "very much not hello") == 0); + GRPC_MDSTR_UNREF (s1); + GRPC_MDSTR_UNREF (s2); + grpc_mdctx_unref (ctx); + GRPC_MDSTR_UNREF (s3); } -static void test_create_metadata(void) { +static void +test_create_metadata (void) +{ grpc_mdctx *ctx; grpc_mdelem *m1, *m2, *m3; - LOG_TEST("test_create_metadata"); - - ctx = grpc_mdctx_create(); - m1 = grpc_mdelem_from_strings(ctx, "a", "b"); - m2 = grpc_mdelem_from_strings(ctx, "a", "b"); - m3 = grpc_mdelem_from_strings(ctx, "a", "c"); - GPR_ASSERT(m1 == m2); - GPR_ASSERT(m3 != m1); - GPR_ASSERT(m3->key == m1->key); - GPR_ASSERT(m3->value != m1->value); - GPR_ASSERT(gpr_slice_str_cmp(m1->key->slice, "a") == 0); - GPR_ASSERT(gpr_slice_str_cmp(m1->value->slice, "b") == 0); - GPR_ASSERT(gpr_slice_str_cmp(m3->value->slice, "c") == 0); - GRPC_MDELEM_UNREF(m1); - GRPC_MDELEM_UNREF(m2); - GRPC_MDELEM_UNREF(m3); - grpc_mdctx_unref(ctx); + LOG_TEST ("test_create_metadata"); + + ctx = grpc_mdctx_create (); + m1 = grpc_mdelem_from_strings (ctx, "a", "b"); + m2 = grpc_mdelem_from_strings (ctx, "a", "b"); + m3 = grpc_mdelem_from_strings (ctx, "a", "c"); + GPR_ASSERT (m1 == m2); + GPR_ASSERT (m3 != m1); + GPR_ASSERT (m3->key == m1->key); + GPR_ASSERT (m3->value != m1->value); + GPR_ASSERT (gpr_slice_str_cmp (m1->key->slice, "a") == 0); + GPR_ASSERT (gpr_slice_str_cmp (m1->value->slice, "b") == 0); + GPR_ASSERT (gpr_slice_str_cmp (m3->value->slice, "c") == 0); + GRPC_MDELEM_UNREF (m1); + GRPC_MDELEM_UNREF (m2); + GRPC_MDELEM_UNREF (m3); + grpc_mdctx_unref (ctx); } -static void test_create_many_ephemeral_metadata(void) { +static void +test_create_many_ephemeral_metadata (void) +{ grpc_mdctx *ctx; char buffer[GPR_LTOA_MIN_BUFSIZE]; long i; size_t mdtab_capacity_before; - LOG_TEST("test_create_many_ephemeral_metadata"); + LOG_TEST ("test_create_many_ephemeral_metadata"); - ctx = grpc_mdctx_create(); - mdtab_capacity_before = grpc_mdctx_get_mdtab_capacity_test_only(ctx); + ctx = grpc_mdctx_create (); + mdtab_capacity_before = grpc_mdctx_get_mdtab_capacity_test_only (ctx); /* add, and immediately delete a bunch of different elements */ - for (i = 0; i < MANY; i++) { - gpr_ltoa(i, buffer); - GRPC_MDELEM_UNREF(grpc_mdelem_from_strings(ctx, "a", buffer)); - } + for (i = 0; i < MANY; i++) + { + gpr_ltoa (i, buffer); + GRPC_MDELEM_UNREF (grpc_mdelem_from_strings (ctx, "a", buffer)); + } /* capacity should not grow */ - GPR_ASSERT(mdtab_capacity_before == - grpc_mdctx_get_mdtab_capacity_test_only(ctx)); - grpc_mdctx_unref(ctx); + GPR_ASSERT (mdtab_capacity_before == grpc_mdctx_get_mdtab_capacity_test_only (ctx)); + grpc_mdctx_unref (ctx); } -static void test_create_many_persistant_metadata(void) { +static void +test_create_many_persistant_metadata (void) +{ grpc_mdctx *ctx; char buffer[GPR_LTOA_MIN_BUFSIZE]; long i; - grpc_mdelem **created = gpr_malloc(sizeof(grpc_mdelem *) * MANY); + grpc_mdelem **created = gpr_malloc (sizeof (grpc_mdelem *) * MANY); grpc_mdelem *md; - LOG_TEST("test_create_many_persistant_metadata"); + LOG_TEST ("test_create_many_persistant_metadata"); - ctx = grpc_mdctx_create(); + ctx = grpc_mdctx_create (); /* add phase */ - for (i = 0; i < MANY; i++) { - gpr_ltoa(i, buffer); - created[i] = grpc_mdelem_from_strings(ctx, "a", buffer); - } + for (i = 0; i < MANY; i++) + { + gpr_ltoa (i, buffer); + created[i] = grpc_mdelem_from_strings (ctx, "a", buffer); + } /* verify phase */ - for (i = 0; i < MANY; i++) { - gpr_ltoa(i, buffer); - md = grpc_mdelem_from_strings(ctx, "a", buffer); - GPR_ASSERT(md == created[i]); - GRPC_MDELEM_UNREF(md); - } + for (i = 0; i < MANY; i++) + { + gpr_ltoa (i, buffer); + md = grpc_mdelem_from_strings (ctx, "a", buffer); + GPR_ASSERT (md == created[i]); + GRPC_MDELEM_UNREF (md); + } /* cleanup phase */ - for (i = 0; i < MANY; i++) { - GRPC_MDELEM_UNREF(created[i]); - } - grpc_mdctx_unref(ctx); + for (i = 0; i < MANY; i++) + { + GRPC_MDELEM_UNREF (created[i]); + } + grpc_mdctx_unref (ctx); - gpr_free(created); + gpr_free (created); } -static void test_spin_creating_the_same_thing(void) { +static void +test_spin_creating_the_same_thing (void) +{ grpc_mdctx *ctx; - LOG_TEST("test_spin_creating_the_same_thing"); + LOG_TEST ("test_spin_creating_the_same_thing"); - ctx = grpc_mdctx_create(); - GPR_ASSERT(grpc_mdctx_get_mdtab_count_test_only(ctx) == 0); - GPR_ASSERT(grpc_mdctx_get_mdtab_free_test_only(ctx) == 0); + ctx = grpc_mdctx_create (); + GPR_ASSERT (grpc_mdctx_get_mdtab_count_test_only (ctx) == 0); + GPR_ASSERT (grpc_mdctx_get_mdtab_free_test_only (ctx) == 0); - GRPC_MDELEM_UNREF(grpc_mdelem_from_strings(ctx, "a", "b")); - GPR_ASSERT(grpc_mdctx_get_mdtab_count_test_only(ctx) == 1); - GPR_ASSERT(grpc_mdctx_get_mdtab_free_test_only(ctx) == 1); + GRPC_MDELEM_UNREF (grpc_mdelem_from_strings (ctx, "a", "b")); + GPR_ASSERT (grpc_mdctx_get_mdtab_count_test_only (ctx) == 1); + GPR_ASSERT (grpc_mdctx_get_mdtab_free_test_only (ctx) == 1); - GRPC_MDELEM_UNREF(grpc_mdelem_from_strings(ctx, "a", "b")); - GPR_ASSERT(grpc_mdctx_get_mdtab_count_test_only(ctx) == 1); - GPR_ASSERT(grpc_mdctx_get_mdtab_free_test_only(ctx) == 1); + GRPC_MDELEM_UNREF (grpc_mdelem_from_strings (ctx, "a", "b")); + GPR_ASSERT (grpc_mdctx_get_mdtab_count_test_only (ctx) == 1); + GPR_ASSERT (grpc_mdctx_get_mdtab_free_test_only (ctx) == 1); - GRPC_MDELEM_UNREF(grpc_mdelem_from_strings(ctx, "a", "b")); - GPR_ASSERT(grpc_mdctx_get_mdtab_count_test_only(ctx) == 1); - GPR_ASSERT(grpc_mdctx_get_mdtab_free_test_only(ctx) == 1); + GRPC_MDELEM_UNREF (grpc_mdelem_from_strings (ctx, "a", "b")); + GPR_ASSERT (grpc_mdctx_get_mdtab_count_test_only (ctx) == 1); + GPR_ASSERT (grpc_mdctx_get_mdtab_free_test_only (ctx) == 1); - grpc_mdctx_unref(ctx); + grpc_mdctx_unref (ctx); } -static void test_things_stick_around(void) { +static void +test_things_stick_around (void) +{ grpc_mdctx *ctx; size_t i, j; char *buffer; size_t nstrs = 1000; - grpc_mdstr **strs = gpr_malloc(sizeof(grpc_mdstr *) * nstrs); - size_t *shuf = gpr_malloc(sizeof(size_t) * nstrs); + grpc_mdstr **strs = gpr_malloc (sizeof (grpc_mdstr *) * nstrs); + size_t *shuf = gpr_malloc (sizeof (size_t) * nstrs); grpc_mdstr *test; - LOG_TEST("test_things_stick_around"); - - ctx = grpc_mdctx_create(); - - for (i = 0; i < nstrs; i++) { - gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", i); - strs[i] = grpc_mdstr_from_string(ctx, buffer, 0); - shuf[i] = i; - gpr_free(buffer); - } - - for (i = 0; i < nstrs; i++) { - GRPC_MDSTR_REF(strs[i]); - GRPC_MDSTR_UNREF(strs[i]); - } - - for (i = 0; i < nstrs; i++) { - size_t p = (size_t)rand() % nstrs; - size_t q = (size_t)rand() % nstrs; - size_t temp = shuf[p]; - shuf[p] = shuf[q]; - shuf[q] = temp; - } - - for (i = 0; i < nstrs; i++) { - GRPC_MDSTR_UNREF(strs[shuf[i]]); - for (j = i + 1; j < nstrs; j++) { - gpr_asprintf(&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", shuf[j]); - test = grpc_mdstr_from_string(ctx, buffer, 0); - GPR_ASSERT(test == strs[shuf[j]]); - GRPC_MDSTR_UNREF(test); - gpr_free(buffer); + LOG_TEST ("test_things_stick_around"); + + ctx = grpc_mdctx_create (); + + for (i = 0; i < nstrs; i++) + { + gpr_asprintf (&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", i); + strs[i] = grpc_mdstr_from_string (ctx, buffer, 0); + shuf[i] = i; + gpr_free (buffer); + } + + for (i = 0; i < nstrs; i++) + { + GRPC_MDSTR_REF (strs[i]); + GRPC_MDSTR_UNREF (strs[i]); + } + + for (i = 0; i < nstrs; i++) + { + size_t p = (size_t) rand () % nstrs; + size_t q = (size_t) rand () % nstrs; + size_t temp = shuf[p]; + shuf[p] = shuf[q]; + shuf[q] = temp; + } + + for (i = 0; i < nstrs; i++) + { + GRPC_MDSTR_UNREF (strs[shuf[i]]); + for (j = i + 1; j < nstrs; j++) + { + gpr_asprintf (&buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%dx", shuf[j]); + test = grpc_mdstr_from_string (ctx, buffer, 0); + GPR_ASSERT (test == strs[shuf[j]]); + GRPC_MDSTR_UNREF (test); + gpr_free (buffer); + } } - } - grpc_mdctx_unref(ctx); - gpr_free(strs); - gpr_free(shuf); + grpc_mdctx_unref (ctx); + gpr_free (strs); + gpr_free (shuf); } -static void test_slices_work(void) { +static void +test_slices_work (void) +{ /* ensure no memory leaks when switching representation from mdstr to slice */ grpc_mdctx *ctx; grpc_mdstr *str; gpr_slice slice; - LOG_TEST("test_slices_work"); + LOG_TEST ("test_slices_work"); - ctx = grpc_mdctx_create(); + ctx = grpc_mdctx_create (); - str = grpc_mdstr_from_string( - ctx, "123456789012345678901234567890123456789012345678901234567890", 0); - slice = gpr_slice_ref(str->slice); - GRPC_MDSTR_UNREF(str); - gpr_slice_unref(slice); + str = grpc_mdstr_from_string (ctx, "123456789012345678901234567890123456789012345678901234567890", 0); + slice = gpr_slice_ref (str->slice); + GRPC_MDSTR_UNREF (str); + gpr_slice_unref (slice); - str = grpc_mdstr_from_string( - ctx, "123456789012345678901234567890123456789012345678901234567890", 0); - slice = gpr_slice_ref(str->slice); - gpr_slice_unref(slice); - GRPC_MDSTR_UNREF(str); + str = grpc_mdstr_from_string (ctx, "123456789012345678901234567890123456789012345678901234567890", 0); + slice = gpr_slice_ref (str->slice); + gpr_slice_unref (slice); + GRPC_MDSTR_UNREF (str); - grpc_mdctx_unref(ctx); + grpc_mdctx_unref (ctx); } -static void test_base64_and_huffman_works(void) { +static void +test_base64_and_huffman_works (void) +{ grpc_mdctx *ctx; grpc_mdstr *str; gpr_slice slice1; gpr_slice slice2; - LOG_TEST("test_base64_and_huffman_works"); + LOG_TEST ("test_base64_and_huffman_works"); - ctx = grpc_mdctx_create(); - str = grpc_mdstr_from_string(ctx, "abcdefg", 0); - slice1 = grpc_mdstr_as_base64_encoded_and_huffman_compressed(str); - slice2 = grpc_chttp2_base64_encode_and_huffman_compress(str->slice); - GPR_ASSERT(0 == gpr_slice_cmp(slice1, slice2)); + ctx = grpc_mdctx_create (); + str = grpc_mdstr_from_string (ctx, "abcdefg", 0); + slice1 = grpc_mdstr_as_base64_encoded_and_huffman_compressed (str); + slice2 = grpc_chttp2_base64_encode_and_huffman_compress (str->slice); + GPR_ASSERT (0 == gpr_slice_cmp (slice1, slice2)); - gpr_slice_unref(slice2); - GRPC_MDSTR_UNREF(str); - grpc_mdctx_unref(ctx); + gpr_slice_unref (slice2); + GRPC_MDSTR_UNREF (str); + grpc_mdctx_unref (ctx); } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_no_op(); - test_create_string(); - test_create_metadata(); - test_create_many_ephemeral_metadata(); - test_create_many_persistant_metadata(); - test_spin_creating_the_same_thing(); - test_things_stick_around(); - test_slices_work(); - test_base64_and_huffman_works(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_no_op (); + test_create_string (); + test_create_metadata (); + test_create_many_ephemeral_metadata (); + test_create_many_persistant_metadata (); + test_spin_creating_the_same_thing (); + test_things_stick_around (); + test_slices_work (); + test_base64_and_huffman_works (); return 0; } diff --git a/test/core/transport/stream_op_test.c b/test/core/transport/stream_op_test.c index 546080deb9..74d70a9286 100644 --- a/test/core/transport/stream_op_test.c +++ b/test/core/transport/stream_op_test.c @@ -38,79 +38,86 @@ #include <grpc/support/log.h> #include "test/core/util/test_config.h" -static void assert_slices_equal(gpr_slice a, gpr_slice b) { - GPR_ASSERT(a.refcount == b.refcount); - if (a.refcount) { - GPR_ASSERT(a.data.refcounted.bytes == b.data.refcounted.bytes); - GPR_ASSERT(a.data.refcounted.length == b.data.refcounted.length); - } else { - GPR_ASSERT(a.data.inlined.length == b.data.inlined.length); - GPR_ASSERT(0 == memcmp(a.data.inlined.bytes, b.data.inlined.bytes, - a.data.inlined.length)); - } +static void +assert_slices_equal (gpr_slice a, gpr_slice b) +{ + GPR_ASSERT (a.refcount == b.refcount); + if (a.refcount) + { + GPR_ASSERT (a.data.refcounted.bytes == b.data.refcounted.bytes); + GPR_ASSERT (a.data.refcounted.length == b.data.refcounted.length); + } + else + { + GPR_ASSERT (a.data.inlined.length == b.data.inlined.length); + GPR_ASSERT (0 == memcmp (a.data.inlined.bytes, b.data.inlined.bytes, a.data.inlined.length)); + } } -int main(int argc, char **argv) { +int +main (int argc, char **argv) +{ /* some basic test data */ - gpr_slice test_slice_1 = gpr_slice_malloc(1); - gpr_slice test_slice_2 = gpr_slice_malloc(2); - gpr_slice test_slice_3 = gpr_slice_malloc(3); - gpr_slice test_slice_4 = gpr_slice_malloc(4); + gpr_slice test_slice_1 = gpr_slice_malloc (1); + gpr_slice test_slice_2 = gpr_slice_malloc (2); + gpr_slice test_slice_3 = gpr_slice_malloc (3); + gpr_slice test_slice_4 = gpr_slice_malloc (4); unsigned i; grpc_stream_op_buffer buf; grpc_stream_op_buffer buf2; - grpc_test_init(argc, argv); + grpc_test_init (argc, argv); /* initialize one of our buffers */ - grpc_sopb_init(&buf); + grpc_sopb_init (&buf); /* it should start out empty */ - GPR_ASSERT(buf.nops == 0); + GPR_ASSERT (buf.nops == 0); /* add some data to the buffer */ - grpc_sopb_add_begin_message(&buf, 1, 2); - grpc_sopb_add_slice(&buf, test_slice_1); - grpc_sopb_add_slice(&buf, test_slice_2); - grpc_sopb_add_slice(&buf, test_slice_3); - grpc_sopb_add_slice(&buf, test_slice_4); - grpc_sopb_add_no_op(&buf); + grpc_sopb_add_begin_message (&buf, 1, 2); + grpc_sopb_add_slice (&buf, test_slice_1); + grpc_sopb_add_slice (&buf, test_slice_2); + grpc_sopb_add_slice (&buf, test_slice_3); + grpc_sopb_add_slice (&buf, test_slice_4); + grpc_sopb_add_no_op (&buf); /* verify that the data went in ok */ - GPR_ASSERT(buf.nops == 6); - GPR_ASSERT(buf.ops[0].type == GRPC_OP_BEGIN_MESSAGE); - GPR_ASSERT(buf.ops[0].data.begin_message.length == 1); - GPR_ASSERT(buf.ops[0].data.begin_message.flags == 2); - GPR_ASSERT(buf.ops[1].type == GRPC_OP_SLICE); - assert_slices_equal(buf.ops[1].data.slice, test_slice_1); - GPR_ASSERT(buf.ops[2].type == GRPC_OP_SLICE); - assert_slices_equal(buf.ops[2].data.slice, test_slice_2); - GPR_ASSERT(buf.ops[3].type == GRPC_OP_SLICE); - assert_slices_equal(buf.ops[3].data.slice, test_slice_3); - GPR_ASSERT(buf.ops[4].type == GRPC_OP_SLICE); - assert_slices_equal(buf.ops[4].data.slice, test_slice_4); - GPR_ASSERT(buf.ops[5].type == GRPC_NO_OP); + GPR_ASSERT (buf.nops == 6); + GPR_ASSERT (buf.ops[0].type == GRPC_OP_BEGIN_MESSAGE); + GPR_ASSERT (buf.ops[0].data.begin_message.length == 1); + GPR_ASSERT (buf.ops[0].data.begin_message.flags == 2); + GPR_ASSERT (buf.ops[1].type == GRPC_OP_SLICE); + assert_slices_equal (buf.ops[1].data.slice, test_slice_1); + GPR_ASSERT (buf.ops[2].type == GRPC_OP_SLICE); + assert_slices_equal (buf.ops[2].data.slice, test_slice_2); + GPR_ASSERT (buf.ops[3].type == GRPC_OP_SLICE); + assert_slices_equal (buf.ops[3].data.slice, test_slice_3); + GPR_ASSERT (buf.ops[4].type == GRPC_OP_SLICE); + assert_slices_equal (buf.ops[4].data.slice, test_slice_4); + GPR_ASSERT (buf.ops[5].type == GRPC_NO_OP); /* initialize the second buffer */ - grpc_sopb_init(&buf2); + grpc_sopb_init (&buf2); /* add a no-op, and then the original buffer */ - grpc_sopb_add_no_op(&buf2); - grpc_sopb_append(&buf2, buf.ops, buf.nops); + grpc_sopb_add_no_op (&buf2); + grpc_sopb_append (&buf2, buf.ops, buf.nops); /* should be one element bigger than the original */ - GPR_ASSERT(buf2.nops == buf.nops + 1); - GPR_ASSERT(buf2.ops[0].type == GRPC_NO_OP); + GPR_ASSERT (buf2.nops == buf.nops + 1); + GPR_ASSERT (buf2.ops[0].type == GRPC_NO_OP); /* and the tail should be the same */ - for (i = 0; i < buf.nops; i++) { - GPR_ASSERT(buf2.ops[i + 1].type == buf.ops[i].type); - } + for (i = 0; i < buf.nops; i++) + { + GPR_ASSERT (buf2.ops[i + 1].type == buf.ops[i].type); + } /* destroy the buffers */ - grpc_sopb_destroy(&buf); - grpc_sopb_destroy(&buf2); + grpc_sopb_destroy (&buf); + grpc_sopb_destroy (&buf2); - gpr_slice_unref(test_slice_1); - gpr_slice_unref(test_slice_2); - gpr_slice_unref(test_slice_3); - gpr_slice_unref(test_slice_4); + gpr_slice_unref (test_slice_1); + gpr_slice_unref (test_slice_2); + gpr_slice_unref (test_slice_3); + gpr_slice_unref (test_slice_4); return 0; } diff --git a/test/core/tsi/transport_security_test.c b/test/core/tsi/transport_security_test.c index bec3866166..4852a44bb1 100644 --- a/test/core/tsi/transport_security_test.c +++ b/test/core/tsi/transport_security_test.c @@ -46,7 +46,8 @@ #include "src/core/tsi/ssl_transport_security.h" #include "test/core/util/test_config.h" -typedef struct { +typedef struct +{ /* 1 if success, 0 if failure. */ int expected; @@ -66,238 +67,255 @@ typedef struct { chromium/src/net/cert/x509_certificate_unittest.cc. TODO(jboeuf) uncomment test cases as we fix tsi_ssl_peer_matches_name. */ const cert_name_test_entry cert_name_test_entries[] = { - {1, "foo.com", "foo.com", NULL}, - {1, "f", "f", NULL}, - {0, "h", "i", NULL}, - {1, "bar.foo.com", "*.foo.com", NULL}, - {1, "www.test.fr", "common.name", - "*.test.com,*.test.co.uk,*.test.de,*.test.fr"}, - /* - {1, "wwW.tESt.fr", "common.name", ",*.*,*.test.de,*.test.FR,www"}, - */ - {0, "f.uk", ".uk", NULL}, - {0, "w.bar.foo.com", "?.bar.foo.com", NULL}, - {0, "www.foo.com", "(www|ftp).foo.com", NULL}, - {0, "www.foo.com", "www.foo.com#", NULL}, /* # = null char. */ - {0, "www.foo.com", "", "www.foo.com#*.foo.com,#,#"}, - {0, "www.house.example", "ww.house.example", NULL}, - {0, "test.org", "", "www.test.org,*.test.org,*.org"}, - {0, "w.bar.foo.com", "w*.bar.foo.com", NULL}, - {0, "www.bar.foo.com", "ww*ww.bar.foo.com", NULL}, - {0, "wwww.bar.foo.com", "ww*ww.bar.foo.com", NULL}, - {0, "wwww.bar.foo.com", "w*w.bar.foo.com", NULL}, - {0, "wwww.bar.foo.com", "w*w.bar.foo.c0m", NULL}, - {0, "WALLY.bar.foo.com", "wa*.bar.foo.com", NULL}, - {0, "wally.bar.foo.com", "*Ly.bar.foo.com", NULL}, - /* - {1, "ww%57.foo.com", "", "www.foo.com"}, - {1, "www&.foo.com", "www%26.foo.com", NULL}, - */ - - /* Common name must not be used if subject alternative name was provided. */ - {0, "www.test.co.jp", "www.test.co.jp", - "*.test.de,*.jp,www.test.co.uk,www.*.co.jp"}, - {0, "www.bar.foo.com", "www.bar.foo.com", - "*.foo.com,*.*.foo.com,*.*.bar.foo.com,*..bar.foo.com,"}, - - /* IDN tests */ - {1, "xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br", NULL}, - {1, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br", NULL}, - {0, "xn--poema-9qae5a.com.br", "", - "*.xn--poema-9qae5a.com.br," - "xn--poema-*.com.br," - "xn--*-9qae5a.com.br," - "*--poema-9qae5a.com.br"}, - - /* The following are adapted from the examples quoted from - http://tools.ietf.org/html/rfc6125#section-6.4.3 - (e.g., *.example.com would match foo.example.com but - not bar.foo.example.com or example.com). */ - {1, "foo.example.com", "*.example.com", NULL}, - {0, "bar.foo.example.com", "*.example.com", NULL}, - {0, "example.com", "*.example.com", NULL}, - - /* Partial wildcards are disallowed, though RFC 2818 rules allow them. - That is, forms such as baz*.example.net, *baz.example.net, and - b*z.example.net should NOT match domains. Instead, the wildcard must - always be the left-most label, and only a single label. */ - {0, "baz1.example.net", "baz*.example.net", NULL}, - {0, "foobaz.example.net", "*baz.example.net", NULL}, - {0, "buzz.example.net", "b*z.example.net", NULL}, - {0, "www.test.example.net", "www.*.example.net", NULL}, - - /* Wildcards should not be valid for public registry controlled domains, - and unknown/unrecognized domains, at least three domain components must - be present. */ - {1, "www.test.example", "*.test.example", NULL}, - {1, "test.example.co.uk", "*.example.co.uk", NULL}, - {0, "test.example", "*.example", NULL}, - /* - {0, "example.co.uk", "*.co.uk", NULL}, - */ - {0, "foo.com", "*.com", NULL}, - {0, "foo.us", "*.us", NULL}, - {0, "foo", "*", NULL}, - - /* IDN variants of wildcards and registry controlled domains. */ - {1, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br", NULL}, - {1, "test.example.xn--mgbaam7a8h", "*.example.xn--mgbaam7a8h", NULL}, - /* - {0, "xn--poema-9qae5a.com.br", "*.com.br", NULL}, - */ - {0, "example.xn--mgbaam7a8h", "*.xn--mgbaam7a8h", NULL}, - - /* Wildcards should be permissible for 'private' registry controlled - domains. */ - {1, "www.appspot.com", "*.appspot.com", NULL}, - {1, "foo.s3.amazonaws.com", "*.s3.amazonaws.com", NULL}, - - /* Multiple wildcards are not valid. */ - {0, "foo.example.com", "*.*.com", NULL}, - {0, "foo.bar.example.com", "*.bar.*.com", NULL}, - - /* Absolute vs relative DNS name tests. Although not explicitly specified - in RFC 6125, absolute reference names (those ending in a .) should - match either absolute or relative presented names. */ - {1, "foo.com", "foo.com.", NULL}, - {1, "foo.com.", "foo.com", NULL}, - {1, "foo.com.", "foo.com.", NULL}, - {1, "f", "f.", NULL}, - {1, "f.", "f", NULL}, - {1, "f.", "f.", NULL}, - {1, "www-3.bar.foo.com", "*.bar.foo.com.", NULL}, - {1, "www-3.bar.foo.com.", "*.bar.foo.com", NULL}, - {1, "www-3.bar.foo.com.", "*.bar.foo.com.", NULL}, - {0, ".", ".", NULL}, - {0, "example.com", "*.com.", NULL}, - {0, "example.com.", "*.com", NULL}, - {0, "example.com.", "*.com.", NULL}, - {0, "foo.", "*.", NULL}, - {0, "foo", "*.", NULL}, - /* - {0, "foo.co.uk", "*.co.uk.", NULL}, - {0, "foo.co.uk.", "*.co.uk.", NULL}, - */ - - /* An empty CN is OK. */ - {1, "test.foo.com", "", "test.foo.com"}, - - /* An IP should not be used for the CN. */ - {0, "173.194.195.139", "173.194.195.139", NULL}, + {1, "foo.com", "foo.com", NULL}, + {1, "f", "f", NULL}, + {0, "h", "i", NULL}, + {1, "bar.foo.com", "*.foo.com", NULL}, + {1, "www.test.fr", "common.name", + "*.test.com,*.test.co.uk,*.test.de,*.test.fr"}, + /* + {1, "wwW.tESt.fr", "common.name", ",*.*,*.test.de,*.test.FR,www"}, + */ + {0, "f.uk", ".uk", NULL}, + {0, "w.bar.foo.com", "?.bar.foo.com", NULL}, + {0, "www.foo.com", "(www|ftp).foo.com", NULL}, + {0, "www.foo.com", "www.foo.com#", NULL}, /* # = null char. */ + {0, "www.foo.com", "", "www.foo.com#*.foo.com,#,#"}, + {0, "www.house.example", "ww.house.example", NULL}, + {0, "test.org", "", "www.test.org,*.test.org,*.org"}, + {0, "w.bar.foo.com", "w*.bar.foo.com", NULL}, + {0, "www.bar.foo.com", "ww*ww.bar.foo.com", NULL}, + {0, "wwww.bar.foo.com", "ww*ww.bar.foo.com", NULL}, + {0, "wwww.bar.foo.com", "w*w.bar.foo.com", NULL}, + {0, "wwww.bar.foo.com", "w*w.bar.foo.c0m", NULL}, + {0, "WALLY.bar.foo.com", "wa*.bar.foo.com", NULL}, + {0, "wally.bar.foo.com", "*Ly.bar.foo.com", NULL}, + /* + {1, "ww%57.foo.com", "", "www.foo.com"}, + {1, "www&.foo.com", "www%26.foo.com", NULL}, + */ + + /* Common name must not be used if subject alternative name was provided. */ + {0, "www.test.co.jp", "www.test.co.jp", + "*.test.de,*.jp,www.test.co.uk,www.*.co.jp"}, + {0, "www.bar.foo.com", "www.bar.foo.com", + "*.foo.com,*.*.foo.com,*.*.bar.foo.com,*..bar.foo.com,"}, + + /* IDN tests */ + {1, "xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br", NULL}, + {1, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br", NULL}, + {0, "xn--poema-9qae5a.com.br", "", + "*.xn--poema-9qae5a.com.br," "xn--poema-*.com.br," "xn--*-9qae5a.com.br," "*--poema-9qae5a.com.br"}, + + /* The following are adapted from the examples quoted from + http://tools.ietf.org/html/rfc6125#section-6.4.3 + (e.g., *.example.com would match foo.example.com but + not bar.foo.example.com or example.com). */ + {1, "foo.example.com", "*.example.com", NULL}, + {0, "bar.foo.example.com", "*.example.com", NULL}, + {0, "example.com", "*.example.com", NULL}, + + /* Partial wildcards are disallowed, though RFC 2818 rules allow them. + That is, forms such as baz*.example.net, *baz.example.net, and + b*z.example.net should NOT match domains. Instead, the wildcard must + always be the left-most label, and only a single label. */ + {0, "baz1.example.net", "baz*.example.net", NULL}, + {0, "foobaz.example.net", "*baz.example.net", NULL}, + {0, "buzz.example.net", "b*z.example.net", NULL}, + {0, "www.test.example.net", "www.*.example.net", NULL}, + + /* Wildcards should not be valid for public registry controlled domains, + and unknown/unrecognized domains, at least three domain components must + be present. */ + {1, "www.test.example", "*.test.example", NULL}, + {1, "test.example.co.uk", "*.example.co.uk", NULL}, + {0, "test.example", "*.example", NULL}, + /* + {0, "example.co.uk", "*.co.uk", NULL}, + */ + {0, "foo.com", "*.com", NULL}, + {0, "foo.us", "*.us", NULL}, + {0, "foo", "*", NULL}, + + /* IDN variants of wildcards and registry controlled domains. */ + {1, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br", NULL}, + {1, "test.example.xn--mgbaam7a8h", "*.example.xn--mgbaam7a8h", NULL}, + /* + {0, "xn--poema-9qae5a.com.br", "*.com.br", NULL}, + */ + {0, "example.xn--mgbaam7a8h", "*.xn--mgbaam7a8h", NULL}, + + /* Wildcards should be permissible for 'private' registry controlled + domains. */ + {1, "www.appspot.com", "*.appspot.com", NULL}, + {1, "foo.s3.amazonaws.com", "*.s3.amazonaws.com", NULL}, + + /* Multiple wildcards are not valid. */ + {0, "foo.example.com", "*.*.com", NULL}, + {0, "foo.bar.example.com", "*.bar.*.com", NULL}, + + /* Absolute vs relative DNS name tests. Although not explicitly specified + in RFC 6125, absolute reference names (those ending in a .) should + match either absolute or relative presented names. */ + {1, "foo.com", "foo.com.", NULL}, + {1, "foo.com.", "foo.com", NULL}, + {1, "foo.com.", "foo.com.", NULL}, + {1, "f", "f.", NULL}, + {1, "f.", "f", NULL}, + {1, "f.", "f.", NULL}, + {1, "www-3.bar.foo.com", "*.bar.foo.com.", NULL}, + {1, "www-3.bar.foo.com.", "*.bar.foo.com", NULL}, + {1, "www-3.bar.foo.com.", "*.bar.foo.com.", NULL}, + {0, ".", ".", NULL}, + {0, "example.com", "*.com.", NULL}, + {0, "example.com.", "*.com", NULL}, + {0, "example.com.", "*.com.", NULL}, + {0, "foo.", "*.", NULL}, + {0, "foo", "*.", NULL}, + /* + {0, "foo.co.uk", "*.co.uk.", NULL}, + {0, "foo.co.uk.", "*.co.uk.", NULL}, + */ + + /* An empty CN is OK. */ + {1, "test.foo.com", "", "test.foo.com"}, + + /* An IP should not be used for the CN. */ + {0, "173.194.195.139", "173.194.195.139", NULL}, }; -typedef struct name_list { +typedef struct name_list +{ const char *name; struct name_list *next; } name_list; -typedef struct { +typedef struct +{ size_t name_count; char *buffer; name_list *names; } parsed_dns_names; -name_list *name_list_add(const char *n) { - name_list *result = gpr_malloc(sizeof(name_list)); +name_list * +name_list_add (const char *n) +{ + name_list *result = gpr_malloc (sizeof (name_list)); result->name = n; result->next = NULL; return result; } -static parsed_dns_names parse_dns_names(const char *dns_names_str) { +static parsed_dns_names +parse_dns_names (const char *dns_names_str) +{ parsed_dns_names result; name_list *current_nl; size_t i; - memset(&result, 0, sizeof(parsed_dns_names)); - if (dns_names_str == 0) return result; + memset (&result, 0, sizeof (parsed_dns_names)); + if (dns_names_str == 0) + return result; result.name_count = 1; - result.buffer = gpr_strdup(dns_names_str); - result.names = name_list_add(result.buffer); + result.buffer = gpr_strdup (dns_names_str); + result.names = name_list_add (result.buffer); current_nl = result.names; - for (i = 0; i < strlen(dns_names_str); i++) { - if (dns_names_str[i] == ',') { - result.buffer[i] = '\0'; - result.name_count++; - i++; - current_nl->next = name_list_add(result.buffer + i); - current_nl = current_nl->next; + for (i = 0; i < strlen (dns_names_str); i++) + { + if (dns_names_str[i] == ',') + { + result.buffer[i] = '\0'; + result.name_count++; + i++; + current_nl->next = name_list_add (result.buffer + i); + current_nl = current_nl->next; + } } - } return result; } -static void destruct_parsed_dns_names(parsed_dns_names *pdn) { +static void +destruct_parsed_dns_names (parsed_dns_names * pdn) +{ name_list *nl = pdn->names; - if (pdn->buffer != NULL) gpr_free(pdn->buffer); - while (nl != NULL) { - name_list *to_be_free = nl; - nl = nl->next; - gpr_free(to_be_free); - } + if (pdn->buffer != NULL) + gpr_free (pdn->buffer); + while (nl != NULL) + { + name_list *to_be_free = nl; + nl = nl->next; + gpr_free (to_be_free); + } } -static char *processed_dns_name(const char *dns_name) { - char *result = gpr_strdup(dns_name); +static char * +processed_dns_name (const char *dns_name) +{ + char *result = gpr_strdup (dns_name); size_t i; - for (i = 0; i < strlen(result); i++) { - if (result[i] == '#') { - result[i] = '\0'; + for (i = 0; i < strlen (result); i++) + { + if (result[i] == '#') + { + result[i] = '\0'; + } } - } return result; } -static tsi_peer peer_from_cert_name_test_entry( - const cert_name_test_entry *entry) { +static tsi_peer +peer_from_cert_name_test_entry (const cert_name_test_entry * entry) +{ size_t i; tsi_peer peer; name_list *nl; - parsed_dns_names dns_entries = parse_dns_names(entry->dns_names); + parsed_dns_names dns_entries = parse_dns_names (entry->dns_names); nl = dns_entries.names; - GPR_ASSERT(tsi_construct_peer(1 + dns_entries.name_count, &peer) == TSI_OK); - GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( - TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, entry->common_name, - &peer.properties[0]) == TSI_OK); + GPR_ASSERT (tsi_construct_peer (1 + dns_entries.name_count, &peer) == TSI_OK); + GPR_ASSERT (tsi_construct_string_peer_property_from_cstring (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, entry->common_name, &peer.properties[0]) == TSI_OK); i = 1; - while (nl != NULL) { - char *processed = processed_dns_name(nl->name); - GPR_ASSERT(tsi_construct_string_peer_property( - TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed, - strlen(nl->name), &peer.properties[i++]) == TSI_OK); - nl = nl->next; - gpr_free(processed); - } - destruct_parsed_dns_names(&dns_entries); + while (nl != NULL) + { + char *processed = processed_dns_name (nl->name); + GPR_ASSERT (tsi_construct_string_peer_property (TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed, strlen (nl->name), &peer.properties[i++]) == TSI_OK); + nl = nl->next; + gpr_free (processed); + } + destruct_parsed_dns_names (&dns_entries); return peer; } -char *cert_name_test_entry_to_string(const cert_name_test_entry *entry) { +char * +cert_name_test_entry_to_string (const cert_name_test_entry * entry) +{ char *s; - gpr_asprintf( - &s, "{ success = %s, host_name = %s, common_name = %s, dns_names = %s}", - entry->expected ? "true" : "false", entry->host_name, entry->common_name, - entry->dns_names != NULL ? entry->dns_names : ""); + gpr_asprintf (&s, "{ success = %s, host_name = %s, common_name = %s, dns_names = %s}", entry->expected ? "true" : "false", entry->host_name, entry->common_name, entry->dns_names != NULL ? entry->dns_names : ""); return s; } -static void test_peer_matches_name(void) { +static void +test_peer_matches_name (void) +{ size_t i = 0; - for (i = 0; i < GPR_ARRAY_SIZE(cert_name_test_entries); i++) { - const cert_name_test_entry *entry = &cert_name_test_entries[i]; - tsi_peer peer = peer_from_cert_name_test_entry(entry); - int result = tsi_ssl_peer_matches_name(&peer, entry->host_name); - if (result != entry->expected) { - char *entry_str = cert_name_test_entry_to_string(entry); - gpr_log(GPR_ERROR, "%s", entry_str); - gpr_free(entry_str); - GPR_ASSERT(0); /* Unexpected result. */ + for (i = 0; i < GPR_ARRAY_SIZE (cert_name_test_entries); i++) + { + const cert_name_test_entry *entry = &cert_name_test_entries[i]; + tsi_peer peer = peer_from_cert_name_test_entry (entry); + int result = tsi_ssl_peer_matches_name (&peer, entry->host_name); + if (result != entry->expected) + { + char *entry_str = cert_name_test_entry_to_string (entry); + gpr_log (GPR_ERROR, "%s", entry_str); + gpr_free (entry_str); + GPR_ASSERT (0); /* Unexpected result. */ + } + tsi_peer_destruct (&peer); } - tsi_peer_destruct(&peer); - } } -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_peer_matches_name(); +int +main (int argc, char **argv) +{ + grpc_test_init (argc, argv); + test_peer_matches_name (); return 0; } diff --git a/test/core/util/grpc_profiler.c b/test/core/util/grpc_profiler.c index c2c0c9cf53..0df6c13e5d 100644 --- a/test/core/util/grpc_profiler.c +++ b/test/core/util/grpc_profiler.c @@ -36,25 +36,34 @@ #if GRPC_HAVE_PERFTOOLS #include <gperftools/profiler.h> -void grpc_profiler_start(const char *filename) { ProfilerStart(filename); } +void +grpc_profiler_start (const char *filename) +{ + ProfilerStart (filename); +} -void grpc_profiler_stop() { ProfilerStop(); } +void +grpc_profiler_stop () +{ + ProfilerStop (); +} #else #include <grpc/support/log.h> -void grpc_profiler_start(const char *filename) { +void +grpc_profiler_start (const char *filename) +{ static int printed_warning = 0; - if (!printed_warning) { - gpr_log(GPR_DEBUG, - "You do not have google-perftools installed, profiling is disabled " - "[for %s]", - filename); - gpr_log(GPR_DEBUG, - "To install on ubuntu: sudo apt-get install google-perftools " - "libgoogle-perftools-dev"); - printed_warning = 1; - } + if (!printed_warning) + { + gpr_log (GPR_DEBUG, "You do not have google-perftools installed, profiling is disabled " "[for %s]", filename); + gpr_log (GPR_DEBUG, "To install on ubuntu: sudo apt-get install google-perftools " "libgoogle-perftools-dev"); + printed_warning = 1; + } } -void grpc_profiler_stop(void) {} +void +grpc_profiler_stop (void) +{ +} #endif diff --git a/test/core/util/grpc_profiler.h b/test/core/util/grpc_profiler.h index 88ec6bcb0e..423c73b331 100644 --- a/test/core/util/grpc_profiler.h +++ b/test/core/util/grpc_profiler.h @@ -35,14 +35,15 @@ #define GRPC_TEST_CORE_UTIL_GRPC_PROFILER_H #ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ +extern "C" +{ +#endif /* __cplusplus */ -void grpc_profiler_start(const char *filename); -void grpc_profiler_stop(); + void grpc_profiler_start (const char *filename); + void grpc_profiler_stop (); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ -#endif /* GRPC_TEST_CORE_UTIL_GRPC_PROFILER_H */ +#endif /* GRPC_TEST_CORE_UTIL_GRPC_PROFILER_H */ diff --git a/test/core/util/parse_hexstring.c b/test/core/util/parse_hexstring.c index eced3173d1..3d01dfc9a7 100644 --- a/test/core/util/parse_hexstring.c +++ b/test/core/util/parse_hexstring.c @@ -34,37 +34,45 @@ #include "test/core/util/parse_hexstring.h" #include <grpc/support/log.h> -gpr_slice parse_hexstring(const char *hexstring) { +gpr_slice +parse_hexstring (const char *hexstring) +{ size_t nibbles = 0; const char *p = 0; gpr_uint8 *out; gpr_uint8 temp; gpr_slice slice; - for (p = hexstring; *p; p++) { - nibbles += (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f'); - } + for (p = hexstring; *p; p++) + { + nibbles += (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f'); + } - GPR_ASSERT((nibbles & 1) == 0); + GPR_ASSERT ((nibbles & 1) == 0); - slice = gpr_slice_malloc(nibbles / 2); - out = GPR_SLICE_START_PTR(slice); + slice = gpr_slice_malloc (nibbles / 2); + out = GPR_SLICE_START_PTR (slice); nibbles = 0; temp = 0; - for (p = hexstring; *p; p++) { - if (*p >= '0' && *p <= '9') { - temp = (gpr_uint8)(temp << 4) | (gpr_uint8)(*p - '0'); - nibbles++; - } else if (*p >= 'a' && *p <= 'f') { - temp = (gpr_uint8)(temp << 4) | (gpr_uint8)(*p - 'a' + 10); - nibbles++; - } - if (nibbles == 2) { - *out++ = temp; - nibbles = 0; + for (p = hexstring; *p; p++) + { + if (*p >= '0' && *p <= '9') + { + temp = (gpr_uint8) (temp << 4) | (gpr_uint8) (*p - '0'); + nibbles++; + } + else if (*p >= 'a' && *p <= 'f') + { + temp = (gpr_uint8) (temp << 4) | (gpr_uint8) (*p - 'a' + 10); + nibbles++; + } + if (nibbles == 2) + { + *out++ = temp; + nibbles = 0; + } } - } return slice; } diff --git a/test/core/util/parse_hexstring.h b/test/core/util/parse_hexstring.h index ddbfe541c6..d2c4770330 100644 --- a/test/core/util/parse_hexstring.h +++ b/test/core/util/parse_hexstring.h @@ -36,6 +36,6 @@ #include <grpc/support/slice.h> -gpr_slice parse_hexstring(const char *hexstring); +gpr_slice parse_hexstring (const char *hexstring); #endif /* GRPC_TEST_CORE_UTIL_PARSE_HEXSTRING_H */ diff --git a/test/core/util/port.h b/test/core/util/port.h index 93788bcab2..ededcbe854 100644 --- a/test/core/util/port.h +++ b/test/core/util/port.h @@ -35,18 +35,19 @@ #define GRPC_TEST_CORE_UTIL_PORT_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* pick a port number that is currently unused by either tcp or udp. return 0 on failure. */ -int grpc_pick_unused_port(); + int grpc_pick_unused_port (); /* pick a port number that is currently unused by either tcp or udp. abort on failure. */ -int grpc_pick_unused_port_or_die(); + int grpc_pick_unused_port_or_die (); #ifdef __cplusplus } #endif -#endif /* GRPC_TEST_CORE_UTIL_PORT_H */ +#endif /* GRPC_TEST_CORE_UTIL_PORT_H */ diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c index 602af23c15..2bb545a376 100644 --- a/test/core/util/port_posix.c +++ b/test/core/util/port_posix.c @@ -57,38 +57,46 @@ static int *chosen_ports = NULL; static size_t num_chosen_ports = 0; -static int has_port_been_chosen(int port) { +static int +has_port_been_chosen (int port) +{ size_t i; - for (i = 0; i < num_chosen_ports; i++) { - if (chosen_ports[i] == port) { - return 1; + for (i = 0; i < num_chosen_ports; i++) + { + if (chosen_ports[i] == port) + { + return 1; + } } - } return 0; } -typedef struct freereq { +typedef struct freereq +{ grpc_pollset pollset; int done; } freereq; -static void destroy_pollset_and_shutdown(void *p, int success, - grpc_closure_list *closure_list) { - grpc_pollset_destroy(p); - grpc_shutdown(); +static void +destroy_pollset_and_shutdown (void *p, int success, grpc_closure_list * closure_list) +{ + grpc_pollset_destroy (p); + grpc_shutdown (); } -static void freed_port_from_server(void *arg, - const grpc_httpcli_response *response, - grpc_closure_list *closure_list) { +static void +freed_port_from_server (void *arg, const grpc_httpcli_response * response, grpc_closure_list * closure_list) +{ freereq *pr = arg; - gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); + gpr_mu_lock (GRPC_POLLSET_MU (&pr->pollset)); pr->done = 1; - grpc_pollset_kick(&pr->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); + grpc_pollset_kick (&pr->pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&pr->pollset)); } -static void free_port_using_server(char *server, int port) { +static void +free_port_using_server (char *server, int port) +{ grpc_httpcli_context context; grpc_httpcli_request req; freereq pr; @@ -96,111 +104,125 @@ static void free_port_using_server(char *server, int port) { grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_closure shutdown_closure; - grpc_init(); + grpc_init (); - memset(&pr, 0, sizeof(pr)); - memset(&req, 0, sizeof(req)); - grpc_pollset_init(&pr.pollset); - grpc_closure_init(&shutdown_closure, destroy_pollset_and_shutdown, - &pr.pollset); + memset (&pr, 0, sizeof (pr)); + memset (&req, 0, sizeof (req)); + grpc_pollset_init (&pr.pollset); + grpc_closure_init (&shutdown_closure, destroy_pollset_and_shutdown, &pr.pollset); req.host = server; - gpr_asprintf(&path, "/drop/%d", port); + gpr_asprintf (&path, "/drop/%d", port); req.path = path; - grpc_httpcli_context_init(&context); - grpc_httpcli_get(&context, &pr.pollset, &req, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), freed_port_from_server, - &pr, &closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); - while (!pr.done) { - grpc_pollset_worker worker; - grpc_pollset_work(&pr.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), &closure_list); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); - - grpc_httpcli_context_destroy(&context); - grpc_closure_list_run(&closure_list); - grpc_pollset_shutdown(&pr.pollset, &shutdown_closure, &closure_list); - grpc_closure_list_run(&closure_list); - gpr_free(path); + grpc_httpcli_context_init (&context); + grpc_httpcli_get (&context, &pr.pollset, &req, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10), freed_port_from_server, &pr, &closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&pr.pollset)); + while (!pr.done) + { + grpc_pollset_worker worker; + grpc_pollset_work (&pr.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1), &closure_list); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&pr.pollset)); + + grpc_httpcli_context_destroy (&context); + grpc_closure_list_run (&closure_list); + grpc_pollset_shutdown (&pr.pollset, &shutdown_closure, &closure_list); + grpc_closure_list_run (&closure_list); + gpr_free (path); } -static void free_chosen_ports() { - char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); - if (env != NULL) { - size_t i; - for (i = 0; i < num_chosen_ports; i++) { - free_port_using_server(env, chosen_ports[i]); +static void +free_chosen_ports () +{ + char *env = gpr_getenv ("GRPC_TEST_PORT_SERVER"); + if (env != NULL) + { + size_t i; + for (i = 0; i < num_chosen_ports; i++) + { + free_port_using_server (env, chosen_ports[i]); + } + gpr_free (env); } - gpr_free(env); - } - gpr_free(chosen_ports); + gpr_free (chosen_ports); } -static void chose_port(int port) { - if (chosen_ports == NULL) { - atexit(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 = gpr_realloc (chosen_ports, sizeof (int) * num_chosen_ports); chosen_ports[num_chosen_ports - 1] = port; } -static int is_port_available(int *port, int is_tcp) { +static int +is_port_available (int *port, int is_tcp) +{ const int proto = is_tcp ? IPPROTO_TCP : 0; - const int fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); + const int fd = socket (AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); int one = 1; struct sockaddr_in addr; - socklen_t alen = sizeof(addr); + socklen_t alen = sizeof (addr); int actual_port; - GPR_ASSERT(*port >= 0); - GPR_ASSERT(*port <= 65535); - if (fd < 0) { - gpr_log(GPR_ERROR, "socket() failed: %s", strerror(errno)); - return 0; - } + GPR_ASSERT (*port >= 0); + GPR_ASSERT (*port <= 65535); + if (fd < 0) + { + gpr_log (GPR_ERROR, "socket() failed: %s", strerror (errno)); + return 0; + } /* Reuseaddr lets us start up a server immediately after it exits */ - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { - gpr_log(GPR_ERROR, "setsockopt() failed: %s", strerror(errno)); - close(fd); - return 0; - } + if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)) < 0) + { + gpr_log (GPR_ERROR, "setsockopt() failed: %s", strerror (errno)); + close (fd); + return 0; + } /* Try binding to port */ addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons((gpr_uint16)*port); - if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - gpr_log(GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror(errno)); - close(fd); - return 0; - } + addr.sin_port = htons ((gpr_uint16) * port); + if (bind (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) + { + gpr_log (GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror (errno)); + close (fd); + return 0; + } /* Get the bound port number */ - if (getsockname(fd, (struct sockaddr *)&addr, &alen) < 0) { - gpr_log(GPR_ERROR, "getsockname() failed: %s", strerror(errno)); - close(fd); - return 0; - } - GPR_ASSERT(alen <= sizeof(addr)); - actual_port = ntohs(addr.sin_port); - GPR_ASSERT(actual_port > 0); - if (*port == 0) { - *port = actual_port; - } else { - GPR_ASSERT(*port == actual_port); - } - - close(fd); + if (getsockname (fd, (struct sockaddr *) &addr, &alen) < 0) + { + gpr_log (GPR_ERROR, "getsockname() failed: %s", strerror (errno)); + close (fd); + return 0; + } + GPR_ASSERT (alen <= sizeof (addr)); + actual_port = ntohs (addr.sin_port); + GPR_ASSERT (actual_port > 0); + if (*port == 0) + { + *port = actual_port; + } + else + { + GPR_ASSERT (*port == actual_port); + } + + close (fd); return 1; } -typedef struct portreq { +typedef struct portreq +{ grpc_pollset pollset; int port; int retries; @@ -208,54 +230,55 @@ typedef struct portreq { grpc_httpcli_context *ctx; } portreq; -static void got_port_from_server(void *arg, - const grpc_httpcli_response *response, - grpc_closure_list *closure_list) { +static void +got_port_from_server (void *arg, const grpc_httpcli_response * response, grpc_closure_list * closure_list) +{ size_t i; int port = 0; portreq *pr = arg; - if (!response || response->status != 200) { - grpc_httpcli_request req; - memset(&req, 0, sizeof(req)); - GPR_ASSERT(pr->retries < 10); - pr->retries++; - req.host = pr->server; - req.path = "/get"; - gpr_log(GPR_DEBUG, "failed port pick from server: retrying"); - sleep(1); - grpc_httpcli_get(pr->ctx, &pr->pollset, &req, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server, - pr, closure_list); - return; - } - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); - for (i = 0; i < response->body_length; i++) { - GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9'); - port = port * 10 + response->body[i] - '0'; - } - GPR_ASSERT(port > 1024); - gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); + if (!response || response->status != 200) + { + grpc_httpcli_request req; + memset (&req, 0, sizeof (req)); + GPR_ASSERT (pr->retries < 10); + pr->retries++; + req.host = pr->server; + req.path = "/get"; + gpr_log (GPR_DEBUG, "failed port pick from server: retrying"); + sleep (1); + grpc_httpcli_get (pr->ctx, &pr->pollset, &req, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10), got_port_from_server, pr, closure_list); + return; + } + GPR_ASSERT (response); + GPR_ASSERT (response->status == 200); + for (i = 0; i < response->body_length; i++) + { + GPR_ASSERT (response->body[i] >= '0' && response->body[i] <= '9'); + port = port * 10 + response->body[i] - '0'; + } + GPR_ASSERT (port > 1024); + gpr_mu_lock (GRPC_POLLSET_MU (&pr->pollset)); pr->port = port; - grpc_pollset_kick(&pr->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); + grpc_pollset_kick (&pr->pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&pr->pollset)); } -static int pick_port_using_server(char *server) { +static int +pick_port_using_server (char *server) +{ grpc_httpcli_context context; grpc_httpcli_request req; portreq pr; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_closure shutdown_closure; - grpc_init(); + grpc_init (); - memset(&pr, 0, sizeof(pr)); - memset(&req, 0, sizeof(req)); - grpc_pollset_init(&pr.pollset); - grpc_closure_init(&shutdown_closure, destroy_pollset_and_shutdown, - &pr.pollset); + memset (&pr, 0, sizeof (pr)); + memset (&req, 0, sizeof (req)); + grpc_pollset_init (&pr.pollset); + grpc_closure_init (&shutdown_closure, destroy_pollset_and_shutdown, &pr.pollset); pr.port = -1; pr.server = server; pr.ctx = &context; @@ -263,27 +286,27 @@ static int pick_port_using_server(char *server) { req.host = server; req.path = "/get"; - grpc_httpcli_context_init(&context); - grpc_httpcli_get(&context, &pr.pollset, &req, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server, - &pr, &closure_list); - grpc_closure_list_run(&closure_list); - gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); - while (pr.port == -1) { - grpc_pollset_worker worker; - grpc_pollset_work(&pr.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), &closure_list); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); - - grpc_httpcli_context_destroy(&context); - grpc_pollset_shutdown(&pr.pollset, &shutdown_closure, &closure_list); - grpc_closure_list_run(&closure_list); + grpc_httpcli_context_init (&context); + grpc_httpcli_get (&context, &pr.pollset, &req, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10), got_port_from_server, &pr, &closure_list); + grpc_closure_list_run (&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&pr.pollset)); + while (pr.port == -1) + { + grpc_pollset_worker worker; + grpc_pollset_work (&pr.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1), &closure_list); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&pr.pollset)); + + grpc_httpcli_context_destroy (&context); + grpc_pollset_shutdown (&pr.pollset, &shutdown_closure, &closure_list); + grpc_closure_list_run (&closure_list); return pr.port; } -int grpc_pick_unused_port(void) { +int +grpc_pick_unused_port (void) +{ /* We repeatedly pick a port and then see whether or not it is available for use both as a TCP socket and a UDP socket. First, we pick a random large port number. For subsequent @@ -300,55 +323,68 @@ int grpc_pick_unused_port(void) { int is_tcp = 1; int trial = 0; - char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); - if (env) { - int port = pick_port_using_server(env); - gpr_free(env); - if (port != 0) { - chose_port(port); - } - return port; - } - - for (;;) { - int port; - trial++; - if (trial == 1) { - port = getpid() % (65536 - 30000) + 30000; - } else if (trial <= NUM_RANDOM_PORTS_TO_PICK) { - port = rand() % (65536 - 30000) + 30000; - } else { - port = 0; - } - - if (has_port_been_chosen(port)) { - continue; + char *env = gpr_getenv ("GRPC_TEST_PORT_SERVER"); + if (env) + { + int port = pick_port_using_server (env); + gpr_free (env); + if (port != 0) + { + chose_port (port); + } + return port; } - if (!is_port_available(&port, is_tcp)) { - continue; + for (;;) + { + int port; + trial++; + if (trial == 1) + { + port = getpid () % (65536 - 30000) + 30000; + } + else if (trial <= NUM_RANDOM_PORTS_TO_PICK) + { + port = rand () % (65536 - 30000) + 30000; + } + else + { + port = 0; + } + + if (has_port_been_chosen (port)) + { + continue; + } + + if (!is_port_available (&port, is_tcp)) + { + continue; + } + + GPR_ASSERT (port > 0); + /* Check that the port # is free for the other type of socket also */ + if (!is_port_available (&port, !is_tcp)) + { + /* In the next iteration trial to bind to the other type first + because perhaps it is more rare. */ + is_tcp = !is_tcp; + continue; + } + + chose_port (port); + return port; } - GPR_ASSERT(port > 0); - /* Check that the port # is free for the other type of socket also */ - if (!is_port_available(&port, !is_tcp)) { - /* In the next iteration trial to bind to the other type first - because perhaps it is more rare. */ - is_tcp = !is_tcp; - continue; - } - - chose_port(port); - return port; - } - /* The port iterator reached the end without finding a suitable port. */ return 0; } -int grpc_pick_unused_port_or_die(void) { - int port = grpc_pick_unused_port(); - GPR_ASSERT(port > 0); +int +grpc_pick_unused_port_or_die (void) +{ + int port = grpc_pick_unused_port (); + GPR_ASSERT (port > 0); return port; } diff --git a/test/core/util/port_windows.c b/test/core/util/port_windows.c index a57fe692e1..fefe33e9ba 100644 --- a/test/core/util/port_windows.c +++ b/test/core/util/port_windows.c @@ -55,141 +55,167 @@ static int *chosen_ports = NULL; static size_t num_chosen_ports = 0; -static int has_port_been_chosen(int port) { +static int +has_port_been_chosen (int port) +{ size_t i; - for (i = 0; i < num_chosen_ports; i++) { - if (chosen_ports[i] == port) { - return 1; + for (i = 0; i < num_chosen_ports; i++) + { + if (chosen_ports[i] == port) + { + return 1; + } } - } return 0; } -static void free_chosen_ports() { gpr_free(chosen_ports); } +static void +free_chosen_ports () +{ + gpr_free (chosen_ports); +} -static void chose_port(int port) { - if (chosen_ports == NULL) { - atexit(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 = gpr_realloc (chosen_ports, sizeof (int) * num_chosen_ports); chosen_ports[num_chosen_ports - 1] = port; } -static int is_port_available(int *port, int is_tcp) { +static int +is_port_available (int *port, int is_tcp) +{ const int proto = is_tcp ? IPPROTO_TCP : 0; - const SOCKET fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); + const SOCKET fd = socket (AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); int one = 1; struct sockaddr_in addr; - socklen_t alen = sizeof(addr); + socklen_t alen = sizeof (addr); int actual_port; - GPR_ASSERT(*port >= 0); - GPR_ASSERT(*port <= 65535); - if (INVALID_SOCKET == fd) { - gpr_log(GPR_ERROR, "socket() failed: %s", strerror(errno)); - return 0; - } + GPR_ASSERT (*port >= 0); + GPR_ASSERT (*port <= 65535); + if (INVALID_SOCKET == fd) + { + gpr_log (GPR_ERROR, "socket() failed: %s", strerror (errno)); + return 0; + } /* Reuseaddr lets us start up a server immediately after it exits */ - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, - sizeof(one)) < 0) { - gpr_log(GPR_ERROR, "setsockopt() failed: %s", strerror(errno)); - closesocket(fd); - return 0; - } + if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &one, sizeof (one)) < 0) + { + gpr_log (GPR_ERROR, "setsockopt() failed: %s", strerror (errno)); + closesocket (fd); + return 0; + } /* Try binding to port */ addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons(*port); - if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - gpr_log(GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror(errno)); - closesocket(fd); - return 0; - } + addr.sin_port = htons (*port); + if (bind (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) + { + gpr_log (GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror (errno)); + closesocket (fd); + return 0; + } /* Get the bound port number */ - if (getsockname(fd, (struct sockaddr *)&addr, &alen) < 0) { - gpr_log(GPR_ERROR, "getsockname() failed: %s", strerror(errno)); - closesocket(fd); - return 0; - } - GPR_ASSERT(alen <= sizeof(addr)); - actual_port = ntohs(addr.sin_port); - GPR_ASSERT(actual_port > 0); - if (*port == 0) { - *port = actual_port; - } else { - GPR_ASSERT(*port == actual_port); - } - - closesocket(fd); + if (getsockname (fd, (struct sockaddr *) &addr, &alen) < 0) + { + gpr_log (GPR_ERROR, "getsockname() failed: %s", strerror (errno)); + closesocket (fd); + return 0; + } + GPR_ASSERT (alen <= sizeof (addr)); + actual_port = ntohs (addr.sin_port); + GPR_ASSERT (actual_port > 0); + if (*port == 0) + { + *port = actual_port; + } + else + { + GPR_ASSERT (*port == actual_port); + } + + closesocket (fd); return 1; } -typedef struct portreq { +typedef struct portreq +{ grpc_pollset pollset; int port; } portreq; -static void got_port_from_server(void *arg, - const grpc_httpcli_response *response) { +static void +got_port_from_server (void *arg, const grpc_httpcli_response * response) +{ size_t i; int port = 0; portreq *pr = arg; - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); - for (i = 0; i < response->body_length; i++) { - GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9'); - port = port * 10 + response->body[i] - '0'; - } - GPR_ASSERT(port > 1024); - gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); + GPR_ASSERT (response); + GPR_ASSERT (response->status == 200); + for (i = 0; i < response->body_length; i++) + { + GPR_ASSERT (response->body[i] >= '0' && response->body[i] <= '9'); + port = port * 10 + response->body[i] - '0'; + } + GPR_ASSERT (port > 1024); + gpr_mu_lock (GRPC_POLLSET_MU (&pr->pollset)); pr->port = port; - grpc_pollset_kick(&pr->pollset, NULL); - gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); + grpc_pollset_kick (&pr->pollset, NULL); + gpr_mu_unlock (GRPC_POLLSET_MU (&pr->pollset)); } -static void destroy_pollset_and_shutdown(void *p) { - grpc_pollset_destroy(p); - grpc_shutdown(); +static void +destroy_pollset_and_shutdown (void *p) +{ + grpc_pollset_destroy (p); + grpc_shutdown (); } -static int pick_port_using_server(char *server) { +static int +pick_port_using_server (char *server) +{ grpc_httpcli_context context; grpc_httpcli_request req; portreq pr; - grpc_init(); + grpc_init (); - memset(&pr, 0, sizeof(pr)); - memset(&req, 0, sizeof(req)); - grpc_pollset_init(&pr.pollset); + memset (&pr, 0, sizeof (pr)); + memset (&req, 0, sizeof (req)); + grpc_pollset_init (&pr.pollset); pr.port = -1; req.host = server; req.path = "/get"; - grpc_httpcli_context_init(&context); - grpc_httpcli_get(&context, &pr.pollset, &req, - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server, - &pr); - gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); - while (pr.port == -1) { - grpc_pollset_worker worker; - grpc_pollset_work(&pr.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1)); - } - gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); - - grpc_httpcli_context_destroy(&context); - grpc_pollset_shutdown(&pr.pollset, destroy_pollset_and_shutdown, &pr.pollset); + grpc_httpcli_context_init (&context); + grpc_httpcli_get (&context, &pr.pollset, &req, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (10), got_port_from_server, &pr); + gpr_mu_lock (GRPC_POLLSET_MU (&pr.pollset)); + while (pr.port == -1) + { + grpc_pollset_worker worker; + grpc_pollset_work (&pr.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1)); + } + gpr_mu_unlock (GRPC_POLLSET_MU (&pr.pollset)); + + grpc_httpcli_context_destroy (&context); + grpc_pollset_shutdown (&pr.pollset, destroy_pollset_and_shutdown, &pr.pollset); return pr.port; } -int grpc_pick_unused_port(void) { +int +grpc_pick_unused_port (void) +{ /* We repeatedly pick a port and then see whether or not it is available for use both as a TCP socket and a UDP socket. First, we pick a random large port number. For subsequent @@ -206,57 +232,70 @@ int grpc_pick_unused_port(void) { int is_tcp = 1; int trial = 0; - char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); - if (env) { - int port = pick_port_using_server(env); - gpr_free(env); - if (port != 0) { - return port; - } - } - - for (;;) { - int port; - trial++; - if (trial == 1) { - port = _getpid() % (65536 - 30000) + 30000; - } else if (trial <= NUM_RANDOM_PORTS_TO_PICK) { - port = rand() % (65536 - 30000) + 30000; - } else { - port = 0; - } - - if (has_port_been_chosen(port)) { - continue; + char *env = gpr_getenv ("GRPC_TEST_PORT_SERVER"); + if (env) + { + int port = pick_port_using_server (env); + gpr_free (env); + if (port != 0) + { + return port; + } } - if (!is_port_available(&port, is_tcp)) { - continue; - } - - GPR_ASSERT(port > 0); - /* Check that the port # is free for the other type of socket also */ - if (!is_port_available(&port, !is_tcp)) { - /* In the next iteration trial to bind to the other type first - because perhaps it is more rare. */ - is_tcp = !is_tcp; - continue; + for (;;) + { + int port; + trial++; + if (trial == 1) + { + port = _getpid () % (65536 - 30000) + 30000; + } + else if (trial <= NUM_RANDOM_PORTS_TO_PICK) + { + port = rand () % (65536 - 30000) + 30000; + } + else + { + port = 0; + } + + if (has_port_been_chosen (port)) + { + continue; + } + + if (!is_port_available (&port, is_tcp)) + { + continue; + } + + GPR_ASSERT (port > 0); + /* Check that the port # is free for the other type of socket also */ + if (!is_port_available (&port, !is_tcp)) + { + /* In the next iteration trial to bind to the other type first + because perhaps it is more rare. */ + is_tcp = !is_tcp; + continue; + } + + /* TODO(ctiller): consider caching this port in some structure, to avoid + handing it out again */ + + chose_port (port); + return port; } - /* TODO(ctiller): consider caching this port in some structure, to avoid - handing it out again */ - - chose_port(port); - return port; - } - /* The port iterator reached the end without finding a suitable port. */ return 0; } -int grpc_pick_unused_port_or_die(void) { - int port = grpc_pick_unused_port(); - GPR_ASSERT(port > 0); +int +grpc_pick_unused_port_or_die (void) +{ + int port = grpc_pick_unused_port (); + GPR_ASSERT (port > 0); return port; } diff --git a/test/core/util/reconnect_server.c b/test/core/util/reconnect_server.c index 8092cbc73a..f730303004 100644 --- a/test/core/util/reconnect_server.c +++ b/test/core/util/reconnect_server.c @@ -45,133 +45,151 @@ #include "src/core/iomgr/tcp_server.h" #include "test/core/util/port.h" -static void pretty_print_backoffs(reconnect_server *server) { +static void +pretty_print_backoffs (reconnect_server * server) +{ gpr_timespec diff; int i = 1; double expected_backoff = 1000.0, backoff; timestamp_list *head = server->head; - gpr_log(GPR_INFO, "reconnect server: new connection"); - for (head = server->head; head && head->next; head = head->next, i++) { - diff = gpr_time_sub(head->next->timestamp, head->timestamp); - backoff = gpr_time_to_millis(diff); - gpr_log(GPR_INFO, - "retry %2d:backoff %6.2fs,expected backoff %6.2fs, jitter %4.2f%%", - i, backoff / 1000.0, expected_backoff / 1000.0, - (backoff - expected_backoff) * 100.0 / expected_backoff); - expected_backoff *= 1.6; - if (expected_backoff > 120 * 1000) { - expected_backoff = 120 * 1000; + gpr_log (GPR_INFO, "reconnect server: new connection"); + for (head = server->head; head && head->next; head = head->next, i++) + { + diff = gpr_time_sub (head->next->timestamp, head->timestamp); + backoff = gpr_time_to_millis (diff); + gpr_log (GPR_INFO, "retry %2d:backoff %6.2fs,expected backoff %6.2fs, jitter %4.2f%%", i, backoff / 1000.0, expected_backoff / 1000.0, (backoff - expected_backoff) * 100.0 / expected_backoff); + expected_backoff *= 1.6; + if (expected_backoff > 120 * 1000) + { + expected_backoff = 120 * 1000; + } } - } } -static void on_connect(void *arg, grpc_endpoint *tcp, - grpc_closure_list *closure_list) { +static void +on_connect (void *arg, grpc_endpoint * tcp, grpc_closure_list * closure_list) +{ char *peer; char *last_colon; - reconnect_server *server = (reconnect_server *)arg; - gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); + reconnect_server *server = (reconnect_server *) arg; + gpr_timespec now = gpr_now (GPR_CLOCK_REALTIME); timestamp_list *new_tail; - peer = grpc_endpoint_get_peer(tcp); - grpc_endpoint_shutdown(tcp, closure_list); - grpc_endpoint_destroy(tcp, closure_list); - if (peer) { - last_colon = strrchr(peer, ':'); - if (server->peer == NULL) { - server->peer = peer; - } else { - if (last_colon == NULL) { - gpr_log(GPR_ERROR, "peer does not contain a ':'"); - } else if (strncmp(server->peer, peer, (size_t)(last_colon - peer)) != - 0) { - gpr_log(GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer); - } - gpr_free(peer); + peer = grpc_endpoint_get_peer (tcp); + grpc_endpoint_shutdown (tcp, closure_list); + grpc_endpoint_destroy (tcp, closure_list); + if (peer) + { + last_colon = strrchr (peer, ':'); + if (server->peer == NULL) + { + server->peer = peer; + } + else + { + if (last_colon == NULL) + { + gpr_log (GPR_ERROR, "peer does not contain a ':'"); + } + else if (strncmp (server->peer, peer, (size_t) (last_colon - peer)) != 0) + { + gpr_log (GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer); + } + gpr_free (peer); + } } - } - new_tail = gpr_malloc(sizeof(timestamp_list)); + new_tail = gpr_malloc (sizeof (timestamp_list)); new_tail->timestamp = now; new_tail->next = NULL; - if (server->tail == NULL) { - server->head = new_tail; - server->tail = new_tail; - } else { - server->tail->next = new_tail; - server->tail = new_tail; - } - pretty_print_backoffs(server); + if (server->tail == NULL) + { + server->head = new_tail; + server->tail = new_tail; + } + else + { + server->tail->next = new_tail; + server->tail = new_tail; + } + pretty_print_backoffs (server); } -void reconnect_server_init(reconnect_server *server) { - grpc_init(); +void +reconnect_server_init (reconnect_server * server) +{ + grpc_init (); server->tcp_server = NULL; - grpc_pollset_init(&server->pollset); + grpc_pollset_init (&server->pollset); server->pollsets[0] = &server->pollset; server->head = NULL; server->tail = NULL; server->peer = NULL; } -void reconnect_server_start(reconnect_server *server, int port) { +void +reconnect_server_start (reconnect_server * server, int port) +{ struct sockaddr_in addr; int port_added; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; addr.sin_family = AF_INET; - addr.sin_port = htons((gpr_uint16)port); - memset(&addr.sin_addr, 0, sizeof(addr.sin_addr)); + addr.sin_port = htons ((gpr_uint16) port); + memset (&addr.sin_addr, 0, sizeof (addr.sin_addr)); - server->tcp_server = grpc_tcp_server_create(); - port_added = - grpc_tcp_server_add_port(server->tcp_server, &addr, sizeof(addr)); - GPR_ASSERT(port_added == port); + server->tcp_server = grpc_tcp_server_create (); + port_added = grpc_tcp_server_add_port (server->tcp_server, &addr, sizeof (addr)); + GPR_ASSERT (port_added == port); - grpc_tcp_server_start(server->tcp_server, server->pollsets, 1, on_connect, - server, &closure_list); - gpr_log(GPR_INFO, "reconnect tcp server listening on 0.0.0.0:%d", port); + grpc_tcp_server_start (server->tcp_server, server->pollsets, 1, on_connect, server, &closure_list); + gpr_log (GPR_INFO, "reconnect tcp server listening on 0.0.0.0:%d", port); - grpc_closure_list_run(&closure_list); + grpc_closure_list_run (&closure_list); } -void reconnect_server_poll(reconnect_server *server, int seconds) { +void +reconnect_server_poll (reconnect_server * server, int seconds) +{ grpc_pollset_worker worker; - gpr_timespec deadline = - gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), - gpr_time_from_seconds(seconds, GPR_TIMESPAN)); + gpr_timespec deadline = gpr_time_add (gpr_now (GPR_CLOCK_MONOTONIC), + gpr_time_from_seconds (seconds, GPR_TIMESPAN)); grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; - gpr_mu_lock(GRPC_POLLSET_MU(&server->pollset)); - grpc_pollset_work(&server->pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), - deadline, &closure_list); - gpr_mu_unlock(GRPC_POLLSET_MU(&server->pollset)); - grpc_closure_list_run(&closure_list); + gpr_mu_lock (GRPC_POLLSET_MU (&server->pollset)); + grpc_pollset_work (&server->pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), deadline, &closure_list); + gpr_mu_unlock (GRPC_POLLSET_MU (&server->pollset)); + grpc_closure_list_run (&closure_list); } -void reconnect_server_clear_timestamps(reconnect_server *server) { +void +reconnect_server_clear_timestamps (reconnect_server * server) +{ timestamp_list *new_head = server->head; - while (server->head) { - new_head = server->head->next; - gpr_free(server->head); - server->head = new_head; - } + while (server->head) + { + new_head = server->head->next; + gpr_free (server->head); + server->head = new_head; + } server->tail = NULL; - gpr_free(server->peer); + gpr_free (server->peer); server->peer = NULL; } -static void do_nothing(void *ignored, int success, - grpc_closure_list *closure_list) {} +static void +do_nothing (void *ignored, int success, grpc_closure_list * closure_list) +{ +} -void reconnect_server_destroy(reconnect_server *server) { +void +reconnect_server_destroy (reconnect_server * server) +{ grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; grpc_closure do_nothing_closure[2]; - grpc_closure_init(&do_nothing_closure[0], do_nothing, NULL); - grpc_closure_init(&do_nothing_closure[1], do_nothing, NULL); - grpc_tcp_server_destroy(server->tcp_server, &do_nothing_closure[0], - &closure_list); - reconnect_server_clear_timestamps(server); - grpc_pollset_shutdown(&server->pollset, &do_nothing_closure[1], - &closure_list); - grpc_closure_list_run(&closure_list); - grpc_pollset_destroy(&server->pollset); - grpc_shutdown(); + grpc_closure_init (&do_nothing_closure[0], do_nothing, NULL); + grpc_closure_init (&do_nothing_closure[1], do_nothing, NULL); + grpc_tcp_server_destroy (server->tcp_server, &do_nothing_closure[0], &closure_list); + reconnect_server_clear_timestamps (server); + grpc_pollset_shutdown (&server->pollset, &do_nothing_closure[1], &closure_list); + grpc_closure_list_run (&closure_list); + grpc_pollset_destroy (&server->pollset); + grpc_shutdown (); } diff --git a/test/core/util/reconnect_server.h b/test/core/util/reconnect_server.h index 8a278e0c7a..aaf277390c 100644 --- a/test/core/util/reconnect_server.h +++ b/test/core/util/reconnect_server.h @@ -39,31 +39,34 @@ #include "src/core/iomgr/tcp_server.h" #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -typedef struct timestamp_list { - gpr_timespec timestamp; - struct timestamp_list *next; -} timestamp_list; + typedef struct timestamp_list + { + gpr_timespec timestamp; + struct timestamp_list *next; + } timestamp_list; -typedef struct reconnect_server { - grpc_tcp_server *tcp_server; - grpc_pollset pollset; - grpc_pollset *pollsets[1]; - timestamp_list *head; - timestamp_list *tail; - char *peer; -} reconnect_server; + typedef struct reconnect_server + { + grpc_tcp_server *tcp_server; + grpc_pollset pollset; + grpc_pollset *pollsets[1]; + timestamp_list *head; + timestamp_list *tail; + char *peer; + } reconnect_server; -void reconnect_server_init(reconnect_server *server); -void reconnect_server_start(reconnect_server *server, int port); -void reconnect_server_poll(reconnect_server *server, int seconds); -void reconnect_server_destroy(reconnect_server *server); -void reconnect_server_clear_timestamps(reconnect_server *server); + void reconnect_server_init (reconnect_server * server); + void reconnect_server_start (reconnect_server * server, int port); + void reconnect_server_poll (reconnect_server * server, int seconds); + void reconnect_server_destroy (reconnect_server * server); + void reconnect_server_clear_timestamps (reconnect_server * server); #ifdef __cplusplus } #endif -#endif /* GRPC_TEST_CORE_UTIL_RECONNECT_SERVER_H */ +#endif /* GRPC_TEST_CORE_UTIL_RECONNECT_SERVER_H */ diff --git a/test/core/util/slice_splitter.c b/test/core/util/slice_splitter.c index 0f05072e50..f0282c0aaa 100644 --- a/test/core/util/slice_splitter.c +++ b/test/core/util/slice_splitter.c @@ -38,101 +38,114 @@ #include <grpc/support/alloc.h> #include <grpc/support/useful.h> -const char *grpc_slice_split_mode_name(grpc_slice_split_mode mode) { - switch (mode) { +const char * +grpc_slice_split_mode_name (grpc_slice_split_mode mode) +{ + switch (mode) + { case GRPC_SLICE_SPLIT_IDENTITY: return "identity"; case GRPC_SLICE_SPLIT_MERGE_ALL: return "merge_all"; case GRPC_SLICE_SPLIT_ONE_BYTE: return "one_byte"; - } + } return "error"; } -void grpc_split_slices(grpc_slice_split_mode mode, gpr_slice *src_slices, - size_t src_slice_count, gpr_slice **dst_slices, - size_t *dst_slice_count) { +void +grpc_split_slices (grpc_slice_split_mode mode, gpr_slice * src_slices, size_t src_slice_count, gpr_slice ** dst_slices, size_t * dst_slice_count) +{ size_t i, j; size_t length; - switch (mode) { + switch (mode) + { case GRPC_SLICE_SPLIT_IDENTITY: *dst_slice_count = src_slice_count; - *dst_slices = gpr_malloc(sizeof(gpr_slice) * src_slice_count); - for (i = 0; i < src_slice_count; i++) { - (*dst_slices)[i] = src_slices[i]; - gpr_slice_ref((*dst_slices)[i]); - } + *dst_slices = gpr_malloc (sizeof (gpr_slice) * src_slice_count); + for (i = 0; i < src_slice_count; i++) + { + (*dst_slices)[i] = src_slices[i]; + gpr_slice_ref ((*dst_slices)[i]); + } break; case GRPC_SLICE_SPLIT_MERGE_ALL: *dst_slice_count = 1; length = 0; - for (i = 0; i < src_slice_count; i++) { - length += GPR_SLICE_LENGTH(src_slices[i]); - } - *dst_slices = gpr_malloc(sizeof(gpr_slice)); - **dst_slices = gpr_slice_malloc(length); + for (i = 0; i < src_slice_count; i++) + { + length += GPR_SLICE_LENGTH (src_slices[i]); + } + *dst_slices = gpr_malloc (sizeof (gpr_slice)); + **dst_slices = gpr_slice_malloc (length); length = 0; - for (i = 0; i < src_slice_count; i++) { - memcpy(GPR_SLICE_START_PTR(**dst_slices) + length, - GPR_SLICE_START_PTR(src_slices[i]), - GPR_SLICE_LENGTH(src_slices[i])); - length += GPR_SLICE_LENGTH(src_slices[i]); - } + for (i = 0; i < src_slice_count; i++) + { + memcpy (GPR_SLICE_START_PTR (**dst_slices) + length, GPR_SLICE_START_PTR (src_slices[i]), GPR_SLICE_LENGTH (src_slices[i])); + length += GPR_SLICE_LENGTH (src_slices[i]); + } break; case GRPC_SLICE_SPLIT_ONE_BYTE: length = 0; - for (i = 0; i < src_slice_count; i++) { - length += GPR_SLICE_LENGTH(src_slices[i]); - } + for (i = 0; i < src_slice_count; i++) + { + length += GPR_SLICE_LENGTH (src_slices[i]); + } *dst_slice_count = length; - *dst_slices = gpr_malloc(sizeof(gpr_slice) * length); + *dst_slices = gpr_malloc (sizeof (gpr_slice) * length); length = 0; - for (i = 0; i < src_slice_count; i++) { - for (j = 0; j < GPR_SLICE_LENGTH(src_slices[i]); j++) { - (*dst_slices)[length] = gpr_slice_sub(src_slices[i], j, j + 1); - length++; - } - } + for (i = 0; i < src_slice_count; i++) + { + for (j = 0; j < GPR_SLICE_LENGTH (src_slices[i]); j++) + { + (*dst_slices)[length] = gpr_slice_sub (src_slices[i], j, j + 1); + length++; + } + } break; - } + } } -void grpc_split_slices_to_buffer(grpc_slice_split_mode mode, - gpr_slice *src_slices, size_t src_slice_count, - gpr_slice_buffer *dst) { +void +grpc_split_slices_to_buffer (grpc_slice_split_mode mode, gpr_slice * src_slices, size_t src_slice_count, gpr_slice_buffer * dst) +{ gpr_slice *slices; size_t nslices; size_t i; - grpc_split_slices(mode, src_slices, src_slice_count, &slices, &nslices); - for (i = 0; i < nslices; i++) { - /* add indexed to avoid re-merging split slices */ - gpr_slice_buffer_add_indexed(dst, slices[i]); - } - gpr_free(slices); + grpc_split_slices (mode, src_slices, src_slice_count, &slices, &nslices); + for (i = 0; i < nslices; i++) + { + /* add indexed to avoid re-merging split slices */ + gpr_slice_buffer_add_indexed (dst, slices[i]); + } + gpr_free (slices); } -void grpc_split_slice_buffer(grpc_slice_split_mode mode, gpr_slice_buffer *src, - gpr_slice_buffer *dst) { - grpc_split_slices_to_buffer(mode, src->slices, src->count, dst); +void +grpc_split_slice_buffer (grpc_slice_split_mode mode, gpr_slice_buffer * src, gpr_slice_buffer * dst) +{ + grpc_split_slices_to_buffer (mode, src->slices, src->count, dst); } -gpr_slice grpc_slice_merge(gpr_slice *slices, size_t nslices) { +gpr_slice +grpc_slice_merge (gpr_slice * slices, size_t nslices) +{ gpr_uint8 *out = NULL; size_t length = 0; size_t capacity = 0; size_t i; - for (i = 0; i < nslices; i++) { - if (GPR_SLICE_LENGTH(slices[i]) + length > capacity) { - capacity = GPR_MAX(capacity * 2, GPR_SLICE_LENGTH(slices[i]) + length); - out = gpr_realloc(out, capacity); + for (i = 0; i < nslices; i++) + { + if (GPR_SLICE_LENGTH (slices[i]) + length > capacity) + { + capacity = GPR_MAX (capacity * 2, GPR_SLICE_LENGTH (slices[i]) + length); + out = gpr_realloc (out, capacity); + } + memcpy (out + length, GPR_SLICE_START_PTR (slices[i]), GPR_SLICE_LENGTH (slices[i])); + length += GPR_SLICE_LENGTH (slices[i]); } - memcpy(out + length, GPR_SLICE_START_PTR(slices[i]), - GPR_SLICE_LENGTH(slices[i])); - length += GPR_SLICE_LENGTH(slices[i]); - } - return gpr_slice_new(out, length, gpr_free); + return gpr_slice_new (out, length, gpr_free); } diff --git a/test/core/util/slice_splitter.h b/test/core/util/slice_splitter.h index d030c2cb55..992001730c 100644 --- a/test/core/util/slice_splitter.h +++ b/test/core/util/slice_splitter.h @@ -40,7 +40,8 @@ #include <grpc/support/slice.h> #include <grpc/support/slice_buffer.h> -typedef enum { +typedef enum +{ /* merge all input slices into a single slice */ GRPC_SLICE_SPLIT_MERGE_ALL, /* leave slices as is */ @@ -51,18 +52,13 @@ typedef enum { /* allocates *dst_slices; caller must unref all slices in dst_slices then free it */ -void grpc_split_slices(grpc_slice_split_mode mode, gpr_slice *src_slices, - size_t src_slice_count, gpr_slice **dst_slices, - size_t *dst_slice_count); +void grpc_split_slices (grpc_slice_split_mode mode, gpr_slice * src_slices, size_t src_slice_count, gpr_slice ** dst_slices, size_t * dst_slice_count); -void grpc_split_slices_to_buffer(grpc_slice_split_mode mode, - gpr_slice *src_slices, size_t src_slice_count, - gpr_slice_buffer *dst); -void grpc_split_slice_buffer(grpc_slice_split_mode mode, gpr_slice_buffer *src, - gpr_slice_buffer *dst); +void grpc_split_slices_to_buffer (grpc_slice_split_mode mode, gpr_slice * src_slices, size_t src_slice_count, gpr_slice_buffer * dst); +void grpc_split_slice_buffer (grpc_slice_split_mode mode, gpr_slice_buffer * src, gpr_slice_buffer * dst); -gpr_slice grpc_slice_merge(gpr_slice *slices, size_t nslices); +gpr_slice grpc_slice_merge (gpr_slice * slices, size_t nslices); -const char *grpc_slice_split_mode_name(grpc_slice_split_mode mode); +const char *grpc_slice_split_mode_name (grpc_slice_split_mode mode); #endif /* GRPC_TEST_CORE_UTIL_SLICE_SPLITTER_H */ diff --git a/test/core/util/test_config.c b/test/core/util/test_config.c index f5d5cdb57c..eb2ff22c8b 100644 --- a/test/core/util/test_config.c +++ b/test/core/util/test_config.c @@ -42,58 +42,81 @@ double g_fixture_slowdown_factor = 1.0; #if GPR_GETPID_IN_UNISTD_H #include <unistd.h> -static unsigned seed(void) { return (unsigned)getpid(); } +static unsigned +seed (void) +{ + return (unsigned) getpid (); +} #endif #if GPR_GETPID_IN_PROCESS_H #include <process.h> -static unsigned seed(void) { return _getpid(); } +static unsigned +seed (void) +{ + return _getpid (); +} #endif #if GPR_WINDOWS_CRASH_HANDLER -LONG crash_handler(struct _EXCEPTION_POINTERS* ex_info) { - gpr_log(GPR_DEBUG, "Exception handler called, dumping information"); - while (ex_info->ExceptionRecord) { - DWORD code = ex_info->ExceptionRecord->ExceptionCode; - DWORD flgs = ex_info->ExceptionRecord->ExceptionFlags; - PVOID addr = ex_info->ExceptionRecord->ExceptionAddress; - gpr_log("code: %x - flags: %d - address: %p", code, flgs, addr); - ex_info->ExceptionRecord = ex_info->ExceptionRecord->ExceptionRecord; - } - if (IsDebuggerPresent()) { - __debugbreak(); - } else { - _exit(1); - } +LONG +crash_handler (struct _EXCEPTION_POINTERS * ex_info) +{ + gpr_log (GPR_DEBUG, "Exception handler called, dumping information"); + while (ex_info->ExceptionRecord) + { + DWORD code = ex_info->ExceptionRecord->ExceptionCode; + DWORD flgs = ex_info->ExceptionRecord->ExceptionFlags; + PVOID addr = ex_info->ExceptionRecord->ExceptionAddress; + gpr_log ("code: %x - flags: %d - address: %p", code, flgs, addr); + ex_info->ExceptionRecord = ex_info->ExceptionRecord->ExceptionRecord; + } + if (IsDebuggerPresent ()) + { + __debugbreak (); + } + else + { + _exit (1); + } return EXCEPTION_EXECUTE_HANDLER; } -void abort_handler(int sig) { - gpr_log(GPR_DEBUG, "Abort handler called."); - if (IsDebuggerPresent()) { - __debugbreak(); - } else { - _exit(1); - } +void +abort_handler (int sig) +{ + gpr_log (GPR_DEBUG, "Abort handler called."); + if (IsDebuggerPresent ()) + { + __debugbreak (); + } + else + { + _exit (1); + } } -static void install_crash_handler() { - SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)crash_handler); - _set_abort_behavior(0, _WRITE_ABORT_MSG); - _set_abort_behavior(0, _CALL_REPORTFAULT); - signal(SIGABRT, abort_handler); +static void +install_crash_handler () +{ + SetUnhandledExceptionFilter ((LPTOP_LEVEL_EXCEPTION_FILTER) crash_handler); + _set_abort_behavior (0, _WRITE_ABORT_MSG); + _set_abort_behavior (0, _CALL_REPORTFAULT); + signal (SIGABRT, abort_handler); } #else -static void install_crash_handler() {} +static void +install_crash_handler () +{ +} #endif -void grpc_test_init(int argc, char** argv) { - install_crash_handler(); - gpr_log(GPR_DEBUG, "test slowdown: machine=%f build=%f total=%f", - (double)GRPC_TEST_SLOWDOWN_MACHINE_FACTOR, - (double)GRPC_TEST_SLOWDOWN_BUILD_FACTOR, - (double)GRPC_TEST_SLOWDOWN_FACTOR); +void +grpc_test_init (int argc, char **argv) +{ + install_crash_handler (); + gpr_log (GPR_DEBUG, "test slowdown: machine=%f build=%f total=%f", (double) GRPC_TEST_SLOWDOWN_MACHINE_FACTOR, (double) GRPC_TEST_SLOWDOWN_BUILD_FACTOR, (double) GRPC_TEST_SLOWDOWN_FACTOR); /* seed rng with pid, so we don't end up with the same random numbers as a concurrently running test binary */ - srand(seed()); + srand (seed ()); } diff --git a/test/core/util/test_config.h b/test/core/util/test_config.h index 15b71747fb..05057b9893 100644 --- a/test/core/util/test_config.h +++ b/test/core/util/test_config.h @@ -37,8 +37,9 @@ #include <grpc/support/time.h> #ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ +extern "C" +{ +#endif /* __cplusplus */ #ifndef GRPC_TEST_SLOWDOWN_BUILD_FACTOR #define GRPC_TEST_SLOWDOWN_BUILD_FACTOR 1.0 @@ -48,7 +49,7 @@ extern "C" { #define GRPC_TEST_SLOWDOWN_MACHINE_FACTOR 1.0 #endif -extern double g_fixture_slowdown_factor; + extern double g_fixture_slowdown_factor; #define GRPC_TEST_SLOWDOWN_FACTOR \ (GRPC_TEST_SLOWDOWN_BUILD_FACTOR * GRPC_TEST_SLOWDOWN_MACHINE_FACTOR * \ @@ -70,10 +71,10 @@ extern double g_fixture_slowdown_factor; #define GRPC_TEST_PICK_PORT #endif -void grpc_test_init(int argc, char **argv); + void grpc_test_init (int argc, char **argv); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ -#endif /* GRPC_TEST_CORE_UTIL_TEST_CONFIG_H */ +#endif /* GRPC_TEST_CORE_UTIL_TEST_CONFIG_H */ |