aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/security
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-05-30 11:11:05 -0700
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-05-30 11:11:05 -0700
commitf29a6c107df0aded7dac1a32bb2cbf10f98b08dd (patch)
treeef86fbb4c08e98e0c7690e01447d6b16a96d5988 /test/core/security
parent1ffb864053d7e124411559295a2567a8aad7411a (diff)
parenta2779c122ec3b2c3b6a475afa2ed18145d1f1b61 (diff)
Merge branch 'but-maybe-i-want-to-poll' into we-dont-need-no-backup
Conflicts: Makefile build.json src/core/security/credentials.c src/core/security/credentials.h src/core/surface/call.c test/core/end2end/tests/request_response_with_payload_and_call_creds.c tools/doxygen/Doxyfile.c++ tools/doxygen/Doxyfile.core
Diffstat (limited to 'test/core/security')
-rw-r--r--test/core/security/auth_context_test.c143
-rw-r--r--test/core/security/credentials_test.c163
2 files changed, 254 insertions, 52 deletions
diff --git a/test/core/security/auth_context_test.c b/test/core/security/auth_context_test.c
new file mode 100644
index 0000000000..54548bf1fc
--- /dev/null
+++ b/test/core/security/auth_context_test.c
@@ -0,0 +1,143 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include<string.h>
+
+#include "src/core/security/security_context.h"
+#include "src/core/support/string.h"
+#include "test/core/util/test_config.h"
+
+#include <grpc/support/log.h>
+
+static void test_empty_context(void) {
+ grpc_auth_context *ctx = grpc_auth_context_create(NULL, 0);
+ grpc_auth_property_iterator it;
+
+ gpr_log(GPR_INFO, __FUNCTION__);
+ GPR_ASSERT(ctx != NULL);
+ GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL);
+ it = grpc_auth_context_peer_identity(ctx);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+ it = grpc_auth_context_property_iterator(ctx);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+ it = grpc_auth_context_find_properties_by_name(ctx, "foo");
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+ grpc_auth_context_unref(ctx);
+}
+
+static void test_simple_context(void) {
+ grpc_auth_context *ctx = grpc_auth_context_create(NULL, 3);
+ grpc_auth_property_iterator it;
+ size_t i;
+
+ gpr_log(GPR_INFO, __FUNCTION__);
+ GPR_ASSERT(ctx != NULL);
+ GPR_ASSERT(ctx->property_count == 3);
+ ctx->properties[0] = grpc_auth_property_init_from_cstring("name", "chapi");
+ ctx->properties[1] = grpc_auth_property_init_from_cstring("name", "chapo");
+ ctx->properties[2] = grpc_auth_property_init_from_cstring("foo", "bar");
+ ctx->peer_identity_property_name = ctx->properties[0].name;
+
+ GPR_ASSERT(
+ strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0);
+ it = grpc_auth_context_property_iterator(ctx);
+ for (i = 0; i < ctx->property_count; i++) {
+ const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
+ GPR_ASSERT(p == &ctx->properties[i]);
+ }
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+
+ it = grpc_auth_context_find_properties_by_name(ctx, "foo");
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[2]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+
+ it = grpc_auth_context_peer_identity(ctx);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[0]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[1]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+
+ grpc_auth_context_unref(ctx);
+}
+
+static void test_chained_context(void) {
+ grpc_auth_context *chained = grpc_auth_context_create(NULL, 2);
+ grpc_auth_context *ctx = grpc_auth_context_create(chained, 3);
+ grpc_auth_property_iterator it;
+ size_t i;
+
+ gpr_log(GPR_INFO, __FUNCTION__);
+ grpc_auth_context_unref(chained);
+ chained->properties[0] =
+ grpc_auth_property_init_from_cstring("name", "padapo");
+ chained->properties[1] = grpc_auth_property_init_from_cstring("foo", "baz");
+ ctx->properties[0] = grpc_auth_property_init_from_cstring("name", "chapi");
+ ctx->properties[1] = grpc_auth_property_init_from_cstring("name", "chapo");
+ ctx->properties[2] = grpc_auth_property_init_from_cstring("foo", "bar");
+ ctx->peer_identity_property_name = ctx->properties[0].name;
+
+ GPR_ASSERT(
+ strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0);
+ it = grpc_auth_context_property_iterator(ctx);
+ for (i = 0; i < ctx->property_count; i++) {
+ const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
+ GPR_ASSERT(p == &ctx->properties[i]);
+ }
+ for (i = 0; i < chained->property_count; i++) {
+ const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
+ GPR_ASSERT(p == &chained->properties[i]);
+ }
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+
+ it = grpc_auth_context_find_properties_by_name(ctx, "foo");
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[2]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &chained->properties[1]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+
+ it = grpc_auth_context_peer_identity(ctx);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[0]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &ctx->properties[1]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == &chained->properties[0]);
+ GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
+
+ grpc_auth_context_unref(ctx);
+}
+
+
+int main(int argc, char **argv) {
+ grpc_test_init(argc, argv);
+ test_empty_context();
+ test_simple_context();
+ test_chained_context();
+ return 0;
+}
+
diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c
index 1b657e3d89..9a77f88e73 100644
--- a/test/core/security/credentials_test.c
+++ b/test/core/security/credentials_test.c
@@ -135,51 +135,113 @@ static grpc_httpcli_response http_response(int status, const char *body) {
return response;
}
+static void test_empty_md_store(void) {
+ grpc_credentials_md_store *store = grpc_credentials_md_store_create(0);
+ GPR_ASSERT(store->num_entries == 0);
+ GPR_ASSERT(store->allocated == 0);
+ grpc_credentials_md_store_unref(store);
+}
+
+static void test_ref_unref_empty_md_store(void) {
+ grpc_credentials_md_store *store = grpc_credentials_md_store_create(0);
+ grpc_credentials_md_store_ref(store);
+ grpc_credentials_md_store_ref(store);
+ GPR_ASSERT(store->num_entries == 0);
+ GPR_ASSERT(store->allocated == 0);
+ grpc_credentials_md_store_unref(store);
+ grpc_credentials_md_store_unref(store);
+ grpc_credentials_md_store_unref(store);
+}
+
+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_add_cstrings_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";
+ grpc_credentials_md_store_add_cstrings(store, key_str, value_str);
+ GPR_ASSERT(store->num_entries == 1);
+ GPR_ASSERT(gpr_slice_str_cmp(store->entries[0].key, key_str) == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(store->entries[0].value, value_str) == 0);
+ grpc_credentials_md_store_unref(store);
+}
+
+static void test_empty_preallocated_md_store(void) {
+ grpc_credentials_md_store *store = grpc_credentials_md_store_create(4);
+ GPR_ASSERT(store->num_entries == 0);
+ GPR_ASSERT(store->allocated == 4);
+ GPR_ASSERT(store->entries != NULL);
+ grpc_credentials_md_store_unref(store);
+}
+
+static void test_add_abunch_to_md_store(void) {
+ grpc_credentials_md_store *store = grpc_credentials_md_store_create(4);
+ size_t num_entries = 1000;
+ const char *key_str = "hello";
+ const char *value_str = "there blah blah blah blah blah blah blah";
+ size_t i;
+ for (i = 0; i < num_entries; i++) {
+ grpc_credentials_md_store_add_cstrings(store, key_str, value_str);
+ }
+ for (i = 0; i < num_entries; i++) {
+ GPR_ASSERT(gpr_slice_str_cmp(store->entries[i].key, key_str) == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(store->entries[i].value, value_str) == 0);
+ }
+ grpc_credentials_md_store_unref(store);
+}
+
static void test_oauth2_token_fetcher_creds_parsing_ok(void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response =
http_response(200, valid_oauth2_json_response);
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_OK);
GPR_ASSERT(token_lifetime.tv_sec == 3599);
GPR_ASSERT(token_lifetime.tv_nsec == 0);
- GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(token_elem->key),
- "Authorization") == 0);
- GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(token_elem->value),
- "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == 0);
- grpc_mdelem_unref(token_elem);
- grpc_mdctx_unref(ctx);
+ GPR_ASSERT(token_md->num_entries == 1);
+ GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].key, "Authorization") == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].value,
+ "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") ==
+ 0);
+ grpc_credentials_md_store_unref(token_md);
}
static void test_oauth2_token_fetcher_creds_parsing_bad_http_status(void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response =
http_response(401, valid_oauth2_json_response);
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_ERROR);
- grpc_mdctx_unref(ctx);
}
static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response = http_response(200, "");
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_ERROR);
- grpc_mdctx_unref(ctx);
}
static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response =
http_response(200,
@@ -187,28 +249,24 @@ static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) {
" \"expires_in\":3599, "
" \"token_type\":\"Bearer\"");
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_ERROR);
- grpc_mdctx_unref(ctx);
}
static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response = http_response(200,
"{"
" \"expires_in\":3599, "
" \"token_type\":\"Bearer\"}");
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_ERROR);
- grpc_mdctx_unref(ctx);
}
static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response =
http_response(200,
@@ -216,35 +274,32 @@ static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) {
" \"expires_in\":3599, "
"}");
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_ERROR);
- grpc_mdctx_unref(ctx);
}
static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime(
void) {
- grpc_mdctx *ctx = grpc_mdctx_create();
- grpc_mdelem *token_elem = NULL;
+ grpc_credentials_md_store *token_md = NULL;
gpr_timespec token_lifetime;
grpc_httpcli_response response =
http_response(200,
"{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
" \"token_type\":\"Bearer\"}");
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
- &response, ctx, &token_elem, &token_lifetime) ==
+ &response, &token_md, &token_lifetime) ==
GRPC_CREDENTIALS_ERROR);
- grpc_mdctx_unref(ctx);
}
-static void check_metadata(expected_md *expected, grpc_mdelem **md_elems,
+static void check_metadata(expected_md *expected, grpc_credentials_md *md_elems,
size_t num_md) {
size_t i;
for (i = 0; i < num_md; i++) {
size_t j;
for (j = 0; j < num_md; j++) {
- if (0 == gpr_slice_str_cmp(md_elems[j]->key->slice, expected[i].key)) {
- GPR_ASSERT(0 == gpr_slice_str_cmp(md_elems[j]->value->slice,
- expected[i].value));
+ if (0 == gpr_slice_str_cmp(md_elems[j].key, expected[i].key)) {
+ GPR_ASSERT(gpr_slice_str_cmp(md_elems[j].value, expected[i].value) ==
+ 0);
break;
}
}
@@ -255,7 +310,7 @@ static void check_metadata(expected_md *expected, grpc_mdelem **md_elems,
}
}
-static void check_iam_metadata(void *user_data, grpc_mdelem **md_elems,
+static void check_iam_metadata(void *user_data, grpc_credentials_md *md_elems,
size_t num_md, grpc_credentials_status status) {
grpc_credentials *c = (grpc_credentials *)user_data;
expected_md emd[] = {
@@ -277,7 +332,7 @@ static void test_iam_creds(void) {
}
static void check_ssl_oauth2_composite_metadata(
- void *user_data, grpc_mdelem **md_elems, size_t num_md,
+ void *user_data, grpc_credentials_md *md_elems, size_t num_md,
grpc_credentials_status status) {
grpc_credentials *c = (grpc_credentials *)user_data;
expected_md emd[] = {
@@ -327,7 +382,7 @@ void test_ssl_fake_transport_security_composite_creds_failure(void) {
}
static void check_ssl_oauth2_iam_composite_metadata(
- void *user_data, grpc_mdelem **md_elems, size_t num_md,
+ void *user_data, grpc_credentials_md *md_elems, size_t num_md,
grpc_credentials_status status) {
grpc_credentials *c = (grpc_credentials *)user_data;
expected_md emd[] = {
@@ -374,20 +429,20 @@ static void test_ssl_oauth2_iam_composite_creds(void) {
}
static void on_oauth2_creds_get_metadata_success(
- void *user_data, grpc_mdelem **md_elems, size_t num_md,
+ void *user_data, grpc_credentials_md *md_elems, size_t num_md,
grpc_credentials_status status) {
GPR_ASSERT(status == GRPC_CREDENTIALS_OK);
GPR_ASSERT(num_md == 1);
- GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->key),
- "Authorization") == 0);
- GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->value),
- "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].key, "Authorization") == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].value,
+ "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") ==
+ 0);
GPR_ASSERT(user_data != NULL);
GPR_ASSERT(strcmp((const char *)user_data, test_user_data) == 0);
}
static void on_oauth2_creds_get_metadata_failure(
- void *user_data, grpc_mdelem **md_elems, size_t num_md,
+ void *user_data, grpc_credentials_md *md_elems, size_t num_md,
grpc_credentials_status status) {
GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR);
GPR_ASSERT(num_md == 0);
@@ -719,24 +774,22 @@ static void test_service_account_creds_signing_failure(void) {
}
static void on_jwt_creds_get_metadata_success(void *user_data,
- grpc_mdelem **md_elems,
+ grpc_credentials_md *md_elems,
size_t num_md,
grpc_credentials_status status) {
char *expected_md_value;
gpr_asprintf(&expected_md_value, "Bearer %s", test_signed_jwt);
GPR_ASSERT(status == GRPC_CREDENTIALS_OK);
GPR_ASSERT(num_md == 1);
- GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->key),
- "Authorization") == 0);
- GPR_ASSERT(strcmp(grpc_mdstr_as_c_string(md_elems[0]->value),
- expected_md_value) == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].key, "Authorization") == 0);
+ GPR_ASSERT(gpr_slice_str_cmp(md_elems[0].value, expected_md_value) == 0);
GPR_ASSERT(user_data != NULL);
GPR_ASSERT(strcmp((const char *)user_data, test_user_data) == 0);
gpr_free(expected_md_value);
}
static void on_jwt_creds_get_metadata_failure(void *user_data,
- grpc_mdelem **md_elems,
+ grpc_credentials_md *md_elems,
size_t num_md,
grpc_credentials_status status) {
GPR_ASSERT(status == GRPC_CREDENTIALS_ERROR);
@@ -796,6 +849,12 @@ static void test_jwt_creds_signing_failure(void) {
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
+ test_empty_md_store();
+ test_ref_unref_empty_md_store();
+ test_add_to_empty_md_store();
+ test_add_cstrings_to_empty_md_store();
+ test_empty_preallocated_md_store();
+ test_add_abunch_to_md_store();
test_oauth2_token_fetcher_creds_parsing_ok();
test_oauth2_token_fetcher_creds_parsing_bad_http_status();
test_oauth2_token_fetcher_creds_parsing_empty_http_body();