aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core
diff options
context:
space:
mode:
Diffstat (limited to 'test/core')
-rw-r--r--test/core/security/create_jwt.c2
-rw-r--r--test/core/security/jwt_verifier_test.c54
-rw-r--r--test/core/security/verify_jwt.c2
-rw-r--r--test/core/support/string_test.c16
4 files changed, 74 insertions, 0 deletions
diff --git a/test/core/security/create_jwt.c b/test/core/security/create_jwt.c
index 741ace9bdd..ac795f29d2 100644
--- a/test/core/security/create_jwt.c
+++ b/test/core/security/create_jwt.c
@@ -72,6 +72,7 @@ int main(int argc, char **argv) {
char *scope = NULL;
char *json_key_file_path = NULL;
char *service_url = NULL;
+ grpc_init();
gpr_cmdline *cl = gpr_cmdline_create("create_jwt");
gpr_cmdline_add_string(cl, "json_key", "File path of the json key.",
&json_key_file_path);
@@ -102,5 +103,6 @@ int main(int argc, char **argv) {
create_jwt(json_key_file_path, service_url, scope);
gpr_cmdline_destroy(cl);
+ grpc_shutdown();
return 0;
}
diff --git a/test/core/security/jwt_verifier_test.c b/test/core/security/jwt_verifier_test.c
index f8afba8d6d..9a21814adc 100644
--- a/test/core/security/jwt_verifier_test.c
+++ b/test/core/security/jwt_verifier_test.c
@@ -166,6 +166,13 @@ static const char claims_without_time_constraint[] =
" \"jti\": \"jwtuniqueid\","
" \"foo\": \"bar\"}";
+static const char claims_with_bad_subject[] =
+ "{ \"aud\": \"https://foo.com\","
+ " \"iss\": \"evil@blah.foo.com\","
+ " \"sub\": \"juju@blah.foo.com\","
+ " \"jti\": \"jwtuniqueid\","
+ " \"foo\": \"bar\"}";
+
static const char invalid_claims[] =
"{ \"aud\": \"https://foo.com\","
" \"iss\": 46," /* Issuer cannot be a number. */
@@ -179,6 +186,38 @@ typedef struct {
const char *expected_subject;
} verifier_test_config;
+static void test_jwt_issuer_email_domain(void) {
+ const char *d = grpc_jwt_issuer_email_domain("https://foo.com");
+ GPR_ASSERT(d == NULL);
+ d = grpc_jwt_issuer_email_domain("foo.com");
+ GPR_ASSERT(d == NULL);
+ d = grpc_jwt_issuer_email_domain("");
+ GPR_ASSERT(d == NULL);
+ d = grpc_jwt_issuer_email_domain("@");
+ GPR_ASSERT(d == NULL);
+ d = grpc_jwt_issuer_email_domain("bar@foo");
+ GPR_ASSERT(strcmp(d, "foo") == 0);
+ d = grpc_jwt_issuer_email_domain("bar@foo.com");
+ GPR_ASSERT(strcmp(d, "foo.com") == 0);
+ d = grpc_jwt_issuer_email_domain("bar@blah.foo.com");
+ GPR_ASSERT(strcmp(d, "foo.com") == 0);
+ d = grpc_jwt_issuer_email_domain("bar.blah@blah.foo.com");
+ GPR_ASSERT(strcmp(d, "foo.com") == 0);
+ d = grpc_jwt_issuer_email_domain("bar.blah@baz.blah.foo.com");
+ GPR_ASSERT(strcmp(d, "foo.com") == 0);
+
+ /* This is not a very good parser but make sure we do not crash on these weird
+ inputs. */
+ d = grpc_jwt_issuer_email_domain("@foo");
+ GPR_ASSERT(strcmp(d, "foo") == 0);
+ d = grpc_jwt_issuer_email_domain("bar@.");
+ GPR_ASSERT(d != NULL);
+ d = grpc_jwt_issuer_email_domain("bar@..");
+ GPR_ASSERT(d != NULL);
+ d = grpc_jwt_issuer_email_domain("bar@...");
+ GPR_ASSERT(d != NULL);
+}
+
static void test_claims_success(void) {
grpc_jwt_claims *claims;
grpc_slice s = grpc_slice_from_copied_string(claims_without_time_constraint);
@@ -242,6 +281,19 @@ static void test_bad_audience_claims_failure(void) {
grpc_jwt_claims_destroy(claims);
}
+static void test_bad_subject_claims_failure(void) {
+ grpc_jwt_claims *claims;
+ grpc_slice s = grpc_slice_from_copied_string(claims_with_bad_subject);
+ grpc_json *json = grpc_json_parse_string_with_len(
+ (char *)GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s));
+ GPR_ASSERT(json != NULL);
+ claims = grpc_jwt_claims_from_json(json, s);
+ GPR_ASSERT(claims != NULL);
+ GPR_ASSERT(grpc_jwt_claims_check(claims, "https://foo.com") ==
+ GRPC_JWT_VERIFIER_BAD_SUBJECT);
+ grpc_jwt_claims_destroy(claims);
+}
+
static char *json_key_str(const char *last_part) {
size_t result_len = strlen(json_key_str_part1) + strlen(json_key_str_part2) +
strlen(last_part);
@@ -563,10 +615,12 @@ static void test_jwt_verifier_bad_format(void) {
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
grpc_init();
+ test_jwt_issuer_email_domain();
test_claims_success();
test_expired_claims_failure();
test_invalid_claims_failure();
test_bad_audience_claims_failure();
+ test_bad_subject_claims_failure();
test_jwt_verifier_google_email_issuer_success();
test_jwt_verifier_custom_email_issuer_success();
test_jwt_verifier_url_issuer_success();
diff --git a/test/core/security/verify_jwt.c b/test/core/security/verify_jwt.c
index 043d29e6bb..ccc85c9f32 100644
--- a/test/core/security/verify_jwt.c
+++ b/test/core/security/verify_jwt.c
@@ -93,6 +93,7 @@ int main(int argc, char **argv) {
char *aud = NULL;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_init();
cl = gpr_cmdline_create("JWT verifier tool");
gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt);
gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud);
@@ -131,5 +132,6 @@ int main(int argc, char **argv) {
grpc_jwt_verifier_destroy(verifier);
gpr_cmdline_destroy(cl);
+ grpc_shutdown();
return !sync.success;
}
diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c
index 78b77fad8e..af232db350 100644
--- a/test/core/support/string_test.c
+++ b/test/core/support/string_test.c
@@ -243,6 +243,8 @@ static void test_int64toa() {
static void test_leftpad() {
char *padded;
+ LOG_TEST_NAME("test_leftpad");
+
padded = gpr_leftpad("foo", ' ', 5);
GPR_ASSERT(0 == strcmp(" foo", padded));
gpr_free(padded);
@@ -273,12 +275,25 @@ static void test_leftpad() {
}
static void test_stricmp(void) {
+ LOG_TEST_NAME("test_stricmp");
+
GPR_ASSERT(0 == gpr_stricmp("hello", "hello"));
GPR_ASSERT(0 == gpr_stricmp("HELLO", "hello"));
GPR_ASSERT(gpr_stricmp("a", "b") < 0);
GPR_ASSERT(gpr_stricmp("b", "a") > 0);
}
+static void test_memrchr(void) {
+ LOG_TEST_NAME("test_memrchr");
+
+ GPR_ASSERT(NULL == gpr_memrchr(NULL, 'a', 0));
+ GPR_ASSERT(NULL == gpr_memrchr("", 'a', 0));
+ GPR_ASSERT(NULL == gpr_memrchr("hello", 'b', 5));
+ GPR_ASSERT(0 == strcmp((const char *)gpr_memrchr("hello", 'h', 5), "hello"));
+ GPR_ASSERT(0 == strcmp((const char *)gpr_memrchr("hello", 'o', 5), "o"));
+ GPR_ASSERT(0 == strcmp((const char *)gpr_memrchr("hello", 'l', 5), "lo"));
+}
+
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_strdup();
@@ -291,5 +306,6 @@ int main(int argc, char **argv) {
test_int64toa();
test_leftpad();
test_stricmp();
+ test_memrchr();
return 0;
}