diff options
Diffstat (limited to 'test/core/security/credentials_test.c')
-rw-r--r-- | test/core/security/credentials_test.c | 83 |
1 files changed, 46 insertions, 37 deletions
diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index 7867293278..e703dbdeb6 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -33,7 +33,7 @@ #include <grpc/support/port_platform.h> -#include "src/core/lib/security/credentials.h" +#include "src/core/lib/security/credentials/credentials.h" #include <openssl/rsa.h> #include <stdlib.h> @@ -45,7 +45,10 @@ #include <grpc/support/time.h> #include "src/core/lib/http/httpcli.h" -#include "src/core/lib/security/json_token.h" +#include "src/core/lib/security/credentials/composite/composite_credentials.h" +#include "src/core/lib/security/credentials/google_default/google_default_credentials.h" +#include "src/core/lib/security/credentials/jwt/jwt_credentials.h" +#include "src/core/lib/security/credentials/oauth2/oauth2_credentials.h" #include "src/core/lib/support/env.h" #include "src/core/lib/support/string.h" #include "src/core/lib/support/tmpfile.h" @@ -154,7 +157,7 @@ static grpc_httpcli_response http_response(int status, const char *body) { grpc_httpcli_response response; memset(&response, 0, sizeof(grpc_httpcli_response)); response.status = status; - response.body = (char *)body; + response.body = gpr_strdup((char *)body); response.body_length = strlen(body); return response; } @@ -244,6 +247,7 @@ static void test_oauth2_token_fetcher_creds_parsing_ok(void) { "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == 0); grpc_credentials_md_store_unref(token_md); + grpc_http_response_destroy(&response); } static void test_oauth2_token_fetcher_creds_parsing_bad_http_status(void) { @@ -254,6 +258,7 @@ static void test_oauth2_token_fetcher_creds_parsing_bad_http_status(void) { GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); + grpc_http_response_destroy(&response); } static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) { @@ -263,6 +268,7 @@ static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) { GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); + grpc_http_response_destroy(&response); } static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) { @@ -276,6 +282,7 @@ static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) { GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); + grpc_http_response_destroy(&response); } static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) { @@ -288,6 +295,7 @@ static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) { GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); + grpc_http_response_destroy(&response); } static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) { @@ -301,6 +309,7 @@ static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) { GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); + grpc_http_response_destroy(&response); } static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime( @@ -314,6 +323,7 @@ static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime( GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_ERROR); + grpc_http_response_destroy(&response); } static void check_metadata(expected_md *expected, grpc_credentials_md *md_elems, @@ -546,37 +556,37 @@ static void validate_compute_engine_http_request( static int compute_engine_httpcli_get_success_override( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data) { - grpc_httpcli_response response = - http_response(200, valid_oauth2_json_response); + gpr_timespec deadline, grpc_closure *on_done, + grpc_httpcli_response *response) { validate_compute_engine_http_request(request); - on_response(exec_ctx, user_data, &response); + *response = http_response(200, valid_oauth2_json_response); + grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_NONE, NULL); return 1; } static int compute_engine_httpcli_get_failure_override( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data) { - grpc_httpcli_response response = http_response(403, "Not Authorized."); + gpr_timespec deadline, grpc_closure *on_done, + grpc_httpcli_response *response) { validate_compute_engine_http_request(request); - on_response(exec_ctx, user_data, &response); + *response = http_response(403, "Not Authorized."); + grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_NONE, NULL); return 1; } static int httpcli_post_should_not_be_called( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, const char *body_bytes, size_t body_size, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data) { + grpc_closure *on_done, grpc_httpcli_response *response) { GPR_ASSERT("HTTP POST should not be called" == NULL); return 1; } -static int httpcli_get_should_not_be_called( - grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data) { +static int httpcli_get_should_not_be_called(grpc_exec_ctx *exec_ctx, + const grpc_httpcli_request *request, + gpr_timespec deadline, + grpc_closure *on_done, + grpc_httpcli_response *response) { GPR_ASSERT("HTTP GET should not be called" == NULL); return 1; } @@ -650,21 +660,20 @@ static void validate_refresh_token_http_request( static int refresh_token_httpcli_post_success( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, const char *body, size_t body_size, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data) { - grpc_httpcli_response response = - http_response(200, valid_oauth2_json_response); + grpc_closure *on_done, grpc_httpcli_response *response) { validate_refresh_token_http_request(request, body, body_size); - on_response(exec_ctx, user_data, &response); + *response = http_response(200, valid_oauth2_json_response); + grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_NONE, NULL); return 1; } static int refresh_token_httpcli_post_failure( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, const char *body, size_t body_size, gpr_timespec deadline, - grpc_httpcli_response_cb on_response, void *user_data) { - grpc_httpcli_response response = http_response(403, "Not Authorized."); + grpc_closure *on_done, grpc_httpcli_response *response) { validate_refresh_token_http_request(request, body, body_size); - on_response(exec_ctx, user_data, &response); + *response = http_response(403, "Not Authorized."); + grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_NONE, NULL); return 1; } @@ -896,17 +905,17 @@ static void test_google_default_creds_refresh_token(void) { static int default_creds_gce_detection_httpcli_get_success_override( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data) { - grpc_httpcli_response response = http_response(200, ""); - grpc_http_header header; - header.key = "Metadata-Flavor"; - header.value = "Google"; - response.hdr_count = 1; - response.hdrs = &header; + gpr_timespec deadline, grpc_closure *on_done, + grpc_httpcli_response *response) { + *response = http_response(200, ""); + grpc_http_header *headers = gpr_malloc(sizeof(*headers) * 1); + headers[0].key = gpr_strdup("Metadata-Flavor"); + headers[0].value = gpr_strdup("Google"); + response->hdr_count = 1; + response->hdrs = headers; GPR_ASSERT(strcmp(request->http.path, "/") == 0); GPR_ASSERT(strcmp(request->host, "metadata.google.internal") == 0); - on_response(exec_ctx, user_data, &response); + grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_NONE, NULL); return 1; } @@ -958,13 +967,13 @@ static void test_google_default_creds_gce(void) { static int default_creds_gce_detection_httpcli_get_failure_override( grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request, - gpr_timespec deadline, grpc_httpcli_response_cb on_response, - void *user_data) { + gpr_timespec deadline, grpc_closure *on_done, + grpc_httpcli_response *response) { /* No magic header. */ - grpc_httpcli_response response = http_response(200, ""); GPR_ASSERT(strcmp(request->http.path, "/") == 0); GPR_ASSERT(strcmp(request->host, "metadata.google.internal") == 0); - on_response(exec_ctx, user_data, &response); + *response = http_response(200, ""); + grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_NONE, NULL); return 1; } |