From 3616add1d71edb348fe2007118601e97b438b290 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 3 Aug 2017 12:40:13 +0200 Subject: [PATCH 1/5] Enhance zcertstore.c tests with SELFTEST_DIR_RW support --- src/zcertstore.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/zcertstore.c b/src/zcertstore.c index 25db99ab5..aa19a9a4e 100644 --- a/src/zcertstore.c +++ b/src/zcertstore.c @@ -316,12 +316,46 @@ zcertstore_test (bool verbose) printf ("\n"); // @selftest + + // Note: If your selftest reads SCMed fixture data, please keep it in + // src/selftest-ro; if your test creates filesystem objects, please + // do so under src/selftest-rw. They are defined below along with a + // usecase for the variables (assert) to make compilers happy. + const char *SELFTEST_DIR_RO = "src/selftest-ro"; + const char *SELFTEST_DIR_RW = "src/selftest-rw"; + assert (SELFTEST_DIR_RO); + assert (SELFTEST_DIR_RW); + // Uncomment these to use C++ strings in C++ selftest code: + //std::string str_SELFTEST_DIR_RO = std::string(SELFTEST_DIR_RO); + //std::string str_SELFTEST_DIR_RW = std::string(SELFTEST_DIR_RW); + //assert ( (str_SELFTEST_DIR_RO != "") ); + //assert ( (str_SELFTEST_DIR_RW != "") ); + // NOTE that for "char*" context you need (str_SELFTEST_DIR_RO + "/myfilename").c_str() + + const char *testbasedir = ".test_zcertstore"; + const char *testfile = "mycert.txt"; + char *basedirpath = NULL; // subdir in a test, under SELFTEST_DIR_RW + char *filepath = NULL; // pathname to testfile in a test, in dirpath + + basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir); + assert (basedirpath); + filepath = zsys_sprintf ("%s/%s", basedirpath, testfile); + assert (filepath); + + // Make sure old aborted tests do not hinder us + zdir_t *dir = zdir_new (basedirpath, NULL); + if (dir) { + zdir_remove (dir, true); + zdir_destroy (&dir); + } + zsys_file_delete (filepath); + zsys_dir_delete (basedirpath); + // Create temporary directory for test files -# define TESTDIR ".test_zcertstore" - zsys_dir_create (TESTDIR); + zsys_dir_create (basedirpath); // Load certificate store from disk; it will be empty - zcertstore_t *certstore = zcertstore_new (TESTDIR); + zcertstore_t *certstore = zcertstore_new (basedirpath); assert (certstore); // Create a single new certificate and save to disk @@ -330,7 +364,7 @@ zcertstore_test (bool verbose) char *client_key = strdup (zcert_public_txt (cert)); assert (client_key); zcert_set_meta (cert, "name", "John Doe"); - zcert_save (cert, TESTDIR "/mycert.txt"); + zcert_save (cert, filepath); zcert_destroy (&cert); // Check that certificate store refreshes as expected @@ -370,11 +404,14 @@ zcertstore_test (bool verbose) zcertstore_destroy (&certstore); // Delete all test files - zdir_t *dir = zdir_new (TESTDIR, NULL); + dir = zdir_new (basedirpath, NULL); assert (dir); zdir_remove (dir, true); zdir_destroy (&dir); + zstr_free (&basedirpath); + zstr_free (&filepath); + #if defined (__WINDOWS__) zsys_shutdown(); #endif From 3f268b690ebef4412f541a9871be0bc70b834753 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 3 Aug 2017 13:09:28 +0200 Subject: [PATCH 2/5] Enhance zcert.c tests with SELFTEST_DIR_RW support --- src/zcert.c | 59 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/src/zcert.c b/src/zcert.c index d5b59ac6e..afc26cf16 100644 --- a/src/zcert.c +++ b/src/zcert.c @@ -461,9 +461,46 @@ zcert_test (bool verbose) { printf (" * zcert: "); // @selftest + + // Note: If your selftest reads SCMed fixture data, please keep it in + // src/selftest-ro; if your test creates filesystem objects, please + // do so under src/selftest-rw. They are defined below along with a + // usecase for the variables (assert) to make compilers happy. + const char *SELFTEST_DIR_RO = "src/selftest-ro"; + const char *SELFTEST_DIR_RW = "src/selftest-rw"; + assert (SELFTEST_DIR_RO); + assert (SELFTEST_DIR_RW); + // Uncomment these to use C++ strings in C++ selftest code: + //std::string str_SELFTEST_DIR_RO = std::string(SELFTEST_DIR_RO); + //std::string str_SELFTEST_DIR_RW = std::string(SELFTEST_DIR_RW); + //assert ( (str_SELFTEST_DIR_RO != "") ); + //assert ( (str_SELFTEST_DIR_RW != "") ); + // NOTE that for "char*" context you need (str_SELFTEST_DIR_RO + "/myfilename").c_str() + + const char *testbasedir = ".test_zcert"; + const char *testfile = "mycert.txt"; + char *basedirpath = NULL; // subdir in a test, under SELFTEST_DIR_RW + char *filepath = NULL; // pathname to testfile in a test, in dirpath + char *filepath_s = NULL; // pathname to testfile+secret in a test, in dirpath + + basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir); + assert (basedirpath); + filepath = zsys_sprintf ("%s/%s", basedirpath, testfile); + assert (filepath); + filepath_s = zsys_sprintf ("%s_secret", filepath); + assert (filepath_s); + + // Make sure old aborted tests do not hinder us + zdir_t *dir = zdir_new (basedirpath, NULL); + if (dir) { + zdir_remove (dir, true); + zdir_destroy (&dir); + } + zsys_file_delete (filepath); + zsys_dir_delete (basedirpath); + // Create temporary directory for test files -# define TESTDIR ".test_zcert" - zsys_dir_create (TESTDIR); + zsys_dir_create (basedirpath); // Create a simple certificate with metadata zcert_t *cert = zcert_new (); @@ -485,20 +522,20 @@ zcert_test (bool verbose) zcert_destroy (&shadow); // Check we can save and load certificate - zcert_save (cert, TESTDIR "/mycert.txt"); - assert (zsys_file_exists (TESTDIR "/mycert.txt")); - assert (zsys_file_exists (TESTDIR "/mycert.txt_secret")); + zcert_save (cert, filepath); + assert (zsys_file_exists (filepath)); + assert (zsys_file_exists (filepath_s)); // Load certificate, will in fact load secret one - shadow = zcert_load (TESTDIR "/mycert.txt"); + shadow = zcert_load (filepath); assert (shadow); assert (zcert_eq (cert, shadow)); zcert_destroy (&shadow); // Delete secret certificate, load public one - int rc = zsys_file_delete (TESTDIR "/mycert.txt_secret"); + int rc = zsys_file_delete (filepath_s); assert (rc == 0); - shadow = zcert_load (TESTDIR "/mycert.txt"); + shadow = zcert_load (filepath); // 32-byte null key encodes as 40 '0' characters assert (streq (zcert_secret_txt (shadow), FORTY_ZEROES)); @@ -515,11 +552,15 @@ zcert_test (bool verbose) zcert_destroy (&cert); // Delete all test files - zdir_t *dir = zdir_new (TESTDIR, NULL); + dir = zdir_new (basedirpath, NULL); assert (dir); zdir_remove (dir, true); zdir_destroy (&dir); + zstr_free (&basedirpath); + zstr_free (&filepath); + zstr_free (&filepath_s); + #if defined (__WINDOWS__) zsys_shutdown(); #endif From 802426fa369b18ee660784116d000366c61d7e49 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 3 Aug 2017 14:28:03 +0200 Subject: [PATCH 3/5] Enhance zproxy.c tests with SELFTEST_DIR_RW support --- src/zproxy.c | 61 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/src/zproxy.c b/src/zproxy.c index 149950d8c..20b94d399 100644 --- a/src/zproxy.c +++ b/src/zproxy.c @@ -480,6 +480,36 @@ zproxy_test (bool verbose) printf ("\n"); // @selftest + + // Note: If your selftest reads SCMed fixture data, please keep it in + // src/selftest-ro; if your test creates filesystem objects, please + // do so under src/selftest-rw. They are defined below along with a + // usecase for the variables (assert) to make compilers happy. + const char *SELFTEST_DIR_RO = "src/selftest-ro"; + const char *SELFTEST_DIR_RW = "src/selftest-rw"; + assert (SELFTEST_DIR_RO); + assert (SELFTEST_DIR_RW); + // Uncomment these to use C++ strings in C++ selftest code: + //std::string str_SELFTEST_DIR_RO = std::string(SELFTEST_DIR_RO); + //std::string str_SELFTEST_DIR_RW = std::string(SELFTEST_DIR_RW); + //assert ( (str_SELFTEST_DIR_RO != "") ); + //assert ( (str_SELFTEST_DIR_RW != "") ); + // NOTE that for "char*" context you need (str_SELFTEST_DIR_RO + "/myfilename").c_str() + + const char *testbasedir = ".test_zproxy"; + const char *testpassfile = "password-file"; + const char *testcertfile = "mycert.txt"; + char *basedirpath = NULL; // subdir in a test, under SELFTEST_DIR_RW + char *passfilepath = NULL; // pathname to testfile in a test, in dirpath + char *certfilepath = NULL; // pathname to testfile in a test, in dirpath + + basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir); + assert (basedirpath); + passfilepath = zsys_sprintf ("%s/%s", basedirpath, testpassfile); + assert (passfilepath); + certfilepath = zsys_sprintf ("%s/%s", basedirpath, testcertfile); + assert (certfilepath); + // Create and configure our proxy zactor_t *proxy = zactor_new (zproxy, NULL); assert (proxy); @@ -571,10 +601,19 @@ zproxy_test (bool verbose) #if (ZMQ_VERSION_MAJOR == 4) // Test authentication functionality -# define TESTDIR ".test_zproxy" + + // Make sure old aborted tests do not hinder us + zdir_t *dir = zdir_new (basedirpath, NULL); + if (dir) { + zdir_remove (dir, true); + zdir_destroy (&dir); + } + zsys_file_delete (passfilepath); + zsys_file_delete (certfilepath); + zsys_dir_delete (basedirpath); // Create temporary directory for test files - zsys_dir_create (TESTDIR); + zsys_dir_create (basedirpath); char *frontend = NULL; char *backend = NULL; @@ -645,7 +684,7 @@ zproxy_test (bool verbose) assert (!success); // Test positive case (server-side passwords defined) - FILE *password = fopen (TESTDIR "/password-file", "w"); + FILE *password = fopen (passfilepath, "w"); assert (password); fprintf (password, "admin=Password\n"); fclose (password); @@ -658,7 +697,7 @@ zproxy_test (bool verbose) zsock_set_plain_password (faucet, "Password"); zsock_set_plain_username (sink, "admin"); zsock_set_plain_password (sink, "Password"); - zstr_sendx (auth, "PLAIN", TESTDIR "/password-file", NULL); + zstr_sendx (auth, "PLAIN", passfilepath, NULL); zsock_wait (auth); success = s_can_connect (&proxy, &faucet, &sink, frontend, backend, verbose, true); @@ -718,8 +757,8 @@ zproxy_test (bool verbose) zsock_set_curve_serverkey (faucet, public_key); zcert_apply (client_cert, sink); zsock_set_curve_serverkey (sink, public_key); - zcert_save_public (client_cert, TESTDIR "/mycert.txt"); - zstr_sendx (auth, "CURVE", TESTDIR, NULL); + zcert_save_public (client_cert, certfilepath); + zstr_sendx (auth, "CURVE", basedirpath, NULL); zsock_wait (auth); success = s_can_connect (&proxy, &faucet, &sink, frontend, backend, verbose, true); @@ -744,11 +783,15 @@ zproxy_test (bool verbose) zstr_free (&backend); // Delete temporary directory and test files - zsys_file_delete (TESTDIR "/password-file"); - zsys_file_delete (TESTDIR "/mycert.txt"); - zsys_dir_delete (TESTDIR); + zsys_file_delete (passfilepath); + zsys_file_delete (certfilepath); + zsys_dir_delete (basedirpath); #endif + zstr_free (&passfilepath); + zstr_free (&certfilepath); + zstr_free (&basedirpath); + #if defined (__WINDOWS__) zsys_shutdown(); #endif From ec0bdcd1e32ebcce363df9b848c58dd8ab0d33fc Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 3 Aug 2017 14:41:31 +0200 Subject: [PATCH 4/5] Enhance zconfig.c tests with SELFTEST_DIR_RW support --- src/zconfig.c | 53 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/zconfig.c b/src/zconfig.c index cc2ef2f52..930d1995e 100644 --- a/src/zconfig.c +++ b/src/zconfig.c @@ -946,9 +946,43 @@ zconfig_test (bool verbose) printf (" * zconfig: "); // @selftest + + // Note: If your selftest reads SCMed fixture data, please keep it in + // src/selftest-ro; if your test creates filesystem objects, please + // do so under src/selftest-rw. They are defined below along with a + // usecase for the variables (assert) to make compilers happy. + const char *SELFTEST_DIR_RO = "src/selftest-ro"; + const char *SELFTEST_DIR_RW = "src/selftest-rw"; + assert (SELFTEST_DIR_RO); + assert (SELFTEST_DIR_RW); + // Uncomment these to use C++ strings in C++ selftest code: + //std::string str_SELFTEST_DIR_RO = std::string(SELFTEST_DIR_RO); + //std::string str_SELFTEST_DIR_RW = std::string(SELFTEST_DIR_RW); + //assert ( (str_SELFTEST_DIR_RO != "") ); + //assert ( (str_SELFTEST_DIR_RW != "") ); + // NOTE that for "char*" context you need (str_SELFTEST_DIR_RO + "/myfilename").c_str() + + const char *testbasedir = ".test_zconfig"; + const char *testfile = "test.cfg"; + char *basedirpath = NULL; // subdir in a test, under SELFTEST_DIR_RW + char *filepath = NULL; // pathname to testfile in a test, in dirpath + + basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir); + assert (basedirpath); + filepath = zsys_sprintf ("%s/%s", basedirpath, testfile); + assert (filepath); + + // Make sure old aborted tests do not hinder us + zdir_t *dir = zdir_new (basedirpath, NULL); + if (dir) { + zdir_remove (dir, true); + zdir_destroy (&dir); + } + zsys_file_delete (filepath); + zsys_dir_delete (basedirpath); + // Create temporary directory for test files -# define TESTDIR ".test_zconfig" - zsys_dir_create (TESTDIR); + zsys_dir_create (basedirpath); zconfig_t *root = zconfig_new ("root", NULL); assert (root); @@ -966,12 +1000,12 @@ zconfig_test (bool verbose) zconfig_set_comment (root, " CURVE certificate"); zconfig_set_comment (root, " -----------------"); assert (zconfig_comments (root)); - zconfig_save (root, TESTDIR "/test.cfg"); + zconfig_save (root, filepath); zconfig_destroy (&root); - root = zconfig_load (TESTDIR "/test.cfg"); + root = zconfig_load (filepath); if (verbose) zconfig_save (root, "-"); - assert (streq (zconfig_filename (root), TESTDIR "/test.cfg")); + assert (streq (zconfig_filename (root), filepath)); char *email = zconfig_get (root, "/headers/email", NULL); assert (email); @@ -980,7 +1014,7 @@ zconfig_test (bool verbose) assert (passwd); assert (streq (passwd, "Top Secret")); - zconfig_savef (root, "%s/%s", TESTDIR, "test.cfg"); + zconfig_savef (root, "%s/%s", basedirpath, testfile); assert (!zconfig_has_changed (root)); int rc = zconfig_reload (&root); assert (rc == 0); @@ -1014,7 +1048,7 @@ zconfig_test (bool verbose) // Test config can't be saved to a file in a path that doesn't // exist or isn't writable - rc = zconfig_savef (root, "%s/path/that/doesnt/exist/%s", TESTDIR, "test.cfg"); + rc = zconfig_savef (root, "%s/path/that/doesnt/exist/%s", basedirpath, testfile); assert (rc == -1); zconfig_destroy (&root); @@ -1054,11 +1088,14 @@ zconfig_test (bool verbose) zconfig_destroy (&config); // Delete all test files - zdir_t *dir = zdir_new (TESTDIR, NULL); + dir = zdir_new (basedirpath, NULL); assert (dir); zdir_remove (dir, true); zdir_destroy (&dir); + zstr_free (&basedirpath); + zstr_free (&filepath); + #if defined (__WINDOWS__) zsys_shutdown(); #endif From 99fa71cd4712ea61e539f86d6fb3620bcd962b7b Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 3 Aug 2017 14:47:47 +0200 Subject: [PATCH 5/5] Enhance zauth.c tests with SELFTEST_DIR_RW support --- src/zauth.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/src/zauth.c b/src/zauth.c index 3cc051d26..f11b319ed 100644 --- a/src/zauth.c +++ b/src/zauth.c @@ -594,9 +594,48 @@ zauth_test (bool verbose) printf ("\n"); // @selftest + + // Note: If your selftest reads SCMed fixture data, please keep it in + // src/selftest-ro; if your test creates filesystem objects, please + // do so under src/selftest-rw. They are defined below along with a + // usecase for the variables (assert) to make compilers happy. + const char *SELFTEST_DIR_RO = "src/selftest-ro"; + const char *SELFTEST_DIR_RW = "src/selftest-rw"; + assert (SELFTEST_DIR_RO); + assert (SELFTEST_DIR_RW); + // Uncomment these to use C++ strings in C++ selftest code: + //std::string str_SELFTEST_DIR_RO = std::string(SELFTEST_DIR_RO); + //std::string str_SELFTEST_DIR_RW = std::string(SELFTEST_DIR_RW); + //assert ( (str_SELFTEST_DIR_RO != "") ); + //assert ( (str_SELFTEST_DIR_RW != "") ); + // NOTE that for "char*" context you need (str_SELFTEST_DIR_RO + "/myfilename").c_str() + + const char *testbasedir = ".test_zauth"; + const char *testpassfile = "password-file"; + const char *testcertfile = "mycert.txt"; + char *basedirpath = NULL; // subdir in a test, under SELFTEST_DIR_RW + char *passfilepath = NULL; // pathname to testfile in a test, in dirpath + char *certfilepath = NULL; // pathname to testfile in a test, in dirpath + + basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir); + assert (basedirpath); + passfilepath = zsys_sprintf ("%s/%s", basedirpath, testpassfile); + assert (passfilepath); + certfilepath = zsys_sprintf ("%s/%s", basedirpath, testcertfile); + assert (certfilepath); + + // Make sure old aborted tests do not hinder us + zdir_t *dir = zdir_new (basedirpath, NULL); + if (dir) { + zdir_remove (dir, true); + zdir_destroy (&dir); + } + zsys_file_delete (passfilepath); + zsys_file_delete (certfilepath); + zsys_dir_delete (basedirpath); + // Create temporary directory for test files -# define TESTDIR ".test_zauth" - zsys_dir_create (TESTDIR); + zsys_dir_create (basedirpath); // Check there's no authentication zsock_t *server = zsock_new (ZMQ_PULL); @@ -645,14 +684,14 @@ zauth_test (bool verbose) success = s_can_connect (&server, &client, true); assert (!success); - FILE *password = fopen (TESTDIR "/password-file", "w"); + FILE *password = fopen (passfilepath, "w"); assert (password); fprintf (password, "admin=Password\n"); fclose (password); zsock_set_plain_server (server, 1); zsock_set_plain_username (client, "admin"); zsock_set_plain_password (client, "Password"); - zstr_sendx (auth, "PLAIN", TESTDIR "/password-file", NULL); + zstr_sendx (auth, "PLAIN", passfilepath, NULL); zsock_wait (auth); success = s_can_connect (&server, &client, false); assert (success); @@ -709,8 +748,8 @@ zauth_test (bool verbose) zcert_apply (client_cert, client); zsock_set_curve_server (server, 1); zsock_set_curve_serverkey (client, server_key); - zcert_save_public (client_cert, TESTDIR "/mycert.txt"); - zstr_sendx (auth, "CURVE", TESTDIR, NULL); + zcert_save_public (client_cert, certfilepath); + zstr_sendx (auth, "CURVE", basedirpath, NULL); zsock_wait (auth); success = s_can_connect (&server, &client, false); assert (success); @@ -770,10 +809,15 @@ zauth_test (bool verbose) zsock_destroy (&server); // Delete all test files - zdir_t *dir = zdir_new (TESTDIR, NULL); + dir = zdir_new (basedirpath, NULL); assert (dir); zdir_remove (dir, true); zdir_destroy (&dir); + + zstr_free (&passfilepath); + zstr_free (&certfilepath); + zstr_free (&basedirpath); + #endif #if defined (__WINDOWS__)