diff options
author | Stef Walter <stef@thewalter.net> | 2013-10-09 21:39:42 +0200 |
---|---|---|
committer | Stef Walter <stef@thewalter.net> | 2014-07-08 08:57:30 +0200 |
commit | 895f0416448c297a3d06160d748cd0e94eadb366 (patch) | |
tree | e4616391553d5c5979d4770baa9c9b8004d8ae6c | |
parent | ccc5e1569b360b54962e7f4cfaded8ab466b021d (diff) |
test: Move some file and directory code into general test stuff
-rw-r--r-- | common/test.c | 83 | ||||
-rw-r--r-- | common/test.h | 10 | ||||
-rw-r--r-- | trust/tests/test-token.c | 52 | ||||
-rw-r--r-- | trust/tests/test-trust.c | 75 | ||||
-rw-r--r-- | trust/tests/test-trust.h | 28 |
5 files changed, 119 insertions, 129 deletions
diff --git a/common/test.c b/common/test.c index 83e9644..a006f74 100644 --- a/common/test.c +++ b/common/test.c @@ -42,6 +42,7 @@ #include "path.h" #include <assert.h> +#include <dirent.h> #include <errno.h> #include <setjmp.h> #include <stdarg.h> @@ -339,6 +340,88 @@ p11_test_directory (const char *prefix) return directory; } +void +p11_test_file_write (const char *base, + const char *name, + const void *contents, + size_t length) +{ + char *path = NULL; + FILE *f; + + if (base) { + if (asprintf (&path, "%s/%s", base, name) < 0) + assert_not_reached (); + name = path; + } + + f = fopen (name, "wb"); + if (f == NULL) { + printf ("# couldn't open file for writing: %s: %s\n", name, strerror (errno)); + free (path); + assert_not_reached (); + } + + if (fwrite (contents, 1, length, f) != length || + fclose (f) != 0) { + printf ("# couldn't write to file: %s: %s\n", name, strerror (errno)); + free (path); + assert_not_reached (); + } + + free (path); +} + +void +p11_test_file_delete (const char *base, + const char *name) +{ + char *path = NULL; + + if (base) { + if (asprintf (&path, "%s/%s", base, name) < 0) + assert_not_reached (); + name = path; + } + + if (unlink (name) < 0) { + printf ("# Couldn't delete file: %s\n", name); + free (path); + assert_not_reached (); + } + + free (path); +} + +void +p11_test_directory_delete (const char *directory) +{ + struct dirent *dp; + DIR *dir; + + dir = opendir (directory); + if (dir == NULL) { + printf ("# Couldn't open directory: %s\n", directory); + assert_not_reached (); + } + + while ((dp = readdir (dir)) != NULL) { + if (strcmp (dp->d_name, ".") == 0 || + strcmp (dp->d_name, "..") == 0) + continue; + + p11_test_file_delete (directory, dp->d_name); + } + + closedir (dir); + + if (rmdir (directory) < 0) { + printf ("# Couldn't remove directory: %s\n", directory); + assert_not_reached (); + } +} + + #ifdef OS_UNIX static void diff --git a/common/test.h b/common/test.h index c9f519a..2d9fa69 100644 --- a/common/test.h +++ b/common/test.h @@ -130,6 +130,16 @@ int p11_test_run (int argc, char * p11_test_directory (const char *prefix); +void p11_test_directory_delete (const char *directory); + +void p11_test_file_write (const char *directory, + const char *name, + const void *contents, + size_t length); + +void p11_test_file_delete (const char *directory, + const char *name); + #ifdef OS_UNIX char * p11_test_copy_setgid (const char *path); diff --git a/trust/tests/test-token.c b/trust/tests/test-token.c index 965de76..f4367c3 100644 --- a/trust/tests/test-token.c +++ b/trust/tests/test-token.c @@ -90,7 +90,7 @@ teardown (void *path) static void teardown_temp (void *unused) { - test_delete_directory (test.directory); + p11_test_directory_delete (test.directory); free (test.directory); teardown (test.directory); } @@ -299,8 +299,8 @@ test_load_already (void) CK_OBJECT_HANDLE handle; int ret; - test_write_file (test.directory, "test.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "test.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); ret = p11_token_load (test.token); assert_num_eq (ret, 1); @@ -327,14 +327,14 @@ test_load_unreadable (void) int ret; - test_write_file (test.directory, "test.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "test.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); ret = p11_token_load (test.token); assert_num_eq (ret, 1); assert (p11_index_find (test.index, cert, -1) != 0); - test_write_file (test.directory, "test.cer", "", 0); + p11_test_file_write (test.directory, "test.cer", "", 0); /* Have to wait to make sure changes are detected */ p11_sleep_ms (1100); @@ -356,14 +356,14 @@ test_load_gone (void) int ret; - test_write_file (test.directory, "test.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "test.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); ret = p11_token_load (test.token); assert_num_eq (ret, 1); assert (p11_index_find (test.index, cert, -1) != 0); - test_delete_file (test.directory, "test.cer"); + p11_test_file_delete (test.directory, "test.cer"); /* Have to wait to make sure changes are detected */ p11_sleep_ms (1100); @@ -392,8 +392,8 @@ test_load_found (void) /* Have to wait to make sure changes are detected */ p11_sleep_ms (1100); - test_write_file (test.directory, "test.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "test.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); ret = p11_token_load (test.token); assert_num_eq (ret, 1); @@ -421,8 +421,8 @@ test_reload_changed (void) int ret; /* Just one file */ - test_write_file (test.directory, "test.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "test.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); ret = p11_token_load (test.token); assert_num_eq (ret, 1); @@ -430,12 +430,12 @@ test_reload_changed (void) assert (handle != 0); /* Replace the file with verisign */ - test_write_file (test.directory, "test.cer", verisign_v1_ca, - sizeof (verisign_v1_ca)); + p11_test_file_write (test.directory, "test.cer", verisign_v1_ca, + sizeof (verisign_v1_ca)); /* Add another file with cacert3, but not reloaded */ - test_write_file (test.directory, "another.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "another.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); attrs = p11_index_lookup (test.index, handle); assert_ptr_not_null (attrs); @@ -467,10 +467,10 @@ test_reload_gone (void) int ret; /* Just one file */ - test_write_file (test.directory, "cacert3.cer", test_cacert3_ca_der, - sizeof (test_cacert3_ca_der)); - test_write_file (test.directory, "verisign.cer", verisign_v1_ca, - sizeof (verisign_v1_ca)); + p11_test_file_write (test.directory, "cacert3.cer", test_cacert3_ca_der, + sizeof (test_cacert3_ca_der)); + p11_test_file_write (test.directory, "verisign.cer", verisign_v1_ca, + sizeof (verisign_v1_ca)); ret = p11_token_load (test.token); assert_num_eq (ret, 2); @@ -478,8 +478,8 @@ test_reload_gone (void) assert (handle != 0); assert (p11_index_find (test.index, verisign, -1) != 0); - test_delete_file (test.directory, "cacert3.cer"); - test_delete_file (test.directory, "verisign.cer"); + p11_test_file_delete (test.directory, "cacert3.cer"); + p11_test_file_delete (test.directory, "verisign.cer"); attrs = p11_index_lookup (test.index, handle); assert_ptr_not_null (attrs); @@ -633,7 +633,7 @@ test_modify_multiple (void) int ret; CK_RV rv; - test_write_file (test.directory, "Test.p11-kit", test_data, strlen (test_data)); + p11_test_file_write (test.directory, "Test.p11-kit", test_data, strlen (test_data)); /* Reload now that we have this new file */ p11_token_load (test.token); @@ -673,7 +673,7 @@ test_remove_one (void) CK_OBJECT_HANDLE handle; CK_RV rv; - test_write_file (test.directory, "Test.p11-kit", test_data, strlen (test_data)); + p11_test_file_write (test.directory, "Test.p11-kit", test_data, strlen (test_data)); test_check_directory (test.directory, ("Test.p11-kit", NULL)); /* Reload now that we have this new file */ @@ -730,7 +730,7 @@ test_remove_multiple (void) int ret; CK_RV rv; - test_write_file (test.directory, "Test.p11-kit", test_data, strlen (test_data)); + p11_test_file_write (test.directory, "Test.p11-kit", test_data, strlen (test_data)); /* Reload now that we have this new file */ p11_token_load (test.token); diff --git a/trust/tests/test-trust.c b/trust/tests/test-trust.c index 8c69107..20306e0 100644 --- a/trust/tests/test-trust.c +++ b/trust/tests/test-trust.c @@ -329,78 +329,3 @@ test_check_directory_msg (const char *file, p11_dict_free (files); } - -void -test_write_file_msg (const char *file, - int line, - const char *function, - const char *directory, - const char *name, - const void *contents, - size_t length) -{ - char *path; - FILE *f; - - if (asprintf (&path, "%s/%s", directory, name) < 0) - assert_not_reached (); - - f = fopen (path, "wb"); - if (f == NULL) { - p11_test_fail (file, line, function, "Couldn't open file for writing: %s: %s", - path, strerror (errno)); - } - - if (fwrite (contents, 1, length, f) != length || - fclose (f) != 0) { - p11_test_fail (file, line, function, "Couldn't write file: %s: %s", - path, strerror (errno)); - } - - free (path); -} - -void -test_delete_file_msg (const char *file, - int line, - const char *function, - const char *directory, - const char *name) -{ - char *path; - - if (asprintf (&path, "%s/%s", directory, name) < 0) - assert_not_reached (); - - if (unlink (path) < 0) - p11_test_fail (file, line, function, "Couldn't delete file: %s", path); - - free (path); -} - -void -test_delete_directory_msg (const char *file, - int line, - const char *function, - const char *directory) -{ - struct dirent *dp; - DIR *dir; - - dir = opendir (directory); - if (dir == NULL) - p11_test_fail (file ,line, function, "Couldn't open directory: %s", directory); - - while ((dp = readdir (dir)) != NULL) { - if (strcmp (dp->d_name, ".") == 0 || - strcmp (dp->d_name, "..") == 0) - continue; - - test_delete_file_msg (file, line, function, directory, dp->d_name); - } - - closedir (dir); - - if (rmdir (directory) < 0) - p11_test_fail (file, line, function, "Couldn't remove directory: %s", directory); -} diff --git a/trust/tests/test-trust.h b/trust/tests/test-trust.h index b5aab5b..b70bbdb 100644 --- a/trust/tests/test-trust.h +++ b/trust/tests/test-trust.h @@ -406,32 +406,4 @@ void test_check_directory_msg (const char *file, (test_check_directory_msg (__FILE__, __LINE__, __FUNCTION__, directory, \ test_check_directory_files files)) -#define test_write_file(directory, name, data, length) \ - (test_write_file_msg (__FILE__, __LINE__, __FUNCTION__, directory, name, data, length)) - -void test_write_file_msg (const char *file, - int line, - const char *function, - const char *directory, - const char *name, - const void *contents, - size_t length); - -#define test_delete_file(directory, name) \ - (test_delete_file_msg (__FILE__, __LINE__, __FUNCTION__, directory, name)) - -void test_delete_file_msg (const char *file, - int line, - const char *function, - const char *directory, - const char *name); - -#define test_delete_directory(directory) \ - (test_delete_directory_msg (__FILE__, __LINE__, __FUNCTION__, directory)) - -void test_delete_directory_msg (const char *file, - int line, - const char *function, - const char *directory); - #endif /* TEST_DATA_H_ */ |