mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-10 18:28:10 +09:00
tests: Add knownhosts test for detecting conflics
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
@@ -33,6 +33,8 @@
|
|||||||
|
|
||||||
#define TORTURE_KNOWN_HOSTS_FILE "libssh_torture_knownhosts"
|
#define TORTURE_KNOWN_HOSTS_FILE "libssh_torture_knownhosts"
|
||||||
|
|
||||||
|
#define BAD_ED25519 "AAAAC3NzaC1lZDI1NTE5AAAAIE74wHmKKkrxpW/dZ69pKPlMoWG9VvWfrNnUkWRQqaDa"
|
||||||
|
|
||||||
static int sshd_group_setup(void **state)
|
static int sshd_group_setup(void **state)
|
||||||
{
|
{
|
||||||
torture_setup_sshd_server(state);
|
torture_setup_sshd_server(state);
|
||||||
@@ -283,6 +285,71 @@ static void torture_knownhosts_unknown(void **state)
|
|||||||
/* session will be freed by session_teardown() */
|
/* session will be freed by session_teardown() */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void torture_knownhosts_conflict(void **state)
|
||||||
|
{
|
||||||
|
struct torture_state *s = *state;
|
||||||
|
ssh_session session = s->ssh.session;
|
||||||
|
char known_hosts_file[1024] = {0};
|
||||||
|
enum ssh_known_hosts_e found;
|
||||||
|
FILE *file = NULL;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
snprintf(known_hosts_file,
|
||||||
|
sizeof(known_hosts_file),
|
||||||
|
"%s/%s",
|
||||||
|
s->socket_dir,
|
||||||
|
TORTURE_KNOWN_HOSTS_FILE);
|
||||||
|
|
||||||
|
file = fopen(known_hosts_file, "w");
|
||||||
|
assert_non_null(file);
|
||||||
|
fprintf(file,
|
||||||
|
"127.0.0.10 %s %s\n",
|
||||||
|
"ssh-ed25519",
|
||||||
|
BAD_ED25519);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519");
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
rc = ssh_connect(session);
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
found = ssh_session_is_known_server(session);
|
||||||
|
assert_int_equal(found, SSH_KNOWN_HOSTS_CHANGED);
|
||||||
|
|
||||||
|
rc = ssh_session_update_known_hosts(session);
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
ssh_disconnect(session);
|
||||||
|
ssh_free(session);
|
||||||
|
|
||||||
|
/* connect again and check host key */
|
||||||
|
session = ssh_new();
|
||||||
|
assert_non_null(session);
|
||||||
|
|
||||||
|
s->ssh.session = session;
|
||||||
|
|
||||||
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519");
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
rc = ssh_connect(session);
|
||||||
|
assert_ssh_return_code(session, rc);
|
||||||
|
|
||||||
|
found = ssh_session_is_known_server(session);
|
||||||
|
assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
|
||||||
|
|
||||||
|
/* session will be freed by session_teardown() */
|
||||||
|
}
|
||||||
|
|
||||||
int torture_run_tests(void) {
|
int torture_run_tests(void) {
|
||||||
int rc;
|
int rc;
|
||||||
struct CMUnitTest tests[] = {
|
struct CMUnitTest tests[] = {
|
||||||
@@ -301,6 +368,9 @@ int torture_run_tests(void) {
|
|||||||
cmocka_unit_test_setup_teardown(torture_knownhosts_unknown,
|
cmocka_unit_test_setup_teardown(torture_knownhosts_unknown,
|
||||||
session_setup,
|
session_setup,
|
||||||
session_teardown),
|
session_teardown),
|
||||||
|
cmocka_unit_test_setup_teardown(torture_knownhosts_conflict,
|
||||||
|
session_setup,
|
||||||
|
session_teardown),
|
||||||
};
|
};
|
||||||
|
|
||||||
ssh_init();
|
ssh_init();
|
||||||
|
|||||||
Reference in New Issue
Block a user