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)); 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); 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); gpr_thd_options_set_joinable(&opt); GPR_ASSERT(gpr_thd_new(&proxy->thd, thread_main, proxy, &opt)); request_call(proxy); return proxy; }
static void prepare_test(int is_client) { int port; char *server_hostport; grpc_op *op; g_state.is_client = is_client; grpc_metadata_array_init(&g_state.initial_metadata_recv); grpc_metadata_array_init(&g_state.trailing_metadata_recv); g_state.deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2); g_state.cq = grpc_completion_queue_create(NULL); g_state.cqv = cq_verifier_create(g_state.cq); g_state.details = NULL; g_state.details_capacity = 0; if (is_client) { /* create a call, channel to a non existant server */ g_state.chan = grpc_insecure_channel_create("nonexistant:54321", NULL, NULL); g_state.call = grpc_channel_create_call( g_state.chan, NULL, GRPC_PROPAGATE_DEFAULTS, g_state.cq, "/Foo", "nonexistant", g_state.deadline, NULL); } else { g_state.server = grpc_server_create(NULL, NULL); grpc_server_register_completion_queue(g_state.server, g_state.cq, NULL); port = grpc_pick_unused_port_or_die(); gpr_join_host_port(&server_hostport, "0.0.0.0", port); grpc_server_add_insecure_http2_port(g_state.server, server_hostport); grpc_server_start(g_state.server); gpr_free(server_hostport); gpr_join_host_port(&server_hostport, "localhost", port); g_state.chan = grpc_insecure_channel_create(server_hostport, NULL, NULL); gpr_free(server_hostport); g_state.call = grpc_channel_create_call( g_state.chan, NULL, GRPC_PROPAGATE_DEFAULTS, g_state.cq, "/Foo", "bar", g_state.deadline, NULL); grpc_metadata_array_init(&g_state.server_initial_metadata_recv); grpc_call_details_init(&g_state.call_details); op = g_state.ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = 0; op->reserved = NULL; op++; GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(g_state.call, g_state.ops, (size_t)(op - g_state.ops), tag(1), NULL)); GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(g_state.server, &g_state.server_call, &g_state.call_details, &g_state.server_initial_metadata_recv, g_state.cq, g_state.cq, tag(101))); cq_expect_completion(g_state.cqv, tag(101), 1); cq_expect_completion(g_state.cqv, tag(1), 1); cq_verify(g_state.cqv); } }
int main(int argc, char **argv) { char *me = argv[0]; char *lslash = strrchr(me, '/'); char root[1024]; 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, "."); } /* start the server */ gpr_asprintf(&args[0], "%s/fling_server%s", root, gpr_subprocess_binary_extension()); args[1] = "--bind"; 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]); /* start the client */ 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); 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]); /* 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); gpr_subprocess_interrupt(svr); status = gpr_subprocess_join(svr); gpr_subprocess_destroy(svr); return status; }
int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, int normalize) { const int save_errno = errno; struct sockaddr_in addr_normalized; char ntop_buf[INET6_ADDRSTRLEN]; const void *ip = NULL; int port; int ret; *out = NULL; if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) { addr = (const struct sockaddr *)&addr_normalized; } if (addr->sa_family == AF_INET) { const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; ip = &addr4->sin_addr; port = ntohs(addr4->sin_port); } else if (addr->sa_family == AF_INET6) { const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; ip = &addr6->sin6_addr; port = ntohs(addr6->sin6_port); } /* Windows inet_ntop wants a mutable ip pointer */ if (ip != NULL && inet_ntop(addr->sa_family, (void *)ip, ntop_buf, sizeof(ntop_buf)) != NULL) { ret = gpr_join_host_port(out, ntop_buf, port); } else { ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family); } /* This is probably redundant, but we wouldn't want to log the wrong error. */ errno = save_errno; return ret; }
static void start_rpc(int use_creds, int target_port) { state.cq = grpc_completion_queue_create(NULL); if (use_creds) { state.creds = grpc_fake_transport_security_credentials_create(); } else { state.creds = NULL; } gpr_join_host_port(&state.target, "127.0.0.1", target_port); if (use_creds) { state.channel = grpc_secure_channel_create(state.creds, state.target, NULL, NULL); } else { state.channel = grpc_insecure_channel_create(state.target, NULL, NULL); } state.call = grpc_channel_create_call( state.channel, NULL, GRPC_PROPAGATE_DEFAULTS, state.cq, "/Service/Method", "localhost", gpr_inf_future(GPR_CLOCK_REALTIME), NULL); memset(&state.op, 0, sizeof(state.op)); state.op.op = GRPC_OP_SEND_INITIAL_METADATA; state.op.data.send_initial_metadata.count = 0; state.op.flags = 0; state.op.reserved = NULL; GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(state.call, &state.op, (size_t)(1), NULL, NULL)); grpc_completion_queue_next(state.cq, n_sec_deadline(5), NULL); }
grpc_end2end_http_proxy* grpc_end2end_http_proxy_create(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_end2end_http_proxy* proxy = gpr_malloc(sizeof(*proxy)); memset(proxy, 0, sizeof(*proxy)); // Construct proxy address. const int proxy_port = grpc_pick_unused_port_or_die(); gpr_join_host_port(&proxy->proxy_name, "localhost", proxy_port); gpr_log(GPR_INFO, "Proxy address: %s", proxy->proxy_name); // Create TCP server. proxy->channel_args = grpc_channel_args_copy(NULL); grpc_error* error = grpc_tcp_server_create( &exec_ctx, NULL, proxy->channel_args, &proxy->server); GPR_ASSERT(error == GRPC_ERROR_NONE); // Bind to port. grpc_resolved_address resolved_addr; struct sockaddr_in* addr = (struct sockaddr_in*)resolved_addr.addr; memset(&resolved_addr, 0, sizeof(resolved_addr)); addr->sin_family = AF_INET; grpc_sockaddr_set_port(&resolved_addr, proxy_port); int port; error = grpc_tcp_server_add_port(proxy->server, &resolved_addr, &port); GPR_ASSERT(error == GRPC_ERROR_NONE); GPR_ASSERT(port == proxy_port); // Start server. proxy->pollset = gpr_malloc(grpc_pollset_size()); grpc_pollset_init(proxy->pollset, &proxy->mu); grpc_tcp_server_start(&exec_ctx, proxy->server, &proxy->pollset, 1, on_accept, proxy); grpc_exec_ctx_finish(&exec_ctx); // Start proxy thread. gpr_thd_options opt = gpr_thd_options_default(); gpr_thd_options_set_joinable(&opt); GPR_ASSERT(gpr_thd_new(&proxy->thd, thread_main, proxy, &opt)); return proxy; }
static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( grpc_channel_args *client_args, grpc_channel_args *server_args) { grpc_end2end_test_fixture f; int port = grpc_pick_unused_port_or_die(); fullstack_fixture_data *ffd = gpr_malloc(sizeof(fullstack_fixture_data)); memset(&f, 0, sizeof(f)); gpr_join_host_port(&ffd->localaddr, "localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create(); return f; }
int main(int argc, char **argv) { char *me = argv[0]; char *lslash = strrchr(me, '/'); char *lunder = strrchr(me, '_'); char *tmp; char root[1024]; char test[64]; int port = grpc_pick_unused_port_or_die(); char *args[10]; int status; size_t i; gpr_subprocess *svr; /* figure out where we are */ if (lslash) { memcpy(root, me, (size_t)(lslash - me)); root[lslash - me] = 0; } else { strcpy(root, "."); } if (argc == 2) { gpr_setenv("GRPC_DEFAULT_SSL_ROOTS_FILE_PATH", argv[1]); } /* figure out our test name */ tmp = lunder - 1; while (*tmp != '_') tmp--; tmp++; memcpy(test, tmp, (size_t)(lunder - tmp)); /* start the server */ gpr_asprintf(&args[0], "%s/bad_ssl_%s_server%s", root, test, gpr_subprocess_binary_extension()); args[1] = "--bind"; gpr_join_host_port(&args[2], "::", port); svr = gpr_subprocess_create(4, (const char **)args); gpr_free(args[0]); for (i = 3; i <= 4; i++) { grpc_init(); run_test(args[2], i); grpc_shutdown(); } gpr_free(args[2]); gpr_subprocess_interrupt(svr); status = gpr_subprocess_join(svr); gpr_subprocess_destroy(svr); return status; }
void test_add_same_port_twice() { int port = grpc_pick_unused_port_or_die(); char *addr = NULL; grpc_completion_queue *cq = grpc_completion_queue_create(NULL); grpc_server *server = grpc_server_create(NULL, NULL); grpc_server_credentials *fake_creds = grpc_fake_transport_security_server_credentials_create(); gpr_join_host_port(&addr, "localhost", port); GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds)); GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds) == 0); grpc_server_credentials_release(fake_creds); gpr_free(addr); grpc_server_shutdown_and_notify(server, cq, NULL); grpc_completion_queue_pluck(cq, NULL, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); grpc_server_destroy(server); grpc_completion_queue_destroy(cq); }
static servers_fixture *setup_servers(const char *server_host, request_data *rdata, const size_t num_servers) { servers_fixture *f = gpr_malloc(sizeof(servers_fixture)); 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); /* Create 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++) { grpc_metadata_array_init(&f->request_metadata_recv[i]); gpr_join_host_port(&f->servers_hostports[i], server_host, grpc_pick_unused_port_or_die()); f->servers[i] = 0; revive_server(f, rdata, i); } return f; }
int main(int argc, char **argv) { grpc_event ev; call_state *s; char *addr_buf = NULL; gpr_cmdline *cl; int shutdown_started = 0; int shutdown_finished = 0; int secure = 0; char *addr = NULL; char *fake_argv[1]; GPR_ASSERT(argc >= 1); fake_argv[0] = argv[0]; grpc_test_init(1, fake_argv); grpc_init(); srand(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(); 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); server = grpc_server_create(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); GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr)); } grpc_server_register_completion_queue(server, cq); 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)) .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))); 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_server_destroy(server); grpc_completion_queue_destroy(cq); grpc_shutdown(); return 0; }
static void start_rpc(int target_port, grpc_status_code expected_status, const char *expected_detail) { grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_status_code status; grpc_call_error error; cq_verifier *cqv; char *details = NULL; size_t details_capacity = 0; state.cq = grpc_completion_queue_create(NULL); cqv = cq_verifier_create(state.cq); gpr_join_host_port(&state.target, "127.0.0.1", target_port); state.channel = grpc_insecure_channel_create(state.target, NULL, NULL); state.call = grpc_channel_create_call( state.channel, NULL, GRPC_PROPAGATE_DEFAULTS, state.cq, "/Service/Method", "localhost", gpr_inf_future(GPR_CLOCK_REALTIME), NULL); grpc_metadata_array_init(&initial_metadata_recv); grpc_metadata_array_init(&trailing_metadata_recv); memset(ops, 0, sizeof(ops)); 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++; error = grpc_call_start_batch(state.call, ops, (size_t)(op - ops), tag(1), NULL); GPR_ASSERT(GRPC_CALL_OK == error); cq_expect_completion(cqv, tag(1), 1); cq_verify(cqv); gpr_log(GPR_DEBUG, "Rpc status: %d, details: %s", status, details); GPR_ASSERT(status == expected_status); GPR_ASSERT(NULL != strstr(details, expected_detail)); grpc_metadata_array_destroy(&initial_metadata_recv); grpc_metadata_array_destroy(&trailing_metadata_recv); gpr_free(details); cq_verifier_destroy(cqv); }
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; grpc_server *server; grpc_completion_queue *client_cq; grpc_completion_queue *server_cq; grpc_call *c; grpc_call *s; cq_verifier *v_client; cq_verifier *v_server; gpr_timespec deadline; int got_port; grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; grpc_status_code status; char *details = NULL; size_t details_capacity = 0; int was_cancelled = 2; grpc_call_details call_details; if (port == 0) { port = grpc_pick_unused_port_or_die(); } 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); /* Create server. */ server_cq = grpc_completion_queue_create(); server = grpc_server_create(NULL); grpc_server_register_completion_queue(server, server_cq); GPR_ASSERT((got_port = grpc_server_add_http2_port(server, server_hostport)) > 0); if (port == 0) { port = got_port; } else { GPR_ASSERT(port == got_port); } grpc_server_start(server); v_server = cq_verifier_create(server_cq); /* Create client. */ gpr_join_host_port(&client_hostport, client_host, port); client_cq = grpc_completion_queue_create(); client = grpc_channel_create(client_hostport, NULL); v_client = cq_verifier_create(client_cq); 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, client_cq, "/foo", "foo.test.google.fr", deadline); GPR_ASSERT(c); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op++; op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; op++; op->op = GRPC_OP_RECV_INITIAL_METADATA; op->data.recv_initial_metadata = &initial_metadata_recv; 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++; GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1))); if (expect_ok) { /* Check for a successful request. */ GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(server, &s, &call_details, &request_metadata_recv, server_cq, server_cq, tag(101))); cq_expect_completion(v_server, tag(101), GRPC_OP_OK); cq_verify(v_server); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 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++; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; op->data.recv_close_on_server.cancelled = &was_cancelled; op++; GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102))); cq_expect_completion(v_server, tag(102), GRPC_OP_OK); cq_verify(v_server); cq_expect_completion(v_client, tag(1), GRPC_OP_OK); cq_verify(v_client); 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 == 0); grpc_call_destroy(s); } else { /* Check for a failed connection. */ cq_expect_completion(v_client, tag(1), GRPC_OP_OK); cq_verify(v_client); GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED); } grpc_call_destroy(c); cq_verifier_destroy(v_client); cq_verifier_destroy(v_server); /* Destroy client. */ grpc_channel_destroy(client); grpc_completion_queue_shutdown(client_cq); drain_cq(client_cq); grpc_completion_queue_destroy(client_cq); /* Destroy server. */ grpc_server_shutdown(server); grpc_server_destroy(server); grpc_completion_queue_shutdown(server_cq); drain_cq(server_cq); grpc_completion_queue_destroy(server_cq); }
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; grpc_server *server; grpc_completion_queue *cq; grpc_call *c; grpc_call *s; cq_verifier *cqv; gpr_timespec deadline; int got_port; grpc_op ops[6]; grpc_op *op; grpc_metadata_array initial_metadata_recv; grpc_metadata_array trailing_metadata_recv; grpc_metadata_array request_metadata_recv; grpc_status_code status; grpc_call_error error; char *details = NULL; size_t details_capacity = 0; int was_cancelled = 2; grpc_call_details call_details; char *peer; int picked_port = 0; if (port == 0) { port = grpc_pick_unused_port_or_die(); picked_port = 1; } 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); /* 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); /* 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); } 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); } 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); memset(ops, 0, sizeof(ops)); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = expect_ok ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 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++; 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); memset(ops, 0, sizeof(ops)); 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_UNAVAILABLE); } grpc_call_destroy(c); cq_verifier_destroy(cqv); /* 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); if (picked_port) { grpc_recycle_unused_port(port); } }
static void run_test(bool wait_for_ready, bool use_service_config) { grpc_channel *chan; grpc_call *call; gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2); grpc_completion_queue *cq; cq_verifier *cqv; grpc_op ops[6]; grpc_op *op; grpc_metadata_array trailing_metadata_recv; grpc_status_code status; char *details = NULL; size_t details_capacity = 0; gpr_log(GPR_INFO, "TEST: wait_for_ready=%d use_service_config=%d", wait_for_ready, use_service_config); grpc_init(); grpc_metadata_array_init(&trailing_metadata_recv); cq = grpc_completion_queue_create(NULL); cqv = cq_verifier_create(cq); /* if using service config, create channel args */ grpc_channel_args *args = NULL; if (use_service_config) { GPR_ASSERT(wait_for_ready); grpc_arg arg; arg.type = GRPC_ARG_STRING; arg.key = GRPC_ARG_SERVICE_CONFIG; arg.value.string = "{\n" " \"methodConfig\": [ {\n" " \"name\": [\n" " { \"service\": \"service\", \"method\": \"method\" }\n" " ],\n" " \"waitForReady\": true\n" " } ]\n" "}"; args = grpc_channel_args_copy_and_add(args, &arg, 1); } /* create a call, channel to a port which will refuse connection */ int port = grpc_pick_unused_port_or_die(); char *addr; gpr_join_host_port(&addr, "127.0.0.1", port); gpr_log(GPR_INFO, "server: %s", addr); chan = grpc_insecure_channel_create(addr, args, NULL); call = grpc_channel_create_call(chan, NULL, GRPC_PROPAGATE_DEFAULTS, cq, "/service/method", "nonexistant", deadline, NULL); gpr_free(addr); memset(ops, 0, sizeof(ops)); op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = (wait_for_ready && !use_service_config) ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 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( 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); if (wait_for_ready) { GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED); } else { GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); } 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); gpr_free(details); grpc_metadata_array_destroy(&trailing_metadata_recv); if (args != NULL) grpc_channel_args_destroy(args); grpc_shutdown(); }
int main(int argc, char **argv) { grpc_event *ev; call_state *s; char *addr_buf = NULL; gpr_cmdline *cl; int shutdown_started = 0; int shutdown_finished = 0; int secure = 0; char *addr = NULL; char *fake_argv[1]; #define MAX_ARGS 4 grpc_arg arge[MAX_ARGS]; grpc_arg *e; grpc_channel_args args = {0, NULL}; grpc_http_server_page home_page = {"/", "text/html", "<head>\n" "<title>Echo Server</title>\n" "</head>\n" "<body>\n" "Welcome to the world of the future!\n" "</body>\n"}; GPR_ASSERT(argc >= 1); fake_argv[0] = argv[0]; grpc_test_init(1, fake_argv); grpc_init(); srand(clock()); memset(arge, 0, sizeof(arge)); args.args = arge; cl = gpr_cmdline_create("echo 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); e = &arge[args.num_args++]; e->type = GRPC_ARG_POINTER; e->key = GRPC_ARG_SERVE_OVER_HTTP; e->value.pointer.p = &home_page; 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(); 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); server = grpc_secure_server_create(ssl_creds, cq, &args); GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr)); grpc_server_credentials_release(ssl_creds); } else { server = grpc_server_create(cq, &args); GPR_ASSERT(grpc_server_add_http2_port(server, addr)); } grpc_server_start(server); gpr_free(addr_buf); addr = addr_buf = NULL; request_call(); signal(SIGINT, sigint_handler); while (!shutdown_finished) { if (got_sigint && !shutdown_started) { gpr_log(GPR_INFO, "Shutting down due to SIGINT"); grpc_server_shutdown(server); grpc_completion_queue_shutdown(cq); shutdown_started = 1; } ev = grpc_completion_queue_next( cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(1))); if (!ev) continue; s = ev->tag; switch (ev->type) { case GRPC_SERVER_RPC_NEW: if (ev->call != NULL) { /* initial ops are already started in request_call */ grpc_call_server_accept_old(ev->call, cq, s); grpc_call_server_end_initial_metadata_old(ev->call, GRPC_WRITE_BUFFER_HINT); GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK); request_call(); } else { GPR_ASSERT(shutdown_started); gpr_free(s); } break; case GRPC_WRITE_ACCEPTED: GPR_ASSERT(ev->data.write_accepted == GRPC_OP_OK); GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK); break; case GRPC_READ: if (ev->data.read) { assert_read_ok(ev->tag, ev->data.read); GPR_ASSERT(grpc_call_start_write_old(ev->call, ev->data.read, s, GRPC_WRITE_BUFFER_HINT) == GRPC_CALL_OK); } else { GPR_ASSERT(grpc_call_start_write_status_old(ev->call, GRPC_STATUS_OK, NULL, s) == GRPC_CALL_OK); } break; case GRPC_FINISH_ACCEPTED: case GRPC_FINISHED: if (gpr_unref(&s->pending_ops)) { grpc_call_destroy(ev->call); gpr_free(s); } break; case GRPC_QUEUE_SHUTDOWN: GPR_ASSERT(shutdown_started); shutdown_finished = 1; break; default: GPR_ASSERT(0); } grpc_event_finish(ev); } grpc_server_destroy(server); grpc_completion_queue_destroy(cq); grpc_shutdown(); return 0; }
int main(int argc, char **argv) { char *me = argv[0]; char *lslash = strrchr(me, '/'); char root[1024]; 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(getpid()); /* figure out where we are */ if (lslash) { memcpy(root, me, 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); gpr_free(args[0]); gpr_free(args[2]); return 1; } /* wait a little */ 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); 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); return 0; }
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; grpc_server *server; grpc_completion_queue *client_cq; grpc_completion_queue *server_cq; grpc_call *c; grpc_call *s; cq_verifier *v_client; cq_verifier *v_server; gpr_timespec deadline; int got_port; if (port == 0) { port = grpc_pick_unused_port_or_die(); } gpr_join_host_port(&server_hostport, server_host, port); /* Create server. */ server_cq = grpc_completion_queue_create(); server = grpc_server_create(server_cq, NULL); GPR_ASSERT((got_port = grpc_server_add_http2_port(server, server_hostport)) > 0); if (port == 0) { port = got_port; } else { GPR_ASSERT(port == got_port); } grpc_server_start(server); v_server = cq_verifier_create(server_cq); /* Create client. */ gpr_join_host_port(&client_hostport, client_host, port); client_cq = grpc_completion_queue_create(); client = grpc_channel_create(client_hostport, NULL); v_client = cq_verifier_create(client_cq); 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_old(client, "/foo", "foo.test.google.fr", deadline); GPR_ASSERT(c); GPR_ASSERT(GRPC_CALL_OK == grpc_call_invoke_old(c, client_cq, tag(2), tag(3), 0)); GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done_old(c, tag(4))); if (expect_ok) { /* Check for a successful request. */ cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_OK); cq_verify(v_client); GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call_old(server, tag(100))); cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo", "foo.test.google.fr", deadline, NULL); cq_verify(v_server); GPR_ASSERT(GRPC_CALL_OK == grpc_call_server_accept_old(s, server_cq, tag(102))); GPR_ASSERT(GRPC_CALL_OK == grpc_call_server_end_initial_metadata_old(s, 0)); cq_expect_client_metadata_read(v_client, tag(2), NULL); cq_verify(v_client); GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status_old(s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(5))); cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL); cq_verify(v_client); cq_expect_finish_accepted(v_server, tag(5), GRPC_OP_OK); cq_expect_finished(v_server, tag(102), NULL); cq_verify(v_server); grpc_call_destroy(c); grpc_call_destroy(s); } else { /* Check for a failed connection. */ cq_expect_client_metadata_read(v_client, tag(2), NULL); cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_DEADLINE_EXCEEDED, "Deadline Exceeded", NULL); cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_ERROR); cq_verify(v_client); grpc_call_destroy(c); } cq_verifier_destroy(v_client); cq_verifier_destroy(v_server); /* Destroy client. */ grpc_channel_destroy(client); grpc_completion_queue_shutdown(client_cq); drain_cq(client_cq); grpc_completion_queue_destroy(client_cq); /* Destroy server. */ grpc_server_shutdown(server); grpc_server_destroy(server); grpc_completion_queue_shutdown(server_cq); drain_cq(server_cq); grpc_completion_queue_destroy(server_cq); }