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(); }
char *grpc_sopb_string(grpc_stream_op_buffer *sopb) { char *out; char *tmp; size_t i; gpr_strvec b; gpr_strvec_init(&b); for (i = 0; i < sopb->nops; i++) { grpc_stream_op *op = &sopb->ops[i]; if (i > 0) gpr_strvec_add(&b, gpr_strdup(", ")); switch (op->type) { case GRPC_NO_OP: gpr_strvec_add(&b, gpr_strdup("NO_OP")); break; case GRPC_OP_BEGIN_MESSAGE: gpr_asprintf(&tmp, "BEGIN_MESSAGE:%d", op->data.begin_message.length); gpr_strvec_add(&b, tmp); break; case GRPC_OP_SLICE: gpr_asprintf(&tmp, "SLICE:%d", GPR_SLICE_LENGTH(op->data.slice)); gpr_strvec_add(&b, tmp); break; case GRPC_OP_METADATA: gpr_strvec_add(&b, gpr_strdup("METADATA{")); put_metadata_list(&b, op->data.metadata); gpr_strvec_add(&b, gpr_strdup("}")); break; } } out = gpr_strvec_flatten(&b, NULL); gpr_strvec_destroy(&b); return out; }
gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request, const char *body_bytes, size_t body_size) { gpr_strvec out; char *tmp; size_t out_len; size_t i; gpr_strvec_init(&out); gpr_strvec_add(&out, gpr_strdup("POST ")); fill_common_header(request, &out); if (body_bytes) { uint8_t has_content_type = 0; for (i = 0; i < request->http.hdr_count; i++) { if (strcmp(request->http.hdrs[i].key, "Content-Type") == 0) { has_content_type = 1; break; } } if (!has_content_type) { gpr_strvec_add(&out, gpr_strdup("Content-Type: text/plain\r\n")); } gpr_asprintf(&tmp, "Content-Length: %lu\r\n", (unsigned long)body_size); gpr_strvec_add(&out, tmp); } gpr_strvec_add(&out, gpr_strdup("\r\n")); tmp = gpr_strvec_flatten(&out, &out_len); gpr_strvec_destroy(&out); if (body_bytes) { tmp = gpr_realloc(tmp, out_len + body_size); memcpy(tmp + out_len, body_bytes, body_size); out_len += body_size; } return gpr_slice_new(tmp, out_len, gpr_free); }
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; } 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); }
// Parses the method config from \a json. Adds an entry to \a entries for // each name found, incrementing \a idx for each entry added. // Returns false on error. static bool parse_json_method_config( grpc_exec_ctx* exec_ctx, grpc_json* json, void* (*create_value)(const grpc_json* method_config_json), const grpc_slice_hash_table_vtable* vtable, grpc_slice_hash_table_entry* entries, size_t* idx) { // Construct value. void* method_config = create_value(json); if (method_config == NULL) return false; // Construct list of paths. bool success = false; gpr_strvec paths; gpr_strvec_init(&paths); for (grpc_json* child = json->child; child != NULL; child = child->next) { if (child->key == NULL) continue; if (strcmp(child->key, "name") == 0) { if (child->type != GRPC_JSON_ARRAY) goto done; for (grpc_json* name = child->child; name != NULL; name = name->next) { char* path = parse_json_method_name(name); gpr_strvec_add(&paths, path); } } } if (paths.count == 0) goto done; // No names specified. // Add entry for each path. for (size_t i = 0; i < paths.count; ++i) { entries[*idx].key = grpc_slice_from_copied_string(paths.strs[i]); entries[*idx].value = vtable->copy_value(method_config); entries[*idx].vtable = vtable; ++*idx; } success = true; done: vtable->destroy_value(exec_ctx, method_config); gpr_strvec_destroy(&paths); return success; }
char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) { char *tmp; char *out; gpr_strvec b; gpr_strvec_init(&b); gpr_strvec_add( &b, gpr_strdup(op->covered_by_poller ? "[COVERED]" : "[UNCOVERED]")); if (op->send_initial_metadata != NULL) { gpr_strvec_add(&b, gpr_strdup(" ")); gpr_strvec_add(&b, gpr_strdup("SEND_INITIAL_METADATA{")); put_metadata_list(&b, *op->send_initial_metadata); gpr_strvec_add(&b, gpr_strdup("}")); } if (op->send_message != NULL) { gpr_strvec_add(&b, gpr_strdup(" ")); gpr_asprintf(&tmp, "SEND_MESSAGE:flags=0x%08x:len=%d", op->send_message->flags, op->send_message->length); gpr_strvec_add(&b, tmp); } if (op->send_trailing_metadata != NULL) { gpr_strvec_add(&b, gpr_strdup(" ")); gpr_strvec_add(&b, gpr_strdup("SEND_TRAILING_METADATA{")); put_metadata_list(&b, *op->send_trailing_metadata); gpr_strvec_add(&b, gpr_strdup("}")); } if (op->recv_initial_metadata != NULL) { gpr_strvec_add(&b, gpr_strdup(" ")); gpr_strvec_add(&b, gpr_strdup("RECV_INITIAL_METADATA")); } if (op->recv_message != NULL) { gpr_strvec_add(&b, gpr_strdup(" ")); gpr_strvec_add(&b, gpr_strdup("RECV_MESSAGE")); } if (op->recv_trailing_metadata != NULL) { gpr_strvec_add(&b, gpr_strdup(" ")); gpr_strvec_add(&b, gpr_strdup("RECV_TRAILING_METADATA")); } if (op->cancel_error != GRPC_ERROR_NONE) { gpr_strvec_add(&b, gpr_strdup(" ")); const char *msg = grpc_error_string(op->cancel_error); gpr_asprintf(&tmp, "CANCEL:%s", msg); grpc_error_free_string(msg); gpr_strvec_add(&b, tmp); } if (op->close_error != GRPC_ERROR_NONE) { gpr_strvec_add(&b, gpr_strdup(" ")); const char *msg = grpc_error_string(op->close_error); gpr_asprintf(&tmp, "CLOSE:%s", msg); grpc_error_free_string(msg); gpr_strvec_add(&b, tmp); } out = gpr_strvec_flatten(&b, NULL); gpr_strvec_destroy(&b); return out; }
char *grpc_transport_op_string(grpc_transport_op *op) { char *tmp; char *out; bool first = true; gpr_strvec b; gpr_strvec_init(&b); if (op->on_connectivity_state_change != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; if (op->connectivity_state != NULL) { gpr_asprintf(&tmp, "ON_CONNECTIVITY_STATE_CHANGE:p=%p:from=%s", op->on_connectivity_state_change, grpc_connectivity_state_name(*op->connectivity_state)); gpr_strvec_add(&b, tmp); } else { gpr_asprintf(&tmp, "ON_CONNECTIVITY_STATE_CHANGE:p=%p:unsubscribe", op->on_connectivity_state_change); gpr_strvec_add(&b, tmp); } } if (op->disconnect_with_error != GRPC_ERROR_NONE) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; const char *err = grpc_error_string(op->disconnect_with_error); gpr_asprintf(&tmp, "DISCONNECT:%s", err); gpr_strvec_add(&b, tmp); grpc_error_free_string(err); } if (op->send_goaway) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; char *msg = op->goaway_message == NULL ? "null" : grpc_dump_slice(*op->goaway_message, GPR_DUMP_ASCII | GPR_DUMP_HEX); gpr_asprintf(&tmp, "SEND_GOAWAY:status=%d:msg=%s", op->goaway_status, msg); if (op->goaway_message != NULL) gpr_free(msg); gpr_strvec_add(&b, tmp); } if (op->set_accept_stream) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; gpr_asprintf(&tmp, "SET_ACCEPT_STREAM:%p(%p,...)", op->set_accept_stream_fn, op->set_accept_stream_user_data); gpr_strvec_add(&b, tmp); } if (op->bind_pollset != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; gpr_strvec_add(&b, gpr_strdup("BIND_POLLSET")); } if (op->bind_pollset_set != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; gpr_strvec_add(&b, gpr_strdup("BIND_POLLSET_SET")); } if (op->send_ping != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = false; gpr_strvec_add(&b, gpr_strdup("SEND_PING")); } out = gpr_strvec_flatten(&b, NULL); gpr_strvec_destroy(&b); return out; }
static void fill_common_header(const grpc_httpcli_request *request, gpr_strvec *buf) { size_t i; gpr_strvec_add(buf, gpr_strdup(request->http.path)); gpr_strvec_add(buf, gpr_strdup(" HTTP/1.0\r\n")); /* just in case some crazy server really expects HTTP/1.1 */ gpr_strvec_add(buf, gpr_strdup("Host: ")); gpr_strvec_add(buf, gpr_strdup(request->host)); gpr_strvec_add(buf, gpr_strdup("\r\n")); gpr_strvec_add(buf, gpr_strdup("Connection: close\r\n")); gpr_strvec_add(buf, gpr_strdup("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n")); /* user supplied headers */ for (i = 0; i < request->http.hdr_count; i++) { gpr_strvec_add(buf, gpr_strdup(request->http.hdrs[i].key)); gpr_strvec_add(buf, gpr_strdup(": ")); gpr_strvec_add(buf, gpr_strdup(request->http.hdrs[i].value)); gpr_strvec_add(buf, gpr_strdup("\r\n")); } }
int main(int argc, char **argv) { grpc_test_init(argc, argv); grpc_init(); int errors = 0; // tests with a minimal stack grpc_arg minimal_stack_arg = {.type = GRPC_ARG_INTEGER, .key = GRPC_ARG_MINIMAL_STACK, .value.integer = 1}; grpc_channel_args minimal_stack_args = {.num_args = 1, .args = &minimal_stack_arg}; errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL, "connected", NULL); errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL, "connected", NULL); errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_SERVER_CHANNEL, "server", "connected", NULL); errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL, "http-client", "connected", NULL); errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL, "http-client", "connected", NULL); errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_SERVER_CHANNEL, "server", "http-server", "connected", NULL); errors += CHECK_STACK(NULL, &minimal_stack_args, GRPC_CLIENT_CHANNEL, "client-channel", NULL); // tests with a default stack errors += CHECK_STACK("unknown", NULL, GRPC_CLIENT_DIRECT_CHANNEL, "message_size", "deadline", "connected", NULL); errors += CHECK_STACK("unknown", NULL, GRPC_CLIENT_SUBCHANNEL, "message_size", "connected", NULL); errors += CHECK_STACK("unknown", NULL, GRPC_SERVER_CHANNEL, "server", "message_size", "deadline", "connected", NULL); errors += CHECK_STACK("chttp2", NULL, GRPC_CLIENT_DIRECT_CHANNEL, "message_size", "deadline", "http-client", "compress", "connected", NULL); errors += CHECK_STACK("chttp2", NULL, GRPC_CLIENT_SUBCHANNEL, "message_size", "http-client", "compress", "connected", NULL); errors += CHECK_STACK("chttp2", NULL, GRPC_SERVER_CHANNEL, "server", "message_size", "deadline", "http-server", "compress", "connected", NULL); errors += CHECK_STACK(NULL, NULL, GRPC_CLIENT_CHANNEL, "client-channel", NULL); GPR_ASSERT(errors == 0); grpc_shutdown(); return 0; } /******************************************************************************* * End of tests definitions, start of test infrastructure */ static int check_stack(const char *file, int line, const char *transport_name, grpc_channel_args *init_args, grpc_channel_stack_type channel_stack_type, ...) { // create dummy channel stack grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create(); grpc_transport_vtable fake_transport_vtable = {.name = transport_name}; grpc_transport fake_transport = {.vtable = &fake_transport_vtable}; grpc_channel_stack_builder_set_target(builder, "foo.test.google.fr"); grpc_channel_args *channel_args = grpc_channel_args_copy(init_args); if (transport_name != NULL) { grpc_channel_stack_builder_set_transport(builder, &fake_transport); } { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_channel_stack_builder_set_channel_arguments(&exec_ctx, builder, channel_args); GPR_ASSERT( grpc_channel_init_create_stack(&exec_ctx, builder, channel_stack_type)); grpc_exec_ctx_finish(&exec_ctx); } // build up our expectation list gpr_strvec v; gpr_strvec_init(&v); va_list args; va_start(args, channel_stack_type); for (;;) { char *a = va_arg(args, char *); if (a == NULL) break; if (v.count != 0) gpr_strvec_add(&v, gpr_strdup(", ")); gpr_strvec_add(&v, gpr_strdup(a)); } va_end(args); char *expect = gpr_strvec_flatten(&v, NULL); gpr_strvec_destroy(&v); // build up our "got" list gpr_strvec_init(&v); grpc_channel_stack_builder_iterator *it = grpc_channel_stack_builder_create_iterator_at_first(builder); while (grpc_channel_stack_builder_move_next(it)) { const char *name = grpc_channel_stack_builder_iterator_filter_name(it); if (name == NULL) continue; if (v.count != 0) gpr_strvec_add(&v, gpr_strdup(", ")); gpr_strvec_add(&v, gpr_strdup(name)); } char *got = gpr_strvec_flatten(&v, NULL); gpr_strvec_destroy(&v); grpc_channel_stack_builder_iterator_destroy(it); // figure out result, log if there's an error int result = 0; if (0 != strcmp(got, expect)) { gpr_strvec_init(&v); gpr_strvec_add(&v, gpr_strdup("{")); for (size_t i = 0; i < channel_args->num_args; i++) { if (i > 0) gpr_strvec_add(&v, gpr_strdup(", ")); gpr_strvec_add(&v, gpr_strdup(channel_args->args[i].key)); gpr_strvec_add(&v, gpr_strdup("=")); switch (channel_args->args[i].type) { case GRPC_ARG_INTEGER: { char *tmp; gpr_asprintf(&tmp, "%d", channel_args->args[i].value.integer); gpr_strvec_add(&v, tmp); break; } case GRPC_ARG_STRING: gpr_strvec_add(&v, gpr_strdup(channel_args->args[i].value.string)); break; case GRPC_ARG_POINTER: { char *tmp; gpr_asprintf(&tmp, "%p", channel_args->args[i].value.pointer.p); gpr_strvec_add(&v, tmp); break; } } } gpr_strvec_add(&v, gpr_strdup("}")); char *args_str = gpr_strvec_flatten(&v, NULL); gpr_strvec_destroy(&v); gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "**************************************************"); gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "FAILED transport=%s; stack_type=%s; channel_args=%s:", transport_name, grpc_channel_stack_type_string(channel_stack_type), args_str); gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "EXPECTED: %s", expect); gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "GOT: %s", got); result = 1; gpr_free(args_str); } gpr_free(got); gpr_free(expect); { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_channel_stack_builder_destroy(&exec_ctx, builder); grpc_channel_args_destroy(&exec_ctx, channel_args); grpc_exec_ctx_finish(&exec_ctx); } return result; }
char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) { char *tmp; char *out; int first = 1; gpr_strvec b; gpr_strvec_init(&b); if (op->send_ops) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("SEND")); if (op->is_last_send) { gpr_strvec_add(&b, gpr_strdup("_LAST")); } gpr_strvec_add(&b, gpr_strdup("[")); gpr_strvec_add(&b, grpc_sopb_string(op->send_ops)); gpr_strvec_add(&b, gpr_strdup("]")); } if (op->recv_ops) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_asprintf(&tmp, "RECV:max_recv_bytes=%d", op->max_recv_bytes); gpr_strvec_add(&b, tmp); } if (op->bind_pollset) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("BIND")); } if (op->cancel_with_status != GRPC_STATUS_OK) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status); gpr_strvec_add(&b, tmp); } if (op->on_consumed != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_asprintf(&tmp, "ON_CONSUMED:%p", op->on_consumed); gpr_strvec_add(&b, tmp); } out = gpr_strvec_flatten(&b, NULL); gpr_strvec_destroy(&b); return out; }
char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) { char *tmp; char *out; int first = 1; gpr_strvec b; gpr_strvec_init(&b); if (op->send_initial_metadata != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("SEND_INITIAL_METADATA{")); put_metadata_list(&b, *op->send_initial_metadata); gpr_strvec_add(&b, gpr_strdup("}")); } if (op->send_message != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_asprintf(&tmp, "SEND_MESSAGE:flags=0x%08x:len=%d", op->send_message->flags, op->send_message->length); gpr_strvec_add(&b, tmp); } if (op->send_trailing_metadata != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("SEND_TRAILING_METADATA{")); put_metadata_list(&b, *op->send_trailing_metadata); gpr_strvec_add(&b, gpr_strdup("}")); } if (op->recv_initial_metadata != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("RECV_INITIAL_METADATA")); } if (op->recv_message != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("RECV_MESSAGE")); } if (op->recv_trailing_metadata != NULL) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("RECV_TRAILING_METADATA")); } if (op->cancel_with_status != GRPC_STATUS_OK) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status); gpr_strvec_add(&b, tmp); } out = gpr_strvec_flatten(&b, NULL); gpr_strvec_destroy(&b); return out; }
char *grpc_transport_op_string(grpc_transport_op *op) { char *tmp; char *out; int first = 1; gpr_strvec b; gpr_strvec_init(&b); if (op->send_ops) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("SEND")); if (op->is_last_send) { gpr_strvec_add(&b, gpr_strdup("_LAST")); } gpr_strvec_add(&b, gpr_strdup("[")); gpr_strvec_add(&b, grpc_sopb_string(op->send_ops)); gpr_strvec_add(&b, gpr_strdup("]")); } if (op->recv_ops) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("RECV")); } if (op->bind_pollset) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_strvec_add(&b, gpr_strdup("BIND")); } if (op->cancel_with_status != GRPC_STATUS_OK) { if (!first) gpr_strvec_add(&b, gpr_strdup(" ")); first = 0; gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status); gpr_strvec_add(&b, tmp); if (op->cancel_message) { gpr_asprintf(&tmp, ";msg='%s'", grpc_mdstr_as_c_string(op->cancel_message)); gpr_strvec_add(&b, tmp); } } out = gpr_strvec_flatten(&b, NULL); gpr_strvec_destroy(&b); return out; }
static void adderr(gpr_strvec *buf, int success) { char *tmp; gpr_asprintf(&tmp, " %s", errstr(success)); gpr_strvec_add(buf, tmp); }
static void addhdr(gpr_strvec *buf, grpc_event *ev) { char *tmp; gpr_asprintf(&tmp, "tag:%p", ev->tag); gpr_strvec_add(buf, tmp); }