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_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"); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_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(&exec_ctx, f.client_ep, &incoming, &done_closure); grpc_exec_ctx_finish(&exec_ctx); GPR_ASSERT(n == 1); GPR_ASSERT(incoming.count == 1); GPR_ASSERT(0 == gpr_slice_cmp(s, incoming.slices[0])); grpc_endpoint_shutdown(&exec_ctx, f.client_ep); grpc_endpoint_shutdown(&exec_ctx, f.server_ep); grpc_endpoint_destroy(&exec_ctx, f.client_ep); grpc_endpoint_destroy(&exec_ctx, f.server_ep); grpc_exec_ctx_finish(&exec_ctx); gpr_slice_unref(s); gpr_slice_buffer_destroy(&incoming); clean_up(); }
static void client_validator(gpr_slice_buffer *incoming) { // Get last frame from incoming slice buffer. gpr_slice_buffer last_frame_buffer; gpr_slice_buffer_init(&last_frame_buffer); gpr_slice_buffer_trim_end(incoming, 13, &last_frame_buffer); GPR_ASSERT(last_frame_buffer.count == 1); gpr_slice last_frame = last_frame_buffer.slices[0]; // Construct expected frame. gpr_slice expected = gpr_slice_malloc(13); uint8_t *p = GPR_SLICE_START_PTR(expected); // Length. *p++ = 0; *p++ = 0; *p++ = 4; // Frame type (RST_STREAM). *p++ = 3; // Flags. *p++ = 0; // Stream ID. *p++ = 0; *p++ = 0; *p++ = 0; *p++ = 1; // Payload (error code). *p++ = 0; *p++ = 0; *p++ = 0; *p++ = 11; // Compare actual and expected. GPR_ASSERT(gpr_slice_cmp(last_frame, expected) == 0); gpr_slice_buffer_destroy(&last_frame_buffer); }
static void test_vector(const char *raw, size_t raw_length, const char *encoded, size_t encoded_length, const uint8_t *dict) { char *raw_msg = gpr_dump(raw, raw_length, GPR_DUMP_HEX | GPR_DUMP_ASCII); char *encoded_msg = gpr_dump(encoded, encoded_length, GPR_DUMP_HEX | GPR_DUMP_ASCII); gpr_log(GPR_DEBUG, "Trial:\nraw = %s\nencoded = %s", raw_msg, encoded_msg); gpr_free(raw_msg); gpr_free(encoded_msg); gpr_slice raw_slice = gpr_slice_from_copied_buffer(raw, raw_length); gpr_slice encoded_slice = gpr_slice_from_copied_buffer(encoded, encoded_length); gpr_slice raw2encoded_slice = gpr_percent_encode_slice(raw_slice, dict); gpr_slice encoded2raw_slice; GPR_ASSERT( gpr_strict_percent_decode_slice(encoded_slice, dict, &encoded2raw_slice)); gpr_slice encoded2raw_permissive_slice = gpr_permissive_percent_decode_slice(encoded_slice); char *raw2encoded_msg = gpr_dump_slice(raw2encoded_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); char *encoded2raw_msg = gpr_dump_slice(encoded2raw_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); char *encoded2raw_permissive_msg = gpr_dump_slice( encoded2raw_permissive_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); gpr_log(GPR_DEBUG, "Result:\nraw2encoded = %s\nencoded2raw = %s\nencoded2raw_permissive " "= %s", raw2encoded_msg, encoded2raw_msg, encoded2raw_permissive_msg); gpr_free(raw2encoded_msg); gpr_free(encoded2raw_msg); gpr_free(encoded2raw_permissive_msg); GPR_ASSERT(0 == gpr_slice_cmp(raw_slice, encoded2raw_slice)); GPR_ASSERT(0 == gpr_slice_cmp(raw_slice, encoded2raw_permissive_slice)); GPR_ASSERT(0 == gpr_slice_cmp(encoded_slice, raw2encoded_slice)); gpr_slice_unref(encoded2raw_slice); gpr_slice_unref(encoded2raw_permissive_slice); gpr_slice_unref(raw2encoded_slice); gpr_slice_unref(raw_slice); gpr_slice_unref(encoded_slice); }
static void test(const uint8_t *data, size_t size, const uint8_t *dict) { struct grpc_memory_counters counters; grpc_memory_counters_init(); gpr_slice input = gpr_slice_from_copied_buffer((const char *)data, size); gpr_slice output = gpr_percent_encode_slice(input, dict); gpr_slice decoded_output; // encoder must always produce decodable output GPR_ASSERT(gpr_strict_percent_decode_slice(output, dict, &decoded_output)); gpr_slice permissive_decoded_output = gpr_permissive_percent_decode_slice(output); // and decoded output must always match the input GPR_ASSERT(gpr_slice_cmp(input, decoded_output) == 0); GPR_ASSERT(gpr_slice_cmp(input, permissive_decoded_output) == 0); gpr_slice_unref(input); gpr_slice_unref(output); gpr_slice_unref(decoded_output); gpr_slice_unref(permissive_decoded_output); counters = grpc_memory_counters_snapshot(); grpc_memory_counters_destroy(); GPR_ASSERT(counters.total_size_relative == 0); }
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 test_varint(uint32_t value, uint32_t prefix_bits, uint8_t prefix_or, const char *expect_bytes, size_t expect_length) { uint32_t nbytes = GRPC_CHTTP2_VARINT_LENGTH(value, prefix_bits); gpr_slice expect = gpr_slice_from_copied_buffer(expect_bytes, expect_length); gpr_slice slice; gpr_log(GPR_DEBUG, "Test: 0x%08x", value); GPR_ASSERT(nbytes == expect_length); slice = gpr_slice_malloc(nbytes); GRPC_CHTTP2_WRITE_VARINT(value, prefix_bits, prefix_or, GPR_SLICE_START_PTR(slice), nbytes); GPR_ASSERT(gpr_slice_cmp(expect, slice) == 0); gpr_slice_unref(expect); 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_hexdump((const char *)GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice), GPR_HEXDUMP_PLAINTEXT); char *he = gpr_hexdump((const char *)GPR_SLICE_START_PTR(expected), GPR_SLICE_LENGTH(expected), GPR_HEXDUMP_PLAINTEXT); 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 test_base64_and_huffman_works(void) { grpc_mdstr *str; gpr_slice slice1; gpr_slice slice2; LOG_TEST("test_base64_and_huffman_works"); grpc_init(); str = grpc_mdstr_from_string("abcdefg"); 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_shutdown(); }
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_impl(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_hexdump((const char *)GPR_SLICE_START_PTR(input), GPR_SLICE_LENGTH(input), GPR_HEXDUMP_PLAINTEXT); char *e = gpr_hexdump((const char *)GPR_SLICE_START_PTR(expect), GPR_SLICE_LENGTH(expect), GPR_HEXDUMP_PLAINTEXT); char *g = gpr_hexdump((const char *)GPR_SLICE_START_PTR(got), GPR_SLICE_LENGTH(got), GPR_HEXDUMP_PLAINTEXT); 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); }
/* 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) { 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, 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_hexdump((char *)GPR_SLICE_START_PTR(expect), GPR_SLICE_LENGTH(expect), GPR_HEXDUMP_PLAINTEXT); char *got_str = gpr_hexdump((char *)GPR_SLICE_START_PTR(merged), GPR_SLICE_LENGTH(merged), GPR_HEXDUMP_PLAINTEXT); 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); }