static void test_int64toa() { char buf[GPR_INT64TOA_MIN_BUFSIZE]; LOG_TEST_NAME("test_int64toa"); /* zero */ GPR_ASSERT(1 == int64_ttoa(0, buf)); GPR_ASSERT(0 == strcmp("0", buf)); /* positive */ GPR_ASSERT(3 == int64_ttoa(123, buf)); GPR_ASSERT(0 == strcmp("123", buf)); /* large positive */ GPR_ASSERT(19 == int64_ttoa(9223372036854775807LL, buf)); GPR_ASSERT(0 == strcmp("9223372036854775807", buf)); /* large negative */ GPR_ASSERT(20 == int64_ttoa(-9223372036854775807LL - 1, buf)); GPR_ASSERT(0 == strcmp("-9223372036854775808", buf)); }
static char *encoded_jwt_claim(const grpc_auth_json_key *json_key, const char *audience, gpr_timespec token_lifetime, const char *scope) { grpc_json *json = grpc_json_create(GRPC_JSON_OBJECT); grpc_json *child = NULL; char *json_str = NULL; char *result = NULL; gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); gpr_timespec expiration = gpr_time_add(now, token_lifetime); char now_str[GPR_LTOA_MIN_BUFSIZE]; char expiration_str[GPR_LTOA_MIN_BUFSIZE]; if (gpr_time_cmp(token_lifetime, grpc_max_auth_token_lifetime()) > 0) { gpr_log(GPR_INFO, "Cropping token lifetime to maximum allowed value."); expiration = gpr_time_add(now, grpc_max_auth_token_lifetime()); } int64_ttoa(now.tv_sec, now_str); int64_ttoa(expiration.tv_sec, expiration_str); child = create_child(NULL, json, "iss", json_key->client_email, GRPC_JSON_STRING); if (scope != NULL) { child = create_child(child, json, "scope", scope, GRPC_JSON_STRING); } else { /* Unscoped JWTs need a sub field. */ child = create_child(child, json, "sub", json_key->client_email, GRPC_JSON_STRING); } child = create_child(child, json, "aud", audience, GRPC_JSON_STRING); child = create_child(child, json, "iat", now_str, GRPC_JSON_NUMBER); create_child(child, json, "exp", expiration_str, GRPC_JSON_NUMBER); json_str = grpc_json_dump_to_string(json, 0); result = grpc_base64_encode(json_str, strlen(json_str), 1, 0); gpr_free(json_str); grpc_json_destroy(json); return result; }
static void test_find(void) { grpc_chttp2_hptbl tbl; uint32_t i; char buffer[32]; grpc_mdelem *elem; grpc_chttp2_hptbl_find_result r; LOG_TEST("test_find"); grpc_chttp2_hptbl_init(&tbl); elem = grpc_mdelem_from_strings("abc", "xyz"); GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); GRPC_MDELEM_UNREF(elem); elem = grpc_mdelem_from_strings("abc", "123"); GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); GRPC_MDELEM_UNREF(elem); elem = grpc_mdelem_from_strings("x", "1"); GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); GRPC_MDELEM_UNREF(elem); 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, "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, "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"); 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", "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, "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++) { int64_ttoa(i, buffer); elem = grpc_mdelem_from_strings("test", buffer); GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); GRPC_MDELEM_UNREF(elem); } 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++) { uint32_t expect = 9999 - i; int64_ttoa(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); }