aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2019-09-25 12:02:45 +0200
committerAndreas Schneider <asn@cryptomilk.org>2019-10-25 15:15:20 +0200
commitd4fe8e1f62814287448bb0a042933e3fd3e07e0a (patch)
treee31f5fb4d1322489daed396fcff6262995110245
parente9e8292370ca1090fbb16a4c55d590c88c9d41dd (diff)
downloadlibssh-d4fe8e1f62814287448bb0a042933e3fd3e07e0a.tar.gz
libssh-d4fe8e1f62814287448bb0a042933e3fd3e07e0a.tar.xz
libssh-d4fe8e1f62814287448bb0a042933e3fd3e07e0a.zip
tests: Use temporary file for known_hosts
Previously, the tests were sharing the same file path to create the known_hosts file, which can create a race condition if the tests run in parallel. Such file was deleted between tests. By using different different files for each test, the risk of race conditions is eliminated. Moreover, it makes unnecessary to destroy the file between tests. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--tests/client/torture_knownhosts.c120
-rw-r--r--tests/client/torture_knownhosts_verify.c91
2 files changed, 136 insertions, 75 deletions
diff --git a/tests/client/torture_knownhosts.c b/tests/client/torture_knownhosts.c
index d075998e..c0b3ee18 100644
--- a/tests/client/torture_knownhosts.c
+++ b/tests/client/torture_knownhosts.c
@@ -33,7 +33,7 @@
#include "session.c"
#include "known_hosts.c"
-#define TORTURE_KNOWN_HOSTS_FILE "libssh_torture_knownhosts"
+#define TMP_FILE_TEMPLATE "known_hosts_XXXXXX"
#define BADRSA "AAAAB3NzaC1yc2EAAAADAQABAAABAQChm5" \
"a6Av65O8cKtx5YXOnui3wJnYE6A6J/I4kZSAibbn14Jcl+34VJQwv96f25AxNmo" \
@@ -62,6 +62,7 @@ static int session_setup(void **state)
struct torture_state *s = *state;
int verbosity = torture_libssh_verbosity();
struct passwd *pwd;
+ bool process_config = false;
int rc;
pwd = getpwnam("bob");
@@ -74,8 +75,9 @@ static int session_setup(void **state)
assert_non_null(s->ssh.session);
ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
+ ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG,
+ &process_config);
ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
-
ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
return 0;
@@ -84,19 +86,10 @@ static int session_setup(void **state)
static int session_teardown(void **state)
{
struct torture_state *s = *state;
- char known_hosts_file[1024];
-
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
- "%s/%s",
- s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
ssh_disconnect(s->ssh.session);
ssh_free(s->ssh.session);
- unlink(known_hosts_file);
-
return 0;
}
@@ -104,17 +97,21 @@ static int session_teardown(void **state)
static void torture_knownhosts_port(void **state) {
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
char buffer[200];
char *p;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
@@ -142,6 +139,7 @@ static void torture_knownhosts_port(void **state) {
ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
+ free(known_hosts_file);
rc = ssh_connect(session);
assert_ssh_return_code(session, rc);
@@ -155,16 +153,20 @@ static void torture_knownhosts_wildcard(void **state)
{
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
const char *key = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
file = fopen(known_hosts_file, "w");
assert_non_null(file);
@@ -176,6 +178,7 @@ static void torture_knownhosts_wildcard(void **state)
assert_ssh_return_code(session, rc);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
+ free(known_hosts_file);
rc = ssh_connect(session);
assert_ssh_return_code(session, rc);
@@ -188,16 +191,20 @@ static void torture_knownhosts_standard_port(void **state)
{
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
const char *key = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
file = fopen(known_hosts_file, "w");
assert_non_null(file);
@@ -209,6 +216,7 @@ static void torture_knownhosts_standard_port(void **state)
assert_ssh_return_code(session, rc);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
+ free(known_hosts_file);
rc = ssh_connect(session);
assert_ssh_return_code(session, rc);
@@ -220,15 +228,19 @@ static void torture_knownhosts_standard_port(void **state)
static void torture_knownhosts_fail(void **state) {
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
@@ -238,6 +250,7 @@ static void torture_knownhosts_fail(void **state) {
file = fopen(known_hosts_file, "w");
assert_non_null(file);
+ free(known_hosts_file);
fprintf(file, "127.0.0.10 ssh-rsa %s\n", BADRSA);
fclose(file);
@@ -252,15 +265,19 @@ static void torture_knownhosts_fail(void **state) {
static void torture_knownhosts_other(void **state) {
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
@@ -270,6 +287,8 @@ static void torture_knownhosts_other(void **state) {
file = fopen(known_hosts_file, "w");
assert_non_null(file);
+ free(known_hosts_file);
+
fprintf(file, "127.0.0.10 ssh-rsa %s\n", BADRSA);
fclose(file);
@@ -283,14 +302,18 @@ static void torture_knownhosts_other(void **state) {
static void torture_knownhosts_other_auto(void **state) {
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
assert_ssh_return_code(session, rc);
@@ -333,20 +356,25 @@ static void torture_knownhosts_other_auto(void **state) {
assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
/* session will be freed by session_teardown() */
+ free(known_hosts_file);
}
static void torture_knownhosts_conflict(void **state) {
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024];
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
assert_ssh_return_code(session, rc);
@@ -393,6 +421,7 @@ static void torture_knownhosts_conflict(void **state) {
assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
/* session will be freed by session_teardown() */
+ free(known_hosts_file);
}
static void torture_knownhosts_no_hostkeychecking(void **state)
@@ -400,19 +429,24 @@ static void torture_knownhosts_no_hostkeychecking(void **state)
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024] = {0};
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
enum ssh_known_hosts_e found;
int strict_host_key_checking = 0;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
+ free(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ecdsa-sha2-nistp521");
assert_ssh_return_code(session, rc);
diff --git a/tests/client/torture_knownhosts_verify.c b/tests/client/torture_knownhosts_verify.c
index 2a2a6b64..85963345 100644
--- a/tests/client/torture_knownhosts_verify.c
+++ b/tests/client/torture_knownhosts_verify.c
@@ -31,9 +31,15 @@
#include "knownhosts.c"
-#define TORTURE_KNOWN_HOSTS_FILE "libssh_torture_knownhosts"
+#define TMP_FILE_TEMPLATE "known_hosts_XXXXXX"
-#define BAD_RSA "AAAAB3NzaC1yc2EAAAADAQABAAABAQDXvXuawzaArEwkLIXTz/EWywLOCtqQL3P9yKkrhz6AplXP2PhOh5pyxa1VfGKe453jNeYBJ0ROto3BshXgZXbo86oLXTkbe0gO5xi3r5WjXxjOFvRRTLot5fPLNDOv9+TnsPmkNn0iIeyPnfrcPIyjWt5zSWUfkNC8oNHxsiSshjpbJvTXSDipukpUy41d7jg4uWGuonMTF7yu7HfuHqq7lhb0WlwSpfbqAbfYARBddcdcARyhix4RMWZZqVY20H3Vsjq8bjKC+NJXFce1PRg+qcOWQdlXEei4dkzAvHvfQRx1TjzkrBZ6B6thmZtyeb9IsiB0tg2g0JN2VTAGkxqp"
+#define BAD_RSA "AAAAB3NzaC1yc2EAAAADAQABAAABAQDXvXuawzaArEwkLIXTz/EWywLOC" \
+ "tqQL3P9yKkrhz6AplXP2PhOh5pyxa1VfGKe453jNeYBJ0ROto3BshXgZX" \
+ "bo86oLXTkbe0gO5xi3r5WjXxjOFvRRTLot5fPLNDOv9+TnsPmkNn0iIey" \
+ "PnfrcPIyjWt5zSWUfkNC8oNHxsiSshjpbJvTXSDipukpUy41d7jg4uWGu" \
+ "onMTF7yu7HfuHqq7lhb0WlwSpfbqAbfYARBddcdcARyhix4RMWZZqVY20" \
+ "H3Vsjq8bjKC+NJXFce1PRg+qcOWQdlXEei4dkzAvHvfQRx1TjzkrBZ6B6" \
+ "thmZtyeb9IsiB0tg2g0JN2VTAGkxqp"
const char template[] = "temp_dir_XXXXXX";
@@ -57,6 +63,8 @@ static int session_setup(void **state)
struct passwd *pwd;
int rc;
+ bool process_config = false;
+
pwd = getpwnam("bob");
assert_non_null(pwd);
@@ -69,6 +77,10 @@ static int session_setup(void **state)
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
assert_ssh_return_code(s->ssh.session, rc);
+ rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG,
+ &process_config);
+ assert_ssh_return_code(s->ssh.session, rc);
+
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
assert_ssh_return_code(s->ssh.session, rc);
@@ -83,19 +95,9 @@ static int session_setup(void **state)
static int session_teardown(void **state)
{
struct torture_state *s = *state;
- char known_hosts_file[1024];
-
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
- "%s/%s",
- s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
-
ssh_disconnect(s->ssh.session);
ssh_free(s->ssh.session);
- unlink(known_hosts_file);
-
return 0;
}
@@ -149,15 +151,19 @@ static void torture_knownhosts_precheck(void **state)
struct ssh_iterator *it = NULL;
size_t algo_count;
const char *algo = NULL;
- char known_hosts_file[1024] = {0};
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
file = fopen(known_hosts_file, "w");
assert_non_null(file);
@@ -177,6 +183,7 @@ static void torture_knownhosts_precheck(void **state)
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
+ free(known_hosts_file);
algo_list = ssh_known_hosts_get_algorithms(session);
assert_non_null(algo_list);
@@ -214,15 +221,19 @@ static void torture_knownhosts_duplicate(void **state)
struct ssh_iterator *it = NULL;
size_t algo_count;
const char *algo = NULL;
- char known_hosts_file[1024] = {0};
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
FILE *file;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
file = fopen(known_hosts_file, "w");
assert_non_null(file);
@@ -242,6 +253,7 @@ static void torture_knownhosts_duplicate(void **state)
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
+ free(known_hosts_file);
algo_list = ssh_known_hosts_get_algorithms(session);
assert_non_null(algo_list);
@@ -261,16 +273,20 @@ static void torture_knownhosts_other(void **state)
{
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024] = {0};
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
enum ssh_known_hosts_e found;
FILE *file = NULL;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
@@ -284,6 +300,7 @@ static void torture_knownhosts_other(void **state)
"127.0.0.10 %s\n",
torture_get_testkey_pub(SSH_KEYTYPE_RSA));
fclose(file);
+ free(known_hosts_file);
rc = ssh_connect(session);
assert_ssh_return_code(session, rc);
@@ -296,15 +313,19 @@ static void torture_knownhosts_unknown(void **state)
{
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024] = {0};
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
enum ssh_known_hosts_e found;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
assert_ssh_return_code(session, rc);
@@ -349,22 +370,27 @@ static void torture_knownhosts_unknown(void **state)
assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
/* session will be freed by session_teardown() */
+ free(known_hosts_file);
}
static void torture_knownhosts_conflict(void **state)
{
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
- char known_hosts_file[1024] = {0};
+ char tmp_file[1024] = {0};
+ char *known_hosts_file = NULL;
enum ssh_known_hosts_e found;
FILE *file = NULL;
int rc;
- snprintf(known_hosts_file,
- sizeof(known_hosts_file),
+ snprintf(tmp_file,
+ sizeof(tmp_file),
"%s/%s",
s->socket_dir,
- TORTURE_KNOWN_HOSTS_FILE);
+ TMP_FILE_TEMPLATE);
+
+ known_hosts_file = torture_create_temp_file(tmp_file);
+ assert_non_null(known_hosts_file);
file = fopen(known_hosts_file, "w");
assert_non_null(file);
@@ -414,6 +440,7 @@ static void torture_knownhosts_conflict(void **state)
assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
/* session will be freed by session_teardown() */
+ free(known_hosts_file);
}
static void torture_knownhosts_new_file(void **state)