From a56fa14fda19044d7f5cce26cf86c6dda8a58eef Mon Sep 17 00:00:00 2001 From: Anderson Toshiyuki Sasaki Date: Tue, 20 Nov 2018 14:48:09 +0100 Subject: [PATCH] tests: Use temporary directories in torture_pki_dsa.c Signed-off-by: Anderson Toshiyuki Sasaki Reviewed-by: Andreas Schneider --- tests/unittests/torture_pki_dsa.c | 106 ++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 28 deletions(-) diff --git a/tests/unittests/torture_pki_dsa.c b/tests/unittests/torture_pki_dsa.c index 41ab9063..12256b27 100644 --- a/tests/unittests/torture_pki_dsa.c +++ b/tests/unittests/torture_pki_dsa.c @@ -14,16 +14,39 @@ #define LIBSSH_DSA_TESTKEY "libssh_testkey.id_dsa" #define LIBSSH_DSA_TESTKEY_PASSPHRASE "libssh_testkey_passphrase.id_dsa" +const char template[] = "temp_dir_XXXXXX"; const unsigned char DSA_HASH[] = "12345678901234567890"; +struct pki_st { + char *cwd; + char *temp_dir; +}; + static int setup_dsa_key(void **state) { - (void) state; /* unused */ + struct pki_st *test_state = NULL; + char *cwd = NULL; + char *tmp_dir = NULL; + int rc = 0; - unlink(LIBSSH_DSA_TESTKEY); - unlink(LIBSSH_DSA_TESTKEY_PASSPHRASE); - unlink(LIBSSH_DSA_TESTKEY ".pub"); - unlink(LIBSSH_DSA_TESTKEY "-cert.pub"); + test_state = (struct pki_st *)malloc(sizeof(struct pki_st)); + assert_non_null(test_state); + + cwd = torture_get_current_working_dir(); + assert_non_null(cwd); + + tmp_dir = torture_make_temp_dir(template); + assert_non_null(tmp_dir); + + test_state->cwd = cwd; + test_state->temp_dir = tmp_dir; + + *state = test_state; + + rc = torture_change_dir(tmp_dir); + assert_int_equal(rc, 0); + + printf("Changed directory to: %s\n", tmp_dir); torture_write_file(LIBSSH_DSA_TESTKEY, torture_get_testkey(SSH_KEYTYPE_DSS, 0, 0)); @@ -39,12 +62,27 @@ static int setup_dsa_key(void **state) static int setup_openssh_dsa_key(void **state) { - (void) state; /* unused */ + struct pki_st *test_state = NULL; + char *cwd = NULL; + char *tmp_dir = NULL; + int rc = 0; - unlink(LIBSSH_DSA_TESTKEY); - unlink(LIBSSH_DSA_TESTKEY_PASSPHRASE); - unlink(LIBSSH_DSA_TESTKEY ".pub"); - unlink(LIBSSH_DSA_TESTKEY "-cert.pub"); + test_state = (struct pki_st *)malloc(sizeof(struct pki_st)); + assert_non_null(test_state); + + cwd = torture_get_current_working_dir(); + assert_non_null(cwd); + + tmp_dir = torture_make_temp_dir(template); + assert_non_null(tmp_dir); + + test_state->cwd = cwd; + test_state->temp_dir = tmp_dir; + + *state = test_state; + + rc = torture_change_dir(tmp_dir); + assert_int_equal(rc, 0); torture_write_file(LIBSSH_DSA_TESTKEY, torture_get_openssh_testkey(SSH_KEYTYPE_DSS, 0, 0)); @@ -58,14 +96,26 @@ static int setup_openssh_dsa_key(void **state) return 0; } -static int teardown_dsa_key(void **state) -{ - (void)state; +static int teardown(void **state) { - unlink(LIBSSH_DSA_TESTKEY); - unlink(LIBSSH_DSA_TESTKEY_PASSPHRASE); - unlink(LIBSSH_DSA_TESTKEY ".pub"); - unlink(LIBSSH_DSA_TESTKEY "-cert.pub"); + struct pki_st *test_state = NULL; + int rc = 0; + + test_state = *((struct pki_st **)state); + + assert_non_null(test_state); + assert_non_null(test_state->cwd); + assert_non_null(test_state->temp_dir); + + rc = torture_change_dir(test_state->cwd); + assert_int_equal(rc, 0); + + rc = torture_rmdirs(test_state->temp_dir); + assert_int_equal(rc, 0); + + SAFE_FREE(test_state->temp_dir); + SAFE_FREE(test_state->cwd); + SAFE_FREE(test_state); return 0; } @@ -586,26 +636,26 @@ int torture_run_tests(void) struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(torture_pki_dsa_import_pubkey_file, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_import_pubkey_from_openssh_privkey, setup_openssh_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_import_privkey_base64, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_import_privkey_base64, setup_openssh_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_publickey_from_privatekey, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_import_cert_file, setup_dsa_key, - teardown_dsa_key), + teardown), #ifdef HAVE_LIBCRYPTO cmocka_unit_test_setup_teardown(torture_pki_dsa_write_privkey, setup_dsa_key, - teardown_dsa_key), + teardown), #endif cmocka_unit_test(torture_pki_dsa_import_privkey_base64_passphrase), cmocka_unit_test(torture_pki_dsa_import_openssh_privkey_base64_passphrase), @@ -613,16 +663,16 @@ int torture_run_tests(void) /* public key */ cmocka_unit_test_setup_teardown(torture_pki_dsa_publickey_base64, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_generate_pubkey_from_privkey, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_duplicate_key, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test_setup_teardown(torture_pki_dsa_duplicate_key, setup_dsa_key, - teardown_dsa_key), + teardown), cmocka_unit_test(torture_pki_dsa_generate_key), };