From 026a417defcd13d0ae5e8a8ddb67c18ff02fa142 Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Mon, 2 Feb 2015 18:36:37 -0800 Subject: Adding support for loading the SSL roots from an environment variable. - Had to add support for files and environment variables as well. - I can't compile on windows so I'm sure there will be some issues. - Tested end-to-end with the simple ssl fullstack test. --- .../end2end/fixtures/chttp2_simple_ssl_fullstack.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'test/core/end2end') diff --git a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c index a70819e47e..876d35a74b 100644 --- a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c +++ b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c @@ -39,6 +39,9 @@ #include "src/core/channel/channel_args.h" #include "src/core/security/credentials.h" #include "src/core/security/security_context.h" +#include "src/core/support/env.h" +#include "src/core/support/file.h" +#include "src/core/support/string.h" #include #include #include @@ -99,7 +102,7 @@ void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) { static void chttp2_init_client_simple_ssl_secure_fullstack( grpc_end2end_test_fixture *f, grpc_channel_args *client_args) { grpc_credentials *ssl_creds = - grpc_ssl_credentials_create(test_root_cert, NULL); + grpc_ssl_credentials_create(NULL, NULL); grpc_arg ssl_name_override = {GRPC_ARG_STRING, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, {"foo.test.google.com"}}; @@ -129,8 +132,21 @@ static grpc_end2end_test_config configs[] = { int main(int argc, char **argv) { size_t i; + FILE *roots_file; + size_t roots_size = strlen(test_root_cert); + char *roots_filename; + grpc_test_init(argc, argv); + /* Set the SSL roots env var. */ + roots_filename = gpr_strdup("chttp2_simple_ssl_fullstack_test_XXXXXX"); + GPR_ASSERT(roots_filename != NULL); + roots_file = gpr_tmpfile(roots_filename); + GPR_ASSERT(roots_file != NULL); + GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size); + fclose(roots_file); + gpr_setenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR, roots_filename); + grpc_init(); for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) { @@ -139,5 +155,9 @@ int main(int argc, char **argv) { grpc_shutdown(); + /* Cleanup. */ + remove(roots_filename); + gpr_free(roots_filename); + return 0; } -- cgit v1.2.3 From 05618967516ca807d4deadea05e90b439c3f2e7c Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Tue, 3 Feb 2015 21:58:53 -0800 Subject: Addressing comments. The new gpr_tmpfile API is actually much nicer to use. Thanks Nico! --- src/core/support/env_posix.c | 3 ++- src/core/support/env_win32.c | 3 ++- src/core/support/file.h | 10 ++++----- src/core/support/file_posix.c | 25 ++++++++++++++++++--- src/core/support/file_win32.c | 26 +++++++++++++++++----- .../end2end/fixtures/chttp2_simple_ssl_fullstack.c | 3 +-- test/core/support/file_test.c | 24 +++++++++++--------- 7 files changed, 65 insertions(+), 29 deletions(-) (limited to 'test/core/end2end') diff --git a/src/core/support/env_posix.c b/src/core/support/env_posix.c index 4af5aea9b0..79b8fcd7d7 100644 --- a/src/core/support/env_posix.c +++ b/src/core/support/env_posix.c @@ -54,7 +54,8 @@ char *gpr_getenv(const char *name) { } void gpr_setenv(const char *name, const char *value) { - GPR_ASSERT(setenv(name, value, 1) == 0); + int res = setenv(name, value, 1); + GPR_ASSERT(res == 0); } #endif /* GPR_POSIX_ENV */ diff --git a/src/core/support/env_win32.c b/src/core/support/env_win32.c index 7964409364..a31fa79d68 100644 --- a/src/core/support/env_win32.c +++ b/src/core/support/env_win32.c @@ -53,7 +53,8 @@ char *gpr_getenv(const char *name) { } void gpr_setenv(const char *name, const char *value) { - GPR_ASSERT(_putenv_s(name, value) == 0); + errno_t res = _putenv_s(name, value); + GPR_ASSERT(res == 0); } #endif /* GPR_WIN32 */ diff --git a/src/core/support/file.h b/src/core/support/file.h index a9d81498e5..92f420e7ce 100644 --- a/src/core/support/file.h +++ b/src/core/support/file.h @@ -48,11 +48,11 @@ extern "C" { will be set to 1 in case of success and 0 in case of failure. */ gpr_slice gpr_load_file(const char *filename, int *success); -/* Creates a temporary file from a template. - The last six characters of template must be "XXXXXX" and these are replaced - with a string that makes the filename unique. Since it will be modified, - template must not be a string constant. */ -FILE *gpr_tmpfile(char *template); +/* Creates a temporary file from a prefix. + If tmp_filename is not NULL, *tmp_filename is assigned the name of the + created file and it is the responsibility of the caller to gpr_free it + unless an error occurs in which case it will be set to NULL. */ +FILE *gpr_tmpfile(const char *prefix, char **tmp_filename); #ifdef __cplusplus } diff --git a/src/core/support/file_posix.c b/src/core/support/file_posix.c index a156e7703c..a763fbcda2 100644 --- a/src/core/support/file_posix.c +++ b/src/core/support/file_posix.c @@ -54,15 +54,26 @@ #include #include +#include #include -FILE *gpr_tmpfile(char *template) { +#include "src/core/support/string.h" + +FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) { FILE *result = NULL; - int fd = mkstemp(template); + char *template; + int fd; + + if (tmp_filename != NULL) *tmp_filename = NULL; + + gpr_asprintf(&template, "%s_XXXXXX", prefix); + GPR_ASSERT(template != NULL); + + fd = mkstemp(template); if (fd == -1) { gpr_log(GPR_ERROR, "mkstemp failed for template %s with error %s.", template, strerror(errno)); - return NULL; + goto end; } result = fdopen(fd, "w+"); if (result == NULL) { @@ -70,6 +81,14 @@ FILE *gpr_tmpfile(char *template) { template, fd, strerror(errno)); unlink(template); close(fd); + goto end; + } + +end: + if (result != NULL && tmp_filename != NULL) { + *tmp_filename = template; + } else { + gpr_free(template); } return result; } diff --git a/src/core/support/file_win32.c b/src/core/support/file_win32.c index b85422c372..d415281e0d 100644 --- a/src/core/support/file_win32.c +++ b/src/core/support/file_win32.c @@ -41,24 +41,38 @@ #include #include +#include #include -FILE *gpr_tmpfile(char *template) { +FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) { FILE *result = NULL; + char *template; + + if (tmp_filename != NULL) *tmp_filename = NULL; + + gpr_asprintf(&template, "%s_XXXXXX", prefix); + GPR_ASSERT(template != NULL); /* _mktemp_s can only create a maximum of 26 file names for any combination of base and template values which is kind of sad... We may revisit this function later to have something better... */ if (_mktemp_s(template, strlen(template) + 1) != 0) { gpr_log(LOG_ERROR, "Could not create tmp file."); - return NULL; + goto end; } - if (fopen_s(&result, template, "wb+") == 0) { - return result; - } else { + if (fopen_s(&result, template, "wb+") != 0) { gpr_log(GPR_ERROR, "Could not open file %s", template); - return NULL; + result = NULL; + goto end; + } + +end: + if (result != NULL && tmp_filename != NULL) { + *tmp_filename = template; + } else { + gpr_free(template); } + return result; } #endif /* GPR_WIN32 */ diff --git a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c index 876d35a74b..149ac8c07b 100644 --- a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c +++ b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c @@ -139,9 +139,8 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); /* Set the SSL roots env var. */ - roots_filename = gpr_strdup("chttp2_simple_ssl_fullstack_test_XXXXXX"); + roots_file = gpr_tmpfile("chttp2_simple_ssl_fullstack_test", &roots_filename); GPR_ASSERT(roots_filename != NULL); - roots_file = gpr_tmpfile(roots_filename); GPR_ASSERT(roots_file != NULL); GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size); fclose(roots_file); diff --git a/test/core/support/file_test.c b/test/core/support/file_test.c index 1d9fb3ff95..b089954186 100644 --- a/test/core/support/file_test.c +++ b/test/core/support/file_test.c @@ -44,17 +44,18 @@ #define LOG_TEST_NAME() gpr_log(GPR_INFO, "%s", __FUNCTION__) -static const char template[] = "file_test_XXXXXX"; +static const char prefix[] = "file_test"; static void test_load_empty_file(void) { FILE *tmp = NULL; gpr_slice slice; int success; - char *tmp_name = gpr_strdup(template); + char *tmp_name; LOG_TEST_NAME(); - tmp = gpr_tmpfile(tmp_name); + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp_name != NULL); GPR_ASSERT(tmp != NULL); fclose(tmp); @@ -71,17 +72,16 @@ static void test_load_failure(void) { FILE *tmp = NULL; gpr_slice slice; int success; - char *tmp_name = gpr_strdup(template); + char *tmp_name; LOG_TEST_NAME(); - tmp = gpr_tmpfile(tmp_name); + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp_name != NULL); GPR_ASSERT(tmp != NULL); fclose(tmp); remove(tmp_name); - GPR_ASSERT(tmp_name != NULL); - slice = gpr_load_file(tmp_name, &success); GPR_ASSERT(success == 0); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); @@ -93,12 +93,13 @@ static void test_load_small_file(void) { FILE *tmp = NULL; gpr_slice slice; int success; - char *tmp_name = gpr_strdup(template); + char *tmp_name; const char *blah = "blah"; LOG_TEST_NAME(); - tmp = gpr_tmpfile(tmp_name); + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp_name != NULL); GPR_ASSERT(tmp != NULL); GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah)); fclose(tmp); @@ -117,7 +118,7 @@ static void test_load_big_file(void) { FILE *tmp = NULL; gpr_slice slice; int success; - char *tmp_name = gpr_strdup(template); + char *tmp_name; unsigned char buffer[124631]; unsigned char *current; size_t i; @@ -128,8 +129,9 @@ static void test_load_big_file(void) { buffer[i] = 42; } - tmp = gpr_tmpfile(tmp_name); + tmp = gpr_tmpfile(prefix, &tmp_name); GPR_ASSERT(tmp != NULL); + GPR_ASSERT(tmp_name != NULL); GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer)); fclose(tmp); -- cgit v1.2.3