Skip to content

Commit

Permalink
Merge pull request #1733 from jimklimov/selftest-rw
Browse files Browse the repository at this point in the history
Problem: More components do not use SELFTEST_DIR
  • Loading branch information
bluca committed Aug 3, 2017
2 parents 8551a11 + 99fa71c commit 2e29b66
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 38 deletions.
58 changes: 51 additions & 7 deletions src/zauth.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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__)
Expand Down
59 changes: 50 additions & 9 deletions src/zcert.c
Expand Up @@ -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 ();
Expand All @@ -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));
Expand All @@ -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
Expand Down
47 changes: 42 additions & 5 deletions src/zcertstore.c
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 45 additions & 8 deletions src/zconfig.c
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e29b66

Please sign in to comment.