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); parser->on_header = onhdr; parser->on_header_user_data = &chk; 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_slice_unref(slices[i]); } gpr_free(slices); GPR_ASSERT(NULL == va_arg(chk.args, char *)); va_end(chk.args); }
static void test_vector(grpc_chttp2_hpack_parser *parser, grpc_slice_split_mode mode, const char *hexstring, ... /* char *key, char *value */) { grpc_slice input = parse_hexstring(hexstring); grpc_slice *slices; size_t nslices; size_t i; test_checker chk; va_start(chk.args, hexstring); parser->on_header = onhdr; parser->on_header_user_data = &chk; grpc_split_slices(mode, &input, 1, &slices, &nslices); grpc_slice_unref(input); for (i = 0; i < nslices; i++) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; GPR_ASSERT(grpc_chttp2_hpack_parser_parse(&exec_ctx, parser, slices[i]) == GRPC_ERROR_NONE); grpc_exec_ctx_finish(&exec_ctx); } for (i = 0; i < nslices; i++) { grpc_slice_unref(slices[i]); } gpr_free(slices); GPR_ASSERT(NULL == va_arg(chk.args, char *)); va_end(chk.args); }
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); 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_httpcli_parser_init(&parser); 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_ASSERT(done); grpc_httpcli_parser_destroy(&parser); gpr_free(slices); }
void grpc_split_slices_to_buffer(grpc_slice_split_mode mode, grpc_slice *src_slices, size_t src_slice_count, grpc_slice_buffer *dst) { grpc_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 */ grpc_slice_buffer_add_indexed(dst, slices[i]); } gpr_free(slices); }
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); 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_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)); 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); 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); }