aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar Julien Boeuf <jboeuf@google.com>2015-04-15 15:52:23 -0700
committerGravatar Julien Boeuf <jboeuf@google.com>2015-04-15 15:56:38 -0700
commit28d75d934958d5bb33422c9ffec1820ff4dca812 (patch)
treed3f9fbc276efb264ab923e91f3f003f379ae5d77 /test
parent0afc3addf4b438d32ab9a7424c0ae588a1c8e212 (diff)
Adding option to add a null terminator when loading a file.
- This will take care of a potential issue with default credentials where the slice pointer is casted as const char * for APIs that need a null terminated string.
Diffstat (limited to 'test')
-rw-r--r--test/core/security/create_jwt.c2
-rw-r--r--test/core/security/fetch_oauth2.c4
-rw-r--r--test/core/support/file_test.c23
3 files changed, 22 insertions, 7 deletions
diff --git a/test/core/security/create_jwt.c b/test/core/security/create_jwt.c
index 614dd1e50c..b02469fb35 100644
--- a/test/core/security/create_jwt.c
+++ b/test/core/security/create_jwt.c
@@ -48,7 +48,7 @@ void create_jwt(const char *json_key_file_path, const char *service_url,
grpc_auth_json_key key;
int ok = 0;
char *jwt;
- gpr_slice json_key_data = gpr_load_file(json_key_file_path, &ok);
+ gpr_slice json_key_data = gpr_load_file(json_key_file_path, 1, &ok);
if (!ok) {
fprintf(stderr, "Could not read %s.\n", json_key_file_path);
exit(1);
diff --git a/test/core/security/fetch_oauth2.c b/test/core/security/fetch_oauth2.c
index cc847c82f7..7a40fe0dbb 100644
--- a/test/core/security/fetch_oauth2.c
+++ b/test/core/security/fetch_oauth2.c
@@ -77,7 +77,7 @@ static void on_oauth2_response(void *user_data, grpc_mdelem **md_elems,
static grpc_credentials *create_service_account_creds(
const char *json_key_file_path, const char *scope) {
int success;
- gpr_slice json_key = gpr_load_file(json_key_file_path, &success);
+ gpr_slice json_key = gpr_load_file(json_key_file_path, 1, &success);
if (!success) {
gpr_log(GPR_ERROR, "Could not read file %s.", json_key_file_path);
exit(1);
@@ -91,7 +91,7 @@ static grpc_credentials *create_refresh_token_creds(
const char *json_refresh_token_file_path) {
int success;
gpr_slice refresh_token =
- gpr_load_file(json_refresh_token_file_path, &success);
+ gpr_load_file(json_refresh_token_file_path, 1, &success);
if (!success) {
gpr_log(GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path);
exit(1);
diff --git a/test/core/support/file_test.c b/test/core/support/file_test.c
index dd8caf7a80..56e7a4948e 100644
--- a/test/core/support/file_test.c
+++ b/test/core/support/file_test.c
@@ -49,6 +49,7 @@ static const char prefix[] = "file_test";
static void test_load_empty_file(void) {
FILE *tmp = NULL;
gpr_slice slice;
+ gpr_slice slice_with_null_term;
int success;
char *tmp_name;
@@ -59,13 +60,19 @@ static void test_load_empty_file(void) {
GPR_ASSERT(tmp != NULL);
fclose(tmp);
- slice = gpr_load_file(tmp_name, &success);
+ slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0);
+ slice_with_null_term = gpr_load_file(tmp_name, 1, &success);
+ GPR_ASSERT(success == 1);
+ GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == 1);
+ GPR_ASSERT(GPR_SLICE_START_PTR(slice_with_null_term)[0] == 0);
+
remove(tmp_name);
gpr_free(tmp_name);
gpr_slice_unref(slice);
+ gpr_slice_unref(slice_with_null_term);
}
static void test_load_failure(void) {
@@ -82,7 +89,7 @@ static void test_load_failure(void) {
fclose(tmp);
remove(tmp_name);
- slice = gpr_load_file(tmp_name, &success);
+ slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 0);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0);
gpr_free(tmp_name);
@@ -92,6 +99,7 @@ static void test_load_failure(void) {
static void test_load_small_file(void) {
FILE *tmp = NULL;
gpr_slice slice;
+ gpr_slice slice_with_null_term;
int success;
char *tmp_name;
const char *blah = "blah";
@@ -104,14 +112,21 @@ static void test_load_small_file(void) {
GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah));
fclose(tmp);
- slice = gpr_load_file(tmp_name, &success);
+ slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah));
GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah)));
+ slice_with_null_term = gpr_load_file(tmp_name, 1, &success);
+ GPR_ASSERT(success == 1);
+ GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1));
+ GPR_ASSERT(strcmp((const char *)GPR_SLICE_START_PTR(slice_with_null_term),
+ blah) == 0);
+
remove(tmp_name);
gpr_free(tmp_name);
gpr_slice_unref(slice);
+ gpr_slice_unref(slice_with_null_term);
}
static void test_load_big_file(void) {
@@ -135,7 +150,7 @@ static void test_load_big_file(void) {
GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer));
fclose(tmp);
- slice = gpr_load_file(tmp_name, &success);
+ slice = gpr_load_file(tmp_name, 0, &success);
GPR_ASSERT(success == 1);
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer));
current = GPR_SLICE_START_PTR(slice);